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