Brian | 2dfb03b | 2007-05-02 18:48:51 -0600 | [diff] [blame^] | 1 | /** |
| 2 | * Test OpenGL 2.0 dx/dy functions for texcoords. |
| 3 | * Brian Paul |
| 4 | * 2 May 2007 |
| 5 | * |
| 6 | * NOTE: resize the window to observe how the partial derivatives of |
| 7 | * the texcoords change. |
| 8 | */ |
| 9 | |
| 10 | |
| 11 | #include <assert.h> |
| 12 | #include <string.h> |
| 13 | #include <stdio.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <math.h> |
| 16 | #include <GL/gl.h> |
| 17 | #include <GL/glut.h> |
| 18 | #include <GL/glext.h> |
| 19 | #include "extfuncs.h" |
| 20 | |
| 21 | |
| 22 | static char *FragProgFile = NULL; |
| 23 | static char *VertProgFile = NULL; |
| 24 | static GLuint fragShader; |
| 25 | static GLuint vertShader; |
| 26 | static GLuint program; |
| 27 | static GLuint SphereList, RectList, CurList; |
| 28 | static GLint win = 0; |
| 29 | static GLboolean anim = GL_TRUE; |
| 30 | static GLfloat xRot = 0.0f, yRot = 0.0f; |
| 31 | |
| 32 | |
| 33 | static void |
| 34 | Redisplay(void) |
| 35 | { |
| 36 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 37 | |
| 38 | glPushMatrix(); |
| 39 | glRotatef(xRot, 1.0f, 0.0f, 0.0f); |
| 40 | glRotatef(yRot, 0.0f, 1.0f, 0.0f); |
| 41 | glCallList(CurList); |
| 42 | glPopMatrix(); |
| 43 | |
| 44 | glutSwapBuffers(); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | static void |
| 49 | Idle(void) |
| 50 | { |
| 51 | yRot = glutGet(GLUT_ELAPSED_TIME) * 0.1; |
| 52 | glutPostRedisplay(); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | static void |
| 57 | Reshape(int width, int height) |
| 58 | { |
| 59 | glViewport(0, 0, width, height); |
| 60 | glMatrixMode(GL_PROJECTION); |
| 61 | glLoadIdentity(); |
| 62 | glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0); |
| 63 | glMatrixMode(GL_MODELVIEW); |
| 64 | glLoadIdentity(); |
| 65 | glTranslatef(0.0f, 0.0f, -15.0f); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | static void |
| 70 | CleanUp(void) |
| 71 | { |
| 72 | glDeleteShader_func(fragShader); |
| 73 | glDeleteShader_func(vertShader); |
| 74 | glDeleteProgram_func(program); |
| 75 | glutDestroyWindow(win); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | static void |
| 80 | Key(unsigned char key, int x, int y) |
| 81 | { |
| 82 | (void) x; |
| 83 | (void) y; |
| 84 | |
| 85 | switch(key) { |
| 86 | case ' ': |
| 87 | case 'a': |
| 88 | anim = !anim; |
| 89 | if (anim) |
| 90 | glutIdleFunc(Idle); |
| 91 | else |
| 92 | glutIdleFunc(NULL); |
| 93 | break; |
| 94 | case 'o': |
| 95 | if (CurList == SphereList) |
| 96 | CurList = RectList; |
| 97 | else |
| 98 | CurList = SphereList; |
| 99 | break; |
| 100 | case 27: |
| 101 | CleanUp(); |
| 102 | exit(0); |
| 103 | break; |
| 104 | } |
| 105 | glutPostRedisplay(); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | static void |
| 110 | SpecialKey(int key, int x, int y) |
| 111 | { |
| 112 | const GLfloat step = 3.0f; |
| 113 | |
| 114 | (void) x; |
| 115 | (void) y; |
| 116 | |
| 117 | switch(key) { |
| 118 | case GLUT_KEY_UP: |
| 119 | xRot -= step; |
| 120 | break; |
| 121 | case GLUT_KEY_DOWN: |
| 122 | xRot += step; |
| 123 | break; |
| 124 | case GLUT_KEY_LEFT: |
| 125 | yRot -= step; |
| 126 | break; |
| 127 | case GLUT_KEY_RIGHT: |
| 128 | yRot += step; |
| 129 | break; |
| 130 | } |
| 131 | glutPostRedisplay(); |
| 132 | } |
| 133 | |
| 134 | |
| 135 | static void |
| 136 | MakeSphere(void) |
| 137 | { |
| 138 | GLUquadricObj *obj = gluNewQuadric(); |
| 139 | SphereList = glGenLists(1); |
| 140 | gluQuadricTexture(obj, GL_TRUE); |
| 141 | glNewList(SphereList, GL_COMPILE); |
| 142 | gluSphere(obj, 2.0f, 30, 15); |
| 143 | glEndList(); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | static void |
| 148 | MakeRect(void) |
| 149 | { |
| 150 | RectList = glGenLists(1); |
| 151 | glNewList(RectList, GL_COMPILE); |
| 152 | glNormal3f(0, 0, 1); |
| 153 | glBegin(GL_POLYGON); |
| 154 | glTexCoord2f(0, 0); glVertex2f(-2, -2); |
| 155 | glTexCoord2f(1, 0); glVertex2f( 2, -2); |
| 156 | glTexCoord2f(1, 1); glVertex2f( 2, 2); |
| 157 | glTexCoord2f(0, 1); glVertex2f(-2, 2); |
| 158 | glEnd(); |
| 159 | glEndList(); |
| 160 | } |
| 161 | |
| 162 | |
| 163 | |
| 164 | static void |
| 165 | LoadAndCompileShader(GLuint shader, const char *text) |
| 166 | { |
| 167 | GLint stat; |
| 168 | |
| 169 | glShaderSource_func(shader, 1, (const GLchar **) &text, NULL); |
| 170 | |
| 171 | glCompileShader_func(shader); |
| 172 | |
| 173 | glGetShaderiv_func(shader, GL_COMPILE_STATUS, &stat); |
| 174 | if (!stat) { |
| 175 | GLchar log[1000]; |
| 176 | GLsizei len; |
| 177 | glGetShaderInfoLog_func(shader, 1000, &len, log); |
| 178 | fprintf(stderr, "fslight: problem compiling shader:\n%s\n", log); |
| 179 | exit(1); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | |
| 184 | /** |
| 185 | * Read a shader from a file. |
| 186 | */ |
| 187 | static void |
| 188 | ReadShader(GLuint shader, const char *filename) |
| 189 | { |
| 190 | const int max = 100*1000; |
| 191 | int n; |
| 192 | char *buffer = (char*) malloc(max); |
| 193 | FILE *f = fopen(filename, "r"); |
| 194 | if (!f) { |
| 195 | fprintf(stderr, "fslight: Unable to open shader file %s\n", filename); |
| 196 | exit(1); |
| 197 | } |
| 198 | |
| 199 | n = fread(buffer, 1, max, f); |
| 200 | printf("fslight: read %d bytes from shader file %s\n", n, filename); |
| 201 | if (n > 0) { |
| 202 | buffer[n] = 0; |
| 203 | LoadAndCompileShader(shader, buffer); |
| 204 | } |
| 205 | |
| 206 | fclose(f); |
| 207 | free(buffer); |
| 208 | } |
| 209 | |
| 210 | |
| 211 | static void |
| 212 | CheckLink(GLuint prog) |
| 213 | { |
| 214 | GLint stat; |
| 215 | glGetProgramiv_func(prog, GL_LINK_STATUS, &stat); |
| 216 | if (!stat) { |
| 217 | GLchar log[1000]; |
| 218 | GLsizei len; |
| 219 | glGetProgramInfoLog_func(prog, 1000, &len, log); |
| 220 | fprintf(stderr, "Linker error:\n%s\n", log); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | |
| 225 | static void |
| 226 | Init(void) |
| 227 | { |
| 228 | static const char *fragShaderText = |
| 229 | "void main() {\n" |
| 230 | " gl_FragColor = abs(dFdy(gl_TexCoord[0])) * 50.0;\n" |
| 231 | " // gl_FragColor = gl_TexCoord[0];\n" |
| 232 | "}\n"; |
| 233 | static const char *vertShaderText = |
| 234 | "varying vec3 normal;\n" |
| 235 | "void main() {\n" |
| 236 | " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n" |
| 237 | " gl_TexCoord[0] = gl_MultiTexCoord0;\n" |
| 238 | "}\n"; |
| 239 | const char *version; |
| 240 | |
| 241 | version = (const char *) glGetString(GL_VERSION); |
| 242 | if (version[0] != '2' || version[1] != '.') { |
| 243 | printf("This program requires OpenGL 2.x, found %s\n", version); |
| 244 | exit(1); |
| 245 | } |
| 246 | |
| 247 | GetExtensionFuncs(); |
| 248 | |
| 249 | fragShader = glCreateShader_func(GL_FRAGMENT_SHADER); |
| 250 | if (FragProgFile) |
| 251 | ReadShader(fragShader, FragProgFile); |
| 252 | else |
| 253 | LoadAndCompileShader(fragShader, fragShaderText); |
| 254 | |
| 255 | vertShader = glCreateShader_func(GL_VERTEX_SHADER); |
| 256 | if (VertProgFile) |
| 257 | ReadShader(vertShader, VertProgFile); |
| 258 | else |
| 259 | LoadAndCompileShader(vertShader, vertShaderText); |
| 260 | |
| 261 | program = glCreateProgram_func(); |
| 262 | glAttachShader_func(program, fragShader); |
| 263 | glAttachShader_func(program, vertShader); |
| 264 | glLinkProgram_func(program); |
| 265 | CheckLink(program); |
| 266 | glUseProgram_func(program); |
| 267 | |
| 268 | /*assert(glGetError() == 0);*/ |
| 269 | |
| 270 | glClearColor(0.3f, 0.3f, 0.3f, 0.0f); |
| 271 | glEnable(GL_DEPTH_TEST); |
| 272 | |
| 273 | MakeSphere(); |
| 274 | MakeRect(); |
| 275 | |
| 276 | CurList = SphereList; |
| 277 | |
| 278 | printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER)); |
| 279 | |
| 280 | assert(glIsProgram_func(program)); |
| 281 | assert(glIsShader_func(fragShader)); |
| 282 | assert(glIsShader_func(vertShader)); |
| 283 | |
| 284 | glColor3f(1, 0, 0); |
| 285 | } |
| 286 | |
| 287 | |
| 288 | static void |
| 289 | ParseOptions(int argc, char *argv[]) |
| 290 | { |
| 291 | int i; |
| 292 | for (i = 1; i < argc; i++) { |
| 293 | if (strcmp(argv[i], "-fs") == 0) { |
| 294 | FragProgFile = argv[i+1]; |
| 295 | } |
| 296 | else if (strcmp(argv[i], "-vs") == 0) { |
| 297 | VertProgFile = argv[i+1]; |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | |
| 303 | int |
| 304 | main(int argc, char *argv[]) |
| 305 | { |
| 306 | glutInit(&argc, argv); |
| 307 | glutInitWindowPosition( 0, 0); |
| 308 | glutInitWindowSize(200, 200); |
| 309 | glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); |
| 310 | win = glutCreateWindow(argv[0]); |
| 311 | glutReshapeFunc(Reshape); |
| 312 | glutKeyboardFunc(Key); |
| 313 | glutSpecialFunc(SpecialKey); |
| 314 | glutDisplayFunc(Redisplay); |
| 315 | if (anim) |
| 316 | glutIdleFunc(Idle); |
| 317 | ParseOptions(argc, argv); |
| 318 | Init(); |
| 319 | glutMainLoop(); |
| 320 | return 0; |
| 321 | } |