2 * Test shadow2DRectProj() and shadow2D() functions.
7 #define GL_GLEXT_PROTOTYPES
14 #include "glut_wrap.h"
17 /** Use GL_RECTANGLE texture (with projective texcoords)? */
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
;
35 static GLenum Filter
= GL_LINEAR
;
40 GLenum err
= glGetError();
42 printf("GL Error %s (0x%x) at line %d\n",
43 gluErrorString(err
), (int) err
, line
);
49 PrintString(const char *s
)
52 glutBitmapCharacter(GLUT_BITMAP_8_BY_13
, (int) *s
);
62 glClear(GL_COLOR_BUFFER_BIT
);
67 glUseProgram(program
);
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);
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);
88 glWindowPos2iARB(80, 20);
89 PrintString("white black white black");
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]);
108 Reshape(int width
, int height
)
110 glViewport(0, 0, width
, height
);
111 glMatrixMode(GL_PROJECTION
);
113 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
114 glMatrixMode(GL_MODELVIEW
);
116 glTranslatef(0.0f
, 0.0f
, -8.0f
);
123 glDeleteShader(fragShader
);
124 glDeleteShader(vertShader
);
125 glDeleteProgram(program
);
126 glutDestroyWindow(win
);
131 Key(unsigned char key
, int x
, int y
)
149 GLfloat image
[TEXSIZE
][TEXSIZE
];
152 for (i
= 0; i
< TEXSIZE
; i
++) {
153 for (j
= 0; j
< TEXSIZE
; j
++) {
154 if (j
< (TEXSIZE
/ 2)) {
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__
);
188 LoadAndCompileShader(GLuint shader
, const char *text
)
191 glShaderSource(shader
, 1, (const GLchar
**) &text
, NULL
);
192 glCompileShader(shader
);
193 glGetShaderiv(shader
, GL_COMPILE_STATUS
, &stat
);
197 glGetShaderInfoLog(shader
, 1000, &len
, log
);
198 fprintf(stderr
, "fslight: problem compiling shader:\n%s\n", log
);
205 * Read a shader from a file.
208 ReadShader(GLuint shader
, const char *filename
)
210 const int max
= 100*1000;
212 char *buffer
= (char*) malloc(max
);
213 FILE *f
= fopen(filename
, "r");
215 fprintf(stderr
, "fslight: Unable to open shader file %s\n", filename
);
219 n
= fread(buffer
, 1, max
, f
);
220 printf("fslight: read %d bytes from shader file %s\n", n
, filename
);
223 LoadAndCompileShader(shader
, buffer
);
232 CheckLink(GLuint prog
)
235 glGetProgramiv(prog
, GL_LINK_STATUS
, &stat
);
239 glGetProgramInfoLog(prog
, 1000, &len
, log
);
240 fprintf(stderr
, "Linker error:\n%s\n", log
);
248 static const char *fragShaderText
=
249 "uniform sampler2DShadow shadowTex2D; \n"
250 "uniform sampler2DRectShadow shadowTexRect; \n"
253 " gl_FragColor = shadow2DRectProj(shadowTexRect, gl_TexCoord[0]); \n"
255 " gl_FragColor = shadow2D(shadowTex2D, gl_TexCoord[0].xyz); \n"
258 static const char *vertShaderText
=
260 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
261 " gl_TexCoord[0] = gl_MultiTexCoord0; \n"
265 if (!glutExtensionSupported("GL_ARB_texture_rectangle")) {
266 printf("This program requires GL_ARB_texture_rectangle\n");
271 if (!GLEW_VERSION_2_0
) {
272 printf("This program requires OpenGL 2.x\n");
275 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER
));
277 fragShader
= glCreateShader(GL_FRAGMENT_SHADER
);
279 ReadShader(fragShader
, FragProgFile
);
281 LoadAndCompileShader(fragShader
, fragShaderText
);
283 vertShader
= glCreateShader(GL_VERTEX_SHADER
);
285 ReadShader(vertShader
, VertProgFile
);
287 LoadAndCompileShader(vertShader
, vertShaderText
);
289 program
= glCreateProgram();
290 glAttachShader(program
, fragShader
);
291 glAttachShader(program
, vertShader
);
292 glLinkProgram(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
);
311 CheckError(__LINE__
);
316 ParseOptions(int argc
, char *argv
[])
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]);
338 glutReshapeFunc(Reshape
);
339 glutKeyboardFunc(Key
);
340 glutDisplayFunc(Redisplay
);
341 ParseOptions(argc
, argv
);