WIP - port to Mali EGL
[mesa-demos/mali.git] / src / glsl / shadow_sampler.c
blobb830030d1ba3cdfef372ef23d64ad802b53a1b3d
1 /**
2 * Test shadow2DRectProj() and shadow2D() functions.
3 * Brian Paul
4 * 11 April 2007
5 */
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 "glut_wrap.h"
17 /** Use GL_RECTANGLE texture (with projective texcoords)? */
18 #define USE_RECT 01
20 #define TEXSIZE 16
23 static char *FragProgFile = NULL;
24 static char *VertProgFile = NULL;
26 static GLuint fragShader;
27 static GLuint vertShader;
28 static GLuint program;
30 static GLint uTexture2D;
31 static GLint uTextureRect;
33 static GLint win = 0;
35 static GLenum Filter = GL_LINEAR;
37 static void
38 CheckError(int line)
40 GLenum err = glGetError();
41 if (err) {
42 printf("GL Error %s (0x%x) at line %d\n",
43 gluErrorString(err), (int) err, line);
48 static void
49 PrintString(const char *s)
51 while (*s) {
52 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
53 s++;
58 static void
59 Redisplay(void)
61 CheckError(__LINE__);
62 glClear(GL_COLOR_BUFFER_BIT);
64 glPushMatrix();
66 CheckError(__LINE__);
67 glUseProgram(program);
68 CheckError(__LINE__);
70 glBegin(GL_POLYGON);
71 #if USE_RECT
72 /* scale coords by two to test projection */
73 glTexCoord4f( 0, 0, 0, 2.0); glVertex2f(-1, -1);
74 glTexCoord4f(2*TEXSIZE, 0, 2*1, 2.0); glVertex2f( 1, -1);
75 glTexCoord4f(2*TEXSIZE, 2*TEXSIZE, 2*1, 2.0); glVertex2f( 1, 1);
76 glTexCoord4f( 0, 2*TEXSIZE, 0, 2.0); glVertex2f(-1, 1);
77 #else
78 glTexCoord3f(0, 0, 0); glVertex2f(-1, -1);
79 glTexCoord3f(1, 0, 1); glVertex2f( 1, -1);
80 glTexCoord3f(1, 1, 1); glVertex2f( 1, 1);
81 glTexCoord3f(0, 1, 0); glVertex2f(-1, 1);
82 #endif
83 glEnd();
85 glPopMatrix();
87 glUseProgram(0);
88 glWindowPos2iARB(80, 20);
89 PrintString("white black white black");
92 GLfloat pix[4];
93 glReadPixels(120, 150, 1, 1, GL_RGBA, GL_FLOAT, pix);
94 printf("Pixel(120, 150): %f %f %f %f\n", pix[0], pix[1], pix[2], pix[3]);
95 glReadPixels(180, 150, 1, 1, GL_RGBA, GL_FLOAT, pix);
96 printf("Pixel(180, 150): %f %f %f %f\n", pix[0], pix[1], pix[2], pix[3]);
97 glReadPixels(220, 150, 1, 1, GL_RGBA, GL_FLOAT, pix);
98 printf("Pixel(220, 150): %f %f %f %f\n", pix[0], pix[1], pix[2], pix[3]);
99 glReadPixels(280, 150, 1, 1, GL_RGBA, GL_FLOAT, pix);
100 printf("Pixel(280, 150): %f %f %f %f\n", pix[0], pix[1], pix[2], pix[3]);
103 glutSwapBuffers();
107 static void
108 Reshape(int width, int height)
110 glViewport(0, 0, width, height);
111 glMatrixMode(GL_PROJECTION);
112 glLoadIdentity();
113 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
114 glMatrixMode(GL_MODELVIEW);
115 glLoadIdentity();
116 glTranslatef(0.0f, 0.0f, -8.0f);
120 static void
121 CleanUp(void)
123 glDeleteShader(fragShader);
124 glDeleteShader(vertShader);
125 glDeleteProgram(program);
126 glutDestroyWindow(win);
130 static void
131 Key(unsigned char key, int x, int y)
133 (void) x;
134 (void) y;
136 switch(key) {
137 case 27:
138 CleanUp();
139 exit(0);
140 break;
142 glutPostRedisplay();
146 static void
147 MakeTexture(void)
149 GLfloat image[TEXSIZE][TEXSIZE];
150 GLuint i, j;
152 for (i = 0; i < TEXSIZE; i++) {
153 for (j = 0; j < TEXSIZE; j++) {
154 if (j < (TEXSIZE / 2)) {
155 image[i][j] = 0.25;
157 else {
158 image[i][j] = 0.75;
163 glActiveTexture(GL_TEXTURE0); /* unit 0 */
164 glBindTexture(GL_TEXTURE_2D, 42);
165 glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, TEXSIZE, TEXSIZE, 0,
166 GL_DEPTH_COMPONENT, GL_FLOAT, image);
167 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
168 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, Filter);
169 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, Filter);
170 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB,
171 GL_COMPARE_R_TO_TEXTURE_ARB);
172 CheckError(__LINE__);
174 glActiveTexture(GL_TEXTURE1); /* unit 1 */
175 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 43);
176 glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_DEPTH_COMPONENT,
177 TEXSIZE, 10, 0,/*16x10*/
178 GL_DEPTH_COMPONENT, GL_FLOAT, image);
179 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, Filter);
180 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, Filter);
181 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_COMPARE_MODE_ARB,
182 GL_COMPARE_R_TO_TEXTURE_ARB);
183 CheckError(__LINE__);
187 static void
188 LoadAndCompileShader(GLuint shader, const char *text)
190 GLint stat;
191 glShaderSource(shader, 1, (const GLchar **) &text, NULL);
192 glCompileShader(shader);
193 glGetShaderiv(shader, GL_COMPILE_STATUS, &stat);
194 if (!stat) {
195 GLchar log[1000];
196 GLsizei len;
197 glGetShaderInfoLog(shader, 1000, &len, log);
198 fprintf(stderr, "fslight: problem compiling shader:\n%s\n", log);
199 exit(1);
205 * Read a shader from a file.
207 static void
208 ReadShader(GLuint shader, const char *filename)
210 const int max = 100*1000;
211 int n;
212 char *buffer = (char*) malloc(max);
213 FILE *f = fopen(filename, "r");
214 if (!f) {
215 fprintf(stderr, "fslight: Unable to open shader file %s\n", filename);
216 exit(1);
219 n = fread(buffer, 1, max, f);
220 printf("fslight: read %d bytes from shader file %s\n", n, filename);
221 if (n > 0) {
222 buffer[n] = 0;
223 LoadAndCompileShader(shader, buffer);
226 fclose(f);
227 free(buffer);
231 static void
232 CheckLink(GLuint prog)
234 GLint stat;
235 glGetProgramiv(prog, GL_LINK_STATUS, &stat);
236 if (!stat) {
237 GLchar log[1000];
238 GLsizei len;
239 glGetProgramInfoLog(prog, 1000, &len, log);
240 fprintf(stderr, "Linker error:\n%s\n", log);
245 static void
246 Init(void)
248 static const char *fragShaderText =
249 "uniform sampler2DShadow shadowTex2D; \n"
250 "uniform sampler2DRectShadow shadowTexRect; \n"
251 "void main() {\n"
252 #if USE_RECT
253 " gl_FragColor = shadow2DRectProj(shadowTexRect, gl_TexCoord[0]); \n"
254 #else
255 " gl_FragColor = shadow2D(shadowTex2D, gl_TexCoord[0].xyz); \n"
256 #endif
257 "}\n";
258 static const char *vertShaderText =
259 "void main() {\n"
260 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
261 " gl_TexCoord[0] = gl_MultiTexCoord0; \n"
262 "}\n";
264 #if USE_RECT
265 if (!glutExtensionSupported("GL_ARB_texture_rectangle")) {
266 printf("This program requires GL_ARB_texture_rectangle\n");
267 exit(1);
269 #endif
271 if (!GLEW_VERSION_2_0) {
272 printf("This program requires OpenGL 2.x\n");
273 exit(1);
275 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
277 fragShader = glCreateShader(GL_FRAGMENT_SHADER);
278 if (FragProgFile)
279 ReadShader(fragShader, FragProgFile);
280 else
281 LoadAndCompileShader(fragShader, fragShaderText);
283 vertShader = glCreateShader(GL_VERTEX_SHADER);
284 if (VertProgFile)
285 ReadShader(vertShader, VertProgFile);
286 else
287 LoadAndCompileShader(vertShader, vertShaderText);
289 program = glCreateProgram();
290 glAttachShader(program, fragShader);
291 glAttachShader(program, vertShader);
292 glLinkProgram(program);
293 CheckLink(program);
294 glUseProgram(program);
296 uTexture2D = glGetUniformLocation(program, "shadowTex2D");
297 uTextureRect = glGetUniformLocation(program, "shadowTexRect");
298 printf("uTexture2D %d uTextureRect %d\n", uTexture2D, uTextureRect);
299 if (uTexture2D >= 0) {
300 glUniform1i(uTexture2D, 0); /* use texture unit 0 */
302 if (uTextureRect >= 0) {
303 glUniform1i(uTextureRect, 1); /* use texture unit 0 */
305 CheckError(__LINE__);
307 glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
308 glColor3f(1, 1, 1);
310 MakeTexture();
311 CheckError(__LINE__);
315 static void
316 ParseOptions(int argc, char *argv[])
318 int i;
319 for (i = 1; i < argc; i++) {
320 if (strcmp(argv[i], "-fs") == 0) {
321 FragProgFile = argv[i+1];
323 else if (strcmp(argv[i], "-vs") == 0) {
324 VertProgFile = argv[i+1];
331 main(int argc, char *argv[])
333 glutInit(&argc, argv);
334 glutInitWindowSize(400, 300);
335 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
336 win = glutCreateWindow(argv[0]);
337 glewInit();
338 glutReshapeFunc(Reshape);
339 glutKeyboardFunc(Key);
340 glutDisplayFunc(Redisplay);
341 ParseOptions(argc, argv);
342 Init();
343 glutMainLoop();
344 return 0;