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