1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include "openglrenderer.h"
52#include <QQuickItem>
53
54#if QT_CONFIG(opengl)
55
56#include <QOpenGLShaderProgram>
57#include <QOpenGLBuffer>
58#include <QOpenGLFunctions>
59
60//! [1]
61OpenGLRenderNode::~OpenGLRenderNode()
62{
63 releaseResources();
64}
65
66void OpenGLRenderNode::releaseResources()
67{
68 delete m_program;
69 m_program = nullptr;
70 delete m_vbo;
71 m_vbo = nullptr;
72}
73//! [1]
74
75void OpenGLRenderNode::init()
76{
77 m_program = new QOpenGLShaderProgram;
78
79 static const char *vertexShaderSource =
80 "attribute highp vec4 posAttr;\n"
81 "attribute lowp vec4 colAttr;\n"
82 "varying lowp vec4 col;\n"
83 "uniform highp mat4 matrix;\n"
84 "void main() {\n"
85 " col = colAttr;\n"
86 " gl_Position = matrix * posAttr;\n"
87 "}\n";
88
89 static const char *fragmentShaderSource =
90 "varying lowp vec4 col;\n"
91 "uniform lowp float opacity;\n"
92 "void main() {\n"
93 " gl_FragColor = col * opacity;\n"
94 "}\n";
95
96 m_program->addCacheableShaderFromSourceCode(type: QOpenGLShader::Vertex, source: vertexShaderSource);
97 m_program->addCacheableShaderFromSourceCode(type: QOpenGLShader::Fragment, source: fragmentShaderSource);
98 m_program->bindAttributeLocation(name: "posAttr", location: 0);
99 m_program->bindAttributeLocation(name: "colAttr", location: 1);
100 m_program->link();
101
102 m_matrixUniform = m_program->uniformLocation(name: "matrix");
103 m_opacityUniform = m_program->uniformLocation(name: "opacity");
104
105 const int VERTEX_SIZE = 6 * sizeof(GLfloat);
106
107 static GLfloat colors[] = {
108 1.0f, 0.0f, 0.0f,
109 0.0f, 1.0f, 0.0f,
110 0.0f, 0.0f, 1.0f
111 };
112
113 m_vbo = new QOpenGLBuffer;
114 m_vbo->create();
115 m_vbo->bind();
116 m_vbo->allocate(count: VERTEX_SIZE + sizeof(colors));
117 m_vbo->write(offset: VERTEX_SIZE, data: colors, count: sizeof(colors));
118 m_vbo->release();
119}
120
121//! [2]
122void OpenGLRenderNode::render(const RenderState *state)
123{
124 if (!m_program)
125 init();
126
127 QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
128 m_program->bind();
129 m_program->setUniformValue(location: m_matrixUniform, value: *state->projectionMatrix() * *matrix());
130 m_program->setUniformValue(location: m_opacityUniform, value: float(inheritedOpacity()));
131//! [2]
132
133 m_vbo->bind();
134
135//! [5]
136 QPointF p0(m_width - 1, m_height - 1);
137 QPointF p1(0, 0);
138 QPointF p2(0, m_height - 1);
139
140 GLfloat vertices[6] = { GLfloat(p0.x()), GLfloat(p0.y()),
141 GLfloat(p1.x()), GLfloat(p1.y()),
142 GLfloat(p2.x()), GLfloat(p2.y()) };
143 m_vbo->write(offset: 0, data: vertices, count: sizeof(vertices));
144//! [5]
145
146 m_program->setAttributeBuffer(location: 0, GL_FLOAT, offset: 0, tupleSize: 2);
147 m_program->setAttributeBuffer(location: 1, GL_FLOAT, offset: sizeof(vertices), tupleSize: 3);
148 m_program->enableAttributeArray(location: 0);
149 m_program->enableAttributeArray(location: 1);
150
151 // We are prepared both for the legacy (direct OpenGL) and the modern
152 // (abstracted by RHI) OpenGL scenegraph. So set all the states that are
153 // important to us.
154
155 //! [3]
156 f->glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
157
158 f->glEnable(GL_BLEND);
159 f->glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
160
161 // Clip support.
162 if (state->scissorEnabled()) {
163 f->glEnable(GL_SCISSOR_TEST);
164 const QRect r = state->scissorRect(); // already bottom-up
165 f->glScissor(x: r.x(), y: r.y(), width: r.width(), height: r.height());
166 }
167 if (state->stencilEnabled()) {
168 f->glEnable(GL_STENCIL_TEST);
169 f->glStencilFunc(GL_EQUAL, ref: state->stencilValue(), mask: 0xFF);
170 f->glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
171 }
172
173 f->glDrawArrays(GL_TRIANGLES, first: 0, count: 3);
174 //! [3]
175}
176
177//! [4]
178QSGRenderNode::StateFlags OpenGLRenderNode::changedStates() const
179{
180 return BlendState | ScissorState | StencilState;
181}
182
183QSGRenderNode::RenderingFlags OpenGLRenderNode::flags() const
184{
185 return BoundedRectRendering | DepthAwareRendering;
186}
187
188QRectF OpenGLRenderNode::rect() const
189{
190 return QRect(0, 0, m_width, m_height);
191}
192//! [4]
193
194void OpenGLRenderNode::sync(QQuickItem *item)
195{
196 m_width = item->width();
197 m_height = item->height();
198}
199
200#endif // opengl
201

source code of qtdeclarative/examples/quick/scenegraph/rendernode/openglrenderer.cpp