Merge commit 'origin/gallium-0.1' into gallium-0.2
[mesa.git] / progs / glsl / vert-tex.c
1 /**
2 * Vertex shader texture sampling test.
3 * Brian Paul
4 * 2 Dec 2008
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 const char *VertShaderText =
20 "uniform sampler2D tex1; \n"
21 "void main() \n"
22 "{ \n"
23 " vec4 pos = gl_Vertex; \n"
24 " pos.z = texture2D(tex1, gl_MultiTexCoord0.xy).x - 0.5; \n"
25 " gl_Position = gl_ModelViewProjectionMatrix * pos; \n"
26 " gl_FrontColor = pos; \n"
27 "} \n";
28
29 static const char *FragShaderText =
30 "void main() \n"
31 "{ \n"
32 " gl_FragColor = gl_Color; \n"
33 "} \n";
34
35
36 static GLuint fragShader;
37 static GLuint vertShader;
38 static GLuint program;
39
40 static GLint win = 0;
41 static GLboolean Anim = GL_TRUE;
42 static GLboolean WireFrame = GL_TRUE;
43 static GLfloat xRot = -70.0f, yRot = 0.0f, zRot = 0.0f;
44
45
46 /* value[0] = tex unit */
47 static struct uniform_info Uniforms[] = {
48 { "tex1", 1, GL_INT, { 0, 0, 0, 0 }, -1 },
49 END_OF_UNIFORMS
50 };
51
52
53
54 static void
55 Idle(void)
56 {
57 zRot = 90 + glutGet(GLUT_ELAPSED_TIME) * 0.05;
58 glutPostRedisplay();
59 }
60
61
62 static void
63 DrawMesh(void)
64 {
65 GLfloat xmin = -2.0, xmax = 2.0;
66 GLfloat ymin = -2.0, ymax = 2.0;
67 GLuint xdivs = 20, ydivs = 20;
68 GLfloat dx = (xmax - xmin) / xdivs;
69 GLfloat dy = (ymax - ymin) / ydivs;
70 GLfloat ds = 1.0 / xdivs, dt = 1.0 / ydivs;
71 GLfloat x, y, s, t;
72 GLuint i, j;
73
74 y = ymin;
75 t = 0.0;
76 for (i = 0; i < ydivs; i++) {
77 x = xmin;
78 s = 0.0;
79 glBegin(GL_QUAD_STRIP);
80 for (j = 0; j < xdivs; j++) {
81 glTexCoord2f(s, t);
82 glVertex2f(x, y);
83 glTexCoord2f(s, t + dt);
84 glVertex2f(x, y + dy);
85 x += dx;
86 s += ds;
87 }
88 glEnd();
89 y += dy;
90 t += dt;
91 }
92 }
93
94
95 static void
96 Redisplay(void)
97 {
98 if (WireFrame)
99 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
100 else
101 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
102
103 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
104
105 glPushMatrix();
106 glRotatef(xRot, 1.0f, 0.0f, 0.0f);
107 glRotatef(yRot, 0.0f, 1.0f, 0.0f);
108 glRotatef(zRot, 0.0f, 0.0f, 1.0f);
109
110 glPushMatrix();
111 DrawMesh();
112 glPopMatrix();
113
114 glPopMatrix();
115
116 glutSwapBuffers();
117 }
118
119
120 static void
121 Reshape(int width, int height)
122 {
123 glViewport(0, 0, width, height);
124 glMatrixMode(GL_PROJECTION);
125 glLoadIdentity();
126 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
127 glMatrixMode(GL_MODELVIEW);
128 glLoadIdentity();
129 glTranslatef(0.0f, 0.0f, -15.0f);
130 }
131
132
133 static void
134 CleanUp(void)
135 {
136 glDeleteShader_func(fragShader);
137 glDeleteShader_func(vertShader);
138 glDeleteProgram_func(program);
139 glutDestroyWindow(win);
140 }
141
142
143 static void
144 Key(unsigned char key, int x, int y)
145 {
146 const GLfloat step = 2.0;
147 (void) x;
148 (void) y;
149
150 switch(key) {
151 case 'a':
152 Anim = !Anim;
153 if (Anim)
154 glutIdleFunc(Idle);
155 else
156 glutIdleFunc(NULL);
157 break;
158 case 'w':
159 WireFrame = !WireFrame;
160 break;
161 case 'z':
162 zRot += step;
163 break;
164 case 'Z':
165 zRot -= step;
166 break;
167 case 27:
168 CleanUp();
169 exit(0);
170 break;
171 }
172 glutPostRedisplay();
173 }
174
175
176 static void
177 SpecialKey(int key, int x, int y)
178 {
179 const GLfloat step = 2.0;
180
181 (void) x;
182 (void) y;
183
184 switch(key) {
185 case GLUT_KEY_UP:
186 xRot += step;
187 break;
188 case GLUT_KEY_DOWN:
189 xRot -= step;
190 break;
191 case GLUT_KEY_LEFT:
192 yRot -= step;
193 break;
194 case GLUT_KEY_RIGHT:
195 yRot += step;
196 break;
197 }
198 glutPostRedisplay();
199 }
200
201
202 static void
203 MakeTexture(void)
204 {
205 const GLuint texWidth = 64, texHeight = 64;
206 GLfloat texImage[64][64];
207 GLuint i, j;
208
209 /* texture is basically z = f(x, y) */
210 for (i = 0; i < texHeight; i++) {
211 GLfloat y = 2.0 * (i / (float) (texHeight - 1)) - 1.0;
212 for (j = 0; j < texWidth; j++) {
213 GLfloat x = 2.0 * (j / (float) (texWidth - 1)) - 1.0;
214 GLfloat z = 0.5 + 0.5 * (sin(4.0 * x) * sin(4.0 * y));
215 texImage[i][j] = z;
216 }
217 }
218
219 glActiveTexture(GL_TEXTURE0);
220 glBindTexture(GL_TEXTURE_2D, 42);
221 glTexImage2D(GL_TEXTURE_2D, 0, GL_INTENSITY, texWidth, texHeight, 0,
222 GL_LUMINANCE, GL_FLOAT, texImage);
223 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
224 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
225 }
226
227
228 static void
229 Init(void)
230 {
231 GLint m;
232
233 glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS_ARB, &m);
234 if (m < 1) {
235 printf("Error: no vertex shader texture units supported.\n");
236 exit(1);
237 }
238
239 if (!ShadersSupported())
240 exit(1);
241
242 GetExtensionFuncs();
243
244 vertShader = CompileShaderText(GL_VERTEX_SHADER, VertShaderText);
245 fragShader = CompileShaderText(GL_FRAGMENT_SHADER, FragShaderText);
246 program = LinkShaders(vertShader, fragShader);
247
248 glUseProgram_func(program);
249
250 assert(glGetError() == 0);
251
252 MakeTexture();
253
254 glClearColor(0.4f, 0.4f, 0.8f, 0.0f);
255
256 glEnable(GL_DEPTH_TEST);
257
258 glColor3f(1, 1, 1);
259 }
260
261
262 int
263 main(int argc, char *argv[])
264 {
265 glutInit(&argc, argv);
266 glutInitWindowSize(500, 500);
267 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
268 win = glutCreateWindow(argv[0]);
269 glutReshapeFunc(Reshape);
270 glutKeyboardFunc(Key);
271 glutSpecialFunc(SpecialKey);
272 glutDisplayFunc(Redisplay);
273 Init();
274 if (Anim)
275 glutIdleFunc(Idle);
276 glutMainLoop();
277 return 0;
278 }
279