522698b5d433886635ed08598dd52a7ba33d540e
2 * "Brick" shader demo. Uses the example shaders from chapter 6 of
3 * the OpenGL Shading Language "orange" book.
18 static char *FragProgFile
= "CH06-brick.frag.txt";
19 static char *VertProgFile
= "CH06-brick.vert.txt";
21 /* program/shader objects */
22 static GLuint fragShader
;
23 static GLuint vertShader
;
24 static GLuint program
;
34 static struct uniform_info Uniforms
[] = {
36 { "LightPosition", 3, -1, { 0.1, 0.1, 9.0, 0} },
38 { "BrickColor", 3, -1, { 0.8, 0.2, 0.2, 0 } },
39 { "MortarColor", 3, -1, { 0.6, 0.6, 0.6, 0 } },
40 { "BrickSize", 2, -1, { 1.0, 0.3, 0, 0 } },
41 { "BrickPct", 2, -1, { 0.9, 0.8, 0, 0 } },
42 { NULL
, 0, 0, { 0, 0, 0, 0 } }
48 static GLfloat xRot
= 0.0f
, yRot
= 0.0f
, zRot
= 0.0f
;
56 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
59 glRotatef(xRot
, 1.0f
, 0.0f
, 0.0f
);
60 glRotatef(yRot
, 0.0f
, 1.0f
, 0.0f
);
61 glRotatef(zRot
, 0.0f
, 0.0f
, 1.0f
);
64 glTexCoord2f(0, 0); glVertex2f(-2, -2);
65 glTexCoord2f(1, 0); glVertex2f( 2, -2);
66 glTexCoord2f(1, 1); glVertex2f( 2, 2);
67 glTexCoord2f(0, 1); glVertex2f(-2, 2);
77 Reshape(int width
, int height
)
79 glViewport(0, 0, width
, height
);
80 glMatrixMode(GL_PROJECTION
);
82 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
83 glMatrixMode(GL_MODELVIEW
);
85 glTranslatef(0.0f
, 0.0f
, -15.0f
);
92 glDeleteShader_func(fragShader
);
93 glDeleteShader_func(vertShader
);
94 glDeleteProgram_func(program
);
95 glutDestroyWindow(win
);
100 Key(unsigned char key
, int x
, int y
)
122 SpecialKey(int key
, int x
, int y
)
124 const GLfloat step
= 3.0f
;
149 LoadAndCompileShader(GLuint shader
, const char *text
)
153 glShaderSource_func(shader
, 1, (const GLchar
**) &text
, NULL
);
155 glCompileShader_func(shader
);
157 glGetShaderiv_func(shader
, GL_COMPILE_STATUS
, &stat
);
161 glGetShaderInfoLog_func(shader
, 1000, &len
, log
);
162 fprintf(stderr
, "brick: problem compiling shader: %s\n", log
);
166 printf("Shader compiled OK\n");
172 * Read a shader from a file.
175 ReadShader(GLuint shader
, const char *filename
)
177 const int max
= 100*1000;
179 char *buffer
= (char*) malloc(max
);
180 FILE *f
= fopen(filename
, "r");
182 fprintf(stderr
, "brick: Unable to open shader file %s\n", filename
);
186 n
= fread(buffer
, 1, max
, f
);
187 printf("brick: read %d bytes from shader file %s\n", n
, filename
);
190 LoadAndCompileShader(shader
, buffer
);
199 CheckLink(GLuint prog
)
202 glGetProgramiv_func(prog
, GL_LINK_STATUS
, &stat
);
206 glGetProgramInfoLog_func(prog
, 1000, &len
, log
);
207 fprintf(stderr
, "Linker error:\n%s\n", log
);
210 fprintf(stderr
, "Link success!\n");
221 version
= (const char *) glGetString(GL_VERSION
);
222 if (version
[0] != '2' || version
[1] != '.') {
223 printf("Warning: this program expects OpenGL 2.0\n");
229 vertShader
= glCreateShader_func(GL_VERTEX_SHADER
);
230 ReadShader(vertShader
, VertProgFile
);
232 fragShader
= glCreateShader_func(GL_FRAGMENT_SHADER
);
233 ReadShader(fragShader
, FragProgFile
);
235 program
= glCreateProgram_func();
236 glAttachShader_func(program
, fragShader
);
237 glAttachShader_func(program
, vertShader
);
238 glLinkProgram_func(program
);
240 glUseProgram_func(program
);
242 for (i
= 0; Uniforms
[i
].name
; i
++) {
244 = glGetUniformLocation_func(program
, Uniforms
[i
].name
);
245 printf("Uniform %s location: %d\n", Uniforms
[i
].name
,
246 Uniforms
[i
].location
);
247 switch (Uniforms
[i
].size
) {
249 glUniform1fv_func(Uniforms
[i
].location
, 1, Uniforms
[i
].value
);
252 glUniform2fv_func(Uniforms
[i
].location
, 1, Uniforms
[i
].value
);
255 glUniform3fv_func(Uniforms
[i
].location
, 1, Uniforms
[i
].value
);
258 glUniform4fv_func(Uniforms
[i
].location
, 1, Uniforms
[i
].value
);
265 assert(glGetError() == 0);
267 glClearColor(0.4f
, 0.4f
, 0.8f
, 0.0f
);
269 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER
));
271 assert(glIsProgram_func(program
));
272 assert(glIsShader_func(fragShader
));
273 assert(glIsShader_func(vertShader
));
280 ParseOptions(int argc
, char *argv
[])
283 for (i
= 1; i
< argc
; i
++) {
284 if (strcmp(argv
[i
], "-fs") == 0) {
285 FragProgFile
= argv
[i
+1];
287 else if (strcmp(argv
[i
], "-vs") == 0) {
288 VertProgFile
= argv
[i
+1];
295 main(int argc
, char *argv
[])
297 glutInit(&argc
, argv
);
298 glutInitWindowPosition( 0, 0);
299 glutInitWindowSize(400, 400);
300 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
301 win
= glutCreateWindow(argv
[0]);
302 glutReshapeFunc(Reshape
);
303 glutKeyboardFunc(Key
);
304 glutSpecialFunc(SpecialKey
);
305 glutDisplayFunc(Redisplay
);
306 ParseOptions(argc
, argv
);