1// Copyright (C) 2015 Lorenz Esch (TU Ilmenau).
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qpervertexcolormaterial.h"
5#include "qpervertexcolormaterial_p.h"
6
7#include <Qt3DRender/qfilterkey.h>
8#include <Qt3DRender/qmaterial.h>
9#include <Qt3DRender/qeffect.h>
10#include <Qt3DRender/qtechnique.h>
11#include <Qt3DRender/qshaderprogram.h>
12#include <Qt3DRender/qparameter.h>
13#include <Qt3DRender/qrenderpass.h>
14#include <Qt3DRender/qgraphicsapifilter.h>
15#include <QtCore/QUrl>
16#include <QtGui/QVector3D>
17#include <QtGui/QVector4D>
18
19QT_BEGIN_NAMESPACE
20
21using namespace Qt3DRender;
22
23namespace Qt3DExtras {
24
25QPerVertexColorMaterialPrivate::QPerVertexColorMaterialPrivate()
26 : QMaterialPrivate()
27 , m_vertexEffect(new QEffect())
28 , m_vertexGL3Technique(new QTechnique())
29 , m_vertexGL2Technique(new QTechnique())
30 , m_vertexES2Technique(new QTechnique())
31 , m_vertexRHITechnique(new QTechnique())
32 , m_vertexGL3RenderPass(new QRenderPass())
33 , m_vertexGL2RenderPass(new QRenderPass())
34 , m_vertexES2RenderPass(new QRenderPass())
35 , m_vertexRHIRenderPass(new QRenderPass())
36 , m_vertexGL3Shader(new QShaderProgram())
37 , m_vertexGL2ES2Shader(new QShaderProgram())
38 , m_vertexRHIShader(new QShaderProgram())
39 , m_filterKey(new QFilterKey)
40{
41}
42
43/*!
44 \class Qt3DExtras::QPerVertexColorMaterial
45 \ingroup qt3d-extras-materials
46 \brief The QPerVertexColorMaterial class provides a default implementation for rendering the
47 color properties set for each vertex.
48 \inmodule Qt3DExtras
49 \since 5.7
50 \inherits Qt3DRender::QMaterial
51
52 This lighting effect is based on the combination of 2 lighting components ambient and diffuse.
53 Ambient is set by the vertex color.
54 Diffuse takes in account the normal distribution of each vertex.
55
56 \list
57 \li Ambient is the color that is emitted by an object without any other light source.
58 \li Diffuse is the color that is emitted for rough surface reflections with the lights
59 \endlist
60
61 This material uses an effect with a single render pass approach and forms fragment lighting.
62 Techniques are provided for OpenGL 2, OpenGL 3 or above as well as OpenGL ES 2.
63*/
64
65/*!
66 Constructs a new QPerVertexColorMaterial instance with parent object \a parent.
67*/
68QPerVertexColorMaterial::QPerVertexColorMaterial(QNode *parent)
69 : QMaterial(*new QPerVertexColorMaterialPrivate, parent)
70{
71 Q_D(QPerVertexColorMaterial);
72 d->init();
73}
74
75/*!
76 Destroys the QPerVertexColorMaterial
77*/
78QPerVertexColorMaterial::~QPerVertexColorMaterial()
79{
80}
81
82// TODO: Define how lights are proties are set in the shaders. Ideally using a QShaderData
83void QPerVertexColorMaterialPrivate::init()
84{
85 m_vertexGL3Shader->setVertexShaderCode(QShaderProgram::loadSource(sourceUrl: QUrl(QStringLiteral("qrc:/shaders/gl3/pervertexcolor.vert"))));
86 m_vertexGL3Shader->setFragmentShaderCode(QShaderProgram::loadSource(sourceUrl: QUrl(QStringLiteral("qrc:/shaders/gl3/pervertexcolor.frag"))));
87 m_vertexGL2ES2Shader->setVertexShaderCode(QShaderProgram::loadSource(sourceUrl: QUrl(QStringLiteral("qrc:/shaders/es2/pervertexcolor.vert"))));
88 m_vertexGL2ES2Shader->setFragmentShaderCode(QShaderProgram::loadSource(sourceUrl: QUrl(QStringLiteral("qrc:/shaders/es2/pervertexcolor.frag"))));
89 m_vertexRHIShader->setVertexShaderCode(QShaderProgram::loadSource(sourceUrl: QUrl(QStringLiteral("qrc:/shaders/rhi/pervertexcolor.vert"))));
90 m_vertexRHIShader->setFragmentShaderCode(QShaderProgram::loadSource(sourceUrl: QUrl(QStringLiteral("qrc:/shaders/rhi/pervertexcolor.frag"))));
91
92 m_vertexGL3Technique->graphicsApiFilter()->setApi(QGraphicsApiFilter::OpenGL);
93 m_vertexGL3Technique->graphicsApiFilter()->setMajorVersion(3);
94 m_vertexGL3Technique->graphicsApiFilter()->setMinorVersion(1);
95 m_vertexGL3Technique->graphicsApiFilter()->setProfile(QGraphicsApiFilter::CoreProfile);
96
97 m_vertexGL2Technique->graphicsApiFilter()->setApi(QGraphicsApiFilter::OpenGL);
98 m_vertexGL2Technique->graphicsApiFilter()->setMajorVersion(2);
99 m_vertexGL2Technique->graphicsApiFilter()->setMinorVersion(0);
100 m_vertexGL2Technique->graphicsApiFilter()->setProfile(QGraphicsApiFilter::NoProfile);
101
102 m_vertexES2Technique->graphicsApiFilter()->setApi(QGraphicsApiFilter::OpenGLES);
103 m_vertexES2Technique->graphicsApiFilter()->setMajorVersion(2);
104 m_vertexES2Technique->graphicsApiFilter()->setMinorVersion(0);
105 m_vertexES2Technique->graphicsApiFilter()->setProfile(QGraphicsApiFilter::NoProfile);
106
107 m_vertexRHITechnique->graphicsApiFilter()->setApi(QGraphicsApiFilter::RHI);
108 m_vertexRHITechnique->graphicsApiFilter()->setMajorVersion(1);
109 m_vertexRHITechnique->graphicsApiFilter()->setMinorVersion(0);
110
111 Q_Q(QPerVertexColorMaterial);
112 m_filterKey->setParent(q);
113 m_filterKey->setName(QStringLiteral("renderingStyle"));
114 m_filterKey->setValue(QStringLiteral("forward"));
115
116 m_vertexGL3Technique->addFilterKey(filterKey: m_filterKey);
117 m_vertexGL2Technique->addFilterKey(filterKey: m_filterKey);
118 m_vertexES2Technique->addFilterKey(filterKey: m_filterKey);
119 m_vertexRHITechnique->addFilterKey(filterKey: m_filterKey);
120
121 m_vertexGL3RenderPass->setShaderProgram(m_vertexGL3Shader);
122 m_vertexGL2RenderPass->setShaderProgram(m_vertexGL2ES2Shader);
123 m_vertexES2RenderPass->setShaderProgram(m_vertexGL2ES2Shader);
124 m_vertexRHIRenderPass->setShaderProgram(m_vertexRHIShader);
125
126 m_vertexGL3Technique->addRenderPass(pass: m_vertexGL3RenderPass);
127 m_vertexGL2Technique->addRenderPass(pass: m_vertexGL2RenderPass);
128 m_vertexES2Technique->addRenderPass(pass: m_vertexES2RenderPass);
129 m_vertexRHITechnique->addRenderPass(pass: m_vertexRHIRenderPass);
130
131 m_vertexEffect->addTechnique(t: m_vertexGL3Technique);
132 m_vertexEffect->addTechnique(t: m_vertexGL2Technique);
133 m_vertexEffect->addTechnique(t: m_vertexES2Technique);
134 m_vertexEffect->addTechnique(t: m_vertexRHITechnique);
135
136 q->setEffect(m_vertexEffect);
137}
138
139} // namespace Qt3DExtras
140
141QT_END_NAMESPACE
142
143#include "moc_qpervertexcolormaterial.cpp"
144

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