Merge branch '7.8'
[mesa.git] / progs / fpglsl / fp-tri.c
1
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5
6 #ifndef WIN32
7 #include <unistd.h>
8 #include <signal.h>
9 #endif
10
11 #include <GL/glew.h>
12 #include <GL/glut.h>
13
14 #include "readtex.c"
15
16
17 #define TEXTURE_FILE "../images/bw.rgb"
18
19 unsigned show_fps = 0;
20 unsigned int frame_cnt = 0;
21 void alarmhandler(int);
22 static const char *filename = NULL;
23
24 static GLuint fragShader;
25 static GLuint vertShader;
26 static GLuint program;
27
28
29 static void usage(char *name)
30 {
31 fprintf(stderr, "usage: %s [ options ] shader_filename\n", name);
32 #ifndef WIN32
33 fprintf(stderr, "\n" );
34 fprintf(stderr, "options:\n");
35 fprintf(stderr, " -fps show frames per second\n");
36 #endif
37 }
38
39 #ifndef WIN32
40 void alarmhandler (int sig)
41 {
42 if (sig == SIGALRM) {
43 printf("%d frames in 5.0 seconds = %.3f FPS\n", frame_cnt,
44 frame_cnt / 5.0);
45
46 frame_cnt = 0;
47 }
48 signal(SIGALRM, alarmhandler);
49 alarm(5);
50 }
51 #endif
52
53
54
55
56 static void load_and_compile_shader(GLuint shader, const char *text)
57 {
58 GLint stat;
59
60 glShaderSource(shader, 1, (const GLchar **) &text, NULL);
61
62 glCompileShader(shader);
63
64 glGetShaderiv(shader, GL_COMPILE_STATUS, &stat);
65 if (!stat) {
66 GLchar log[1000];
67 GLsizei len;
68 glGetShaderInfoLog(shader, 1000, &len, log);
69 fprintf(stderr, "fp-tri: problem compiling shader:\n%s\n", log);
70 exit(1);
71 }
72 }
73
74 static void read_shader(GLuint shader, const char *filename)
75 {
76 const int max = 100*1000;
77 int n;
78 char *buffer = (char*) malloc(max);
79 FILE *f = fopen(filename, "r");
80 if (!f) {
81 fprintf(stderr, "fp-tri: Unable to open shader file %s\n", filename);
82 exit(1);
83 }
84
85 n = fread(buffer, 1, max, f);
86 printf("fp-tri: read %d bytes from shader file %s\n", n, filename);
87 if (n > 0) {
88 buffer[n] = 0;
89 load_and_compile_shader(shader, buffer);
90 }
91
92 fclose(f);
93 free(buffer);
94 }
95
96 static void check_link(GLuint prog)
97 {
98 GLint stat;
99 glGetProgramiv(prog, GL_LINK_STATUS, &stat);
100 if (!stat) {
101 GLchar log[1000];
102 GLsizei len;
103 glGetProgramInfoLog(prog, 1000, &len, log);
104 fprintf(stderr, "Linker error:\n%s\n", log);
105 }
106 }
107
108 static void setup_uniforms()
109 {
110 {
111 GLint loc1f = glGetUniformLocationARB(program, "Offset1f");
112 GLint loc2f = glGetUniformLocationARB(program, "Offset2f");
113 GLint loc4f = glGetUniformLocationARB(program, "Offset4f");
114 GLfloat vecKer[] =
115 { 1.0, 0.0, 0.0, 1.0,
116 0.0, 1.0, 0.0, 1.0,
117 1.0, 0.0, 0.0, 1.0,
118 0.0, 0.0, 0.0, 1.0
119 };
120 if (loc1f >= 0)
121 glUniform1fv(loc1f, 16, vecKer);
122
123 if (loc2f >= 0)
124 glUniform2fv(loc2f, 8, vecKer);
125
126 if (loc4f >= 0)
127 glUniform4fv(loc4f, 4, vecKer);
128
129 }
130
131 {
132 GLint loci = glGetUniformLocationARB(program, "KernelSizeInt");
133 if (loci >= 0)
134 glUniform1i(loci, 4);
135 }
136 {
137 GLint loc1f = glGetUniformLocationARB(program, "KernelValue1f");
138 GLint loc2f = glGetUniformLocationARB(program, "KernelValue2f");
139 GLint loc4f = glGetUniformLocationARB(program, "KernelValue4f");
140 GLfloat vecKer[] =
141 { 1.0, 0.0, 0.0, 0.25,
142 0.0, 1.0, 0.0, 0.25,
143 0.0, 0.0, 1.0, 0.25,
144 0.0, 0.0, 0.0, 0.25,
145 0.5, 0.0, 0.0, 0.35,
146 0.0, 0.5, 0.0, 0.35,
147 0.0, 0.0, 0.5, 0.35,
148 0.0, 0.0, 0.0, 0.35
149 };
150 if (loc1f >= 0)
151 glUniform1fv(loc1f, 16, vecKer);
152
153 if (loc2f >= 0)
154 glUniform2fv(loc2f, 8, vecKer);
155
156 if (loc4f >= 0)
157 glUniform4fv(loc4f, 4, vecKer);
158 }
159 }
160
161 static void prepare_shaders()
162 {
163 static const char *fragShaderText =
164 "void main() {\n"
165 " gl_FragColor = gl_Color;\n"
166 "}\n";
167 static const char *vertShaderText =
168 "void main() {\n"
169 " gl_FrontColor = gl_Color;\n"
170 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
171 "}\n";
172 fragShader = glCreateShader(GL_FRAGMENT_SHADER);
173 if (filename)
174 read_shader(fragShader, filename);
175 else
176 load_and_compile_shader(fragShader, fragShaderText);
177
178
179 vertShader = glCreateShader(GL_VERTEX_SHADER);
180 load_and_compile_shader(vertShader, vertShaderText);
181
182 program = glCreateProgram();
183 glAttachShader(program, fragShader);
184 glAttachShader(program, vertShader);
185 glLinkProgram(program);
186 check_link(program);
187 glUseProgram(program);
188
189 setup_uniforms();
190 }
191
192 #define LEVELS 8
193 #define SIZE (1<<LEVELS)
194 static int TexWidth = SIZE, TexHeight = SIZE;
195
196
197 static void
198 ResetTextureLevel( int i )
199 {
200 GLubyte tex2d[SIZE*SIZE][4];
201
202 {
203 GLint Width = TexWidth / (1 << i);
204 GLint Height = TexHeight / (1 << i);
205 GLint s, t;
206
207 for (s = 0; s < Width; s++) {
208 for (t = 0; t < Height; t++) {
209 tex2d[t*Width+s][0] = ((s / 16) % 2) ? 0 : 255;
210 tex2d[t*Width+s][1] = ((t / 16) % 2) ? 0 : 255;
211 tex2d[t*Width+s][2] = 128;
212 tex2d[t*Width+s][3] = 255;
213 }
214 }
215
216 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
217
218 glTexImage2D(GL_TEXTURE_2D, i, GL_RGB, Width, Height, 0,
219 GL_RGBA, GL_UNSIGNED_BYTE, tex2d);
220 }
221 }
222
223
224 static void
225 ResetTexture( void )
226 {
227 int i;
228
229 for (i = 0; i <= LEVELS; i++)
230 {
231 ResetTextureLevel(i);
232 }
233 }
234
235 static void Init( void )
236 {
237 GLuint Texture;
238
239 /* Load texture */
240 glGenTextures(1, &Texture);
241 glBindTexture(GL_TEXTURE_2D, Texture);
242 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
243 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
244 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
245 if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
246 printf("Error: couldn't load texture image file %s\n", TEXTURE_FILE);
247 exit(1);
248 }
249
250
251 glGenTextures(1, &Texture);
252 glActiveTextureARB(GL_TEXTURE0_ARB + 1);
253 glBindTexture(GL_TEXTURE_2D, Texture);
254 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
255 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
256 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
257
258 {
259 GLubyte data[32][32];
260 int width = 32;
261 int height = 32;
262 int i;
263 int j;
264
265 for (i = 0; i < 32; i++)
266 for (j = 0; j < 32; j++)
267 {
268 /**
269 ** +-----------+
270 ** | W |
271 ** | +-----+ |
272 ** | | | |
273 ** | | B | |
274 ** | | | |
275 ** | +-----+ |
276 ** | |
277 ** +-----------+
278 **/
279 int i2 = i - height / 2;
280 int j2 = j - width / 2;
281 int h8 = height / 8;
282 int w8 = width / 8;
283 if ( -h8 <= i2 && i2 <= h8 && -w8 <= j2 && j2 <= w8 ) {
284 data[i][j] = 0x00;
285 } else if ( -2 * h8 <= i2 && i2 <= 2 * h8 && -2 * w8 <= j2 && j2 <= 2 * w8 ) {
286 data[i][j] = 0x55;
287 } else if ( -3 * h8 <= i2 && i2 <= 3 * h8 && -3 * w8 <= j2 && j2 <= 3 * w8 ) {
288 data[i][j] = 0xaa;
289 } else {
290 data[i][j] = 0xff;
291 }
292 }
293
294 glTexImage2D( GL_TEXTURE_2D, 0,
295 GL_ALPHA8,
296 32, 32, 0,
297 GL_ALPHA, GL_UNSIGNED_BYTE, data );
298 }
299
300 glGenTextures(1, &Texture);
301 glActiveTextureARB(GL_TEXTURE0_ARB + 2);
302 glBindTexture(GL_TEXTURE_2D, Texture);
303 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
304 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
305 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
306 ResetTexture();
307
308 glClearColor(.1, .3, .5, 0);
309 }
310
311
312
313
314 static void args(int argc, char *argv[])
315 {
316 GLint i;
317
318 for (i = 1; i < argc; i++) {
319 if (strcmp(argv[i], "-fps") == 0) {
320 show_fps = 1;
321 }
322 else if (i == argc - 1) {
323 filename = argv[i];
324 }
325 else {
326 usage(argv[0]);
327 exit(1);
328 }
329 }
330 }
331
332
333
334
335
336 static void Reshape(int width, int height)
337 {
338
339 glViewport(0, 0, (GLint)width, (GLint)height);
340
341 glMatrixMode(GL_PROJECTION);
342 glLoadIdentity();
343 glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
344 glMatrixMode(GL_MODELVIEW);
345 }
346
347 static void CleanUp(void)
348 {
349 glDeleteShader(fragShader);
350 glDeleteShader(vertShader);
351 glDeleteProgram(program);
352 }
353
354 static void Key(unsigned char key, int x, int y)
355 {
356
357 switch (key) {
358 case 27:
359 CleanUp();
360 exit(1);
361 default:
362 break;
363 }
364
365 glutPostRedisplay();
366 }
367
368 static void Display(void)
369 {
370 glClear(GL_COLOR_BUFFER_BIT);
371
372 glUseProgram(program);
373 glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 0, 1.0, 1.0, 0.0, 0.0);
374 glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 1, 0.0, 0.0, 1.0, 1.0);
375 glBegin(GL_TRIANGLES);
376
377 glColor3f(0,0,1);
378 glTexCoord3f(1,1,0);
379 glVertex3f( 0.9, -0.9, -30.0);
380
381 glColor3f(1,0,0);
382 glTexCoord3f(1,-1,0);
383 glVertex3f( 0.9, 0.9, -30.0);
384
385 glColor3f(0,1,0);
386 glTexCoord3f(-1,0,0);
387 glVertex3f(-0.9, 0.0, -30.0);
388 glEnd();
389
390 glFlush();
391 if (show_fps) {
392 ++frame_cnt;
393 glutPostRedisplay();
394 }
395 }
396
397
398 int main(int argc, char **argv)
399 {
400 glutInit(&argc, argv);
401 glutInitWindowPosition(0, 0);
402 glutInitWindowSize(250, 250);
403 glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH);
404 args(argc, argv);
405 glutCreateWindow(filename ? filename : "fp-tri");
406 glewInit();
407 glutReshapeFunc(Reshape);
408 glutKeyboardFunc(Key);
409 glutDisplayFunc(Display);
410 prepare_shaders();
411 Init();
412 #ifndef WIN32
413 if (show_fps) {
414 signal(SIGALRM, alarmhandler);
415 alarm(5);
416 }
417 #endif
418 glutMainLoop();
419 return 0;
420 }