Merge branch '7.8'
[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/glew.h>
16 #include <GL/glut.h>
17 #include "shaderutil.h"
18
19
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;
29
30
31 static void
32 Idle(void)
33 {
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);
39
40 glutPostRedisplay();
41 }
42
43
44 static void
45 Redisplay(void)
46 {
47 glClear(GL_COLOR_BUFFER_BIT);
48
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();
62
63 glutSwapBuffers();
64 }
65
66
67 static void
68 Reshape(int width, int height)
69 {
70 glViewport(0, 0, width, height);
71 glMatrixMode(GL_PROJECTION);
72 glLoadIdentity();
73 glOrtho(0, width, 0, height, -55, 55);
74
75 glMatrixMode(GL_MODELVIEW);
76 glLoadIdentity();
77
78 WinWidth = width;
79 WinHeight = height;
80 }
81
82
83 static void
84 CleanUp(void)
85 {
86 glDeleteShader(fragShader);
87 glDeleteShader(vertShader);
88 glDeleteProgram(program);
89 glutDestroyWindow(win);
90 }
91
92
93 static void
94 Key(unsigned char key, int x, int y)
95 {
96 (void) x;
97 (void) y;
98
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;
109 }
110 glutPostRedisplay();
111 }
112
113
114 static void
115 Init(void)
116 {
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";
126
127 if (!ShadersSupported())
128 exit(1);
129
130 vertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText);
131 fragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText);
132 program = LinkShaders(vertShader, fragShader);
133
134 glUseProgram(program);
135
136 /*assert(glGetError() == 0);*/
137
138 glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
139
140 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
141
142 assert(glIsProgram(program));
143 assert(glIsShader(fragShader));
144 assert(glIsShader(vertShader));
145
146 glColor3f(1, 0, 0);
147 }
148
149
150 static void
151 ParseOptions(int argc, char *argv[])
152 {
153 int i;
154 for (i = 1; i < argc; i++) {
155 if (strcmp(argv[i], "-fs") == 0) {
156 FragProgFile = argv[i+1];
157 }
158 else if (strcmp(argv[i], "-vs") == 0) {
159 VertProgFile = argv[i+1];
160 }
161 }
162 }
163
164
165 int
166 main(int argc, char *argv[])
167 {
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;
181 }