11 static const char *filename
= NULL
;
12 static GLuint nr_steps
= 4;
13 static GLuint prim
= GL_TRIANGLES
;
14 static GLfloat psz
= 1.0;
15 static GLboolean pointsmooth
= 0;
16 static GLboolean program_point_size
= 0;
18 static GLuint fragShader
;
19 static GLuint vertShader
;
20 static GLuint program
;
22 static void usage( char *name
)
24 fprintf( stderr
, "usage: %s [ options ] shader_filename\n", name
);
25 fprintf( stderr
, "\n" );
26 fprintf( stderr
, "options:\n" );
27 fprintf( stderr
, " -f flat shaded\n" );
28 fprintf( stderr
, " -nNr subdivision steps\n" );
32 static void load_and_compile_shader(GLuint shader
, const char *text
)
36 glShaderSource(shader
, 1, (const GLchar
**) &text
, NULL
);
38 glCompileShader(shader
);
40 glGetShaderiv(shader
, GL_COMPILE_STATUS
, &stat
);
44 glGetShaderInfoLog(shader
, 1000, &len
, log
);
45 fprintf(stderr
, "vp-tris: problem compiling shader:\n%s\n", log
);
50 static void read_shader(GLuint shader
, const char *filename
)
52 const int max
= 100*1000;
54 char *buffer
= (char*) malloc(max
);
55 FILE *f
= fopen(filename
, "r");
57 fprintf(stderr
, "vp-tris: Unable to open shader file %s\n", filename
);
61 n
= fread(buffer
, 1, max
, f
);
62 printf("vp-tris: read %d bytes from shader file %s\n", n
, filename
);
65 load_and_compile_shader(shader
, buffer
);
72 static void check_link(GLuint prog
)
75 glGetProgramiv(prog
, GL_LINK_STATUS
, &stat
);
79 glGetProgramInfoLog(prog
, 1000, &len
, log
);
80 fprintf(stderr
, "Linker error:\n%s\n", log
);
84 static void setup_uniforms(void)
87 GLint loc1f
= glGetUniformLocationARB(program
, "Offset1f");
88 GLint loc2f
= glGetUniformLocationARB(program
, "Offset2f");
89 GLint loc4f
= glGetUniformLocationARB(program
, "Offset4f");
97 glUniform1fv(loc1f
, 16, vecKer
);
100 glUniform2fv(loc2f
, 8, vecKer
);
103 glUniform4fv(loc4f
, 4, vecKer
);
108 GLint loc1f
= glGetUniformLocationARB(program
, "KernelValue1f");
109 GLint loc2f
= glGetUniformLocationARB(program
, "KernelValue2f");
110 GLint loc4f
= glGetUniformLocationARB(program
, "KernelValue4f");
112 { 1.0, 0.0, 0.0, 0.25,
122 glUniform1fv(loc1f
, 16, vecKer
);
125 glUniform2fv(loc2f
, 8, vecKer
);
128 glUniform4fv(loc4f
, 4, vecKer
);
132 static void prepare_shaders(void)
134 static const char *fragShaderText
=
136 " gl_FragColor = gl_Color;\n"
138 static const char *vertShaderText
=
140 " gl_FrontColor = gl_Color;\n"
141 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
143 fragShader
= glCreateShader(GL_FRAGMENT_SHADER
);
144 load_and_compile_shader(fragShader
, fragShaderText
);
147 vertShader
= glCreateShader(GL_VERTEX_SHADER
);
149 read_shader(vertShader
, filename
);
151 load_and_compile_shader(vertShader
, vertShaderText
);
153 program
= glCreateProgram();
154 glAttachShader(program
, fragShader
);
155 glAttachShader(program
, vertShader
);
156 glLinkProgram(program
);
158 glUseProgram(program
);
163 static void args(int argc
, char *argv
[])
167 for (i
= 1; i
< argc
; i
++) {
168 if (strncmp(argv
[i
], "-n", 2) == 0) {
169 nr_steps
= atoi((argv
[i
]) + 2);
171 else if (strcmp(argv
[i
], "-f") == 0) {
172 glShadeModel(GL_FLAT
);
174 else if (i
== argc
- 1) {
200 static void make_midpoint( union vert
*out
,
201 const union vert
*v0
,
202 const union vert
*v1
)
205 for (i
= 0; i
< 6; i
++)
206 out
->f
[i
] = v0
->f
[i
] + .5 * (v1
->f
[i
] - v0
->f
[i
]);
209 static void subdiv( union vert
*v0
,
215 glColor3fv(v0
->v
.color
);
216 glVertex3fv(v0
->v
.pos
);
217 glColor3fv(v1
->v
.color
);
218 glVertex3fv(v1
->v
.pos
);
219 glColor3fv(v2
->v
.color
);
220 glVertex3fv(v2
->v
.pos
);
225 make_midpoint(&m
[0], v0
, v1
);
226 make_midpoint(&m
[1], v1
, v2
);
227 make_midpoint(&m
[2], v2
, v0
);
229 subdiv(&m
[0], &m
[2], v0
, depth
-1);
230 subdiv(&m
[1], &m
[0], v1
, depth
-1);
231 subdiv(&m
[2], &m
[1], v2
, depth
-1);
232 subdiv(&m
[0], &m
[1], &m
[2], depth
-1);
236 static void enable( GLenum value
, GLboolean flag
)
245 #define ASSIGN_3V( V, V0, V1, V2 ) \
252 static void Display( void )
254 glClearColor(0.3, 0.3, 0.3, 1);
255 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
258 glUseProgram(program
);
259 enable( GL_POINT_SMOOTH
, pointsmooth
);
260 enable( GL_VERTEX_PROGRAM_POINT_SIZE_ARB
, program_point_size
);
268 ASSIGN_3V(v
[0].v
.color
, 0,0,1);
269 ASSIGN_3V(v
[0].v
.pos
, 0.9, -0.9, 0.0);
270 ASSIGN_3V(v
[1].v
.color
, 1,0,0);
271 ASSIGN_3V(v
[1].v
.pos
, 0.9, 0.9, 0.0);
272 ASSIGN_3V(v
[2].v
.color
, 0,1,0);
273 ASSIGN_3V(v
[2].v
.pos
, -0.9, 0, 0.0);
275 subdiv(&v
[0], &v
[1], &v
[2], nr_steps
);
285 static void Reshape( int width
, int height
)
287 glViewport( 0, 0, width
, height
);
288 glMatrixMode( GL_PROJECTION
);
290 glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
291 glMatrixMode( GL_MODELVIEW
);
293 /*glTranslatef( 0.0, 0.0, -15.0 );*/
297 static void CleanUp(void)
299 glDeleteShader(fragShader
);
300 glDeleteShader(vertShader
);
301 glDeleteProgram(program
);
304 static void Key( unsigned char key
, int x
, int y
)
323 pointsmooth
= !pointsmooth
;
326 program_point_size
= !program_point_size
;
348 int main( int argc
, char *argv
[] )
350 glutInit( &argc
, argv
);
351 glutInitWindowPosition( 0, 0 );
352 glutInitWindowSize( 250, 250 );
353 glutInitDisplayMode( GLUT_RGB
| GLUT_SINGLE
| GLUT_DEPTH
);
354 glutCreateWindow(argv
[argc
-1]);
356 glutReshapeFunc( Reshape
);
357 glutKeyboardFunc( Key
);
358 glutDisplayFunc( Display
);