Brian | f94e4f2 | 2007-01-28 19:01:04 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Test noise() functions. |
| 3 | * 28 Jan 2007 |
| 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> |
Brian | f94e4f2 | 2007-01-28 19:01:04 -0700 | [diff] [blame] | 12 | #include <GL/gl.h> |
| 13 | #include <GL/glut.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 | f94e4f2 | 2007-01-28 19:01:04 -0700 | [diff] [blame] | 17 | |
| 18 | |
| 19 | static const char *VertShaderText = |
| 20 | "void main() {\n" |
| 21 | " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n" |
Brian | 21625d7 | 2007-02-25 12:46:56 -0700 | [diff] [blame] | 22 | " gl_TexCoord[0] = gl_MultiTexCoord0;\n" |
Brian | f94e4f2 | 2007-01-28 19:01:04 -0700 | [diff] [blame] | 23 | "}\n"; |
| 24 | |
| 25 | static const char *FragShaderText = |
| 26 | "uniform vec4 Scale, Bias;\n" |
| 27 | "uniform float Slice;\n" |
| 28 | "void main()\n" |
| 29 | "{\n" |
| 30 | " vec4 scale = vec4(5.0);\n" |
| 31 | " vec4 p;\n" |
| 32 | " p.xy = gl_TexCoord[0].xy;\n" |
| 33 | " p.z = Slice;\n" |
Gary Wong | 0df3dfa | 2008-12-13 12:58:18 -0700 | [diff] [blame] | 34 | " p.w = 0;\n" |
Brian | f94e4f2 | 2007-01-28 19:01:04 -0700 | [diff] [blame] | 35 | " vec4 n = noise4(p * scale);\n" |
| 36 | " gl_FragColor = n * Scale + Bias;\n" |
| 37 | "}\n"; |
| 38 | |
| 39 | |
Brian | f94e4f2 | 2007-01-28 19:01:04 -0700 | [diff] [blame] | 40 | static struct uniform_info Uniforms[] = { |
Brian | 2dca337 | 2008-04-09 22:28:23 -0600 | [diff] [blame] | 41 | { "Scale", 4, GL_FLOAT, { 0.5, 0.4, 0.0, 0}, -1 }, |
| 42 | { "Bias", 4, GL_FLOAT, { 0.5, 0.3, 0.0, 0}, -1 }, |
| 43 | { "Slice", 1, GL_FLOAT, { 0.5, 0, 0, 0}, -1 }, |
| 44 | END_OF_UNIFORMS |
Brian | f94e4f2 | 2007-01-28 19:01:04 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | /* program/shader objects */ |
| 48 | static GLuint fragShader; |
| 49 | static GLuint vertShader; |
| 50 | static GLuint program; |
| 51 | |
| 52 | static GLint win = 0; |
| 53 | static GLfloat xRot = 0.0f, yRot = 0.0f, zRot = 0.0f; |
| 54 | static GLfloat Slice = 0.0; |
| 55 | static GLboolean Anim = GL_FALSE; |
| 56 | |
| 57 | |
| 58 | static void |
| 59 | Idle(void) |
| 60 | { |
| 61 | Slice += 0.01; |
| 62 | glutPostRedisplay(); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | static void |
| 67 | Redisplay(void) |
| 68 | { |
| 69 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 70 | |
| 71 | glUniform1fv_func(Uniforms[2].location, 1, &Slice); |
| 72 | |
| 73 | glPushMatrix(); |
| 74 | glRotatef(xRot, 1.0f, 0.0f, 0.0f); |
| 75 | glRotatef(yRot, 0.0f, 1.0f, 0.0f); |
| 76 | glRotatef(zRot, 0.0f, 0.0f, 1.0f); |
| 77 | |
| 78 | glBegin(GL_POLYGON); |
| 79 | glTexCoord2f(0, 0); glVertex2f(-2, -2); |
| 80 | glTexCoord2f(1, 0); glVertex2f( 2, -2); |
| 81 | glTexCoord2f(1, 1); glVertex2f( 2, 2); |
| 82 | glTexCoord2f(0, 1); glVertex2f(-2, 2); |
| 83 | glEnd(); |
| 84 | |
| 85 | glPopMatrix(); |
| 86 | |
| 87 | glutSwapBuffers(); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | static void |
| 92 | Reshape(int width, int height) |
| 93 | { |
| 94 | glViewport(0, 0, width, height); |
| 95 | glMatrixMode(GL_PROJECTION); |
| 96 | glLoadIdentity(); |
| 97 | glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0); |
| 98 | glMatrixMode(GL_MODELVIEW); |
| 99 | glLoadIdentity(); |
| 100 | glTranslatef(0.0f, 0.0f, -15.0f); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | static void |
| 105 | CleanUp(void) |
| 106 | { |
| 107 | glDeleteShader_func(fragShader); |
| 108 | glDeleteShader_func(vertShader); |
| 109 | glDeleteProgram_func(program); |
| 110 | glutDestroyWindow(win); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | static void |
| 115 | Key(unsigned char key, int x, int y) |
| 116 | { |
| 117 | const GLfloat step = 0.01; |
| 118 | (void) x; |
| 119 | (void) y; |
| 120 | |
| 121 | switch(key) { |
| 122 | case 'a': |
| 123 | Anim = !Anim; |
| 124 | glutIdleFunc(Anim ? Idle : NULL); |
| 125 | case 's': |
| 126 | Slice -= step; |
| 127 | break; |
| 128 | case 'S': |
| 129 | Slice += step; |
| 130 | break; |
| 131 | case 'z': |
| 132 | zRot -= 1.0; |
| 133 | break; |
| 134 | case 'Z': |
| 135 | zRot += 1.0; |
| 136 | break; |
| 137 | case 27: |
| 138 | CleanUp(); |
| 139 | exit(0); |
| 140 | break; |
| 141 | } |
| 142 | glutPostRedisplay(); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | static void |
| 147 | SpecialKey(int key, int x, int y) |
| 148 | { |
| 149 | const GLfloat step = 3.0f; |
| 150 | |
| 151 | (void) x; |
| 152 | (void) y; |
| 153 | |
| 154 | switch(key) { |
| 155 | case GLUT_KEY_UP: |
| 156 | xRot -= step; |
| 157 | break; |
| 158 | case GLUT_KEY_DOWN: |
| 159 | xRot += step; |
| 160 | break; |
| 161 | case GLUT_KEY_LEFT: |
| 162 | yRot -= step; |
| 163 | break; |
| 164 | case GLUT_KEY_RIGHT: |
| 165 | yRot += step; |
| 166 | break; |
| 167 | } |
| 168 | glutPostRedisplay(); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | |
| 173 | static void |
Brian | f94e4f2 | 2007-01-28 19:01:04 -0700 | [diff] [blame] | 174 | Init(void) |
| 175 | { |
Brian | 2dca337 | 2008-04-09 22:28:23 -0600 | [diff] [blame] | 176 | if (!ShadersSupported()) |
| 177 | exit(1); |
Brian | f94e4f2 | 2007-01-28 19:01:04 -0700 | [diff] [blame] | 178 | |
| 179 | GetExtensionFuncs(); |
| 180 | |
Brian | 2dca337 | 2008-04-09 22:28:23 -0600 | [diff] [blame] | 181 | vertShader = CompileShaderText(GL_VERTEX_SHADER, VertShaderText); |
| 182 | fragShader = CompileShaderText(GL_FRAGMENT_SHADER, FragShaderText); |
| 183 | program = LinkShaders(vertShader, fragShader); |
Brian | f94e4f2 | 2007-01-28 19:01:04 -0700 | [diff] [blame] | 184 | |
Brian | f94e4f2 | 2007-01-28 19:01:04 -0700 | [diff] [blame] | 185 | glUseProgram_func(program); |
| 186 | |
Brian | 2dca337 | 2008-04-09 22:28:23 -0600 | [diff] [blame] | 187 | InitUniforms(program, Uniforms); |
Brian | f94e4f2 | 2007-01-28 19:01:04 -0700 | [diff] [blame] | 188 | |
| 189 | assert(glGetError() == 0); |
| 190 | |
| 191 | glClearColor(0.4f, 0.4f, 0.8f, 0.0f); |
| 192 | |
| 193 | printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER)); |
| 194 | |
| 195 | assert(glIsProgram_func(program)); |
| 196 | assert(glIsShader_func(fragShader)); |
| 197 | assert(glIsShader_func(vertShader)); |
| 198 | |
| 199 | glColor3f(1, 0, 0); |
| 200 | } |
| 201 | |
| 202 | |
| 203 | int |
| 204 | main(int argc, char *argv[]) |
| 205 | { |
| 206 | glutInit(&argc, argv); |
| 207 | glutInitWindowPosition( 0, 0); |
| 208 | glutInitWindowSize(400, 400); |
| 209 | glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); |
| 210 | win = glutCreateWindow(argv[0]); |
Keith Whitwell | b799af9 | 2009-06-29 14:13:58 +0100 | [diff] [blame^] | 211 | glewInit(); |
Brian | f94e4f2 | 2007-01-28 19:01:04 -0700 | [diff] [blame] | 212 | glutReshapeFunc(Reshape); |
| 213 | glutKeyboardFunc(Key); |
| 214 | glutSpecialFunc(SpecialKey); |
| 215 | glutDisplayFunc(Redisplay); |
| 216 | Init(); |
| 217 | glutMainLoop(); |
| 218 | return 0; |
| 219 | } |
| 220 | |