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