1/****************************************************************************
2**
3** Copyright (C) 2015 Lorenz Esch (TU Ilmenau).
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt3D module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
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** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qpervertexcolormaterial.h"
41#include "qpervertexcolormaterial_p.h"
42
43#include <Qt3DRender/qfilterkey.h>
44#include <Qt3DRender/qmaterial.h>
45#include <Qt3DRender/qeffect.h>
46#include <Qt3DRender/qtechnique.h>
47#include <Qt3DRender/qshaderprogram.h>
48#include <Qt3DRender/qparameter.h>
49#include <Qt3DRender/qrenderpass.h>
50#include <Qt3DRender/qgraphicsapifilter.h>
51#include <QtCore/QUrl>
52#include <QtGui/QVector3D>
53#include <QtGui/QVector4D>
54
55QT_BEGIN_NAMESPACE
56
57using namespace Qt3DRender;
58
59namespace Qt3DExtras {
60
61QPerVertexColorMaterialPrivate::QPerVertexColorMaterialPrivate()
62 : QMaterialPrivate()
63 , m_vertexEffect(new QEffect())
64 , m_vertexGL3Technique(new QTechnique())
65 , m_vertexGL2Technique(new QTechnique())
66 , m_vertexES2Technique(new QTechnique())
67 , m_vertexRHITechnique(new QTechnique())
68 , m_vertexGL3RenderPass(new QRenderPass())
69 , m_vertexGL2RenderPass(new QRenderPass())
70 , m_vertexES2RenderPass(new QRenderPass())
71 , m_vertexRHIRenderPass(new QRenderPass())
72 , m_vertexGL3Shader(new QShaderProgram())
73 , m_vertexGL2ES2Shader(new QShaderProgram())
74 , m_vertexRHIShader(new QShaderProgram())
75 , m_filterKey(new QFilterKey)
76{
77}
78
79/*!
80 \class Qt3DExtras::QPerVertexColorMaterial
81 \ingroup qt3d-extras-materials
82 \brief The QPerVertexColorMaterial class provides a default implementation for rendering the
83 color properties set for each vertex.
84 \inmodule Qt3DExtras
85 \since 5.7
86 \inherits Qt3DRender::QMaterial
87
88 This lighting effect is based on the combination of 2 lighting components ambient and diffuse.
89 Ambient is set by the vertex color.
90 Diffuse takes in account the normal distribution of each vertex.
91
92 \list
93 \li Ambient is the color that is emitted by an object without any other light source.
94 \li Diffuse is the color that is emitted for rough surface reflections with the lights
95 \endlist
96
97 This material uses an effect with a single render pass approach and forms fragment lighting.
98 Techniques are provided for OpenGL 2, OpenGL 3 or above as well as OpenGL ES 2.
99*/
100
101/*!
102 Constructs a new QPerVertexColorMaterial instance with parent object \a parent.
103*/
104QPerVertexColorMaterial::QPerVertexColorMaterial(QNode *parent)
105 : QMaterial(*new QPerVertexColorMaterialPrivate, parent)
106{
107 Q_D(QPerVertexColorMaterial);
108 d->init();
109}
110
111/*!
112 Destroys the QPerVertexColorMaterial
113*/
114QPerVertexColorMaterial::~QPerVertexColorMaterial()
115{
116}
117
118// TODO: Define how lights are proties are set in the shaders. Ideally using a QShaderData
119void QPerVertexColorMaterialPrivate::init()
120{
121 m_vertexGL3Shader->setVertexShaderCode(QShaderProgram::loadSource(sourceUrl: QUrl(QStringLiteral("qrc:/shaders/gl3/pervertexcolor.vert"))));
122 m_vertexGL3Shader->setFragmentShaderCode(QShaderProgram::loadSource(sourceUrl: QUrl(QStringLiteral("qrc:/shaders/gl3/pervertexcolor.frag"))));
123 m_vertexGL2ES2Shader->setVertexShaderCode(QShaderProgram::loadSource(sourceUrl: QUrl(QStringLiteral("qrc:/shaders/es2/pervertexcolor.vert"))));
124 m_vertexGL2ES2Shader->setFragmentShaderCode(QShaderProgram::loadSource(sourceUrl: QUrl(QStringLiteral("qrc:/shaders/es2/pervertexcolor.frag"))));
125 m_vertexRHIShader->setVertexShaderCode(QShaderProgram::loadSource(sourceUrl: QUrl(QStringLiteral("qrc:/shaders/rhi/pervertexcolor.vert"))));
126 m_vertexRHIShader->setFragmentShaderCode(QShaderProgram::loadSource(sourceUrl: QUrl(QStringLiteral("qrc:/shaders/rhi/pervertexcolor.frag"))));
127
128 m_vertexGL3Technique->graphicsApiFilter()->setApi(QGraphicsApiFilter::OpenGL);
129 m_vertexGL3Technique->graphicsApiFilter()->setMajorVersion(3);
130 m_vertexGL3Technique->graphicsApiFilter()->setMinorVersion(1);
131 m_vertexGL3Technique->graphicsApiFilter()->setProfile(QGraphicsApiFilter::CoreProfile);
132
133 m_vertexGL2Technique->graphicsApiFilter()->setApi(QGraphicsApiFilter::OpenGL);
134 m_vertexGL2Technique->graphicsApiFilter()->setMajorVersion(2);
135 m_vertexGL2Technique->graphicsApiFilter()->setMinorVersion(0);
136 m_vertexGL2Technique->graphicsApiFilter()->setProfile(QGraphicsApiFilter::NoProfile);
137
138 m_vertexES2Technique->graphicsApiFilter()->setApi(QGraphicsApiFilter::OpenGLES);
139 m_vertexES2Technique->graphicsApiFilter()->setMajorVersion(2);
140 m_vertexES2Technique->graphicsApiFilter()->setMinorVersion(0);
141 m_vertexES2Technique->graphicsApiFilter()->setProfile(QGraphicsApiFilter::NoProfile);
142
143 m_vertexRHITechnique->graphicsApiFilter()->setApi(QGraphicsApiFilter::RHI);
144 m_vertexRHITechnique->graphicsApiFilter()->setMajorVersion(1);
145 m_vertexRHITechnique->graphicsApiFilter()->setMinorVersion(0);
146
147 Q_Q(QPerVertexColorMaterial);
148 m_filterKey->setParent(q);
149 m_filterKey->setName(QStringLiteral("renderingStyle"));
150 m_filterKey->setValue(QStringLiteral("forward"));
151
152 m_vertexGL3Technique->addFilterKey(filterKey: m_filterKey);
153 m_vertexGL2Technique->addFilterKey(filterKey: m_filterKey);
154 m_vertexES2Technique->addFilterKey(filterKey: m_filterKey);
155 m_vertexRHITechnique->addFilterKey(filterKey: m_filterKey);
156
157 m_vertexGL3RenderPass->setShaderProgram(m_vertexGL3Shader);
158 m_vertexGL2RenderPass->setShaderProgram(m_vertexGL2ES2Shader);
159 m_vertexES2RenderPass->setShaderProgram(m_vertexGL2ES2Shader);
160 m_vertexRHIRenderPass->setShaderProgram(m_vertexRHIShader);
161
162 m_vertexGL3Technique->addRenderPass(pass: m_vertexGL3RenderPass);
163 m_vertexGL2Technique->addRenderPass(pass: m_vertexGL2RenderPass);
164 m_vertexES2Technique->addRenderPass(pass: m_vertexES2RenderPass);
165 m_vertexRHITechnique->addRenderPass(pass: m_vertexRHIRenderPass);
166
167 m_vertexEffect->addTechnique(t: m_vertexGL3Technique);
168 m_vertexEffect->addTechnique(t: m_vertexGL2Technique);
169 m_vertexEffect->addTechnique(t: m_vertexES2Technique);
170 m_vertexEffect->addTechnique(t: m_vertexRHITechnique);
171
172 q->setEffect(m_vertexEffect);
173}
174
175} // namespace Qt3DExtras
176
177QT_END_NAMESPACE
178

source code of qt3d/src/extras/defaults/qpervertexcolormaterial.cpp