progs/glsl: silence warnings, remove debug code, adjust window size, etc
[mesa.git] / progs / glsl / fsraytrace.c
1 // -*- mode: c; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; coding: utf-8-unix -*-
2 /*
3 Copyright (c) 2010 Kristóf Ralovich
4
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 THE SOFTWARE.
22 */
23
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <GL/glew.h>
28 #include <GL/glut.h>
29 #include "shaderutil.h"
30
31
32 static int Win;
33 static int WinWidth = 512, WinHeight = 512;
34 static GLfloat Xrot = 0, Yrot = 0;
35 static int mouseGrabbed = 0;
36 static GLuint vertShader;
37 static GLuint fragShader;
38 static GLuint program;
39
40 static const char* vsSource =
41 "varying vec2 rayDir;\n"
42 "\n"
43 "void main()\n"
44 "{\n"
45 " rayDir = gl_MultiTexCoord0.xy - vec2(0.5,0.5);\n"
46 " gl_Position = gl_ProjectionMatrix * gl_Vertex;\n"
47 "}\n";
48
49 static const char* fsSource =
50 "const float INF = 9999.9; \n"
51 "const float EPSILON = 0.00001; \n"
52 "const vec3 lightPos = vec3(0.0, 8.0, 1.0); \n"
53 "const vec4 backgroundColor = vec4(0.2,0.3,0.4,1); \n"
54 " \n"
55 "varying vec2 rayDir; \n"
56 " \n"
57 "uniform mat3 rot; \n"
58 " \n"
59 "struct Ray \n"
60 "{ \n"
61 "vec3 orig; \n"
62 "vec3 dir; \n"
63 "}; \n"
64 " \n"
65 "struct Sphere \n"
66 "{ \n"
67 " vec3 c; \n"
68 " float r; \n"
69 "}; \n"
70 " \n"
71 "struct Isec \n"
72 "{ \n"
73 " float t; \n"
74 " int idx; \n"
75 " vec3 hit; \n"
76 " vec3 n; \n"
77 "}; \n"
78 " \n"
79 "const Sphere spheres0 = Sphere( vec3(0.0,0.0,-1.0), 0.5 ); \n"
80 "const Sphere spheres1 = Sphere( vec3(-3.0,0.0,-1.0), 1.5 ); \n"
81 "const Sphere spheres2 = Sphere( vec3(0.0,3.0,-1.0), 0.5 ); \n"
82 "const Sphere spheres3 = Sphere( vec3(2.0,0.0,-1.0), 1.0 ); \n"
83 " \n"
84 "// Mesa intel gen4 generates \"unsupported IR in fragment shader 13\" for\n"
85 "// sqrt, let's work around. \n"
86 "float \n"
87 "sqrt_hack(float f2) \n"
88 "{ \n"
89 " vec3 v = vec3(f2,0.0,0.0); \n"
90 " return length(v); \n"
91 "} \n"
92 " \n"
93 "void \n"
94 "intersect(const in Ray ray, \n"
95 " const in Sphere sph, \n"
96 " const in int idx, \n"
97 " inout Isec isec) \n"
98 "{ \n"
99 " // Project both o and the sphere to the plane perpendicular to d \n"
100 " // and containing c. Let x be the point where the ray intersects \n"
101 " // the plane. If |x-c| < r, the ray intersects the sphere. \n"
102 " vec3 o = ray.orig; \n"
103 " vec3 d = ray.dir; \n"
104 " vec3 n = -d; \n"
105 " vec3 c = sph.c; \n"
106 " float r = sph.r; \n"
107 " float t = dot(c-o,n)/dot(n,d); \n"
108 " vec3 x = o+d*t; \n"
109 " float e = length(x-c); \n"
110 " if(e > r) \n"
111 " { \n"
112 " // no intersection \n"
113 " return; \n"
114 " } \n"
115 " \n"
116 " // Apply Pythagorean theorem on the (intersection,x,c) triangle \n"
117 " // to get the distance between c and the intersection. \n"
118 "#ifndef BUGGY_INTEL_GEN4_GLSL \n"
119 " float f = sqrt(r*r - e*e); \n"
120 "#else \n"
121 " float f = sqrt_hack(r*r - e*e); \n"
122 "#endif \n"
123 " float dist = t - f; \n"
124 " if(dist < 0.0) \n"
125 " { \n"
126 " // inside the sphere \n"
127 " return; \n"
128 " } \n"
129 " \n"
130 " if(dist < EPSILON) \n"
131 " return; \n"
132 " \n"
133 " if(dist > isec.t) \n"
134 " return; \n"
135 " \n"
136 " isec.t = dist; \n"
137 " isec.idx = idx; \n"
138 " \n"
139 " isec.hit = ray.orig + ray.dir * isec.t; \n"
140 " isec.n = (isec.hit - c) / r; \n"
141 "} \n"
142 " \n"
143 "Isec \n"
144 "intersect(const in Ray ray, \n"
145 " const in float max_t /*= INF*/) \n"
146 "{ \n"
147 " Isec nearest; \n"
148 " nearest.t = max_t; \n"
149 " nearest.idx = -1; \n"
150 " \n"
151 " intersect(ray, spheres0, 0, nearest); \n"
152 " intersect(ray, spheres1, 1, nearest); \n"
153 " intersect(ray, spheres2, 2, nearest); \n"
154 " intersect(ray, spheres3, 3, nearest); \n"
155 " \n"
156 " return nearest; \n"
157 "} \n"
158 " \n"
159 "vec4 \n"
160 "idx2color(const in int idx) \n"
161 "{ \n"
162 " vec4 diff; \n"
163 " if(idx == 0) \n"
164 " diff = vec4(1.0, 0.0, 0.0, 0.0); \n"
165 " else if(idx == 1) \n"
166 " diff = vec4(0.0, 1.0, 0.0, 0.0); \n"
167 " else if(idx == 2) \n"
168 " diff = vec4(0.0, 0.0, 1.0, 0.0); \n"
169 " else if(idx == 3) \n"
170 " diff = vec4(1.0, 1.0, 0.0, 0.0); \n"
171 " return diff; \n"
172 "} \n"
173 " \n"
174 "vec4 \n"
175 "trace0(const in Ray ray) \n"
176 "{ \n"
177 " Isec isec = intersect(ray, INF); \n"
178 " \n"
179 " if(isec.idx == -1) \n"
180 " { \n"
181 " return backgroundColor; \n"
182 " } \n"
183 " \n"
184 " vec4 diff = idx2color(isec.idx); \n"
185 " \n"
186 " vec3 N = isec.n; \n"
187 " vec3 L = normalize(lightPos-isec.hit); \n"
188 " vec3 camera_dir = normalize(ray.orig - isec.hit); \n"
189 " return dot(N,L)*diff + pow( \n"
190 " clamp(dot(reflect(-L,N),camera_dir),0.0,1.0),16.0); \n"
191 "} \n"
192 " \n"
193 "vec4 \n"
194 "trace1(const in Ray ray) \n"
195 "{ \n"
196 " Isec isec = intersect(ray, INF); \n"
197 " \n"
198 " if(isec.idx == -1) \n"
199 " { \n"
200 " return backgroundColor; \n"
201 " } \n"
202 " \n"
203 " Ray reflRay = Ray(isec.hit, reflect(ray.dir, isec.n)); \n"
204 " \n"
205 " vec4 reflCol = trace0(reflRay); \n"
206 " \n"
207 " vec4 diff = idx2color(isec.idx) + reflCol; \n"
208 " \n"
209 " vec3 N = isec.n; \n"
210 " vec3 L = normalize(lightPos-isec.hit); \n"
211 " vec3 camera_dir = normalize(ray.orig - isec.hit); \n"
212 " return dot(N,L)*diff + pow( \n"
213 " clamp(dot(reflect(-L,N),camera_dir),0.0,1.0),16.0); \n"
214 "} \n"
215 " \n"
216 "\n"
217 "void\n"
218 "main()\n"
219 "{\n"
220 " const float z = -0.5;\n"
221 " const vec3 cameraPos = vec3(0,0,3); \n"
222 " Ray r = Ray(cameraPos, normalize(vec3(rayDir, z) * rot));\n"
223 " gl_FragColor = trace1(r);\n"
224 "}\n";
225
226
227
228 static void
229 Idle(void)
230 {
231 glutPostRedisplay();
232 }
233
234
235 static void
236 Draw(void)
237 {
238 float rot[9] = {1,0,0, 0,1,0, 0,0,1};
239 GLint location = glGetUniformLocation(program, "rot");
240 static const float m = -10.F;
241 static const float p = 10.F;
242 static const float d = -0.5F;
243
244 glUseProgram(program);
245 glUniformMatrix3fv(location, 1, 0, rot);
246
247 glBegin(GL_QUADS);
248 {
249 glTexCoord2f(0.0F, 0.0F); glVertex3f(m, m, d);
250 glTexCoord2f(1.0F, 0.0F); glVertex3f(p, m, d);
251 glTexCoord2f(1.0F, 1.0F); glVertex3f(p, p, d);
252 glTexCoord2f(0.0F, 1.0F); glVertex3f(m, p, d);
253 }
254 glEnd();
255 glUseProgram(0);
256
257 glutSwapBuffers();
258
259 {
260 static int frames = 0;
261 static int t0 = 0;
262 static int t1 = 0;
263 float dt;
264 frames++;
265 t1 = glutGet(GLUT_ELAPSED_TIME);
266 dt = (float)(t1-t0)/1000.0F;
267 if(dt >= 5.0F)
268 {
269 float fps = (float)frames / dt;
270 printf("%f FPS (%d frames in %f seconds)\n", fps, frames, dt);
271 frames = 0;
272 t0 = t1;
273 }
274 }
275 }
276
277
278 static void
279 Reshape(int width, int height)
280 {
281 WinWidth = width;
282 WinHeight = height;
283 glViewport(0, 0, width, height);
284 glMatrixMode(GL_PROJECTION);
285 glLoadIdentity();
286 glOrtho(-10, 10, -10, 10, -1, 1);
287 glMatrixMode(GL_MODELVIEW);
288 glLoadIdentity();
289 }
290
291
292 static void
293 Key(unsigned char key, int x, int y)
294 {
295 (void) x;
296 (void) y;
297 switch (key) {
298 case 27:
299 glutDestroyWindow(Win);
300 exit(0);
301 break;
302 }
303 glutPostRedisplay();
304 }
305
306
307 static
308 void
309 drag(int x, int y)
310 {
311 float scale = 1.5F;
312 if(mouseGrabbed)
313 {
314 Xrot = (float)(x - WinWidth/2) / scale;
315 Yrot = (float)(y - WinHeight/2) / scale;
316 printf("%4.2f %4.2f\n", Xrot, Yrot);
317 }
318 }
319
320
321 static
322 void
323 mouse(int button, int state, int x, int y)
324 {
325 if(state == GLUT_DOWN)
326 {
327 mouseGrabbed = !mouseGrabbed;
328 }
329 }
330
331
332 static void
333 Init(void)
334 {
335 glDisable(GL_DEPTH_TEST);
336
337 if(!ShadersSupported())
338 {
339 fprintf(stderr, "Shaders are not supported!\n");
340 exit(-1);
341 }
342
343 vertShader = CompileShaderText(GL_VERTEX_SHADER, vsSource);
344 fragShader = CompileShaderText(GL_FRAGMENT_SHADER, fsSource);
345 program = LinkShaders(vertShader, fragShader);
346 glUseProgram(0);
347
348 if(glGetError() != 0)
349 {
350 fprintf(stderr, "Shaders were not loaded!\n");
351 exit(-1);
352 }
353
354 if(!glIsShader(vertShader))
355 {
356 fprintf(stderr, "Vertex shader failed!\n");
357 exit(-1);
358 }
359
360 if(!glIsProgram(program))
361 {
362 fprintf(stderr, "Shader program failed!\n");
363 exit(-1);
364 }
365
366 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
367 }
368
369
370 int
371 main(int argc, char *argv[])
372 {
373 glutInitWindowSize(WinWidth, WinHeight);
374 glutInit(&argc, argv);
375 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
376 Win = glutCreateWindow(argv[0]);
377 glewInit();
378 glutReshapeFunc(Reshape);
379 glutKeyboardFunc(Key);
380 glutDisplayFunc(Draw);
381 glutMouseFunc(mouse);
382 glutPassiveMotionFunc(drag);
383 glutIdleFunc(Idle);
384 Init();
385 glutMainLoop();
386 return 0;
387 }
388