Brian | 39091cc | 2008-12-02 22:51:39 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Vertex shader texture sampling test. |
| 3 | * Brian Paul |
| 4 | * 2 Dec 2008 |
| 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 | 39091cc | 2008-12-02 22:51:39 -0700 | [diff] [blame] | 13 | #include <GL/gl.h> |
| 14 | #include <GL/glut.h> |
| 15 | #include <GL/glext.h> |
| 16 | #include "extfuncs.h" |
| 17 | #include "shaderutil.h" |
| 18 | |
| 19 | |
| 20 | static const char *VertShaderText = |
| 21 | "uniform sampler2D tex1; \n" |
| 22 | "void main() \n" |
| 23 | "{ \n" |
| 24 | " vec4 pos = gl_Vertex; \n" |
| 25 | " pos.z = texture2D(tex1, gl_MultiTexCoord0.xy).x - 0.5; \n" |
| 26 | " gl_Position = gl_ModelViewProjectionMatrix * pos; \n" |
| 27 | " gl_FrontColor = pos; \n" |
| 28 | "} \n"; |
| 29 | |
| 30 | static const char *FragShaderText = |
| 31 | "void main() \n" |
| 32 | "{ \n" |
| 33 | " gl_FragColor = gl_Color; \n" |
| 34 | "} \n"; |
| 35 | |
| 36 | |
| 37 | static GLuint fragShader; |
| 38 | static GLuint vertShader; |
| 39 | static GLuint program; |
| 40 | |
| 41 | static GLint win = 0; |
| 42 | static GLboolean Anim = GL_TRUE; |
| 43 | static GLboolean WireFrame = GL_TRUE; |
| 44 | static GLfloat xRot = -70.0f, yRot = 0.0f, zRot = 0.0f; |
| 45 | |
| 46 | |
| 47 | /* value[0] = tex unit */ |
| 48 | static struct uniform_info Uniforms[] = { |
| 49 | { "tex1", 1, GL_INT, { 0, 0, 0, 0 }, -1 }, |
| 50 | END_OF_UNIFORMS |
| 51 | }; |
| 52 | |
| 53 | |
| 54 | |
| 55 | static void |
| 56 | Idle(void) |
| 57 | { |
| 58 | zRot = 90 + glutGet(GLUT_ELAPSED_TIME) * 0.05; |
| 59 | glutPostRedisplay(); |
| 60 | } |
| 61 | |
| 62 | |
| 63 | static void |
| 64 | DrawMesh(void) |
| 65 | { |
| 66 | GLfloat xmin = -2.0, xmax = 2.0; |
| 67 | GLfloat ymin = -2.0, ymax = 2.0; |
| 68 | GLuint xdivs = 20, ydivs = 20; |
| 69 | GLfloat dx = (xmax - xmin) / xdivs; |
| 70 | GLfloat dy = (ymax - ymin) / ydivs; |
| 71 | GLfloat ds = 1.0 / xdivs, dt = 1.0 / ydivs; |
| 72 | GLfloat x, y, s, t; |
| 73 | GLuint i, j; |
| 74 | |
| 75 | y = ymin; |
| 76 | t = 0.0; |
| 77 | for (i = 0; i < ydivs; i++) { |
| 78 | x = xmin; |
| 79 | s = 0.0; |
| 80 | glBegin(GL_QUAD_STRIP); |
| 81 | for (j = 0; j < xdivs; j++) { |
| 82 | glTexCoord2f(s, t); |
| 83 | glVertex2f(x, y); |
| 84 | glTexCoord2f(s, t + dt); |
| 85 | glVertex2f(x, y + dy); |
| 86 | x += dx; |
| 87 | s += ds; |
| 88 | } |
| 89 | glEnd(); |
| 90 | y += dy; |
| 91 | t += dt; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | |
| 96 | static void |
| 97 | Redisplay(void) |
| 98 | { |
| 99 | if (WireFrame) |
| 100 | glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); |
| 101 | else |
| 102 | glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); |
| 103 | |
| 104 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 105 | |
| 106 | glPushMatrix(); |
| 107 | glRotatef(xRot, 1.0f, 0.0f, 0.0f); |
| 108 | glRotatef(yRot, 0.0f, 1.0f, 0.0f); |
| 109 | glRotatef(zRot, 0.0f, 0.0f, 1.0f); |
| 110 | |
| 111 | glPushMatrix(); |
| 112 | DrawMesh(); |
| 113 | glPopMatrix(); |
| 114 | |
| 115 | glPopMatrix(); |
| 116 | |
| 117 | glutSwapBuffers(); |
| 118 | } |
| 119 | |
| 120 | |
| 121 | static void |
| 122 | Reshape(int width, int height) |
| 123 | { |
| 124 | glViewport(0, 0, width, height); |
| 125 | glMatrixMode(GL_PROJECTION); |
| 126 | glLoadIdentity(); |
| 127 | glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0); |
| 128 | glMatrixMode(GL_MODELVIEW); |
| 129 | glLoadIdentity(); |
| 130 | glTranslatef(0.0f, 0.0f, -15.0f); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | static void |
| 135 | CleanUp(void) |
| 136 | { |
| 137 | glDeleteShader_func(fragShader); |
| 138 | glDeleteShader_func(vertShader); |
| 139 | glDeleteProgram_func(program); |
| 140 | glutDestroyWindow(win); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | static void |
| 145 | Key(unsigned char key, int x, int y) |
| 146 | { |
| 147 | const GLfloat step = 2.0; |
| 148 | (void) x; |
| 149 | (void) y; |
| 150 | |
| 151 | switch(key) { |
| 152 | case 'a': |
| 153 | Anim = !Anim; |
| 154 | if (Anim) |
| 155 | glutIdleFunc(Idle); |
| 156 | else |
| 157 | glutIdleFunc(NULL); |
| 158 | break; |
| 159 | case 'w': |
| 160 | WireFrame = !WireFrame; |
| 161 | break; |
| 162 | case 'z': |
| 163 | zRot += step; |
| 164 | break; |
| 165 | case 'Z': |
| 166 | zRot -= step; |
| 167 | break; |
| 168 | case 27: |
| 169 | CleanUp(); |
| 170 | exit(0); |
| 171 | break; |
| 172 | } |
| 173 | glutPostRedisplay(); |
| 174 | } |
| 175 | |
| 176 | |
| 177 | static void |
| 178 | SpecialKey(int key, int x, int y) |
| 179 | { |
| 180 | const GLfloat step = 2.0; |
| 181 | |
| 182 | (void) x; |
| 183 | (void) y; |
| 184 | |
| 185 | switch(key) { |
| 186 | case GLUT_KEY_UP: |
| 187 | xRot += step; |
| 188 | break; |
| 189 | case GLUT_KEY_DOWN: |
| 190 | xRot -= step; |
| 191 | break; |
| 192 | case GLUT_KEY_LEFT: |
| 193 | yRot -= step; |
| 194 | break; |
| 195 | case GLUT_KEY_RIGHT: |
| 196 | yRot += step; |
| 197 | break; |
| 198 | } |
| 199 | glutPostRedisplay(); |
| 200 | } |
| 201 | |
| 202 | |
| 203 | static void |
| 204 | MakeTexture(void) |
| 205 | { |
| 206 | const GLuint texWidth = 64, texHeight = 64; |
| 207 | GLfloat texImage[64][64]; |
| 208 | GLuint i, j; |
| 209 | |
| 210 | /* texture is basically z = f(x, y) */ |
| 211 | for (i = 0; i < texHeight; i++) { |
| 212 | GLfloat y = 2.0 * (i / (float) (texHeight - 1)) - 1.0; |
| 213 | for (j = 0; j < texWidth; j++) { |
| 214 | GLfloat x = 2.0 * (j / (float) (texWidth - 1)) - 1.0; |
| 215 | GLfloat z = 0.5 + 0.5 * (sin(4.0 * x) * sin(4.0 * y)); |
| 216 | texImage[i][j] = z; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | glActiveTexture(GL_TEXTURE0); |
| 221 | glBindTexture(GL_TEXTURE_2D, 42); |
| 222 | glTexImage2D(GL_TEXTURE_2D, 0, GL_INTENSITY, texWidth, texHeight, 0, |
| 223 | GL_LUMINANCE, GL_FLOAT, texImage); |
| 224 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 225 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 226 | } |
| 227 | |
| 228 | |
| 229 | static void |
| 230 | Init(void) |
| 231 | { |
| 232 | GLint m; |
| 233 | |
| 234 | glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS_ARB, &m); |
| 235 | if (m < 1) { |
| 236 | printf("Error: no vertex shader texture units supported.\n"); |
| 237 | exit(1); |
| 238 | } |
| 239 | |
| 240 | if (!ShadersSupported()) |
| 241 | exit(1); |
| 242 | |
| 243 | GetExtensionFuncs(); |
| 244 | |
| 245 | vertShader = CompileShaderText(GL_VERTEX_SHADER, VertShaderText); |
| 246 | fragShader = CompileShaderText(GL_FRAGMENT_SHADER, FragShaderText); |
| 247 | program = LinkShaders(vertShader, fragShader); |
| 248 | |
| 249 | glUseProgram_func(program); |
| 250 | |
| 251 | assert(glGetError() == 0); |
| 252 | |
| 253 | MakeTexture(); |
| 254 | |
| 255 | glClearColor(0.4f, 0.4f, 0.8f, 0.0f); |
| 256 | |
| 257 | glEnable(GL_DEPTH_TEST); |
| 258 | |
| 259 | glColor3f(1, 1, 1); |
| 260 | } |
| 261 | |
| 262 | |
| 263 | int |
| 264 | main(int argc, char *argv[]) |
| 265 | { |
| 266 | glutInit(&argc, argv); |
| 267 | glutInitWindowSize(500, 500); |
| 268 | glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); |
| 269 | win = glutCreateWindow(argv[0]); |
Keith Whitwell | b799af9 | 2009-06-29 14:13:58 +0100 | [diff] [blame] | 270 | glewInit(); |
Brian | 39091cc | 2008-12-02 22:51:39 -0700 | [diff] [blame] | 271 | glutReshapeFunc(Reshape); |
| 272 | glutKeyboardFunc(Key); |
| 273 | glutSpecialFunc(SpecialKey); |
| 274 | glutDisplayFunc(Redisplay); |
| 275 | Init(); |
| 276 | if (Anim) |
| 277 | glutIdleFunc(Idle); |
| 278 | glutMainLoop(); |
| 279 | return 0; |
| 280 | } |
| 281 | |