ARB prog parser: Delete the old parser
[profile/ivi/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 }
144
145
146 static void
147 MakeRect(void)
148 {
149    RectList = glGenLists(1);
150    glNewList(RectList, GL_COMPILE);
151    glBegin(GL_POLYGON);
152    glTexCoord2f(0, 0);   glVertex2f(-2, -2);
153    glTexCoord2f(1, 0);   glVertex2f( 2, -2);
154    glTexCoord2f(1, 1);   glVertex2f( 2,  2);
155    glTexCoord2f(0, 1);   glVertex2f(-2,  2);
156    glEnd();
157    glEndList();
158 }
159
160
161 static void
162 Init(void)
163 {
164    static const char *fragShaderText =
165       "void main() {\n"
166       "   gl_FragColor = abs(dFdy(gl_TexCoord[0])) * 50.0;\n"
167       "  // gl_FragColor = gl_TexCoord[0];\n"
168       "}\n";
169    static const char *vertShaderText =
170       "void main() {\n"
171       "   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
172       "   gl_TexCoord[0] = gl_MultiTexCoord0;\n"
173       "}\n";
174
175    if (!ShadersSupported())
176       exit(1);
177
178    vertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText);
179    fragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText);
180    program = LinkShaders(vertShader, fragShader);
181
182    glUseProgram(program);
183
184    /*assert(glGetError() == 0);*/
185
186    glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
187    glEnable(GL_DEPTH_TEST);
188
189    MakeSphere();
190    MakeRect();
191
192    CurList = SphereList;
193
194    printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
195
196    assert(glIsProgram(program));
197    assert(glIsShader(fragShader));
198    assert(glIsShader(vertShader));
199
200    glColor3f(1, 0, 0);
201 }
202
203
204 static void
205 ParseOptions(int argc, char *argv[])
206 {
207    int i;
208    for (i = 1; i < argc; i++) {
209       if (strcmp(argv[i], "-fs") == 0) {
210          FragProgFile = argv[i+1];
211       }
212       else if (strcmp(argv[i], "-vs") == 0) {
213          VertProgFile = argv[i+1];
214       }
215    }
216 }
217
218
219 int
220 main(int argc, char *argv[])
221 {
222    glutInit(&argc, argv);
223    glutInitWindowPosition( 0, 0);
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 }