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