2 * "Brick" shader demo. Uses the example shaders from chapter 6 of
3 * the OpenGL Shading Language "orange" book.
13 #include "glut_wrap.h"
14 #include "shaderutil.h"
17 static char *FragProgFile
= "CH06-brick.frag";
18 static char *VertProgFile
= "CH06-brick.vert";
20 /* program/shader objects */
21 static GLuint fragShader
;
22 static GLuint vertShader
;
23 static GLuint program
;
25 static struct uniform_info Uniforms
[] = {
27 { "LightPosition", 1, GL_FLOAT_VEC3
, { 0.1, 0.1, 9.0, 0}, -1 },
29 { "BrickColor", 1, GL_FLOAT_VEC3
, { 0.8, 0.2, 0.2, 0 }, -1 },
30 { "MortarColor", 1, GL_FLOAT_VEC3
, { 0.6, 0.6, 0.6, 0 }, -1 },
31 { "BrickSize", 1, GL_FLOAT_VEC2
, { 1.0, 0.3, 0, 0 }, -1 },
32 { "BrickPct", 1, GL_FLOAT_VEC2
, { 0.9, 0.8, 0, 0 }, -1 },
39 static GLfloat xRot
= 0.0f
, yRot
= 0.0f
, zRot
= 0.0f
;
47 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
50 glRotatef(xRot
, 1.0f
, 0.0f
, 0.0f
);
51 glRotatef(yRot
, 0.0f
, 1.0f
, 0.0f
);
52 glRotatef(zRot
, 0.0f
, 0.0f
, 1.0f
);
55 glTexCoord2f(0, 0); glVertex2f(-2, -2);
56 glTexCoord2f(1, 0); glVertex2f( 2, -2);
57 glTexCoord2f(1, 1); glVertex2f( 2, 2);
58 glTexCoord2f(0, 1); glVertex2f(-2, 2);
68 Reshape(int width
, int height
)
70 glViewport(0, 0, width
, height
);
71 glMatrixMode(GL_PROJECTION
);
73 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
74 glMatrixMode(GL_MODELVIEW
);
76 glTranslatef(0.0f
, 0.0f
, -15.0f
);
83 DeleteShader(fragShader
);
84 DeleteShader(vertShader
);
85 DeleteProgram(program
);
86 glutDestroyWindow(win
);
91 Key(unsigned char key
, int x
, int y
)
113 SpecialKey(int key
, int x
, int y
)
115 const GLfloat step
= 3.0f
;
142 if (!ShadersSupported())
145 vertShader
= CompileShaderFile(GL_VERTEX_SHADER
, VertProgFile
);
146 fragShader
= CompileShaderFile(GL_FRAGMENT_SHADER
, FragProgFile
);
147 program
= LinkShaders(vertShader
, fragShader
);
151 SetUniformValues(program
, Uniforms
);
152 PrintUniforms(Uniforms
);
154 assert(glGetError() == 0);
156 glClearColor(0.4f
, 0.4f
, 0.8f
, 0.0f
);
158 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER
));
160 if (GLEW_VERSION_2_0
) {
161 assert(glIsProgram(program
));
162 assert(glIsShader(fragShader
));
163 assert(glIsShader(vertShader
));
171 ParseOptions(int argc
, char *argv
[])
174 for (i
= 1; i
< argc
; i
++) {
175 if (strcmp(argv
[i
], "-fs") == 0) {
176 FragProgFile
= argv
[i
+1];
178 else if (strcmp(argv
[i
], "-vs") == 0) {
179 VertProgFile
= argv
[i
+1];
186 main(int argc
, char *argv
[])
188 glutInit(&argc
, argv
);
189 glutInitWindowSize(400, 400);
190 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
191 win
= glutCreateWindow(argv
[0]);
193 glutReshapeFunc(Reshape
);
194 glutKeyboardFunc(Key
);
195 glutSpecialFunc(SpecialKey
);
196 glutDisplayFunc(Redisplay
);
197 ParseOptions(argc
, argv
);