6e1617a7ebd87bcbc2bfe8d18c6f7ea952e0aff5
[mesa.git] / progs / glsl / vsraytrace.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 = 256, WinHeight = 256;
34 static int WinWidth = 50, WinHeight = 50;
35 static int mouseGrabbed = 0;
36
37 static const char* vsSource =
38 "const float INF = 9999.9; \n"
39 "const float EPSILON = 0.00001; \n"
40 "const vec3 lightPos = vec3(0.0, 8.0, 1.0); \n"
41 "const vec4 backgroundColor = vec4(0.2,0.3,0.4,1); \n"
42 " \n"
43 "uniform mat3 rot; \n"
44 " \n"
45 "struct Ray \n"
46 "{ \n"
47 "vec3 orig; \n"
48 "vec3 dir; \n"
49 "}; \n"
50 " \n"
51 "struct Sphere \n"
52 "{ \n"
53 " vec3 c; \n"
54 " float r; \n"
55 "}; \n"
56 " \n"
57 "struct Isec \n"
58 "{ \n"
59 " float t; \n"
60 " int idx; \n"
61 " vec3 hit; \n"
62 " vec3 n; \n"
63 "}; \n"
64 " \n"
65 "const Sphere spheres0 = Sphere( vec3(0.0,0.0,-1.0), 0.5 ); \n"
66 "const Sphere spheres1 = Sphere( vec3(-3.0,0.0,-1.0), 1.5 ); \n"
67 "const Sphere spheres2 = Sphere( vec3(0.0,3.0,-1.0), 0.5 ); \n"
68 "const Sphere spheres3 = Sphere( vec3(2.0,0.0,-1.0), 1.0 ); \n"
69 " \n"
70 "// Mesa intel gen4 generates \"unsupported IR in fragment shader 13\" for\n"
71 "// sqrt, let's work around. \n"
72 "float \n"
73 "sqrt_hack(float f2) \n"
74 "{ \n"
75 " vec3 v = vec3(f2,0.0,0.0); \n"
76 " return length(v); \n"
77 "} \n"
78 " \n"
79 "void \n"
80 "intersect(const in Ray ray, \n"
81 " const in Sphere sph, \n"
82 " const in int idx, \n"
83 " inout Isec isec) \n"
84 "{ \n"
85 " // Project both o and the sphere to the plane perpendicular to d \n"
86 " // and containing c. Let x be the point where the ray intersects \n"
87 " // the plane. If |x-c| < r, the ray intersects the sphere. \n"
88 " vec3 o = ray.orig; \n"
89 " vec3 d = ray.dir; \n"
90 " vec3 n = -d; \n"
91 " vec3 c = sph.c; \n"
92 " float r = sph.r; \n"
93 " float t = dot(c-o,n)/dot(n,d); \n"
94 " vec3 x = o+d*t; \n"
95 " float e = length(x-c); \n"
96 " if(e > r) \n"
97 " { \n"
98 " // no intersection \n"
99 " return; \n"
100 " } \n"
101 " \n"
102 " // Apply Pythagorean theorem on the (intersection,x,c) triangle \n"
103 " // to get the distance between c and the intersection. \n"
104 "#define BUGGY_INTEL_GEN4_GLSL 1 \n"
105 "#ifndef BUGGY_INTEL_GEN4_GLSL \n"
106 " float f = sqrt(r*r - e*e); \n"
107 "#else \n"
108 " float f = sqrt_hack(r*r - e*e); \n"
109 "#endif \n"
110 " float dist = t - f; \n"
111 " if(dist < 0.0) \n"
112 " { \n"
113 " // inside the sphere \n"
114 " return; \n"
115 " } \n"
116 " \n"
117 " if(dist < EPSILON) \n"
118 " return; \n"
119 " \n"
120 " if(dist > isec.t) \n"
121 " return; \n"
122 " \n"
123 " isec.t = dist; \n"
124 " isec.idx = idx; \n"
125 " \n"
126 " isec.hit = ray.orig + ray.dir * isec.t; \n"
127 " isec.n = (isec.hit - c) / r; \n"
128 "} \n"
129 " \n"
130 "Isec \n"
131 "intersect(const in Ray ray, \n"
132 " const in float max_t /*= INF*/) \n"
133 "{ \n"
134 " Isec nearest; \n"
135 " nearest.t = max_t; \n"
136 " nearest.idx = -1; \n"
137 " \n"
138 " intersect(ray, spheres0, 0, nearest); \n"
139 " intersect(ray, spheres1, 1, nearest); \n"
140 " intersect(ray, spheres2, 2, nearest); \n"
141 " intersect(ray, spheres3, 3, nearest); \n"
142 " \n"
143 " return nearest; \n"
144 "} \n"
145 " \n"
146 "vec4 \n"
147 "idx2color(const in int idx) \n"
148 "{ \n"
149 " vec4 diff; \n"
150 " if(idx == 0) \n"
151 " diff = vec4(1.0, 0.0, 0.0, 0.0); \n"
152 " else if(idx == 1) \n"
153 " diff = vec4(0.0, 1.0, 0.0, 0.0); \n"
154 " else if(idx == 2) \n"
155 " diff = vec4(0.0, 0.0, 1.0, 0.0); \n"
156 " else if(idx == 3) \n"
157 " diff = vec4(1.0, 1.0, 0.0, 0.0); \n"
158 " return diff; \n"
159 "} \n"
160 " \n"
161 "vec4 \n"
162 "trace0(const in Ray ray) \n"
163 "{ \n"
164 " Isec isec = intersect(ray, INF); \n"
165 " \n"
166 " if(isec.idx == -1) \n"
167 " { \n"
168 " return backgroundColor; \n"
169 " } \n"
170 " \n"
171 " vec4 diff = idx2color(isec.idx); \n"
172 " \n"
173 " vec3 N = isec.n; \n"
174 " vec3 L = normalize(lightPos-isec.hit); \n"
175 " vec3 camera_dir = normalize(ray.orig - isec.hit); \n"
176 " return dot(N,L)*diff + pow( \n"
177 " clamp(dot(reflect(-L,N),camera_dir),0.0,1.0),16.0); \n"
178 "} \n"
179 " \n"
180 "vec4 \n"
181 "trace1(const in Ray ray) \n"
182 "{ \n"
183 " Isec isec = intersect(ray, INF); \n"
184 " \n"
185 " if(isec.idx == -1) \n"
186 " { \n"
187 " return backgroundColor; \n"
188 " } \n"
189 " \n"
190 " Ray reflRay = Ray(isec.hit, reflect(ray.dir, isec.n)); \n"
191 " \n"
192 " vec4 reflCol = trace0(reflRay); \n"
193 " \n"
194 " vec4 diff = idx2color(isec.idx) + reflCol; \n"
195 " \n"
196 " vec3 N = isec.n; \n"
197 " vec3 L = normalize(lightPos-isec.hit); \n"
198 " vec3 camera_dir = normalize(ray.orig - isec.hit); \n"
199 " return dot(N,L)*diff + pow( \n"
200 " clamp(dot(reflect(-L,N),camera_dir),0.0,1.0),16.0); \n"
201 "} \n"
202 " \n"
203 "void main() \n"
204 "{ \n"
205 " const vec3 cameraPos = vec3(0,0,3); \n"
206 " const vec3 rayDir = normalize(vec3(gl_Vertex.x, gl_Vertex.y, -1.0) * rot);\n"
207 " Ray ray = Ray(cameraPos, rayDir); \n"
208 " gl_Position = gl_Vertex; \n"
209 " gl_FrontColor = trace1(ray); \n"
210 "} \n";
211
212 static GLuint vertShader;
213 static GLuint program;
214
215
216 static void
217 Draw(void)
218 {
219 const float w = 0.5F * WinWidth;
220 const float h = 0.5F * WinHeight;
221 int x,y;
222
223 float rot[9] = {1,0,0, 0,1,0, 0,0,1};
224 GLint location = glGetUniformLocation(program, "rot");
225 //printf("loc=%d\n", location);
226
227 glUseProgram(program);
228 glUniformMatrix3fv(location, 1, 0, rot);
229 glBegin(GL_POINTS);
230 for(y = 0; y < WinHeight; y++)
231 {
232 for(x = 0; x < WinWidth; x++)
233 {
234 const float posx = x / w - 1.0F;
235 const float posy = y / h - 1.0F;
236 glVertex2f(posx, posy);
237 }
238 }
239 glEnd();
240 glUseProgram(0);
241
242 glutSwapBuffers();
243
244 static int frames = 0;
245 static int t0 = 0;
246 static int t1 = 0;
247 frames++;
248 t1 = glutGet(GLUT_ELAPSED_TIME);
249 float dt = (float)(t1-t0)/1000.0F;
250 if(dt >= 5.0F)
251 {
252 float fps = (float)frames / dt;
253 printf("%f FPS (%d frames in %f seconds)\n", fps, frames, dt);
254 frames = 0;
255 t0 = t1;
256 }
257 }
258
259
260 static void
261 Reshape(int width, int height)
262 {
263 WinWidth = width;
264 WinHeight = height;
265 glViewport(0, 0, width, height);
266 glMatrixMode(GL_PROJECTION);
267 glLoadIdentity();
268 glMatrixMode(GL_MODELVIEW);
269 glLoadIdentity();
270 }
271
272
273 static void
274 Key(unsigned char key, int x, int y)
275 {
276 if(key == 27)
277 {
278 glutDestroyWindow(Win);
279 exit(0);
280 }
281 glutPostRedisplay();
282 }
283
284
285 static
286 void
287 drag(int x, int y)
288 {
289 if(mouseGrabbed)
290 {
291 printf("%4d %4d\n", x, y);
292 }
293 }
294
295
296 static
297 void
298 mouse(int button, int state, int x, int y)
299 {
300 if(state == GLUT_DOWN)
301 {
302 mouseGrabbed = !mouseGrabbed;
303 }
304 }
305
306
307 static void
308 Init(void)
309 {
310 glDisable(GL_DEPTH_TEST);
311
312 if(!ShadersSupported())
313 {
314 fprintf(stderr, "Shaders are not supported!\n");
315 exit(-1);
316 }
317
318 vertShader = CompileShaderText(GL_VERTEX_SHADER, vsSource);
319 program = LinkShaders(vertShader, 0);
320 glUseProgram(0);
321
322 if(glGetError() != 0)
323 {
324 fprintf(stderr, "Shaders were not loaded!\n");
325 exit(-1);
326 }
327
328 if(!glIsShader(vertShader))
329 {
330 fprintf(stderr, "Vertex shader failed!\n");
331 exit(-1);
332 }
333
334 if(!glIsProgram(program))
335 {
336 fprintf(stderr, "Shader program failed!\n");
337 exit(-1);
338 }
339
340 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
341 }
342
343
344 int
345 main(int argc, char *argv[])
346 {
347 // setenv("LIBGL_ALWAYS_SOFTWARE", "1", 1);
348 glutInit(&argc, argv);
349 glutInitWindowSize(WinWidth, WinHeight);
350 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
351 Win = glutCreateWindow(argv[0]);
352 glewInit();
353 glutReshapeFunc(Reshape);
354 glutKeyboardFunc(Key);
355 glutDisplayFunc(Draw);
356 glutIdleFunc(Draw);
357 glutMouseFunc(mouse);
358 glutMotionFunc(drag);
359 Init();
360 glutMainLoop();
361 return 0;
362 }
363