17 #define TEXTURE_FILE "../images/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()
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 static void prepare_shaders()
163 static const char *fragShaderText
=
165 " gl_FragColor = gl_Color;\n"
167 static const char *vertShaderText
=
169 " gl_FrontColor = gl_Color;\n"
170 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
172 fragShader
= glCreateShader(GL_FRAGMENT_SHADER
);
174 read_shader(fragShader
, filename
);
176 load_and_compile_shader(fragShader
, fragShaderText
);
179 vertShader
= glCreateShader(GL_VERTEX_SHADER
);
180 load_and_compile_shader(vertShader
, vertShaderText
);
182 program
= glCreateProgram();
183 glAttachShader(program
, fragShader
);
184 glAttachShader(program
, vertShader
);
185 glLinkProgram(program
);
187 glUseProgram(program
);
193 #define SIZE (1<<LEVELS)
194 static int TexWidth
= SIZE
, TexHeight
= SIZE
;
198 ResetTextureLevel( int i
)
200 GLubyte tex2d
[SIZE
*SIZE
][4];
203 GLint Width
= TexWidth
/ (1 << i
);
204 GLint Height
= TexHeight
/ (1 << i
);
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;
216 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
218 glTexImage2D(GL_TEXTURE_2D
, i
, GL_RGB
, Width
, Height
, 0,
219 GL_RGBA
, GL_UNSIGNED_BYTE
, tex2d
);
229 for (i
= 0; i
<= LEVELS
; i
++)
231 ResetTextureLevel(i
);
235 static void Init( void )
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
);
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);
259 GLubyte data
[32][32];
265 for (i
= 0; i
< 32; i
++)
266 for (j
= 0; j
< 32; j
++)
279 int i2
= i
- height
/ 2;
280 int j2
= j
- width
/ 2;
283 if ( -h8
<= i2
&& i2
<= h8
&& -w8
<= j2
&& j2
<= w8
) {
285 } else if ( -2 * h8
<= i2
&& i2
<= 2 * h8
&& -2 * w8
<= j2
&& j2
<= 2 * w8
) {
287 } else if ( -3 * h8
<= i2
&& i2
<= 3 * h8
&& -3 * w8
<= j2
&& j2
<= 3 * w8
) {
294 glTexImage2D( GL_TEXTURE_2D
, 0,
297 GL_ALPHA
, GL_UNSIGNED_BYTE
, data
);
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);
308 glClearColor(.1, .3, .5, 0);
314 static void args(int argc
, char *argv
[])
318 for (i
= 1; i
< argc
; i
++) {
319 if (strcmp(argv
[i
], "-fps") == 0) {
322 else if (i
== argc
- 1) {
336 static void Reshape(int width
, int height
)
339 glViewport(0, 0, (GLint
)width
, (GLint
)height
);
341 glMatrixMode(GL_PROJECTION
);
343 glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
344 glMatrixMode(GL_MODELVIEW
);
347 static void CleanUp(void)
349 glDeleteShader(fragShader
);
350 glDeleteShader(vertShader
);
351 glDeleteProgram(program
);
354 static void Key(unsigned char key
, int x
, int y
)
368 static void Display(void)
370 glClear(GL_COLOR_BUFFER_BIT
);
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
);
379 glVertex3f( 0.9, -0.9, -30.0);
382 glTexCoord3f(1,-1,0);
383 glVertex3f( 0.9, 0.9, -30.0);
386 glTexCoord3f(-1,0,0);
387 glVertex3f(-0.9, 0.0, -30.0);
398 int main(int argc
, char **argv
)
400 glutInit(&argc
, argv
);
401 glutInitWindowPosition(0, 0);
402 glutInitWindowSize(250, 250);
403 glutInitDisplayMode(GLUT_RGB
| GLUT_SINGLE
| GLUT_DEPTH
);
405 glutCreateWindow(filename
? filename
: "fp-tri");
407 glutReshapeFunc(Reshape
);
408 glutKeyboardFunc(Key
);
409 glutDisplayFunc(Display
);
414 signal(SIGALRM
, alarmhandler
);