2 * "Toy Ball" shader demo. Uses the example shaders from
3 * chapter 11 of the OpenGL Shading Language "orange" book.
13 #include "glut_wrap.h"
14 #include "shaderutil.h"
17 static char *FragProgFile
= "CH11-toyball.frag";
18 static char *VertProgFile
= "CH11-toyball.vert";
20 /* program/shader objects */
21 static GLuint fragShader
;
22 static GLuint vertShader
;
23 static GLuint program
;
26 static struct uniform_info Uniforms
[] = {
27 { "LightDir", 1, GL_FLOAT_VEC4
, { 0.57737, 0.57735, 0.57735, 0.0 }, -1 },
28 { "HVector", 1, GL_FLOAT_VEC4
, { 0.32506, 0.32506, 0.88808, 0.0 }, -1 },
29 { "BallCenter", 1, GL_FLOAT_VEC4
, { 0.0, 0.0, 0.0, 1.0 }, -1 },
30 { "SpecularColor", 1, GL_FLOAT_VEC4
, { 0.4, 0.4, 0.4, 60.0 }, -1 },
31 { "Red", 1, GL_FLOAT_VEC4
, { 0.6, 0.0, 0.0, 1.0 }, -1 },
32 { "Blue", 1, GL_FLOAT_VEC4
, { 0.0, 0.3, 0.6, 1.0 }, -1 },
33 { "Yellow", 1, GL_FLOAT_VEC4
, { 0.6, 0.5, 0.0, 1.0 }, -1 },
34 { "HalfSpace0", 1, GL_FLOAT_VEC4
, { 1.0, 0.0, 0.0, 0.2 }, -1 },
35 { "HalfSpace1", 1, GL_FLOAT_VEC4
, { 0.309016994, 0.951056516, 0.0, 0.2 }, -1 },
36 { "HalfSpace2", 1, GL_FLOAT_VEC4
, { -0.809016994, 0.587785252, 0.0, 0.2 }, -1 },
37 { "HalfSpace3", 1, GL_FLOAT_VEC4
, { -0.809016994, -0.587785252, 0.0, 0.2 }, -1 },
38 { "HalfSpace4", 1, GL_FLOAT_VEC4
, { 0.309116994, -0.951056516, 0.0, 0.2 }, -1 },
39 { "InOrOutInit", 1, GL_FLOAT
, { -3.0, 0, 0, 0 }, -1 },
40 { "StripeWidth", 1, GL_FLOAT
, { 0.3, 0, 0, 0 }, -1 },
41 { "FWidth", 1, GL_FLOAT
, { 0.005, 0, 0, 0 }, -1 },
46 static GLboolean Anim
= GL_FALSE
;
47 static GLfloat TexRot
= 0.0;
48 static GLfloat xRot
= 0.0f
, yRot
= 0.0f
, zRot
= 0.0f
;
64 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
67 glRotatef(xRot
, 1.0f
, 0.0f
, 0.0f
);
68 glRotatef(yRot
, 0.0f
, 1.0f
, 0.0f
);
69 glRotatef(zRot
, 0.0f
, 0.0f
, 1.0f
);
71 glMatrixMode(GL_TEXTURE
);
73 glRotatef(TexRot
, 0.0f
, 1.0f
, 0.0f
);
74 glMatrixMode(GL_MODELVIEW
);
76 glutSolidSphere(2.0, 20, 10);
85 Reshape(int width
, int height
)
87 glViewport(0, 0, width
, height
);
88 glMatrixMode(GL_PROJECTION
);
90 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
91 glMatrixMode(GL_MODELVIEW
);
93 glTranslatef(0.0f
, 0.0f
, -15.0f
);
100 glDeleteShader(fragShader
);
101 glDeleteShader(vertShader
);
102 glDeleteProgram(program
);
103 glutDestroyWindow(win
);
108 Key(unsigned char key
, int x
, int y
)
110 const GLfloat step
= 2.0;
138 SpecialKey(int key
, int x
, int y
)
140 const GLfloat step
= 2.0;
167 if (!ShadersSupported())
170 vertShader
= CompileShaderFile(GL_VERTEX_SHADER
, VertProgFile
);
171 fragShader
= CompileShaderFile(GL_FRAGMENT_SHADER
, FragProgFile
);
172 program
= LinkShaders(vertShader
, fragShader
);
174 glUseProgram(program
);
176 SetUniformValues(program
, Uniforms
);
177 PrintUniforms(Uniforms
);
179 assert(glGetError() == 0);
181 glClearColor(0.4f
, 0.4f
, 0.8f
, 0.0f
);
183 glEnable(GL_DEPTH_TEST
);
190 ParseOptions(int argc
, char *argv
[])
193 for (i
= 1; i
< argc
; i
++) {
194 if (strcmp(argv
[i
], "-fs") == 0) {
195 FragProgFile
= argv
[i
+1];
197 else if (strcmp(argv
[i
], "-vs") == 0) {
198 VertProgFile
= argv
[i
+1];
205 main(int argc
, char *argv
[])
207 glutInit(&argc
, argv
);
208 glutInitWindowSize(400, 400);
209 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
210 win
= glutCreateWindow(argv
[0]);
212 glutReshapeFunc(Reshape
);
213 glutKeyboardFunc(Key
);
214 glutSpecialFunc(SpecialKey
);
215 glutDisplayFunc(Redisplay
);
216 ParseOptions(argc
, argv
);