Brian | 2ccd264 | 2007-01-15 17:27:24 -0700 | [diff] [blame] | 1 | /** |
| 2 | * "Brick" shader demo. Uses the example shaders from chapter 6 of |
| 3 | * the OpenGL Shading Language "orange" book. |
| 4 | * 10 Jan 2007 |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
| 8 | #include <string.h> |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <math.h> |
Keith Whitwell | b799af9 | 2009-06-29 14:13:58 +0100 | [diff] [blame^] | 12 | #include <GL/glew.h> |
Brian | 2ccd264 | 2007-01-15 17:27:24 -0700 | [diff] [blame] | 13 | #include <GL/gl.h> |
| 14 | #include <GL/glut.h> |
| 15 | #include <GL/glext.h> |
| 16 | #include "extfuncs.h" |
Brian | 2dca337 | 2008-04-09 22:28:23 -0600 | [diff] [blame] | 17 | #include "shaderutil.h" |
Brian | 2ccd264 | 2007-01-15 17:27:24 -0700 | [diff] [blame] | 18 | |
| 19 | |
Brian Paul | c0dd912 | 2008-08-16 09:36:46 -0600 | [diff] [blame] | 20 | static char *FragProgFile = "CH06-brick.frag"; |
| 21 | static char *VertProgFile = "CH06-brick.vert"; |
Brian | 2ccd264 | 2007-01-15 17:27:24 -0700 | [diff] [blame] | 22 | |
| 23 | /* program/shader objects */ |
| 24 | static GLuint fragShader; |
| 25 | static GLuint vertShader; |
| 26 | static GLuint program; |
| 27 | |
Brian | 2ccd264 | 2007-01-15 17:27:24 -0700 | [diff] [blame] | 28 | static struct uniform_info Uniforms[] = { |
| 29 | /* vert */ |
Brian | 2dca337 | 2008-04-09 22:28:23 -0600 | [diff] [blame] | 30 | { "LightPosition", 3, GL_FLOAT, { 0.1, 0.1, 9.0, 0}, -1 }, |
Brian | 2ccd264 | 2007-01-15 17:27:24 -0700 | [diff] [blame] | 31 | /* frag */ |
Brian | 2dca337 | 2008-04-09 22:28:23 -0600 | [diff] [blame] | 32 | { "BrickColor", 3, GL_FLOAT, { 0.8, 0.2, 0.2, 0 }, -1 }, |
| 33 | { "MortarColor", 3, GL_FLOAT, { 0.6, 0.6, 0.6, 0 }, -1 }, |
| 34 | { "BrickSize", 2, GL_FLOAT, { 1.0, 0.3, 0, 0 }, -1 }, |
| 35 | { "BrickPct", 2, GL_FLOAT, { 0.9, 0.8, 0, 0 }, -1 }, |
| 36 | END_OF_UNIFORMS |
Brian | 2ccd264 | 2007-01-15 17:27:24 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | static GLint win = 0; |
| 40 | |
| 41 | |
| 42 | static GLfloat xRot = 0.0f, yRot = 0.0f, zRot = 0.0f; |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | static void |
| 48 | Redisplay(void) |
| 49 | { |
| 50 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 51 | |
| 52 | glPushMatrix(); |
| 53 | glRotatef(xRot, 1.0f, 0.0f, 0.0f); |
| 54 | glRotatef(yRot, 0.0f, 1.0f, 0.0f); |
| 55 | glRotatef(zRot, 0.0f, 0.0f, 1.0f); |
| 56 | |
| 57 | glBegin(GL_POLYGON); |
| 58 | glTexCoord2f(0, 0); glVertex2f(-2, -2); |
| 59 | glTexCoord2f(1, 0); glVertex2f( 2, -2); |
| 60 | glTexCoord2f(1, 1); glVertex2f( 2, 2); |
| 61 | glTexCoord2f(0, 1); glVertex2f(-2, 2); |
| 62 | glEnd(); |
| 63 | |
| 64 | glPopMatrix(); |
| 65 | |
| 66 | glutSwapBuffers(); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | static void |
| 71 | Reshape(int width, int height) |
| 72 | { |
| 73 | glViewport(0, 0, width, height); |
| 74 | glMatrixMode(GL_PROJECTION); |
| 75 | glLoadIdentity(); |
| 76 | glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0); |
| 77 | glMatrixMode(GL_MODELVIEW); |
| 78 | glLoadIdentity(); |
| 79 | glTranslatef(0.0f, 0.0f, -15.0f); |
| 80 | } |
| 81 | |
| 82 | |
| 83 | static void |
| 84 | CleanUp(void) |
| 85 | { |
| 86 | glDeleteShader_func(fragShader); |
| 87 | glDeleteShader_func(vertShader); |
| 88 | glDeleteProgram_func(program); |
| 89 | glutDestroyWindow(win); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | static void |
| 94 | Key(unsigned char key, int x, int y) |
| 95 | { |
| 96 | (void) x; |
| 97 | (void) y; |
| 98 | |
| 99 | switch(key) { |
| 100 | case 'z': |
| 101 | zRot -= 1.0; |
| 102 | break; |
| 103 | case 'Z': |
| 104 | zRot += 1.0; |
| 105 | break; |
| 106 | case 27: |
| 107 | CleanUp(); |
| 108 | exit(0); |
| 109 | break; |
| 110 | } |
| 111 | glutPostRedisplay(); |
| 112 | } |
| 113 | |
| 114 | |
| 115 | static void |
| 116 | SpecialKey(int key, int x, int y) |
| 117 | { |
| 118 | const GLfloat step = 3.0f; |
| 119 | |
| 120 | (void) x; |
| 121 | (void) y; |
| 122 | |
| 123 | switch(key) { |
| 124 | case GLUT_KEY_UP: |
| 125 | xRot -= step; |
| 126 | break; |
| 127 | case GLUT_KEY_DOWN: |
| 128 | xRot += step; |
| 129 | break; |
| 130 | case GLUT_KEY_LEFT: |
| 131 | yRot -= step; |
| 132 | break; |
| 133 | case GLUT_KEY_RIGHT: |
| 134 | yRot += step; |
| 135 | break; |
| 136 | } |
| 137 | glutPostRedisplay(); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | |
| 142 | static void |
Brian | 2ccd264 | 2007-01-15 17:27:24 -0700 | [diff] [blame] | 143 | Init(void) |
| 144 | { |
Brian | 2dca337 | 2008-04-09 22:28:23 -0600 | [diff] [blame] | 145 | if (!ShadersSupported()) |
| 146 | exit(1); |
Brian | 2ccd264 | 2007-01-15 17:27:24 -0700 | [diff] [blame] | 147 | |
| 148 | GetExtensionFuncs(); |
| 149 | |
Brian | 2dca337 | 2008-04-09 22:28:23 -0600 | [diff] [blame] | 150 | vertShader = CompileShaderFile(GL_VERTEX_SHADER, VertProgFile); |
| 151 | fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, FragProgFile); |
| 152 | program = LinkShaders(vertShader, fragShader); |
Brian | 2ccd264 | 2007-01-15 17:27:24 -0700 | [diff] [blame] | 153 | |
Brian | 2ccd264 | 2007-01-15 17:27:24 -0700 | [diff] [blame] | 154 | glUseProgram_func(program); |
| 155 | |
Brian | 2dca337 | 2008-04-09 22:28:23 -0600 | [diff] [blame] | 156 | InitUniforms(program, Uniforms); |
Brian | 2ccd264 | 2007-01-15 17:27:24 -0700 | [diff] [blame] | 157 | |
| 158 | assert(glGetError() == 0); |
| 159 | |
| 160 | glClearColor(0.4f, 0.4f, 0.8f, 0.0f); |
| 161 | |
| 162 | printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER)); |
| 163 | |
| 164 | assert(glIsProgram_func(program)); |
| 165 | assert(glIsShader_func(fragShader)); |
| 166 | assert(glIsShader_func(vertShader)); |
| 167 | |
| 168 | glColor3f(1, 0, 0); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | static void |
| 173 | ParseOptions(int argc, char *argv[]) |
| 174 | { |
| 175 | int i; |
| 176 | for (i = 1; i < argc; i++) { |
| 177 | if (strcmp(argv[i], "-fs") == 0) { |
| 178 | FragProgFile = argv[i+1]; |
| 179 | } |
| 180 | else if (strcmp(argv[i], "-vs") == 0) { |
| 181 | VertProgFile = argv[i+1]; |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | |
| 187 | int |
| 188 | main(int argc, char *argv[]) |
| 189 | { |
| 190 | glutInit(&argc, argv); |
| 191 | glutInitWindowPosition( 0, 0); |
| 192 | glutInitWindowSize(400, 400); |
| 193 | glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); |
| 194 | win = glutCreateWindow(argv[0]); |
Keith Whitwell | b799af9 | 2009-06-29 14:13:58 +0100 | [diff] [blame^] | 195 | glewInit(); |
Brian | 2ccd264 | 2007-01-15 17:27:24 -0700 | [diff] [blame] | 196 | glutReshapeFunc(Reshape); |
| 197 | glutKeyboardFunc(Key); |
| 198 | glutSpecialFunc(SpecialKey); |
| 199 | glutDisplayFunc(Redisplay); |
| 200 | ParseOptions(argc, argv); |
| 201 | Init(); |
| 202 | glutMainLoop(); |
| 203 | return 0; |
| 204 | } |
| 205 | |