2 * Simple example for testing basic features of
11 #define GL_GLEXT_PROTOTYPES
13 #include "glut_wrap.h"
14 #include "shaderutil.h"
16 static const char *filename
= "bezier.geom";
18 static GLuint fragShader
;
19 static GLuint vertShader
;
20 static GLuint geoShader
;
21 static GLuint program
;
25 GLfloat vertices
[][3] =
26 { { -0.9, -0.9, 0.0 },
38 static struct uniform_info Uniforms
[] = {
39 { "NumSubdivisions", 1, GL_INT
, { 50, 0, 0, 0 }, -1 },
43 static void usage( char *name
)
45 fprintf(stderr
, "usage: %s\n", name
);
46 fprintf(stderr
, "\n" );
50 static void load_and_compile_shader(GLuint shader
, const char *text
)
54 glShaderSource(shader
, 1, (const GLchar
**) &text
, NULL
);
55 glCompileShader(shader
);
56 glGetShaderiv(shader
, GL_COMPILE_STATUS
, &stat
);
60 glGetShaderInfoLog(shader
, 1000, &len
, log
);
61 fprintf(stderr
, "bezier: problem compiling shader:\n%s\n", log
);
66 static void read_shader(GLuint shader
, const char *filename
)
68 const int max
= 100*1000;
70 char *buffer
= (char*) malloc(max
);
71 FILE *f
= fopen(filename
, "r");
73 fprintf(stderr
, "bezier: Unable to open shader file %s\n", filename
);
77 n
= fread(buffer
, 1, max
, f
);
78 printf("bezier: read %d bytes from shader file %s\n", n
, filename
);
81 load_and_compile_shader(shader
, buffer
);
88 static void check_link(GLuint prog
)
91 glGetProgramiv(prog
, GL_LINK_STATUS
, &stat
);
95 glGetProgramInfoLog(prog
, 1000, &len
, log
);
96 fprintf(stderr
, "Linker error:\n%s\n", log
);
100 static void menu_selected(int entry
)
107 Uniforms
[0].value
[0] = entry
;
110 SetUniformValues(program
, Uniforms
);
115 static void menu_init(void)
117 glutCreateMenu(menu_selected
);
119 glutAddMenuEntry("1 Subdivision", 1);
120 glutAddMenuEntry("2 Subdivisions", 2);
121 glutAddMenuEntry("3 Subdivisions", 3);
122 glutAddMenuEntry("4 Subdivisions", 4);
123 glutAddMenuEntry("5 Subdivisions", 5);
124 glutAddMenuEntry("6 Subdivisions", 6);
125 glutAddMenuEntry("7 Subdivisions", 7);
126 glutAddMenuEntry("10 Subdivisions", 10);
127 glutAddMenuEntry("50 Subdivisions", 50);
128 glutAddMenuEntry("100 Subdivisions", 100);
129 glutAddMenuEntry("500 Subdivisions", 500);
131 glutAddMenuEntry("Quit", QUIT
);
133 glutAttachMenu(GLUT_RIGHT_BUTTON
);
136 static void init(void)
138 static const char *fragShaderText
=
140 " gl_FragColor = gl_Color;\n"
142 static const char *vertShaderText
=
144 " gl_FrontColor = gl_Color;\n"
145 " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
147 static const char *geoShaderText
=
149 "#extension GL_ARB_geometry_shader4 : enable\n"
152 " for(int i = 0; i < gl_VerticesIn; ++i)\n"
154 " gl_FrontColor = gl_FrontColorIn[i];\n"
155 " gl_Position = gl_PositionIn[i];\n"
161 if (!ShadersSupported())
164 if (!glutExtensionSupported("GL_ARB_geometry_shader4")) {
165 printf("This demo requires GL_ARB_geometry_shader4\n");
171 fragShader
= glCreateShader(GL_FRAGMENT_SHADER
);
172 load_and_compile_shader(fragShader
, fragShaderText
);
174 vertShader
= glCreateShader(GL_VERTEX_SHADER
);
175 load_and_compile_shader(vertShader
, vertShaderText
);
177 geoShader
= glCreateShader(GL_GEOMETRY_SHADER_ARB
);
179 read_shader(geoShader
, filename
);
181 load_and_compile_shader(geoShader
,
184 program
= glCreateProgram();
185 glAttachShader(program
, vertShader
);
186 glAttachShader(program
, geoShader
);
187 glAttachShader(program
, fragShader
);
189 glProgramParameteriARB(program
, GL_GEOMETRY_INPUT_TYPE_ARB
,
190 GL_LINES_ADJACENCY_ARB
);
191 glProgramParameteriARB(program
, GL_GEOMETRY_OUTPUT_TYPE_ARB
,
196 glGetIntegerv(GL_MAX_GEOMETRY_OUTPUT_VERTICES_ARB
,&temp
);
197 glProgramParameteriARB(program
,GL_GEOMETRY_VERTICES_OUT_ARB
,temp
);
200 glLinkProgram(program
);
202 glUseProgram(program
);
204 SetUniformValues(program
, Uniforms
);
205 PrintUniforms(Uniforms
);
207 assert(glGetError() == 0);
209 glEnableClientState( GL_VERTEX_ARRAY
);
210 glEnableClientState( GL_COLOR_ARRAY
);
212 glVertexPointer( 3, GL_FLOAT
, sizeof(vertices
[0]), vertices
);
213 glColorPointer( 4, GL_FLOAT
, sizeof(color
[0]), color
);
216 static void args(int argc
, char *argv
[])
224 static void Display( void )
226 glClearColor(0, 0, 0, 1);
227 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
229 glUseProgram(program
);
231 glEnable(GL_VERTEX_PROGRAM_ARB
);
233 glDrawArrays(GL_LINES_ADJACENCY_ARB
, 0, 4);
239 static void Reshape( int width
, int height
)
241 glViewport( 0, 0, width
, height
);
242 glMatrixMode( GL_PROJECTION
);
244 glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
245 glMatrixMode( GL_MODELVIEW
);
247 /*glTranslatef( 0.0, 0.0, -15.0 );*/
251 static void CleanUp(void)
253 glDeleteShader(fragShader
);
254 glDeleteShader(vertShader
);
255 glDeleteProgram(program
);
258 static void Key( unsigned char key
, int x
, int y
)
271 int main( int argc
, char *argv
[] )
273 glutInit( &argc
, argv
);
274 glutInitWindowPosition( 0, 0 );
275 glutInitWindowSize( 250, 250 );
276 glutInitDisplayMode( GLUT_RGB
| GLUT_SINGLE
| GLUT_DEPTH
);
277 glutCreateWindow(argv
[0]);
279 glutReshapeFunc( Reshape
);
280 glutKeyboardFunc( Key
);
281 glutDisplayFunc( Display
);