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
|
// glut_example.c
// Stanford University, CS248, Fall 2000
//
// Demonstrates basic use of GLUT toolkit for CS248 video game assignment.
// More GLUT details at http://reality.sgi.com/mjk_asd/spec3/spec3.html
// Here you'll find examples of initialization, basic viewing transformations,
// mouse and keyboard callbacks, menus, some rendering primitives, lighting,
// double buffering, Z buffering, and texturing.
//
// Matt Ginzton -- magi@cs.stanford.edu
#include <GL/glew.h>
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
const char *vertex_shader_source =
"attribute vec4 in_position; \n"
"attribute vec4 in_color; \n"
"\n"
"varying vec4 vVaryingColor; \n"
" \n"
"void main() \n"
"{ \n"
" gl_Position = in_position;\n"
" vVaryingColor = in_color;\n"
"} \n";
const char *fragment_shader_source =
"precision mediump float; \n"
" \n"
"varying vec4 vVaryingColor; \n"
" \n"
"void main() \n"
"{ \n"
" gl_FragColor = vVaryingColor; \n"
"} \n";
GLfloat vVertices[] = {
// front
-1.0f, -1.0f, +1.0f, // point blue
+1.0f, -1.0f, +1.0f, // point magenta
-1.0f, +1.0f, +1.0f, // point cyan
+1.0f, +1.0f, +1.0f, // point white
};
GLfloat vColors[] = {
// front
0.0f, 0.0f, 1.0f, // blue
1.0f, 0.0f, 1.0f, // magenta
0.0f, 1.0f, 1.0f, // cyan
1.0f, 1.0f, 1.0f, // white
};
#define VERTEX_BUFFER_SIZE 0x60000
#define COMPONENTS_PER_VERTEX (3)
#define NUM_VERTICES (4)
GLint modelviewmatrix_handle;
GLint modelviewprojectionmatrix_handle;
GLint normalmatrix_handle;
GLuint vertex_shader;
GLuint fragment_shader;
GLuint program;
#define VIEWING_DISTANCE_MIN 3.0
#define TEXTURE_ID_CUBE 1
enum {
MENU_LIGHTING = 1,
MENU_POLYMODE,
MENU_TEXTURING,
MENU_EXIT
};
typedef int BOOL;
#define TRUE 1
#define FALSE 0
static BOOL g_bLightingEnabled = TRUE;
static BOOL g_bFillPolygons = TRUE;
static BOOL g_bTexture = FALSE;
static BOOL g_bButton1Down = FALSE;
static GLfloat g_fTeapotAngle = 0.0;
static GLfloat g_fTeapotAngle2 = 0.0;
static GLfloat g_fViewDistance = 3 * VIEWING_DISTANCE_MIN;
static GLfloat g_nearPlane = 1;
static GLfloat g_farPlane = 1000;
static int g_Width = 600; // Initial window width
static int g_Height = 600; // Initial window height
static int g_yClick = 0;
static float g_lightPos[4] = { 10, 10, -100, 1 }; // Position of light
#ifdef _WIN32
static DWORD last_idle_time;
#else
static struct timeval last_idle_time;
#endif
void DrawCubeFace(float fSize)
{
fSize /= 2.0;
glBegin(GL_QUADS);
glVertex3f(-fSize, -fSize, fSize); glTexCoord2f (0, 0);
glVertex3f(fSize, -fSize, fSize); glTexCoord2f (1, 0);
glVertex3f(fSize, fSize, fSize); glTexCoord2f (1, 1);
glVertex3f(-fSize, fSize, fSize); glTexCoord2f (0, 1);
glEnd();
}
void DrawCubeWithTextureCoords (float fSize)
{
glPushMatrix();
DrawCubeFace (fSize);
glRotatef (90, 1, 0, 0);
DrawCubeFace (fSize);
glRotatef (90, 1, 0, 0);
DrawCubeFace (fSize);
glRotatef (90, 1, 0, 0);
DrawCubeFace (fSize);
glRotatef (90, 0, 1, 0);
DrawCubeFace (fSize);
glRotatef (180, 0, 1, 0);
DrawCubeFace (fSize);
glPopMatrix();
}
void RenderObjects(void)
{
float colorBronzeDiff[4] = { 0.8, 0.6, 0.0, 1.0 };
float colorBronzeSpec[4] = { 1.0, 1.0, 0.4, 1.0 };
float colorBlue[4] = { 0.0, 0.2, 1.0, 1.0 };
float colorNone[4] = { 0.0, 0.0, 0.0, 0.0 };
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
// Main object (cube) ... transform to its coordinates, and render
glRotatef(15, 1, 0, 0);
glRotatef(45, 0, 1, 0);
glRotatef(g_fTeapotAngle, 0, 0, 1);
glMaterialfv(GL_FRONT, GL_DIFFUSE, colorBlue);
glMaterialfv(GL_FRONT, GL_SPECULAR, colorNone);
glColor4fv(colorBlue);
glBindTexture(GL_TEXTURE_2D, TEXTURE_ID_CUBE);
DrawCubeWithTextureCoords(1.0);
// Child object (teapot) ... relative transform, and render
glPushMatrix();
glTranslatef(2, 0, 0);
glRotatef(g_fTeapotAngle2, 1, 1, 0);
glMaterialfv(GL_FRONT, GL_DIFFUSE, colorBronzeDiff);
glMaterialfv(GL_FRONT, GL_SPECULAR, colorBronzeSpec);
glMaterialf(GL_FRONT, GL_SHININESS, 50.0);
glColor4fv(colorBronzeDiff);
glBindTexture(GL_TEXTURE_2D, 0);
glutSolidTeapot(0.3);
glPopMatrix();
glPopMatrix();
}
void display(void)
{
// Clear frame buffer and depth buffer
glClearColor(0.5, 0.5, 0.5, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_CULL_FACE);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDrawArrays(GL_TRIANGLE_STRIP, 4, 4);
glDrawArrays(GL_TRIANGLE_STRIP, 8, 4);
glDrawArrays(GL_TRIANGLE_STRIP, 12, 4);
glDrawArrays(GL_TRIANGLE_STRIP, 16, 4);
glDrawArrays(GL_TRIANGLE_STRIP, 20, 4);
// Make sure changes appear onscreen
glutSwapBuffers();
}
void reshape(GLint width, GLint height)
{
g_Width = width;
g_Height = height;
glViewport(0, 0, g_Width, g_Height);
}
void InitGraphics(void)
{
GLint ret;
printf("GL Extensions \"%s\"\n", glGetString(GL_EXTENSIONS));
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
if (!vertex_shader) {
exit(1);
}
glShaderSource(vertex_shader, 1, &vertex_shader_source, NULL);
glCompileShader(vertex_shader);
glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &ret);
if (!ret) {
char *log;
printf("Error: vertex shader compilation failed!:\n");
glGetShaderiv(vertex_shader, GL_INFO_LOG_LENGTH, &ret);
if (ret > 1) {
log = malloc(ret);
glGetShaderInfoLog(vertex_shader, ret, NULL, log);
printf("%s", log);
}
exit(1);
} else
printf("Vertex shader compilation succeeded!\n");
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
if (!fragment_shader) {
exit(1);
}
glShaderSource(fragment_shader, 1, &fragment_shader_source, NULL);
glCompileShader(fragment_shader);
glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &ret);
if (!ret) {
char *log;
printf("Error: fragment shader compilation failed!:\n");
glGetShaderiv(fragment_shader, GL_INFO_LOG_LENGTH, &ret);
if (ret > 1) {
log = malloc(ret);
glGetShaderInfoLog(fragment_shader, ret, NULL, log);
printf("%s", log);
}
exit(-1);
} else
printf("Fragment shader compilation succeeded!\n");
program = glCreateProgram();
if (!program) {
printf("Error: failed to create program!\n");
exit(-1);
}
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glBindAttribLocation(program, 0, "in_position");
glBindAttribLocation(program, 1, "in_color");
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &ret);
if (!ret) {
char *log;
printf("Error: program linking failed!:\n");
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &ret);
if (ret > 1) {
log = malloc(ret);
glGetProgramInfoLog(program, ret, NULL, log);
printf("%s", log);
}
exit(-1);
} else
printf("program linking succeeded!\n");
glUseProgram(program);
/* Make vertex buffer object */
float vtx_logical[NUM_VERTICES * COMPONENTS_PER_VERTEX * 3];
size_t stride = COMPONENTS_PER_VERTEX * 3;
for(int vert=0; vert<NUM_VERTICES; ++vert)
{
int src_idx = vert * COMPONENTS_PER_VERTEX;
int dest_idx = vert * stride;
for(int comp=0; comp<COMPONENTS_PER_VERTEX; ++comp)
{
vtx_logical[dest_idx+comp+0] = vVertices[src_idx + comp]; /* 0 */
vtx_logical[dest_idx+comp+6] = vColors[src_idx + comp]; /* 2 */
}
}
int vbo = 0;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vtx_logical), (void*)vtx_logical, GL_STREAM_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride*4, (void*)(0*4));
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, stride*4, (void*)(6*4));
glEnableVertexAttribArray(1);
modelviewmatrix_handle = glGetUniformLocation(program, "modelviewMatrix");
modelviewprojectionmatrix_handle = glGetUniformLocation(program, "modelviewprojectionMatrix");
normalmatrix_handle = glGetUniformLocation(program, "normalMatrix");
}
void MouseButton(int button, int state, int x, int y)
{
// Respond to mouse button presses.
// If button1 pressed, mark this state so we know in motion function.
if (button == GLUT_LEFT_BUTTON)
{
g_bButton1Down = (state == GLUT_DOWN) ? TRUE : FALSE;
g_yClick = y - 3 * g_fViewDistance;
}
}
void MouseMotion(int x, int y)
{
// If button1 pressed, zoom in/out if mouse is moved up/down.
if (g_bButton1Down)
{
g_fViewDistance = (y - g_yClick) / 3.0;
if (g_fViewDistance < VIEWING_DISTANCE_MIN)
g_fViewDistance = VIEWING_DISTANCE_MIN;
glutPostRedisplay();
}
}
void AnimateScene(void)
{
float dt;
#ifdef _WIN32
DWORD time_now;
time_now = GetTickCount();
dt = (float) (time_now - last_idle_time) / 1000.0;
#else
// Figure out time elapsed since last call to idle function
struct timeval time_now;
gettimeofday(&time_now, NULL);
dt = (float)(time_now.tv_sec - last_idle_time.tv_sec) +
1.0e-6*(time_now.tv_usec - last_idle_time.tv_usec);
#endif
// Animate the teapot by updating its angles
g_fTeapotAngle += dt * 30.0;
g_fTeapotAngle2 += dt * 100.0;
// Save time_now for next time
last_idle_time = time_now;
// Force redraw
glutPostRedisplay();
}
void SelectFromMenu(int idCommand)
{
switch (idCommand)
{
case MENU_LIGHTING:
g_bLightingEnabled = !g_bLightingEnabled;
if (g_bLightingEnabled)
glEnable(GL_LIGHTING);
else
glDisable(GL_LIGHTING);
break;
case MENU_POLYMODE:
g_bFillPolygons = !g_bFillPolygons;
glPolygonMode (GL_FRONT_AND_BACK, g_bFillPolygons ? GL_FILL : GL_LINE);
break;
case MENU_TEXTURING:
g_bTexture = !g_bTexture;
if (g_bTexture)
glEnable(GL_TEXTURE_2D);
else
glDisable(GL_TEXTURE_2D);
break;
case MENU_EXIT:
exit (0);
break;
}
// Almost any menu selection requires a redraw
glutPostRedisplay();
}
void Keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 27: // ESCAPE key
exit (0);
break;
case 'l':
SelectFromMenu(MENU_LIGHTING);
break;
case 'p':
SelectFromMenu(MENU_POLYMODE);
break;
case 't':
SelectFromMenu(MENU_TEXTURING);
break;
}
}
int BuildPopupMenu (void)
{
int menu;
menu = glutCreateMenu (SelectFromMenu);
glutAddMenuEntry ("Toggle lighting\tl", MENU_LIGHTING);
glutAddMenuEntry ("Toggle polygon fill\tp", MENU_POLYMODE);
glutAddMenuEntry ("Toggle texturing\tt", MENU_TEXTURING);
glutAddMenuEntry ("Exit demo\tEsc", MENU_EXIT);
return menu;
}
int main(int argc, char** argv)
{
// GLUT Window Initialization:
glutInit (&argc, argv);
glutInitWindowSize (g_Width, g_Height);
glutInitDisplayMode ( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow ("Orientation check");
GLenum err = glewInit();
if (GLEW_OK != err)
{
/* Problem: glewInit failed, something is seriously wrong. */
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
exit(1);
}
fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
// Initialize OpenGL graphics state
InitGraphics();
// Register callbacks:
glutDisplayFunc (display);
glutReshapeFunc (reshape);
glutKeyboardFunc (Keyboard);
glutMouseFunc (MouseButton);
glutMotionFunc (MouseMotion);
glutIdleFunc (AnimateScene);
// Create our popup menu
BuildPopupMenu ();
glutAttachMenu (GLUT_RIGHT_BUTTON);
// Get the initial time, for use by animation
#ifdef _WIN32
last_idle_time = GetTickCount();
#else
gettimeofday (&last_idle_time, NULL);
#endif
// Turn the flow of control over to GLUT
glutMainLoop ();
return 0;
}
|