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