lens_flare.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #include "lens_flare.h"
  2. #ifndef IGL_NO_OPENGL
  3. #include "C_STR.h"
  4. #include "unproject.h"
  5. #include "project.h"
  6. #include "shine_textures.h"
  7. #include "flare_textures.h"
  8. #include <iostream>
  9. // http://www.opengl.org/archives/resources/features/KilgardTechniques/LensFlare/glflare.c
  10. static void setup_texture(
  11. const uint8_t * texture,
  12. const int width,
  13. const int height,
  14. GLuint texobj,
  15. GLenum minFilter, GLenum maxFilter)
  16. {
  17. glBindTexture(GL_TEXTURE_2D, texobj);
  18. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  19. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
  20. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, maxFilter);
  21. glTexImage2D(GL_TEXTURE_2D, 0, 1, width, height, 0,
  22. GL_LUMINANCE, GL_UNSIGNED_BYTE, texture);
  23. }
  24. void igl::lens_flare_load_textures(
  25. std::vector<GLuint> & shine_id,
  26. std::vector<GLuint> & flare_id)
  27. {
  28. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  29. shine_id.resize(10);
  30. glGenTextures(10,&shine_id[0]);
  31. for (int i = 0; i < (int)shine_id.size(); i++) {
  32. setup_texture(
  33. SHINE_TEXTURES[i],
  34. SHINE_TEXTURE_WIDTHS[i],
  35. SHINE_TEXTURE_HEIGHTS[i],
  36. shine_id[i], GL_LINEAR, GL_LINEAR);
  37. }
  38. flare_id.resize(6);
  39. glGenTextures(6,&flare_id[0]);
  40. for (int i = 0; i < (int)flare_id.size(); i++) {
  41. setup_texture(
  42. FLARE_TEXTURES[i],
  43. FLARE_TEXTURE_WIDTHS[i],
  44. FLARE_TEXTURE_HEIGHTS[i],
  45. flare_id[i], GL_LINEAR, GL_LINEAR);
  46. }
  47. }
  48. void igl::lens_flare_create(
  49. const float * A,
  50. const float * B,
  51. const float * C,
  52. std::vector<igl::Flare> & flares)
  53. {
  54. using namespace igl;
  55. using namespace std;
  56. flares.resize(12);
  57. /* Shines */
  58. flares[0] = Flare(-1, 1.0f, 0.1f, C, 1.0);
  59. flares[1] = Flare(-1, 1.0f, 0.15f, B, 1.0);
  60. flares[2] = Flare(-1, 1.0f, 0.35f, A, 1.0);
  61. /* Flares */
  62. flares[3] = Flare(2, 1.3f, 0.04f, A, 0.6);
  63. flares[4] = Flare(3, 1.0f, 0.1f, A, 0.4);
  64. flares[5] = Flare(1, 0.5f, 0.2f, A, 0.3);
  65. flares[6] = Flare(3, 0.2f, 0.05f, A, 0.3);
  66. flares[7] = Flare(0, 0.0f, 0.04f, A, 0.3);
  67. flares[8] = Flare(5, -0.25f, 0.07f, A, 0.5);
  68. flares[9] = Flare(5, -0.4f, 0.02f, A, 0.6);
  69. flares[10] = Flare(5, -0.6f, 0.04f, A, 0.4);
  70. flares[11] = Flare(5, -1.0f, 0.03f, A, 0.2);
  71. }
  72. void igl::lens_flare_draw(
  73. const std::vector<igl::Flare> & flares,
  74. const std::vector<GLuint> & shine_ids,
  75. const std::vector<GLuint> & flare_ids,
  76. const Eigen::Vector3f & light,
  77. const float near_clip,
  78. int & shine_tic)
  79. {
  80. bool ot2 = glIsEnabled(GL_TEXTURE_2D);
  81. bool ob = glIsEnabled(GL_BLEND);
  82. bool odt = glIsEnabled(GL_DEPTH_TEST);
  83. bool ocm = glIsEnabled(GL_COLOR_MATERIAL);
  84. bool ol = glIsEnabled(GL_LIGHTING);
  85. int obsa,obda,odf,odwm;
  86. glGetIntegerv(GL_BLEND_SRC_ALPHA,&obsa);
  87. glGetIntegerv(GL_BLEND_DST_ALPHA,&obda);
  88. glGetIntegerv(GL_DEPTH_FUNC,&odf);
  89. glGetIntegerv(GL_DEPTH_WRITEMASK,&odwm);
  90. glDisable(GL_COLOR_MATERIAL);
  91. glEnable(GL_DEPTH_TEST);
  92. //glDepthFunc(GL_LEQUAL);
  93. glDepthMask(GL_FALSE);
  94. glEnable(GL_TEXTURE_2D);
  95. glDisable(GL_LIGHTING);
  96. glEnable(GL_BLEND);
  97. glBlendFunc(GL_ONE, GL_ONE);
  98. using namespace Eigen;
  99. using namespace igl;
  100. using namespace std;
  101. //// view_dir direction from eye to position is is looking at
  102. //const Vector3f view_dir = (at - from).normalized();
  103. //// near_clip distance from eye to near clipping plane along view_dir
  104. //// center position on near clipping plane along viewdir from eye
  105. //const Vector3f center = from + near_clip*view_dir;
  106. Vector3f plight = project(light);
  107. // Orthogonal vectors to view direction at light
  108. Vector3f psx = plight;
  109. psx(0) += 1;
  110. Vector3f psy = plight;
  111. psy(1) += 1;
  112. // axis toward center
  113. int vp[4];
  114. glGetIntegerv(GL_VIEWPORT,vp);
  115. Vector3f center = unproject(Vector3f(0.5*vp[2],0.5*vp[3],plight[2]-1e-3));
  116. //Vector3f center(0,0,1);
  117. Vector3f axis = light-center;
  118. glLineWidth(4.);
  119. glColor3f(1,0,0);
  120. glBegin(GL_LINES);
  121. glVertex3fv(center.data());
  122. glVertex3fv(light.data());
  123. glEnd();
  124. const Vector3f SX = unproject(psx).normalized();
  125. const Vector3f SY = unproject(psy).normalized();
  126. for(int i = 0; i < (int)flares.size(); i++)
  127. {
  128. const Vector3f sx = flares[i].scale * SX;
  129. const Vector3f sy = flares[i].scale * SY;
  130. glColor3fv(flares[i].color);
  131. if (flares[i].type < 0) {
  132. glBindTexture(GL_TEXTURE_2D, shine_ids[shine_tic]);
  133. shine_tic = (shine_tic + 1) % shine_ids.size();
  134. } else
  135. {
  136. glBindTexture(GL_TEXTURE_2D, flare_ids[flares[i].type]);
  137. }
  138. /* position = center + flare[i].loc * axis */
  139. const Vector3f position = center + flares[i].loc * axis;
  140. Vector3f tmp;
  141. glBegin(GL_QUADS);
  142. glTexCoord2f(0.0, 0.0);
  143. tmp = position + sx;
  144. tmp = tmp + sy;
  145. glVertex3fv(tmp.data());
  146. glTexCoord2f(1.0, 0.0);
  147. tmp = position - sx;
  148. tmp = tmp + sy;
  149. glVertex3fv(tmp.data());
  150. glTexCoord2f(1.0, 1.0);
  151. tmp = position - sx;
  152. tmp = tmp - sy;
  153. glVertex3fv(tmp.data());
  154. glTexCoord2f(0.0, 1.0);
  155. tmp = position + sx;
  156. tmp = tmp - sy;
  157. glVertex3fv(tmp.data());
  158. glEnd();
  159. }
  160. ot2?glEnable(GL_TEXTURE_2D):glDisable(GL_TEXTURE_2D);
  161. ob?glEnable(GL_BLEND):glDisable(GL_BLEND);
  162. odt?glEnable(GL_DEPTH_TEST):glDisable(GL_DEPTH_TEST);
  163. ocm?glEnable(GL_COLOR_MATERIAL):glDisable(GL_COLOR_MATERIAL);
  164. ol?glEnable(GL_LIGHTING):glDisable(GL_LIGHTING);
  165. glBlendFunc(obsa,obda);
  166. glDepthFunc(odf);
  167. glDepthMask(odwm);
  168. }
  169. #endif