0b7561f3e474650e68c50255a93d6822e1ad7855
[mesa.git] / progs / glsl / fragcoord.c
1 /**
2 * Test GLSL gl_FragCoord fragment program attribute.
3 * Color the quad's fragments according to their window position.
4 *
5 * Brian Paul
6 * 20 Nov 2008
7 */
8
9
10 #include <assert.h>
11 #include <string.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <math.h>
15 #include <GL/gl.h>
16 #include <GL/glut.h>
17 #include <GL/glext.h>
18 #include "extfuncs.h"
19 #include "shaderutil.h"
20
21
22 static GLint WinWidth = 200, WinHeight = 200;
23 static char *FragProgFile = NULL;
24 static char *VertProgFile = NULL;
25 static GLuint fragShader;
26 static GLuint vertShader;
27 static GLuint program;
28 static GLint win = 0;
29 static GLboolean Anim = GL_TRUE;
30 static GLfloat PosX = 0.0, PosY = 0.0;
31
32
33 static void
34 Idle(void)
35 {
36 float r = (WinWidth < WinHeight) ? WinWidth : WinHeight;
37 float a = glutGet(GLUT_ELAPSED_TIME) * 0.001;
38 r *= 0.25;
39 PosX = WinWidth / 2 + r * cos(a);
40 PosY = WinHeight / 2 + r * sin(a);
41
42 glutPostRedisplay();
43 }
44
45
46 static void
47 Redisplay(void)
48 {
49 glClear(GL_COLOR_BUFFER_BIT);
50
51 glPushMatrix();
52 glTranslatef(PosX, PosY, 0.0);
53 #if 0
54 glBegin(GL_POLYGON);
55 glVertex2f(-50, -50);
56 glVertex2f( 50, -50);
57 glVertex2f( 50, 50);
58 glVertex2f(-50, 50);
59 glEnd();
60 #else
61 glutSolidSphere(50, 20, 10);
62 #endif
63 glPopMatrix();
64
65 glutSwapBuffers();
66 }
67
68
69 static void
70 Reshape(int width, int height)
71 {
72 glViewport(0, 0, width, height);
73 glMatrixMode(GL_PROJECTION);
74 glLoadIdentity();
75 glOrtho(0, width, 0, height, -55, 55);
76
77 glMatrixMode(GL_MODELVIEW);
78 glLoadIdentity();
79
80 WinWidth = width;
81 WinHeight = height;
82 }
83
84
85 static void
86 CleanUp(void)
87 {
88 glDeleteShader_func(fragShader);
89 glDeleteShader_func(vertShader);
90 glDeleteProgram_func(program);
91 glutDestroyWindow(win);
92 }
93
94
95 static void
96 Key(unsigned char key, int x, int y)
97 {
98 (void) x;
99 (void) y;
100
101 switch(key) {
102 case ' ':
103 case 'a':
104 Anim = !Anim;
105 glutIdleFunc(Anim ? Idle : NULL);
106 break;
107 case 27:
108 CleanUp();
109 exit(0);
110 break;
111 }
112 glutPostRedisplay();
113 }
114
115
116 static void
117 Init(void)
118 {
119 static const char *fragShaderText =
120 "void main() { \n"
121 " vec4 scale = vec4(.005, 0.005, 0.5, 1.0);\n"
122 " gl_FragColor = gl_FragCoord * scale; \n"
123 "}\n";
124 static const char *vertShaderText =
125 "void main() {\n"
126 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
127 "}\n";
128
129 if (!ShadersSupported())
130 exit(1);
131
132 GetExtensionFuncs();
133
134 vertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText);
135 fragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText);
136 program = LinkShaders(vertShader, fragShader);
137
138 glUseProgram_func(program);
139
140 /*assert(glGetError() == 0);*/
141
142 glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
143
144 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
145
146 assert(glIsProgram_func(program));
147 assert(glIsShader_func(fragShader));
148 assert(glIsShader_func(vertShader));
149
150 glColor3f(1, 0, 0);
151 }
152
153
154 static void
155 ParseOptions(int argc, char *argv[])
156 {
157 int i;
158 for (i = 1; i < argc; i++) {
159 if (strcmp(argv[i], "-fs") == 0) {
160 FragProgFile = argv[i+1];
161 }
162 else if (strcmp(argv[i], "-vs") == 0) {
163 VertProgFile = argv[i+1];
164 }
165 }
166 }
167
168
169 int
170 main(int argc, char *argv[])
171 {
172 glutInit(&argc, argv);
173 glutInitWindowPosition( 0, 0);
174 glutInitWindowSize(WinWidth, WinHeight);
175 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
176 win = glutCreateWindow(argv[0]);
177 glutReshapeFunc(Reshape);
178 glutKeyboardFunc(Key);
179 glutDisplayFunc(Redisplay);
180 ParseOptions(argc, argv);
181 Init();
182 glutIdleFunc(Anim ? Idle : NULL);
183 glutMainLoop();
184 return 0;
185 }