WIP - port to Mali EGL
[mesa-demos/mali.git] / src / glsl / brick.c
blob3021856f94dd8636163dbf10def5aec938f88a3b
1 /**
2 * "Brick" shader demo. Uses the example shaders from chapter 6 of
3 * the OpenGL Shading Language "orange" book.
4 * 10 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 = "CH06-brick.frag";
18 static char *VertProgFile = "CH06-brick.vert";
20 /* program/shader objects */
21 static GLuint fragShader;
22 static GLuint vertShader;
23 static GLuint program;
25 static struct uniform_info Uniforms[] = {
26 /* vert */
27 { "LightPosition", 1, GL_FLOAT_VEC3, { 0.1, 0.1, 9.0, 0}, -1 },
28 /* frag */
29 { "BrickColor", 1, GL_FLOAT_VEC3, { 0.8, 0.2, 0.2, 0 }, -1 },
30 { "MortarColor", 1, GL_FLOAT_VEC3, { 0.6, 0.6, 0.6, 0 }, -1 },
31 { "BrickSize", 1, GL_FLOAT_VEC2, { 1.0, 0.3, 0, 0 }, -1 },
32 { "BrickPct", 1, GL_FLOAT_VEC2, { 0.9, 0.8, 0, 0 }, -1 },
33 END_OF_UNIFORMS
36 static GLint win = 0;
39 static GLfloat xRot = 0.0f, yRot = 0.0f, zRot = 0.0f;
44 static void
45 Redisplay(void)
47 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
49 glPushMatrix();
50 glRotatef(xRot, 1.0f, 0.0f, 0.0f);
51 glRotatef(yRot, 0.0f, 1.0f, 0.0f);
52 glRotatef(zRot, 0.0f, 0.0f, 1.0f);
54 glBegin(GL_POLYGON);
55 glTexCoord2f(0, 0); glVertex2f(-2, -2);
56 glTexCoord2f(1, 0); glVertex2f( 2, -2);
57 glTexCoord2f(1, 1); glVertex2f( 2, 2);
58 glTexCoord2f(0, 1); glVertex2f(-2, 2);
59 glEnd();
61 glPopMatrix();
63 glutSwapBuffers();
67 static void
68 Reshape(int width, int height)
70 glViewport(0, 0, width, height);
71 glMatrixMode(GL_PROJECTION);
72 glLoadIdentity();
73 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
74 glMatrixMode(GL_MODELVIEW);
75 glLoadIdentity();
76 glTranslatef(0.0f, 0.0f, -15.0f);
80 static void
81 CleanUp(void)
83 DeleteShader(fragShader);
84 DeleteShader(vertShader);
85 DeleteProgram(program);
86 glutDestroyWindow(win);
90 static void
91 Key(unsigned char key, int x, int y)
93 (void) x;
94 (void) y;
96 switch(key) {
97 case 'z':
98 zRot -= 1.0;
99 break;
100 case 'Z':
101 zRot += 1.0;
102 break;
103 case 27:
104 CleanUp();
105 exit(0);
106 break;
108 glutPostRedisplay();
112 static void
113 SpecialKey(int key, int x, int y)
115 const GLfloat step = 3.0f;
117 (void) x;
118 (void) y;
120 switch(key) {
121 case GLUT_KEY_UP:
122 xRot -= step;
123 break;
124 case GLUT_KEY_DOWN:
125 xRot += step;
126 break;
127 case GLUT_KEY_LEFT:
128 yRot -= step;
129 break;
130 case GLUT_KEY_RIGHT:
131 yRot += step;
132 break;
134 glutPostRedisplay();
139 static void
140 Init(void)
142 if (!ShadersSupported())
143 exit(1);
145 vertShader = CompileShaderFile(GL_VERTEX_SHADER, VertProgFile);
146 fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, FragProgFile);
147 program = LinkShaders(vertShader, fragShader);
149 UseProgram(program);
151 SetUniformValues(program, Uniforms);
152 PrintUniforms(Uniforms);
154 assert(glGetError() == 0);
156 glClearColor(0.4f, 0.4f, 0.8f, 0.0f);
158 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
160 if (GLEW_VERSION_2_0) {
161 assert(glIsProgram(program));
162 assert(glIsShader(fragShader));
163 assert(glIsShader(vertShader));
166 glColor3f(1, 0, 0);
170 static void
171 ParseOptions(int argc, char *argv[])
173 int i;
174 for (i = 1; i < argc; i++) {
175 if (strcmp(argv[i], "-fs") == 0) {
176 FragProgFile = argv[i+1];
178 else if (strcmp(argv[i], "-vs") == 0) {
179 VertProgFile = argv[i+1];
186 main(int argc, char *argv[])
188 glutInit(&argc, argv);
189 glutInitWindowSize(400, 400);
190 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
191 win = glutCreateWindow(argv[0]);
192 glewInit();
193 glutReshapeFunc(Reshape);
194 glutKeyboardFunc(Key);
195 glutSpecialFunc(SpecialKey);
196 glutDisplayFunc(Redisplay);
197 ParseOptions(argc, argv);
198 Init();
199 glutMainLoop();
200 return 0;