2 * Procedural Bump Mapping demo. Uses the example shaders from
3 * chapter 11 of the OpenGL Shading Language "orange" book.
18 static char *FragProgFile
= "CH11-bumpmap.frag.txt";
19 static char *VertProgFile
= "CH11-bumpmap.vert.txt";
21 /* program/shader objects */
22 static GLuint fragShader
;
23 static GLuint vertShader
;
24 static GLuint program
;
34 static struct uniform_info Uniforms
[] = {
35 { "LightPosition", 3, -1, { 0.57737, 0.57735, 0.57735, 0.0 } },
36 { "SurfaceColor", 3, -1, { 0.8, 0.8, 0.2, 0 } },
37 { "BumpDensity", 1, -1, { 10.0, 0, 0, 0 } },
38 { "BumpSize", 1, -1, { 0.125, 0, 0, 0 } },
39 { "SpecularFactor", 1, -1, { 0.5, 0, 0, 0 } },
40 { NULL
, 0, 0, { 0, 0, 0, 0 } }
45 static GLfloat xRot
= 20.0f
, yRot
= 0.0f
, zRot
= 0.0f
;
47 static GLuint tangentAttrib
;
49 static GLboolean Anim
= GL_FALSE
;
55 GLenum err
= glGetError();
57 printf("GL Error %s (0x%x) at line %d\n",
58 gluErrorString(err
), (int) err
, line
);
63 * Draw a square, specifying normal and tangent vectors.
69 glVertexAttrib3f_func(tangentAttrib
, 1, 0, 0);
71 glTexCoord2f(0, 0); glVertex2f(-size
, -size
);
72 glTexCoord2f(1, 0); glVertex2f( size
, -size
);
73 glTexCoord2f(1, 1); glVertex2f( size
, size
);
74 glTexCoord2f(0, 1); glVertex2f(-size
, size
);
84 glRotatef(90, 0, 1, 0);
85 glTranslatef(0, 0, size
);
91 glRotatef(-90, 0, 1, 0);
92 glTranslatef(0, 0, size
);
98 glRotatef(90, 1, 0, 0);
99 glTranslatef(0, 0, size
);
105 glRotatef(-90, 1, 0, 0);
106 glTranslatef(0, 0, size
);
113 glTranslatef(0, 0, size
);
119 glRotatef(180, 0, 1, 0);
120 glTranslatef(0, 0, size
);
130 GLint t
= glutGet(GLUT_ELAPSED_TIME
);
139 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
142 glRotatef(xRot
, 1.0f
, 0.0f
, 0.0f
);
143 glRotatef(yRot
, 0.0f
, 1.0f
, 0.0f
);
144 glRotatef(zRot
, 0.0f
, 0.0f
, 1.0f
);
153 CheckError(__LINE__
);
160 Reshape(int width
, int height
)
162 glViewport(0, 0, width
, height
);
163 glMatrixMode(GL_PROJECTION
);
165 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
166 glMatrixMode(GL_MODELVIEW
);
168 glTranslatef(0.0f
, 0.0f
, -15.0f
);
175 glDeleteShader_func(fragShader
);
176 glDeleteShader_func(vertShader
);
177 glDeleteProgram_func(program
);
178 glutDestroyWindow(win
);
183 Key(unsigned char key
, int x
, int y
)
185 const GLfloat step
= 2.0;
192 glutIdleFunc(Anim
? Idle
: NULL
);
210 SpecialKey(int key
, int x
, int y
)
212 const GLfloat step
= 2.0;
237 LoadAndCompileShader(GLuint shader
, const char *text
)
241 glShaderSource_func(shader
, 1, (const GLchar
**) &text
, NULL
);
243 glCompileShader_func(shader
);
245 glGetShaderiv_func(shader
, GL_COMPILE_STATUS
, &stat
);
249 glGetShaderInfoLog_func(shader
, 1000, &len
, log
);
250 fprintf(stderr
, "brick: problem compiling shader: %s\n", log
);
254 printf("Shader compiled OK\n");
260 * Read a shader from a file.
263 ReadShader(GLuint shader
, const char *filename
)
265 const int max
= 100*1000;
267 char *buffer
= (char*) malloc(max
);
268 FILE *f
= fopen(filename
, "r");
270 fprintf(stderr
, "brick: Unable to open shader file %s\n", filename
);
274 n
= fread(buffer
, 1, max
, f
);
275 printf("brick: read %d bytes from shader file %s\n", n
, filename
);
278 LoadAndCompileShader(shader
, buffer
);
287 CheckLink(GLuint prog
)
290 glGetProgramiv_func(prog
, GL_LINK_STATUS
, &stat
);
294 glGetProgramInfoLog_func(prog
, 1000, &len
, log
);
295 fprintf(stderr
, "Linker error:\n%s\n", log
);
298 fprintf(stderr
, "Link success!\n");
309 version
= (const char *) glGetString(GL_VERSION
);
310 if (version
[0] != '2' || version
[1] != '.') {
311 printf("Warning: this program expects OpenGL 2.0\n");
314 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER
));
318 vertShader
= glCreateShader_func(GL_VERTEX_SHADER
);
319 ReadShader(vertShader
, VertProgFile
);
321 fragShader
= glCreateShader_func(GL_FRAGMENT_SHADER
);
322 ReadShader(fragShader
, FragProgFile
);
324 program
= glCreateProgram_func();
325 glAttachShader_func(program
, fragShader
);
326 glAttachShader_func(program
, vertShader
);
327 glLinkProgram_func(program
);
329 glUseProgram_func(program
);
331 assert(glIsProgram_func(program
));
332 assert(glIsShader_func(fragShader
));
333 assert(glIsShader_func(vertShader
));
335 assert(glGetError() == 0);
337 CheckError(__LINE__
);
339 for (i
= 0; Uniforms
[i
].name
; i
++) {
341 = glGetUniformLocation_func(program
, Uniforms
[i
].name
);
342 printf("Uniform %s location: %d\n", Uniforms
[i
].name
,
343 Uniforms
[i
].location
);
344 switch (Uniforms
[i
].size
) {
346 glUniform1fv_func(Uniforms
[i
].location
, 1, Uniforms
[i
].value
);
349 glUniform2fv_func(Uniforms
[i
].location
, 1, Uniforms
[i
].value
);
352 glUniform3fv_func(Uniforms
[i
].location
, 1, Uniforms
[i
].value
);
355 glUniform4fv_func(Uniforms
[i
].location
, 1, Uniforms
[i
].value
);
362 CheckError(__LINE__
);
364 tangentAttrib
= glGetAttribLocation_func(program
, "Tangent");
365 printf("Tangent Attrib: %d\n", tangentAttrib
);
367 assert(tangentAttrib
>= 0);
369 CheckError(__LINE__
);
371 glClearColor(0.4f
, 0.4f
, 0.8f
, 0.0f
);
373 glEnable(GL_DEPTH_TEST
);
380 ParseOptions(int argc
, char *argv
[])
383 for (i
= 1; i
< argc
; i
++) {
384 if (strcmp(argv
[i
], "-fs") == 0) {
385 FragProgFile
= argv
[i
+1];
387 else if (strcmp(argv
[i
], "-vs") == 0) {
388 VertProgFile
= argv
[i
+1];
395 main(int argc
, char *argv
[])
397 glutInit(&argc
, argv
);
398 glutInitWindowPosition( 0, 0);
399 glutInitWindowSize(400, 400);
400 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
401 win
= glutCreateWindow(argv
[0]);
402 glutReshapeFunc(Reshape
);
403 glutKeyboardFunc(Key
);
404 glutSpecialFunc(SpecialKey
);
405 glutDisplayFunc(Redisplay
);
406 ParseOptions(argc
, argv
);