2 * Test OpenGL 2.0 dx/dy functions for texcoords.
6 * NOTE: resize the window to observe how the partial derivatives of
7 * the texcoords change.
17 #include "glut_wrap.h"
18 #include "shaderutil.h"
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
;
28 static GLboolean anim
= GL_TRUE
;
29 static GLfloat xRot
= 0.0f
, yRot
= 0.0f
;
30 static GLint WinSize
[2];
31 static GLint WinSizeUniform
= -1;
37 glUniform2iv(WinSizeUniform
, 1, WinSize
);
39 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
42 glRotatef(xRot
, 1.0f
, 0.0f
, 0.0f
);
43 glRotatef(yRot
, 0.0f
, 1.0f
, 0.0f
);
54 yRot
= glutGet(GLUT_ELAPSED_TIME
) * 0.1;
60 Reshape(int width
, int height
)
64 glViewport(0, 0, width
, height
);
65 glMatrixMode(GL_PROJECTION
);
67 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
68 glMatrixMode(GL_MODELVIEW
);
70 glTranslatef(0.0f
, 0.0f
, -15.0f
);
77 glDeleteShader(fragShader
);
78 glDeleteShader(vertShader
);
79 glDeleteProgram(program
);
80 glutDestroyWindow(win
);
85 Key(unsigned char key
, int x
, int y
)
100 if (CurList
== SphereList
)
103 CurList
= SphereList
;
115 SpecialKey(int key
, int x
, int y
)
117 const GLfloat step
= 3.0f
;
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);
149 gluDeleteQuadric(obj
);
156 RectList
= glGenLists(1);
157 glNewList(RectList
, GL_COMPILE
);
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);
171 static const char *fragShaderText
=
172 "uniform ivec2 WinSize; \n"
174 " vec2 d = dFdy(gl_TexCoord[0].xy) * vec2(WinSize); \n"
175 " gl_FragColor = vec4(d.x, d.y, 0.0, 1.0);\n"
176 " // gl_FragColor = gl_TexCoord[0];\n"
178 static const char *vertShaderText
=
180 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
181 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
184 if (!ShadersSupported())
187 vertShader
= CompileShaderText(GL_VERTEX_SHADER
, vertShaderText
);
188 fragShader
= CompileShaderText(GL_FRAGMENT_SHADER
, fragShaderText
);
189 program
= LinkShaders(vertShader
, fragShader
);
191 glUseProgram(program
);
192 WinSizeUniform
= glGetUniformLocation(program
, "WinSize");
194 /*assert(glGetError() == 0);*/
196 glClearColor(0.3f
, 0.3f
, 0.3f
, 0.0f
);
197 glEnable(GL_DEPTH_TEST
);
202 CurList
= SphereList
;
204 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER
));
206 assert(glIsProgram(program
));
207 assert(glIsShader(fragShader
));
208 assert(glIsShader(vertShader
));
215 ParseOptions(int argc
, char *argv
[])
218 for (i
= 1; i
< argc
; i
++) {
219 if (strcmp(argv
[i
], "-fs") == 0) {
220 FragProgFile
= argv
[i
+1];
222 else if (strcmp(argv
[i
], "-vs") == 0) {
223 VertProgFile
= argv
[i
+1];
230 main(int argc
, char *argv
[])
232 WinSize
[0] = WinSize
[1] = 200;
234 glutInit(&argc
, argv
);
235 glutInitWindowSize(WinSize
[0], WinSize
[1]);
236 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
237 win
= glutCreateWindow(argv
[0]);
239 glutReshapeFunc(Reshape
);
240 glutKeyboardFunc(Key
);
241 glutSpecialFunc(SpecialKey
);
242 glutDisplayFunc(Redisplay
);
245 ParseOptions(argc
, argv
);