lens_flare.cpp 5.3 KB

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