D:/Zythum/DinoKod/Bsp/BspDraw.cpp

00001 //---------------------------------------------------------------------------------------------
00002 //      This file is a part of "DinoKod".
00003 //      Copyright © 2003 Dino Productions. All Rights Reserved.
00004 //      
00005 //      File                    : BspDraw.cpp
00006 //      Author                  : Sebastien LEIX        sebastien.leix@wanadoo.fr
00007 //      Date                    : 07/09/2002
00008 //      Modification    :
00009 //
00010 //---------------------------------------------------------------------------------------------
00011 #include <conio.h>
00012 
00013 #include "Common/Assert.h"
00014 #include "Common/Console.h"
00015 #include "Common/Directory.h"
00016 #include "Common/Plane.h"
00017 #include "Common/Math3D.h"
00018 
00019 #include "Bitmap/Bmp.h"
00020 
00021 #include "Render/Render.h"
00022 #include "Render/Texture.h"
00023 #include "Render/Camera.h"
00024 #include "Render/VertexBuffer.h"
00025 #include "Landscape/SunDraw.h"
00026 #include "Landscape/SkyDraw.h"
00027 
00028 #include "Bsp/BezierPatch.h"
00029 
00030 #include "Bsp/BspDraw.h"
00031 
00032 //--------------------------------------------------------------------------
00033 //--------------------------------------------------------------------------
00034 //--------------------------------------------------------------------------
00035 KBspDraw::KBspDraw()
00036 : KBsp()
00037 {
00038         m_pShaderId                             = NULL;
00039         m_pLightMapId                   = NULL;
00040 
00041         m_bSwapRgb                              = false;
00042         m_bUseLightMaps                 = true;
00043         m_pCamera                               = NULL;
00044                                                         
00045         m_pRender                               = NULL;
00046 
00047         m_pVisibleFace                  = NULL;
00048 
00049         m_nVertex                               = 0;
00050         m_pVBuffer                              = NULL;
00051         m_bWireFrame                    = false;
00052 
00053         m_pSortedFaceList               = NULL;
00054         m_pRenderFaces                  = NULL;
00055 
00056         m_pSunDraw                              = NULL;
00057         m_pSkyDraw                              = NULL;
00058         m_CurrentLoadedShader   = 0;
00059 }
00060 
00061 //--------------------------------------------------------------------------
00062 KBspDraw::~KBspDraw()
00063 {
00064 }
00065 
00066 //--------------------------------------------------------------------------
00067 s32 KBspDraw::Init( KRender* pRender )
00068 {
00069         KASSERT( pRender );
00070 
00071         m_pRender       = pRender;
00072         m_bSwapRgb      = pRender->IsNeedToSwapRgb();
00073 /*
00074         // Initialise le soleil
00075         m_pSunDraw = new KSunDraw();
00076         m_pSunDraw->Init( m_pRender );
00077 
00078         // Initialise le ciel
00079         m_pSkyDraw = new KSkyDraw();
00080         m_pSkyDraw->Init( pRender, 30, m_pSunDraw );
00081 */
00082         return BSP_OK;
00083 }
00084 
00085 //--------------------------------------------------------------------------
00086 void KBspDraw::End()
00087 {
00088         m_pRender       = NULL;
00089         m_pCamera       = NULL;
00090 
00091         if( m_pSkyDraw )
00092                 Deletep( m_pSkyDraw );
00093 
00094         if( m_pSunDraw )
00095                 Deletep( m_pSunDraw );
00096 }
00097 
00098 //--------------------------------------------------------------------------
00099 s32 KBspDraw::LoadBSP( const char* pFileName )
00100 {
00101         s32                     Result;
00102 
00103         Result = KBsp::LoadBSP( pFileName );
00104         if( Result != BSP_OK )
00105                 return Result;
00106 
00107         m_bLoaded = false;
00108 
00109         m_CurrentLoadedShader   = 0;
00110         m_pVisibleFace                  = new bool[m_nFaces];
00111         m_pRenderFaces                  = new KRenderFace[m_nFaces];
00112         m_pSortedFaceList               = new KBspFaceList[m_nFaces];
00113 
00114         // Chargement des shaders
00115         Result = LoadShaders( m_pRender );
00116 
00117         if( Result != BSP_OK )
00118         {
00119                 Close();
00120                 return Result;
00121         }
00122 
00123         return BSP_OK;
00124 }
00125 
00126 //--------------------------------------------------------------------------
00127 s32 KBspDraw::FinishLoad()
00128 {
00129         // Chargement des lightmaps
00130         s32     Result = LoadLightMaps( m_pRender );
00131 
00132         if( Result != BSP_OK )
00133         {
00134                 Close();
00135                 return Result;
00136         }
00137 
00138         SortFacesByTextures();
00139         ComputeVertices();
00140 
00141         m_bLoaded = true;
00142 
00143         return BSP_OK;
00144 }
00145 
00146 //--------------------------------------------------------------------------
00147 s32 KBspDraw::CloseBSP()
00148 {
00149         if( m_pVisibleFace )            Deletev( m_pVisibleFace );
00150         if( m_pSortedFaceList )         Deletev( m_pSortedFaceList );
00151         
00152         if( m_pRenderFaces )
00153         {
00154                 for( u32 f = 0; f < m_nFaces; f ++ )
00155                 {
00156                         if( m_pRenderFaces[f].m_pVertex )
00157                                 Deletev( m_pRenderFaces[f].m_pVertex );
00158                         if( m_pRenderFaces[f].m_pIndices )
00159                                 Deletev( m_pRenderFaces[f].m_pIndices );
00160                 }
00161                 Deletev( m_pRenderFaces );
00162         }
00163         
00164         m_CurrentLoadedShader = 0;
00165 
00166         DestroyShaders();
00167         DestroyLightMaps();
00168 
00169         return KBsp::CloseBSP();
00170 }
00171 
00172 //--------------------------------------------------------------------------
00173 void KBspDraw::Draw()
00174 {
00175         if( !IsLoaded() )
00176                 return;
00177 
00178         KASSERT( m_pRender );
00179 
00180 //      m_pSunDraw->Draw( 0.22f );
00181 //      m_pSkyDraw->Draw();
00182 //      m_pSunDraw->DrawLensFlare();
00183 
00184         // Reset la liste des faces a afficher
00185         memset( m_pVisibleFace, 0, sizeof( bool ) * m_nFaces );
00186 
00187         m_pCamera = m_pRender->GetCamera();
00188         KASSERT( m_pCamera );
00189 
00190         m_pCamera->GetFrustumPlanes( m_pCameraPlane );
00191         u32 Leaf                = FindLeaf( 0, m_pCamera->GetPos() );
00192 
00193 //      KVector Point, Normal;
00194 //      CheckCollision( 0, KVector( 852, 2252, 184 ), KVector( 1176, -2252, 184 ), &Point, &Normal );
00195         
00196         if( Leaf )
00197         {
00198 //              g_Console << "Leaf : " << (int)Leaf << KENDL;
00199 //              m_pRender->SetRenderState( KRS_FILLMODE, KRFILLMODE_WIREFRAME );
00200                 DrawVisiblesLeaves( Leaf );
00201                 DrawFaces();
00202 
00203 //              for( Leaf = 0; Leaf < m_nLeaves; Leaf ++ )
00204 //                      DisplayLeaf( Leaf );
00205 //              m_pRender->SetClear( true );
00206 //              for( Leaf = 0; Leaf < m_nLeaves; Leaf ++ )
00207 //                      DrawLeaf( Leaf );
00208 /*      
00209                 m_pRender->SetRenderState( KRS_FILLMODE, KRFILLMODE_SOLID );
00210                 memset( m_pVisibleFace, 0, sizeof( bool ) * m_nFaces );
00211                 DrawLeaf( Leaf );
00212                 DrawFaces();*/
00213 /*
00214                 DrawVisiblesLeaves( Leaf );
00215 
00216                 if( m_bWireFrame )
00217                         m_pRender->SetRenderState( KRS_FILLMODE, KRFILLMODE_WIREFRAME );
00218                 
00219                 DrawFaces();
00220 */
00221 //              m_pRender->SetRenderState( KRS_FILLMODE, KRFILLMODE_SOLID );
00222         }
00223         else
00224         {
00225 //              m_pRender->SetRenderState( KRS_FILLMODE, KRFILLMODE_WIREFRAME );
00226 //              for( Leaf = 0; Leaf < m_nLeaves(); Leaf ++ )
00227 //                      DrawLeaf( Leaf );
00228                 if( m_bWireFrame )
00229                         m_pRender->SetRenderState( KRS_FILLMODE, KRFILLMODE_WIREFRAME );
00230                 
00231                 DrawFaces();
00232                 
00233                 m_pRender->SetRenderState( KRS_FILLMODE, KRFILLMODE_SOLID );
00234         }
00235 
00236 //      // Reset la liste des faces a afficher
00237 //      memset( m_pVisibleFace, 0, sizeof( bool ) * m_nFaces );
00238 }
00239 
00240 //--------------------------------------------------------------------------
00241 void KBspDraw::DrawVisiblesLeaves( u32 CurrentLeaf )
00242 {
00243         u32             l;
00244         u32             c;
00245         u32             m;
00246         
00247         KASSERT( CurrentLeaf < m_nLeaves );
00248 
00249         m = m_pLeaves[CurrentLeaf].m_FirstVisListId;
00250 
00251         for( l = 0; l < m_nLeaves; l ++ )
00252         {
00253                 if( m_pVisiList )
00254                 {
00255                         c = m_pLeaves[l].m_FirstVisListId;
00256 
00257                         if( ( c == -1 ) || ( m == -1 ) || (*(m_pVisiList->m_VisiList + m * m_pVisiList->m_SizeOfVector + ((c)>>3)) & (1 << ((c) & 7))) )
00258                                 DrawLeaf( l );
00259                 }
00260                 else
00261                         DrawLeaf( l );
00262         }
00263 }
00264 
00265 //--------------------------------------------------------------------------
00266 bool KBspDraw::IsBBoxInsideFrustum( KVector& Min, KVector Max )
00267 {
00268         for( s32 i = 0; i < 6; i ++ )
00269         {
00270                 if( m_pCameraPlane[i].m_Normal.x * Min.x + m_pCameraPlane[i].m_Normal.y * Min.y + m_pCameraPlane[i].m_Normal.z * Min.z + m_pCameraPlane[i].m_Distance > 0.0f )  continue;
00271                 if( m_pCameraPlane[i].m_Normal.x * Max.x + m_pCameraPlane[i].m_Normal.y * Min.y + m_pCameraPlane[i].m_Normal.z * Min.z + m_pCameraPlane[i].m_Distance > 0.0f )  continue;
00272                 if( m_pCameraPlane[i].m_Normal.x * Min.x + m_pCameraPlane[i].m_Normal.y * Max.y + m_pCameraPlane[i].m_Normal.z * Min.z + m_pCameraPlane[i].m_Distance > 0.0f )  continue;
00273                 if( m_pCameraPlane[i].m_Normal.x * Max.x + m_pCameraPlane[i].m_Normal.y * Max.y + m_pCameraPlane[i].m_Normal.z * Min.z + m_pCameraPlane[i].m_Distance > 0.0f )  continue;
00274                 if( m_pCameraPlane[i].m_Normal.x * Min.x + m_pCameraPlane[i].m_Normal.y * Min.y + m_pCameraPlane[i].m_Normal.z * Max.z + m_pCameraPlane[i].m_Distance > 0.0f )  continue;
00275                 if( m_pCameraPlane[i].m_Normal.x * Max.x + m_pCameraPlane[i].m_Normal.y * Min.y + m_pCameraPlane[i].m_Normal.z * Max.z + m_pCameraPlane[i].m_Distance > 0.0f )  continue;
00276                 if( m_pCameraPlane[i].m_Normal.x * Min.x + m_pCameraPlane[i].m_Normal.y * Max.y + m_pCameraPlane[i].m_Normal.z * Max.z + m_pCameraPlane[i].m_Distance > 0.0f )  continue;
00277                 if( m_pCameraPlane[i].m_Normal.x * Max.x + m_pCameraPlane[i].m_Normal.y * Max.y + m_pCameraPlane[i].m_Normal.z * Max.z + m_pCameraPlane[i].m_Distance > 0.0f )  continue;
00278 /*
00279                 if(m_Frustum[i][A] * x  + m_Frustum[i][B] * y  + m_Frustum[i][C] * z  + m_Frustum[i][D] > 0)  continue;
00280                 if(m_Frustum[i][A] * x2 + m_Frustum[i][B] * y  + m_Frustum[i][C] * z  + m_Frustum[i][D] > 0)  continue;
00281                 if(m_Frustum[i][A] * x  + m_Frustum[i][B] * y2 + m_Frustum[i][C] * z  + m_Frustum[i][D] > 0)  continue;
00282                 if(m_Frustum[i][A] * x2 + m_Frustum[i][B] * y2 + m_Frustum[i][C] * z  + m_Frustum[i][D] > 0)  continue;
00283                 if(m_Frustum[i][A] * x  + m_Frustum[i][B] * y  + m_Frustum[i][C] * z2 + m_Frustum[i][D] > 0)  continue;
00284                 if(m_Frustum[i][A] * x2 + m_Frustum[i][B] * y  + m_Frustum[i][C] * z2 + m_Frustum[i][D] > 0)  continue;
00285                 if(m_Frustum[i][A] * x  + m_Frustum[i][B] * y2 + m_Frustum[i][C] * z2 + m_Frustum[i][D] > 0)  continue;
00286                 if(m_Frustum[i][A] * x2 + m_Frustum[i][B] * y2 + m_Frustum[i][C] * z2 + m_Frustum[i][D] > 0)  continue;
00287 */
00288                 return false;
00289         }
00290 
00291         return true;
00292 }
00293 
00294 //--------------------------------------------------------------------------
00295 void KBspDraw::DrawLeaf( u32 Leaf )
00296 {
00297         KASSERT( IsLoaded() );
00298         KASSERT( m_pRender );
00299         KASSERT( m_pCamera );
00300         KASSERT( Leaf < m_nLeaves );
00301 
00302         if( IsBBoxInsideFrustum(        KVector( m_pLeaves[Leaf].m_BBMin[0], m_pLeaves[Leaf].m_BBMin[1], m_pLeaves[Leaf].m_BBMin[2] ),
00303                                                                 KVector( m_pLeaves[Leaf].m_BBMax[0], m_pLeaves[Leaf].m_BBMax[1], m_pLeaves[Leaf].m_BBMax[2] ) ) )
00304         {
00305                 //
00306                 //      DISPLAY DU LEAF
00307                 //
00308                 s32             FaceId;
00309                 for(    FaceId = m_pLeaves[Leaf].m_FirstFaceId;
00310                                 FaceId < m_pLeaves[Leaf].m_FirstFaceId + m_pLeaves[Leaf].m_NumFaces;
00311                                 FaceId ++ )
00312                 {
00313                         m_pVisibleFace[ m_pFaceList[FaceId].m_Face ] = true;
00314                 }
00315         }
00316         return;
00317 }
00318 
00319 //--------------------------------------------------------------------------
00320 void KBspDraw::DrawFaces()
00321 {
00322         KRenderFace             RenderFace;
00323         u32                             Face;
00324         u32                             FaceId;
00325         u32                             TexId1 = -1;
00326         u32                             TexId2 = -1;
00327         KTEXTURE                LightMapId;
00328 
00329         KASSERT( m_pRenderFaces );
00330         KASSERT( m_pFaces );
00331         KASSERT( m_pSortedFaceList );
00332         KASSERT( m_pVisibleFace );
00333 
00334         m_CurrentShaderId       = KSHADER_NO;
00335         m_CurrentLightMapId     = KTEXTURE_NO;
00336 
00337         u32             FirstFace       = 0;//GetpModels( 0 )->m_FirstFaceId;
00338         u32             nFaces          = m_nFaces;//GetpModels( 0 )->m_NumFaces;
00339 
00340         for( Face = FirstFace; Face < FirstFace + nFaces; Face ++ )
00341         {
00342                 FaceId = m_pSortedFaceList[Face].m_Face;
00343 
00344 //              if( ( FaceId < GetpModels( 0 )->m_FirstFaceId ) && ( FaceId > ( GetpModels( 0 )->m_FirstFaceId + GetpModels( 0 )->m_NumFaces ) ) )
00345 //                      continue;
00346 
00347                 if( !m_pVisibleFace[FaceId] )
00348                         continue;
00349 
00350 //              if( m_pFaces[FaceId].m_EffectId != -1 )
00351 //                      continue;
00352 
00353                 if( ( TexId1 != m_pFaces[FaceId].m_ShaderId ) || ( TexId2 != m_pFaces[FaceId].m_LightMapId ) )
00354                 {
00355                         // Flush et Dessine les triangles
00356                         m_pRender->FlushTriangles();
00357                         // Change de shader
00358                         TexId1 = m_pFaces[FaceId].m_ShaderId;
00359                         TexId2 = m_pFaces[FaceId].m_LightMapId;
00360 
00361                         if( m_bUseLightMaps && m_pLightMapId )
00362                                 LightMapId = (TexId2 == -1) ? KTEXTURE_NO : m_pLightMapId[TexId2];
00363                         else
00364                                 LightMapId = KTEXTURE_NO;
00365                         
00366                         m_CurrentShaderId       = (TexId1 == -1) ? KTEXTURE_NO : m_pShaderId[TexId1];
00367                         m_CurrentLightMapId     = LightMapId;
00368                         m_pRender->SetShader( m_CurrentShaderId, m_CurrentLightMapId, (s32)m_pFaces[FaceId].m_Type == KBFT_MESH );
00369                 }
00370 
00371                 if( ( m_pFaces[FaceId].m_Type == KBFT_POLYGON ) || ( m_pFaces[FaceId].m_Type == KBFT_MESH ) )
00372                 {
00373                         m_pRender->DrawTriangles( m_pRenderFaces[FaceId] );
00374                 }
00375                 else if( m_pFaces[FaceId].m_Type == KBFT_PATCH )
00376                 {
00377                         RenderFace.m_VertexType = m_pBezierPatches[FaceId].GetVertexType();
00378                         RenderFace.m_pVertex    = m_pBezierPatches[FaceId].GetpVertices();
00379                         RenderFace.m_nVertex    = m_pBezierPatches[FaceId].GetnVertices();
00380                         RenderFace.m_pIndices   = m_pBezierPatches[FaceId].GetpIndices();
00381                         RenderFace.m_nIndices   = m_pBezierPatches[FaceId].GetnIndices();
00382                         m_pRender->DrawTriangles( RenderFace );
00383                 }
00384         }
00385         m_pRender->FlushTriangles();
00386 }
00387 
00388 //--------------------------------------------------------------------------
00389 void KBspDraw::DisplayLeaf( u32 LeafId )
00390 {
00391         s32                             FaceId;
00392         u32                             Face;
00393         KRenderFace             RenderFace;
00394         u32                             TexId1;
00395         u32                             TexId2;
00396         KTEXTURE                ShaderId;
00397         KTEXTURE                LightMapId;
00398 
00399         for(    FaceId = m_pLeaves[LeafId].m_FirstFaceId;
00400                         FaceId < m_pLeaves[LeafId].m_FirstFaceId + m_pLeaves[LeafId].m_NumFaces;
00401                         FaceId ++ )
00402         {
00403                 Face = m_pFaceList[FaceId].m_Face;
00404         
00405                 TexId1 = m_pFaces[Face].m_ShaderId;
00406                 TexId2 = m_pFaces[Face].m_LightMapId;
00407 
00408                 LightMapId      = (TexId2 == -1) ? KTEXTURE_NO : m_pLightMapId[TexId2];
00409                 ShaderId        = (TexId1 == -1) ? KTEXTURE_NO : m_pShaderId[TexId1];
00410 
00411                 m_pRender->SetShader( ShaderId, LightMapId, (s32)m_pFaces[Face].m_Type == KBFT_MESH );
00412                 
00413                 if( ( m_pFaces[Face].m_Type == KBFT_POLYGON ) || ( m_pFaces[Face].m_Type == KBFT_MESH ) )
00414                 {
00415                         m_pRender->DrawTriangles( m_pRenderFaces[Face] );
00416                 }
00417                 else if( m_pFaces[Face].m_Type == KBFT_PATCH )
00418                 {
00419                         RenderFace.m_VertexType = m_pBezierPatches[Face].GetVertexType();
00420                         RenderFace.m_pVertex    = m_pBezierPatches[Face].GetpVertices();
00421                         RenderFace.m_nVertex    = m_pBezierPatches[Face].GetnVertices();
00422                         RenderFace.m_pIndices   = m_pBezierPatches[Face].GetpIndices();
00423                         RenderFace.m_nIndices   = m_pBezierPatches[Face].GetnIndices();
00424                         m_pRender->DrawTriangles( RenderFace );
00425                 }
00426                 m_pRender->FlushTriangles();
00427         }
00428 }
00429 
00430 //--------------------------------------------------------------------------
00431 void KBspDraw::ActiveLightMaps( bool bActive )
00432 {
00433         m_bUseLightMaps = bActive;
00434 
00435         ComputeVertices();
00436         ComputeBezier();
00437 }
00438 
00439 //---------------------------------------------------------------------------------------------------------------------
00440 int KBspDraw::CumpareFunc( const void* pElem1, const void* pElem2 )
00441 {
00442         s64             Result = ((KBspComputedFace*)pElem1)->m_SortId - ((KBspComputedFace*)pElem2)->m_SortId;
00443 
00444         if( Result > 0 )
00445                 return 1;
00446         if( Result < 0 )
00447                 return -1;
00448 
00449         return 0;
00450 }
00451 
00452 //---------------------------------------------------------------------------------------------------------------------
00453 void KBspDraw::SortFacesByTextures()
00454 {
00455         u32     Face;
00456         
00457         KBspComputedFace*       pComputedFaces  = new KBspComputedFace[m_nFaces];
00458         KShader*                        pShader;
00459         u64                                     ShaderSortValue;
00460         KSHADER                         ShaderId;
00461 
00462         for( Face = 0; Face < m_nFaces; Face ++ )
00463         {
00464                 if( ( m_pFaces[Face].m_ShaderId >= 65535 ) || ( m_pFaces[Face].m_LightMapId >= 65535 ) )
00465                 {
00466                         KError::FatalError( NULL, "KBspDraw::SortFacesByTextures(...) : ( m_pFaces[Face].m_TextureId >= 65535 ) || ( m_pFaces[Face].m_LightMapId >= 65535 )" );
00467                         return;
00468                 }
00469 
00470                 ShaderId = m_pShaderId[m_pFaces[Face].m_ShaderId];
00471 
00472                 if( ( ShaderId != KSHADER_NO ) && ( pShader = m_pRender->GetpShaderBank()->GetpShader( ShaderId ) ) )
00473                         ShaderSortValue = pShader->m_SortValue;
00474                 else
00475                         ShaderSortValue = 0;
00476 
00477                 pComputedFaces[Face].m_ShaderId                 = m_pFaces[Face].m_ShaderId;
00478                 pComputedFaces[Face].m_EffectId                 = m_pFaces[Face].m_EffectId;
00479                 pComputedFaces[Face].m_Type                             = m_pFaces[Face].m_Type;
00480                 pComputedFaces[Face].m_FirstVertexId    = m_pFaces[Face].m_FirstVertexId;
00481                 pComputedFaces[Face].m_NumVertices              = m_pFaces[Face].m_NumVertices;
00482                 pComputedFaces[Face].m_FirstMeshVert    = m_pFaces[Face].m_FirstMeshVert;
00483                 pComputedFaces[Face].m_NumMeshVerts             = m_pFaces[Face].m_NumMeshVerts;
00484                 pComputedFaces[Face].m_LightMapId               = m_pFaces[Face].m_LightMapId;
00485                 pComputedFaces[Face].m_LightMapStart    = m_pFaces[Face].m_LightMapStart;
00486                 pComputedFaces[Face].m_LightMapSize             = m_pFaces[Face].m_LightMapSize;
00487                 pComputedFaces[Face].m_LightMapOrigin   = m_pFaces[Face].m_LightMapOrigin;
00488                 pComputedFaces[Face].m_LightMapVecs[0]  = m_pFaces[Face].m_LightMapVecs[0];
00489                 pComputedFaces[Face].m_LightMapVecs[1]  = m_pFaces[Face].m_LightMapVecs[1];
00490                 pComputedFaces[Face].m_Normal                   = m_pFaces[Face].m_Normal;
00491                 pComputedFaces[Face].m_Size                             = m_pFaces[Face].m_Size;
00492                 pComputedFaces[Face].m_FaceId                   = Face;
00493                 pComputedFaces[Face].m_SortId                   = (s64)( ( (ShaderSortValue+1) << 32 ) | ( (pComputedFaces[Face].m_LightMapId+1) << 16 ) | (pComputedFaces[Face].m_ShaderId+1) );
00494         }
00495 
00496         qsort( pComputedFaces, m_nFaces, sizeof( KBspComputedFace ), CumpareFunc );
00497         
00498         for( Face = 0; Face < m_nFaces; Face ++ )
00499         {
00500                 // Creation de la liste de correspondance
00501                 m_pSortedFaceList[Face].m_Face = pComputedFaces[Face].m_FaceId;
00502 
00503                 // Creation de la liste de faces
00504                 s32             MeshVertexId;
00505                 u32             VertexId;
00506 
00507                 if( m_pFaces[Face].m_NumMeshVerts )
00508                 {
00509                         KLVertex2*              pTmpVertex = NULL;
00510                         KLVertex2               Vertex;
00511                         u16                             nVertex = 0;
00512                         u16                             nIndex  = 0;
00513 
00514                         pTmpVertex = new KLVertex2[m_pFaces[Face].m_NumMeshVerts];
00515                         m_pRenderFaces[Face].m_VertexType       = KRLVERTEX2;
00516                         m_pRenderFaces[Face].m_pIndices         = new u16[m_pFaces[Face].m_NumMeshVerts];
00517                         m_pRenderFaces[Face].m_nIndices         = (u16)m_pFaces[Face].m_NumMeshVerts;
00518 
00519                         // Construit la face avec les vertex et les indices
00520                         for(    MeshVertexId = m_pFaces[Face].m_FirstMeshVert;
00521                                         MeshVertexId < m_pFaces[Face].m_FirstMeshVert + m_pFaces[Face].m_NumMeshVerts;
00522                                         MeshVertexId ++ )
00523                         {
00524                                 KASSERT( nVertex < m_pFaces[Face].m_NumMeshVerts );
00525                                 KASSERT( nVertex < 65535 );
00526                                 
00527                                 // Construit le vertex
00528                                 VertexId = m_pFaces[Face].m_FirstVertexId + m_pMeshVerts[MeshVertexId].m_FirstVertexId;
00529                                 Vertex.Position = m_pVertices[VertexId].m_Position;
00530                                 Vertex.Color    = KRGBA2INT( m_pVertices[VertexId].m_Color );
00531                                 Vertex.tu1              = m_pVertices[VertexId].m_UV[0].x;
00532                                 Vertex.tv1              = m_pVertices[VertexId].m_UV[0].y;
00533                                 Vertex.tu2              = m_pVertices[VertexId].m_UV[1].x;
00534                                 Vertex.tv2              = m_pVertices[VertexId].m_UV[1].y;
00535 
00536                                 // Verifie si le vertex est deja dans la liste
00537                                 // Dans ce cas on ne le duplique pas
00538                                 KINDEX  Index = -1;
00539                                 for( u32 i = 0; i < nVertex; i ++ )
00540                                 {
00541                                         if( pTmpVertex[i] == Vertex )
00542                                         {
00543                                                 Index = (KINDEX)i;
00544                                                 break;
00545                                         }
00546                                 }
00547 
00548                                 if( Index != (KINDEX)-1 )
00549                                 {
00550                                         // Index deja dans la liste
00551                                         m_pRenderFaces[Face].m_pIndices[nIndex] = Index;
00552                                         nIndex  ++;
00553                                 }
00554                                 else
00555                                 {
00556                                         pTmpVertex[nVertex]     = Vertex;
00557                                         m_pRenderFaces[Face].m_pIndices[nIndex] = nVertex;
00558                                         nVertex ++;
00559                                         nIndex  ++;
00560                                 }
00561                         }
00562 
00563                         KASSERT( nVertex );
00564 
00565                         m_pRenderFaces[Face].m_pVertex  = new KLVertex2[nVertex];
00566                         memcpy( m_pRenderFaces[Face].m_pVertex, pTmpVertex, sizeof( KLVertex2 ) * nVertex );
00567                         m_pRenderFaces[Face].m_nVertex  = nVertex;
00568 
00569                         Deletep( pTmpVertex );
00570                 }
00571         }
00572 
00573         Deletev( pComputedFaces );
00574 }
00575 
00576 //--------------------------------------------------------------------------
00577 s32 KBspDraw::LoadShaders( KRender* pRender )
00578 {
00579         KASSERT( !m_pShaderId );
00580         
00581         if( !m_nShaders )
00582                 return BSP_OK;
00583         
00584         m_pShaderId = new KSHADER[m_nShaders];
00585         for( u32 s = 0; s < m_nShaders; s ++ )
00586                 m_pShaderId[s] = KSHADER_NO;
00587 
00588 /*
00589         for( u32 s = 0; s < (u32)GetnShaders(); s ++ )
00590         {
00591                 char*   pShaderName = GetpShaders( s )->m_pName;
00592                 
00593                 // Enleve le repertoire textures/ rajouté par q3radiant
00594                 char*   pTextureDir = "textures/";
00595                 if( strnicmp( pShaderName, pTextureDir, strlen( pTextureDir ) ) == 0 )
00596                         pShaderName += strlen( pTextureDir );
00597 
00598                 m_pShaderId[s] = pRender->GetpShaderBank()->LoadShader( pShaderName );
00599                 
00600                 if( m_pShaderId[s] == KSHADER_NO )
00601                         g_Console << "Could not load Shader : " << GetpShaders( s )->m_pName << KENDL;
00602         }
00603 */
00604         return BSP_OK;
00605 }
00606 
00607 //--------------------------------------------------------------------------
00608 s32 KBspDraw::LoadShader( KRender* pRender )
00609 {
00610         if( m_CurrentLoadedShader < GetnShaders() )
00611         {
00612                 char*   pShaderName = GetpShaders( m_CurrentLoadedShader )->m_pName;
00613                 
00614                 // Enleve le repertoire textures/ rajouté par q3radiant
00615                 char*   pTextureDir = "textures/";
00616                 if( strnicmp( pShaderName, pTextureDir, strlen( pTextureDir ) ) == 0 )
00617                         pShaderName += strlen( pTextureDir );
00618 
00619                 m_pShaderId[m_CurrentLoadedShader] = pRender->GetpShaderBank()->LoadShader( pShaderName );
00620                 
00621                 if( m_pShaderId[m_CurrentLoadedShader] == KSHADER_NO )
00622                         g_Console << "Could not load Shader : " << GetpShaders( m_CurrentLoadedShader )->m_pName << KENDL;
00623 
00624                 m_CurrentLoadedShader ++;
00625         }
00626 
00627         return m_CurrentLoadedShader;
00628 }
00629 
00630 //--------------------------------------------------------------------------
00631 s32 KBspDraw::LoadLightMaps( KRender* pRender )
00632 {
00633         KASSERT( !m_pLightMapId );
00634 
00635         float   OverBright = 5.0f;
00636         float   BaseBright = 0.0f;
00637         
00638         if( !m_nLightMaps )
00639                 return BSP_OK;
00640         
00641         m_pLightMapId = new KTEXTURE[m_nLightMaps];
00642 
00643         u32             LightMap;
00644 
00645         g_Console << KENDL;
00646         
00647         for( LightMap = 0; LightMap < m_nLightMaps; LightMap ++ )
00648         {
00649                 KTEXTURE        TexId;
00650                 
00651                 TexId = pRender->GetpTextureBank()->CreateTexture( 128, 128 );
00652                 if( TexId == KTEXTURE_NO )
00653                 {
00654                         KError::Error( pRender->GetWindow(), "KBspLoader::LoadLightMaps(...) : Cannot create lightmap" );
00655                         TexId = pRender->GetpTextureBank()->GetErrorTextureId();
00656                 }
00657 
00658                 KTexture*       pTexture = pRender->GetpTextureBank()->GetpTexture( TexId );
00659                 u32                     x, y;
00660                 KRgba           Pixel;
00661                 char            pFileName[256];
00662 
00663                 sprintf( pFileName, "$lightmap%i", LightMap );
00664                 pTexture->SetpFileName( pFileName );
00665 
00666                 pTexture->Lock();
00667                 for( y = 0; y < 128; y ++ )
00668                 {
00669                         for( x = 0; x < 128; x ++ )
00670                         {
00671                                 float Scale = 1.0f;
00672                                 float TmpScale;
00673 
00674                                 float r = (float) m_pLightMaps[LightMap].m_Color[y][x].r;
00675                                 float g = (float) m_pLightMaps[LightMap].m_Color[y][x].g;
00676                                 float b = (float) m_pLightMaps[LightMap].m_Color[y][x].b;
00677                                 
00678                                 r = r * OverBright / 255.0f;
00679                                 g = g * OverBright / 255.0f;
00680                                 b = b * OverBright / 255.0f;
00681                                 
00682                                 if( r > 1.0f && ( TmpScale = ( 1.0f / r ) ) < Scale) Scale = TmpScale;
00683                                 if( g > 1.0f && ( TmpScale = ( 1.0f / g ) ) < Scale) Scale = TmpScale;
00684                                 if( b > 1.0f && ( TmpScale = ( 1.0f / b ) ) < Scale) Scale = TmpScale;
00685 
00686                                 Scale *= 255.0f;
00687                                 r *= Scale;
00688                                 g *= Scale;
00689                                 b *= Scale;
00690 
00691                                 Pixel.b = (u8)b;
00692                                 Pixel.g = (u8)g;
00693                                 Pixel.r = (u8)r;
00694                                 Pixel.a = 255;
00695 
00696                                 pTexture->SetPixel( x, y, Pixel.GetColor() );
00697 /*
00698                                 pPixel  = &pBits[ ((y * 128) + x) * (Depth / 8)];
00699                                 
00700                                 if( Depth == 16 )
00701                                         *((u16*)pPixel) = (u16)( ((Pixel.a / 16 ) << 12) | ((Pixel.b / 16) << 8) | ((Pixel.g / 16) << 4) | (Pixel.r / 16) );
00702                                 if( Depth == 32 )
00703                                 {
00704                                         pPixel[0] = Pixel.r;
00705                                         pPixel[1] = Pixel.g;
00706                                         pPixel[2] = Pixel.b;
00707                                         pPixel[3] = Pixel.a;
00708                                 }*/
00709                         }
00710                 }
00711                 pTexture->Unlock();
00712 
00713                 m_pLightMapId[LightMap] = TexId;        
00714         }
00715 
00716         return BSP_OK;
00717 }
00718 
00719 //--------------------------------------------------------------------------
00720 void KBspDraw::DestroyShaders()
00721 {
00722         for( u32 s = 0; s < (u32)GetnShaders(); s ++ )
00723         {
00724                 m_pRender->GetpShaderBank()->UnloadShader( m_pShaderId[s] );
00725         }
00726         
00727         if( m_pShaderId )
00728                 Deletev( m_pShaderId );
00729 }
00730 
00731 //--------------------------------------------------------------------------
00732 void KBspDraw::DestroyLightMaps()
00733 {
00734         if( m_pLightMapId )
00735         {
00736                 for( u32 i = 0; i < m_nLightMaps; i ++ )
00737                 {
00738                         m_pRender->GetpTextureBank()->UnloadTexture( m_pLightMapId[i] );
00739                 }
00740         }
00741 
00742         if( m_pLightMapId )
00743                 Deletev( m_pLightMapId );
00744 }
00745 
00746 

Generated on Sun Mar 25 20:02:10 2007 for Zythum Project by  doxygen 1.5.1-p1