Merge commit 'origin/gallium-0.1'
[mesa.git] / progs / glsl / bump.c
1 /**
2 * Procedural Bump Mapping demo. Uses the example shaders from
3 * chapter 11 of the OpenGL Shading Language "orange" book.
4 * 16 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/glut.h>
13 #include <GL/glu.h>
14 #include <GL/glext.h>
15 #include "extfuncs.h"
16 #include "shaderutil.h"
17
18
19 static char *FragProgFile = "CH11-bumpmap.frag";
20 static char *VertProgFile = "CH11-bumpmap.vert";
21
22 /* program/shader objects */
23 static GLuint fragShader;
24 static GLuint vertShader;
25 static GLuint program;
26
27
28 static struct uniform_info Uniforms[] = {
29 { "LightPosition", 3, GL_FLOAT, { 0.57737, 0.57735, 0.57735, 0.0 }, -1 },
30 { "SurfaceColor", 3, GL_FLOAT, { 0.8, 0.8, 0.2, 0 }, -1 },
31 { "BumpDensity", 1, GL_FLOAT, { 10.0, 0, 0, 0 }, -1 },
32 { "BumpSize", 1, GL_FLOAT, { 0.125, 0, 0, 0 }, -1 },
33 { "SpecularFactor", 1, GL_FLOAT, { 0.5, 0, 0, 0 }, -1 },
34 END_OF_UNIFORMS
35 };
36
37 static GLint win = 0;
38
39 static GLfloat xRot = 20.0f, yRot = 0.0f, zRot = 0.0f;
40
41 static GLuint tangentAttrib;
42
43 static GLboolean Anim = GL_FALSE;
44
45
46 static void
47 CheckError(int line)
48 {
49 GLenum err = glGetError();
50 if (err) {
51 printf("GL Error %s (0x%x) at line %d\n",
52 gluErrorString(err), (int) err, line);
53 }
54 }
55
56 /*
57 * Draw a square, specifying normal and tangent vectors.
58 */
59 static void
60 Square(GLfloat size)
61 {
62 glNormal3f(0, 0, 1);
63 glVertexAttrib3f_func(tangentAttrib, 1, 0, 0);
64 glBegin(GL_POLYGON);
65 glTexCoord2f(0, 0); glVertex2f(-size, -size);
66 glTexCoord2f(1, 0); glVertex2f( size, -size);
67 glTexCoord2f(1, 1); glVertex2f( size, size);
68 glTexCoord2f(0, 1); glVertex2f(-size, size);
69 glEnd();
70 }
71
72
73 static void
74 Cube(GLfloat size)
75 {
76 /* +X */
77 glPushMatrix();
78 glRotatef(90, 0, 1, 0);
79 glTranslatef(0, 0, size);
80 Square(size);
81 glPopMatrix();
82
83 /* -X */
84 glPushMatrix();
85 glRotatef(-90, 0, 1, 0);
86 glTranslatef(0, 0, size);
87 Square(size);
88 glPopMatrix();
89
90 /* +Y */
91 glPushMatrix();
92 glRotatef(90, 1, 0, 0);
93 glTranslatef(0, 0, size);
94 Square(size);
95 glPopMatrix();
96
97 /* -Y */
98 glPushMatrix();
99 glRotatef(-90, 1, 0, 0);
100 glTranslatef(0, 0, size);
101 Square(size);
102 glPopMatrix();
103
104
105 /* +Z */
106 glPushMatrix();
107 glTranslatef(0, 0, size);
108 Square(size);
109 glPopMatrix();
110
111 /* -Z */
112 glPushMatrix();
113 glRotatef(180, 0, 1, 0);
114 glTranslatef(0, 0, size);
115 Square(size);
116 glPopMatrix();
117
118 }
119
120
121 static void
122 Idle(void)
123 {
124 GLint t = glutGet(GLUT_ELAPSED_TIME);
125 yRot = t * 0.05;
126 glutPostRedisplay();
127 }
128
129
130 static void
131 Redisplay(void)
132 {
133 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
134
135 glPushMatrix();
136 glRotatef(xRot, 1.0f, 0.0f, 0.0f);
137 glRotatef(yRot, 0.0f, 1.0f, 0.0f);
138 glRotatef(zRot, 0.0f, 0.0f, 1.0f);
139
140 Cube(1.5);
141
142 glPopMatrix();
143
144 CheckError(__LINE__);
145
146 glutSwapBuffers();
147 }
148
149
150 static void
151 Reshape(int width, int height)
152 {
153 glViewport(0, 0, width, height);
154 glMatrixMode(GL_PROJECTION);
155 glLoadIdentity();
156 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
157 glMatrixMode(GL_MODELVIEW);
158 glLoadIdentity();
159 glTranslatef(0.0f, 0.0f, -15.0f);
160 }
161
162
163 static void
164 CleanUp(void)
165 {
166 glDeleteShader_func(fragShader);
167 glDeleteShader_func(vertShader);
168 glDeleteProgram_func(program);
169 glutDestroyWindow(win);
170 }
171
172
173 static void
174 Key(unsigned char key, int x, int y)
175 {
176 const GLfloat step = 2.0;
177 (void) x;
178 (void) y;
179
180 switch(key) {
181 case 'a':
182 Anim = !Anim;
183 glutIdleFunc(Anim ? Idle : NULL);
184 break;
185 case 'z':
186 zRot += step;
187 break;
188 case 'Z':
189 zRot -= step;
190 break;
191 case 27:
192 CleanUp();
193 exit(0);
194 break;
195 }
196 glutPostRedisplay();
197 }
198
199
200 static void
201 SpecialKey(int key, int x, int y)
202 {
203 const GLfloat step = 2.0;
204
205 (void) x;
206 (void) y;
207
208 switch(key) {
209 case GLUT_KEY_UP:
210 xRot += step;
211 break;
212 case GLUT_KEY_DOWN:
213 xRot -= step;
214 break;
215 case GLUT_KEY_LEFT:
216 yRot -= step;
217 break;
218 case GLUT_KEY_RIGHT:
219 yRot += step;
220 break;
221 }
222 glutPostRedisplay();
223 }
224
225
226 static void
227 Init(void)
228 {
229 if (!ShadersSupported())
230 exit(1);
231
232 GetExtensionFuncs();
233
234 vertShader = CompileShaderFile(GL_VERTEX_SHADER, VertProgFile);
235 fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, FragProgFile);
236 program = LinkShaders(vertShader, fragShader);
237
238 glUseProgram_func(program);
239
240 assert(glIsProgram_func(program));
241 assert(glIsShader_func(fragShader));
242 assert(glIsShader_func(vertShader));
243
244 assert(glGetError() == 0);
245
246 CheckError(__LINE__);
247
248 InitUniforms(program, Uniforms);
249
250 CheckError(__LINE__);
251
252 tangentAttrib = glGetAttribLocation_func(program, "Tangent");
253 printf("Tangent Attrib: %d\n", tangentAttrib);
254
255 assert(tangentAttrib >= 0);
256
257 CheckError(__LINE__);
258
259 glClearColor(0.4f, 0.4f, 0.8f, 0.0f);
260
261 glEnable(GL_DEPTH_TEST);
262
263 glColor3f(1, 0, 0);
264 }
265
266
267 static void
268 ParseOptions(int argc, char *argv[])
269 {
270 int i;
271 for (i = 1; i < argc; i++) {
272 if (strcmp(argv[i], "-fs") == 0) {
273 FragProgFile = argv[i+1];
274 }
275 else if (strcmp(argv[i], "-vs") == 0) {
276 VertProgFile = argv[i+1];
277 }
278 }
279 }
280
281
282 int
283 main(int argc, char *argv[])
284 {
285 glutInit(&argc, argv);
286 glutInitWindowPosition( 0, 0);
287 glutInitWindowSize(400, 400);
288 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
289 win = glutCreateWindow(argv[0]);
290 glutReshapeFunc(Reshape);
291 glutKeyboardFunc(Key);
292 glutSpecialFunc(SpecialKey);
293 glutDisplayFunc(Redisplay);
294 ParseOptions(argc, argv);
295 Init();
296 glutMainLoop();
297 return 0;
298 }
299