This repository has been archived by the owner on Aug 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathmain.cpp
637 lines (609 loc) · 19.1 KB
/
main.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
#include <fstream>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glext.h>
#include <GL/glut.h>
#include <GL/glm/glm.hpp>
#include <GL/glm/gtc/matrix_transform.hpp>
#include <GL/glm/gtx/transform2.hpp>
#include <GL/glm/gtc/type_ptr.hpp>
#define GL_ERROR() checkForOpenGLError(__FILE__, __LINE__)
using namespace std;
using glm::mat4;
using glm::vec3;
GLuint g_vao;
GLuint g_programHandle;
GLuint g_winWidth = 400;
GLuint g_winHeight = 400;
GLint g_angle = 0;
GLuint g_frameBuffer;
// transfer function
GLuint g_tffTexObj;
GLuint g_bfTexObj;
GLuint g_texWidth;
GLuint g_texHeight;
GLuint g_volTexObj;
GLuint g_rcVertHandle;
GLuint g_rcFragHandle;
GLuint g_bfVertHandle;
GLuint g_bfFragHandle;
float g_stepSize = 0.001f;
int checkForOpenGLError(const char* file, int line)
{
// return 1 if an OpenGL error occured, 0 otherwise.
GLenum glErr;
int retCode = 0;
glErr = glGetError();
while(glErr != GL_NO_ERROR)
{
cout << "glError in file " << file
<< "@line " << line << gluErrorString(glErr) << endl;
retCode = 1;
exit(EXIT_FAILURE);
}
return retCode;
}
void keyboard(unsigned char key, int x, int y);
void display(void);
void initVBO();
void initShader();
void initFrameBuffer(GLuint, GLuint, GLuint);
GLuint initTFF1DTex(const char* filename);
GLuint initFace2DTex(GLuint texWidth, GLuint texHeight);
GLuint initVol3DTex(const char* filename, GLuint width, GLuint height, GLuint depth);
void render(GLenum cullFace);
void init()
{
g_texWidth = g_winWidth;
g_texHeight = g_winHeight;
initVBO();
initShader();
g_tffTexObj = initTFF1DTex("tff.dat");
g_bfTexObj = initFace2DTex(g_texWidth, g_texHeight);
g_volTexObj = initVol3DTex("head256.raw", 256, 256, 225);
GL_ERROR();
initFrameBuffer(g_bfTexObj, g_texWidth, g_texHeight);
GL_ERROR();
}
// init the vertex buffer object
void initVBO()
{
GLfloat vertices[24] = {
0.0, 0.0, 0.0,
0.0, 0.0, 1.0,
0.0, 1.0, 0.0,
0.0, 1.0, 1.0,
1.0, 0.0, 0.0,
1.0, 0.0, 1.0,
1.0, 1.0, 0.0,
1.0, 1.0, 1.0
};
// draw the six faces of the boundbox by drawwing triangles
// draw it contra-clockwise
// front: 1 5 7 3
// back: 0 2 6 4
// left:0 1 3 2
// right:7 5 4 6
// up: 2 3 7 6
// down: 1 0 4 5
GLuint indices[36] = {
1,5,7,
7,3,1,
0,2,6,
6,4,0,
0,1,3,
3,2,0,
7,5,4,
4,6,7,
2,3,7,
7,6,2,
1,0,4,
4,5,1
};
GLuint gbo[2];
glGenBuffers(2, gbo);
GLuint vertexdat = gbo[0];
GLuint veridxdat = gbo[1];
glBindBuffer(GL_ARRAY_BUFFER, vertexdat);
glBufferData(GL_ARRAY_BUFFER, 24*sizeof(GLfloat), vertices, GL_STATIC_DRAW);
// used in glDrawElement()
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, veridxdat);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 36*sizeof(GLuint), indices, GL_STATIC_DRAW);
GLuint vao;
glGenVertexArrays(1, &vao);
// vao like a closure binding 3 buffer object: verlocdat vercoldat and veridxdat
glBindVertexArray(vao);
glEnableVertexAttribArray(0); // for vertexloc
glEnableVertexAttribArray(1); // for vertexcol
// the vertex location is the same as the vertex color
glBindBuffer(GL_ARRAY_BUFFER, vertexdat);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (GLfloat *)NULL);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (GLfloat *)NULL);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, veridxdat);
// glBindVertexArray(0);
g_vao = vao;
}
void drawBox(GLenum glFaces)
{
glEnable(GL_CULL_FACE);
glCullFace(glFaces);
glBindVertexArray(g_vao);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, (GLuint *)NULL);
glDisable(GL_CULL_FACE);
}
// check the compilation result
GLboolean compileCheck(GLuint shader)
{
GLint err;
glGetShaderiv(shader, GL_COMPILE_STATUS, &err);
if (GL_FALSE == err)
{
GLint logLen;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLen);
if (logLen > 0)
{
char* log = (char *)malloc(logLen);
GLsizei written;
glGetShaderInfoLog(shader, logLen, &written, log);
cerr << "Shader log: " << log << endl;
free(log);
}
}
return err;
}
// init shader object
GLuint initShaderObj(const GLchar* srcfile, GLenum shaderType)
{
ifstream inFile(srcfile, ifstream::in);
// use assert?
if (!inFile)
{
cerr << "Error openning file: " << srcfile << endl;
exit(EXIT_FAILURE);
}
const int MAX_CNT = 10000;
GLchar *shaderCode = (GLchar *) calloc(MAX_CNT, sizeof(GLchar));
inFile.read(shaderCode, MAX_CNT);
if (inFile.eof())
{
size_t bytecnt = inFile.gcount();
*(shaderCode + bytecnt) = '\0';
}
else if(inFile.fail())
{
cout << srcfile << "read failed " << endl;
}
else
{
cout << srcfile << "is too large" << endl;
}
// create the shader Object
GLuint shader = glCreateShader(shaderType);
if (0 == shader)
{
cerr << "Error creating vertex shader." << endl;
}
// cout << shaderCode << endl;
// cout << endl;
const GLchar* codeArray[] = {shaderCode};
glShaderSource(shader, 1, codeArray, NULL);
free(shaderCode);
// compile the shader
glCompileShader(shader);
if (GL_FALSE == compileCheck(shader))
{
cerr << "shader compilation failed" << endl;
}
return shader;
}
GLint checkShaderLinkStatus(GLuint pgmHandle)
{
GLint status;
glGetProgramiv(pgmHandle, GL_LINK_STATUS, &status);
if (GL_FALSE == status)
{
GLint logLen;
glGetProgramiv(pgmHandle, GL_INFO_LOG_LENGTH, &logLen);
if (logLen > 0)
{
GLchar * log = (GLchar *)malloc(logLen);
GLsizei written;
glGetProgramInfoLog(pgmHandle, logLen, &written, log);
cerr << "Program log: " << log << endl;
}
}
return status;
}
// link shader program
GLuint createShaderPgm()
{
// Create the shader program
GLuint programHandle = glCreateProgram();
if (0 == programHandle)
{
cerr << "Error create shader program" << endl;
exit(EXIT_FAILURE);
}
return programHandle;
}
// init the 1 dimentional texture for transfer function
GLuint initTFF1DTex(const char* filename)
{
// read in the user defined data of transfer function
ifstream inFile(filename, ifstream::in);
if (!inFile)
{
cerr << "Error openning file: " << filename << endl;
exit(EXIT_FAILURE);
}
const int MAX_CNT = 10000;
GLubyte *tff = (GLubyte *) calloc(MAX_CNT, sizeof(GLubyte));
inFile.read(reinterpret_cast<char *>(tff), MAX_CNT);
if (inFile.eof())
{
size_t bytecnt = inFile.gcount();
*(tff + bytecnt) = '\0';
cout << "bytecnt " << bytecnt << endl;
}
else if(inFile.fail())
{
cout << filename << "read failed " << endl;
}
else
{
cout << filename << "is too large" << endl;
}
GLuint tff1DTex;
glGenTextures(1, &tff1DTex);
glBindTexture(GL_TEXTURE_1D, tff1DTex);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA8, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, tff);
free(tff);
return tff1DTex;
}
// init the 2D texture for render backface 'bf' stands for backface
GLuint initFace2DTex(GLuint bfTexWidth, GLuint bfTexHeight)
{
GLuint backFace2DTex;
glGenTextures(1, &backFace2DTex);
glBindTexture(GL_TEXTURE_2D, backFace2DTex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, bfTexWidth, bfTexHeight, 0, GL_RGBA, GL_FLOAT, NULL);
return backFace2DTex;
}
// init 3D texture to store the volume data used fo ray casting
GLuint initVol3DTex(const char* filename, GLuint w, GLuint h, GLuint d)
{
FILE *fp;
size_t size = w * h * d;
GLubyte *data = new GLubyte[size]; // 8bit
if (!(fp = fopen(filename, "rb")))
{
cout << "Error: opening .raw file failed" << endl;
exit(EXIT_FAILURE);
}
else
{
cout << "OK: open .raw file successed" << endl;
}
if ( fread(data, sizeof(char), size, fp)!= size)
{
cout << "Error: read .raw file failed" << endl;
exit(1);
}
else
{
cout << "OK: read .raw file successed" << endl;
}
fclose(fp);
glGenTextures(1, &g_volTexObj);
// bind 3D texture target
glBindTexture(GL_TEXTURE_3D, g_volTexObj);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT);
// pixel transfer happens here from client to OpenGL server
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glTexImage3D(GL_TEXTURE_3D, 0, GL_INTENSITY, w, h, d, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE,data);
delete []data;
cout << "volume texture created" << endl;
return g_volTexObj;
}
void checkFramebufferStatus()
{
GLenum complete = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (complete != GL_FRAMEBUFFER_COMPLETE)
{
cout << "framebuffer is not complete" << endl;
exit(EXIT_FAILURE);
}
}
// init the framebuffer, the only framebuffer used in this program
void initFrameBuffer(GLuint texObj, GLuint texWidth, GLuint texHeight)
{
// create a depth buffer for our framebuffer
GLuint depthBuffer;
glGenRenderbuffers(1, &depthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, texWidth, texHeight);
// attach the texture and the depth buffer to the framebuffer
glGenFramebuffers(1, &g_frameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, g_frameBuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texObj, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer);
checkFramebufferStatus();
glEnable(GL_DEPTH_TEST);
}
void rcSetUinforms()
{
// setting uniforms such as
// ScreenSize
// StepSize
// TransferFunc
// ExitPoints i.e. the backface, the backface hold the ExitPoints of ray casting
// VolumeTex the texture that hold the volume data i.e. head256.raw
GLint screenSizeLoc = glGetUniformLocation(g_programHandle, "ScreenSize");
if (screenSizeLoc >= 0)
{
glUniform2f(screenSizeLoc, (float)g_winWidth, (float)g_winHeight);
}
else
{
cout << "ScreenSize"
<< "is not bind to the uniform"
<< endl;
}
GLint stepSizeLoc = glGetUniformLocation(g_programHandle, "StepSize");
GL_ERROR();
if (stepSizeLoc >= 0)
{
glUniform1f(stepSizeLoc, g_stepSize);
}
else
{
cout << "StepSize"
<< "is not bind to the uniform"
<< endl;
}
GL_ERROR();
GLint transferFuncLoc = glGetUniformLocation(g_programHandle, "TransferFunc");
if (transferFuncLoc >= 0)
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_1D, g_tffTexObj);
glUniform1i(transferFuncLoc, 0);
}
else
{
cout << "TransferFunc"
<< "is not bind to the uniform"
<< endl;
}
GL_ERROR();
GLint backFaceLoc = glGetUniformLocation(g_programHandle, "ExitPoints");
if (backFaceLoc >= 0)
{
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, g_bfTexObj);
glUniform1i(backFaceLoc, 1);
}
else
{
cout << "ExitPoints"
<< "is not bind to the uniform"
<< endl;
}
GL_ERROR();
GLint volumeLoc = glGetUniformLocation(g_programHandle, "VolumeTex");
if (volumeLoc >= 0)
{
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_3D, g_volTexObj);
glUniform1i(volumeLoc, 2);
}
else
{
cout << "VolumeTex"
<< "is not bind to the uniform"
<< endl;
}
}
// init the shader object and shader program
void initShader()
{
// vertex shader object for first pass
g_bfVertHandle = initShaderObj("shader/backface.vert", GL_VERTEX_SHADER);
// fragment shader object for first pass
g_bfFragHandle = initShaderObj("shader/backface.frag", GL_FRAGMENT_SHADER);
// vertex shader object for second pass
g_rcVertHandle = initShaderObj("shader/raycasting.vert", GL_VERTEX_SHADER);
// fragment shader object for second pass
g_rcFragHandle = initShaderObj("shader/raycasting.frag", GL_FRAGMENT_SHADER);
// create the shader program , use it in an appropriate time
g_programHandle = createShaderPgm();
// 获得由着色器编译器分配的索引(可选)
}
// link the shader objects using the shader program
void linkShader(GLuint shaderPgm, GLuint newVertHandle, GLuint newFragHandle)
{
const GLsizei maxCount = 2;
GLsizei count;
GLuint shaders[maxCount];
glGetAttachedShaders(shaderPgm, maxCount, &count, shaders);
// cout << "get VertHandle: " << shaders[0] << endl;
// cout << "get FragHandle: " << shaders[1] << endl;
GL_ERROR();
for (int i = 0; i < count; i++) {
glDetachShader(shaderPgm, shaders[i]);
}
// Bind index 0 to the shader input variable "VerPos"
glBindAttribLocation(shaderPgm, 0, "VerPos");
// Bind index 1 to the shader input variable "VerClr"
glBindAttribLocation(shaderPgm, 1, "VerClr");
GL_ERROR();
glAttachShader(shaderPgm,newVertHandle);
glAttachShader(shaderPgm,newFragHandle);
GL_ERROR();
glLinkProgram(shaderPgm);
if (GL_FALSE == checkShaderLinkStatus(shaderPgm))
{
cerr << "Failed to relink shader program!" << endl;
exit(EXIT_FAILURE);
}
GL_ERROR();
}
// the color of the vertex in the back face is also the location
// of the vertex
// save the back face to the user defined framebuffer bound
// with a 2D texture named `g_bfTexObj`
// draw the front face of the box
// in the rendering process, i.e. the ray marching process
// loading the volume `g_volTexObj` as well as the `g_bfTexObj`
// after vertex shader processing we got the color as well as the location of
// the vertex (in the object coordinates, before transformation).
// and the vertex assemblied into primitives before entering
// fragment shader processing stage.
// in fragment shader processing stage. we got `g_bfTexObj`
// (correspond to 'VolumeTex' in glsl)and `g_volTexObj`(correspond to 'ExitPoints')
// as well as the location of primitives.
// the most important is that we got the GLSL to exec the logic. Here we go!
// draw the back face of the box
void display()
{
glEnable(GL_DEPTH_TEST);
// test the gl_error
GL_ERROR();
// render to texture
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, g_frameBuffer);
glViewport(0, 0, g_winWidth, g_winHeight);
linkShader(g_programHandle, g_bfVertHandle, g_bfFragHandle);
glUseProgram(g_programHandle);
// cull front face
render(GL_FRONT);
glUseProgram(0);
GL_ERROR();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, g_winWidth, g_winHeight);
linkShader(g_programHandle, g_rcVertHandle, g_rcFragHandle);
GL_ERROR();
glUseProgram(g_programHandle);
rcSetUinforms();
GL_ERROR();
// glUseProgram(g_programHandle);
// cull back face
render(GL_BACK);
// need or need not to resume the state of only one active texture unit?
// glActiveTexture(GL_TEXTURE1);
// glBindTexture(GL_TEXTURE_2D, 0);
// glDisable(GL_TEXTURE_2D);
// glActiveTexture(GL_TEXTURE2);
// glBindTexture(GL_TEXTURE_3D, 0);
// glDisable(GL_TEXTURE_3D);
// glActiveTexture(GL_TEXTURE0);
// glBindTexture(GL_TEXTURE_1D, 0);
// glDisable(GL_TEXTURE_1D);
// glActiveTexture(GL_TEXTURE0);
glUseProgram(0);
GL_ERROR();
// // for test the first pass
// glBindFramebuffer(GL_READ_FRAMEBUFFER, g_frameBuffer);
// checkFramebufferStatus();
// glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
// glViewport(0, 0, g_winWidth, g_winHeight);
// glClearColor(0.0, 0.0, 1.0, 1.0);
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// GL_ERROR();
// glBlitFramebuffer(0, 0, g_winWidth, g_winHeight,0, 0,
// g_winWidth, g_winHeight, GL_COLOR_BUFFER_BIT, GL_NEAREST);
// glBindFramebuffer(GL_FRAMEBUFFER, 0);
// GL_ERROR();
glutSwapBuffers();
}
// both of the two pass use the "render() function"
// the first pass render the backface of the boundbox
// the second pass render the frontface of the boundbox
// together with the frontface, use the backface as a 2D texture in the second pass
// to calculate the entry point and the exit point of the ray in and out the box.
void render(GLenum cullFace)
{
GL_ERROR();
glClearColor(0.2f,0.2f,0.2f,1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// transform the box
glm::mat4 projection = glm::perspective(60.0f, (GLfloat)g_winWidth/g_winHeight, 0.1f, 400.f);
glm::mat4 view = glm::lookAt(glm::vec3(0.0f, 0.0f, 2.0f),
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4 model = mat4(1.0f);
model *= glm::rotate((float)g_angle, glm::vec3(0.0f, 1.0f, 0.0f));
// to make the "head256.raw" i.e. the volume data stand up.
model *= glm::rotate(90.0f, vec3(1.0f, 0.0f, 0.0f));
model *= glm::translate(glm::vec3(-0.5f, -0.5f, -0.5f));
// notice the multiplication order: reverse order of transform
glm::mat4 mvp = projection * view * model;
GLuint mvpIdx = glGetUniformLocation(g_programHandle, "MVP");
if (mvpIdx >= 0)
{
glUniformMatrix4fv(mvpIdx, 1, GL_FALSE, &mvp[0][0]);
}
else
{
cerr << "can't get the MVP" << endl;
}
GL_ERROR();
drawBox(cullFace);
GL_ERROR();
// glutWireTeapot(0.5);
}
void rotateDisplay()
{
g_angle = (g_angle + 1) % 360;
glutPostRedisplay();
}
void reshape(int w, int h)
{
g_winWidth = w;
g_winHeight = h;
g_texWidth = w;
g_texHeight = h;
}
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case '\x1B':
exit(EXIT_SUCCESS);
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(400, 400);
glutCreateWindow("GLUT Test");
GLenum err = glewInit();
if (GLEW_OK != err)
{
/* Problem: glewInit failed, something is seriously wrong. */
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
}
glutKeyboardFunc(&keyboard);
glutDisplayFunc(&display);
glutReshapeFunc(&reshape);
glutIdleFunc(&rotateDisplay);
init();
glutMainLoop();
return EXIT_SUCCESS;
}