Gary Wong | d427a29 | 2008-12-13 14:00:37 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Another test for noise() functions (noise1 to noise4 tested independently). |
| 3 | * 13 Dec 2008 |
| 4 | */ |
| 5 | |
| 6 | #include <assert.h> |
| 7 | #include <string.h> |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <math.h> |
Keith Whitwell | b799af9 | 2009-06-29 14:13:58 +0100 | [diff] [blame^] | 11 | #include <GL/glew.h> |
Gary Wong | d427a29 | 2008-12-13 14:00:37 -0700 | [diff] [blame] | 12 | #include <GL/gl.h> |
| 13 | #include <GL/glut.h> |
| 14 | #include <GL/glext.h> |
| 15 | #include "extfuncs.h" |
| 16 | |
| 17 | static const char *VertShaderText = |
| 18 | "void main() {\n" |
| 19 | " gl_TexCoord[0].xyz = gl_Vertex.xyz;\n" |
| 20 | " gl_TexCoord[0].w = gl_MultiTexCoord1.x;\n" |
| 21 | " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n" |
| 22 | "}\n"; |
| 23 | |
| 24 | static const char *FragShaderText[ 4 ] = { |
| 25 | "void main()\n" |
| 26 | "{\n" |
Gary Wong | a42342c | 2008-12-13 20:06:21 -0700 | [diff] [blame] | 27 | " gl_FragColor.rgb = noise3( gl_TexCoord[ 0 ].w ) * 0.5 + 0.5;\n" |
| 28 | " gl_FragColor.a = 1;\n" |
Gary Wong | d427a29 | 2008-12-13 14:00:37 -0700 | [diff] [blame] | 29 | "}\n", |
| 30 | "void main()\n" |
| 31 | "{\n" |
Gary Wong | a42342c | 2008-12-13 20:06:21 -0700 | [diff] [blame] | 32 | " gl_FragColor.rgb = noise3( gl_TexCoord[ 0 ].xw ) * 0.5 + 0.5;\n" |
| 33 | " gl_FragColor.a = 1;\n" |
Gary Wong | d427a29 | 2008-12-13 14:00:37 -0700 | [diff] [blame] | 34 | "}\n", |
| 35 | "void main()\n" |
| 36 | "{\n" |
Gary Wong | a42342c | 2008-12-13 20:06:21 -0700 | [diff] [blame] | 37 | " gl_FragColor.rgb = noise3( gl_TexCoord[ 0 ].xyw ) * 0.5 + 0.5;\n" |
| 38 | " gl_FragColor.a = 1;\n" |
Gary Wong | d427a29 | 2008-12-13 14:00:37 -0700 | [diff] [blame] | 39 | "}\n", |
| 40 | "void main()\n" |
| 41 | "{\n" |
Gary Wong | a42342c | 2008-12-13 20:06:21 -0700 | [diff] [blame] | 42 | " gl_FragColor.rgb = noise3( gl_TexCoord[ 0 ].xyzw ) * 0.5 + 0.5;\n" |
| 43 | " gl_FragColor.a = 1;\n" |
Gary Wong | d427a29 | 2008-12-13 14:00:37 -0700 | [diff] [blame] | 44 | "}\n" |
| 45 | }; |
| 46 | |
| 47 | struct uniform_info { |
| 48 | const char *name; |
| 49 | GLuint size; |
| 50 | GLint location; |
| 51 | GLfloat value[4]; |
| 52 | }; |
| 53 | |
| 54 | /* program/shader objects */ |
| 55 | static GLuint fragShader[ 4 ]; |
| 56 | static GLuint vertShader; |
| 57 | static GLuint program[ 4 ]; |
| 58 | |
| 59 | static GLint win = 0; |
| 60 | static GLfloat xRot = 0.0f, yRot = 0.0f, zRot = 0.0f; |
| 61 | static GLfloat Slice = 0.0; |
| 62 | static GLboolean Anim = GL_FALSE; |
| 63 | |
| 64 | |
| 65 | static void |
| 66 | Idle(void) |
| 67 | { |
| 68 | Slice += 0.01; |
| 69 | glutPostRedisplay(); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | static void |
| 74 | Redisplay(void) |
| 75 | { |
| 76 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 77 | |
| 78 | glMultiTexCoord1f( GL_TEXTURE1, Slice ); |
| 79 | |
| 80 | glPushMatrix(); |
| 81 | glRotatef(xRot, 1.0f, 0.0f, 0.0f); |
| 82 | glRotatef(yRot, 0.0f, 1.0f, 0.0f); |
| 83 | glRotatef(zRot, 0.0f, 0.0f, 1.0f); |
| 84 | |
| 85 | glutSolidTeapot( 1.0 ); |
| 86 | |
| 87 | glPopMatrix(); |
| 88 | |
| 89 | glutSwapBuffers(); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | static void |
| 94 | Reshape(int width, int height) |
| 95 | { |
| 96 | glViewport(0, 0, width, height); |
| 97 | glMatrixMode(GL_PROJECTION); |
| 98 | glLoadIdentity(); |
| 99 | glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0); |
| 100 | glMatrixMode(GL_MODELVIEW); |
| 101 | glLoadIdentity(); |
| 102 | glTranslatef(0.0f, 0.0f, -15.0f); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | static void |
| 107 | CleanUp(void) |
| 108 | { |
| 109 | GLint i; |
| 110 | |
| 111 | glDeleteShader_func(vertShader); |
| 112 | for( i = 0; i < 4; i++ ) { |
| 113 | glDeleteShader_func(fragShader[ i ]); |
| 114 | glDeleteProgram_func(program[ i ]); |
| 115 | } |
| 116 | glutDestroyWindow(win); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | static void |
| 121 | Key(unsigned char key, int x, int y) |
| 122 | { |
| 123 | const GLfloat step = 0.01; |
| 124 | (void) x; |
| 125 | (void) y; |
| 126 | |
| 127 | switch(key) { |
| 128 | case 'a': |
| 129 | Anim = !Anim; |
| 130 | glutIdleFunc(Anim ? Idle : NULL); |
| 131 | case 's': |
| 132 | Slice -= step; |
| 133 | break; |
| 134 | case 'S': |
| 135 | Slice += step; |
| 136 | break; |
| 137 | case 'z': |
| 138 | zRot -= 1.0; |
| 139 | break; |
| 140 | case 'Z': |
| 141 | zRot += 1.0; |
| 142 | break; |
| 143 | case '1': |
| 144 | case '2': |
| 145 | case '3': |
| 146 | case '4': |
| 147 | glUseProgram_func(program[ key - '1' ]); |
| 148 | break; |
| 149 | case 27: |
| 150 | CleanUp(); |
| 151 | exit(0); |
| 152 | break; |
| 153 | } |
| 154 | glutPostRedisplay(); |
| 155 | } |
| 156 | |
| 157 | |
| 158 | static void |
| 159 | SpecialKey(int key, int x, int y) |
| 160 | { |
| 161 | const GLfloat step = 3.0f; |
| 162 | |
| 163 | (void) x; |
| 164 | (void) y; |
| 165 | |
| 166 | switch(key) { |
| 167 | case GLUT_KEY_UP: |
| 168 | xRot -= step; |
| 169 | break; |
| 170 | case GLUT_KEY_DOWN: |
| 171 | xRot += step; |
| 172 | break; |
| 173 | case GLUT_KEY_LEFT: |
| 174 | yRot -= step; |
| 175 | break; |
| 176 | case GLUT_KEY_RIGHT: |
| 177 | yRot += step; |
| 178 | break; |
| 179 | } |
| 180 | glutPostRedisplay(); |
| 181 | } |
| 182 | |
| 183 | |
| 184 | |
| 185 | static void |
| 186 | LoadAndCompileShader(GLuint shader, const char *text) |
| 187 | { |
| 188 | GLint stat; |
| 189 | |
| 190 | glShaderSource_func(shader, 1, (const GLchar **) &text, NULL); |
| 191 | |
| 192 | glCompileShader_func(shader); |
| 193 | |
| 194 | glGetShaderiv_func(shader, GL_COMPILE_STATUS, &stat); |
| 195 | if (!stat) { |
| 196 | GLchar log[1000]; |
| 197 | GLsizei len; |
| 198 | glGetShaderInfoLog_func(shader, 1000, &len, log); |
| 199 | fprintf(stderr, "noise: problem compiling shader: %s\n", log); |
| 200 | exit(1); |
| 201 | } |
| 202 | else { |
| 203 | printf("Shader compiled OK\n"); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | |
| 208 | static void |
| 209 | CheckLink(GLuint prog) |
| 210 | { |
| 211 | GLint stat; |
| 212 | glGetProgramiv_func(prog, GL_LINK_STATUS, &stat); |
| 213 | if (!stat) { |
| 214 | GLchar log[1000]; |
| 215 | GLsizei len; |
| 216 | glGetProgramInfoLog_func(prog, 1000, &len, log); |
| 217 | fprintf(stderr, "Linker error:\n%s\n", log); |
| 218 | } |
| 219 | else { |
| 220 | fprintf(stderr, "Link success!\n"); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | |
| 225 | static void |
| 226 | Init(void) |
| 227 | { |
| 228 | const char *version; |
| 229 | GLint i; |
| 230 | |
| 231 | version = (const char *) glGetString(GL_VERSION); |
| 232 | if (version[0] != '2' || version[1] != '.') { |
| 233 | printf("Warning: this program expects OpenGL 2.0\n"); |
| 234 | /*exit(1);*/ |
| 235 | } |
| 236 | |
| 237 | GetExtensionFuncs(); |
| 238 | |
| 239 | vertShader = glCreateShader_func(GL_VERTEX_SHADER); |
| 240 | LoadAndCompileShader(vertShader, VertShaderText); |
| 241 | |
| 242 | for( i = 0; i < 4; i++ ) { |
| 243 | fragShader[ i ] = glCreateShader_func(GL_FRAGMENT_SHADER); |
| 244 | LoadAndCompileShader(fragShader[ i ], FragShaderText[ i ]); |
| 245 | program[ i ] = glCreateProgram_func(); |
| 246 | glAttachShader_func(program[ i ], fragShader[ i ]); |
| 247 | glAttachShader_func(program[ i ], vertShader); |
| 248 | glLinkProgram_func(program[ i ]); |
| 249 | CheckLink(program[ i ]); |
| 250 | } |
| 251 | |
| 252 | glUseProgram_func(program[ 0 ]); |
| 253 | |
| 254 | assert(glGetError() == 0); |
| 255 | |
| 256 | glClearColor(0.4f, 0.4f, 0.8f, 0.0f); |
| 257 | |
| 258 | glColor3f(1, 0, 0); |
| 259 | |
| 260 | glFrontFace( GL_CW ); |
| 261 | glEnable( GL_CULL_FACE ); |
| 262 | glEnable( GL_DEPTH_TEST ); |
| 263 | } |
| 264 | |
| 265 | |
| 266 | int |
| 267 | main(int argc, char *argv[]) |
| 268 | { |
| 269 | glutInit(&argc, argv); |
| 270 | glutInitWindowPosition( 0, 0); |
| 271 | glutInitWindowSize(400, 400); |
| 272 | glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); |
| 273 | win = glutCreateWindow(argv[0]); |
Keith Whitwell | b799af9 | 2009-06-29 14:13:58 +0100 | [diff] [blame^] | 274 | glewInit(); |
Gary Wong | d427a29 | 2008-12-13 14:00:37 -0700 | [diff] [blame] | 275 | glutReshapeFunc(Reshape); |
| 276 | glutKeyboardFunc(Key); |
| 277 | glutSpecialFunc(SpecialKey); |
| 278 | glutDisplayFunc(Redisplay); |
| 279 | Init(); |
| 280 | glutMainLoop(); |
| 281 | return 0; |
| 282 | } |
| 283 | |