~ubuntu-branches/ubuntu/oneiric/mesa-demos/oneiric

« back to all changes in this revision

Viewing changes to src/glsl/shadow_sampler.c

  • Committer: Bazaar Package Importer
  • Author(s): Christopher James Halse Rogers
  • Date: 2010-09-27 16:18:27 UTC
  • Revision ID: james.westby@ubuntu.com-20100927161827-1yfgolc1oy9sjhi8
Tags: upstream-8.0.1
ImportĀ upstreamĀ versionĀ 8.0.1

Show diffs side-by-side

added added

removed removed

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