-
Notifications
You must be signed in to change notification settings - Fork 0
/
twosides.cpp
341 lines (273 loc) · 10.4 KB
/
twosides.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include "twosides.h"
#include <QtGlobal>
#include <QDebug>
#include <QFile>
#include <QImage>
#include <QTime>
#include <QVector2D>
#include <QVector3D>
#include <QMatrix4x4>
#include <cmath>
#include <cstring>
MyWindow::~MyWindow()
{
if (mProgramSimpleADS != 0) delete mProgramSimpleADS;
if (mProgram2Sides != 0) delete mProgram2Sides;
}
MyWindow::MyWindow()
: mProgramSimpleADS(0), mProgram2Sides(0), currentTimeMs(0), currentTimeS(0)
{
setSurfaceType(QWindow::OpenGLSurface);
setFlags(Qt::Window | Qt::WindowSystemMenuHint | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
QSurfaceFormat format;
format.setDepthBufferSize(24);
format.setMajorVersion(4);
format.setMinorVersion(3);
format.setSamples(4);
format.setProfile(QSurfaceFormat::CoreProfile);
setFormat(format);
create();
resize(800, 600);
mContext = new QOpenGLContext(this);
mContext->setFormat(format);
mContext->create();
mContext->makeCurrent( this );
mFuncs = mContext->versionFunctions<QOpenGLFunctions_4_3_Core>();
if ( !mFuncs )
{
qWarning( "Could not obtain OpenGL versions object" );
exit( 1 );
}
if (mFuncs->initializeOpenGLFunctions() == GL_FALSE)
{
qWarning( "Could not initialize core open GL functions" );
exit( 1 );
}
initializeOpenGLFunctions();
QTimer *repaintTimer = new QTimer(this);
connect(repaintTimer, &QTimer::timeout, this, &MyWindow::render);
repaintTimer->start(1000/60);
QTimer *elapsedTimer = new QTimer(this);
connect(elapsedTimer, &QTimer::timeout, this, &MyWindow::modCurTime);
elapsedTimer->start(1);
}
void MyWindow::modCurTime()
{
currentTimeMs++;
currentTimeS=currentTimeMs/1000.0f;
}
void MyWindow::initialize()
{
mFuncs->glGenVertexArrays(1, &mVAO);
mFuncs->glBindVertexArray(mVAO);
CreateVertexBuffer();
initShaders();
initMatrices();
mRotationMatrixLocation = mProgramSimpleADS->uniformLocation("RotationMatrix");
glFrontFace(GL_CCW);
glEnable(GL_DEPTH_TEST);
}
void MyWindow::CreateVertexBuffer()
{
QMatrix4x4 transform;
transform.translate(QVector3D(0.0f, 1.5f, 0.25f));
mTeapot = new Teapot(13, transform);
// Create and populate the buffer objects
unsigned int handle[3];
glGenBuffers(3, handle);
glBindBuffer(GL_ARRAY_BUFFER, handle[0]);
glBufferData(GL_ARRAY_BUFFER, (3 * mTeapot->getnVerts()) * sizeof(float), mTeapot->getv(), GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, handle[1]);
glBufferData(GL_ARRAY_BUFFER, (3 * mTeapot->getnVerts()) * sizeof(float), mTeapot->getn(), GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[2]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * mTeapot->getnFaces() * sizeof(unsigned int), mTeapot->getelems(), GL_STATIC_DRAW);
// Setup the VAO
// Vertex positions
mFuncs->glBindVertexBuffer(0, handle[0], 0, sizeof(GLfloat) * 3);
mFuncs->glVertexAttribFormat(0, 3, GL_FLOAT, GL_FALSE, 0);
mFuncs->glVertexAttribBinding(0, 0);
// Vertex normals
mFuncs->glBindVertexBuffer(1, handle[1], 0, sizeof(GLfloat) * 3);
mFuncs->glVertexAttribFormat(1, 3, GL_FLOAT, GL_FALSE, 0);
mFuncs->glVertexAttribBinding(1, 1);
// Indices
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[2]);
mFuncs->glBindVertexArray(0);
}
void MyWindow::initMatrices()
{
ModelMatrix2Sided.translate(1.6f, 0.5f, 0.0f);
ModelMatrix2Sided.rotate( -90.0f, QVector3D(1.0f,0.0f,0.0f));
ModelMatrix2Sided.scale(0.50f);
ModelMatrixSimpleADS.translate(-1.6f, 0.5f, 0.0f);
ModelMatrixSimpleADS.rotate( -90.0f, QVector3D(1.0f,0.0f,0.0f));
ModelMatrixSimpleADS.scale(0.50f);
ViewMatrix.lookAt(QVector3D(2.0f,4.0f,2.0f), QVector3D(0.0f,0.0f,0.0f), QVector3D(0.0f,1.0f,0.0f));
}
void MyWindow::resizeEvent(QResizeEvent *)
{
mUpdateSize = true;
ProjectionMatrix.setToIdentity();
ProjectionMatrix.perspective(70.0f, (float)this->width()/(float)this->height(), 0.3f, 100.0f);
}
void MyWindow::render()
{
if(!isVisible() || !isExposed())
return;
if (!mContext->makeCurrent(this))
return;
static bool initialized = false;
if (!initialized) {
initialize();
initialized = true;
}
if (mUpdateSize) {
glViewport(0, 0, size().width(), size().height());
mUpdateSize = false;
}
static float EvolvingVal = 0;
EvolvingVal += 0.1f;
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//QMatrix4x4 RotationMatrix;
//RotationMatrix.rotate(EvolvingVal, QVector3D(0.1f, 0.0f, 0.1f));
//ModelMatrix.rotate(0.3f, QVector3D(0.1f, 0.0f, 0.1f));
mFuncs->glBindVertexArray(mVAO);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
mProgramSimpleADS->bind();
{
QVector4D worldLight = QVector4D(2.0f, 4.0f, 2.0f, 1.0f);
mProgramSimpleADS->setUniformValue("Light.Position", ViewMatrix * worldLight );
mProgramSimpleADS->setUniformValue("Material.Kd", 0.9f, 0.5f, 0.3f);
mProgramSimpleADS->setUniformValue("Light.Ld", 1.0f, 1.0f, 1.0f);
mProgramSimpleADS->setUniformValue("Material.Ka", 0.9f, 0.5f, 0.3f);
mProgramSimpleADS->setUniformValue("Light.La", 0.4f, 0.4f, 0.4f);
mProgramSimpleADS->setUniformValue("Material.Ks", 0.8f, 0.8f, 0.8f);
mProgramSimpleADS->setUniformValue("Light.Ls", 1.0f, 1.0f, 1.0f);
mProgramSimpleADS->setUniformValue("Material.Shininess", 100.0f);
QMatrix4x4 mv1 = ViewMatrix * ModelMatrixSimpleADS;
mProgramSimpleADS->setUniformValue("ModelViewMatrix", mv1);
mProgramSimpleADS->setUniformValue("NormalMatrix", mv1.normalMatrix());
mProgramSimpleADS->setUniformValue("MVP", ProjectionMatrix * mv1);
glDrawElements(GL_TRIANGLES, 6 * mTeapot->getnFaces(), GL_UNSIGNED_INT, ((GLubyte *)NULL + (0)));
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
}
mProgramSimpleADS->release();
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
mProgram2Sides->bind();
{
QVector4D worldLight = QVector4D(2.0f, 4.0f, 2.0f, 1.0f);
mProgram2Sides->setUniformValue("Light.Position", ViewMatrix * worldLight );
mProgram2Sides->setUniformValue("Material.Kd", 0.9f, 0.5f, 0.3f);
mProgram2Sides->setUniformValue("Light.Ld", 1.0f, 1.0f, 1.0f);
mProgram2Sides->setUniformValue("Material.Ka", 0.9f, 0.5f, 0.3f);
mProgram2Sides->setUniformValue("Light.La", 0.4f, 0.4f, 0.4f);
mProgram2Sides->setUniformValue("Material.Ks", 0.8f, 0.8f, 0.8f);
mProgram2Sides->setUniformValue("Light.Ls", 1.0f, 1.0f, 1.0f);
mProgram2Sides->setUniformValue("Material.Shininess", 100.0f);
QMatrix4x4 mv2 = ViewMatrix * ModelMatrix2Sided;
mProgram2Sides->setUniformValue("ModelViewMatrix", mv2);
mProgram2Sides->setUniformValue("NormalMatrix", mv2.normalMatrix());
mProgram2Sides->setUniformValue("MVP", ProjectionMatrix * mv2);
glDrawElements(GL_TRIANGLES, 6 * mTeapot->getnFaces(), GL_UNSIGNED_INT, ((GLubyte *)NULL + (0)));
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
}
mProgram2Sides->release();
mContext->swapBuffers(this);
}
void MyWindow::initShaders()
{
QOpenGLShader vShader(QOpenGLShader::Vertex);
QOpenGLShader fShader(QOpenGLShader::Fragment);
QFile shaderFile;
QByteArray shaderSource;
//Simple ADS
shaderFile.setFileName(":/vshader_ads.txt");
shaderFile.open(QIODevice::ReadOnly);
shaderSource = shaderFile.readAll();
shaderFile.close();
qDebug() << "vertex simple ADS compile: " << vShader.compileSourceCode(shaderSource);
shaderFile.setFileName(":/fshader_ads.txt");
shaderFile.open(QIODevice::ReadOnly);
shaderSource = shaderFile.readAll();
shaderFile.close();
qDebug() << "frag simple ADS compile: " << fShader.compileSourceCode(shaderSource);
mProgramSimpleADS = new (QOpenGLShaderProgram);
mProgramSimpleADS->addShader(&vShader);
mProgramSimpleADS->addShader(&fShader);
qDebug() << "shader link tree: " << mProgramSimpleADS->link();
//2 sided ADS
shaderFile.setFileName(":/vshader_2sides.txt");
shaderFile.open(QIODevice::ReadOnly);
shaderSource = shaderFile.readAll();
shaderFile.close();
qDebug() << "vertex 2-sided compile: " << vShader.compileSourceCode(shaderSource);
shaderFile.setFileName(":/fshader_2sides.txt");
shaderFile.open(QIODevice::ReadOnly);
shaderSource = shaderFile.readAll();
shaderFile.close();
qDebug() << "frag 2-sided compile: " << fShader.compileSourceCode(shaderSource);
mProgram2Sides = new (QOpenGLShaderProgram);
mProgram2Sides->addShader(&vShader);
mProgram2Sides->addShader(&fShader);
qDebug() << "shader link 2-sided: " << mProgram2Sides->link();
}
void MyWindow::PrepareTexture(GLenum TextureTarget, const QString& FileName, GLuint& TexObject, bool flip)
{
QImage TexImg;
if (!TexImg.load(FileName)) qDebug() << "Erreur chargement texture";
if (flip==true) TexImg=TexImg.mirrored();
glGenTextures(1, &TexObject);
glBindTexture(TextureTarget, TexObject);
glTexImage2D(TextureTarget, 0, GL_RGB, TexImg.width(), TexImg.height(), 0, GL_BGRA, GL_UNSIGNED_BYTE, TexImg.bits());
glTexParameterf(TextureTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(TextureTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
void MyWindow::keyPressEvent(QKeyEvent *keyEvent)
{
switch(keyEvent->key())
{
case Qt::Key_P:
break;
case Qt::Key_Up:
break;
case Qt::Key_Down:
break;
case Qt::Key_Left:
break;
case Qt::Key_Right:
break;
case Qt::Key_Delete:
break;
case Qt::Key_PageDown:
break;
case Qt::Key_Home:
break;
case Qt::Key_Z:
break;
case Qt::Key_Q:
break;
case Qt::Key_S:
break;
case Qt::Key_D:
break;
case Qt::Key_A:
break;
case Qt::Key_E:
break;
default:
break;
}
}
void MyWindow::printMatrix(const QMatrix4x4& mat)
{
const float *locMat = mat.transposed().constData();
for (int i=0; i<4; i++)
{
qDebug() << locMat[i*4] << " " << locMat[i*4+1] << " " << locMat[i*4+2] << " " << locMat[i*4+3];
}
}