12 #include "glut_wrap.h"
17 #define TEXTURE_FILE DEMOS_DATA_DIR "bw.rgb"
19 unsigned show_fps
= 0;
20 unsigned int frame_cnt
= 0;
21 void alarmhandler(int);
22 static const char *filename
= NULL
;
24 static GLuint fragShader
;
25 static GLuint vertShader
;
26 static GLuint program
;
29 static void usage(char *name
)
31 fprintf(stderr
, "usage: %s [ options ] shader_filename\n", name
);
33 fprintf(stderr
, "\n" );
34 fprintf(stderr
, "options:\n");
35 fprintf(stderr
, " -fps show frames per second\n");
40 void alarmhandler (int sig
)
43 printf("%d frames in 5.0 seconds = %.3f FPS\n", frame_cnt
,
48 signal(SIGALRM
, alarmhandler
);
56 static void load_and_compile_shader(GLuint shader
, const char *text
)
60 glShaderSource(shader
, 1, (const GLchar
**) &text
, NULL
);
62 glCompileShader(shader
);
64 glGetShaderiv(shader
, GL_COMPILE_STATUS
, &stat
);
68 glGetShaderInfoLog(shader
, 1000, &len
, log
);
69 fprintf(stderr
, "fp-tri: problem compiling shader:\n%s\n", log
);
74 static void read_shader(GLuint shader
, const char *filename
)
76 const int max
= 100*1000;
78 char *buffer
= (char*) malloc(max
);
79 FILE *f
= fopen(filename
, "r");
81 fprintf(stderr
, "fp-tri: Unable to open shader file %s\n", filename
);
85 n
= fread(buffer
, 1, max
, f
);
86 printf("fp-tri: read %d bytes from shader file %s\n", n
, filename
);
89 load_and_compile_shader(shader
, buffer
);
96 static void check_link(GLuint prog
)
99 glGetProgramiv(prog
, GL_LINK_STATUS
, &stat
);
103 glGetProgramInfoLog(prog
, 1000, &len
, log
);
104 fprintf(stderr
, "Linker error:\n%s\n", log
);
108 static void setup_uniforms(void)
111 GLint loc1f
= glGetUniformLocationARB(program
, "Offset1f");
112 GLint loc2f
= glGetUniformLocationARB(program
, "Offset2f");
113 GLint loc4f
= glGetUniformLocationARB(program
, "Offset4f");
115 { 1.0, 0.0, 0.0, 1.0,
121 glUniform1fv(loc1f
, 16, vecKer
);
124 glUniform2fv(loc2f
, 8, vecKer
);
127 glUniform4fv(loc4f
, 4, vecKer
);
132 GLint loci
= glGetUniformLocationARB(program
, "KernelSizeInt");
134 glUniform1i(loci
, 4);
137 GLint loc1f
= glGetUniformLocationARB(program
, "KernelValue1f");
138 GLint loc2f
= glGetUniformLocationARB(program
, "KernelValue2f");
139 GLint loc4f
= glGetUniformLocationARB(program
, "KernelValue4f");
141 { 1.0, 0.0, 0.0, 0.25,
151 glUniform1fv(loc1f
, 16, vecKer
);
154 glUniform2fv(loc2f
, 8, vecKer
);
157 glUniform4fv(loc4f
, 4, vecKer
);
161 GLint tex1
= glGetUniformLocationARB(program
, "tex1");
162 GLint tex2
= glGetUniformLocationARB(program
, "tex2");
164 glUniform1i(tex1
, 0);
166 glUniform1i(tex2
, 1);
170 static void prepare_shaders(void)
172 static const char *fragShaderText
=
174 " gl_FragColor = gl_Color;\n"
176 static const char *vertShaderText
=
178 " gl_FrontColor = gl_Color;\n"
179 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
181 fragShader
= glCreateShader(GL_FRAGMENT_SHADER
);
183 read_shader(fragShader
, filename
);
185 load_and_compile_shader(fragShader
, fragShaderText
);
188 vertShader
= glCreateShader(GL_VERTEX_SHADER
);
189 load_and_compile_shader(vertShader
, vertShaderText
);
191 program
= glCreateProgram();
192 glAttachShader(program
, fragShader
);
193 glAttachShader(program
, vertShader
);
194 glLinkProgram(program
);
196 glUseProgram(program
);
202 #define SIZE (1<<LEVELS)
203 static int TexWidth
= SIZE
, TexHeight
= SIZE
;
207 ResetTextureLevel( int i
)
209 GLubyte tex2d
[SIZE
*SIZE
][4];
212 GLint Width
= TexWidth
/ (1 << i
);
213 GLint Height
= TexHeight
/ (1 << i
);
216 for (s
= 0; s
< Width
; s
++) {
217 for (t
= 0; t
< Height
; t
++) {
218 tex2d
[t
*Width
+s
][0] = ((s
/ 16) % 2) ? 0 : 255;
219 tex2d
[t
*Width
+s
][1] = ((t
/ 16) % 2) ? 0 : 255;
220 tex2d
[t
*Width
+s
][2] = 128;
221 tex2d
[t
*Width
+s
][3] = 255;
225 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
227 glTexImage2D(GL_TEXTURE_2D
, i
, GL_RGB
, Width
, Height
, 0,
228 GL_RGBA
, GL_UNSIGNED_BYTE
, tex2d
);
238 for (i
= 0; i
<= LEVELS
; i
++)
240 ResetTextureLevel(i
);
244 static void Init( void )
248 /* Setup texture unit 0 */
249 glGenTextures(1, &Texture
);
250 glBindTexture(GL_TEXTURE_2D
, Texture
);
251 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
252 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
253 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
254 if (!LoadRGBMipmaps(TEXTURE_FILE
, GL_RGB
)) {
255 printf("Error: couldn't load texture image file %s\n", TEXTURE_FILE
);
259 /* Setup texture unit 1 */
260 glGenTextures(1, &Texture
);
261 glActiveTextureARB(GL_TEXTURE0_ARB
+ 1);
262 glBindTexture(GL_TEXTURE_2D
, Texture
);
263 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
264 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
265 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
268 GLubyte data
[32][32];
274 for (i
= 0; i
< 32; i
++)
275 for (j
= 0; j
< 32; j
++)
288 int i2
= i
- height
/ 2;
289 int j2
= j
- width
/ 2;
292 if ( -h8
<= i2
&& i2
<= h8
&& -w8
<= j2
&& j2
<= w8
) {
294 } else if ( -2 * h8
<= i2
&& i2
<= 2 * h8
&& -2 * w8
<= j2
&& j2
<= 2 * w8
) {
296 } else if ( -3 * h8
<= i2
&& i2
<= 3 * h8
&& -3 * w8
<= j2
&& j2
<= 3 * w8
) {
303 glTexImage2D( GL_TEXTURE_2D
, 0,
306 GL_ALPHA
, GL_UNSIGNED_BYTE
, data
);
309 /* Setup texture unit 2 */
310 glGenTextures(1, &Texture
);
311 glActiveTextureARB(GL_TEXTURE0_ARB
+ 2);
312 glBindTexture(GL_TEXTURE_2D
, Texture
);
313 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST_MIPMAP_NEAREST
);
314 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
315 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
318 glClearColor(.1, .3, .5, 0);
324 static void args(int argc
, char *argv
[])
328 for (i
= 1; i
< argc
; i
++) {
329 if (strcmp(argv
[i
], "-fps") == 0) {
332 else if (i
== argc
- 1) {
346 static void Reshape(int width
, int height
)
349 glViewport(0, 0, (GLint
)width
, (GLint
)height
);
351 glMatrixMode(GL_PROJECTION
);
353 glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
354 glMatrixMode(GL_MODELVIEW
);
357 static void CleanUp(void)
359 glDeleteShader(fragShader
);
360 glDeleteShader(vertShader
);
361 glDeleteProgram(program
);
364 static void Key(unsigned char key
, int x
, int y
)
378 static void Display(void)
380 glClear(GL_COLOR_BUFFER_BIT
);
382 glUseProgram(program
);
383 glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 0, 1.0, 1.0, 0.0, 0.0);
384 glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB
, 1, 0.0, 0.0, 1.0, 1.0);
385 glBegin(GL_TRIANGLES
);
389 glVertex3f( 0.9, -0.9, -970.0);
392 glTexCoord3f(1,-1,0);
393 glVertex3f( 0.9, 0.9, -30.0);
396 glTexCoord3f(-1,0,0);
397 glVertex3f(-0.9, 0.0, -30.0);
408 int main(int argc
, char **argv
)
410 glutInit(&argc
, argv
);
411 glutInitWindowPosition(0, 0);
412 glutInitWindowSize(250, 250);
413 glutInitDisplayMode(GLUT_RGB
| GLUT_SINGLE
| GLUT_DEPTH
);
415 glutCreateWindow(filename
? filename
: "fp-tri");
417 glutReshapeFunc(Reshape
);
418 glutKeyboardFunc(Key
);
419 glutDisplayFunc(Display
);
424 signal(SIGALRM
, alarmhandler
);