1 /**
2 * "Brick" shader demo. Uses the example shaders from chapter 6 of
3 * the OpenGL Shading Language "orange" book.
4 * 10 Jan 2007
5 */
6
7 #include <assert.h>
8 #include <string.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <math.h>
12 // #include <GL/glew.h>
13 #include <GL/glut.h>
14 #include <GL/glext.h>
15 #include "shaderutil.h"
16
17
18 static char *FragProgFile = "CH06-brick.frag";
19 static char *VertProgFile = "CH06-brick.vert";
20
21 /* program/shader objects */
22 static GLuint fragShader;
23 static GLuint vertShader;
24 static GLuint program;
25
26 static struct uniform_info Uniforms[] = {
27 /* vert */
28 { "LightPosition", 1, GL_FLOAT_VEC3, { 0.1, 0.1, 9.0, 0}, -1 },
29 /* frag */
30 { "BrickColor", 1, GL_FLOAT_VEC3, { 0.8, 0.2, 0.2, 0 }, -1 },
31 { "MortarColor", 1, GL_FLOAT_VEC3, { 0.6, 0.6, 0.6, 0 }, -1 },
32 { "BrickSize", 1, GL_FLOAT_VEC2, { 1.0, 0.3, 0, 0 }, -1 },
33 { "BrickPct", 1, GL_FLOAT_VEC2, { 0.9, 0.8, 0, 0 }, -1 },
34 END_OF_UNIFORMS
35 };
36
37 static GLint win = 0;
38
39
40 static GLfloat xRot = 0.0f, yRot = 0.0f, zRot = 0.0f;
41
42
43
44
45 static void
Redisplay(void)46 Redisplay(void)
47 {
48 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
49
50 glPushMatrix();
51 glRotatef(xRot, 1.0f, 0.0f, 0.0f);
52 glRotatef(yRot, 0.0f, 1.0f, 0.0f);
53 glRotatef(zRot, 0.0f, 0.0f, 1.0f);
54
55 glBegin(GL_POLYGON);
56 glTexCoord2f(0, 0); glVertex2f(-2, -2);
57 glTexCoord2f(1, 0); glVertex2f( 2, -2);
58 glTexCoord2f(1, 1); glVertex2f( 2, 2);
59 glTexCoord2f(0, 1); glVertex2f(-2, 2);
60 glEnd();
61
62 glPopMatrix();
63
64 glutSwapBuffers();
65 }
66
67
68 static void
Reshape(int width,int height)69 Reshape(int width, int height)
70 {
71 glViewport(0, 0, width, height);
72 glMatrixMode(GL_PROJECTION);
73 glLoadIdentity();
74 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
75 glMatrixMode(GL_MODELVIEW);
76 glLoadIdentity();
77 glTranslatef(0.0f, 0.0f, -15.0f);
78 }
79
80
81 static void
CleanUp(void)82 CleanUp(void)
83 {
84 glDeleteShader(fragShader);
85 glDeleteShader(vertShader);
86 glDeleteProgram(program);
87 glutDestroyWindow(win);
88 }
89
90
91 static void
Key(unsigned char key,int x,int y)92 Key(unsigned char key, int x, int y)
93 {
94 (void) x;
95 (void) y;
96
97 switch(key) {
98 case 'z':
99 zRot -= 1.0;
100 break;
101 case 'Z':
102 zRot += 1.0;
103 break;
104 case 27:
105 CleanUp();
106 exit(0);
107 break;
108 }
109 glutPostRedisplay();
110 }
111
112
113 static void
SpecialKey(int key,int x,int y)114 SpecialKey(int key, int x, int y)
115 {
116 const GLfloat step = 3.0f;
117
118 (void) x;
119 (void) y;
120
121 switch(key) {
122 case GLUT_KEY_UP:
123 xRot -= step;
124 break;
125 case GLUT_KEY_DOWN:
126 xRot += step;
127 break;
128 case GLUT_KEY_LEFT:
129 yRot -= step;
130 break;
131 case GLUT_KEY_RIGHT:
132 yRot += step;
133 break;
134 }
135 glutPostRedisplay();
136 }
137
138
139
140 static void
Init(void)141 Init(void)
142 {
143 if (!ShadersSupported())
144 exit(1);
145
146 vertShader = CompileShaderFile(GL_VERTEX_SHADER, VertProgFile);
147 fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, FragProgFile);
148 program = LinkShaders(vertShader, fragShader);
149
150 glUseProgram(program);
151
152 SetUniformValues(program, Uniforms);
153 PrintUniforms(Uniforms);
154
155 assert(glGetError() == 0);
156
157 glClearColor(0.4f, 0.4f, 0.8f, 0.0f);
158
159 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
160
161 assert(glIsProgram(program));
162 assert(glIsShader(fragShader));
163 assert(glIsShader(vertShader));
164
165 glColor3f(1, 0, 0);
166 }
167
168
169 static void
ParseOptions(int argc,char * argv[])170 ParseOptions(int argc, char *argv[])
171 {
172 int i;
173 for (i = 1; i < argc; i++) {
174 if (strcmp(argv[i], "-fs") == 0) {
175 FragProgFile = argv[i+1];
176 }
177 else if (strcmp(argv[i], "-vs") == 0) {
178 VertProgFile = argv[i+1];
179 }
180 }
181 }
182
183
184 int
main(int argc,char * argv[])185 main(int argc, char *argv[])
186 {
187 glutInit(&argc, argv);
188 glutInitWindowSize(400, 400);
189 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
190 win = glutCreateWindow(argv[0]);
191 // glewInit();
192 glutReshapeFunc(Reshape);
193 glutKeyboardFunc(Key);
194 glutSpecialFunc(SpecialKey);
195 glutDisplayFunc(Redisplay);
196 ParseOptions(argc, argv);
197 Init();
198 glutMainLoop();
199 return 0;
200 }
201
202