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.
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
;
29 static GLboolean anim
= GL_TRUE
;
30 static GLfloat xRot
= 0.0f
, yRot
= 0.0f
;
36 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
39 glRotatef(xRot
, 1.0f
, 0.0f
, 0.0f
);
40 glRotatef(yRot
, 0.0f
, 1.0f
, 0.0f
);
51 yRot
= glutGet(GLUT_ELAPSED_TIME
) * 0.1;
57 Reshape(int width
, int height
)
59 glViewport(0, 0, width
, height
);
60 glMatrixMode(GL_PROJECTION
);
62 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
63 glMatrixMode(GL_MODELVIEW
);
65 glTranslatef(0.0f
, 0.0f
, -15.0f
);
72 glDeleteShader_func(fragShader
);
73 glDeleteShader_func(vertShader
);
74 glDeleteProgram_func(program
);
75 glutDestroyWindow(win
);
80 Key(unsigned char key
, int x
, int y
)
95 if (CurList
== SphereList
)
110 SpecialKey(int key
, int x
, int y
)
112 const GLfloat step
= 3.0f
;
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);
150 RectList
= glGenLists(1);
151 glNewList(RectList
, GL_COMPILE
);
153 glTexCoord2f(0, 0); glVertex2f(-2, -2);
154 glTexCoord2f(1, 0); glVertex2f( 2, -2);
155 glTexCoord2f(1, 1); glVertex2f( 2, 2);
156 glTexCoord2f(0, 1); glVertex2f(-2, 2);
164 LoadAndCompileShader(GLuint shader
, const char *text
)
168 glShaderSource_func(shader
, 1, (const GLchar
**) &text
, NULL
);
170 glCompileShader_func(shader
);
172 glGetShaderiv_func(shader
, GL_COMPILE_STATUS
, &stat
);
176 glGetShaderInfoLog_func(shader
, 1000, &len
, log
);
177 fprintf(stderr
, "fslight: problem compiling shader:\n%s\n", log
);
184 * Read a shader from a file.
187 ReadShader(GLuint shader
, const char *filename
)
189 const int max
= 100*1000;
191 char *buffer
= (char*) malloc(max
);
192 FILE *f
= fopen(filename
, "r");
194 fprintf(stderr
, "fslight: Unable to open shader file %s\n", filename
);
198 n
= fread(buffer
, 1, max
, f
);
199 printf("fslight: read %d bytes from shader file %s\n", n
, filename
);
202 LoadAndCompileShader(shader
, buffer
);
211 CheckLink(GLuint prog
)
214 glGetProgramiv_func(prog
, GL_LINK_STATUS
, &stat
);
218 glGetProgramInfoLog_func(prog
, 1000, &len
, log
);
219 fprintf(stderr
, "Linker error:\n%s\n", log
);
227 static const char *fragShaderText
=
229 " gl_FragColor = abs(dFdy(gl_TexCoord[0])) * 50.0;\n"
230 " // gl_FragColor = gl_TexCoord[0];\n"
232 static const char *vertShaderText
=
234 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
235 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
239 version
= (const char *) glGetString(GL_VERSION
);
240 if (version
[0] != '2' || version
[1] != '.') {
241 printf("This program requires OpenGL 2.x, found %s\n", version
);
247 fragShader
= glCreateShader_func(GL_FRAGMENT_SHADER
);
249 ReadShader(fragShader
, FragProgFile
);
251 LoadAndCompileShader(fragShader
, fragShaderText
);
253 vertShader
= glCreateShader_func(GL_VERTEX_SHADER
);
255 ReadShader(vertShader
, VertProgFile
);
257 LoadAndCompileShader(vertShader
, vertShaderText
);
259 program
= glCreateProgram_func();
260 glAttachShader_func(program
, fragShader
);
261 glAttachShader_func(program
, vertShader
);
262 glLinkProgram_func(program
);
264 glUseProgram_func(program
);
266 /*assert(glGetError() == 0);*/
268 glClearColor(0.3f
, 0.3f
, 0.3f
, 0.0f
);
269 glEnable(GL_DEPTH_TEST
);
274 CurList
= SphereList
;
276 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER
));
278 assert(glIsProgram_func(program
));
279 assert(glIsShader_func(fragShader
));
280 assert(glIsShader_func(vertShader
));
287 ParseOptions(int argc
, char *argv
[])
290 for (i
= 1; i
< argc
; i
++) {
291 if (strcmp(argv
[i
], "-fs") == 0) {
292 FragProgFile
= argv
[i
+1];
294 else if (strcmp(argv
[i
], "-vs") == 0) {
295 VertProgFile
= argv
[i
+1];
302 main(int argc
, char *argv
[])
304 glutInit(&argc
, argv
);
305 glutInitWindowPosition( 0, 0);
306 glutInitWindowSize(200, 200);
307 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
308 win
= glutCreateWindow(argv
[0]);
309 glutReshapeFunc(Reshape
);
310 glutKeyboardFunc(Key
);
311 glutSpecialFunc(SpecialKey
);
312 glutDisplayFunc(Redisplay
);
315 ParseOptions(argc
, argv
);