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

游戏引擎

开发平台:

Visual C++

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