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