Clone of mesa.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

fsraytrace.c 18KB

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