Merge commit 'origin/master' into gallium-0.2
[mesa.git] / progs / glsl / deriv.c
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>
16 #include <GL/gl.h>
17 #include <GL/glut.h>
18 #include <GL/glext.h>
19 #include "extfuncs.h"
20 #include "shaderutil.h"
21
22
23 static char *FragProgFile = NULL;
24 static char *VertProgFile = NULL;
25 static GLuint fragShader;
26 static GLuint vertShader;
27 static GLuint program;
28 static GLuint SphereList, RectList, CurList;
29 static GLint win = 0;
30 static GLboolean anim = GL_TRUE;
31 static GLfloat xRot = 0.0f, yRot = 0.0f;
32
33
34 static void
35 Redisplay(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
49 static void
50 Idle(void)
51 {
52 yRot = glutGet(GLUT_ELAPSED_TIME) * 0.1;
53 glutPostRedisplay();
54 }
55
56
57 static void
58 Reshape(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
70 static void
71 CleanUp(void)
72 {
73 glDeleteShader_func(fragShader);
74 glDeleteShader_func(vertShader);
75 glDeleteProgram_func(program);
76 glutDestroyWindow(win);
77 }
78
79
80 static void
81 Key(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
110 static void
111 SpecialKey(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
136 static void
137 MakeSphere(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
148 static void
149 MakeRect(void)
150 {
151 RectList = glGenLists(1);
152 glNewList(RectList, GL_COMPILE);
153 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
163 static void
164 Init(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 =
172 "void main() {\n"
173 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
174 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
175 "}\n";
176
177 if (!ShadersSupported())
178 exit(1);
179
180 GetExtensionFuncs();
181
182 vertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText);
183 fragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText);
184 program = LinkShaders(vertShader, fragShader);
185
186 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
208 static void
209 ParseOptions(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
223 int
224 main(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 }