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