progs/glsl: compile with scons and glew
[mesa.git] / progs / glsl / toyball.c
1 /**
2 * "Toy Ball" shader 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/glew.h>
13 #include <GL/gl.h>
14 #include <GL/glut.h>
15 #include <GL/glext.h>
16 #include "extfuncs.h"
17 #include "shaderutil.h"
18
19
20 static char *FragProgFile = "CH11-toyball.frag";
21 static char *VertProgFile = "CH11-toyball.vert";
22
23 /* program/shader objects */
24 static GLuint fragShader;
25 static GLuint vertShader;
26 static GLuint program;
27
28
29 static struct uniform_info Uniforms[] = {
30 { "LightDir", 4, GL_FLOAT, { 0.57737, 0.57735, 0.57735, 0.0 }, -1 },
31 { "HVector", 4, GL_FLOAT, { 0.32506, 0.32506, 0.88808, 0.0 }, -1 },
32 { "BallCenter", 4, GL_FLOAT, { 0.0, 0.0, 0.0, 1.0 }, -1 },
33 { "SpecularColor", 4, GL_FLOAT, { 0.4, 0.4, 0.4, 60.0 }, -1 },
34 { "Red", 4, GL_FLOAT, { 0.6, 0.0, 0.0, 1.0 }, -1 },
35 { "Blue", 4, GL_FLOAT, { 0.0, 0.3, 0.6, 1.0 }, -1 },
36 { "Yellow", 4, GL_FLOAT, { 0.6, 0.5, 0.0, 1.0 }, -1 },
37 { "HalfSpace0", 4, GL_FLOAT, { 1.0, 0.0, 0.0, 0.2 }, -1 },
38 { "HalfSpace1", 4, GL_FLOAT, { 0.309016994, 0.951056516, 0.0, 0.2 }, -1 },
39 { "HalfSpace2", 4, GL_FLOAT, { -0.809016994, 0.587785252, 0.0, 0.2 }, -1 },
40 { "HalfSpace3", 4, GL_FLOAT, { -0.809016994, -0.587785252, 0.0, 0.2 }, -1 },
41 { "HalfSpace4", 4, GL_FLOAT, { 0.309116994, -0.951056516, 0.0, 0.2 }, -1 },
42 { "InOrOutInit", 1, GL_FLOAT, { -3.0, 0, 0, 0 }, -1 },
43 { "StripeWidth", 1, GL_FLOAT, { 0.3, 0, 0, 0 }, -1 },
44 { "FWidth", 1, GL_FLOAT, { 0.005, 0, 0, 0 }, -1 },
45 END_OF_UNIFORMS
46 };
47
48 static GLint win = 0;
49 static GLboolean Anim = GL_FALSE;
50 static GLfloat TexRot = 0.0;
51 static GLfloat xRot = 0.0f, yRot = 0.0f, zRot = 0.0f;
52
53
54 static void
55 Idle(void)
56 {
57 TexRot += 2.0;
58 if (TexRot > 360.0)
59 TexRot -= 360.0;
60 glutPostRedisplay();
61 }
62
63
64 static void
65 Redisplay(void)
66 {
67 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
68
69 glPushMatrix();
70 glRotatef(xRot, 1.0f, 0.0f, 0.0f);
71 glRotatef(yRot, 0.0f, 1.0f, 0.0f);
72 glRotatef(zRot, 0.0f, 0.0f, 1.0f);
73
74 glMatrixMode(GL_TEXTURE);
75 glLoadIdentity();
76 glRotatef(TexRot, 0.0f, 1.0f, 0.0f);
77 glMatrixMode(GL_MODELVIEW);
78
79 glutSolidSphere(2.0, 20, 10);
80
81 glPopMatrix();
82
83 glutSwapBuffers();
84 }
85
86
87 static void
88 Reshape(int width, int height)
89 {
90 glViewport(0, 0, width, height);
91 glMatrixMode(GL_PROJECTION);
92 glLoadIdentity();
93 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
94 glMatrixMode(GL_MODELVIEW);
95 glLoadIdentity();
96 glTranslatef(0.0f, 0.0f, -15.0f);
97 }
98
99
100 static void
101 CleanUp(void)
102 {
103 glDeleteShader_func(fragShader);
104 glDeleteShader_func(vertShader);
105 glDeleteProgram_func(program);
106 glutDestroyWindow(win);
107 }
108
109
110 static void
111 Key(unsigned char key, int x, int y)
112 {
113 const GLfloat step = 2.0;
114 (void) x;
115 (void) y;
116
117 switch(key) {
118 case 'a':
119 Anim = !Anim;
120 if (Anim)
121 glutIdleFunc(Idle);
122 else
123 glutIdleFunc(NULL);
124 break;
125 case 'z':
126 zRot += step;
127 break;
128 case 'Z':
129 zRot -= step;
130 break;
131 case 27:
132 CleanUp();
133 exit(0);
134 break;
135 }
136 glutPostRedisplay();
137 }
138
139
140 static void
141 SpecialKey(int key, int x, int y)
142 {
143 const GLfloat step = 2.0;
144
145 (void) x;
146 (void) y;
147
148 switch(key) {
149 case GLUT_KEY_UP:
150 xRot += step;
151 break;
152 case GLUT_KEY_DOWN:
153 xRot -= step;
154 break;
155 case GLUT_KEY_LEFT:
156 yRot -= step;
157 break;
158 case GLUT_KEY_RIGHT:
159 yRot += step;
160 break;
161 }
162 glutPostRedisplay();
163 }
164
165
166
167 static void
168 Init(void)
169 {
170 if (!ShadersSupported())
171 exit(1);
172
173 GetExtensionFuncs();
174
175 vertShader = CompileShaderFile(GL_VERTEX_SHADER, VertProgFile);
176 fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, FragProgFile);
177 program = LinkShaders(vertShader, fragShader);
178
179 glUseProgram_func(program);
180
181 InitUniforms(program, Uniforms);
182
183 assert(glGetError() == 0);
184
185 glClearColor(0.4f, 0.4f, 0.8f, 0.0f);
186
187 glEnable(GL_DEPTH_TEST);
188
189 glColor3f(1, 0, 0);
190 }
191
192
193 static void
194 ParseOptions(int argc, char *argv[])
195 {
196 int i;
197 for (i = 1; i < argc; i++) {
198 if (strcmp(argv[i], "-fs") == 0) {
199 FragProgFile = argv[i+1];
200 }
201 else if (strcmp(argv[i], "-vs") == 0) {
202 VertProgFile = argv[i+1];
203 }
204 }
205 }
206
207
208 int
209 main(int argc, char *argv[])
210 {
211 glutInit(&argc, argv);
212 glutInitWindowPosition( 0, 0);
213 glutInitWindowSize(400, 400);
214 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
215 win = glutCreateWindow(argv[0]);
216 glewInit();
217 glutReshapeFunc(Reshape);
218 glutKeyboardFunc(Key);
219 glutSpecialFunc(SpecialKey);
220 glutDisplayFunc(Redisplay);
221 ParseOptions(argc, argv);
222 Init();
223 glutMainLoop();
224 return 0;
225 }
226