|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt3D module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "painter.h" |
|
43 #include "light.h" |
|
44 #include "lightmodel.h" |
|
45 #include "material.h" |
|
46 #include <QtOpenGL/qglshaderprogram.h> |
|
47 |
|
48 ShaderPainter::ShaderPainter() |
|
49 { |
|
50 materialProgram = new QGLShaderProgram(); |
|
51 materialProgram->addShaderFromSourceFile |
|
52 (QGLShader::Vertex, QLatin1String(":/lighting.vsh")); |
|
53 materialProgram->addShaderFromSourceFile |
|
54 (QGLShader::Fragment, QLatin1String(":/material.fsh")); |
|
55 materialProgram->bindAttributeLocation("vertex", 0); |
|
56 materialProgram->bindAttributeLocation("normal", 1); |
|
57 materialProgram->bindAttributeLocation("texcoord", 2); |
|
58 materialProgram->link(); |
|
59 |
|
60 textureProgram = new QGLShaderProgram(); |
|
61 textureProgram->addShaderFromSourceFile |
|
62 (QGLShader::Vertex, QLatin1String(":/lighting.vsh")); |
|
63 textureProgram->addShaderFromSourceFile |
|
64 (QGLShader::Fragment, QLatin1String(":/texture.fsh")); |
|
65 textureProgram->bindAttributeLocation("vertex", 0); |
|
66 textureProgram->bindAttributeLocation("normal", 1); |
|
67 textureProgram->bindAttributeLocation("texcoord", 2); |
|
68 textureProgram->link(); |
|
69 |
|
70 currentProgram = 0; |
|
71 matricesChanged = false; |
|
72 } |
|
73 |
|
74 ShaderPainter::~ShaderPainter() |
|
75 { |
|
76 delete materialProgram; |
|
77 delete textureProgram; |
|
78 } |
|
79 |
|
80 void ShaderPainter::setMatrices(const QMatrix4x4 &mv, const QMatrix4x4 &proj) |
|
81 { |
|
82 combinedMatrix = proj * mv; |
|
83 modelViewMatrix = mv; |
|
84 normalMatrix = mv.normalMatrix(); |
|
85 matricesChanged = true; |
|
86 } |
|
87 |
|
88 void ShaderPainter::selectMaterial(Material *material) |
|
89 { |
|
90 if (currentProgram != materialProgram) { |
|
91 materialProgram->bind(); |
|
92 materialProgram->enableAttributeArray(0); |
|
93 materialProgram->enableAttributeArray(1); |
|
94 materialProgram->disableAttributeArray(2); |
|
95 currentProgram = materialProgram; |
|
96 matricesChanged = true; |
|
97 } |
|
98 if (matricesChanged) { |
|
99 materialProgram->setUniformValue("matrix", combinedMatrix); |
|
100 materialProgram->setUniformValue("modelView", modelViewMatrix); |
|
101 materialProgram->setUniformValue("normalMatrix", normalMatrix); |
|
102 matricesChanged = false; |
|
103 } |
|
104 updateMaterials(materialProgram, material); |
|
105 } |
|
106 |
|
107 void ShaderPainter::selectTexturedMaterial(Material *material) |
|
108 { |
|
109 if (currentProgram != textureProgram) { |
|
110 textureProgram->bind(); |
|
111 textureProgram->enableAttributeArray(0); |
|
112 textureProgram->enableAttributeArray(1); |
|
113 textureProgram->enableAttributeArray(2); |
|
114 textureProgram->setUniformValue("tex", 0); |
|
115 currentProgram = textureProgram; |
|
116 matricesChanged = true; |
|
117 } |
|
118 if (matricesChanged) { |
|
119 textureProgram->setUniformValue("matrix", combinedMatrix); |
|
120 textureProgram->setUniformValue("modelView", modelViewMatrix); |
|
121 textureProgram->setUniformValue("normalMatrix", normalMatrix); |
|
122 matricesChanged = false; |
|
123 } |
|
124 updateMaterials(textureProgram, material); |
|
125 } |
|
126 |
|
127 void ShaderPainter::setVertices(const float *array, int stride) |
|
128 { |
|
129 // Doesn't matter which program we use - they have the same locations. |
|
130 materialProgram->setAttributeArray(0, array, 3, stride * sizeof(float)); |
|
131 } |
|
132 |
|
133 void ShaderPainter::setTexCoords(const float *array, int stride) |
|
134 { |
|
135 materialProgram->setAttributeArray(2, array, 2, stride * sizeof(float)); |
|
136 } |
|
137 |
|
138 void ShaderPainter::setNormals(const float *array, int stride) |
|
139 { |
|
140 materialProgram->setAttributeArray(1, array, 3, stride * sizeof(float)); |
|
141 } |
|
142 |
|
143 void ShaderPainter::updateMaterials |
|
144 (QGLShaderProgram *program, Material *material) |
|
145 { |
|
146 // Set the uniform variables for the light. |
|
147 QVector4D pli = light->eyePosition(QMatrix4x4()); |
|
148 program->setUniformValue("pli", QVector3D(pli.x(), pli.y(), pli.z())); |
|
149 program->setUniformValue("pliw", GLfloat(pli.w())); |
|
150 |
|
151 // Set the uniform variables for the light model. |
|
152 program->setUniformValue("viewerAtInfinity", (int)(lightModel->viewerPosition() == LightModel::ViewerAtInfinity)); |
|
153 program->setUniformValue("acs", lightModel->ambientSceneColor()); |
|
154 |
|
155 // Set the uniform variables for the material. |
|
156 program->setUniformValue("acm", material->ambientColor()); |
|
157 program->setUniformValue("dcm", material->diffuseColor()); |
|
158 program->setUniformValue("scm", material->specularColor()); |
|
159 program->setUniformValue("srm", (float)(material->shininess())); |
|
160 } |