1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029 |
- //---------------------------------------------------------------------------
- //
- // TerrTxm2.h -- File contains class definitions for the Terrain Textures
- //
- // MechCommander 2 -- Microsoft
- //
- //---------------------------------------------------------------------------//
- // Copyright (C) Microsoft Corporation. All rights reserved. //
- //===========================================================================//
- //---------------------------------------------------------------------------
- // Include Files
- #include <windows.h> /*only for declaration of DeleteFile() */
- #ifndef TERRTXM2_H
- #include "terrtxm2.h"
- #endif
- #ifndef PATHS_H
- #include "paths.h"
- #endif
- #ifndef TGAINFO_H
- #include "tgainfo.h"
- #endif
- #ifndef CIDENT_H
- #include "cident.h"
- #endif
- #ifndef FILE_H
- #include "file.h"
- #endif
- #ifndef VERTEX_H
- #include "vertex.h"
- #endif
- #ifndef TERRAIN_H
- #include "terrain.h"
- #endif
- #ifndef CAMERA_H
- #include "camera.h"
- #endif
- #include "resizeimage.h"
- #include <io.h>
- #include <sys\stat.h>
- #define COLOR_MAP_HEAP_SIZE 20480000
- #define COLOR_TXM_HEAP_SIZE 4096
- #define COLOR_MAP_TEXTURE_SIZE 256
- #define COLOR_MAP_RES float(COLOR_MAP_TEXTURE_SIZE)
- bool forceShadowBurnIn = false;
- void* DecodeJPG( const char* FileName, BYTE* Data, DWORD DataSize, DWORD* TextureWidth, DWORD* TextureHeight, bool TextureLoad, void *pDestSurf );
- DWORD TerrainColorMap::terrainTypeIDs[ TOTAL_COLORMAP_TYPES ] =
- {
- 20000,
- 20001,
- 20002,
- 20003,
- 20004
- };
- #define MBOK 1
- //---------------------------------------------------------------------------
- // Class TerrainColorMap
- void TerrainColorMap::init (void)
- {
- ColorMap = NULL;
- colorMapHeap = NULL;
- colorMapRAMHeap = NULL;
-
- numTextures = 0;
- textures = NULL;
- txmRAM = NULL;
-
- numTexturesAcross = fractionPerTexture = 0.0f;
-
- colorMapStarted = false;
-
- detailTextureRAM = NULL;
- detailTextureNodeIndex = 0xffffffff;
- detailTextureTilingFactor = 30.0f;
- waterTextureRAM = NULL;
- waterTextureNodeIndex = 0xffffffff;
- waterTextureTilingFactor = 48.0f;
- numWaterDetailFrames = 0;
- int i;
- for (i = 0; i < MAX_WATER_DETAIL_TEXTURES; i++)
- {
- waterDetailNodeIndex[i] = 0xffffffff;
- }
- waterDetailFrameRate = 8;
- waterDetailTilingFactor = 45.0f;
-
- hGauss = 5.0f;
- roughDistance = -1.0f;
- }
- void TerrainColorMap::destroy (void)
- {
- if (textures)
- {
- colorMapHeap->Free(textures);
- textures = NULL;
- }
- if (txmRAM)
- {
- for (unsigned long i=0;i<numTextures;i++)
- {
- colorMapRAMHeap->Free(txmRAM[i].ourRAM);
- txmRAM[i].ourRAM = NULL;
- }
-
- colorMapRAMHeap->Free(txmRAM);
- txmRAM = NULL;
- }
- if (ColorMap)
- {
- colorMapRAMHeap->Free(ColorMap);
- ColorMap = NULL;
- }
-
- if (colorMapHeap)
- {
- delete colorMapHeap;
- colorMapHeap = NULL;
- }
-
- if (colorMapRAMHeap)
- {
- delete colorMapRAMHeap;
- colorMapRAMHeap = NULL;
- }
-
- numTextures = 0;
- }
- //---------------------------------------------------------------------------
- void TerrainColorMap::getColorMapData (MemoryPtr ourRAM, long index, long width)
- {
- long numWide = width / COLOR_MAP_TEXTURE_SIZE;
- long startCol = ((index % numWide) * COLOR_MAP_TEXTURE_SIZE);
- if (startCol > 0)
- startCol-= (index % numWide);
-
- long startRow = ((index / numWide) * width * COLOR_MAP_TEXTURE_SIZE);
- if (startRow > 0)
- startRow -= (index / numWide) * width;
-
- MemoryPtr ourColor = ColorMap + (startCol + startRow) * sizeof(DWORD);
-
- for (long i=0;i<COLOR_MAP_TEXTURE_SIZE;i++)
- {
- memcpy(ourRAM,ourColor,COLOR_MAP_TEXTURE_SIZE * sizeof(DWORD));
- ourRAM += COLOR_MAP_TEXTURE_SIZE * sizeof(DWORD);
- ourColor += width * sizeof(DWORD);
- }
- }
- //---------------------------------------------------------------------------
- inline void fractalPass (float *heightMap, long edgeSize, long THRESHOLD, long NOISE)
- {
- //Add a bunch o Gaussian noise to the new hi-res height map to break up the lines.
- // This works as follows:
- // If there is a Height Difference between the points, Add noise based on difference.
- // If there is NO height difference, DO NOT ADD NOISE. Let smooth be smooth!
- float *noiseMap = (float *) malloc(sizeof(float) * edgeSize * edgeSize);
- for (long y=THRESHOLD;y<(edgeSize-THRESHOLD);y++)
- {
- for (long x=THRESHOLD;x<(edgeSize-THRESHOLD);x++)
- {
- if (RandomNumber(THRESHOLD) == 0)
- {
- float noise = float(NOISE * 0.5f) - RandomNumber(NOISE);
- noiseMap[x + (y * edgeSize)] = noise;
- }
- else
- {
- noiseMap[x + (y * edgeSize)] = 0.0f;
- }
- }
- }
-
- for (long i=0;i<edgeSize * edgeSize;i++)
- {
- heightMap[i] += noiseMap[i];
- }
-
- free(noiseMap);
- }
- //---------------------------------------------------------------------------
- void rescaleMap (float *dst, float *src, long dstSize, long srcSize)
- {
- Image source, dest;
- source.xSize = source.ySize = srcSize;
- source.data = src;
-
- dest.xSize = dest.ySize = dstSize;
- dest.data = dst;
-
- // zoom(&dest, &source, mitchellFilter, MitchellSupport);
- zoom(&dest, &source, triangleFilter, triangleSupport);
- }
- //---------------------------------------------------------------------------
- void TerrainColorMap::refractalizeBaseMesh (char *fileName, long Threshold, long Noise)
- {
- //Find max and min vertex elevations for scaling below.
- float maxVertex = land->getTerrainElevation(0,0);
- float minVertex = maxVertex;
- for (long y=0;y<Terrain::realVerticesMapSide;y++)
- {
- for (long x=0;x<Terrain::realVerticesMapSide;x++)
- {
- float currentElevation = land->getTerrainElevation(y,x);
- if (currentElevation < minVertex)
- minVertex = currentElevation;
-
- if (currentElevation > maxVertex)
- maxVertex = currentElevation;
- }
- }
-
- float scalar = 0.0f;
- //Check if Map has no elevation!!
- if (maxVertex != minVertex)
- {
- scalar = 255.0f / (maxVertex - minVertex);
- }
-
- //Create an image which is x by x for the existing terrain.
- // Scale it into the range of 0 to 255.
- // these can be floats!!!!!!!
- float *srcData = (float *)malloc(sizeof(float) * Terrain::realVerticesMapSide * Terrain::realVerticesMapSide);
- memset(srcData,0,sizeof(float) * Terrain::realVerticesMapSide * Terrain::realVerticesMapSide);
-
- for (y=0;y<Terrain::realVerticesMapSide;y++)
- {
- for (long x=0;x<Terrain::realVerticesMapSide;x++)
- {
- float currentElevation = land->getTerrainElevation(y,x);
- float scaledValue = (currentElevation - minVertex) * scalar;
- srcData[x + (y * Terrain::realVerticesMapSide)] = scaledValue;
- }
- }
-
- //Spit out original Targa as well.
- MemoryPtr sourceData = (MemoryPtr)malloc(Terrain::realVerticesMapSide * Terrain::realVerticesMapSide);
- memset(sourceData,0,Terrain::realVerticesMapSide * Terrain::realVerticesMapSide);
-
- float *tmpEdge = srcData;
- MemoryPtr tmpOutput = sourceData;
- for (long i=0;i<Terrain::realVerticesMapSide * Terrain::realVerticesMapSide;i++)
- {
- *tmpOutput = (BYTE)CLAMP(*tmpEdge,BLACK_PIXEL,WHITE_PIXEL);
- tmpOutput++;
- tmpEdge++;
- }
- FullPathFileName OEMheightName;
- OEMheightName.init(terrainPath,fileName,".tga");
-
- SetFileAttributes(OEMheightName,FILE_ATTRIBUTE_NORMAL);
-
- TGAFileHeader tgaOutput;
- tgaOutput.cm_entry_size = 0;
- tgaOutput.cm_first_entry = 0;
- tgaOutput.cm_length = 0;
- tgaOutput.color_map = 0;
- tgaOutput.height = Terrain::realVerticesMapSide;
- tgaOutput.image_descriptor = 32; //Right side up!!
- tgaOutput.image_id_len = 0;
- tgaOutput.image_type = UNC_GRAY;
- tgaOutput.pixel_depth = 8;
- tgaOutput.width = Terrain::realVerticesMapSide;
- tgaOutput.x_origin = 0;
- tgaOutput.y_origin = 0;
- File OEMtgaFile;
- long result = OEMtgaFile.create(OEMheightName);
- if (result != NO_ERR)
- STOP(("Couldnt create height map %s on refractalize. Error: %d",OEMheightName,result));
-
- OEMtgaFile.write((MemoryPtr)&tgaOutput,sizeof(TGAFileHeader));
- OEMtgaFile.write(sourceData,Terrain::realVerticesMapSide * Terrain::realVerticesMapSide);
-
- OEMtgaFile.close();
-
- long edgeSize = (numTexturesAcross * COLOR_MAP_RES);
- float *dstData = (float *)malloc(sizeof(float) * edgeSize * edgeSize);
- memset(dstData,0,sizeof(float) * edgeSize * edgeSize);
- rescaleMap(dstData,srcData,edgeSize,Terrain::realVerticesMapSide);
-
- //Now, apply refractalization to dstData.
- if (Noise)
- fractalPass(dstData,edgeSize,Threshold,Noise);
-
- //Save dstData as the height map.
- char newName[1024];
- sprintf(newName,"%s.height",fileName);
-
- FullPathFileName heightName;
- heightName.init(terrainPath,newName,".tga");
-
- SetFileAttributes(heightName,FILE_ATTRIBUTE_NORMAL);
-
- MemoryPtr outputData = (MemoryPtr)malloc(edgeSize * edgeSize);
- memset(outputData,0,sizeof(BYTE) * edgeSize * edgeSize);
-
- tmpEdge = dstData;
- tmpOutput = outputData;
- for (i=0;i<edgeSize*edgeSize;i++)
- {
- *tmpOutput = (BYTE)CLAMP(*tmpEdge,BLACK_PIXEL,WHITE_PIXEL);
- tmpOutput++;
- tmpEdge++;
- }
- tgaOutput.cm_entry_size = 0;
- tgaOutput.cm_first_entry = 0;
- tgaOutput.cm_length = 0;
- tgaOutput.color_map = 0;
- tgaOutput.height = edgeSize;
- tgaOutput.image_descriptor = 32; //Right side up!!
- tgaOutput.image_id_len = 0;
- tgaOutput.image_type = UNC_TRUE;
- tgaOutput.pixel_depth = 24;
- tgaOutput.width = edgeSize;
- tgaOutput.x_origin = 0;
- tgaOutput.y_origin = 0;
- //flipTopToBottom(outputData,8,edgeSize,edgeSize);
-
- File tgaFile;
- result = tgaFile.create(heightName);
- if (result != NO_ERR)
- STOP(("Couldnt create height map %s on refractalize. Error: %d",heightName,result));
-
- tgaFile.write((MemoryPtr)&tgaOutput,sizeof(TGAFileHeader));
- MemoryPtr outputByte = outputData;
- MemoryPtr output24Bit = (MemoryPtr)malloc(edgeSize * edgeSize * 3);
- MemoryPtr output24Temp = output24Bit;
- for (i=0;i<edgeSize*edgeSize;i++)
- {
- *output24Temp = *outputByte;
- output24Temp++;
- *output24Temp = *outputByte;
- output24Temp++;
- *output24Temp = *outputByte;
- output24Temp++;
- outputByte++;
- }
-
- tgaFile.write(output24Bit,edgeSize * edgeSize * 3);
- tgaFile.close();
-
- free(output24Bit);
-
- //ReBurn the shadows.
- recalcLight(fileName);
- //Destroy RAM allocated. No Leaking!!
- free(dstData);
- free(srcData);
- free(sourceData);
- free(outputData);
- }
- float ContrastEnhance = 1.5f;
- float ShadowEnhance = 2.0f;
- //---------------------------------------------------------------------------
- void TerrainColorMap::burnInShadows (bool doBumpPass, char *fileName)
- {
- //-----------------------------------------------------------------
- // for each pixel in the colormap, figure out where light is and
- // darken/lighten color based on light angle and shadows
-
- //first create a height map which is pixel for pixel identical to
- // the color map.
- long pixelWidth = numTexturesAcross * COLOR_MAP_TEXTURE_SIZE;
- float *heightMap = (float *)malloc(sizeof(float) * pixelWidth * pixelWidth);
- gosASSERT(heightMap != NULL);
- memset(heightMap,0,sizeof(float) * pixelWidth * pixelWidth);
-
- float *shadowMap = (float *)malloc(sizeof(float) * pixelWidth * pixelWidth);
- gosASSERT(shadowMap != NULL);
- memset(shadowMap,0,sizeof(float) * pixelWidth * pixelWidth);
-
- float worldUnitsPerPixel = (Terrain::worldUnitsMapSide / pixelWidth);
-
- float startY = Terrain::mapTopLeft3d.y;
- float *hMap = heightMap;
- float *sMap = shadowMap;
- if (Terrain::userMin == Terrain::userMax)
- {
- //Spread 'em out so the lighting actually works!!
- Terrain::userMin = 0;
- Terrain::userMax = 500;
- }
- //First, check if there is a height Map on disk. If so, use it, not a fractally generated one.
- bool heightMapExists = false;
- float highPoint = 0.0f;
- if (fileName)
- {
- char newName[1024];
- sprintf(newName,"%s.height",fileName);
-
- FullPathFileName heightName;
- heightName.init(terrainPath,newName,".tga");
-
- if (fileExists(heightName))
- {
- heightMapExists = true;
-
- File heightMapFile;
- long result = heightMapFile.open(heightName);
- if (result != NO_ERR)
- STOP(("Unable to find Hi-Res Height Data"));
-
- MemoryPtr tgaFileImage = (MemoryPtr)malloc(heightMapFile.fileSize());
- gosASSERT(tgaFileImage != NULL);
-
- heightMapFile.read(tgaFileImage,heightMapFile.fileSize());
-
- TGAFileHeader heightMapInfo;
- memcpy(&heightMapInfo,tgaFileImage,sizeof(TGAFileHeader));
-
- if (heightMapInfo.width != pixelWidth)
- {
- PAUSE(("Hi-Res Height Map wrong size %d. Should be %d. No shadows will be burnt. Press Continue",heightMapInfo.width,pixelWidth));
- return;
- }
- if (heightMapInfo.image_type == UNC_TRUE)
- {
- MemoryPtr loadBuffer = tgaFileImage + sizeof(TGAFileHeader);
- if (heightMapInfo.width != heightMapInfo.height)
- STOP(("Height Map is not a perfect Square"));
-
- //-----------------------------------------------------------------
- // Check if 24 or 32 bit. If 24, do the necessary stuff to it.
- // FIrst find MAX and MIN values for height field.
- BYTE mapMin = 255;
- BYTE mapMax = 0;
- if (heightMapInfo.pixel_depth == 24)
- {
- //24-Bit color means we must skip to every third color data.
- MemoryPtr lMap = loadBuffer;
- for (long i = 0;i<(heightMapInfo.width * heightMapInfo.width);i++)
- {
- BYTE val = *lMap;
- lMap += 3;
- if (val > mapMax)
- mapMax = val;
-
- if (val < mapMin)
- mapMin = val;
- }
- }
- else
- {
- //32-bit color means skip three instead of two bytes.
- MemoryPtr lMap = loadBuffer;
- for (long i = 0;i<(heightMapInfo.width * heightMapInfo.width);i++)
- {
- BYTE val = *lMap;
- lMap += 4;
- if (val > mapMax)
- mapMax = val;
-
- if (val < mapMin)
- mapMin = val;
- }
- }
-
- //Now create Height Map in Floating Point Space.
- if (heightMapInfo.pixel_depth == 24)
- {
- //24-Bit color means we must skip to every third color data.
- float * hMap = heightMap;
- MemoryPtr lMap = loadBuffer;
- for (long i = 0;i<(heightMapInfo.width * heightMapInfo.width);i++)
- {
- float val = (float)(*lMap);
- lMap += 3;
- float h = Terrain::userMin;
- if (0.0 != mapMax)
- {
- h += (val - mapMin)/mapMax * ( Terrain::userMax - Terrain::userMin );
- *hMap = h * ShadowEnhance;
-
- if (highPoint < *hMap)
- highPoint = *hMap;
-
- hMap++;
- }
- }
- }
- else
- {
- //32-bit color means skip three instead of two bytes.
- float * hMap = heightMap;
- MemoryPtr lMap = loadBuffer;
- for (long i = 0;i<(heightMapInfo.width * heightMapInfo.width);i++)
- {
- float val = (float)(*lMap);
- lMap += 4;
- float h = Terrain::userMin;
- if (0.0 != mapMax)
- {
- h += (val - mapMin)/mapMax * ( Terrain::userMax - Terrain::userMin );
- *hMap = h * ShadowEnhance;
-
- if (highPoint < *hMap)
- highPoint = *hMap;
-
- hMap++;
- }
- }
- }
-
- free(tgaFileImage);
- tgaFileImage = NULL;
-
- //------------------------------------------------------------------------
- // Must check image_descriptor to see if we need to un upside down image.
- //bool left = (colorMapInfo.iimage_descriptor & 16) != 0;
- bool top = (heightMapInfo.image_descriptor & 32) != 0;
-
- if (!top)
- {
- flipTopToBottom((MemoryPtr)heightMap,32,heightMapInfo.width,heightMapInfo.height);
- }
- }
- }
- }
- else
- {
- long skipLong = 1;
- for (long y=0;y<pixelWidth;y++)
- {
- float startX = Terrain::mapTopLeft3d.x;
- for (long x=0;x<pixelWidth;x++)
- {
- Stuff::Vector3D pos;
- pos.x = startX;
- pos.y = startY;
- pos.z = 0.0f;
-
- if (!(x%skipLong) && !(y%skipLong))
- *hMap = land->getTerrainElevation(pos);
- else
- *hMap = 0.0f;
-
- *sMap = 0.0f;
-
- if (highPoint < *hMap)
- highPoint = *hMap;
-
- hMap++;
- sMap++;
-
- startX += worldUnitsPerPixel;
- }
- startY -= worldUnitsPerPixel;
- }
- }
-
- //----------------------------------------------------------
- // We now have a MUCH more accurate height map then we did.
- // Do normal calculations first.
- Stuff::Vector3D lightDir;
- lightDir.x = lightDir.y = 0.0f;
- lightDir.z = 1.0f;
-
- if (eye)
- lightDir = eye->lightDirection;
-
- Stuff::Vector3D shadowDir(lightDir);
- shadowDir *= worldUnitsPerPixel; //Distance which is the per pixel distance for checking shadows
-
- //---------------------
- //Shadow Map Pass Here
- highPoint *= ShadowEnhance;
- float *thisHeight = heightMap;
- float *thisShadow = shadowMap;
- for (long y=0;y<pixelWidth;y++)
- {
- for (long x=0;x<pixelWidth;x++)
- {
- //---------------------------------------------
- // No problem
- // Generate at will!
- float v0,v1,v2,v3,v4,v5,v6,v7,v8;
- v0 = *thisHeight;
- if (x && y)
- v1 = *(thisHeight - pixelWidth - 1);
- else
- v1 = v0;
- if (y)
- v2 = *(thisHeight - pixelWidth );
- else
- v2 = v0;
- if (y && (x < (pixelWidth - 1)))
- v3 = *(thisHeight - pixelWidth + 1);
- else
- v3 = v0;
- if (x < (pixelWidth - 1))
- v4 = *(thisHeight + 1);
- else
- v4 = v0;
- if ((x < (pixelWidth - 1)) && (y < (pixelWidth - 1)))
- v5 = *(thisHeight + pixelWidth + 1);
- else
- v5 = v0;
- if ((y < (pixelWidth - 1)))
- v6 = *(thisHeight + pixelWidth );
- else
- v6 = v0;
- if (x && (y < (pixelWidth - 1)))
- v7 = *(thisHeight + pixelWidth - 1);
- else
- v7 = v0;
- if (x)
- v8 = *(thisHeight - 1);
- else
- v8 = v0;
-
- if (Terrain::recalcShadows || forceShadowBurnIn)
- {
- //-----------------------------------------------------
- // Try and project shadow lines.
- Stuff::Vector3D vertexPos;
- vertexPos.x = ((float(x) * worldUnitsPerPixel) + Terrain::mapTopLeft3d.x);
- vertexPos.y = (Terrain::mapTopLeft3d.y - (float(y) * worldUnitsPerPixel));
- vertexPos.z = *thisHeight * ShadowEnhance;
-
- if ((shadowDir.x != 0.0f) || (shadowDir.y != 0.0f))
- {
- vertexPos.Add(vertexPos,shadowDir);
- if (!heightMapExists)
- {
- while (eye && (vertexPos.z < highPoint) && Terrain::IsValidTerrainPosition(vertexPos))
- {
- float elev = land->getTerrainElevation(vertexPos) * ShadowEnhance;
- if (elev >= vertexPos.z)
- {
- *thisShadow = -1.0f; //Mark as shadowed
- break; //Stop Looking!!
- }
-
- vertexPos.Add(vertexPos,shadowDir);
- }
- }
- else
- {
- float tx = x;
- float ty = y;
- while (eye && (vertexPos.z < highPoint) && Terrain::IsValidTerrainPosition(vertexPos))
- {
- //Figure out index into array from vertexPos and use REAL height data for shadows.
- long tileX = tx;
- long tileY = ty;
-
- float elev = heightMap[tileX + (tileY * pixelWidth)];
- if (elev > vertexPos.z)
- {
- *thisShadow = -1.0f; //Mark as shadowed
- break; //Stop Looking!!
- }
- tx += lightDir.x;
- ty -= lightDir.y;
- vertexPos.Add(vertexPos,shadowDir);
- }
- }
- }
- }
-
- if (*thisShadow != -1.0f)
- {
- Stuff::Vector3D normals[8];
- Stuff::Vector3D triVect[2];
-
- //-------------------------------------
- // Tri 021
- triVect[0].x = 0.0f;
- triVect[0].y = worldUnitsPerPixel;
- triVect[0].z = (v2 - v0) * ContrastEnhance;
-
- triVect[1].x = -worldUnitsPerPixel;
- triVect[1].y = worldUnitsPerPixel;
- triVect[1].z = (v1 - v0) * ContrastEnhance;
-
- normals[0].Cross(triVect[0],triVect[1]);
- gosASSERT(normals[0].z > 0.0);
-
- normals[0].Normalize(normals[0]);
-
- //-------------------------------------
- // Tri 032
- triVect[0].x = worldUnitsPerPixel;
- triVect[0].y = worldUnitsPerPixel;
- triVect[0].z = (v3 - v0) * ContrastEnhance;
-
- triVect[1].x = 0.0f;
- triVect[1].y = worldUnitsPerPixel;
- triVect[1].z = (v2 - v0) * ContrastEnhance;
-
- normals[1].Cross(triVect[0],triVect[1]);
- gosASSERT(normals[1].z > 0.0);
-
- normals[1].Normalize(normals[1]);
-
- //-------------------------------------
- // Tri 043
- triVect[0].x = worldUnitsPerPixel;
- triVect[0].y = 0.0f;
- triVect[0].z = (v4 - v0) * ContrastEnhance;
-
- triVect[1].x = worldUnitsPerPixel;
- triVect[1].y = worldUnitsPerPixel;
- triVect[1].z = (v3 - v0) * ContrastEnhance;
-
-
- normals[2].Cross(triVect[0],triVect[1]);
- gosASSERT(normals[2].z > 0.0);
-
- normals[2].Normalize(normals[2]);
-
- //-------------------------------------
- // Tri 054
- triVect[0].x = worldUnitsPerPixel;
- triVect[0].y = -worldUnitsPerPixel;
- triVect[0].z = (v5 - v0) * ContrastEnhance;
-
- triVect[1].x = worldUnitsPerPixel;
- triVect[1].y = 0.0f;
- triVect[1].z = (v4 - v0) * ContrastEnhance;
-
- normals[3].Cross(triVect[0],triVect[1]);
- gosASSERT(normals[3].z > 0.0);
-
- normals[3].Normalize(normals[3]);
-
- //-------------------------------------
- // Tri 065
- triVect[0].x = 0;
- triVect[0].y = -worldUnitsPerPixel;
- triVect[0].z = (v6 - v0) * ContrastEnhance;
-
- triVect[1].x = worldUnitsPerPixel;
- triVect[1].y = -worldUnitsPerPixel;
- triVect[1].z = (v5 - v0) * ContrastEnhance;
-
- normals[4].Cross(triVect[0],triVect[1]);
- gosASSERT(normals[4].z > 0.0);
-
- normals[4].Normalize(normals[4]);
-
- //-------------------------------------
- // Tri 076
- triVect[0].x = -worldUnitsPerPixel;
- triVect[0].y = -worldUnitsPerPixel;
- triVect[0].z = (v7 - v0) * ContrastEnhance;
-
- triVect[1].x = 0.0;
- triVect[1].y = -worldUnitsPerPixel;
- triVect[1].z = (v6 - v0) * ContrastEnhance;
-
- normals[5].Cross(triVect[0],triVect[1]);
- gosASSERT(normals[5].z > 0.0);
-
- normals[5].Normalize(normals[5]);
-
- //-------------------------------------
- // Tri 087
- triVect[0].x = -worldUnitsPerPixel;
- triVect[0].y = 0.0;
- triVect[0].z = (v8 - v0) * ContrastEnhance;
-
- triVect[1].x = -worldUnitsPerPixel;
- triVect[1].y = -worldUnitsPerPixel;
- triVect[1].z = (v7 - v0) * ContrastEnhance;
-
- normals[6].Cross(triVect[0],triVect[1]);
- gosASSERT(normals[6].z > 0.0);
-
- normals[6].Normalize(normals[6]);
-
- //-------------------------------------
- // Tri 018
- triVect[0].x = -worldUnitsPerPixel;
- triVect[0].y = worldUnitsPerPixel;
- triVect[0].z = (v1 - v0) * ContrastEnhance;
-
- triVect[1].x = -worldUnitsPerPixel;
- triVect[1].y = 0.0;
- triVect[1].z = (v8 - v0) * ContrastEnhance;
-
- normals[7].Cross(triVect[0],triVect[1]);
- gosASSERT(normals[7].z > 0.0);
-
- normals[7].Normalize(normals[7]);
-
- Stuff::Vector3D vertexNormal;
- vertexNormal.x = normals[0].x + normals[1].x + normals[2].x + normals[3].x + normals[4].x + normals[5].x + normals[6].x + normals[7].x;
- vertexNormal.y = normals[0].y + normals[1].y + normals[2].y + normals[3].y + normals[4].y + normals[5].y + normals[6].y + normals[7].y;
- vertexNormal.z = normals[0].z + normals[1].z + normals[2].z + normals[3].z + normals[4].z + normals[5].z + normals[6].z + normals[7].z;
- vertexNormal.x /= 8.0;
- vertexNormal.y /= 8.0;
- vertexNormal.z /= 8.0;
-
- gosASSERT(vertexNormal.z > 0.0);
-
- *thisShadow = vertexNormal * lightDir;
- }
-
- thisHeight++;
- thisShadow++;
- }
- }
-
- //Smooth out the shadow map to make shadows better.
- for (long i=pixelWidth;i<((pixelWidth * pixelWidth)-pixelWidth);i++)
- {
- if (shadowMap[i] == -1.0f)
- {
- float v1,v2,v3,v4,v5,v6,v7,v8;
- //All of the pixels around this one should be no more then 0.3f
- v1 = shadowMap[i - pixelWidth - 1];
- v2 = shadowMap[i - pixelWidth ];
- v3 = shadowMap[i - pixelWidth + 1];
- v4 = shadowMap[i + 1];
- v5 = shadowMap[i + pixelWidth + 1];
- v6 = shadowMap[i + pixelWidth ];
- v7 = shadowMap[i + pixelWidth - 1];
- v8 = shadowMap[i - 1];
-
- if (v1 > 0.2f)
- shadowMap[i - pixelWidth - 1] = 0.2f;
-
- if (v2 > 0.2f)
- shadowMap[i - pixelWidth ] = 0.2f;
-
- if (v3 > 0.2f)
- shadowMap[i - pixelWidth + 1] = 0.2f;
-
- if (v4 > 0.2f)
- shadowMap[i + 1] = 0.2f;
-
- if (v5 > 0.2f)
- shadowMap[i + pixelWidth + 1] = 0.2f;
-
- if (v6 > 0.2f)
- shadowMap[i + pixelWidth ] = 0.2f;
-
- if (v7 > 0.2f)
- shadowMap[i + pixelWidth - 1] = 0.2f;
-
- if (v8 > 0.2f)
- shadowMap[i - 1] = 0.2f;
- }
- }
-
- // Shadow map is now done being calculated.
- // Apply the shadows to the magical terrain.
- DWORD *cMap = (DWORD *)ColorMap;
- for (i=0;i<(pixelWidth * pixelWidth);i++)
- {
- if (shadowMap[i] != 0.0f)
- {
- float lightFactor = 0.0f;
- if (shadowMap[i] != -1.0f) //Otherwise, its a shadow and light is 0!!
- {
- lightFactor = shadowMap[i];
- }
- else
- {
- lightFactor = 0.0f;
- }
-
- DWORD lightr, lightg, lightb;
- lightr = eye->getLightRed(lightFactor);
- lightg = eye->getLightGreen(lightFactor);
- lightb = eye->getLightBlue(lightFactor);
-
- DWORD currentColor = *cMap;
-
- float currentBlue = currentColor & 0xff;
- float currentGreen = (currentColor >> 8) & 0xff;
- float currentRed = (currentColor >> 16) & 0xff;
-
- currentRed *= ((float)lightr / 255.0f);
- currentBlue *= ((float)lightb / 255.0f);
- currentGreen *= ((float)lightg / 255.0f);
-
- lightr = currentRed;
- lightg = currentGreen;
- lightb = currentBlue;
-
- *cMap = (0xff << 24) + (lightr << 16) + (lightg << 8) + (lightb);
- }
- cMap++;
- }
-
- //All Done. Get rid of the height and Shadow maps
- free(heightMap);
- free(shadowMap);
- }
- //---------------------------------------------------------------------------
- void saveTGAFile(MemoryPtr ColorMap, char * fileName, long pixelWidth)
- {
- TGAFileHeader colorMapInfo;
- colorMapInfo.image_id_len = 0;
- colorMapInfo.color_map = 0;
- colorMapInfo.image_type = UNC_TRUE;
- colorMapInfo.cm_first_entry= 0;
- colorMapInfo.cm_length = 0;
- colorMapInfo.cm_entry_size = 0;
- colorMapInfo.x_origin = 0;
- colorMapInfo.y_origin = 0;
- colorMapInfo.width = pixelWidth;
- colorMapInfo.height = pixelWidth;
- colorMapInfo.pixel_depth = 32.0;
- colorMapInfo.image_descriptor = 32; //DO NOT FLIP!
-
- char newName[1024];
- sprintf(newName,"%s.burnin",fileName);
-
- FullPathFileName outputName;
- outputName.init(texturePath,newName,".tga");
-
- _chmod(outputName,_S_IREAD | _S_IWRITE);
-
- File outputFile;
- outputFile.create(outputName);
-
- outputFile.write((MemoryPtr)(&colorMapInfo),sizeof(TGAFileHeader));
- outputFile.write(ColorMap,pixelWidth*pixelWidth*sizeof(DWORD));
- outputFile.close();
- }
-
- //---------------------------------------------------------------------------
- inline bool textureIsOKFormat (const char *fileName)
- {
- File tgaFile;
- long result = tgaFile.open(fileName);
- if (result == NO_ERR)
- {
- struct TGAFileHeader tgaHeader;
- tgaFile.read((MemoryPtr)&tgaHeader,sizeof(TGAFileHeader));
- if (((tgaHeader.image_type == UNC_TRUE) || (tgaHeader.image_type == RLE_TRUE)) &&
- (tgaHeader.width == tgaHeader.height) &&
- ((tgaHeader.width == 32) ||
- (tgaHeader.width == 64) ||
- (tgaHeader.width == 128) ||
- (tgaHeader.width == 256)))
- return true;
- tgaFile.close();
- }
- return false;
- }
- //---------------------------------------------------------------------------
- void TerrainColorMap::resetDetailTexture (const char *fileName)
- {
- if (!textureIsOKFormat(fileName))
- {
- char msg[2048];
- sprintf(msg,"Texture %s is not 24bit or 32bit, 32x32, 64x64, 128x128 or 256x256 TGA File. Not loaded!",fileName);
- MessageBox( NULL, msg, NULL, MBOK );
- return;
- }
- mcTextureManager->removeTextureNode (detailTextureNodeIndex);
- detailTextureNodeIndex = 0xffffffff;
- #if 0
- //Load up the detail texture.
- // Detail texture is named same as colormap with .detail in name.
- // if no file exists, no texture drawn.
- char dName[1024];
- sprintf(dName,"%s.detail",fileName);
-
- FullPathFileName detailFile;
- detailFile.init(texturePath,dName,".tga");
- if (fileExists(detailFile)) //Otherwise, its already 0xffffffff!!
- detailTextureNodeIndex = mcTextureManager->loadTexture(detailFile,gos_Texture_Alpha,gosHint_DontShrink);
- #else
- detailTextureNodeIndex = mcTextureManager->loadTexture(fileName,gos_Texture_Alpha,gosHint_DontShrink);
- #endif
- }
- //---------------------------------------------------------------------------
- void TerrainColorMap::resetWaterTexture (const char *fileName)
- {
- if (!textureIsOKFormat(fileName))
- {
- char msg[2048];
- sprintf(msg,"Texture %s is not 24bit or 32bit, 32x32, 64x64, 128x128 or 256x256 TGA File. Not loaded!",fileName);
- MessageBox( NULL, msg, NULL, MBOK );
- return;
- }
- mcTextureManager->removeTextureNode (waterTextureNodeIndex);
- waterTextureNodeIndex = 0xffffffff;
-
- #if 0
- //load up the water texture.
- // Water texture is named the same as the colormap with .water in name.
- // If no file exists, no texture drawn!!
- char dName[1024];
- sprintf(dName,"%s.water",fileName);
-
- FullPathFileName waterFile;
- waterFile.init(texturePath,dName,".tga");
-
- if (fileExists(waterFile)) //Otherwise, its already 0xffffffff!!
- waterTextureNodeIndex = mcTextureManager->loadTexture(waterFile,gos_Texture_Solid,0);
- #else
- waterTextureNodeIndex = mcTextureManager->loadTexture(fileName,gos_Texture_Solid,0);
- #endif
- }
- //---------------------------------------------------------------------------
- void TerrainColorMap::resetWaterDetailTextures (char *fileName)
- {
- //Then, load up the water detail texture(s).
- // Water detail texture is named the same as the colormap with .water0000 in name
- // Where 0000 is the frame number of the animation for the water detail.
- // If the first frame does not exist, no texture is drawn!!
- for (long i=0;i<MAX_WATER_DETAIL_TEXTURES;i++)
- {
- char waterFile[1024];
- if (strlen("0000.tga") > strlen(fileName))
- {
- strcpy(waterFile, "0000.tga");
- }
- else
- {
- strcpy(waterFile, fileName);
- char dName[1024];
- sprintf(dName,"%04d.tga",i);
- char *subStringToBeReplaced = &(waterFile[strlen(waterFile)-strlen("0000.tga")]);
- strcpy(subStringToBeReplaced, dName);
- }
-
- bool textureIsOK = true;
- if (!textureIsOKFormat(waterFile))
- {
- //PAUSE(("Texture %s is Not 24bit or 32bit TGA File. Not loaded. Press Continue",fileName));
- textureIsOK = false;
- }
- if (textureIsOK && fileExists(waterFile))
- {
- if (!i) //If we found the first one OK, erase the current ones!!
- {
- for (long j=0;j<MAX_WATER_DETAIL_TEXTURES;j++)
- {
- mcTextureManager->removeTextureNode (waterDetailNodeIndex[j]);
- waterDetailNodeIndex[j] = 0xffffffff;
- }
- numWaterDetailFrames = 0;
- }
- waterDetailNodeIndex[i] = mcTextureManager->loadTexture(waterFile,gos_Texture_Alpha,gosHint_DontShrink);
- numWaterDetailFrames++;
- }
- else
- {
- if (!i)
- break; //No water detail!
-
- waterDetailNodeIndex[i] = 0xffffffff;
- }
- }
- }
- //---------------------------------------------------------------------------
- void TerrainColorMap::recalcLight(char *fileName)
- {
- colorMapRAMHeap = new UserHeap;
- colorMapRAMHeap->init(COLOR_MAP_HEAP_SIZE,"ColorMap");
-
- FullPathFileName colorMapName;
- colorMapName.init(texturePath,fileName,".tga");
-
- char newName[1024];
- sprintf(newName,"%s.burnin",fileName);
-
- FullPathFileName burnInName;
- burnInName.init(texturePath,newName,".tga");
- File colorMapFile;
- long result = colorMapFile.open(colorMapName);
- if (result != NO_ERR)
- {
- PAUSE(("Unable to open Terrain Color Map %s. Cannot Re-light map. Press Continue",colorMapName));
- return;
- }
-
- MemoryPtr tgaFileImage = (MemoryPtr)malloc(colorMapFile.fileSize());
- gosASSERT(tgaFileImage != NULL);
-
- colorMapFile.read(tgaFileImage,colorMapFile.fileSize());
-
- TGAFileHeader colorMapInfo;
- memcpy(&colorMapInfo,tgaFileImage,sizeof(TGAFileHeader));
-
- if (colorMapInfo.image_type == UNC_TRUE)
- {
- MemoryPtr loadBuffer = tgaFileImage + sizeof(TGAFileHeader);
- if (colorMapInfo.width != colorMapInfo.height)
- STOP(("Color Map is not a perfect Square"));
-
- //-----------------------------------------------------------------
- // Check if 24 or 32 bit. If 24, do the necessary stuff to it.
- ColorMap = (MemoryPtr)colorMapRAMHeap->Malloc(colorMapInfo.width * colorMapInfo.width * sizeof(DWORD));
- gosASSERT(ColorMap != NULL);
-
- if (colorMapInfo.pixel_depth == 24)
- {
- //24-Bit color means we must add in an opaque alpha to each 24 bits of color data.
- MemoryPtr cMap = ColorMap;
- MemoryPtr lMap = loadBuffer;
- for (long i = 0;i<(colorMapInfo.width * colorMapInfo.width);i++)
- {
- *cMap = *lMap; //Red
- cMap++;
- lMap++;
-
- *cMap = *lMap; //Green
- cMap++;
- lMap++;
-
- *cMap = *lMap; //Blue
- cMap++;
- lMap++;
-
- *cMap = 0xff; //Alpha
- cMap++;
- }
- }
- else
- {
- //32-bit color means all we have to do is copy the buffer.
- memcpy(ColorMap,loadBuffer,colorMapInfo.width * colorMapInfo.width * sizeof(DWORD));
- }
-
- free(tgaFileImage);
- tgaFileImage = NULL;
-
- numTextures = colorMapInfo.width / COLOR_MAP_TEXTURE_SIZE;
- numTexturesAcross = numTextures;
- fractionPerTexture = 1.0f / numTextures;
-
- float checkNum = float(colorMapInfo.width) / float(COLOR_MAP_TEXTURE_SIZE);
- if (checkNum != float(numTextures))
- STOP(("Color Map is %d pixels wide which is not even divisible by %d",colorMapInfo.width,COLOR_MAP_TEXTURE_SIZE));
-
- numTextures *= numTextures;
-
- txmRAM = (ColorMapRAM *)colorMapRAMHeap->Malloc(sizeof(ColorMapRAM) * numTextures);
- gosASSERT(txmRAM != NULL);
-
- //------------------------------------------------------------------------
- // Must check image_descriptor to see if we need to un upside down image.
- //bool left = (colorMapInfo.iimage_descriptor & 16) != 0;
- bool top = (colorMapInfo.image_descriptor & 32) != 0;
-
- if (!top)
- {
- flipTopToBottom(ColorMap,32,colorMapInfo.width,colorMapInfo.height);
- }
-
- //Apply shadow map. calc every time for now. Save as in editor, eventually.
- burnInShadows(true,fileName);
- saveTGAFile(ColorMap,fileName,colorMapInfo.width);
-
- //Now, divide up the color map into separate COLOR_MAP_TEXTURE_SIZE textures.
- // and hand the data to the textureManager.
-
- for (unsigned long i=0;i<numTextures;i++)
- {
- mcTextureManager->removeTextureNode(textures[i].mcTextureNodeIndex);
-
- txmRAM[i].ourRAM = (MemoryPtr)colorMapRAMHeap->Malloc(sizeof(DWORD) * COLOR_MAP_TEXTURE_SIZE * COLOR_MAP_TEXTURE_SIZE);
- gosASSERT(txmRAM[i].ourRAM != NULL);
-
- getColorMapData(txmRAM[i].ourRAM,i, colorMapInfo.width);
-
- textures[i].mcTextureNodeIndex = mcTextureManager->textureFromMemory((DWORD *)txmRAM[i].ourRAM,gos_Texture_Solid,gosHint_DontShrink,COLOR_MAP_TEXTURE_SIZE);
- }
- }
- //At this point, the color Map DATA should be freeable!!
- // All of the textures have been passed to the mcTextureManager!
- for (unsigned long i=0;i<numTextures;i++)
- {
- colorMapRAMHeap->Free(txmRAM[i].ourRAM);
- txmRAM[i].ourRAM = NULL;
- }
-
- colorMapRAMHeap->Free(txmRAM);
- txmRAM = NULL;
-
- colorMapRAMHeap->Free(ColorMap);
- ColorMap = NULL;
-
- if (colorMapRAMHeap)
- {
- delete colorMapRAMHeap;
- colorMapRAMHeap = NULL;
- }
- }
- //---------------------------------------------------------------------------
- void TerrainColorMap::resetBaseTexture (char *fileName)
- {
- //First, free up the existing colormap.
- for (unsigned long i=0;i<numTextures;i++)
- {
- mcTextureManager->removeTextureNode(textures[i].mcTextureNodeIndex);
- textures[i].mcTextureNodeIndex = 0xffffffff;
- }
-
- colorMapRAMHeap = new UserHeap;
- colorMapRAMHeap->init(COLOR_MAP_HEAP_SIZE,"ColorMap");
-
- FullPathFileName colorMapName;
- colorMapName.init(texturePath,fileName,".tga");
-
- char newName[1024];
- sprintf(newName,"%s.burnin",fileName);
-
- FullPathFileName burnInName;
- burnInName.init(texturePath,newName,".tga");
- bool burnedIn = false;
- File colorMapFile;
- long result = colorMapFile.open(burnInName);
- if (result != NO_ERR)
- {
- result = colorMapFile.open(colorMapName);
- if (result != NO_ERR)
- STOP(("Unable to open Terrain Color Map %s",colorMapName));
- }
- else
- {
- burnedIn = true;
- }
-
- MemoryPtr tgaFileImage = (MemoryPtr)malloc(colorMapFile.fileSize());
- gosASSERT(tgaFileImage != NULL);
-
- colorMapFile.read(tgaFileImage,colorMapFile.fileSize());
-
- TGAFileHeader colorMapInfo;
- memcpy(&colorMapInfo,tgaFileImage,sizeof(TGAFileHeader));
-
- if (colorMapInfo.image_type == UNC_TRUE)
- {
- MemoryPtr loadBuffer = tgaFileImage + sizeof(TGAFileHeader);
- if (colorMapInfo.width != colorMapInfo.height)
- STOP(("Color Map is not a perfect Square"));
-
- //-----------------------------------------------------------------
- // Check if 24 or 32 bit. If 24, do the necessary stuff to it.
- ColorMap = (MemoryPtr)colorMapRAMHeap->Malloc(colorMapInfo.width * colorMapInfo.width * sizeof(DWORD));
- gosASSERT(ColorMap != NULL);
-
- if (colorMapInfo.pixel_depth == 24)
- {
- //24-Bit color means we must add in an opaque alpha to each 24 bits of color data.
- MemoryPtr cMap = ColorMap;
- MemoryPtr lMap = loadBuffer;
- for (long i = 0;i<(colorMapInfo.width * colorMapInfo.width);i++)
- {
- *cMap = *lMap; //Red
- cMap++;
- lMap++;
-
- *cMap = *lMap; //Green
- cMap++;
- lMap++;
-
- *cMap = *lMap; //Blue
- cMap++;
- lMap++;
-
- *cMap = 0xff; //Alpha
- cMap++;
- }
- }
- else
- {
- //32-bit color means all we have to do is copy the buffer.
- memcpy(ColorMap,loadBuffer,colorMapInfo.width * colorMapInfo.width * sizeof(DWORD));
- }
-
- free(tgaFileImage);
- tgaFileImage = NULL;
-
- numTextures = colorMapInfo.width / COLOR_MAP_TEXTURE_SIZE;
- numTexturesAcross = numTextures;
- fractionPerTexture = 1.0f / numTextures;
-
- float checkNum = float(colorMapInfo.width) / float(COLOR_MAP_TEXTURE_SIZE);
- if (checkNum != float(numTextures))
- STOP(("Color Map is %d pixels wide which is not even divisible by %d",colorMapInfo.width,COLOR_MAP_TEXTURE_SIZE));
-
- numTextures *= numTextures;
- textures = (ColorMapTextures *)colorMapHeap->Malloc(sizeof(ColorMapTextures) * numTextures);
- gosASSERT(textures != NULL);
-
- txmRAM = (ColorMapRAM *)colorMapRAMHeap->Malloc(sizeof(ColorMapRAM) * numTextures);
- gosASSERT(txmRAM != NULL);
-
- //------------------------------------------------------------------------
- // Must check image_descriptor to see if we need to un upside down image.
- //bool left = (colorMapInfo.iimage_descriptor & 16) != 0;
- bool top = (colorMapInfo.image_descriptor & 32) != 0;
- if (!top)
- {
- flipTopToBottom(ColorMap,32,colorMapInfo.width,colorMapInfo.height);
- }
- //Apply shadow map. calc every time for now. Save as in editor, eventually.
- if (!burnedIn)
- {
- burnInShadows(true,fileName);
- saveTGAFile(ColorMap,fileName,colorMapInfo.width);
- }
-
- //Now, divide up the color map into separate COLOR_MAP_TEXTURE_SIZE textures.
- // and hand the data to the textureManager.
-
- for (unsigned long i=0;i<numTextures;i++)
- {
- txmRAM[i].ourRAM = (MemoryPtr)colorMapRAMHeap->Malloc(sizeof(DWORD) * COLOR_MAP_TEXTURE_SIZE * COLOR_MAP_TEXTURE_SIZE);
- gosASSERT(txmRAM[i].ourRAM != NULL);
-
- getColorMapData(txmRAM[i].ourRAM,i, colorMapInfo.width);
-
- textures[i].mcTextureNodeIndex = mcTextureManager->textureFromMemory((DWORD *)txmRAM[i].ourRAM,gos_Texture_Solid,gosHint_DontShrink,COLOR_MAP_TEXTURE_SIZE);
- }
- }
-
- //At this point, the color Map DATA should be freeable!!
- // All of the textures have been passed to the mcTextureManager!
- for (i=0;i<numTextures;i++)
- {
- colorMapRAMHeap->Free(txmRAM[i].ourRAM);
- txmRAM[i].ourRAM = NULL;
- }
-
- colorMapRAMHeap->Free(txmRAM);
- txmRAM = NULL;
-
- colorMapRAMHeap->Free(ColorMap);
- ColorMap = NULL;
-
- if (colorMapRAMHeap)
- {
- delete colorMapRAMHeap;
- colorMapRAMHeap = NULL;
- }
- }
- //---------------------------------------------------------------------------
- //Used by editor for TacMap
- void TerrainColorMap::getScaledColorMap (MemoryPtr bfr, long dWidth)
- {
- bool wasColorMapAround = true;
- if (!ColorMap)
- {
- wasColorMapAround = false;
- char *fileName = Terrain::colorMapName;
- if (!fileName)
- fileName = Terrain::terrainName;
- colorMapRAMHeap = new UserHeap;
- colorMapRAMHeap->init(COLOR_MAP_HEAP_SIZE,"ColorMap");
- FullPathFileName colorMapName;
- colorMapName.init(texturePath,fileName,".tga");
-
- char newName[1024];
- sprintf(newName,"%s.burnin",fileName);
-
- FullPathFileName burnInName;
- burnInName.init(texturePath,newName,".tga");
- //This is what I meant. DO NOT use burnin unless we have to!
- File colorMapFile;
- long result = colorMapFile.open(burnInName);
- if (result != NO_ERR)
- {
- result = colorMapFile.open(colorMapName);
- if (result != NO_ERR)
- {
- #if defined(DEBUG) || defined(PROFILE)
- PAUSE(("Unable to open Terrain Color Map %s. Press Continue",colorMapName));
- #endif
- return;
- }
- }
-
- MemoryPtr tgaFileImage = (MemoryPtr)malloc(colorMapFile.fileSize());
- gosASSERT(tgaFileImage != NULL);
-
- colorMapFile.read(tgaFileImage,colorMapFile.fileSize());
-
- TGAFileHeader colorMapInfo;
- memcpy(&colorMapInfo,tgaFileImage,sizeof(TGAFileHeader));
-
- if (colorMapInfo.image_type == UNC_TRUE)
- {
- MemoryPtr loadBuffer = tgaFileImage + sizeof(TGAFileHeader);
- if (colorMapInfo.width != colorMapInfo.height)
- STOP(("Color Map is not a perfect Square"));
-
- //-----------------------------------------------------------------
- // Check if 24 or 32 bit. If 24, do the necessary stuff to it.
- ColorMap = (MemoryPtr)colorMapRAMHeap->Malloc(colorMapInfo.width * colorMapInfo.width * sizeof(DWORD));
- gosASSERT(ColorMap != NULL);
-
- if (colorMapInfo.pixel_depth == 24)
- {
- //24-Bit color means we must add in an opaque alpha to each 24 bits of color data.
- MemoryPtr cMap = ColorMap;
- MemoryPtr lMap = loadBuffer;
- for (long i = 0;i<(colorMapInfo.width * colorMapInfo.width);i++)
- {
- *cMap = *lMap; //Red
- cMap++;
- lMap++;
-
- *cMap = *lMap; //Green
- cMap++;
- lMap++;
-
- *cMap = *lMap; //Blue
- cMap++;
- lMap++;
-
- *cMap = 0xff; //Alpha
- cMap++;
- }
- }
- else
- {
- //32-bit color means all we have to do is copy the buffer.
- memcpy(ColorMap,loadBuffer,colorMapInfo.width * colorMapInfo.width * sizeof(DWORD));
- }
-
- free(tgaFileImage);
- tgaFileImage = NULL;
-
- DWORD numTextures = colorMapInfo.width / COLOR_MAP_TEXTURE_SIZE;
- numTexturesAcross = numTextures;
-
- //------------------------------------------------------------------------
- // Must check image_descriptor to see if we need to un upside down image.
- //bool left = (colorMapInfo.iimage_descriptor & 16) != 0;
- bool top = (colorMapInfo.image_descriptor & 32) != 0;
-
- if (!top)
- {
- flipTopToBottom(ColorMap,32,colorMapInfo.width,colorMapInfo.height);
- }
- }
- }
- //Grab every xth pixel from every yth scan line!
- float cMapWidth = numTexturesAcross * COLOR_MAP_TEXTURE_SIZE;
- float xSkip = cMapWidth / float(dWidth);
- float ySkip = xSkip;
- long xOffset = 0, yOffset = 0;
- DWORD *currentColor = (DWORD *)ColorMap;
- DWORD *currentBfr = (DWORD *)bfr;
- for (long i=0;i<(dWidth * dWidth);i++)
- {
- *currentBfr = *currentColor;
- xOffset++;
- if (xOffset >= dWidth)
- {
- xOffset = 0;
- yOffset++;
- }
- currentBfr++;
- currentColor = ((DWORD *)ColorMap) + long(xOffset * xSkip) + (long(yOffset*ySkip) * (long)cMapWidth);
- }
- if (!wasColorMapAround)
- {
- delete colorMapRAMHeap;
- colorMapRAMHeap = NULL;
- ColorMap = NULL;
- }
- }
- static long sReadIdFloat(FitIniFile* missionFile, const char *varName, float &value) {
- long result = 0;
- float tmpFloat;
- result = missionFile->readIdFloat((char *)varName, tmpFloat);
- if (NO_ERR != result) {
- //assert(false);
- } else {
- value = tmpFloat;
- }
- return result;
- }
- //---------------------------------------------------------------------------
- long TerrainColorMap::init (char *fileName)
- {
- bool usedJPG = false;
- if (!colorMapStarted)
- {
- //Load up the detail texture first.
- // Detail texture is named same as colormap with .detail in name.
- // if no file exists, no texture drawn.
- char dName[1024];
- sprintf(dName,"%s.detail",fileName);
-
- FullPathFileName detailFile;
- detailFile.init(texturePath,dName,".tga");
- if (!fileExists(detailFile))
- {
- /* If a detail texture doesn't exist for this mission, use the default detail texture.*/
- detailFile.destroy();
- sprintf(dName,"defaults\\default_detail");
- detailFile.init(texturePath,dName,".tga");
- }
- if (fileExists(detailFile)) //Otherwise, its already 0xffffffff!!
- detailTextureNodeIndex = mcTextureManager->loadTexture(detailFile,gos_Texture_Alpha,gosHint_DontShrink);
- else
- gosASSERT(false);
-
- //Ok, now load up the water texture.
- // Water texture is named the same as the colormap with .water in name.
- // If no file exists, no texture drawn!!
- sprintf(dName,"%s.water",fileName);
-
- FullPathFileName waterFile;
- waterFile.init(texturePath,dName,".tga");
-
- if (!fileExists(waterFile))
- {
- /* If a water texture doesn't exist for this mission, use the default water texture.*/
- waterFile.destroy();
- sprintf(dName,"defaults\\default_water");
- waterFile.init(texturePath,dName,".tga");
- }
- if (fileExists(waterFile))
- waterTextureNodeIndex = mcTextureManager->loadTexture(waterFile,gos_Texture_Solid,0);
- else
- gosASSERT(false);
- //Then, load up the water detail texture(s).
- // Water detail texture is named the same as the colormap with .water0000 in name
- // Where 0000 is the frame number of the animation for the water detail.
- // If the first frame does not exist, no texture is drawn!!
- char waterDetailBaseName[1024];
- sprintf(waterDetailBaseName,"%s.water",fileName);
- {
- sprintf(dName,"%s%04d",waterDetailBaseName,0);
-
- FullPathFileName waterFile;
- waterFile.init(texturePath,dName,".tga");
-
- if (!fileExists(waterFile))
- {
- sprintf(waterDetailBaseName,"defaults\\default_water");
- }
- }
- for (long i=0;i<MAX_WATER_DETAIL_TEXTURES;i++)
- {
- waterDetailNodeIndex[i] = 0xffffffff;
- }
- sprintf(dName,"%s%04d",waterDetailBaseName,0);
- waterFile.init(texturePath,dName,".tga");
- resetWaterDetailTextures((char *)waterFile);
- {
- detailTextureTilingFactor = 30.0f;
- waterTextureTilingFactor = 48.0f;
- waterDetailTilingFactor = 45.0f;
- FullPathFileName missionFitFilePath;
- missionFitFilePath.init(missionPath,fileName,".fit");
- FitIniFile missionFitIni;
- if (fileExists(missionFitFilePath))
- {
- missionFitIni.open((char*)(const char*)missionFitFilePath);
- long result = missionFitIni.seekBlock( "Terrain" );
- gosASSERT( result == NO_ERR );
- result = sReadIdFloat(&missionFitIni, "DetailTextureTilingFactor", detailTextureTilingFactor);
- result = sReadIdFloat(&missionFitIni, "WaterTextureTilingFactor", waterTextureTilingFactor);
- result = sReadIdFloat(&missionFitIni, "WaterDetailTilingFactor", waterDetailTilingFactor);
- missionFitIni.close();
- }
- }
- colorMapHeap = new UserHeap;
- colorMapHeap->init(COLOR_TXM_HEAP_SIZE,"ColorTXM");
-
- colorMapRAMHeap = new UserHeap;
- colorMapRAMHeap->init(COLOR_MAP_HEAP_SIZE,"ColorMap");
-
- FullPathFileName colorMapName;
- colorMapName.init(texturePath,fileName,".tga");
-
- char newName[1024];
- sprintf(newName,"%s.burnin",fileName);
-
- FullPathFileName burnInName;
- burnInName.init(texturePath,newName,".tga");
- FullPathFileName burnInJpg;
- burnInJpg.init(texturePath,newName,".jpg");
- //NEW!!
- // Look for the JPG first.
- // If its there, read it in, pass it to the Decoder and bypass the rest of this!!
- DWORD jpgColorMapWidth = 0;
- DWORD jpgColorMapHeight = 0;
- File colorMapFile;
- long result = colorMapFile.open(burnInJpg);
- if (result == NO_ERR)
- {
- long fileSize = colorMapFile.fileSize();
- MemoryPtr jpgData = (MemoryPtr)malloc(fileSize);
- colorMapFile.read(jpgData,fileSize);
- ColorMap = (MemoryPtr)DecodeJPG(burnInJpg, jpgData, fileSize, &jpgColorMapWidth, &jpgColorMapHeight, false, NULL);
- DWORD numTextures = jpgColorMapWidth / COLOR_MAP_TEXTURE_SIZE;
- numTexturesAcross = numTextures;
- fractionPerTexture = 1.0f / numTextures;
-
- float checkNum = float(jpgColorMapWidth) / float(COLOR_MAP_TEXTURE_SIZE);
- if (checkNum != float(numTextures))
- STOP(("Color Map is %d pixels wide which is not even divisible by %d",jpgColorMapWidth,COLOR_MAP_TEXTURE_SIZE));
-
- numTextures *= numTextures;
- textures = (ColorMapTextures *)colorMapHeap->Malloc(sizeof(ColorMapTextures) * numTextures);
- gosASSERT(textures != NULL);
-
- txmRAM = (ColorMapRAM *)colorMapRAMHeap->Malloc(sizeof(ColorMapRAM) * numTextures);
- gosASSERT(txmRAM != NULL);
-
- //Now, divide up the color map into separate COLOR_MAP_TEXTURE_SIZE textures.
- // and hand the data to the textureManager.
- for (unsigned long i=0;i<numTextures;i++)
- {
- txmRAM[i].ourRAM = (MemoryPtr)colorMapRAMHeap->Malloc(sizeof(DWORD) * COLOR_MAP_TEXTURE_SIZE * COLOR_MAP_TEXTURE_SIZE);
- gosASSERT(txmRAM[i].ourRAM != NULL);
-
- getColorMapData(txmRAM[i].ourRAM,i, jpgColorMapWidth);
-
- textures[i].mcTextureNodeIndex = mcTextureManager->textureFromMemory((DWORD *)txmRAM[i].ourRAM,gos_Texture_Solid,gosHint_DontShrink,COLOR_MAP_TEXTURE_SIZE);
- }
-
- free(jpgData);
- jpgData = NULL;
- colorMapStarted = true;
- usedJPG = true;
- }
- else
- {
- bool burnedIn = false;
- File colorMapFile;
- result = colorMapFile.open(burnInName);
- if (result != NO_ERR)
- {
- result = colorMapFile.open(colorMapName);
- if (result != NO_ERR)
- STOP(("Unable to open Terrain Color Map %s",colorMapName));
- }
- else
- {
- burnedIn = true;
- }
- MemoryPtr tgaFileImage = (MemoryPtr)malloc(colorMapFile.fileSize());
- gosASSERT(tgaFileImage != NULL);
- colorMapFile.read(tgaFileImage,colorMapFile.fileSize());
- TGAFileHeader colorMapInfo;
- memcpy(&colorMapInfo,tgaFileImage,sizeof(TGAFileHeader));
- if (colorMapInfo.image_type == UNC_TRUE)
- {
- MemoryPtr loadBuffer = tgaFileImage + sizeof(TGAFileHeader);
- if (colorMapInfo.width != colorMapInfo.height)
- STOP(("Color Map is not a perfect Square"));
- //-----------------------------------------------------------------
- // Check if 24 or 32 bit. If 24, do the necessary stuff to it.
- ColorMap = (MemoryPtr)colorMapRAMHeap->Malloc(colorMapInfo.width * colorMapInfo.width * sizeof(DWORD));
- gosASSERT(ColorMap != NULL);
- if (colorMapInfo.pixel_depth == 24)
- {
- //24-Bit color means we must add in an opaque alpha to each 24 bits of color data.
- MemoryPtr cMap = ColorMap;
- MemoryPtr lMap = loadBuffer;
- for (long i = 0;i<(colorMapInfo.width * colorMapInfo.width);i++)
- {
- *cMap = *lMap; //Red
- cMap++;
- lMap++;
- *cMap = *lMap; //Green
- cMap++;
- lMap++;
- *cMap = *lMap; //Blue
- cMap++;
- lMap++;
- *cMap = 0xff; //Alpha
- cMap++;
- }
- }
- else
- {
- //32-bit color means all we have to do is copy the buffer.
- memcpy(ColorMap,loadBuffer,colorMapInfo.width * colorMapInfo.width * sizeof(DWORD));
- }
- free(tgaFileImage);
- tgaFileImage = NULL;
- DWORD numTextures = colorMapInfo.width / COLOR_MAP_TEXTURE_SIZE;
- numTexturesAcross = numTextures;
- fractionPerTexture = 1.0f / numTextures;
- float checkNum = float(colorMapInfo.width) / float(COLOR_MAP_TEXTURE_SIZE);
- if (checkNum != float(numTextures))
- STOP(("Color Map is %d pixels wide which is not even divisible by %d",colorMapInfo.width,COLOR_MAP_TEXTURE_SIZE));
- numTextures *= numTextures;
- textures = (ColorMapTextures *)colorMapHeap->Malloc(sizeof(ColorMapTextures) * numTextures);
- gosASSERT(textures != NULL);
- txmRAM = (ColorMapRAM *)colorMapRAMHeap->Malloc(sizeof(ColorMapRAM) * numTextures);
- gosASSERT(txmRAM != NULL);
- //------------------------------------------------------------------------
- // Must check image_descriptor to see if we need to un upside down image.
- //bool left = (colorMapInfo.iimage_descriptor & 16) != 0;
- bool top = (colorMapInfo.image_descriptor & 32) != 0;
- if (!top)
- {
- flipTopToBottom(ColorMap,32,colorMapInfo.width,colorMapInfo.height);
- }
- //Apply shadow map. calc every time for now. Save as in editor, eventually.
- if (!burnedIn)
- {
- burnInShadows(true,fileName);
- saveTGAFile(ColorMap,fileName,colorMapInfo.width);
- }
- //Now, divide up the color map into separate COLOR_MAP_TEXTURE_SIZE textures.
- // and hand the data to the textureManager.
- for (unsigned long i=0;i<numTextures;i++)
- {
- txmRAM[i].ourRAM = (MemoryPtr)colorMapRAMHeap->Malloc(sizeof(DWORD) * COLOR_MAP_TEXTURE_SIZE * COLOR_MAP_TEXTURE_SIZE);
- gosASSERT(txmRAM[i].ourRAM != NULL);
- getColorMapData(txmRAM[i].ourRAM,i, colorMapInfo.width);
- textures[i].mcTextureNodeIndex = mcTextureManager->textureFromMemory((DWORD *)txmRAM[i].ourRAM,gos_Texture_Solid,gosHint_DontShrink,COLOR_MAP_TEXTURE_SIZE);
- }
- }
- colorMapStarted = true;
- }
- }
- //At this point, the color Map DATA should be freeable!!
- // All of the textures have been passed to the mcTextureManager!
- for (unsigned long i=0;i<numTextures;i++)
- {
- colorMapRAMHeap->Free(txmRAM[i].ourRAM);
- txmRAM[i].ourRAM = NULL;
- }
-
- colorMapRAMHeap->Free(txmRAM);
- txmRAM = NULL;
-
- if (usedJPG)
- {
- gos_Free(ColorMap);
- ColorMap = NULL;
- }
- else
- {
- colorMapRAMHeap->Free(ColorMap);
- ColorMap = NULL;
- }
-
- if (colorMapRAMHeap)
- {
- delete colorMapRAMHeap;
- colorMapRAMHeap = NULL;
- }
- return 0;
- }
- float textureOffset = 0.4f;
- //---------------------------------------------------------------------------
- DWORD TerrainColorMap::getTextureHandle (VertexPtr vMin, VertexPtr vMax, TerrainUVData *uvData)
- {
- float posX = 0.0f, posY = 0.0f, maxX = 0.0f, maxY = 0.0f;
-
- Stuff::Vector3D pos1(vMin->vx,vMin->vy,0.0f),pos2(vMax->vx,vMax->vy,0.0f);
- if (Terrain::IsValidTerrainPosition(pos1) && Terrain::IsValidTerrainPosition(pos2))
- {
- //-----------------------------------------------------------------------------------------------------
- //Since we have quads with tris in both directions, we need to determine what max and min really are!!
- if (vMin->vx > vMax->vx)
- {
- //Tris are in a weird arrangement. Swap max and min X.
- posX = (vMax->vx - Terrain::mapTopLeft3d.x) * Terrain::oneOverWorldUnitsMapSide;
- maxX = (vMin->vx - Terrain::mapTopLeft3d.x) * Terrain::oneOverWorldUnitsMapSide;
- }
- else
- {
- //Tris in normal arrangement. Just calc 'em out.
- posX = (vMin->vx - Terrain::mapTopLeft3d.x) * Terrain::oneOverWorldUnitsMapSide;
- maxX = (vMax->vx - Terrain::mapTopLeft3d.x) * Terrain::oneOverWorldUnitsMapSide;
- }
-
- posY = (Terrain::mapTopLeft3d.y - vMin->vy) * Terrain::oneOverWorldUnitsMapSide;
- maxY = (Terrain::mapTopLeft3d.y - vMax->vy) * Terrain::oneOverWorldUnitsMapSide;
-
- //------------------------------------------------------
- // Figure out which texture by the vertex v's position.
-
- //We now have the position in x and y as fraction from 0.0 to 1.0
- // The texture is easy. Just mutiply by number of textures across.
- #define EDGE_ADJUST_FACTOR 0.0005f
- long txmNumX = (posX + EDGE_ADJUST_FACTOR) * numTexturesAcross;
- long txmNumY = (posY + EDGE_ADJUST_FACTOR) * numTexturesAcross;
- long resultTexture = txmNumX + (txmNumY * numTexturesAcross);
-
- // Now we need the UV Coords in this texture. This is the fraction from 0.0 to 1.0
- // across the texture we are as vMin and vMax.
- //
- // IMPORTANT OPTIMIZATION NOTE: We only need to do this ONCE per terrain face at load time.
- // It never changes!!!!!!!!
- // We could calc as we get to the terrain faces and just know they're done, too!
- // Could also store in the new PostcompVertex Structure!!
-
- //Also need to adjust UVs by texturePixelAdjust to handle overlaping correctly.
- float minX = (float)txmNumX * fractionPerTexture;
- uvData->maxU = (maxX - minX) * numTexturesAcross;
- uvData->minU = (posX - minX) * numTexturesAcross;
-
- float minY = (float)txmNumY * fractionPerTexture;
- uvData->maxV = (maxY - minY) * numTexturesAcross;
- uvData->minV = (posY - minY) * numTexturesAcross;
-
- /*
- float textureAdjustFactor = (textureOffset / COLOR_MAP_RES);
- if (uvData->minU <= textureAdjustFactor)
- uvData->minU = textureAdjustFactor;
- else
- uvData->minU += xAdjust;
-
- if (uvData->minV <= textureAdjustFactor)
- uvData->minV = textureAdjustFactor;
- else
- uvData->minV += yAdjust;
-
- if (uvData->maxU >= 1.0f)
- uvData->maxU = 1.0f - textureAdjustFactor;
- else
- uvData->maxU += xAdjust;
-
- if (uvData->maxV >= 1.0f)
- uvData->maxV = 1.0f - textureAdjustFactor;
- else
- uvData->maxV += yAdjust;
- */
- #if 0
- if ((uvData->minU < 0.0f) || (uvData->minU >= 1.0f))
- STOP(("UvData our of range in minU %f",uvData->minU));
-
- if ((uvData->minV < 0.0f) || (uvData->minV >= 1.0f))
- STOP(("UvData our of range in minV %f",uvData->minV));
-
- if ((uvData->maxU < 0.0f) || (uvData->maxU >= 1.0f))
- STOP(("UvData our of range in maxU %f",uvData->maxU));
-
- if ((uvData->maxV < 0.0f) || (uvData->maxV >= 1.0f))
- STOP(("UvData our of range in maxV %f",uvData->maxV));
- #endif
-
- mcTextureManager->get_gosTextureHandle(textures[resultTexture].mcTextureNodeIndex);
- return textures[resultTexture].mcTextureNodeIndex;
- }
-
- uvData->minU = uvData->minV = uvData->maxU = uvData->maxV = 0.0f;
- return 0;
- }
- long TerrainColorMap::saveDetailTexture(const char *fileName)
- {
- char dName[1024];
- sprintf(dName,"%s.detail",fileName);
- FullPathFileName detailFile;
- detailFile.init(texturePath,dName,".tga");
- return mcTextureManager->saveTexture(detailTextureNodeIndex, detailFile);
- }
- long TerrainColorMap::saveWaterTexture(const char *fileName)
- {
- char dName[1024];
- sprintf(dName,"%s.water",fileName);
- FullPathFileName waterFile;
- waterFile.init(texturePath,dName,".tga");
- return mcTextureManager->saveTexture(waterTextureNodeIndex, waterFile);
- }
- long TerrainColorMap::saveWaterDetail(const char *fileName)
- {
- long result = NO_ERR;
- int i;
- for (i = 0; i < getWaterDetailNumFrames(); i++)
- {
- char dName[1024];
- sprintf(dName,"%s.water%04d",fileName, i);
- FullPathFileName waterDetailFile;
- waterDetailFile.init(texturePath,dName,".tga");
- long lResult = mcTextureManager->saveTexture(waterDetailNodeIndex[i], waterDetailFile);
- if (NO_ERR != lResult)
- {
- result = lResult;
- }
- }
- for (i = getWaterDetailNumFrames(); i < MAX_WATER_DETAIL_TEXTURES; i++)
- {
- char dName[1024];
- sprintf(dName,"%s.water%04d",fileName, i);
- FullPathFileName waterDetailFile;
- waterDetailFile.init(texturePath,dName,".tga");
- File textureFile;
- long textureFileOpenResult = textureFile.open(waterDetailFile);
- textureFile.close();
- if (NO_ERR != textureFileOpenResult)
- {
- break;
- }
- textureFileOpenResult = textureFile.create(waterDetailFile);
- textureFile.deleteFile(); /*what is this supposed to do?*/
- textureFile.close();
- DeleteFile(waterDetailFile);
- }
- return result;
- }
- long TerrainColorMap::saveTilingFactors(FitIniFile *fitFile)
- {
- long result = NO_ERR;
- fitFile->writeIdFloat( "DetailTextureTilingFactor", getDetailTilingFactor() );
- fitFile->writeIdFloat( "WaterTextureTilingFactor", getWaterTextureTilingFactor() );
- fitFile->writeIdFloat( "WaterDetailTilingFactor", getWaterDetailTilingFactor() );
- return result;
- }
- //---------------------------------------------------------------------------
|