Brian | f44ba11 | 2007-01-16 14:55:43 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Procedural Bump Mapping demo. Uses the example shaders from |
| 3 | * chapter 11 of the OpenGL Shading Language "orange" book. |
| 4 | * 16 Jan 2007 |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
| 8 | #include <string.h> |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <math.h> |
| 12 | #include <GL/glut.h> |
| 13 | #include <GL/glu.h> |
| 14 | #include <GL/glext.h> |
| 15 | #include "extfuncs.h" |
Brian | 2dca337 | 2008-04-09 22:28:23 -0600 | [diff] [blame] | 16 | #include "shaderutil.h" |
Brian | f44ba11 | 2007-01-16 14:55:43 -0700 | [diff] [blame] | 17 | |
| 18 | |
Brian Paul | c0dd912 | 2008-08-16 09:36:46 -0600 | [diff] [blame] | 19 | static char *FragProgFile = "CH11-bumpmap.frag"; |
| 20 | static char *VertProgFile = "CH11-bumpmap.vert"; |
Brian | f44ba11 | 2007-01-16 14:55:43 -0700 | [diff] [blame] | 21 | |
| 22 | /* program/shader objects */ |
| 23 | static GLuint fragShader; |
| 24 | static GLuint vertShader; |
| 25 | static GLuint program; |
| 26 | |
| 27 | |
Brian | f44ba11 | 2007-01-16 14:55:43 -0700 | [diff] [blame] | 28 | static struct uniform_info Uniforms[] = { |
Brian | 2dca337 | 2008-04-09 22:28:23 -0600 | [diff] [blame] | 29 | { "LightPosition", 3, GL_FLOAT, { 0.57737, 0.57735, 0.57735, 0.0 }, -1 }, |
| 30 | { "SurfaceColor", 3, GL_FLOAT, { 0.8, 0.8, 0.2, 0 }, -1 }, |
| 31 | { "BumpDensity", 1, GL_FLOAT, { 10.0, 0, 0, 0 }, -1 }, |
| 32 | { "BumpSize", 1, GL_FLOAT, { 0.125, 0, 0, 0 }, -1 }, |
| 33 | { "SpecularFactor", 1, GL_FLOAT, { 0.5, 0, 0, 0 }, -1 }, |
| 34 | END_OF_UNIFORMS |
Brian | f44ba11 | 2007-01-16 14:55:43 -0700 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | static GLint win = 0; |
| 38 | |
Brian | 271d504 | 2007-01-16 15:27:11 -0700 | [diff] [blame] | 39 | static GLfloat xRot = 20.0f, yRot = 0.0f, zRot = 0.0f; |
Brian | f44ba11 | 2007-01-16 14:55:43 -0700 | [diff] [blame] | 40 | |
| 41 | static GLuint tangentAttrib; |
| 42 | |
Brian | 271d504 | 2007-01-16 15:27:11 -0700 | [diff] [blame] | 43 | static GLboolean Anim = GL_FALSE; |
| 44 | |
Brian | f44ba11 | 2007-01-16 14:55:43 -0700 | [diff] [blame] | 45 | |
| 46 | static void |
| 47 | CheckError(int line) |
| 48 | { |
| 49 | GLenum err = glGetError(); |
| 50 | if (err) { |
| 51 | printf("GL Error %s (0x%x) at line %d\n", |
| 52 | gluErrorString(err), (int) err, line); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * Draw a square, specifying normal and tangent vectors. |
| 58 | */ |
| 59 | static void |
| 60 | Square(GLfloat size) |
| 61 | { |
| 62 | glNormal3f(0, 0, 1); |
| 63 | glVertexAttrib3f_func(tangentAttrib, 1, 0, 0); |
| 64 | glBegin(GL_POLYGON); |
| 65 | glTexCoord2f(0, 0); glVertex2f(-size, -size); |
| 66 | glTexCoord2f(1, 0); glVertex2f( size, -size); |
| 67 | glTexCoord2f(1, 1); glVertex2f( size, size); |
| 68 | glTexCoord2f(0, 1); glVertex2f(-size, size); |
| 69 | glEnd(); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | static void |
Brian | 271d504 | 2007-01-16 15:27:11 -0700 | [diff] [blame] | 74 | Cube(GLfloat size) |
| 75 | { |
| 76 | /* +X */ |
| 77 | glPushMatrix(); |
| 78 | glRotatef(90, 0, 1, 0); |
| 79 | glTranslatef(0, 0, size); |
| 80 | Square(size); |
| 81 | glPopMatrix(); |
| 82 | |
| 83 | /* -X */ |
| 84 | glPushMatrix(); |
| 85 | glRotatef(-90, 0, 1, 0); |
| 86 | glTranslatef(0, 0, size); |
| 87 | Square(size); |
| 88 | glPopMatrix(); |
| 89 | |
| 90 | /* +Y */ |
| 91 | glPushMatrix(); |
| 92 | glRotatef(90, 1, 0, 0); |
| 93 | glTranslatef(0, 0, size); |
| 94 | Square(size); |
| 95 | glPopMatrix(); |
| 96 | |
| 97 | /* -Y */ |
| 98 | glPushMatrix(); |
| 99 | glRotatef(-90, 1, 0, 0); |
| 100 | glTranslatef(0, 0, size); |
| 101 | Square(size); |
| 102 | glPopMatrix(); |
| 103 | |
| 104 | |
| 105 | /* +Z */ |
| 106 | glPushMatrix(); |
| 107 | glTranslatef(0, 0, size); |
| 108 | Square(size); |
| 109 | glPopMatrix(); |
| 110 | |
| 111 | /* -Z */ |
| 112 | glPushMatrix(); |
| 113 | glRotatef(180, 0, 1, 0); |
| 114 | glTranslatef(0, 0, size); |
| 115 | Square(size); |
| 116 | glPopMatrix(); |
| 117 | |
| 118 | } |
| 119 | |
| 120 | |
| 121 | static void |
| 122 | Idle(void) |
| 123 | { |
| 124 | GLint t = glutGet(GLUT_ELAPSED_TIME); |
| 125 | yRot = t * 0.05; |
| 126 | glutPostRedisplay(); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | static void |
Brian | f44ba11 | 2007-01-16 14:55:43 -0700 | [diff] [blame] | 131 | Redisplay(void) |
| 132 | { |
| 133 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 134 | |
| 135 | glPushMatrix(); |
| 136 | glRotatef(xRot, 1.0f, 0.0f, 0.0f); |
| 137 | glRotatef(yRot, 0.0f, 1.0f, 0.0f); |
| 138 | glRotatef(zRot, 0.0f, 0.0f, 1.0f); |
| 139 | |
Brian | 271d504 | 2007-01-16 15:27:11 -0700 | [diff] [blame] | 140 | Cube(1.5); |
Brian | f44ba11 | 2007-01-16 14:55:43 -0700 | [diff] [blame] | 141 | |
| 142 | glPopMatrix(); |
| 143 | |
Brian | f44ba11 | 2007-01-16 14:55:43 -0700 | [diff] [blame] | 144 | CheckError(__LINE__); |
| 145 | |
| 146 | glutSwapBuffers(); |
| 147 | } |
| 148 | |
| 149 | |
| 150 | static void |
| 151 | Reshape(int width, int height) |
| 152 | { |
Brian Paul | 08a1e1e | 2009-04-09 17:04:58 -0600 | [diff] [blame] | 153 | float ar = (float) width / (float) height; |
Brian | f44ba11 | 2007-01-16 14:55:43 -0700 | [diff] [blame] | 154 | glViewport(0, 0, width, height); |
| 155 | glMatrixMode(GL_PROJECTION); |
| 156 | glLoadIdentity(); |
Brian Paul | 08a1e1e | 2009-04-09 17:04:58 -0600 | [diff] [blame] | 157 | glFrustum(-ar, ar, -1.0, 1.0, 5.0, 25.0); |
Brian | f44ba11 | 2007-01-16 14:55:43 -0700 | [diff] [blame] | 158 | glMatrixMode(GL_MODELVIEW); |
| 159 | glLoadIdentity(); |
| 160 | glTranslatef(0.0f, 0.0f, -15.0f); |
| 161 | } |
| 162 | |
| 163 | |
| 164 | static void |
| 165 | CleanUp(void) |
| 166 | { |
| 167 | glDeleteShader_func(fragShader); |
| 168 | glDeleteShader_func(vertShader); |
| 169 | glDeleteProgram_func(program); |
| 170 | glutDestroyWindow(win); |
| 171 | } |
| 172 | |
| 173 | |
| 174 | static void |
| 175 | Key(unsigned char key, int x, int y) |
| 176 | { |
| 177 | const GLfloat step = 2.0; |
| 178 | (void) x; |
| 179 | (void) y; |
| 180 | |
| 181 | switch(key) { |
Brian | 271d504 | 2007-01-16 15:27:11 -0700 | [diff] [blame] | 182 | case 'a': |
| 183 | Anim = !Anim; |
| 184 | glutIdleFunc(Anim ? Idle : NULL); |
| 185 | break; |
Brian | f44ba11 | 2007-01-16 14:55:43 -0700 | [diff] [blame] | 186 | case 'z': |
| 187 | zRot += step; |
| 188 | break; |
| 189 | case 'Z': |
| 190 | zRot -= step; |
| 191 | break; |
| 192 | case 27: |
| 193 | CleanUp(); |
| 194 | exit(0); |
| 195 | break; |
| 196 | } |
| 197 | glutPostRedisplay(); |
| 198 | } |
| 199 | |
| 200 | |
| 201 | static void |
| 202 | SpecialKey(int key, int x, int y) |
| 203 | { |
| 204 | const GLfloat step = 2.0; |
| 205 | |
| 206 | (void) x; |
| 207 | (void) y; |
| 208 | |
| 209 | switch(key) { |
| 210 | case GLUT_KEY_UP: |
| 211 | xRot += step; |
| 212 | break; |
| 213 | case GLUT_KEY_DOWN: |
| 214 | xRot -= step; |
| 215 | break; |
| 216 | case GLUT_KEY_LEFT: |
| 217 | yRot -= step; |
| 218 | break; |
| 219 | case GLUT_KEY_RIGHT: |
| 220 | yRot += step; |
| 221 | break; |
| 222 | } |
| 223 | glutPostRedisplay(); |
| 224 | } |
| 225 | |
| 226 | |
Brian | f44ba11 | 2007-01-16 14:55:43 -0700 | [diff] [blame] | 227 | static void |
| 228 | Init(void) |
| 229 | { |
Brian | 2dca337 | 2008-04-09 22:28:23 -0600 | [diff] [blame] | 230 | if (!ShadersSupported()) |
| 231 | exit(1); |
Brian | f44ba11 | 2007-01-16 14:55:43 -0700 | [diff] [blame] | 232 | |
| 233 | GetExtensionFuncs(); |
| 234 | |
Brian | 2dca337 | 2008-04-09 22:28:23 -0600 | [diff] [blame] | 235 | vertShader = CompileShaderFile(GL_VERTEX_SHADER, VertProgFile); |
| 236 | fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, FragProgFile); |
| 237 | program = LinkShaders(vertShader, fragShader); |
Brian | f44ba11 | 2007-01-16 14:55:43 -0700 | [diff] [blame] | 238 | |
Brian | f44ba11 | 2007-01-16 14:55:43 -0700 | [diff] [blame] | 239 | glUseProgram_func(program); |
| 240 | |
| 241 | assert(glIsProgram_func(program)); |
| 242 | assert(glIsShader_func(fragShader)); |
| 243 | assert(glIsShader_func(vertShader)); |
| 244 | |
| 245 | assert(glGetError() == 0); |
| 246 | |
| 247 | CheckError(__LINE__); |
| 248 | |
Brian | 2dca337 | 2008-04-09 22:28:23 -0600 | [diff] [blame] | 249 | InitUniforms(program, Uniforms); |
Brian | f44ba11 | 2007-01-16 14:55:43 -0700 | [diff] [blame] | 250 | |
| 251 | CheckError(__LINE__); |
| 252 | |
| 253 | tangentAttrib = glGetAttribLocation_func(program, "Tangent"); |
| 254 | printf("Tangent Attrib: %d\n", tangentAttrib); |
| 255 | |
| 256 | assert(tangentAttrib >= 0); |
| 257 | |
| 258 | CheckError(__LINE__); |
| 259 | |
| 260 | glClearColor(0.4f, 0.4f, 0.8f, 0.0f); |
| 261 | |
| 262 | glEnable(GL_DEPTH_TEST); |
| 263 | |
| 264 | glColor3f(1, 0, 0); |
| 265 | } |
| 266 | |
| 267 | |
| 268 | static void |
| 269 | ParseOptions(int argc, char *argv[]) |
| 270 | { |
| 271 | int i; |
| 272 | for (i = 1; i < argc; i++) { |
| 273 | if (strcmp(argv[i], "-fs") == 0) { |
| 274 | FragProgFile = argv[i+1]; |
| 275 | } |
| 276 | else if (strcmp(argv[i], "-vs") == 0) { |
| 277 | VertProgFile = argv[i+1]; |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | |
| 283 | int |
| 284 | main(int argc, char *argv[]) |
| 285 | { |
| 286 | glutInit(&argc, argv); |
| 287 | glutInitWindowPosition( 0, 0); |
| 288 | glutInitWindowSize(400, 400); |
| 289 | glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); |
| 290 | win = glutCreateWindow(argv[0]); |
Keith Whitwell | b799af9 | 2009-06-29 14:13:58 +0100 | [diff] [blame^] | 291 | glewInit(); |
Brian | f44ba11 | 2007-01-16 14:55:43 -0700 | [diff] [blame] | 292 | glutReshapeFunc(Reshape); |
| 293 | glutKeyboardFunc(Key); |
| 294 | glutSpecialFunc(SpecialKey); |
| 295 | glutDisplayFunc(Redisplay); |
| 296 | ParseOptions(argc, argv); |
| 297 | Init(); |
| 298 | glutMainLoop(); |
| 299 | return 0; |
| 300 | } |
| 301 | |