progs/glsl: compile with scons and glew
[mesa.git] / progs / glsl / shadow_sampler.c
1 /**
2 * Test shadow2DRectProj() and shadow2D() functions.
3 * Brian Paul
4 * 11 April 2007
5 */
6
7 #define GL_GLEXT_PROTOTYPES
8 #include <assert.h>
9 #include <string.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <math.h>
13 #include <GL/glew.h>
14 #include <GL/gl.h>
15 #include <GL/glut.h>
16 #include <GL/glext.h>
17 #include "extfuncs.h"
18
19
20 /** Use GL_RECTANGLE texture (with projective texcoords)? */
21 #define USE_RECT 01
22
23 #define TEXSIZE 16
24
25
26 static char *FragProgFile = NULL;
27 static char *VertProgFile = NULL;
28
29 static GLuint fragShader;
30 static GLuint vertShader;
31 static GLuint program;
32
33 static GLint uTexture2D;
34 static GLint uTextureRect;
35
36 static GLint win = 0;
37
38 static GLenum Filter = GL_LINEAR;
39
40 static void
41 CheckError(int line)
42 {
43 GLenum err = glGetError();
44 if (err) {
45 printf("GL Error %s (0x%x) at line %d\n",
46 gluErrorString(err), (int) err, line);
47 }
48 }
49
50
51 static void
52 PrintString(const char *s)
53 {
54 while (*s) {
55 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
56 s++;
57 }
58 }
59
60
61 static void
62 Redisplay(void)
63 {
64 CheckError(__LINE__);
65 glClear(GL_COLOR_BUFFER_BIT);
66
67 glPushMatrix();
68
69 CheckError(__LINE__);
70 glUseProgram_func(program);
71 CheckError(__LINE__);
72
73 glBegin(GL_POLYGON);
74 #if USE_RECT
75 /* scale coords by two to test projection */
76 glTexCoord4f( 0, 0, 0, 2.0); glVertex2f(-1, -1);
77 glTexCoord4f(2*TEXSIZE, 0, 2*1, 2.0); glVertex2f( 1, -1);
78 glTexCoord4f(2*TEXSIZE, 2*TEXSIZE, 2*1, 2.0); glVertex2f( 1, 1);
79 glTexCoord4f( 0, 2*TEXSIZE, 0, 2.0); glVertex2f(-1, 1);
80 #else
81 glTexCoord3f(0, 0, 0); glVertex2f(-1, -1);
82 glTexCoord3f(1, 0, 1); glVertex2f( 1, -1);
83 glTexCoord3f(1, 1, 1); glVertex2f( 1, 1);
84 glTexCoord3f(0, 1, 0); glVertex2f(-1, 1);
85 #endif
86 glEnd();
87
88 glPopMatrix();
89
90 glUseProgram_func(0);
91 glWindowPos2iARB(80, 20);
92 PrintString("white black white black");
93
94 glutSwapBuffers();
95 }
96
97
98 static void
99 Reshape(int width, int height)
100 {
101 glViewport(0, 0, width, height);
102 glMatrixMode(GL_PROJECTION);
103 glLoadIdentity();
104 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
105 glMatrixMode(GL_MODELVIEW);
106 glLoadIdentity();
107 glTranslatef(0.0f, 0.0f, -8.0f);
108 }
109
110
111 static void
112 CleanUp(void)
113 {
114 glDeleteShader_func(fragShader);
115 glDeleteShader_func(vertShader);
116 glDeleteProgram_func(program);
117 glutDestroyWindow(win);
118 }
119
120
121 static void
122 Key(unsigned char key, int x, int y)
123 {
124 (void) x;
125 (void) y;
126
127 switch(key) {
128 case 27:
129 CleanUp();
130 exit(0);
131 break;
132 }
133 glutPostRedisplay();
134 }
135
136
137 static void
138 MakeTexture(void)
139 {
140 GLfloat image[TEXSIZE][TEXSIZE];
141 GLuint i, j;
142
143 for (i = 0; i < TEXSIZE; i++) {
144 for (j = 0; j < TEXSIZE; j++) {
145 if (j < (TEXSIZE / 2)) {
146 image[i][j] = 0.25;
147 }
148 else {
149 image[i][j] = 0.75;
150 }
151 }
152 }
153
154 glActiveTexture(GL_TEXTURE0); /* unit 0 */
155 glBindTexture(GL_TEXTURE_2D, 42);
156 glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, TEXSIZE, TEXSIZE, 0,
157 GL_DEPTH_COMPONENT, GL_FLOAT, image);
158 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
159 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, Filter);
160 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, Filter);
161 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB,
162 GL_COMPARE_R_TO_TEXTURE_ARB);
163 CheckError(__LINE__);
164
165 glActiveTexture(GL_TEXTURE1); /* unit 1 */
166 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 43);
167 glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_DEPTH_COMPONENT,
168 TEXSIZE, 10, 0,/*16x10*/
169 GL_DEPTH_COMPONENT, GL_FLOAT, image);
170 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, Filter);
171 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, Filter);
172 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_COMPARE_MODE_ARB,
173 GL_COMPARE_R_TO_TEXTURE_ARB);
174 CheckError(__LINE__);
175 }
176
177
178 static void
179 LoadAndCompileShader(GLuint shader, const char *text)
180 {
181 GLint stat;
182 glShaderSource_func(shader, 1, (const GLchar **) &text, NULL);
183 glCompileShader_func(shader);
184 glGetShaderiv_func(shader, GL_COMPILE_STATUS, &stat);
185 if (!stat) {
186 GLchar log[1000];
187 GLsizei len;
188 glGetShaderInfoLog_func(shader, 1000, &len, log);
189 fprintf(stderr, "fslight: problem compiling shader:\n%s\n", log);
190 exit(1);
191 }
192 }
193
194
195 /**
196 * Read a shader from a file.
197 */
198 static void
199 ReadShader(GLuint shader, const char *filename)
200 {
201 const int max = 100*1000;
202 int n;
203 char *buffer = (char*) malloc(max);
204 FILE *f = fopen(filename, "r");
205 if (!f) {
206 fprintf(stderr, "fslight: Unable to open shader file %s\n", filename);
207 exit(1);
208 }
209
210 n = fread(buffer, 1, max, f);
211 printf("fslight: read %d bytes from shader file %s\n", n, filename);
212 if (n > 0) {
213 buffer[n] = 0;
214 LoadAndCompileShader(shader, buffer);
215 }
216
217 fclose(f);
218 free(buffer);
219 }
220
221
222 static void
223 CheckLink(GLuint prog)
224 {
225 GLint stat;
226 glGetProgramiv_func(prog, GL_LINK_STATUS, &stat);
227 if (!stat) {
228 GLchar log[1000];
229 GLsizei len;
230 glGetProgramInfoLog_func(prog, 1000, &len, log);
231 fprintf(stderr, "Linker error:\n%s\n", log);
232 }
233 }
234
235
236 static void
237 Init(void)
238 {
239 static const char *fragShaderText =
240 "uniform sampler2DShadow shadowTex2D; \n"
241 "uniform sampler2DRectShadow shadowTexRect; \n"
242 "void main() {\n"
243 #if USE_RECT
244 " gl_FragColor = shadow2DRectProj(shadowTexRect, gl_TexCoord[0]); \n"
245 #else
246 " gl_FragColor = shadow2D(shadowTex2D, gl_TexCoord[0].xyz); \n"
247 #endif
248 "}\n";
249 static const char *vertShaderText =
250 "void main() {\n"
251 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
252 " gl_TexCoord[0] = gl_MultiTexCoord0; \n"
253 "}\n";
254 const char *version;
255
256 #if USE_RECT
257 if (!glutExtensionSupported("GL_ARB_texture_rectangle")) {
258 printf("This program requires GL_ARB_texture_rectangle\n");
259 exit(1);
260 }
261 #endif
262
263 version = (const char *) glGetString(GL_VERSION);
264 if (version[0] != '2' || version[1] != '.') {
265 printf("This program requires OpenGL 2.x, found %s\n", version);
266 exit(1);
267 }
268 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
269
270 GetExtensionFuncs();
271
272 fragShader = glCreateShader_func(GL_FRAGMENT_SHADER);
273 if (FragProgFile)
274 ReadShader(fragShader, FragProgFile);
275 else
276 LoadAndCompileShader(fragShader, fragShaderText);
277
278 vertShader = glCreateShader_func(GL_VERTEX_SHADER);
279 if (VertProgFile)
280 ReadShader(vertShader, VertProgFile);
281 else
282 LoadAndCompileShader(vertShader, vertShaderText);
283
284 program = glCreateProgram_func();
285 glAttachShader_func(program, fragShader);
286 glAttachShader_func(program, vertShader);
287 glLinkProgram_func(program);
288 CheckLink(program);
289 glUseProgram_func(program);
290
291 uTexture2D = glGetUniformLocation_func(program, "shadowTex2D");
292 uTextureRect = glGetUniformLocation_func(program, "shadowTexRect");
293 printf("uTexture2D %d uTextureRect %d\n", uTexture2D, uTextureRect);
294 if (uTexture2D >= 0) {
295 glUniform1i_func(uTexture2D, 0); /* use texture unit 0 */
296 }
297 if (uTextureRect >= 0) {
298 glUniform1i_func(uTextureRect, 1); /* use texture unit 0 */
299 }
300 CheckError(__LINE__);
301
302 glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
303 glColor3f(1, 1, 1);
304
305 MakeTexture();
306 CheckError(__LINE__);
307 }
308
309
310 static void
311 ParseOptions(int argc, char *argv[])
312 {
313 int i;
314 for (i = 1; i < argc; i++) {
315 if (strcmp(argv[i], "-fs") == 0) {
316 FragProgFile = argv[i+1];
317 }
318 else if (strcmp(argv[i], "-vs") == 0) {
319 VertProgFile = argv[i+1];
320 }
321 }
322 }
323
324
325 int
326 main(int argc, char *argv[])
327 {
328 glutInit(&argc, argv);
329 glutInitWindowPosition( 0, 0);
330 glutInitWindowSize(400, 300);
331 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
332 win = glutCreateWindow(argv[0]);
333 glewInit();
334 glutReshapeFunc(Reshape);
335 glutKeyboardFunc(Key);
336 glutDisplayFunc(Redisplay);
337 ParseOptions(argc, argv);
338 Init();
339 glutMainLoop();
340 return 0;
341 }
342
343