2 * "Toy Ball" shader demo. Uses the example shaders from
3 * chapter 11 of the OpenGL Shading Language "orange" book.
17 #include "shaderutil.h"
20 static char *FragProgFile
= "CH11-toyball.frag";
21 static char *VertProgFile
= "CH11-toyball.vert";
23 /* program/shader objects */
24 static GLuint fragShader
;
25 static GLuint vertShader
;
26 static GLuint program
;
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 },
49 static GLboolean Anim
= GL_FALSE
;
50 static GLfloat TexRot
= 0.0;
51 static GLfloat xRot
= 0.0f
, yRot
= 0.0f
, zRot
= 0.0f
;
67 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
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
);
74 glMatrixMode(GL_TEXTURE
);
76 glRotatef(TexRot
, 0.0f
, 1.0f
, 0.0f
);
77 glMatrixMode(GL_MODELVIEW
);
79 glutSolidSphere(2.0, 20, 10);
88 Reshape(int width
, int height
)
90 glViewport(0, 0, width
, height
);
91 glMatrixMode(GL_PROJECTION
);
93 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
94 glMatrixMode(GL_MODELVIEW
);
96 glTranslatef(0.0f
, 0.0f
, -15.0f
);
103 glDeleteShader_func(fragShader
);
104 glDeleteShader_func(vertShader
);
105 glDeleteProgram_func(program
);
106 glutDestroyWindow(win
);
111 Key(unsigned char key
, int x
, int y
)
113 const GLfloat step
= 2.0;
141 SpecialKey(int key
, int x
, int y
)
143 const GLfloat step
= 2.0;
170 if (!ShadersSupported())
175 vertShader
= CompileShaderFile(GL_VERTEX_SHADER
, VertProgFile
);
176 fragShader
= CompileShaderFile(GL_FRAGMENT_SHADER
, FragProgFile
);
177 program
= LinkShaders(vertShader
, fragShader
);
179 glUseProgram_func(program
);
181 InitUniforms(program
, Uniforms
);
183 assert(glGetError() == 0);
185 glClearColor(0.4f
, 0.4f
, 0.8f
, 0.0f
);
187 glEnable(GL_DEPTH_TEST
);
194 ParseOptions(int argc
, char *argv
[])
197 for (i
= 1; i
< argc
; i
++) {
198 if (strcmp(argv
[i
], "-fs") == 0) {
199 FragProgFile
= argv
[i
+1];
201 else if (strcmp(argv
[i
], "-vs") == 0) {
202 VertProgFile
= argv
[i
+1];
209 main(int argc
, char *argv
[])
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]);
217 glutReshapeFunc(Reshape
);
218 glutKeyboardFunc(Key
);
219 glutSpecialFunc(SpecialKey
);
220 glutDisplayFunc(Redisplay
);
221 ParseOptions(argc
, argv
);