Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vsraytrace.c 18KB

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