#include "stdafx.h" #include "K3DTypes.h" #include "SGameLens.h" #include "KRenderObject.h" #include "KResourceManager.h" #include "K3DCamera.h" #include #include "TerrainPrimitive.h" bool SGameLensFlare::s_bRenderFlag = true; SGameFlare::SGameFlare() { memset( m_Vertex, 0, sizeof( m_Vertex ) ); } SGameFlare::~SGameFlare() { } void SGameFlare::Init( K3DRenderDevice *pDevice, K3DIndexBuffer * pIndexBuf ) { m_nWidth = 100; m_nHeight = 100; m_Color.color = 0xFFFFFFFF; int n = 0; m_Vertex[n].p = D3DXVECTOR3( 0.0f, 0.0f, 0.0f ); m_Vertex[n].rhw = 1.0f; m_Vertex[n].color = m_Color.color; m_Vertex[n].tu = 0.0f; m_Vertex[n].tv = 0.0f; ++n; m_Vertex[n].p = D3DXVECTOR3( (float)-m_nWidth, 0.0f, 0.0f ); m_Vertex[n].rhw = 1.0f; m_Vertex[n].color = m_Color.color; m_Vertex[n].tu = 1.0f; m_Vertex[n].tv = 0.0f; ++n; m_Vertex[n].p = D3DXVECTOR3( 0.0f, 0.0f, (float)-m_nHeight ); m_Vertex[n].rhw = 1.0f; m_Vertex[n].color = m_Color.color; m_Vertex[n].tu = 0.0f; m_Vertex[n].tv = 1.0f; ++n; m_Vertex[n].p = D3DXVECTOR3( (float)-m_nWidth, 0.0f, (float)-m_nHeight ); m_Vertex[n].rhw = 1.0f; m_Vertex[n].color = m_Color.color; m_Vertex[n].tu = 1.0f; m_Vertex[n].tv = 1.0f; ++n; static WORD indexbuffer[6] = { 2, 3, 1, 2, 1, 0, }; m_spTexture = NULL; m_fVis = 1.f; m_pParentMat = NULL; //SkyBox 위치 K3DMatrixIdentity( m_LocalMat ); //구름 위치 K3DMatrixIdentity( m_WorldMat ); m_pPrimitive = new SLensPrimitive; //Render Object m_pPrimitive->SetTransparent( true ); m_pPrimitive->SetTransform( &m_LocalMat, &m_WorldMat ); m_pPrimitive->SetCommonVertex( m_Vertex ); m_pPrimitive->SetCommonIndexed( indexbuffer ); } void SGameFlare::CalsPos( float fRealIntensity, int nScreenX, int nScreenY, int nDistanceX, int nDistanceY ) { int iSpotCenterPosX = (int)(nScreenX/2 - ((float)nDistanceX * m_fLinePos)); int iSpotCenterPosY = (int)(nScreenY/2 - ((float)nDistanceY * m_fLinePos)); float fSize = 2.0f*fRealIntensity/255.f; if( fSize > 1.0f ) fSize = 0.99f; int iSizeDiv2 = (int)((float)nScreenX * m_fSize/(2.0f-fSize)); if (fRealIntensity > 50.f) fRealIntensity = 50.f; if (fRealIntensity < 0.0f) fRealIntensity = 0.0f; m_Color = KColor( 255, 255, 255, BYTE(fRealIntensity) ); int n = 0; m_Vertex[n].p = D3DXVECTOR3( (float)(iSpotCenterPosX-iSizeDiv2), (float)(iSpotCenterPosY-iSizeDiv2), 0.4f ); m_Vertex[n].color = m_Color.color; ++n; m_Vertex[n].p = D3DXVECTOR3( (float)(iSpotCenterPosX+iSizeDiv2), (float)(iSpotCenterPosY-iSizeDiv2), 0.4f ); m_Vertex[n].color = m_Color.color; ++n; m_Vertex[n].p = D3DXVECTOR3( (float)(iSpotCenterPosX-iSizeDiv2), (float)(iSpotCenterPosY+iSizeDiv2), 0.4f ); m_Vertex[n].color = m_Color.color; ++n; m_Vertex[n].p = D3DXVECTOR3( (float)(iSpotCenterPosX+iSizeDiv2), (float)(iSpotCenterPosY+iSizeDiv2), 0.4f ); m_Vertex[n].color = m_Color.color; ++n; } void SGameFlare::Process( DWORD dwTime ) { } void SGameFlare::Render( KViewportObject *viewport ) { if( m_pPrimitive ) { viewport->Register( m_pPrimitive , KRenderObject::RENDEREFX_LENSFLARE ); } } //======================================================================== //======================================================================== SGameLensFlare::SGameLensFlare() { for( int i(0); LENS_FLARE_NUM>i; i++ ) m_pFlare[i] = NULL; m_pCamera = NULL; m_pLight = NULL; m_pProjMatrix = NULL; m_nScreenWidth = 1; m_nScreenHeight = 1; m_pMat = NULL; m_fIntensityBorder = 300; m_bRenderFlag = FALSE; } SGameLensFlare::~SGameLensFlare() { for( int i(0); LENS_FLARE_NUM>i; i++ ) SAFE_DELETE( m_pFlare[i] ); SAFE_DELETE( m_pMat ); } void SGameLensFlare::Init( K3DRenderDevice *pDevice ) { m_Pos = K3DVector(0,0,0); //TODO : 리소스 옵션 로딩 추가 작업 중. NX3LoadPack loadpack; loadpack.Init(); LPCSTR TEXTURE_NAME[3] = {"LensFlare_01.dds", "LensFlare_02.dds", "LensFlare_03.dds"}; for(int i = 0 ; i < 3; ++i) { m_spTextureArray[i] = KTextureManager::GetManager()->GetTexture( TEXTURE_NAME[i], &loadpack, true, KTextureManager::GetManager()->GetMipMapBiasLevel() ); } //앞이 태양쪽 int nIndexTex[] = { 2, 2, 2, 0, 2, 0, 1, 0, 1, 0, 1, 1, 1 }; float fLinePos[] = { 0.9f, 0.8f ,0.7f , 0.6f , 0.5f ,0.4f ,0.1f ,-0.2f ,-0.3f ,-0.4f ,-0.7f ,-1.0f ,-1.3f }; float fSize[] = { 0.2f, 0.8f,0.01f, 0.05f, 0.13f,0.05f,0.05f, 0.05f, 0.09f, 0.15f, 0.15f, 0.32f, 0.40f }; // float fSize[] = { 0.6f, 0.5f, 0.01f, 0.05f, 0.13f, 0.05f, 0.05f, 0.05f, 0.09f, 0.15f, 0.15f, 0.2f, 0.3f }; m_pMat = new K3DMaterial; m_pMat->SetAmbient( K3DColor( 1.f, 1.f, 1.f, 1.f ) ); m_pMat->SetDiffuse( K3DColor( 1.f, 1.f, 1.f, 1.f ) ); m_pMat->SetSpecular( K3DColor( 1.f, 1.f, 1.f, 1.f ) ); m_pMat->SetSpecularPower( 1.0f ); for( int i(0); LENS_FLARE_NUM>i; i++ ) { m_pFlare[i] = new SGameFlare; m_pFlare[i]->Init( pDevice ); m_pFlare[i]->SetTexture( m_spTextureArray[nIndexTex[i]] ); m_pFlare[i]->SetMaterial( m_pMat ); m_pFlare[i]->SetLinePos( fLinePos[i] ); m_pFlare[i]->SetSize( fSize[i] ); } } void SGameLensFlare::SetCamera( K3DCamera* pCamera ) { m_pCamera = pCamera; } void SGameLensFlare::SetLight( const K3DLight* pLight ) { m_pLight = pLight; } void SGameLensFlare::CalsFlarePos() { if( m_pLight == NULL ) return; K3DMatrix matWorld, matView, matConcat, matViewportScale; K3DVector4 vResult; matViewportScale = K3DMatrix( m_nScreenWidth/2.f, 0.f , 0.f, 0.f, 0.f , -m_nScreenHeight/2.f, 0.f, 0.f, 0.f , 0.f , 1.f, 0.f, m_nScreenWidth/2.f, m_nScreenHeight/2.f , 0.f, 1.f ); K3DMatrixIdentity(matView); matView = *m_pCamera->GetViewMatrix(); K3DMatrixIdentity(matWorld); // no need for a world xform matConcat = matWorld; matConcat *= matView; matConcat *= *m_pProjMatrix; matConcat *= matViewportScale; // K3DVector dir; // K3DVectorNomalize( &dir, &K3DVector(m_pLight->position.x, m_pLight->position.y, m_pLight->position.z ) ); // dir *= 400.f; // K3DVector pos = dir+m_Pos; m_vSunDir *= 400.f; K3DVector pos = m_vSunDir+m_Pos; K3DVector4Transform( vResult, pos, matConcat); int m_nCoordX = (int)(vResult.x/vResult.w); int m_nCoordY = (int)(vResult.y/vResult.w); int m_nCoordW = (int)(vResult.w); if( m_nCoordW < 0.0f ) return; float fRealIntensity; int iAwayX = (m_nCoordX < 0) ? -m_nCoordX : (m_nCoordX > m_nScreenWidth) ? m_nCoordX-m_nScreenWidth : 0; int iAwayY = (m_nCoordY < 0) ? -m_nCoordY : (m_nCoordY > m_nScreenHeight) ? m_nCoordY-m_nScreenHeight : 0; float fAway = (iAwayX > iAwayY) ? (float)iAwayX : (float)iAwayY; if (fAway > m_fIntensityBorder) fAway = m_fIntensityBorder; fRealIntensity = 1.0f - (fAway / m_fIntensityBorder); fRealIntensity = 0.f + (fRealIntensity*(1.f)); if( fRealIntensity > 0.f ) { fRealIntensity *= 255; } { int iCenterOfScreenX = m_nScreenWidth/2; int iCenterOfScreenY = m_nScreenHeight/2; int iDistanceX = iCenterOfScreenX - m_nCoordX; int iDistanceY = iCenterOfScreenY - m_nCoordY; for( int i(0); LENS_FLARE_NUM>i; i++ ) { m_pFlare[i]->CalsPos( fRealIntensity, m_nScreenWidth, m_nScreenHeight, iDistanceX, iDistanceY ); } } } void SGameLensFlare::Process( DWORD dwTime ) { if( !m_bRenderFlag ) return; if( m_pLight == NULL ) return; CalsFlarePos(); for( int i(0); LENS_FLARE_NUM>i; i++ ) { m_pFlare[i]->Process( dwTime ); } } void SGameLensFlare::Render( KViewportObject *viewport ) { if( !GetForceRenderFlag() ) return; if( !m_bRenderFlag ) return; if( m_pLight == NULL ) return; for( int i(0); LENS_FLARE_NUM>i; i++ ) { m_pFlare[i]->Render( viewport ); } }