WIP - port to Mali EGL
[mesa-demos/mali.git] / src / glsl / fsraytrace.c
blob4cb141f4d0b2465f6069b39001913235fae5f78f
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
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:
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
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.
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <GL/glew.h>
28 #include "glut_wrap.h"
29 #include "shaderutil.h"
30 #include <math.h>
32 static int Win;
33 static int WinWidth = 512, WinHeight = 512;
34 static int mouseGrabbed = 0;
35 static GLuint vertShader;
36 static GLuint fragShader;
37 static GLuint program;
38 static float rot[9] = {1,0,0, 0,1,0, 0,0,1};
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";
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 #ifdef __APPLE__
80 "Sphere spheres0 = Sphere( vec3(0.0,0.0,-1.0), 0.5 ); \n"
81 "Sphere spheres1 = Sphere( vec3(-3.0,0.0,-1.0), 1.5 ); \n"
82 "Sphere spheres2 = Sphere( vec3(0.0,3.0,-1.0), 0.5 ); \n"
83 "Sphere spheres3 = Sphere( vec3(2.0,0.0,-1.0), 1.0 ); \n"
84 #else
85 "const Sphere spheres0 = Sphere( vec3(0.0,0.0,-1.0), 0.5 ); \n"
86 "const Sphere spheres1 = Sphere( vec3(-3.0,0.0,-1.0), 1.5 ); \n"
87 "const Sphere spheres2 = Sphere( vec3(0.0,3.0,-1.0), 0.5 ); \n"
88 "const Sphere spheres3 = Sphere( vec3(2.0,0.0,-1.0), 1.0 ); \n"
89 #endif
90 " \n"
91 "// Mesa intel gen4 generates \"unsupported IR in fragment shader 13\" for\n"
92 "// sqrt, let's work around. \n"
93 "float \n"
94 "sqrt_hack(float f2) \n"
95 "{ \n"
96 " vec3 v = vec3(f2,0.0,0.0); \n"
97 " return length(v); \n"
98 "} \n"
99 " \n"
100 "void \n"
101 "intersect(const in Ray ray, \n"
102 " const in Sphere sph, \n"
103 " const in int idx, \n"
104 " inout Isec isec) \n"
105 "{ \n"
106 " // Project both o and the sphere to the plane perpendicular to d \n"
107 " // and containing c. Let x be the point where the ray intersects \n"
108 " // the plane. If |x-c| < r, the ray intersects the sphere. \n"
109 " vec3 o = ray.orig; \n"
110 " vec3 d = ray.dir; \n"
111 " vec3 n = -d; \n"
112 " vec3 c = sph.c; \n"
113 " float r = sph.r; \n"
114 " float t = dot(c-o,n)/dot(n,d); \n"
115 " vec3 x = o+d*t; \n"
116 " float e = length(x-c); \n"
117 " if(e > r) \n"
118 " { \n"
119 " // no intersection \n"
120 " return; \n"
121 " } \n"
122 " \n"
123 " // Apply Pythagorean theorem on the (intersection,x,c) triangle \n"
124 " // to get the distance between c and the intersection. \n"
125 "#ifndef BUGGY_INTEL_GEN4_GLSL \n"
126 " float f = sqrt(r*r - e*e); \n"
127 "#else \n"
128 " float f = sqrt_hack(r*r - e*e); \n"
129 "#endif \n"
130 " float dist = t - f; \n"
131 " if(dist < 0.0) \n"
132 " { \n"
133 " // inside the sphere \n"
134 " return; \n"
135 " } \n"
136 " \n"
137 " if(dist < EPSILON) \n"
138 " return; \n"
139 " \n"
140 " if(dist > isec.t) \n"
141 " return; \n"
142 " \n"
143 " isec.t = dist; \n"
144 " isec.idx = idx; \n"
145 " \n"
146 " isec.hit = ray.orig + ray.dir * isec.t; \n"
147 " isec.n = (isec.hit - c) / r; \n"
148 "} \n"
149 " \n"
150 "Isec \n"
151 "intersect(const in Ray ray, \n"
152 " const in float max_t /*= INF*/) \n"
153 "{ \n"
154 " Isec nearest; \n"
155 " nearest.t = max_t; \n"
156 " nearest.idx = -1; \n"
157 " \n"
158 " intersect(ray, spheres0, 0, nearest); \n"
159 " intersect(ray, spheres1, 1, nearest); \n"
160 " intersect(ray, spheres2, 2, nearest); \n"
161 " intersect(ray, spheres3, 3, nearest); \n"
162 " \n"
163 " return nearest; \n"
164 "} \n"
165 " \n"
166 "vec4 \n"
167 "idx2color(const in int idx) \n"
168 "{ \n"
169 " vec4 diff; \n"
170 " if(idx == 0) \n"
171 " diff = vec4(1.0, 0.0, 0.0, 0.0); \n"
172 " else if(idx == 1) \n"
173 " diff = vec4(0.0, 1.0, 0.0, 0.0); \n"
174 " else if(idx == 2) \n"
175 " diff = vec4(0.0, 0.0, 1.0, 0.0); \n"
176 " else if(idx == 3) \n"
177 " diff = vec4(1.0, 1.0, 0.0, 0.0); \n"
178 " return diff; \n"
179 "} \n"
180 " \n"
181 "vec4 \n"
182 "trace0(const in Ray ray) \n"
183 "{ \n"
184 " Isec isec = intersect(ray, INF); \n"
185 " \n"
186 " if(isec.idx == -1) \n"
187 " { \n"
188 " return backgroundColor; \n"
189 " } \n"
190 " \n"
191 " vec4 diff = idx2color(isec.idx); \n"
192 " \n"
193 " vec3 N = isec.n; \n"
194 " vec3 L = normalize(lightPos-isec.hit); \n"
195 " vec3 camera_dir = normalize(ray.orig - isec.hit); \n"
196 " return dot(N,L)*diff + pow( \n"
197 " clamp(dot(reflect(-L,N),camera_dir),0.0,1.0),16.0); \n"
198 "} \n"
199 " \n"
200 "vec4 \n"
201 "trace1(const in Ray ray) \n"
202 "{ \n"
203 " Isec isec = intersect(ray, INF); \n"
204 " \n"
205 " if(isec.idx == -1) \n"
206 " { \n"
207 " return backgroundColor; \n"
208 " } \n"
209 " \n"
210 " Ray reflRay = Ray(isec.hit, reflect(ray.dir, isec.n)); \n"
211 " \n"
212 " vec4 reflCol = trace0(reflRay); \n"
213 " \n"
214 " vec4 diff = idx2color(isec.idx) + reflCol; \n"
215 " \n"
216 " vec3 N = isec.n; \n"
217 " vec3 L = normalize(lightPos-isec.hit); \n"
218 " vec3 camera_dir = normalize(ray.orig - isec.hit); \n"
219 " return dot(N,L)*diff + pow( \n"
220 " clamp(dot(reflect(-L,N),camera_dir),0.0,1.0),16.0); \n"
221 "} \n"
222 " \n"
223 "void main() \n"
224 "{ \n"
225 " const float z = -0.5; \n"
226 " const vec3 cameraPos = vec3(0,0,3); \n"
227 " Ray r = Ray(cameraPos, normalize(vec3(rayDir, z) * rot)); \n"
228 " gl_FragColor = trace1(r); \n"
229 "}\n";
231 static
232 float
233 deg2rad(const float degree)
235 return( degree * 0.017453292519943295769236907684886F);
238 static void
239 rotate_xy(float* mat3, const float degreesAroundX, const float degreesAroundY)
241 const float radX = deg2rad(degreesAroundX);
242 const float c1 = cosf(radX);
243 const float s1 = sinf(radX);
244 const float radY = deg2rad(degreesAroundY);
245 const float c2 = cosf(radY);
246 const float s2 = sinf(radY);
247 mat3[0] = c2; mat3[3] = 0.0F; mat3[6] = s2;
248 mat3[1] = s1*s2; mat3[4] = c1; mat3[7] = -s1*c2;
249 mat3[2] = -c1*s2;mat3[5] = s1; mat3[8] = c1*c2;
252 static void
253 identity(float* mat3)
255 mat3[0] = 1.0F; mat3[3] = 0.0F; mat3[6] = 0.0F;
256 mat3[1] = 0.0F; mat3[4] = 1.0F; mat3[7] = 0.0F;
257 mat3[2] = 0.0F; mat3[5] = 0.0F; mat3[8] = 1.0F;
260 static void
261 Draw(void)
263 GLint location = glGetUniformLocation(program, "rot");
264 static const float m = -10.F;
265 static const float p = 10.F;
266 static const float d = -0.5F;
268 glUseProgram(program);
269 glUniformMatrix3fv(location, 1, 0, rot);
271 glBegin(GL_QUADS);
273 glTexCoord2f(0.0F, 0.0F); glVertex3f(m, m, d);
274 glTexCoord2f(1.0F, 0.0F); glVertex3f(p, m, d);
275 glTexCoord2f(1.0F, 1.0F); glVertex3f(p, p, d);
276 glTexCoord2f(0.0F, 1.0F); glVertex3f(m, p, d);
278 glEnd();
279 glUseProgram(0);
281 glutSwapBuffers();
284 static int frames = 0;
285 static int t0 = 0;
286 static int t1 = 0;
287 float dt;
288 frames++;
289 t1 = glutGet(GLUT_ELAPSED_TIME);
290 dt = (float)(t1-t0)/1000.0F;
291 if(dt >= 5.0F)
293 float fps = (float)frames / dt;
294 printf("%f FPS (%d frames in %f seconds)\n", fps, frames, dt);
295 frames = 0;
296 t0 = t1;
302 static void
303 Reshape(int width, int height)
305 WinWidth = width;
306 WinHeight = height;
307 glViewport(0, 0, width, height);
308 glMatrixMode(GL_PROJECTION);
309 glLoadIdentity();
310 glOrtho(-10, 10, -10, 10, -1, 1);
311 glMatrixMode(GL_MODELVIEW);
312 glLoadIdentity();
316 static void
317 Key(unsigned char key, int x, int y)
319 (void) x;
320 (void) y;
321 switch (key) {
322 case 27:
323 glutDestroyWindow(Win);
324 exit(0);
325 break;
327 glutPostRedisplay();
331 static
332 void
333 drag(int x, int y)
335 float scale = 1.5F;
336 if(mouseGrabbed)
338 static GLfloat xRot = 0, yRot = 0;
339 xRot = (float)(x - WinWidth/2) / scale;
340 yRot = (float)(y - WinHeight/2) / scale;
341 identity(rot);
342 rotate_xy(rot, yRot, xRot);
343 glutPostRedisplay();
348 static
349 void
350 mouse(int button, int state, int x, int y)
352 mouseGrabbed = (state == GLUT_DOWN);
356 static void
357 Init(void)
359 glDisable(GL_DEPTH_TEST);
361 if(!ShadersSupported())
363 fprintf(stderr, "Shaders are not supported!\n");
364 exit(-1);
367 vertShader = CompileShaderText(GL_VERTEX_SHADER, vsSource);
368 fragShader = CompileShaderText(GL_FRAGMENT_SHADER, fsSource);
369 program = LinkShaders(vertShader, fragShader);
370 glUseProgram(0);
372 if(glGetError() != 0)
374 fprintf(stderr, "Shaders were not loaded!\n");
375 exit(-1);
378 if(!glIsShader(vertShader))
380 fprintf(stderr, "Vertex shader failed!\n");
381 exit(-1);
384 if(!glIsProgram(program))
386 fprintf(stderr, "Shader program failed!\n");
387 exit(-1);
390 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
395 main(int argc, char *argv[])
397 glutInitWindowSize(WinWidth, WinHeight);
398 glutInit(&argc, argv);
399 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
400 Win = glutCreateWindow(argv[0]);
401 glewInit();
402 glutReshapeFunc(Reshape);
403 glutKeyboardFunc(Key);
404 glutDisplayFunc(Draw);
405 glutMouseFunc(mouse);
406 glutMotionFunc(drag);
407 glutIdleFunc(Draw);
408 Init();
409 glutMainLoop();
410 return 0;