Main Page | Alphabetical List | Compound List | File List | Compound Members | File Members

Pcache.cpp

Go to the documentation of this file.
00001 /****************************************************************************************/
00002 /*  PCache.cpp                                                                          */
00003 /*                                                                                      */
00004 /*  Author: John Pollard                                                                */
00005 /*  Description: D3D poly cache                                                         */
00006 /*                                                                                      */
00007 /*  The contents of this file are subject to the Genesis3D Public License               */
00008 /*  Version 1.01 (the "License"); you may not use this file except in                   */
00009 /*  compliance with the License. You may obtain a copy of the License at                */
00010 /*  http://www.genesis3d.com                                                            */
00011 /*                                                                                      */
00012 /*  Software distributed under the License is distributed on an "AS IS"                 */
00013 /*  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See                */
00014 /*  the License for the specific language governing rights and limitations              */
00015 /*  under the License.                                                                  */
00016 /*                                                                                      */
00017 /*  The Original Code is Genesis3D, released March 25, 1999.                            */
00018 /*Genesis3D Version 1.1 released November 15, 1999                            */
00019 /*  Copyright (C) 1999 WildTangent, Inc. All Rights Reserved           */
00020 /*                                                                                      */
00021 /****************************************************************************************/
00022 #include <Windows.h>
00023 #include <stdio.h>
00024 
00025 #include "D3DCache.h"
00026 #include "D3D_Fx.h"
00027 
00028 #include "PCache.h"
00029 #include "D3DDrv.h"
00030 #include "THandle.h"
00031 #include "D3D_Err.h"
00032 
00033 
00034 
00035 
00036 
00037 
00038 
00039 //====================================================================================
00040 //      Local static variables
00041 //====================================================================================
00042 
00043 DRV_CacheInfo                                           CacheInfo;
00044 
00045 //
00046 //      World Cache
00047 //
00048 
00049 
00050 #define MAX_WORLD_POLYS                         256
00051 #define MAX_WORLD_POLY_VERTS            1024
00052 
00053 #define MAX_MISC_POLYS                          256
00054 #define MAX_MISC_POLY_VERTS                     1024
00055 
00056 
00057 typedef struct
00058 {
00059         float           u;
00060         float           v;
00061         uint32          Color;
00062 } PCache_TVert;
00063 
00064 typedef struct
00065 {
00066         geRDriver_THandle       *THandle;
00067 
00068         DRV_LInfo       *LInfo;                                         // Original pointer to linfo
00069         uint32          Flags;                                          // Flags for this poly
00070         float           ShiftU;
00071         float           ShiftV;
00072         float           ScaleU;
00073         float           ScaleV;
00074         int32           MipLevel;
00075         uint32          SortKey;
00076         int32           FirstVert;
00077         int32           NumVerts;
00078 } World_Poly;
00079 
00080 #define MAX_TEXTURE_STAGES              2                       // Up to 2 tmu's (stages)
00081 
00082 // Verts we defined in the D3D flexible vertex format (FVF)
00083 // This is a transformed and lit vertex definition, with up to 8 sets of uvs
00084 typedef struct
00085 {
00086         float                   u,v;
00087 } PCache_UVSet;
00088 
00089 typedef struct
00090 {
00091         float                   x,y,z;                                  // Screen x, y, z
00092         float                   rhw;                                    // homogenous w
00093         DWORD                   color;                                  // color
00094         DWORD                   specular;
00095         PCache_UVSet    uv[MAX_TEXTURE_STAGES]; // uv sets for each stage
00096 } PCache_Vert;
00097 
00098 typedef struct
00099 {
00100         World_Poly              Polys[MAX_WORLD_POLYS];
00101         World_Poly              *SortedPolys[MAX_WORLD_POLYS];
00102         World_Poly              *SortedPolys2[MAX_WORLD_POLYS];
00103         PCache_Vert             Verts[MAX_WORLD_POLY_VERTS];
00104 
00105         PCache_TVert    TVerts[MAX_WORLD_POLY_VERTS];           // Original uv
00106 
00107         int32                   NumPolys;
00108         int32                   NumPolys2;
00109         int32                   NumVerts;
00110 } World_Cache;
00111 
00112 static World_Cache              WorldCache;
00113 
00114 #define PREP_WORLD_VERTS_NORMAL                 1                               // Prep verts as normal
00115 #define PREP_WORLD_VERTS_LMAP                   2                               // Prep verts as lightmaps
00116 #define PREP_WORLD_VERTS_SINGLE_PASS    3                               // Prep verts for a single pass
00117 
00118 #define RENDER_WORLD_POLYS_NORMAL               1                               // Render polys as normal
00119 #define RENDER_WORLD_POLYS_LMAP                 2                               // Render polys as lightmaps
00120 #define RENDER_WORLD_POLYS_SINGLE_PASS  3
00121 
00122 //
00123 //      Misc cache
00124 //
00125 
00126 typedef struct
00127 {
00128         geRDriver_THandle       *THandle;
00129         uint32          Flags;                                          // Flags for this poly
00130         int32           MipLevel;
00131         int32           FirstVert;
00132         int32           NumVerts;
00133 
00134         uint32          SortKey;
00135 } Misc_Poly;
00136 
00137 typedef struct
00138 {
00139         Misc_Poly               Polys[MAX_MISC_POLYS];
00140         Misc_Poly               *SortedPolys[MAX_MISC_POLYS];
00141         PCache_Vert             Verts[MAX_MISC_POLY_VERTS];
00142         //float                 ZVert[MAX_MISC_POLY_VERTS];
00143 
00144         int32                   NumPolys;
00145         int32                   NumVerts;
00146 } Misc_Cache;
00147 
00148 static Misc_Cache               MiscCache;
00149 
00150 //====================================================================================
00151 //      Local static functions prototypes
00152 //====================================================================================
00153 geBoolean World_PolyPrepVerts(World_Poly *pPoly, int32 PrepMode, int32 Stage1, int32 Stage2);
00154 
00155 static BOOL RenderWorldPolys(int32 RenderMode);
00156 static BOOL ClearWorldCache(void);
00157 static int32 GetMipLevel(DRV_TLVertex *Verts, int32 NumVerts, float ScaleU, float ScaleV, int32 MaxMipLevel);
00158 
00159 #include <Math.h>
00160 
00161 //====================================================================================
00162 //      PCache_InsertWorldPoly
00163 //====================================================================================
00164 BOOL PCache_InsertWorldPoly(DRV_TLVertex *Verts, int32 NumVerts, geRDriver_THandle *THandle, DRV_TexInfo *TexInfo, DRV_LInfo *LInfo, uint32 Flags)
00165 {
00166         int32                   Mip;
00167         float                   ZRecip, DrawScaleU, DrawScaleV;
00168         World_Poly              *pCachePoly;
00169         DRV_TLVertex    *pVerts;
00170         PCache_TVert    *pTVerts;
00171         PCache_Vert             *pD3DVerts;
00172         int32                   i;
00173         uint32                  Alpha;
00174 
00175 
00176         if ((WorldCache.NumVerts + NumVerts) >= MAX_WORLD_POLY_VERTS)
00177         {
00178                 // If the cache is full, we must flush it before going on...
00179                 if (!PCache_FlushWorldPolys())
00180                         return GE_FALSE;
00181         }
00182         else if (WorldCache.NumPolys+1 >= MAX_WORLD_POLYS)
00183         {
00184                 // If the cache is full, we must flush it before going on...
00185                 if (!PCache_FlushWorldPolys())
00186                         return GE_FALSE;
00187         }
00188 
00189         DrawScaleU = 1.0f / TexInfo->DrawScaleU;
00190         DrawScaleV = 1.0f / TexInfo->DrawScaleV;
00191 
00192         Mip = GetMipLevel(Verts, NumVerts, DrawScaleU, DrawScaleV, THandle->NumMipLevels-1);
00193 
00194         // Get a pointer to the original polys verts
00195         pVerts = Verts;
00196         
00197         // Store info about this poly in the cache
00198         pCachePoly = &WorldCache.Polys[WorldCache.NumPolys];
00199 
00200         pCachePoly->THandle = THandle;
00201         pCachePoly->LInfo = LInfo;
00202         pCachePoly->Flags = Flags;
00203         pCachePoly->FirstVert = WorldCache.NumVerts;
00204         pCachePoly->NumVerts = NumVerts;
00205         pCachePoly->ShiftU = TexInfo->ShiftU;
00206         pCachePoly->ShiftV = TexInfo->ShiftV;
00207         pCachePoly->ScaleU = DrawScaleU;
00208         pCachePoly->ScaleV = DrawScaleV;
00209         pCachePoly->MipLevel = Mip;
00210 
00211         // Don't forget the sort key:
00212         pCachePoly->SortKey = ((THandle - TextureHandles)<<4)+Mip;
00213 
00214         // Get a pointer into the world verts
00215         pD3DVerts = &WorldCache.Verts[WorldCache.NumVerts];
00216         pTVerts = &WorldCache.TVerts[WorldCache.NumVerts];
00217 
00218         if (Flags & DRV_RENDER_ALPHA)
00219                 Alpha = (uint32)pVerts->a<<24;
00220         else
00221                 Alpha = (uint32)(255<<24);
00222 
00223         for (i=0; i< NumVerts; i++)
00224         {
00225                 ZRecip = 1.0f/(pVerts->z);
00226 
00227                 pD3DVerts->x = pVerts->x;
00228                 pD3DVerts->y = pVerts->y;
00229 
00230                 pD3DVerts->z = (1.0f - ZRecip); // ZBUFFER
00231                 pD3DVerts->rhw = ZRecip;
00232 
00233                 if (AppInfo.FogEnable && !(Flags & DRV_RENDER_POLY_NO_FOG)) // poly fog
00234                 {
00235                         DWORD   FogVal;
00236                         float   Val;
00237 
00238                         Val = pVerts->z;
00239 
00240                         if (Val > AppInfo.FogEnd)
00241                                 Val = AppInfo.FogEnd;
00242 
00243                         FogVal = (DWORD)((AppInfo.FogEnd-Val)/(AppInfo.FogEnd-AppInfo.FogStart)*255.0f);
00244                 
00245                         if (FogVal < 0)
00246                                 FogVal = 0;
00247                         else if (FogVal > 255)
00248                                 FogVal = 255;
00249                 
00250                         pD3DVerts->specular = (FogVal<<24);             // Alpha component in specular is the fog value (0...255)
00251                 }
00252                 else
00253                         pD3DVerts->specular = 0;
00254                 
00255                 // Store the uv's so the prep pass can use them...
00256                 pTVerts->u = pVerts->u;
00257                 pTVerts->v = pVerts->v;
00258 
00259                 pTVerts->Color = Alpha | ((uint32)pVerts->r<<16) | ((uint32)pVerts->g<<8) | (uint32)pVerts->b;
00260 
00261                 pTVerts++;
00262                 pVerts++;        
00263                 pD3DVerts++;
00264 
00265         }
00266         
00267         // Update globals about the world poly cache
00268         WorldCache.NumVerts += NumVerts;
00269         WorldCache.NumPolys++;
00270 
00271         return TRUE;
00272 }
00273 
00274 //====================================================================================
00275 //      PCache_FlushWorldPolys
00276 //====================================================================================
00277 BOOL PCache_FlushWorldPolys(void)
00278 {
00279         if (!WorldCache.NumPolys)
00280                 return TRUE;
00281 
00282         if (!THandle_CheckCache())
00283                 return GE_FALSE;
00284         
00285         if (AppInfo.CanDoMultiTexture)
00286         {
00287                 RenderWorldPolys(RENDER_WORLD_POLYS_SINGLE_PASS);
00288         }
00289         else
00290         {
00291                 // Render them as normal
00292                 if (!RenderWorldPolys(RENDER_WORLD_POLYS_NORMAL))
00293                         return GE_FALSE;
00294 
00295                 // Render them as lmaps
00296                 RenderWorldPolys(RENDER_WORLD_POLYS_LMAP);
00297         }
00298 
00299         ClearWorldCache();
00300 
00301         return TRUE;
00302 }
00303 
00304 //====================================================================================
00305 //====================================================================================
00306 static int MiscBitmapHandleComp(const void *a, const void *b)
00307 {
00308         uint32  Id1, Id2;
00309 
00310         Id1 = (uint32)(*(Misc_Poly**)a)->SortKey;
00311         Id2 = (uint32)(*(Misc_Poly**)b)->SortKey;
00312 
00313         if ( Id1 == Id2)
00314                 return 0;
00315 
00316         if (Id1 < Id2)
00317                 return -1;
00318 
00319         return 1;
00320 }
00321 
00322 //====================================================================================
00323 //====================================================================================
00324 static void SortMiscPolysByHandle(void)
00325 {
00326         Misc_Poly       *pPoly;
00327         int32           i;
00328 
00329         pPoly = MiscCache.Polys;
00330 
00331         for (i=0; i<MiscCache.NumPolys; i++)
00332         {
00333                 MiscCache.SortedPolys[i] = pPoly;
00334                 pPoly++;
00335         }
00336         
00337         // Sort the polys
00338         qsort(&MiscCache.SortedPolys, MiscCache.NumPolys, sizeof(MiscCache.SortedPolys[0]), MiscBitmapHandleComp);
00339 }
00340 
00341 //=====================================================================================
00342 //      FillLMapSurface
00343 //=====================================================================================
00344 static void FillLMapSurface(DRV_LInfo *LInfo, int32 LNum)
00345 {
00346         U16                                     *pTempBits;
00347         int32                           w, h, Width, Height, Size;
00348         U8                                      *pBitPtr;
00349         RGB_LUT                         *Lut;
00350         geRDriver_THandle       *THandle;
00351         int32                           Extra;
00352 
00353         THandle = LInfo->THandle;
00354 
00355         pBitPtr = (U8*)LInfo->RGBLight[LNum];
00356 
00357         Width = LInfo->Width;
00358         Height = LInfo->Height;
00359         Size = 1<<THandle->Log;
00360 
00361         Lut = &AppInfo.Lut1;
00362 
00363         THandle_Lock(THandle, 0, (void**)&pTempBits);
00364 
00365         Extra = Size - Width;
00366 
00367         for (h=0; h< Height; h++)
00368         {
00369                 for (w=0; w< Width; w++)
00370                 {
00371                         U8      R, G, B;
00372                         U16     Color;
00373                         R = *pBitPtr++;
00374                         G = *pBitPtr++;
00375                         B =  *pBitPtr++;
00376                         
00377                         Color = (U16)(Lut->R[R] | Lut->G[G] | Lut->B[B]);
00378 
00379                         *pTempBits++ = Color;
00380                 }
00381                 pTempBits += Extra;
00382         }
00383 
00384         THandle_UnLock(THandle, 0);
00385 }
00386 
00387 
00388 //=====================================================================================
00389 //      LoadLMapFromSystem
00390 //=====================================================================================
00391 static void LoadLMapFromSystem(DRV_LInfo *LInfo, int32 Log, int32 LNum)
00392 {
00393         U16                                     *pTempBits;
00394         int32                           w, h, Width, Height, Size, Extra;
00395         U8                                      *pBitPtr;
00396         LPDIRECTDRAWSURFACE4 Surface;
00397         RGB_LUT                         *Lut;
00398     DDSURFACEDESC2              ddsd;
00399     HRESULT                             ddrval;
00400 
00401         pBitPtr = (U8*)LInfo->RGBLight[LNum];
00402 
00403         Width = LInfo->Width;
00404         Height = LInfo->Height;
00405         Size = 1<<Log;
00406 
00407         Extra = Size - Width;
00408 
00409         Lut = &AppInfo.Lut1;
00410 
00411         Surface = SystemToVideo[Log].Surface;
00412 
00413     memset(&ddsd, 0, sizeof(DDSURFACEDESC2));
00414     ddsd.dwSize = sizeof(DDSURFACEDESC2);
00415     ddrval = Surface->Lock(NULL, &ddsd, DDLOCK_WAIT, NULL);
00416 
00417         pTempBits = (USHORT*)ddsd.lpSurface;
00418 
00419         for (h=0; h< Height; h++)
00420         {
00421                 for (w=0; w< Width; w++)
00422                 {
00423                         U8      R, G, B;
00424                         U16     Color;
00425                         R = *pBitPtr++;
00426                         G = *pBitPtr++;
00427                         B =  *pBitPtr++;
00428                         
00429                         Color = (U16)(Lut->R[R] | Lut->G[G] | Lut->B[B]);
00430                         
00431                         *pTempBits++ = Color;
00432                 }
00433                 pTempBits += Extra;
00434         }
00435 
00436     ddrval = Surface->Unlock(NULL);
00437 }
00438 
00439 static BOOL IsKeyDown(int KeyCode)
00440 {
00441         if (GetAsyncKeyState(KeyCode) & 0x8000)
00442                 return TRUE;
00443 
00444         return FALSE;
00445 }
00446 
00447 extern uint32 CurrentLRU;
00448 
00449 //=====================================================================================
00450 //      SetupMipData
00451 //=====================================================================================
00452 geBoolean SetupMipData(THandle_MipData *MipData)
00453 {
00454         if (!MipData->Slot || D3DCache_SlotGetUserData(MipData->Slot) != MipData)
00455         {
00456                 MipData->Slot = D3DCache_TypeFindSlot(MipData->CacheType);
00457 
00458                 D3DCache_SlotSetUserData(MipData->Slot, MipData);
00459 
00460                 AppInfo.lpD3DDevice->SetRenderState(D3DRENDERSTATE_FLUSHBATCH, 0);
00461 
00462                 return GE_FALSE;
00463         }
00464 
00465         return GE_TRUE;
00466 }
00467 
00468 //=====================================================================================
00469 //      SetupLMap
00470 //=====================================================================================
00471 geBoolean SetupLMap(int32 Stage, DRV_LInfo *LInfo, int32 LNum, geBoolean Dynamic)
00472 {
00473         geRDriver_THandle       *THandle;
00474         THandle_MipData         *MipData;
00475 
00476         THandle = LInfo->THandle;
00477         MipData = &THandle->MipData[0];
00478 
00479         if (Dynamic)
00480                 MipData->Flags |= THANDLE_UPDATE;
00481 
00482         if (!SetupMipData(MipData))
00483         {
00484                 MipData->Flags |= THANDLE_UPDATE;               // Force an upload
00485                 CacheInfo.LMapMisses++;
00486         }
00487 
00488         if (MipData->Flags & THANDLE_UPDATE)
00489         {
00490                 HRESULT                                 Error;
00491                 LPDIRECTDRAWSURFACE4    Surface;
00492 
00493                 Surface = D3DCache_SlotGetSurface(MipData->Slot);
00494 
00495                 LoadLMapFromSystem(LInfo, THandle->Log, LNum);
00496 
00497                 Error = Surface->BltFast(0, 0, SystemToVideo[THandle->Log].Surface, NULL, DDBLTFAST_WAIT);
00498                 
00499                 if (Error != DD_OK)
00500                 {
00501                         if(Error==DDERR_SURFACELOST)
00502                         {
00503                                 if (!D3DMain_RestoreAllSurfaces())
00504                                         return GE_FALSE;
00505                         }
00506                         else
00507                         {
00508                                 D3DMain_Log("SetupTexture: System to Video cache Blt failed.\n %s", D3DErrorToString(Error));
00509                                 return GE_FALSE;
00510                         }
00511                 }
00512         }
00513 
00514         if (Dynamic)            // If it was dynmamic, force an update for one more frame
00515                 MipData->Flags |= THANDLE_UPDATE;
00516         else
00517                 MipData->Flags &= ~THANDLE_UPDATE;
00518 
00519         D3DCache_SlotSetLRU(MipData->Slot, CurrentLRU);
00520         D3DSetTexture(Stage, D3DCache_SlotGetTexture(MipData->Slot));
00521 
00522         return GE_TRUE;
00523 }
00524 
00525 //=====================================================================================
00526 //      SetupTexture
00527 //=====================================================================================
00528 geBoolean SetupTexture(int32 Stage, geRDriver_THandle *THandle, int32 MipLevel)
00529 {
00530         THandle_MipData         *MipData;
00531 
00532         MipData = &THandle->MipData[MipLevel];
00533         
00534         if (!SetupMipData(MipData))
00535         {
00536                 MipData->Flags |= THANDLE_UPDATE;               // Force an upload
00537                 CacheInfo.TexMisses++;
00538         }
00539 
00540         if (MipData->Flags & THANDLE_UPDATE)
00541         {
00542                 HRESULT                                 Error;
00543                 LPDIRECTDRAWSURFACE4    Surface;
00544 
00545                 Surface = D3DCache_SlotGetSurface(MipData->Slot);
00546 
00547                 Error = Surface->BltFast(0, 0, MipData->Surface, NULL, DDBLTFAST_WAIT);
00548 
00549                 if (Error != DD_OK)
00550                 {
00551                         if(Error==DDERR_SURFACELOST)
00552                         {
00553                                 if (!D3DMain_RestoreAllSurfaces())
00554                                         return FALSE;
00555                         }
00556                         else
00557                         {
00558                                 D3DMain_Log("SetupTexture: System to Video cache Blt failed.\n %s", D3DErrorToString(Error));
00559                                 return GE_FALSE;
00560                         }
00561                 }
00562         }
00563 
00564         MipData->Flags &= ~THANDLE_UPDATE;
00565 
00566         D3DCache_SlotSetLRU(MipData->Slot, CurrentLRU);
00567         D3DSetTexture(Stage, D3DCache_SlotGetTexture(MipData->Slot));
00568 
00569         return GE_TRUE;
00570 }
00571 
00572 //====================================================================================
00573 //      PCache_FlushMiscPolys
00574 //====================================================================================
00575 BOOL PCache_FlushMiscPolys(void)
00576 {
00577         int32                           i;
00578         Misc_Poly                       *pPoly;
00579 
00580         if (!MiscCache.NumPolys)
00581                 return TRUE;
00582 
00583         if (!THandle_CheckCache())
00584                 return GE_FALSE;
00585 
00586         // Set the render states
00587         if (AppInfo.CanDoMultiTexture)
00588         {
00589                 AppInfo.lpD3DDevice->SetTextureStageState( 1, D3DTSS_COLOROP,   D3DTOP_DISABLE );
00590                 AppInfo.lpD3DDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );
00591                 D3DSetTexture(1, NULL);         // Reset texture stage 1
00592         }
00593         
00594         AppInfo.lpD3DDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0);
00595 
00596         AppInfo.lpD3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
00597         AppInfo.lpD3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
00598         AppInfo.lpD3DDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
00599         AppInfo.lpD3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
00600         AppInfo.lpD3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
00601         AppInfo.lpD3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_MODULATE);
00602 
00603         D3DBlendFunc (D3DBLEND_SRCALPHA, D3DBLEND_INVSRCALPHA);
00604         D3DBlendEnable(TRUE);
00605 
00606         // Sort the polys by handle
00607         SortMiscPolysByHandle();
00608 
00609         for (i=0; i< MiscCache.NumPolys; i++)
00610         {
00611                 pPoly = MiscCache.SortedPolys[i];
00612 
00613                 if (pPoly->Flags & DRV_RENDER_NO_ZMASK)         // We are assuming that this is not going to change all that much
00614                         D3DZEnable(FALSE);
00615                 else
00616                         D3DZEnable(TRUE);
00617 
00618                 if (pPoly->Flags & DRV_RENDER_NO_ZWRITE)        // We are assuming that this is not going to change all that much
00619                         D3DZWriteEnable(FALSE); 
00620                 else
00621                         D3DZWriteEnable(TRUE);
00622                                                                           
00623                 if (pPoly->Flags & DRV_RENDER_CLAMP_UV)
00624                         D3DTexWrap(0, FALSE);
00625                 else
00626                         D3DTexWrap(0, TRUE);
00627 
00628                 if (!SetupTexture(0, pPoly->THandle, pPoly->MipLevel))
00629                         return GE_FALSE;
00630 
00631                 if ( (pPoly->Flags & DRV_RENDER_POLY_NO_FOG) && AppInfo.FogEnable) // poly fog
00632                         AppInfo.lpD3DDevice->SetRenderState(D3DRENDERSTATE_FOGENABLE , FALSE);
00633 
00634                 D3DTexturedPoly(&MiscCache.Verts[pPoly->FirstVert], pPoly->NumVerts);
00635 
00636                 if ( (pPoly->Flags & DRV_RENDER_POLY_NO_FOG) && AppInfo.FogEnable) // poly fog
00637                         AppInfo.lpD3DDevice->SetRenderState(D3DRENDERSTATE_FOGENABLE , TRUE);
00638         }
00639 
00640         // Turn z stuff back on...
00641         D3DZWriteEnable (TRUE);
00642         D3DZEnable(TRUE);
00643         
00644         MiscCache.NumPolys = 0;
00645         MiscCache.NumVerts = 0;
00646 
00647         AppInfo.lpD3DDevice->SetRenderState(D3DRENDERSTATE_FLUSHBATCH, 0);
00648         AppInfo.lpD3DDevice->EndScene();
00649         AppInfo.lpD3DDevice->BeginScene();
00650 
00651         return TRUE;
00652 }
00653 
00654 //====================================================================================
00655 //      PCache_InsertMiscPoly
00656 //====================================================================================
00657 BOOL PCache_InsertMiscPoly(DRV_TLVertex *Verts, int32 NumVerts, geRDriver_THandle *THandle, uint32 Flags)
00658 {
00659         int32                   Mip;
00660         float                   ZRecip, u, v, ScaleU, ScaleV, InvScale;
00661         Misc_Poly               *pCachePoly;
00662         DRV_TLVertex    *pVerts;
00663         PCache_Vert             *pD3DVerts;
00664         int32                   i, SAlpha;
00665 
00666         if ((MiscCache.NumVerts + NumVerts) >= MAX_MISC_POLY_VERTS)
00667         {
00668                 // If the cache is full, we must flush it before going on...
00669                 PCache_FlushMiscPolys();
00670         }
00671         else if (MiscCache.NumPolys+1 >= MAX_MISC_POLYS)
00672         {
00673                 // If the cache is full, we must flush it before going on...
00674                 PCache_FlushMiscPolys();
00675         }
00676 
00677         Mip = GetMipLevel(Verts, NumVerts, (float)THandle->Width, (float)THandle->Height, THandle->NumMipLevels-1);
00678 
00679         // Store info about this poly in the cache
00680         pCachePoly = &MiscCache.Polys[MiscCache.NumPolys];
00681 
00682         pCachePoly->THandle = THandle;
00683         pCachePoly->Flags = Flags;
00684         pCachePoly->FirstVert = MiscCache.NumVerts;
00685         pCachePoly->NumVerts = NumVerts;
00686         pCachePoly->MipLevel = Mip;
00687         pCachePoly->SortKey = ((THandle - TextureHandles)<<4)+Mip;
00688 
00689         // Get scale value for vertices
00690         //TCache_GetUVInvScale(Bitmap, Mip, &InvScale);
00691         InvScale = 1.0f / (float)((1<<THandle->Log));
00692 
00693         // Convert them to take account that the vertices are allready from 0 to 1
00694         ScaleU = (float)THandle->Width * InvScale;
00695         ScaleV = (float)THandle->Height * InvScale;
00696 
00697         // Precompute the alpha value...
00698         SAlpha = ((int32)Verts->a)<<24;
00699 
00700         // Get a pointer to the original polys verts
00701         pVerts = Verts;
00702         // Get a pointer into the world verts
00703         pD3DVerts = &MiscCache.Verts[MiscCache.NumVerts];
00704 
00705         for (i=0; i< NumVerts; i++)
00706         {
00707                 ZRecip = 1/(pVerts->z);
00708 
00709                 pD3DVerts->x = pVerts->x;
00710                 pD3DVerts->y = pVerts->y;
00711 
00712                 pD3DVerts->z = (1.0f - ZRecip);         // ZBUFFER
00713                 pD3DVerts->rhw = ZRecip;
00714                 
00715                 u = pVerts->u * ScaleU;
00716                 v = pVerts->v * ScaleV;
00717 
00718                 pD3DVerts->uv[0].u = u;
00719                 pD3DVerts->uv[0].v = v;
00720 
00721                 pD3DVerts->color = SAlpha | ((int32)pVerts->r<<16) | ((int32)pVerts->g<<8) | (int32)pVerts->b;
00722 
00723                 if (AppInfo.FogEnable && !(Flags & DRV_RENDER_POLY_NO_FOG) ) // poly fog
00724                 {
00725                         DWORD   FogVal;
00726                         float   Val;
00727 
00728                         Val = pVerts->z;
00729 
00730                         if (Val > AppInfo.FogEnd)
00731                                 Val = AppInfo.FogEnd;
00732 
00733                         FogVal = (DWORD)((AppInfo.FogEnd-Val)/(AppInfo.FogEnd-AppInfo.FogStart)*255.0f);
00734                 
00735                         if (FogVal < 0)
00736                                 FogVal = 0;
00737                         else if (FogVal > 255)
00738                                 FogVal = 255;
00739                 
00740                         pD3DVerts->specular = (FogVal<<24);             // Alpha component in specular is the fog value (0...255)
00741                 }
00742                 else
00743                         pD3DVerts->specular = 0;
00744 
00745                 pVerts++;
00746                 pD3DVerts++;
00747         }
00748         
00749         // Update globals about the misc poly cache
00750         MiscCache.NumVerts += NumVerts;
00751         MiscCache.NumPolys++;
00752 
00753         return TRUE;
00754 }
00755 
00756 //====================================================================================
00757 //      **** LOCAL STATIC FUNCTIONS *****
00758 //====================================================================================
00759 
00760 //====================================================================================
00761 //      World_PolyPrepVerts
00762 //====================================================================================
00763 geBoolean World_PolyPrepVerts(World_Poly *pPoly, int32 PrepMode, int32 Stage1, int32 Stage2)
00764 {
00765         float                   InvScale, u, v;
00766         PCache_TVert    *pTVerts;
00767         PCache_Vert             *pVerts;
00768         float                   ShiftU, ShiftV, ScaleU, ScaleV;
00769         int32                   j;
00770 
00771         switch (PrepMode)
00772         {
00773                 case PREP_WORLD_VERTS_NORMAL:
00774                 {
00775                         pTVerts = &WorldCache.TVerts[pPoly->FirstVert];
00776 
00777                         ShiftU = pPoly->ShiftU;
00778                         ShiftV = pPoly->ShiftV;
00779                         ScaleU = pPoly->ScaleU;
00780                         ScaleV = pPoly->ScaleV;
00781 
00782                         // Get scale value for vertices
00783                         InvScale = 1.0f / (float)((1<<pPoly->THandle->Log));
00784 
00785                         pVerts = &WorldCache.Verts[pPoly->FirstVert];
00786                         
00787                         for (j=0; j< pPoly->NumVerts; j++)
00788                         {
00789                                 u = pTVerts->u*ScaleU+ShiftU;
00790                                 v = pTVerts->v*ScaleV+ShiftV;
00791 
00792                                 pVerts->uv[Stage1].u = u * InvScale;
00793                                 pVerts->uv[Stage1].v = v * InvScale;
00794 
00795                                 pVerts->color = pTVerts->Color;
00796 
00797                                 pTVerts++;
00798                                 pVerts++;
00799                         }
00800 
00801                         break;
00802                 }
00803 
00804                 case PREP_WORLD_VERTS_LMAP:
00805                 {
00806                         if (!pPoly->LInfo)
00807                                 return GE_TRUE;
00808 
00809                         ShiftU = (float)-pPoly->LInfo->MinU + 8.0f;
00810                         ShiftV = (float)-pPoly->LInfo->MinV + 8.0f;
00811 
00812                         // Get scale value for vertices
00813                         InvScale = 1.0f/(float)((1<<pPoly->LInfo->THandle->Log)<<4);
00814                                 
00815                         pTVerts = &WorldCache.TVerts[pPoly->FirstVert];
00816                         pVerts = &WorldCache.Verts[pPoly->FirstVert];
00817 
00818                         for (j=0; j< pPoly->NumVerts; j++)
00819                         {
00820                                 u = pTVerts->u + ShiftU;
00821                                 v = pTVerts->v + ShiftV;
00822 
00823                                 pVerts->uv[Stage1].u = u * InvScale;
00824                                 pVerts->uv[Stage1].v = v * InvScale;
00825 
00826                                 pVerts->color = 0xffffffff;
00827 
00828                                 pTVerts++;
00829                                 pVerts++;
00830                         }
00831                         break;
00832                 }
00833 
00834                 case PREP_WORLD_VERTS_SINGLE_PASS:
00835                 {
00836                         float InvScale2, ShiftU2, ShiftV2;
00837 
00838                         pTVerts = &WorldCache.TVerts[pPoly->FirstVert];
00839 
00840                         // Set up shifts and scaled for texture uv's
00841                         ShiftU = pPoly->ShiftU;
00842                         ShiftV = pPoly->ShiftV;
00843                         ScaleU = pPoly->ScaleU;
00844                         ScaleV = pPoly->ScaleV;
00845 
00846                         // Get scale value for vertices
00847                         InvScale = 1.0f / (float)((1<<pPoly->THandle->Log));
00848 
00849                         // Set up shifts and scaled for lightmap uv's
00850                         ShiftU2 = (float)-pPoly->LInfo->MinU + 8.0f;
00851                         ShiftV2 = (float)-pPoly->LInfo->MinV + 8.0f;
00852                         InvScale2 = 1.0f/(float)((1<<pPoly->LInfo->THandle->Log)<<4);
00853 
00854                         pVerts = &WorldCache.Verts[pPoly->FirstVert];
00855 
00856                         for (j=0; j< pPoly->NumVerts; j++)
00857                         {
00858                                 u = pTVerts->u*ScaleU+ShiftU;
00859                                 v = pTVerts->v*ScaleV+ShiftV;
00860 
00861                                 pVerts->uv[Stage1].u = u * InvScale;
00862                                 pVerts->uv[Stage1].v = v * InvScale;
00863                         
00864                                 u = pTVerts->u + ShiftU2;
00865                                 v = pTVerts->v + ShiftV2;
00866 
00867                                 pVerts->uv[Stage2].u = u * InvScale2;
00868                                 pVerts->uv[Stage2].v = v * InvScale2;
00869 
00870                                 pVerts->color = pTVerts->Color;
00871 
00872                                 pTVerts++;
00873                                 pVerts++;
00874                         }
00875 
00876                         break;
00877                 }
00878 
00879                 default:
00880                         return FALSE;
00881         }
00882 
00883         return TRUE;
00884 }
00885 
00886 //====================================================================================
00887 //====================================================================================
00888 static int BitmapHandleComp(const void *a, const void *b)
00889 {
00890         int32   Id1, Id2;
00891 
00892         Id1 = (*(World_Poly**)a)->SortKey;
00893         Id2 = (*(World_Poly**)b)->SortKey;
00894 
00895         if ( Id1 == Id2)
00896                 return 0;
00897 
00898         if (Id1 < Id2)
00899                 return -1;
00900 
00901         return 1;
00902 }
00903 
00904 //====================================================================================
00905 //====================================================================================
00906 static void SortWorldPolysByHandle(void)
00907 {
00908         World_Poly      *pPoly;
00909         int32           i;
00910 
00911         pPoly = WorldCache.Polys;
00912 
00913         for (i=0; i<WorldCache.NumPolys; i++)
00914         {
00915                 WorldCache.SortedPolys[i] = pPoly;
00916                 pPoly++;
00917         }
00918         
00919         // Sort the polys
00920         qsort(&WorldCache.SortedPolys, WorldCache.NumPolys, sizeof(WorldCache.SortedPolys[0]), BitmapHandleComp);
00921 }
00922 
00923 #define TSTAGE_0                        0
00924 #define TSTAGE_1                        1
00925 
00926 D3DTEXTUREHANDLE                OldId;
00927 LPDIRECT3DTEXTURE2              OldTexture[8];
00928 //====================================================================================
00929 //      RenderWorldPolys
00930 //====================================================================================
00931 static BOOL RenderWorldPolys(int32 RenderMode)
00932 {
00933         World_Poly                      *pPoly;
00934         int32                           i;
00935 
00936         if(!AppInfo.RenderingIsOK)
00937         {
00938                 return  TRUE;
00939         }
00940         switch (RenderMode)
00941         {       
00942                 case RENDER_WORLD_POLYS_NORMAL:
00943                 {
00944                         AppInfo.lpD3DDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0 );
00945 
00946                         // Set the default state for the normal poly render mode for the world
00947                         D3DBlendEnable(TRUE);
00948                         D3DBlendFunc (D3DBLEND_SRCALPHA, D3DBLEND_INVSRCALPHA);
00949                         
00950                         // Get the first poly in the sorted list
00951                         SortWorldPolysByHandle();
00952                         
00953                         for (i=0; i< WorldCache.NumPolys; i++)
00954                         {
00955                                 pPoly = WorldCache.SortedPolys[i];
00956 
00957                                 if (pPoly->Flags & DRV_RENDER_CLAMP_UV)
00958                                         D3DTexWrap(0, FALSE);
00959                                 else
00960                                         D3DTexWrap(0, TRUE);
00961 
00962                                 if (!SetupTexture(0, pPoly->THandle, pPoly->MipLevel))
00963                                         return GE_FALSE;
00964 
00965                                 World_PolyPrepVerts(pPoly, PREP_WORLD_VERTS_NORMAL, 0, 0);
00966 
00967                                 D3DTexturedPoly(&WorldCache.Verts[pPoly->FirstVert], pPoly->NumVerts);
00968                         }
00969                         
00970                         break;
00971                 }
00972                 
00973                 case RENDER_WORLD_POLYS_LMAP:
00974                 {
00975                         AppInfo.lpD3DDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0);
00976 
00977                         D3DTexWrap(0, FALSE);
00978 
00979                         D3DBlendEnable(TRUE);
00980                         D3DBlendFunc (D3DBLEND_DESTCOLOR, D3DBLEND_ZERO);
00981 
00982                         pPoly = WorldCache.Polys;
00983 
00984                         for (i=0; i< WorldCache.NumPolys; i++, pPoly++)
00985                         {
00986                                 BOOL    Dynamic = 0;
00987 
00988                                 if (!pPoly->LInfo)
00989                                         continue;
00990 
00991                                 // Call the engine to set this sucker up, because it's visible...
00992                                 D3DDRV.SetupLightmap(pPoly->LInfo, &Dynamic);
00993 
00994                                 if (!SetupLMap(0, pPoly->LInfo, 0, Dynamic))
00995                                         return GE_FALSE;
00996 
00997                                 World_PolyPrepVerts(pPoly, PREP_WORLD_VERTS_LMAP, 0, 0);
00998 
00999                                 D3DTexturedPoly(&WorldCache.Verts[pPoly->FirstVert], pPoly->NumVerts);
01000                                 
01001                                 if (pPoly->LInfo->RGBLight[1])
01002                                 {
01003                                         AppInfo.lpD3DDevice->SetRenderState(D3DRENDERSTATE_FOGENABLE, FALSE);
01004 
01005                                         D3DBlendFunc (D3DBLEND_ONE, D3DBLEND_ONE);                              // Change to a fog state
01006 
01007                                         // For some reason, some cards can't upload data to the same texture twice, and have it take.
01008                                         // So we force Fog maps to use a different slot than the lightmap was using...
01009                                         pPoly->LInfo->THandle->MipData[0].Slot = NULL;
01010 
01011                                         if (!SetupLMap(0, pPoly->LInfo, 1, 1))  // Dynamic is 1, because fog is always dynamic
01012                                                 return GE_FALSE;
01013 
01014                                         D3DTexturedPoly(&WorldCache.Verts[pPoly->FirstVert], pPoly->NumVerts);
01015                 
01016                                         D3DBlendFunc (D3DBLEND_DESTCOLOR, D3DBLEND_ZERO);               // Restore state
01017 
01018                                         if (AppInfo.FogEnable)
01019                                                 AppInfo.lpD3DDevice->SetRenderState(D3DRENDERSTATE_FOGENABLE , TRUE);
01020                                 }
01021                         }
01022                         break;
01023                 }
01024 
01025                 case RENDER_WORLD_POLYS_SINGLE_PASS:
01026                 {
01027                         // Setup texture stage states
01028                         AppInfo.lpD3DDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0 );
01029                         AppInfo.lpD3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
01030                         AppInfo.lpD3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
01031                         AppInfo.lpD3DDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
01032                         AppInfo.lpD3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
01033                         AppInfo.lpD3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
01034                         AppInfo.lpD3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_MODULATE);
01035                                                                          
01036                         AppInfo.lpD3DDevice->SetTextureStageState( 1, D3DTSS_TEXCOORDINDEX, 1 );
01037                         AppInfo.lpD3DDevice->SetTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_TEXTURE );
01038                         AppInfo.lpD3DDevice->SetTextureStageState( 1, D3DTSS_COLORARG2, D3DTA_CURRENT ); 
01039                         AppInfo.lpD3DDevice->SetTextureStageState( 1, D3DTSS_COLOROP,   D3DTOP_MODULATE );
01040                         AppInfo.lpD3DDevice->SetTextureStageState( 1, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE );
01041                         AppInfo.lpD3DDevice->SetTextureStageState( 1, D3DTSS_ALPHAARG2, D3DTA_CURRENT );
01042                         //AppInfo.lpD3DDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP,   D3DTOP_SELECTARG2 );
01043                         AppInfo.lpD3DDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP,   D3DTOP_MODULATE );
01044 
01045                         // Setup frame buffer blend modes
01046                         D3DBlendEnable(TRUE);
01047                         D3DBlendFunc (D3DBLEND_SRCALPHA, D3DBLEND_INVSRCALPHA);
01048 
01049                         // Set the default state for the normal poly render mode for the world
01050                         D3DTexWrap(TSTAGE_0, TRUE);
01051                         D3DTexWrap(TSTAGE_1, FALSE);
01052 
01053                         // Sort the list for front back operation to get the least number of world texture misses
01054                         SortWorldPolysByHandle();
01055                         
01056                         // Reset non lightmaps faces to 0
01057                         WorldCache.NumPolys2 = 0;
01058 
01059                         for (i=0; i< WorldCache.NumPolys; i++)
01060                         {
01061                                 BOOL    Dynamic = 0;
01062 
01063                                 pPoly = WorldCache.SortedPolys[i];
01064 
01065                                 if (!pPoly->LInfo)
01066                                 {
01067                                         // Put gouraud only polys in a seperate list, and render last
01068                                         WorldCache.SortedPolys2[WorldCache.NumPolys2++] = pPoly;
01069                                         continue;
01070                                 }
01071 
01072                                 if (pPoly->Flags & DRV_RENDER_CLAMP_UV)
01073                                         D3DTexWrap(TSTAGE_0, FALSE);
01074                                 else
01075                                         D3DTexWrap(TSTAGE_0, TRUE);
01076 
01077                                 if (!SetupTexture(TSTAGE_0, pPoly->THandle, pPoly->MipLevel))
01078                                         return GE_FALSE;                                
01079 
01080                                 // Call the engine to set this sucker up, because it's visible...
01081                                 D3DDRV.SetupLightmap(pPoly->LInfo, &Dynamic);
01082 
01083                                 if (!SetupLMap(TSTAGE_1, pPoly->LInfo, 0, Dynamic))
01084                                         return GE_FALSE;
01085                                         
01086                                 // Prep the verts for a lightmap and texture map
01087                                 World_PolyPrepVerts(pPoly, PREP_WORLD_VERTS_SINGLE_PASS, TSTAGE_0, TSTAGE_1);
01088 
01089                                 if ( (pPoly->Flags & DRV_RENDER_POLY_NO_FOG) && AppInfo.FogEnable) // poly fog
01090                                                 AppInfo.lpD3DDevice->SetRenderState(D3DRENDERSTATE_FOGENABLE , FALSE);
01091 
01092                                 // Draw the texture
01093                                 D3DTexturedPoly(&WorldCache.Verts[pPoly->FirstVert], pPoly->NumVerts);
01094 
01095                                 if ( (pPoly->Flags & DRV_RENDER_POLY_NO_FOG) && AppInfo.FogEnable) // poly fog
01096                                                 AppInfo.lpD3DDevice->SetRenderState(D3DRENDERSTATE_FOGENABLE , TRUE);
01097 
01098                                 // Render any fog maps
01099                                 if (pPoly->LInfo->RGBLight[1])
01100                                 {
01101                                         D3DBlendFunc (D3DBLEND_ONE, D3DBLEND_ONE);                              // Change to a fog state
01102 
01103                                 #if (TSTAGE_0 == 0)
01104                                         AppInfo.lpD3DDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG2);
01105                                         AppInfo.lpD3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
01106 
01107                                         AppInfo.lpD3DDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
01108                                 #else
01109                                         AppInfo.lpD3DDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );
01110                                         AppInfo.lpD3DDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
01111                                 #endif
01112 
01113                                         // For some reason, some cards can't upload data to the same texture twice, and have it take.
01114                                         // So we force Fog maps to use a different slot other than what the lightmap was using...
01115                                         pPoly->LInfo->THandle->MipData[0].Slot = NULL;
01116 
01117                                         if (!SetupLMap(TSTAGE_1, pPoly->LInfo, 1, 1))   // Dynamic is 1, because fog is always dynamic
01118                                                 return GE_FALSE;
01119 
01120                                         D3DTexturedPoly(&WorldCache.Verts[pPoly->FirstVert], pPoly->NumVerts);
01121         
01122                                         // Restore states to the last state before fag map
01123                                 #if (TSTAGE_0 == 0)
01124                                         AppInfo.lpD3DDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
01125                                         AppInfo.lpD3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_MODULATE);
01126         
01127                                         AppInfo.lpD3DDevice->SetTextureStageState( 1, D3DTSS_COLOROP,   D3DTOP_MODULATE );
01128                                 #else
01129                                         AppInfo.lpD3DDevice->SetTextureStageState( 1, D3DTSS_COLOROP,   D3DTOP_MODULATE );
01130                                         AppInfo.lpD3DDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP,   D3DTOP_SELECTARG2 );
01131                                 #endif
01132 
01133                                         D3DBlendFunc (D3DBLEND_SRCALPHA, D3DBLEND_INVSRCALPHA);
01134                                 }
01135                                 
01136                                 
01137                         }
01138                         
01139                         // Setup for any non-lightmaped faces faces, turn tmu1 off
01140                 #if (TSTAGE_0 == 0)
01141                         AppInfo.lpD3DDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE);
01142                         AppInfo.lpD3DDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
01143                 #else
01144                         AppInfo.lpD3DDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG2);
01145                         AppInfo.lpD3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG2);
01146                 #endif
01147 
01148                         // Render all the faces without lightmaps
01149                         for (i=0; i< WorldCache.NumPolys2; i++)
01150                         {
01151                                 BOOL    Dynamic = 0;
01152 
01153                                 pPoly = WorldCache.SortedPolys2[i];
01154 
01155                                 if (pPoly->Flags & DRV_RENDER_CLAMP_UV)
01156                                         D3DTexWrap(TSTAGE_0, FALSE);
01157                                 else
01158                                         D3DTexWrap(TSTAGE_0, TRUE);
01159 
01160                                 if (!SetupTexture(TSTAGE_0, pPoly->THandle, pPoly->MipLevel))
01161                                         return GE_FALSE;                                
01162 
01163                                 // Prep verts as if there was no lightmap
01164                                 World_PolyPrepVerts(pPoly, PREP_WORLD_VERTS_NORMAL, TSTAGE_0, TSTAGE_1);
01165 
01166                                 if ( (pPoly->Flags & DRV_RENDER_POLY_NO_FOG) && AppInfo.FogEnable) // poly fog
01167                                                 AppInfo.lpD3DDevice->SetRenderState(D3DRENDERSTATE_FOGENABLE , FALSE);
01168 
01169                                 // Draw the texture
01170                                 D3DTexturedPoly(&WorldCache.Verts[pPoly->FirstVert], pPoly->NumVerts);
01171 
01172                                 if ( (pPoly->Flags & DRV_RENDER_POLY_NO_FOG) && AppInfo.FogEnable) // poly fog
01173                                         AppInfo.lpD3DDevice->SetRenderState(D3DRENDERSTATE_FOGENABLE , TRUE);
01174                         }
01175 
01176                         break;                                           
01177                 }
01178 
01179                 default:
01180                         return FALSE;
01181         }
01182 
01183 
01184         return TRUE;
01185 }
01186 
01187 //====================================================================================
01188 //      ClearWorldCache
01189 //====================================================================================
01190 static BOOL ClearWorldCache(void)
01191 {
01192         WorldCache.NumPolys = 0;
01193         WorldCache.NumVerts = 0;
01194 
01195         return TRUE;
01196 }
01197 
01198 //====================================================================================
01199 //====================================================================================
01200 BOOL PCache_Reset(void)
01201 {
01202         WorldCache.NumPolys = 0;
01203         WorldCache.NumVerts = 0;
01204 
01205         MiscCache.NumPolys = 0;
01206         MiscCache.NumVerts = 0;
01207 
01208         return TRUE;
01209 }
01210 
01211 //====================================================================================
01212 //      GetMipLevel
01213 //====================================================================================
01214 static int32 GetMipLevel(DRV_TLVertex *Verts, int32 NumVerts, float ScaleU, float ScaleV, int32 MaxMipLevel)
01215 {
01216         int32           Mip;
01217 
01218         if (MaxMipLevel == 0)
01219                 return 0;
01220 
01221         //
01222         //      Get the MipLevel
01223         //
01224         {
01225                 float           du, dv, dx, dy, MipScale;
01226 
01227                 int32           i;
01228 
01229                 MipScale = 999999.0f;
01230 
01231                 for (i=0; i< NumVerts; i++)
01232                 {
01233                         float                   MipScaleT;
01234                         DRV_TLVertex    *pVert0, *pVert1;
01235                         int32                   i2;
01236 
01237                         i2 = i+1;
01238 
01239                         if (i2 >= NumVerts)
01240                                 i2=0;
01241 
01242                         pVert0 = &Verts[i];
01243                         pVert1 = &Verts[i2];
01244 
01245                         du = pVert1->u - pVert0->u;
01246                         dv = pVert1->v - pVert0->v;
01247                         dx = pVert1->x - pVert0->x;
01248                         dy = pVert1->y - pVert0->y;
01249                         
01250                         du *= ScaleU;
01251                         dv *= ScaleV;
01252 
01253                         MipScaleT = ((du*du)+(dv*dv)) / ((dx*dx)+(dy*dy));
01254 
01255                         if (MipScaleT < MipScale)
01256                                 MipScale = MipScaleT;           // Record the best MipScale (the one closest to the the eye)
01257                 }
01258 
01259                 if (MipScale <= 4)              // 2, 6, 12
01260                         Mip = 0;
01261                 else if (MipScale <= 15)
01262                         Mip = 1;
01263                 else if (MipScale <= 40)
01264                         Mip = 2;
01265                 else
01266                         Mip = 3;
01267         }
01268 
01269         if (Mip > MaxMipLevel)
01270                 Mip = MaxMipLevel;
01271 
01272         return Mip;
01273 }

Generated on Tue Sep 30 12:36:06 2003 for GTestAndEngine by doxygen 1.3.2