WIP - port to Mali EGL
[mesa-demos/mali.git] / src / glsl / toyball.c
blob5f279513825751c0418eb25612f059adbff774dc
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 */
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 "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 },
42 END_OF_UNIFORMS
45 static GLint win = 0;
46 static GLboolean Anim = GL_FALSE;
47 static GLfloat TexRot = 0.0;
48 static GLfloat xRot = 0.0f, yRot = 0.0f, zRot = 0.0f;
51 static void
52 Idle(void)
54 TexRot += 2.0;
55 if (TexRot > 360.0)
56 TexRot -= 360.0;
57 glutPostRedisplay();
61 static void
62 Redisplay(void)
64 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
66 glPushMatrix();
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);
72 glLoadIdentity();
73 glRotatef(TexRot, 0.0f, 1.0f, 0.0f);
74 glMatrixMode(GL_MODELVIEW);
76 glutSolidSphere(2.0, 20, 10);
78 glPopMatrix();
80 glutSwapBuffers();
84 static void
85 Reshape(int width, int height)
87 glViewport(0, 0, width, height);
88 glMatrixMode(GL_PROJECTION);
89 glLoadIdentity();
90 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
91 glMatrixMode(GL_MODELVIEW);
92 glLoadIdentity();
93 glTranslatef(0.0f, 0.0f, -15.0f);
97 static void
98 CleanUp(void)
100 glDeleteShader(fragShader);
101 glDeleteShader(vertShader);
102 glDeleteProgram(program);
103 glutDestroyWindow(win);
107 static void
108 Key(unsigned char key, int x, int y)
110 const GLfloat step = 2.0;
111 (void) x;
112 (void) y;
114 switch(key) {
115 case 'a':
116 Anim = !Anim;
117 if (Anim)
118 glutIdleFunc(Idle);
119 else
120 glutIdleFunc(NULL);
121 break;
122 case 'z':
123 zRot += step;
124 break;
125 case 'Z':
126 zRot -= step;
127 break;
128 case 27:
129 CleanUp();
130 exit(0);
131 break;
133 glutPostRedisplay();
137 static void
138 SpecialKey(int key, int x, int y)
140 const GLfloat step = 2.0;
142 (void) x;
143 (void) y;
145 switch(key) {
146 case GLUT_KEY_UP:
147 xRot += step;
148 break;
149 case GLUT_KEY_DOWN:
150 xRot -= step;
151 break;
152 case GLUT_KEY_LEFT:
153 yRot -= step;
154 break;
155 case GLUT_KEY_RIGHT:
156 yRot += step;
157 break;
159 glutPostRedisplay();
164 static void
165 Init(void)
167 if (!ShadersSupported())
168 exit(1);
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);
185 glColor3f(1, 0, 0);
189 static void
190 ParseOptions(int argc, char *argv[])
192 int i;
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]);
211 glewInit();
212 glutReshapeFunc(Reshape);
213 glutKeyboardFunc(Key);
214 glutSpecialFunc(SpecialKey);
215 glutDisplayFunc(Redisplay);
216 ParseOptions(argc, argv);
217 Init();
218 glutMainLoop();
219 return 0;