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> |
Keith Whitwell | b799af9 | 2009-06-29 14:13:58 +0100 | [diff] [blame^] | 16 | #include <GL/glew.h> |
Brian | 2dfb03b | 2007-05-02 18:48:51 -0600 | [diff] [blame] | 17 | #include <GL/gl.h> |
| 18 | #include <GL/glut.h> |
| 19 | #include <GL/glext.h> |
| 20 | #include "extfuncs.h" |
Brian | 2dca337 | 2008-04-09 22:28:23 -0600 | [diff] [blame] | 21 | #include "shaderutil.h" |
Brian | 2dfb03b | 2007-05-02 18:48:51 -0600 | [diff] [blame] | 22 | |
| 23 | |
| 24 | static char *FragProgFile = NULL; |
| 25 | static char *VertProgFile = NULL; |
| 26 | static GLuint fragShader; |
| 27 | static GLuint vertShader; |
| 28 | static GLuint program; |
| 29 | static GLuint SphereList, RectList, CurList; |
| 30 | static GLint win = 0; |
| 31 | static GLboolean anim = GL_TRUE; |
| 32 | static GLfloat xRot = 0.0f, yRot = 0.0f; |
| 33 | |
| 34 | |
| 35 | static void |
| 36 | Redisplay(void) |
| 37 | { |
| 38 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 39 | |
| 40 | glPushMatrix(); |
| 41 | glRotatef(xRot, 1.0f, 0.0f, 0.0f); |
| 42 | glRotatef(yRot, 0.0f, 1.0f, 0.0f); |
| 43 | glCallList(CurList); |
| 44 | glPopMatrix(); |
| 45 | |
| 46 | glutSwapBuffers(); |
| 47 | } |
| 48 | |
| 49 | |
| 50 | static void |
| 51 | Idle(void) |
| 52 | { |
| 53 | yRot = glutGet(GLUT_ELAPSED_TIME) * 0.1; |
| 54 | glutPostRedisplay(); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | static void |
| 59 | Reshape(int width, int height) |
| 60 | { |
| 61 | glViewport(0, 0, width, height); |
| 62 | glMatrixMode(GL_PROJECTION); |
| 63 | glLoadIdentity(); |
| 64 | glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0); |
| 65 | glMatrixMode(GL_MODELVIEW); |
| 66 | glLoadIdentity(); |
| 67 | glTranslatef(0.0f, 0.0f, -15.0f); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | static void |
| 72 | CleanUp(void) |
| 73 | { |
| 74 | glDeleteShader_func(fragShader); |
| 75 | glDeleteShader_func(vertShader); |
| 76 | glDeleteProgram_func(program); |
| 77 | glutDestroyWindow(win); |
| 78 | } |
| 79 | |
| 80 | |
| 81 | static void |
| 82 | Key(unsigned char key, int x, int y) |
| 83 | { |
| 84 | (void) x; |
| 85 | (void) y; |
| 86 | |
| 87 | switch(key) { |
| 88 | case ' ': |
| 89 | case 'a': |
| 90 | anim = !anim; |
| 91 | if (anim) |
| 92 | glutIdleFunc(Idle); |
| 93 | else |
| 94 | glutIdleFunc(NULL); |
| 95 | break; |
| 96 | case 'o': |
| 97 | if (CurList == SphereList) |
| 98 | CurList = RectList; |
| 99 | else |
| 100 | CurList = SphereList; |
| 101 | break; |
| 102 | case 27: |
| 103 | CleanUp(); |
| 104 | exit(0); |
| 105 | break; |
| 106 | } |
| 107 | glutPostRedisplay(); |
| 108 | } |
| 109 | |
| 110 | |
| 111 | static void |
| 112 | SpecialKey(int key, int x, int y) |
| 113 | { |
| 114 | const GLfloat step = 3.0f; |
| 115 | |
| 116 | (void) x; |
| 117 | (void) y; |
| 118 | |
| 119 | switch(key) { |
| 120 | case GLUT_KEY_UP: |
| 121 | xRot -= step; |
| 122 | break; |
| 123 | case GLUT_KEY_DOWN: |
| 124 | xRot += step; |
| 125 | break; |
| 126 | case GLUT_KEY_LEFT: |
| 127 | yRot -= step; |
| 128 | break; |
| 129 | case GLUT_KEY_RIGHT: |
| 130 | yRot += step; |
| 131 | break; |
| 132 | } |
| 133 | glutPostRedisplay(); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | static void |
| 138 | MakeSphere(void) |
| 139 | { |
| 140 | GLUquadricObj *obj = gluNewQuadric(); |
| 141 | SphereList = glGenLists(1); |
| 142 | gluQuadricTexture(obj, GL_TRUE); |
| 143 | glNewList(SphereList, GL_COMPILE); |
| 144 | gluSphere(obj, 2.0f, 30, 15); |
| 145 | glEndList(); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | static void |
| 150 | MakeRect(void) |
| 151 | { |
| 152 | RectList = glGenLists(1); |
| 153 | glNewList(RectList, GL_COMPILE); |
Brian | 2dfb03b | 2007-05-02 18:48:51 -0600 | [diff] [blame] | 154 | glBegin(GL_POLYGON); |
| 155 | glTexCoord2f(0, 0); glVertex2f(-2, -2); |
| 156 | glTexCoord2f(1, 0); glVertex2f( 2, -2); |
| 157 | glTexCoord2f(1, 1); glVertex2f( 2, 2); |
| 158 | glTexCoord2f(0, 1); glVertex2f(-2, 2); |
| 159 | glEnd(); |
| 160 | glEndList(); |
| 161 | } |
| 162 | |
| 163 | |
Brian | 2dfb03b | 2007-05-02 18:48:51 -0600 | [diff] [blame] | 164 | static void |
| 165 | Init(void) |
| 166 | { |
| 167 | static const char *fragShaderText = |
| 168 | "void main() {\n" |
| 169 | " gl_FragColor = abs(dFdy(gl_TexCoord[0])) * 50.0;\n" |
| 170 | " // gl_FragColor = gl_TexCoord[0];\n" |
| 171 | "}\n"; |
| 172 | static const char *vertShaderText = |
Brian | 2dfb03b | 2007-05-02 18:48:51 -0600 | [diff] [blame] | 173 | "void main() {\n" |
| 174 | " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n" |
| 175 | " gl_TexCoord[0] = gl_MultiTexCoord0;\n" |
| 176 | "}\n"; |
Brian | 2dfb03b | 2007-05-02 18:48:51 -0600 | [diff] [blame] | 177 | |
Brian | 2dca337 | 2008-04-09 22:28:23 -0600 | [diff] [blame] | 178 | if (!ShadersSupported()) |
Brian | 2dfb03b | 2007-05-02 18:48:51 -0600 | [diff] [blame] | 179 | exit(1); |
Brian | 2dfb03b | 2007-05-02 18:48:51 -0600 | [diff] [blame] | 180 | |
| 181 | GetExtensionFuncs(); |
| 182 | |
Brian | 2dca337 | 2008-04-09 22:28:23 -0600 | [diff] [blame] | 183 | vertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText); |
| 184 | fragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText); |
| 185 | program = LinkShaders(vertShader, fragShader); |
Brian | 2dfb03b | 2007-05-02 18:48:51 -0600 | [diff] [blame] | 186 | |
Brian | 2dfb03b | 2007-05-02 18:48:51 -0600 | [diff] [blame] | 187 | glUseProgram_func(program); |
| 188 | |
| 189 | /*assert(glGetError() == 0);*/ |
| 190 | |
| 191 | glClearColor(0.3f, 0.3f, 0.3f, 0.0f); |
| 192 | glEnable(GL_DEPTH_TEST); |
| 193 | |
| 194 | MakeSphere(); |
| 195 | MakeRect(); |
| 196 | |
| 197 | CurList = SphereList; |
| 198 | |
| 199 | printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER)); |
| 200 | |
| 201 | assert(glIsProgram_func(program)); |
| 202 | assert(glIsShader_func(fragShader)); |
| 203 | assert(glIsShader_func(vertShader)); |
| 204 | |
| 205 | glColor3f(1, 0, 0); |
| 206 | } |
| 207 | |
| 208 | |
| 209 | static void |
| 210 | ParseOptions(int argc, char *argv[]) |
| 211 | { |
| 212 | int i; |
| 213 | for (i = 1; i < argc; i++) { |
| 214 | if (strcmp(argv[i], "-fs") == 0) { |
| 215 | FragProgFile = argv[i+1]; |
| 216 | } |
| 217 | else if (strcmp(argv[i], "-vs") == 0) { |
| 218 | VertProgFile = argv[i+1]; |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | |
| 224 | int |
| 225 | main(int argc, char *argv[]) |
| 226 | { |
| 227 | glutInit(&argc, argv); |
| 228 | glutInitWindowPosition( 0, 0); |
| 229 | glutInitWindowSize(200, 200); |
| 230 | glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); |
| 231 | win = glutCreateWindow(argv[0]); |
Keith Whitwell | b799af9 | 2009-06-29 14:13:58 +0100 | [diff] [blame^] | 232 | glewInit(); |
Brian | 2dfb03b | 2007-05-02 18:48:51 -0600 | [diff] [blame] | 233 | glutReshapeFunc(Reshape); |
| 234 | glutKeyboardFunc(Key); |
| 235 | glutSpecialFunc(SpecialKey); |
| 236 | glutDisplayFunc(Redisplay); |
| 237 | if (anim) |
| 238 | glutIdleFunc(Idle); |
| 239 | ParseOptions(argc, argv); |
| 240 | Init(); |
| 241 | glutMainLoop(); |
| 242 | return 0; |
| 243 | } |