6e1617a7ebd87bcbc2bfe8d18c6f7ea952e0aff5
1 // -*- mode: c; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; coding: utf-8-unix -*-
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
29 #include "shaderutil.h"
33 //static int WinWidth = 256, WinHeight = 256;
34 static int WinWidth
= 50, WinHeight
= 50;
35 static int mouseGrabbed
= 0;
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"
43 "uniform mat3 rot; \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"
70 "// Mesa intel gen4 generates \"unsupported IR in fragment shader 13\" for\n"
71 "// sqrt, let's work around. \n"
73 "sqrt_hack(float f2) \n"
75 " vec3 v = vec3(f2,0.0,0.0); \n"
76 " return length(v); \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"
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"
92 " float r = sph.r; \n"
93 " float t = dot(c-o,n)/dot(n,d); \n"
95 " float e = length(x-c); \n"
98 " // no intersection \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"
108 " float f = sqrt_hack(r*r - e*e); \n"
110 " float dist = t - f; \n"
113 " // inside the sphere \n"
117 " if(dist < EPSILON) \n"
120 " if(dist > isec.t) \n"
124 " isec.idx = idx; \n"
126 " isec.hit = ray.orig + ray.dir * isec.t; \n"
127 " isec.n = (isec.hit - c) / r; \n"
131 "intersect(const in Ray ray, \n"
132 " const in float max_t /*= INF*/) \n"
135 " nearest.t = max_t; \n"
136 " nearest.idx = -1; \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"
143 " return nearest; \n"
147 "idx2color(const in int idx) \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"
162 "trace0(const in Ray ray) \n"
164 " Isec isec = intersect(ray, INF); \n"
166 " if(isec.idx == -1) \n"
168 " return backgroundColor; \n"
171 " vec4 diff = idx2color(isec.idx); \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"
181 "trace1(const in Ray ray) \n"
183 " Isec isec = intersect(ray, INF); \n"
185 " if(isec.idx == -1) \n"
187 " return backgroundColor; \n"
190 " Ray reflRay = Ray(isec.hit, reflect(ray.dir, isec.n)); \n"
192 " vec4 reflCol = trace0(reflRay); \n"
194 " vec4 diff = idx2color(isec.idx) + reflCol; \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"
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"
212 static GLuint vertShader
;
213 static GLuint program
;
219 const float w
= 0.5F
* WinWidth
;
220 const float h
= 0.5F
* WinHeight
;
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);
227 glUseProgram(program
);
228 glUniformMatrix3fv(location
, 1, 0, rot
);
230 for(y
= 0; y
< WinHeight
; y
++)
232 for(x
= 0; x
< WinWidth
; x
++)
234 const float posx
= x
/ w
- 1.0F
;
235 const float posy
= y
/ h
- 1.0F
;
236 glVertex2f(posx
, posy
);
244 static int frames
= 0;
248 t1
= glutGet(GLUT_ELAPSED_TIME
);
249 float dt
= (float)(t1
-t0
)/1000.0F
;
252 float fps
= (float)frames
/ dt
;
253 printf("%f FPS (%d frames in %f seconds)\n", fps
, frames
, dt
);
261 Reshape(int width
, int height
)
265 glViewport(0, 0, width
, height
);
266 glMatrixMode(GL_PROJECTION
);
268 glMatrixMode(GL_MODELVIEW
);
274 Key(unsigned char key
, int x
, int y
)
278 glutDestroyWindow(Win
);
291 printf("%4d %4d\n", x
, y
);
298 mouse(int button
, int state
, int x
, int y
)
300 if(state
== GLUT_DOWN
)
302 mouseGrabbed
= !mouseGrabbed
;
310 glDisable(GL_DEPTH_TEST
);
312 if(!ShadersSupported())
314 fprintf(stderr
, "Shaders are not supported!\n");
318 vertShader
= CompileShaderText(GL_VERTEX_SHADER
, vsSource
);
319 program
= LinkShaders(vertShader
, 0);
322 if(glGetError() != 0)
324 fprintf(stderr
, "Shaders were not loaded!\n");
328 if(!glIsShader(vertShader
))
330 fprintf(stderr
, "Vertex shader failed!\n");
334 if(!glIsProgram(program
))
336 fprintf(stderr
, "Shader program failed!\n");
340 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER
));
345 main(int argc
, char *argv
[])
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]);
353 glutReshapeFunc(Reshape
);
354 glutKeyboardFunc(Key
);
355 glutDisplayFunc(Draw
);
357 glutMouseFunc(mouse
);
358 glutMotionFunc(drag
);