WIP - port to Mali EGL
[mesa-demos/mali.git] / src / glsl / fragcoord.c
blob81bd26e38db0d7b2f0d96ab31b886f0c4a4c4552
1 /**
2 * Test GLSL gl_FragCoord fragment program attribute.
3 * Color the quad's fragments according to their window position.
5 * Brian Paul
6 * 20 Nov 2008
7 */
10 #include <assert.h>
11 #include <string.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <math.h>
15 #include <GL/glew.h>
16 #include "glut_wrap.h"
17 #include "shaderutil.h"
20 static GLint WinWidth = 200, WinHeight = 200;
21 static char *FragProgFile = NULL;
22 static char *VertProgFile = NULL;
23 static GLuint fragShader;
24 static GLuint vertShader;
25 static GLuint program;
26 static GLint win = 0;
27 static GLboolean Anim = GL_TRUE;
28 static GLfloat PosX = 0.0, PosY = 0.0;
31 static void
32 Idle(void)
34 float r = (WinWidth < WinHeight) ? WinWidth : WinHeight;
35 float a = glutGet(GLUT_ELAPSED_TIME) * 0.001;
36 r *= 0.25;
37 PosX = WinWidth / 2 + r * cos(a);
38 PosY = WinHeight / 2 + r * sin(a);
40 glutPostRedisplay();
44 static void
45 Redisplay(void)
47 glClear(GL_COLOR_BUFFER_BIT);
49 glPushMatrix();
50 glTranslatef(PosX, PosY, 0.0);
51 #if 0
52 glBegin(GL_POLYGON);
53 glVertex2f(-50, -50);
54 glVertex2f( 50, -50);
55 glVertex2f( 50, 50);
56 glVertex2f(-50, 50);
57 glEnd();
58 #else
59 glutSolidSphere(50, 20, 10);
60 #endif
61 glPopMatrix();
63 glutSwapBuffers();
67 static void
68 Reshape(int width, int height)
70 glViewport(0, 0, width, height);
71 glMatrixMode(GL_PROJECTION);
72 glLoadIdentity();
73 glOrtho(0, width, 0, height, -55, 55);
75 glMatrixMode(GL_MODELVIEW);
76 glLoadIdentity();
78 WinWidth = width;
79 WinHeight = height;
83 static void
84 CleanUp(void)
86 glDeleteShader(fragShader);
87 glDeleteShader(vertShader);
88 glDeleteProgram(program);
89 glutDestroyWindow(win);
93 static void
94 Key(unsigned char key, int x, int y)
96 (void) x;
97 (void) y;
99 switch(key) {
100 case ' ':
101 case 'a':
102 Anim = !Anim;
103 glutIdleFunc(Anim ? Idle : NULL);
104 break;
105 case 27:
106 CleanUp();
107 exit(0);
108 break;
110 glutPostRedisplay();
114 static void
115 Init(void)
117 static const char *fragShaderText =
118 "void main() { \n"
119 " vec4 scale = vec4(.005, 0.005, 0.5, 1.0);\n"
120 " gl_FragColor = gl_FragCoord * scale; \n"
121 "}\n";
122 static const char *vertShaderText =
123 "void main() {\n"
124 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
125 "}\n";
127 if (!ShadersSupported())
128 exit(1);
130 vertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText);
131 fragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText);
132 program = LinkShaders(vertShader, fragShader);
134 glUseProgram(program);
136 /*assert(glGetError() == 0);*/
138 glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
140 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
142 assert(glIsProgram(program));
143 assert(glIsShader(fragShader));
144 assert(glIsShader(vertShader));
146 glColor3f(1, 0, 0);
150 static void
151 ParseOptions(int argc, char *argv[])
153 int i;
154 for (i = 1; i < argc; i++) {
155 if (strcmp(argv[i], "-fs") == 0) {
156 FragProgFile = argv[i+1];
158 else if (strcmp(argv[i], "-vs") == 0) {
159 VertProgFile = argv[i+1];
166 main(int argc, char *argv[])
168 glutInit(&argc, argv);
169 glutInitWindowSize(WinWidth, WinHeight);
170 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
171 win = glutCreateWindow(argv[0]);
172 glewInit();
173 glutReshapeFunc(Reshape);
174 glutKeyboardFunc(Key);
175 glutDisplayFunc(Redisplay);
176 ParseOptions(argc, argv);
177 Init();
178 glutIdleFunc(Anim ? Idle : NULL);
179 glutMainLoop();
180 return 0;