LensFlare.cpp
上传用户:hkb425
上传日期:2007-06-16
资源大小:34191k
文件大小:6k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. // LensFlare.cpp: implementation of the CLensFlare class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "LensFlare.h"
  6. #include "texture.h"
  7. //////////////////////////////////////////////////////////////////////
  8. // Construction/Destruction
  9. //////////////////////////////////////////////////////////////////////
  10. CLensFlare::CLensFlare()
  11. {
  12. }
  13. CLensFlare::~CLensFlare()
  14. {
  15. }
  16. bool CLensFlare::InitLensFlare()
  17. {
  18. CTexture cTexture;
  19. char filename[64];
  20.     for(int i=0;i<7;i++)
  21. {
  22. wsprintf(filename,"flare/flare%d.bmp",i);
  23.     if(!cTexture.MakeAlphaTextureBind(filename,&m_texFlare[i]))return false;
  24. }
  25. m_LightPos=VERTEX(128000,30000,-72000);
  26.     m_cHmap.m_pSunPos->xpos= m_LightPos.xpos;
  27.     m_cHmap.m_pSunPos->ypos= m_LightPos.ypos;
  28. m_cHmap.m_pSunPos->zpos= m_LightPos.zpos;
  29.     return true;
  30. }
  31. void CLensFlare::UpdateLensFlare()
  32. {
  33. if(!CheckLightVisible())
  34. {
  35. m_cHmap.m_bSunVisible=true; //restore
  36. return;
  37. }
  38.     m_cHmap.m_bSunVisible=true; //restore
  39. float size=0.06f;
  40.     float offset=1;
  41. // glAlphaFunc(GL_GEQUAL,0.25f);
  42. glDisable(GL_DEPTH_TEST);
  43. glBlendFunc(GL_SRC_ALPHA,GL_ONE);
  44. glEnable(GL_BLEND);
  45.     glEnable(GL_TEXTURE_2D); 
  46.     glPushMatrix();
  47. glTranslatef(m_cHmap.m_pViewPos->xpos,m_cHmap.m_pViewPos->ypos,m_cHmap.m_pViewPos->zpos);
  48.    glRotatef(*m_cHmap.m_pViewRotY,  0.0f,1.0f,0.0f);
  49. glRotatef(-*m_cHmap.m_pViewRotX,  1.0f,0.0f,0.0f);
  50.     /////////////////////1
  51. glBindTexture(GL_TEXTURE_2D, m_texFlare[1]);
  52. glColor3f(0.05f,0.3f,0.5f);
  53. offset=1.3f;
  54. size=0.08f;
  55.    glBegin(GL_QUADS);
  56.     glTexCoord2f(0,0);
  57. glVertex3f(sx*offset-size,sy*offset-size,-1.03923f);
  58.      glTexCoord2f(1,0);
  59. glVertex3f(sx*offset+size,sy*offset-size,-1.03923f);
  60.     glTexCoord2f(1,1);
  61. glVertex3f(sx*offset+size,sy*offset+size,-1.03923f);
  62.     glTexCoord2f(0,1);
  63. glVertex3f(sx*offset-size,sy*offset+size,-1.03923f);
  64.     glEnd();
  65.     /////////////////////2
  66. glBindTexture(GL_TEXTURE_2D, m_texFlare[2]);
  67. glColor3f(0.0f,0.3f,0.5f);
  68. offset=0.7f;
  69. size=0.03f;
  70.    glBegin(GL_QUADS);
  71.     glTexCoord2f(0,0);
  72. glVertex3f(sx*offset-size,sy*offset-size,-1.03923f);
  73.      glTexCoord2f(1,0);
  74. glVertex3f(sx*offset+size,sy*offset-size,-1.03923f);
  75.     glTexCoord2f(1,1);
  76. glVertex3f(sx*offset+size,sy*offset+size,-1.03923f);
  77.     glTexCoord2f(0,1);
  78. glVertex3f(sx*offset-size,sy*offset+size,-1.03923f);
  79.     glEnd();
  80.     /////////////////////3
  81. glBindTexture(GL_TEXTURE_2D, m_texFlare[3]);
  82. glColor3f(0.05f,0.3f,0.5f);
  83.  
  84. offset=0.5f;
  85. size=0.04f;
  86.    glBegin(GL_QUADS);
  87.     glTexCoord2f(0,0);
  88. glVertex3f(sx*offset-size,sy*offset-size,-1.03923f);
  89.      glTexCoord2f(1,0);
  90. glVertex3f(sx*offset+size,sy*offset-size,-1.03923f);
  91.     glTexCoord2f(1,1);
  92. glVertex3f(sx*offset+size,sy*offset+size,-1.03923f);
  93.     glTexCoord2f(0,1);
  94. glVertex3f(sx*offset-size,sy*offset+size,-1.03923f);
  95.     glEnd();
  96.     /////////////////////4
  97. glBindTexture(GL_TEXTURE_2D, m_texFlare[4]);
  98. glColor3f(0.4f,0.1f,0.0f);
  99.  
  100. offset=0.1f;
  101. size=0.06f;
  102.    glBegin(GL_QUADS);
  103.     glTexCoord2f(0,0);
  104. glVertex3f(sx*offset-size,sy*offset-size,-1.03923f);
  105.      glTexCoord2f(1,0);
  106. glVertex3f(sx*offset+size,sy*offset-size,-1.03923f);
  107.     glTexCoord2f(1,1);
  108. glVertex3f(sx*offset+size,sy*offset+size,-1.03923f);
  109.     glTexCoord2f(0,1);
  110. glVertex3f(sx*offset-size,sy*offset+size,-1.03923f);
  111.     glEnd();
  112.     /////////////////////5
  113. glBindTexture(GL_TEXTURE_2D, m_texFlare[5]);
  114. glColor3f(0.3f,0.05f,0.0f);
  115.  
  116. offset=-0.3f;
  117. size=0.12f;
  118.    glBegin(GL_QUADS);
  119.     glTexCoord2f(0,0);
  120. glVertex3f(sx*offset-size,sy*offset-size,-1.03923f);
  121.      glTexCoord2f(1,0);
  122. glVertex3f(sx*offset+size,sy*offset-size,-1.03923f);
  123.     glTexCoord2f(1,1);
  124. glVertex3f(sx*offset+size,sy*offset+size,-1.03923f);
  125.     glTexCoord2f(0,1);
  126. glVertex3f(sx*offset-size,sy*offset+size,-1.03923f);
  127.     glEnd();
  128.     /////////////////////6
  129. glBindTexture(GL_TEXTURE_2D, m_texFlare[6]);
  130. glColor3f(0.3f,0.0f,0.0f);
  131.  
  132. offset=-0.9f;
  133. size=0.14f;
  134.    glBegin(GL_QUADS);
  135.     glTexCoord2f(0,0);
  136. glVertex3f(sx*offset-size,sy*offset-size,-1.03923f);
  137.      glTexCoord2f(1,0);
  138. glVertex3f(sx*offset+size,sy*offset-size,-1.03923f);
  139.     glTexCoord2f(1,1);
  140. glVertex3f(sx*offset+size,sy*offset+size,-1.03923f);
  141.     glTexCoord2f(0,1);
  142. glVertex3f(sx*offset-size,sy*offset+size,-1.03923f);
  143.     glEnd();
  144. ////////////////////////////////////////////////
  145. /////////////////////Draw light Shine
  146. glBindTexture(GL_TEXTURE_2D, m_texFlare[0]);
  147. glTranslatef(sx,sy,0);
  148.     glRotatef((*m_cHmap.m_pViewRotY+*m_cHmap.m_pViewRotX)*2,  0.0f,0.0f,1.0f);
  149. glColor3f(0.7f,0.4f,0.0f);
  150. size=0.15f;
  151.    glBegin(GL_QUADS);
  152.     glTexCoord2f(0,0);
  153. glVertex3f(-size,-size,-1.03923f);
  154.      glTexCoord2f(1,0);
  155. glVertex3f(+size,-size,-1.03923f);
  156.     glTexCoord2f(1,1);
  157. glVertex3f(+size,+size,-1.03923f);
  158.     glTexCoord2f(0,1);
  159. glVertex3f(-size,+size,-1.03923f);
  160.     glEnd();
  161. ////////////////////////////////////////////////
  162. glColor3f(1,1,1);
  163. glPopMatrix();
  164. glDisable(GL_BLEND);
  165. glDisable(GL_ALPHA_TEST);
  166. }
  167. bool CLensFlare::CheckLightVisible()
  168. {
  169. VERTEX v0=VERTEX(m_LightPos.xpos-m_cHmap.m_pViewPos->xpos,
  170.              m_LightPos.ypos-m_cHmap.m_pViewPos->ypos,
  171.  m_LightPos.zpos-m_cHmap.m_pViewPos->zpos);
  172. float rotx=*m_cHmap.m_pViewRotX;
  173. float roty=*m_cHmap.m_pViewRotY;
  174. //////// y rotate
  175.     VERTEX v1=VERTEX(v0.xpos*cosf(roty*0.0174533f)-
  176.              v0.zpos*sinf(roty*0.0174533f),
  177.          v0.ypos,
  178.                      v0.xpos*sinf(roty*0.0174533f)+
  179.              v0.zpos*cosf(roty*0.0174533f));
  180.     //////// x rotate
  181.     VERTEX v2=VERTEX(v1.xpos,
  182.              v1.ypos*cosf(rotx*0.0174533f)-
  183.              v1.zpos*sinf(rotx*0.0174533f),
  184.                      v1.ypos*sinf(rotx*0.0174533f)+
  185.              v1.zpos*cosf(rotx*0.0174533f));
  186. ////////////////////
  187. if(v2.zpos>-1)return false;
  188. double scale=-1.03923/v2.zpos;
  189. sx=float(v2.xpos*scale);
  190. sy=float(v2.ypos*scale);
  191. if(sx>0.8f || sx<-0.8f)return false;
  192. if(sy>0.6f || sy<-0.6f)return false;
  193. //////// Check Visible
  194.     if(!m_cHmap.CheckSunVisible())
  195.         return false;
  196.     return true;
  197. }