LensFlare.cpp
上传用户:hkb425
上传日期:2007-06-16
资源大小:34191k
文件大小:6k
- // LensFlare.cpp: implementation of the CLensFlare class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "LensFlare.h"
- #include "texture.h"
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CLensFlare::CLensFlare()
- {
- }
- CLensFlare::~CLensFlare()
- {
- }
- bool CLensFlare::InitLensFlare()
- {
- CTexture cTexture;
- char filename[64];
- for(int i=0;i<7;i++)
- {
- wsprintf(filename,"flare/flare%d.bmp",i);
- if(!cTexture.MakeAlphaTextureBind(filename,&m_texFlare[i]))return false;
- }
- m_LightPos=VERTEX(128000,30000,-72000);
- m_cHmap.m_pSunPos->xpos= m_LightPos.xpos;
- m_cHmap.m_pSunPos->ypos= m_LightPos.ypos;
- m_cHmap.m_pSunPos->zpos= m_LightPos.zpos;
- return true;
- }
- void CLensFlare::UpdateLensFlare()
- {
- if(!CheckLightVisible())
- {
- m_cHmap.m_bSunVisible=true; //restore
- return;
- }
- m_cHmap.m_bSunVisible=true; //restore
- float size=0.06f;
- float offset=1;
- // glAlphaFunc(GL_GEQUAL,0.25f);
- glDisable(GL_DEPTH_TEST);
- glBlendFunc(GL_SRC_ALPHA,GL_ONE);
- glEnable(GL_BLEND);
- glEnable(GL_TEXTURE_2D);
- glPushMatrix();
- glTranslatef(m_cHmap.m_pViewPos->xpos,m_cHmap.m_pViewPos->ypos,m_cHmap.m_pViewPos->zpos);
- glRotatef(*m_cHmap.m_pViewRotY, 0.0f,1.0f,0.0f);
- glRotatef(-*m_cHmap.m_pViewRotX, 1.0f,0.0f,0.0f);
- /////////////////////1
- glBindTexture(GL_TEXTURE_2D, m_texFlare[1]);
- glColor3f(0.05f,0.3f,0.5f);
- offset=1.3f;
- size=0.08f;
- glBegin(GL_QUADS);
- glTexCoord2f(0,0);
- glVertex3f(sx*offset-size,sy*offset-size,-1.03923f);
- glTexCoord2f(1,0);
- glVertex3f(sx*offset+size,sy*offset-size,-1.03923f);
- glTexCoord2f(1,1);
- glVertex3f(sx*offset+size,sy*offset+size,-1.03923f);
- glTexCoord2f(0,1);
- glVertex3f(sx*offset-size,sy*offset+size,-1.03923f);
- glEnd();
- /////////////////////2
- glBindTexture(GL_TEXTURE_2D, m_texFlare[2]);
- glColor3f(0.0f,0.3f,0.5f);
-
- offset=0.7f;
- size=0.03f;
- glBegin(GL_QUADS);
- glTexCoord2f(0,0);
- glVertex3f(sx*offset-size,sy*offset-size,-1.03923f);
- glTexCoord2f(1,0);
- glVertex3f(sx*offset+size,sy*offset-size,-1.03923f);
- glTexCoord2f(1,1);
- glVertex3f(sx*offset+size,sy*offset+size,-1.03923f);
- glTexCoord2f(0,1);
- glVertex3f(sx*offset-size,sy*offset+size,-1.03923f);
- glEnd();
- /////////////////////3
- glBindTexture(GL_TEXTURE_2D, m_texFlare[3]);
- glColor3f(0.05f,0.3f,0.5f);
-
- offset=0.5f;
- size=0.04f;
- glBegin(GL_QUADS);
- glTexCoord2f(0,0);
- glVertex3f(sx*offset-size,sy*offset-size,-1.03923f);
- glTexCoord2f(1,0);
- glVertex3f(sx*offset+size,sy*offset-size,-1.03923f);
- glTexCoord2f(1,1);
- glVertex3f(sx*offset+size,sy*offset+size,-1.03923f);
- glTexCoord2f(0,1);
- glVertex3f(sx*offset-size,sy*offset+size,-1.03923f);
- glEnd();
- /////////////////////4
- glBindTexture(GL_TEXTURE_2D, m_texFlare[4]);
- glColor3f(0.4f,0.1f,0.0f);
-
- offset=0.1f;
- size=0.06f;
- glBegin(GL_QUADS);
- glTexCoord2f(0,0);
- glVertex3f(sx*offset-size,sy*offset-size,-1.03923f);
- glTexCoord2f(1,0);
- glVertex3f(sx*offset+size,sy*offset-size,-1.03923f);
- glTexCoord2f(1,1);
- glVertex3f(sx*offset+size,sy*offset+size,-1.03923f);
- glTexCoord2f(0,1);
- glVertex3f(sx*offset-size,sy*offset+size,-1.03923f);
- glEnd();
- /////////////////////5
- glBindTexture(GL_TEXTURE_2D, m_texFlare[5]);
- glColor3f(0.3f,0.05f,0.0f);
-
- offset=-0.3f;
- size=0.12f;
- glBegin(GL_QUADS);
- glTexCoord2f(0,0);
- glVertex3f(sx*offset-size,sy*offset-size,-1.03923f);
- glTexCoord2f(1,0);
- glVertex3f(sx*offset+size,sy*offset-size,-1.03923f);
- glTexCoord2f(1,1);
- glVertex3f(sx*offset+size,sy*offset+size,-1.03923f);
- glTexCoord2f(0,1);
- glVertex3f(sx*offset-size,sy*offset+size,-1.03923f);
- glEnd();
- /////////////////////6
- glBindTexture(GL_TEXTURE_2D, m_texFlare[6]);
- glColor3f(0.3f,0.0f,0.0f);
-
- offset=-0.9f;
- size=0.14f;
- glBegin(GL_QUADS);
- glTexCoord2f(0,0);
- glVertex3f(sx*offset-size,sy*offset-size,-1.03923f);
- glTexCoord2f(1,0);
- glVertex3f(sx*offset+size,sy*offset-size,-1.03923f);
- glTexCoord2f(1,1);
- glVertex3f(sx*offset+size,sy*offset+size,-1.03923f);
- glTexCoord2f(0,1);
- glVertex3f(sx*offset-size,sy*offset+size,-1.03923f);
- glEnd();
- ////////////////////////////////////////////////
- /////////////////////Draw light Shine
-
- glBindTexture(GL_TEXTURE_2D, m_texFlare[0]);
- glTranslatef(sx,sy,0);
- glRotatef((*m_cHmap.m_pViewRotY+*m_cHmap.m_pViewRotX)*2, 0.0f,0.0f,1.0f);
- glColor3f(0.7f,0.4f,0.0f);
- size=0.15f;
- glBegin(GL_QUADS);
- glTexCoord2f(0,0);
- glVertex3f(-size,-size,-1.03923f);
- glTexCoord2f(1,0);
- glVertex3f(+size,-size,-1.03923f);
- glTexCoord2f(1,1);
- glVertex3f(+size,+size,-1.03923f);
- glTexCoord2f(0,1);
- glVertex3f(-size,+size,-1.03923f);
- glEnd();
- ////////////////////////////////////////////////
- glColor3f(1,1,1);
- glPopMatrix();
- glDisable(GL_BLEND);
- glDisable(GL_ALPHA_TEST);
- }
- bool CLensFlare::CheckLightVisible()
- {
- VERTEX v0=VERTEX(m_LightPos.xpos-m_cHmap.m_pViewPos->xpos,
- m_LightPos.ypos-m_cHmap.m_pViewPos->ypos,
- m_LightPos.zpos-m_cHmap.m_pViewPos->zpos);
- float rotx=*m_cHmap.m_pViewRotX;
- float roty=*m_cHmap.m_pViewRotY;
- //////// y rotate
- VERTEX v1=VERTEX(v0.xpos*cosf(roty*0.0174533f)-
- v0.zpos*sinf(roty*0.0174533f),
- v0.ypos,
- v0.xpos*sinf(roty*0.0174533f)+
- v0.zpos*cosf(roty*0.0174533f));
- //////// x rotate
- VERTEX v2=VERTEX(v1.xpos,
- v1.ypos*cosf(rotx*0.0174533f)-
- v1.zpos*sinf(rotx*0.0174533f),
- v1.ypos*sinf(rotx*0.0174533f)+
- v1.zpos*cosf(rotx*0.0174533f));
- ////////////////////
- if(v2.zpos>-1)return false;
- double scale=-1.03923/v2.zpos;
- sx=float(v2.xpos*scale);
- sy=float(v2.ypos*scale);
- if(sx>0.8f || sx<-0.8f)return false;
- if(sy>0.6f || sy<-0.6f)return false;
- //////// Check Visible
- if(!m_cHmap.CheckSunVisible())
- return false;
- return true;
- }