4be266622b58afbf60f03848e39147ed4a583e5f
[mesa.git] / progs / glsl / brick.c
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 */
6
7 #include <assert.h>
8 #include <string.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <math.h>
12 #include <GL/gl.h>
13 #include <GL/glut.h>
14 #include <GL/glext.h>
15 #include "extfuncs.h"
16 #include "shaderutil.h"
17
18
19 static char *FragProgFile = "CH06-brick.frag.txt";
20 static char *VertProgFile = "CH06-brick.vert.txt";
21
22 /* program/shader objects */
23 static GLuint fragShader;
24 static GLuint vertShader;
25 static GLuint program;
26
27 static struct uniform_info Uniforms[] = {
28 /* vert */
29 { "LightPosition", 3, GL_FLOAT, { 0.1, 0.1, 9.0, 0}, -1 },
30 /* frag */
31 { "BrickColor", 3, GL_FLOAT, { 0.8, 0.2, 0.2, 0 }, -1 },
32 { "MortarColor", 3, GL_FLOAT, { 0.6, 0.6, 0.6, 0 }, -1 },
33 { "BrickSize", 2, GL_FLOAT, { 1.0, 0.3, 0, 0 }, -1 },
34 { "BrickPct", 2, GL_FLOAT, { 0.9, 0.8, 0, 0 }, -1 },
35 END_OF_UNIFORMS
36 };
37
38 static GLint win = 0;
39
40
41 static GLfloat xRot = 0.0f, yRot = 0.0f, zRot = 0.0f;
42
43
44
45
46 static void
47 Redisplay(void)
48 {
49 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
50
51 glPushMatrix();
52 glRotatef(xRot, 1.0f, 0.0f, 0.0f);
53 glRotatef(yRot, 0.0f, 1.0f, 0.0f);
54 glRotatef(zRot, 0.0f, 0.0f, 1.0f);
55
56 glBegin(GL_POLYGON);
57 glTexCoord2f(0, 0); glVertex2f(-2, -2);
58 glTexCoord2f(1, 0); glVertex2f( 2, -2);
59 glTexCoord2f(1, 1); glVertex2f( 2, 2);
60 glTexCoord2f(0, 1); glVertex2f(-2, 2);
61 glEnd();
62
63 glPopMatrix();
64
65 glutSwapBuffers();
66 }
67
68
69 static void
70 Reshape(int width, int height)
71 {
72 glViewport(0, 0, width, height);
73 glMatrixMode(GL_PROJECTION);
74 glLoadIdentity();
75 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
76 glMatrixMode(GL_MODELVIEW);
77 glLoadIdentity();
78 glTranslatef(0.0f, 0.0f, -15.0f);
79 }
80
81
82 static void
83 CleanUp(void)
84 {
85 glDeleteShader_func(fragShader);
86 glDeleteShader_func(vertShader);
87 glDeleteProgram_func(program);
88 glutDestroyWindow(win);
89 }
90
91
92 static void
93 Key(unsigned char key, int x, int y)
94 {
95 (void) x;
96 (void) y;
97
98 switch(key) {
99 case 'z':
100 zRot -= 1.0;
101 break;
102 case 'Z':
103 zRot += 1.0;
104 break;
105 case 27:
106 CleanUp();
107 exit(0);
108 break;
109 }
110 glutPostRedisplay();
111 }
112
113
114 static void
115 SpecialKey(int key, int x, int y)
116 {
117 const GLfloat step = 3.0f;
118
119 (void) x;
120 (void) y;
121
122 switch(key) {
123 case GLUT_KEY_UP:
124 xRot -= step;
125 break;
126 case GLUT_KEY_DOWN:
127 xRot += step;
128 break;
129 case GLUT_KEY_LEFT:
130 yRot -= step;
131 break;
132 case GLUT_KEY_RIGHT:
133 yRot += step;
134 break;
135 }
136 glutPostRedisplay();
137 }
138
139
140
141 static void
142 Init(void)
143 {
144 if (!ShadersSupported())
145 exit(1);
146
147 GetExtensionFuncs();
148
149 vertShader = CompileShaderFile(GL_VERTEX_SHADER, VertProgFile);
150 fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, FragProgFile);
151 program = LinkShaders(vertShader, fragShader);
152
153 glUseProgram_func(program);
154
155 InitUniforms(program, Uniforms);
156
157 assert(glGetError() == 0);
158
159 glClearColor(0.4f, 0.4f, 0.8f, 0.0f);
160
161 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
162
163 assert(glIsProgram_func(program));
164 assert(glIsShader_func(fragShader));
165 assert(glIsShader_func(vertShader));
166
167 glColor3f(1, 0, 0);
168 }
169
170
171 static void
172 ParseOptions(int argc, char *argv[])
173 {
174 int i;
175 for (i = 1; i < argc; i++) {
176 if (strcmp(argv[i], "-fs") == 0) {
177 FragProgFile = argv[i+1];
178 }
179 else if (strcmp(argv[i], "-vs") == 0) {
180 VertProgFile = argv[i+1];
181 }
182 }
183 }
184
185
186 int
187 main(int argc, char *argv[])
188 {
189 glutInit(&argc, argv);
190 glutInitWindowPosition( 0, 0);
191 glutInitWindowSize(400, 400);
192 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
193 win = glutCreateWindow(argv[0]);
194 glutReshapeFunc(Reshape);
195 glutKeyboardFunc(Key);
196 glutSpecialFunc(SpecialKey);
197 glutDisplayFunc(Redisplay);
198 ParseOptions(argc, argv);
199 Init();
200 glutMainLoop();
201 return 0;
202 }
203