blob: 2b644acb6d42006bf58a5d2d2b14de250ce31d45 [file] [log] [blame]
Brianf44ba112007-01-16 14:55:43 -07001/**
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>
Keith Whitwellb799af92009-06-29 14:13:58 +010012#include <GL/glew.h>
Brianf44ba112007-01-16 14:55:43 -070013#include <GL/gl.h>
14#include <GL/glut.h>
15#include <GL/glext.h>
16#include "extfuncs.h"
Brian2dca3372008-04-09 22:28:23 -060017#include "shaderutil.h"
Brianf44ba112007-01-16 14:55:43 -070018
19
Brian Paulc0dd9122008-08-16 09:36:46 -060020static char *FragProgFile = "CH11-toyball.frag";
21static char *VertProgFile = "CH11-toyball.vert";
Brianf44ba112007-01-16 14:55:43 -070022
23/* program/shader objects */
24static GLuint fragShader;
25static GLuint vertShader;
26static GLuint program;
27
28
Brianf44ba112007-01-16 14:55:43 -070029static struct uniform_info Uniforms[] = {
Brian2dca3372008-04-09 22:28:23 -060030 { "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
Brianf44ba112007-01-16 14:55:43 -070046};
47
48static GLint win = 0;
Brianf68067e2007-02-25 17:24:40 -070049static GLboolean Anim = GL_FALSE;
50static GLfloat TexRot = 0.0;
Brianf44ba112007-01-16 14:55:43 -070051static GLfloat xRot = 0.0f, yRot = 0.0f, zRot = 0.0f;
52
53
54static void
Brianf68067e2007-02-25 17:24:40 -070055Idle(void)
56{
57 TexRot += 2.0;
58 if (TexRot > 360.0)
59 TexRot -= 360.0;
60 glutPostRedisplay();
61}
62
63
64static void
Brianf44ba112007-01-16 14:55:43 -070065Redisplay(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
Brianf68067e2007-02-25 17:24:40 -070074 glMatrixMode(GL_TEXTURE);
75 glLoadIdentity();
76 glRotatef(TexRot, 0.0f, 1.0f, 0.0f);
77 glMatrixMode(GL_MODELVIEW);
78
Brianf44ba112007-01-16 14:55:43 -070079 glutSolidSphere(2.0, 20, 10);
80
81 glPopMatrix();
82
Brianf44ba112007-01-16 14:55:43 -070083 glutSwapBuffers();
84}
85
86
87static void
88Reshape(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
100static void
101CleanUp(void)
102{
103 glDeleteShader_func(fragShader);
104 glDeleteShader_func(vertShader);
105 glDeleteProgram_func(program);
106 glutDestroyWindow(win);
107}
108
109
110static void
111Key(unsigned char key, int x, int y)
112{
113 const GLfloat step = 2.0;
114 (void) x;
115 (void) y;
116
117 switch(key) {
Brianf68067e2007-02-25 17:24:40 -0700118 case 'a':
119 Anim = !Anim;
120 if (Anim)
121 glutIdleFunc(Idle);
122 else
123 glutIdleFunc(NULL);
124 break;
Brianf44ba112007-01-16 14:55:43 -0700125 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
140static void
141SpecialKey(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
167static void
Brianf44ba112007-01-16 14:55:43 -0700168Init(void)
169{
Brian2dca3372008-04-09 22:28:23 -0600170 if (!ShadersSupported())
171 exit(1);
Brianf44ba112007-01-16 14:55:43 -0700172
173 GetExtensionFuncs();
174
Brian2dca3372008-04-09 22:28:23 -0600175 vertShader = CompileShaderFile(GL_VERTEX_SHADER, VertProgFile);
176 fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, FragProgFile);
177 program = LinkShaders(vertShader, fragShader);
Brianf44ba112007-01-16 14:55:43 -0700178
Brianf44ba112007-01-16 14:55:43 -0700179 glUseProgram_func(program);
180
Brian2dca3372008-04-09 22:28:23 -0600181 InitUniforms(program, Uniforms);
Brianf44ba112007-01-16 14:55:43 -0700182
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
193static void
194ParseOptions(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
208int
209main(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]);
Keith Whitwellb799af92009-06-29 14:13:58 +0100216 glewInit();
Brianf44ba112007-01-16 14:55:43 -0700217 glutReshapeFunc(Reshape);
218 glutKeyboardFunc(Key);
219 glutSpecialFunc(SpecialKey);
220 glutDisplayFunc(Redisplay);
221 ParseOptions(argc, argv);
222 Init();
223 glutMainLoop();
224 return 0;
225}
226