| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211 |
- /*
- ** Command & Conquer Generals(tm)
- ** Copyright 2025 Electronic Arts Inc.
- **
- ** This program is free software: you can redistribute it and/or modify
- ** it under the terms of the GNU General Public License as published by
- ** the Free Software Foundation, either version 3 of the License, or
- ** (at your option) any later version.
- **
- ** This program is distributed in the hope that it will be useful,
- ** but WITHOUT ANY WARRANTY; without even the implied warranty of
- ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- ** GNU General Public License for more details.
- **
- ** You should have received a copy of the GNU General Public License
- ** along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- ////////////////////////////////////////////////////////////////////////////////
- // //
- // (c) 2001-2003 Electronic Arts Inc. //
- // //
- ////////////////////////////////////////////////////////////////////////////////
- // FILE: TerrainTex.cpp ////////////////////////////////////////////////
- //-----------------------------------------------------------------------------
- //
- // Westwood Studios Pacific.
- //
- // Confidential Information
- // Copyright (C) 2001 - All Rights Reserved
- //
- //-----------------------------------------------------------------------------
- //
- // Project: RTS3
- //
- // File name: TerrainTex.cpp
- //
- // Created: John Ahlquist, April 2001
- //
- // Desc: TextureClass overrides to perform custom texturing for the terrain.
- //
- //-----------------------------------------------------------------------------
- //-----------------------------------------------------------------------------
- // Includes
- //-----------------------------------------------------------------------------
- #include <stdlib.h>
- #include "W3DDevice/GameClient/TerrainTex.h"
- #include "W3DDevice/GameClient/WorldHeightMap.h"
- #include "W3DDevice/GameClient/TileData.h"
- #include "common/GlobalData.h"
- #include "WW3D2/dx8wrapper.h"
- #include "d3dx8tex.h"
- /******************************************************************************
- TerrainTextureClass
- ******************************************************************************/
- //-----------------------------------------------------------------------------
- // Public Functions
- //-----------------------------------------------------------------------------
- //=============================================================================
- // TerrainTextureClass::TerrainTextureClass
- //=============================================================================
- /** Constructor. Calls parent constructor to create a 16 bit per pixel D3D
- texture of the desired height and mip level. */
- //=============================================================================
- TerrainTextureClass::TerrainTextureClass(int height, MipCountType mipLevelCount) :
- TextureClass(TEXTURE_WIDTH, height,
- WW3D_FORMAT_A1R5G5B5, mipLevelCount )
- {
- }
- //=============================================================================
- // TerrainTextureClass::update
- //=============================================================================
- /** Sets the tile bitmap data into the texture. The tiles are placed with 4
- pixel borders around them, so that when the tiles are scaled and bilinearly
- interpolated, you don't get seams between the tiles. */
- //=============================================================================
- int TerrainTextureClass::update(WorldHeightMap *htMap)
- {
- // D3DTexture is our texture;
- IDirect3DSurface8 *surface_level;
- D3DSURFACE_DESC surface_desc;
- D3DLOCKED_RECT locked_rect;
- DX8_ErrorCode(D3DTexture->GetSurfaceLevel(0, &surface_level));
- DX8_ErrorCode(surface_level->GetDesc(&surface_desc));
- if (surface_desc.Width < TEXTURE_WIDTH) {
- surface_level->Release();
- if (surface_desc.Width == 256) {
- return update256(htMap);
- }
- return false;
- }
- DX8_ErrorCode(surface_level->LockRect(&locked_rect, NULL, 0));
- Int tilePixelExtent = TILE_PIXEL_EXTENT;
- Int tilesPerRow = surface_desc.Width/(2*TILE_PIXEL_EXTENT+TILE_OFFSET);
- tilesPerRow *= 2;
- // Int numRows = surface_desc.Height/(tilePixelExtent+TILE_OFFSET);
- #ifdef _DEBUG
- //DEBUG_ASSERTCRASH(tilesPerRow*numRows >= htMap->m_numBitmapTiles, ("Too many tiles."));
- DEBUG_ASSERTCRASH((Int)surface_desc.Width >= tilePixelExtent*tilesPerRow, ("Bitmap too small."));
- #endif
- if (surface_desc.Format == D3DFMT_A1R5G5B5) {
- #if 0
- UnsignedInt cellX, cellY;
- for (cellX = 0; cellX < surface_desc.Width; cellX++) {
- for (cellY = 0; cellY < surface_desc.Height; cellY++) {
- UnsignedByte *pBGR = ((UnsignedByte *)locked_rect.pBits)+(cellY*surface_desc.Width+cellX)*2;
- *((Short*)pBGR) = (((255-2*cellY)>>3)<<10) + ((4*cellX)>>4);
- }
- }
- #endif
- Int tileNdx;
- Int pixelBytes = 2;
- for (tileNdx=0; tileNdx < htMap->m_numBitmapTiles; tileNdx++) {
- TileData *pTile = htMap->getSourceTile(tileNdx);
- if (!pTile) continue;
- ICoord2D position = pTile->m_tileLocationInTexture;
- if (position.x<=0) continue; // all real tile offsets start at 2. jba.
- Int i,j;
- for (j=0; j<tilePixelExtent; j++) {
- UnsignedByte *pBGR = pTile->getRGBDataForWidth(tilePixelExtent);
- pBGR += (tilePixelExtent-1-j)*TILE_BYTES_PER_PIXEL*tilePixelExtent; // invert to match.
- Int row = position.y+j;
- UnsignedByte *pBGRX = ((UnsignedByte*)locked_rect.pBits) +
- (row)*surface_desc.Width*pixelBytes;
- Int column = position.x;
- pBGRX += column*pixelBytes;
- for (i=0; i<tilePixelExtent; i++) {
- *((Short*)pBGRX) = 0x8000 + ((pBGR[2]>>3)<<10) + ((pBGR[1]>>3)<<5) + (pBGR[0]>>3);
- pBGRX +=pixelBytes;
- pBGR +=TILE_BYTES_PER_PIXEL;
- }
- }
- }
- // Now draw the 4 pixel border around each tile class.
- Int texClass;
- for (texClass=0; texClass<htMap->m_numTextureClasses; texClass++) {
- Int width = htMap->m_textureClasses[texClass].width;
- ICoord2D origin = htMap->m_textureClasses[texClass].positionInTexture;
- if (origin.x<=0) continue;
- width *= TILE_PIXEL_EXTENT;
- // Duplicate 4 columns of pixels before and after.
- Int j;
- for (j=0; j<width; j++) {
- Int row = origin.y+j;
- UnsignedByte *pBGRX = ((UnsignedByte*)locked_rect.pBits) +
- (row)*surface_desc.Width*pixelBytes;
- Int column = origin.x;
- pBGRX += column*pixelBytes;
- // copy before
- memcpy(pBGRX-(4)*pixelBytes, pBGRX+(width-4)*pixelBytes, 4*pixelBytes);
- // copy after
- memcpy(pBGRX+(width*pixelBytes), pBGRX, 4*pixelBytes);
- }
- // Duplicate 4 rows of pixels before and after.
- for (j=0; j<4; j++) {
- // copy before.
- Int row = origin.y-j-1;
- UnsignedByte *pBGRX = ((UnsignedByte*)locked_rect.pBits) +
- (row)*surface_desc.Width*pixelBytes;
- UnsignedByte *target = pBGRX+(origin.x-4)*pixelBytes;
- memcpy(target, target+width*surface_desc.Width*pixelBytes, (width+8)*pixelBytes);
- // copy after.
- row = origin.y+j;
- pBGRX = ((UnsignedByte*)locked_rect.pBits) +
- (row)*surface_desc.Width*pixelBytes;
- target = pBGRX+(origin.x-4)*pixelBytes;
- memcpy(target+width*surface_desc.Width*pixelBytes, target, (width+8)*pixelBytes);
- }
- }
- }
- surface_level->UnlockRect();
- surface_level->Release();
- DX8_ErrorCode(D3DXFilterTexture(D3DTexture, NULL, 0, D3DX_FILTER_BOX));
- if (TheWritableGlobalData->m_textureReductionFactor) {
- D3DTexture->SetLOD(TheWritableGlobalData->m_textureReductionFactor);
- }
- return(surface_desc.Height);
- }
- #if 0 // old version.
- //=============================================================================
- // TerrainTextureClass::update
- //=============================================================================
- /** Sets the tile bitmap data into the texture. The tiles are placed with 4
- pixel borders around them, so that when the tiles are scaled and bilinearly
- interpolated, you don't get seams between the tiles. */
- //=============================================================================
- int TerrainTextureClass::update(WorldHeightMap *htMap)
- {
- // D3DTexture is our texture;
- IDirect3DSurface8 *surface_level;
- D3DSURFACE_DESC surface_desc;
- D3DLOCKED_RECT locked_rect;
- DX8_ErrorCode(D3DTexture->GetSurfaceLevel(0, &surface_level));
- DX8_ErrorCode(surface_level->GetDesc(&surface_desc));
- if (surface_desc.Width < TEXTURE_WIDTH) {
- surface_level->Release();
- if (surface_desc.Width == 256) {
- return update256(htMap);
- }
- return false;
- }
- DX8_ErrorCode(surface_level->LockRect(&locked_rect, NULL, 0));
- Int tilePixelExtent = TILE_PIXEL_EXTENT;
- Int tilesPerRow = surface_desc.Width/(2*TILE_PIXEL_EXTENT+TILE_OFFSET);
- tilesPerRow *= 2;
- Int numRows = surface_desc.Height/(tilePixelExtent+TILE_OFFSET);
- #ifdef _DEBUG
- assert(tilesPerRow*numRows >= htMap->m_numBitmapTiles);
- assert((Int)surface_desc.Width >= tilePixelExtent*tilesPerRow);
- #endif
- if (surface_desc.Format == D3DFMT_A1R5G5B5) {
- Int cellX, cellY;
- #if 0
- for (cellX = 0; cellX < surface_desc.Width; cellX++) {
- for (cellY = 0; cellY < surface_desc.Height; cellY++) {
- UnsignedByte *pBGR = ((UnsignedByte *)locked_rect.pBits)+(cellY*surface_desc.Width+cellX)*2;
- *((Short*)pBGR) = (((255-2*cellY)>>3)<<10) + ((4*cellX)>>4);
- }
- }
- #endif
- Int pixelBytes = 2;
- for (cellY = 0; cellY < numRows; cellY++) {
- for (cellX = 0; cellX < tilesPerRow; cellX++) {
- Int tileNdx = cellX/2 + (tilesPerRow/2)*(cellY/2);
- tileNdx *=4;
- if (cellX&1) tileNdx++;
- if (!(cellY&1)) tileNdx += 2;
- #define ADD_EXTRA_TILES 1
- #if ADD_EXTRA_TILES // Fills in an extra 2 columns and 1 row of tiles if there is room.
- if (!htMap->getSourceTile(tileNdx) && htMap->getSourceTile(tileNdx-4)) {
- tileNdx -= 4;
- }
- if (!htMap->getSourceTile(tileNdx) && htMap->getSourceTile(tileNdx-8)) {
- tileNdx -= 8;
- }
- if (!htMap->getSourceTile(tileNdx) && htMap->getSourceTile(tileNdx-2*tilesPerRow)) {
- tileNdx -= 2*tilesPerRow;
- }
- #endif
- if (htMap->getSourceTile(tileNdx)) {
- Int i,j;
- for (j=0; j<tilePixelExtent; j++) {
- UnsignedByte *pBGR = htMap->getSourceTile(tileNdx)->getRGBDataForWidth(tilePixelExtent);
- pBGR += (tilePixelExtent-1-j)*TILE_BYTES_PER_PIXEL*tilePixelExtent; // invert to match.
- Int row = cellY*tilePixelExtent+j;
- row += TILE_OFFSET/2;
- row += TILE_OFFSET*(cellY/2);
- UnsignedByte *pBGRX = ((UnsignedByte*)locked_rect.pBits) +
- (row)*surface_desc.Width*pixelBytes;
- Int column = cellX*tilePixelExtent;
- column += TILE_OFFSET*(cellX/2);
- pBGRX += column*pixelBytes;
- pBGRX += (TILE_OFFSET/2)*pixelBytes;
- for (i=0; i<tilePixelExtent; i++) {
- *((Short*)pBGRX) = 0x8000 + ((pBGR[2]>>3)<<10) + ((pBGR[1]>>3)<<5) + (pBGR[0]>>3);
- pBGRX +=pixelBytes;
- pBGR +=TILE_BYTES_PER_PIXEL;
- }
- }
- }
- }
- }
- for (cellY = 0; cellY < numRows; cellY++) {
- for (cellX = 0; cellX < tilesPerRow; cellX++) {
- // Duplicate 4 rows of pixels before and after.
- Int j;
- for (j=0; j<tilePixelExtent; j++) {
- Int row = cellY*tilePixelExtent+j;
- row += TILE_OFFSET/2;
- row += TILE_OFFSET*(cellY/2);
- UnsignedByte *pBGRX = ((UnsignedByte*)locked_rect.pBits) +
- (row)*surface_desc.Width*pixelBytes;
- Int column = cellX*tilePixelExtent;
- column += TILE_OFFSET*(cellX/2);
- pBGRX += column*pixelBytes;
- if (cellX&1) {
- Int bytesToCopy = 4*pixelBytes;
- if (cellX == tilesPerRow-1) {
- // last cell, so fill to the end of the texture.
- Int usedWidth = tilesPerRow*tilePixelExtent + TILE_OFFSET*(tilesPerRow/2);
- usedWidth *= pixelBytes;
- bytesToCopy += surface_desc.Width*pixelBytes - usedWidth;
- }
- // Copy after.
- pBGRX -= tilePixelExtent*pixelBytes;
- pBGRX += (TILE_OFFSET/2)*pixelBytes;
- memcpy(pBGRX+(2*tilePixelExtent*pixelBytes), pBGRX, bytesToCopy);
- } else {
- // copy before
- memcpy(pBGRX, pBGRX+(2*tilePixelExtent)*pixelBytes, 4*pixelBytes);
- }
- }
- }
- }
- for (cellY = 0; cellY < numRows; cellY++) {
- Int rowBytes = surface_desc.Width*pixelBytes;
- Int row = cellY*tilePixelExtent;
- row += TILE_OFFSET/2;
- row += TILE_OFFSET*(cellY/2);
- if ( (cellY&1) == 0) {
- UnsignedByte *pBGRX = ((UnsignedByte*)locked_rect.pBits) +
- (row)*rowBytes;
- row += 2*tilePixelExtent;
- UnsignedByte *pBase = ((UnsignedByte*)locked_rect.pBits) +
- (row)*rowBytes;
- memcpy(pBGRX-4*rowBytes, pBase-4*rowBytes, 4*rowBytes);
- } else {
- UnsignedByte *pBase = ((UnsignedByte*)locked_rect.pBits) +
- (row-tilePixelExtent)*rowBytes;
- row += tilePixelExtent;
- UnsignedByte *pBGRX = ((UnsignedByte*)locked_rect.pBits) +
- (row)*rowBytes;
- memcpy(pBGRX, pBase, 4*rowBytes);
- }
- }
-
- }
- surface_level->UnlockRect();
- surface_level->Release();
- DX8_ErrorCode(D3DXFilterTexture(D3DTexture, NULL, 0, D3DX_FILTER_BOX));
- return(surface_desc.Height);
- }
- #endif
- //=============================================================================
- // TerrainTextureClass::update256
- //=============================================================================
- /** Sets the tile bitmap data into the texture. Handles the special case for voodoos
- and the like where the max texture size is 256. The tiles are placed with 1
- pixel borders around them, so that when the tiles are scaled and bilinearly
- interpolated, you don't get seams between the tiles. */
- //=============================================================================
- int TerrainTextureClass::update256(WorldHeightMap *htMap)
- {
- // D3DTexture is our texture;
- IDirect3DSurface8 *surface_level;
- D3DSURFACE_DESC surface_desc;
- D3DLOCKED_RECT locked_rect;
- DX8_ErrorCode(D3DTexture->GetSurfaceLevel(0, &surface_level));
- DX8_ErrorCode(surface_level->GetDesc(&surface_desc));
- if (surface_desc.Width != 256) {
- surface_level->Release();
- return surface_desc.Height;
- }
- DX8_ErrorCode(surface_level->LockRect(&locked_rect, NULL, 0));
- Int tilePixelExtent = TILE_PIXEL_EXTENT/4; // We are going from 1024 to 256, so need to divide all the constants by 4.
- Int tileOffset = TILE_OFFSET/4;
- Int tilesPerRow = surface_desc.Width/(2*tilePixelExtent+tileOffset);
- tilesPerRow *= 2;
- Int numRows = surface_desc.Height/(tilePixelExtent+tileOffset);
- #ifdef _DEBUG
- assert(tilesPerRow*numRows >= htMap->m_numBitmapTiles);
- assert((Int)surface_desc.Width >= tilePixelExtent*tilesPerRow);
- #endif
- if (surface_desc.Format == D3DFMT_A1R5G5B5) {
- Int cellX, cellY;
- #if 0
- for (cellX = 0; cellX < surface_desc.Width; cellX++) {
- for (cellY = 0; cellY < surface_desc.Height; cellY++) {
- UnsignedByte *pBGR = ((UnsignedByte *)locked_rect.pBits)+(cellY*surface_desc.Width+cellX)*2;
- *((Short*)pBGR) = (((255-2*cellY)>>3)<<10) + ((4*cellX)>>4);
- }
- }
- numRows = 0;
- #endif
- Int pixelBytes = 2;
- for (cellY = 0; cellY < numRows; cellY++) {
- for (cellX = 0; cellX < tilesPerRow; cellX++) {
- Int tileNdx = cellX/2 + (tilesPerRow/2)*(cellY/2);
- tileNdx *=4;
- if (cellX&1) tileNdx++;
- if (!(cellY&1)) tileNdx += 2;
- #if ADD_EXTRA_TILES // Fills in an extra 2 columns and 1 row of tiles if there is room.
- if (!htMap->getSourceTile(tileNdx) && htMap->getSourceTile(tileNdx-4)) {
- tileNdx -= 4;
- }
- if (!htMap->getSourceTile(tileNdx) && htMap->getSourceTile(tileNdx-8)) {
- tileNdx -= 8;
- }
- if (!htMap->getSourceTile(tileNdx) && htMap->getSourceTile(tileNdx-2*tilesPerRow)) {
- tileNdx -= 2*tilesPerRow;
- }
- #endif
- if (htMap->getSourceTile(tileNdx)) {
- Int i,j;
- for (j=0; j<tilePixelExtent; j++) {
- UnsignedByte *pBGR = htMap->getSourceTile(tileNdx)->getRGBDataForWidth(tilePixelExtent);
- pBGR += (tilePixelExtent-1-j)*TILE_BYTES_PER_PIXEL*tilePixelExtent; // invert to match.
- Int row = cellY*tilePixelExtent+j;
- row += tileOffset/2;
- row += tileOffset*(cellY/2);
- UnsignedByte *pBGRX = ((UnsignedByte*)locked_rect.pBits) +
- (row)*surface_desc.Width*pixelBytes;
- Int column = cellX*tilePixelExtent;
- column += tileOffset*(cellX/2);
- pBGRX += column*pixelBytes;
- pBGRX += (tileOffset/2)*pixelBytes;
- for (i=0; i<tilePixelExtent; i++) {
- *((Short*)pBGRX) = 0x8000 + ((pBGR[2]>>3)<<10) + ((pBGR[1]>>3)<<5) + (pBGR[0]>>3);
- pBGRX +=pixelBytes;
- pBGR +=TILE_BYTES_PER_PIXEL;
- }
- }
- }
- }
- }
- for (cellY = 0; cellY < numRows; cellY++) {
- for (cellX = 0; cellX < tilesPerRow; cellX++) {
- // Duplicate 1 rows of pixels before and after.
- Int j;
- for (j=0; j<tilePixelExtent; j++) {
- Int row = cellY*tilePixelExtent+j;
- row += tileOffset/2;
- row += tileOffset*(cellY/2);
- UnsignedByte *pBGRX = ((UnsignedByte*)locked_rect.pBits) +
- (row)*surface_desc.Width*pixelBytes;
- Int column = cellX*tilePixelExtent;
- column += tileOffset*(cellX/2);
- pBGRX += column*pixelBytes;
- if (cellX&1) {
- // Copy after.
- pBGRX -= tilePixelExtent*pixelBytes;
- pBGRX += (tileOffset/2)*pixelBytes;
- Int bytesToCopy = pixelBytes;
- if (cellX == tilesPerRow-1) {
- // last cell, so fill to the end of the texture.
- Int usedWidth = tilesPerRow*tilePixelExtent + tileOffset*(tilesPerRow/2);
- usedWidth *= pixelBytes;
- bytesToCopy += surface_desc.Width*pixelBytes - usedWidth;
- }
- memcpy(pBGRX+(2*tilePixelExtent*pixelBytes), pBGRX, bytesToCopy);
- // Copy after.
- pBGRX -= tilePixelExtent*pixelBytes;
- pBGRX += (TILE_OFFSET/2)*pixelBytes;
- } else {
- // copy before
- memcpy(pBGRX, pBGRX+(2*tilePixelExtent)*pixelBytes, pixelBytes);
- }
- }
- }
- }
- for (cellY = 0; cellY < numRows; cellY++) {
- Int rowBytes = surface_desc.Width*pixelBytes;
- Int row = cellY*tilePixelExtent;
- row += tileOffset/2;
- row += tileOffset*(cellY/2);
- if ( (cellY&1) == 0) {
- UnsignedByte *pBGRX = ((UnsignedByte*)locked_rect.pBits) +
- (row)*rowBytes;
- row += 2*tilePixelExtent;
- UnsignedByte *pBase = ((UnsignedByte*)locked_rect.pBits) +
- (row)*rowBytes;
- memcpy(pBGRX-rowBytes, pBase-rowBytes, rowBytes);
- } else {
- UnsignedByte *pBase = ((UnsignedByte*)locked_rect.pBits) +
- (row-tilePixelExtent)*rowBytes;
- row += tilePixelExtent;
- UnsignedByte *pBGRX = ((UnsignedByte*)locked_rect.pBits) +
- (row)*rowBytes;
- memcpy(pBGRX, pBase, rowBytes);
- }
- }
- }
- surface_level->UnlockRect();
- surface_level->Release();
- DX8_ErrorCode(D3DXFilterTexture(D3DTexture, NULL, 0, D3DX_FILTER_BOX));
- // Note - normal width for the terrain texture is 1024. We are at 256
- // probably running on a voodoo. The height we return is scaled up
- // to match the expected width of 1024. jba.
- return(surface_desc.Height*4);
- }
- //=============================================================================
- // TerrainTextureClass::Apply
- //=============================================================================
- /** Sets the texture as the current D3D texture, and does some custom setup
- (standard D3D setup, but beyond the scope of W3D). */
- //=============================================================================
- void TerrainTextureClass::Apply(unsigned int stage)
- {
- // Do the base apply.
- TextureClass::Apply(stage);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP);
- if (TheGlobalData && TheGlobalData->m_bilinearTerrainTex || TheGlobalData->m_trilinearTerrainTex) {
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_MINFILTER, D3DTEXF_LINEAR);
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_MAGFILTER, D3DTEXF_LINEAR);
- } else {
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_MINFILTER, D3DTEXF_POINT);
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_MAGFILTER, D3DTEXF_POINT);
- }
- if (TheGlobalData && TheGlobalData->m_trilinearTerrainTex) {
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_MIPFILTER, D3DTEXF_LINEAR);
- } else {
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_MIPFILTER, D3DTEXF_POINT);
- }
- // Now setup the texture pipeline.
- if (stage==0) {
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_TEXCOORDINDEX, 0 );
- DX8Wrapper::Set_DX8_Render_State(D3DRS_ALPHABLENDENABLE,false);
- }
- }
- /******************************************************************************
- AlphaTerrainTextureClass
- ******************************************************************************/
- //-----------------------------------------------------------------------------
- // Public Functions
- //-----------------------------------------------------------------------------
- //=============================================================================
- // AlphaTerrainTextureClass::AlphaTerrainTextureClass
- //=============================================================================
- /** Constructor. Calls parent constructor to creat a throw away 8x8 texture,
- then uses the base texture's D3D texture. This way the base tiles pass, drawn
- using TerrainTextureClass shares the same texture with the blended edges pass,
- saving lots of texture memory, and preventing seams between blended tiles. */
- //=============================================================================
- AlphaTerrainTextureClass::AlphaTerrainTextureClass( TextureClass *pBaseTex ):
- TextureClass(8, 8,
- WW3D_FORMAT_A1R5G5B5, MIP_LEVELS_1 )
- {
- if (D3DTexture) {
- // Release the 8x8 texture.
- D3DTexture->Release();
- D3DTexture = NULL;
- }
- // Attach the base texture's d3d texture.
- D3DTexture = pBaseTex->Peek_DX8_Texture();
- D3DTexture->AddRef();
- }
- //=============================================================================
- // AlphaTerrainTextureClass::Apply
- //=============================================================================
- /** Sets the texture as the current D3D texture, and does some custom setup.
- This may be applied in either single pass, as the second texture in the pipe,
- or multipass. If stage==0, we are doing multipass and we set up the pipe
- for a single texture. If stage==1, then we are doing a single pass, and we
- set up the pipe so that we blend onto the base texture in stage 0.
- (standard D3D setup, but beyond the scope of W3D). */
- //=============================================================================
- void AlphaTerrainTextureClass::Apply(unsigned int stage)
- {
- // Do the base apply.
- TextureClass::Apply(stage);
-
- // Set the bilinear or trilinear filtering.
- if (TheGlobalData && TheGlobalData->m_bilinearTerrainTex || TheGlobalData->m_trilinearTerrainTex) {
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_MINFILTER, D3DTEXF_LINEAR);
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_MAGFILTER, D3DTEXF_LINEAR);
- } else {
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_MINFILTER, D3DTEXF_POINT);
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_MAGFILTER, D3DTEXF_POINT);
- }
- if (TheGlobalData && TheGlobalData->m_trilinearTerrainTex) {
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_MIPFILTER, D3DTEXF_LINEAR);
- } else {
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_MIPFILTER, D3DTEXF_POINT);
- }
- // Since we are using multiple distinct tiles, the textures doesn't wrap, so clamp it.
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP);
- // Now setup the texture pipeline.
- if (stage==0) {
- // Modulate the diffuse color with the texture as lighting comes from diffuse.
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_TEXCOORDINDEX, 1 );
- // Blend the result using the alpha. (came from diffuse mod texture)
- DX8Wrapper::Set_DX8_Render_State(D3DRS_ALPHABLENDENABLE,true);
- DX8Wrapper::Set_DX8_Render_State(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
- DX8Wrapper::Set_DX8_Render_State(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
- // Disable stage 2.
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
- } else if (stage==1) {
- if (TheGlobalData && !TheGlobalData->m_multiPassTerrain)
- {
- ///@todo: Remove 8-Stage Nvidia hack after drivers are fixed.
- //This method is a backdoor specific to Nvidia based cards. It will fail on
- //other hardware. Allows single pass blend of 2 textures and post modulate diffuse.
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_COLOROP, D3DTOP_MODULATE);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_TEXCOORDINDEX, 0);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_COLOROP, D3DTOP_ADD);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_TEXCOORDINDEX, 1);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_COLORARG1, D3DTA_DIFFUSE | D3DTA_COMPLEMENT | D3DTA_ALPHAREPLICATE);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_ALPHAOP, D3DTOP_ADD);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_ALPHAARG1, D3DTA_TFACTOR | D3DTA_COMPLEMENT);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_ALPHAARG2, D3DTA_TFACTOR);
- DX8Wrapper::Set_DX8_Texture(2, NULL);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 2, D3DTSS_COLOROP, D3DTOP_MODULATE);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 2, D3DTSS_TEXCOORDINDEX, 2);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 2, D3DTSS_COLORARG1, D3DTA_TEXTURE);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 2, D3DTSS_COLORARG2, D3DTA_TEXTURE);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 2, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 2, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 2, D3DTSS_ALPHAARG2, D3DTA_TFACTOR);
- DX8Wrapper::Set_DX8_Texture(3, NULL);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 3, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 3, D3DTSS_TEXCOORDINDEX, 3);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 3, D3DTSS_COLORARG1, D3DTA_DIFFUSE | 0 | D3DTA_ALPHAREPLICATE);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 3, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 3, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 3, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 3, D3DTSS_ALPHAARG2, D3DTA_TFACTOR);
- DX8Wrapper::Set_DX8_Texture(4, NULL);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 4, D3DTSS_COLOROP, D3DTOP_MODULATE);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 4, D3DTSS_TEXCOORDINDEX, 4);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 4, D3DTSS_COLORARG1, D3DTA_CURRENT);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 4, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 4, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 4, D3DTSS_ALPHAARG1, D3DTA_CURRENT);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 4, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
- DX8Wrapper::Set_DX8_Texture(5, NULL);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 5, D3DTSS_COLOROP, D3DTOP_ADD);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 5, D3DTSS_TEXCOORDINDEX, 5);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 5, D3DTSS_COLORARG1, D3DTA_DIFFUSE);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 5, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 5, D3DTSS_ALPHAOP, D3DTOP_ADD);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 5, D3DTSS_ALPHAARG1, D3DTA_TFACTOR | D3DTA_COMPLEMENT);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 5, D3DTSS_ALPHAARG2, D3DTA_TFACTOR);
- DX8Wrapper::Set_DX8_Texture(6, NULL);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 6, D3DTSS_COLOROP, D3DTOP_MODULATE);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 6, D3DTSS_TEXCOORDINDEX, 6);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 6, D3DTSS_COLORARG1, D3DTA_TFACTOR);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 6, D3DTSS_COLORARG2, D3DTA_TFACTOR);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 6, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 6, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 6, D3DTSS_ALPHAARG2, D3DTA_TFACTOR);
- DX8Wrapper::Set_DX8_Texture(7, NULL);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 7, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 7, D3DTSS_TEXCOORDINDEX, 7);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 7, D3DTSS_COLORARG1, D3DTA_TFACTOR);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 7, D3DTSS_COLORARG2, D3DTA_TFACTOR);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 7, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 7, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 7, D3DTSS_ALPHAARG2, D3DTA_TFACTOR);
- }
- else
- {
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_COLORARG1, D3DTA_TEXTURE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_COLOROP, D3DTOP_MODULATE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_COLORARG2, D3DTA_CURRENT );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
- }
- }
- }
- /******************************************************************************
- LightMapTerrainTextureClass
- ******************************************************************************/
- //-----------------------------------------------------------------------------
- // Public Functions
- //-----------------------------------------------------------------------------
- //=============================================================================
- // LightMapTerrainTextureClass::LightMapTerrainTextureClass
- //=============================================================================
- /** Constructor. Calls parent constructor to load the .tga texture. */
- //=============================================================================
- LightMapTerrainTextureClass::LightMapTerrainTextureClass(AsciiString name, MipCountType mipLevelCount) :
- TextureClass(name.isEmpty()?"TSNoiseUrb.tga":name.str(),name.isEmpty()?"TSNoiseUrb.tga":name.str(), mipLevelCount )
- {
- }
- #define STRETCH_FACTOR ((float)(1/(63.0*MAP_XY_FACTOR/2))) /* covers 63/2 tiles */
- //=============================================================================
- // LightMapTerrainTextureClass::Apply
- //=============================================================================
- /** Sets the texture as the current D3D texture, and does some custom setup.
- The LightMapTerrainTextureClass may be applied by itself, or with the
- CloudMapTerrainTextureClass. This may be applied in either single pass,
- as the second texture in the pipe,
- or multipass. If stage==0, we are doing multipass and we set up the pipe
- for a single texture. If stage==1, then we are doing a single pass, and we
- set up the pipe so that we blend onto the cloud map texture in stage 0.
- Also, texture is mapped using the x/y coordinates of the map, saving us
- yet another set of uv coordinates.
- (standard D3D setup, but beyond the scope of W3D). */
- //=============================================================================
- void LightMapTerrainTextureClass::Apply(unsigned int stage)
- {
- // Do the base apply.
- TextureClass::Apply(stage);
- /* previous setup */
- if (TheGlobalData && TheGlobalData->m_trilinearTerrainTex) {
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_MIPFILTER, D3DTEXF_LINEAR);
- } else {
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_MIPFILTER, D3DTEXF_POINT);
- }
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_MINFILTER, D3DTEXF_POINT);
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_MAGFILTER, D3DTEXF_LINEAR);
- // Disable 3rd stage just in case.
- DX8Wrapper::Set_DX8_Texture_Stage_State( 2, D3DTSS_COLOROP, D3DTOP_DISABLE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 2, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
- // Now setup the texture pipeline.
- DX8Wrapper::Set_DX8_Texture_Stage_State( stage, D3DTSS_COLORARG1, D3DTA_TEXTURE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( stage, D3DTSS_COLORARG2, D3DTA_CURRENT );
- if (stage == 0) {
- DX8Wrapper::Set_DX8_Texture_Stage_State( stage, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
- //Disable second stage
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
- } else {
- DX8Wrapper::Set_DX8_Texture_Stage_State( stage, D3DTSS_COLOROP, D3DTOP_MODULATE );
- }
- DX8Wrapper::Set_DX8_Texture_Stage_State( stage, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEPOSITION);
- // Two output coordinates are used.
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2);
- DX8Wrapper::Set_DX8_Texture_Stage_State( stage, D3DTSS_ADDRESSU, D3DTADDRESS_WRAP);
- DX8Wrapper::Set_DX8_Texture_Stage_State( stage, D3DTSS_ADDRESSV, D3DTADDRESS_WRAP);
- Matrix4 curView;
- DX8Wrapper::_Get_DX8_Transform(D3DTS_VIEW, curView);
- D3DXMATRIX inv;
- float det;
- D3DXMatrixInverse(&inv, &det, (D3DXMATRIX*)&curView);
- D3DXMATRIX scale;
- D3DXMatrixScaling(&scale, STRETCH_FACTOR, STRETCH_FACTOR,1);
- inv *=scale;
- if (stage==0) {
- DX8Wrapper::_Set_DX8_Transform(D3DTS_TEXTURE0, *((Matrix4*)&inv));
- } if (stage==1) {
- DX8Wrapper::_Set_DX8_Transform(D3DTS_TEXTURE1, *((Matrix4*)&inv));
- }
-
-
- if (stage==0) {
- DX8Wrapper::Set_DX8_Render_State(D3DRS_ALPHABLENDENABLE,true);
- DX8Wrapper::Set_DX8_Render_State(D3DRS_SRCBLEND,D3DBLEND_DESTCOLOR);
- DX8Wrapper::Set_DX8_Render_State(D3DRS_DESTBLEND,D3DBLEND_ZERO);
- }
- }
- /******************************************************************************
- AlphaEdgeTextureClass
- ******************************************************************************/
- //-----------------------------------------------------------------------------
- // Public Functions
- //-----------------------------------------------------------------------------
- /**
- * AlphaEdgeTextureClass - Generates the alpha edge blending for terrain.
- *
- */
- AlphaEdgeTextureClass::AlphaEdgeTextureClass( int height, MipCountType mipLevelCount) :
- // TextureClass("EdgingTemplate.tga","EdgingTemplate.tga", mipLevelCount )
- TextureClass(TEXTURE_WIDTH, height, WW3D_FORMAT_A8R8G8B8, mipLevelCount )
- {
- }
- int AlphaEdgeTextureClass::update256(WorldHeightMap *htMap)
- {
- return 1;
- }
- int AlphaEdgeTextureClass::update(WorldHeightMap *htMap)
- {
- // D3DTexture is our texture;
- IDirect3DSurface8 *surface_level;
- D3DSURFACE_DESC surface_desc;
- D3DLOCKED_RECT locked_rect;
- DX8_ErrorCode(D3DTexture->GetSurfaceLevel(0, &surface_level));
- DX8_ErrorCode(surface_level->LockRect(&locked_rect, NULL, 0));
- DX8_ErrorCode(surface_level->GetDesc(&surface_desc));
- Int tilePixelExtent = TILE_PIXEL_EXTENT; // blend tiles are 1/4 tiles.
- // Int tilesPerRow = surface_desc.Width / (tilePixelExtent+8);
- // Int numRows = surface_desc.Height/(tilePixelExtent+8);
- if (surface_desc.Format == D3DFMT_A8R8G8B8) {
- #if 1
- #if 1
- Int cellX, cellY;
- for (cellX = 0; (UnsignedInt)cellX < surface_desc.Width; cellX++) {
- for (cellY = 0; cellY < surface_desc.Height; cellY++) {
- UnsignedByte *pBGR = ((UnsignedByte *)locked_rect.pBits)+(cellY*surface_desc.Width+cellX)*4;
- pBGR[2] = 255-cellY/2;
- pBGR[0] = cellX/2;
- pBGR[3] = cellX/2; // alpha.
- pBGR[3] = 128; // alpha.
- }
- }
- #endif
- #if 1
- Int tileNdx;
- Int pixelBytes = 4;
- for (tileNdx=0; tileNdx < htMap->m_numEdgeTiles; tileNdx++) {
- TileData *pTile = htMap->getEdgeTile(tileNdx);
- if (!pTile) continue;
- ICoord2D position = pTile->m_tileLocationInTexture;
- if (position.x<=0) continue; // all real edge offsets start at 4. jba.
- Int i,j;
- Int column = position.x;
- for (j=0; j<tilePixelExtent; j++) {
- Int row = position.y+j;
- UnsignedByte *pBGR = htMap->getEdgeTile(tileNdx)->getRGBDataForWidth(tilePixelExtent);
- pBGR += (tilePixelExtent-1-j)*TILE_BYTES_PER_PIXEL*tilePixelExtent; // invert to match.
- UnsignedByte *pBGRX = ((UnsignedByte*)locked_rect.pBits) +
- (row)*surface_desc.Width*pixelBytes;
- pBGRX += column*pixelBytes;
- for (i=0; i<tilePixelExtent; i++) {
- pBGRX[0] = pBGR[0]; //r
- pBGRX[1] = pBGR[1]; //g
- pBGRX[2] = pBGR[2]; //b
- if (pBGR[0]==0 && pBGR[1]==0 && pBGR[2]==0) {
- pBGRX[3] = 0x80;
- } else if (pBGR[0]==0xff && pBGR[1]==0xff && pBGR[2]==0xff) {
- pBGRX[3] = 0x00;
- } else {
- pBGRX[3] = 0xff;
- }
-
- pBGRX += pixelBytes;
- pBGR += TILE_BYTES_PER_PIXEL;
- }
- }
- }
- #endif
- #endif
- }
- surface_level->UnlockRect();
- surface_level->Release();
- DX8_ErrorCode(D3DXFilterTexture(D3DTexture, NULL, 0, D3DX_FILTER_BOX));
- return(surface_desc.Height);
- }
- void AlphaEdgeTextureClass::Apply(unsigned int stage)
- {
- // Do the base apply.
- TextureClass::Apply(stage);
-
- if (TheGlobalData && TheGlobalData->m_bilinearTerrainTex || TheGlobalData->m_trilinearTerrainTex) {
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_MINFILTER, D3DTEXF_LINEAR);
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_MAGFILTER, D3DTEXF_LINEAR);
- } else {
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_MINFILTER, D3DTEXF_POINT);
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_MAGFILTER, D3DTEXF_POINT);
- }
- if (TheGlobalData && TheGlobalData->m_trilinearTerrainTex) {
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_MIPFILTER, D3DTEXF_LINEAR);
- } else {
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_MIPFILTER, D3DTEXF_POINT);
- }
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP);
- // Now setup the texture pipeline.
- if (stage==0) {
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_TEXCOORDINDEX, 1 );
- DX8Wrapper::Set_DX8_Render_State(D3DRS_ALPHABLENDENABLE,true);
- DX8Wrapper::Set_DX8_Render_State(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
- DX8Wrapper::Set_DX8_Render_State(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
- } else if (stage==1) {
- // Drawing texture through the mask.
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_ALPHAARG1, D3DTA_CURRENT );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_COLORARG1, D3DTA_CURRENT );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_COLORARG2, D3DTA_TEXTURE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_ALPHAARG1, D3DTA_CURRENT );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_ALPHAARG2, D3DTA_TEXTURE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG2 );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_TEXCOORDINDEX, 1 );
- DX8Wrapper::Set_DX8_Render_State(D3DRS_ALPHABLENDENABLE,true);
- DX8Wrapper::Set_DX8_Render_State(D3DRS_SRCBLEND,D3DBLEND_ONE);
- DX8Wrapper::Set_DX8_Render_State(D3DRS_DESTBLEND,D3DBLEND_ZERO);
- }
- }
- /******************************************************************************
- CloudMapTerrainTextureClass
- ******************************************************************************/
- //-----------------------------------------------------------------------------
- // Public Functions
- //-----------------------------------------------------------------------------
- //=============================================================================
- // CloudMapTerrainTextureClass::CloudMapTerrainTextureClass
- //=============================================================================
- /** Constructor. Calls parent constructor to load the .tga texture, and sets
- up the "sliding" parameters for the clouds to slide over the terrain. */
- //=============================================================================
- //@todo - Allow adjustment of the cloud slide rate, and lose the hard coded "cloudmap.tga"
- CloudMapTerrainTextureClass::CloudMapTerrainTextureClass(MipCountType mipLevelCount) :
- TextureClass("TSCloudMed.tga","TSCloudMed.tga", mipLevelCount )
- {
- Set_Mip_Mapping( FILTER_TYPE_FAST );
- m_xSlidePerSecond = -0.02f;
- m_ySlidePerSecond = 1.50f * m_xSlidePerSecond;
- m_curTick = 0;
- m_xOffset = 0;
- m_yOffset = 0;
- }
- //=============================================================================
- // CloudMapTerrainTextureClass::Apply
- //=============================================================================
- /** Sets the texture as the current D3D texture, and does some custom setup.
- The CloudMapTerrainTextureClass may be applied by itself, or with the
- LightMapTerrainTexture. This may be applied in either single pass,
- as the first texture in the pipe with LightMapTerrainTextureClass as the
- second stage of the pape, or multipass. We setup for stage 0, assuming that
- we are the only texture, as LightMapTerrainTexture will adjust for multitexture
- if it is applied to stage 1.
- Also, texture is mapped using the x/y coordinates of the map, saving us
- yet another set of uv coordinates.
- (standard D3D setup, but beyond the scope of W3D). */
- //=============================================================================
- void CloudMapTerrainTextureClass::Apply(unsigned int stage)
- {
- // Do the base apply.
- TextureClass::Apply(stage);
- /* previous setup */
- if (TheGlobalData && TheGlobalData->m_trilinearTerrainTex) {
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_MIPFILTER, D3DTEXF_LINEAR);
- } else {
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_MIPFILTER, D3DTEXF_POINT);
- }
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_MINFILTER, D3DTEXF_LINEAR);
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_MAGFILTER, D3DTEXF_LINEAR);
- // Now setup the texture pipeline.
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEPOSITION);
- // Two output coordinates are used.
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2);
- DX8Wrapper::Set_DX8_Texture_Stage_State( stage, D3DTSS_ADDRESSU, D3DTADDRESS_WRAP);
- DX8Wrapper::Set_DX8_Texture_Stage_State( stage, D3DTSS_ADDRESSV, D3DTADDRESS_WRAP);
- Matrix4 curView;
- DX8Wrapper::_Get_DX8_Transform(D3DTS_VIEW, curView);
- D3DXMATRIX inv;
- float det;
- D3DXMatrixInverse(&inv, &det, (D3DXMATRIX*)&curView);
- D3DXMATRIX scale;
- D3DXMatrixScaling(&scale, STRETCH_FACTOR, STRETCH_FACTOR,1);
- inv *=scale;
- D3DXMATRIX offset;
- Int delta = m_curTick;
- m_curTick = ::GetTickCount();
- delta = m_curTick-delta;
- m_xOffset += m_xSlidePerSecond*delta/1000;
- m_yOffset += m_ySlidePerSecond*delta/1000;
- if (m_xOffset > 1) m_xOffset -= 1;
- if (m_yOffset > 1) m_yOffset -= 1;
- if (m_xOffset < -1) m_xOffset += 1;
- if (m_yOffset < -1) m_yOffset += 1;
- D3DXMatrixTranslation(&offset, m_xOffset, m_yOffset,0);
- inv *= offset;
- if (stage==0) {
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
- DX8Wrapper::_Set_DX8_Transform(D3DTS_TEXTURE0, *((Matrix4*)&inv));
-
- // Disable 3rd stage just in case.
- DX8Wrapper::Set_DX8_Texture_Stage_State( 2, D3DTSS_COLOROP, D3DTOP_DISABLE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 2, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
- DX8Wrapper::Set_DX8_Render_State(D3DRS_ALPHABLENDENABLE,true);
- DX8Wrapper::Set_DX8_Render_State(D3DRS_SRCBLEND,D3DBLEND_DESTCOLOR);
- DX8Wrapper::Set_DX8_Render_State(D3DRS_DESTBLEND,D3DBLEND_ZERO);
- } else if (stage==1) {
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_COLORARG1, D3DTA_TEXTURE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_COLORARG2, D3DTA_CURRENT );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_COLOROP, D3DTOP_MODULATE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_ALPHAARG1, D3DTA_CURRENT );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
- DX8Wrapper::_Set_DX8_Transform(D3DTS_TEXTURE1, *((Matrix4*)&inv));
- }
- }
- //=============================================================================
- // CloudMapTerrainTextureClass::restore
- //=============================================================================
- /** Cleans up any custom settings to the texturing pipeline that may not be
- understood by w3d. */
- //=============================================================================
- void CloudMapTerrainTextureClass::restore(void)
- {
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_ADDRESSU, D3DTADDRESS_WRAP);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_ADDRESSV, D3DTADDRESS_WRAP);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_TEXCOORDINDEX, 0 );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_COLORARG1, D3DTA_TEXTURE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_COLOROP, D3DTOP_MODULATE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_ADDRESSU, D3DTADDRESS_WRAP);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_ADDRESSV, D3DTADDRESS_WRAP);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_TEXCOORDINDEX, 0 );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE);
- DX8Wrapper::Set_DX8_Render_State(D3DRS_ALPHABLENDENABLE,false);
- DX8Wrapper::Set_DX8_Render_State(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
- DX8Wrapper::Set_DX8_Render_State(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
- if (TheGlobalData && !TheGlobalData->m_multiPassTerrain)
- {
- ///@todo: Remove 8-Stage Nvidia hack after drivers are fixed.
- //This method is a backdoor specific to Nvidia based cards. It will fail on
- //other hardware. Allows single pass blend of 2 textures and post modulate diffuse.
- Int i;
- for (i=0; i<8; i++) {
- DX8Wrapper::Set_DX8_Texture_Stage_State( i, D3DTSS_COLOROP, D3DTOP_DISABLE);
- DX8Wrapper::Set_DX8_Texture_Stage_State( i, D3DTSS_TEXCOORDINDEX, i);
- DX8Wrapper::Set_DX8_Texture_Stage_State( i, D3DTSS_COLORARG1, D3DTA_TEXTURE);
- DX8Wrapper::Set_DX8_Texture_Stage_State( i, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
- DX8Wrapper::Set_DX8_Texture_Stage_State( i, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
- DX8Wrapper::Set_DX8_Texture_Stage_State( i, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
- DX8Wrapper::Set_DX8_Texture_Stage_State( i, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
- DX8Wrapper::Set_DX8_Texture(i, NULL);
- }
- }
- }
- /******************************************************************************
- ScorchTextureClass
- ******************************************************************************/
- //-----------------------------------------------------------------------------
- // Public Functions
- //-----------------------------------------------------------------------------
- //=============================================================================
- // ScorchTextureClass::ScorchTextureClass
- //=============================================================================
- /** Constructor. Calls parent constructor to load the .tga texture. */
- //=============================================================================
- /// @todo - get "EXScorch01.tga" from not hard coded location.
- ScorchTextureClass::ScorchTextureClass(MipCountType mipLevelCount) :
- TextureClass("EXScorch01.tga","EXScorch01.tga", mipLevelCount )
- {
- }
- //=============================================================================
- // ScorchTextureClass::Apply
- //=============================================================================
- /** Sets the texture as the current D3D texture, and does some custom setup.
- The ScorchTextureClass is applied by iteself, as it's mesh is a subset of the
- terrain mesh.
- (standard D3D setup, but beyond the scope of W3D). */
- //=============================================================================
- void ScorchTextureClass::Apply(unsigned int stage)
- {
- // Do the base apply.
- TextureClass::Apply(stage);
- // Setup bilinear or trilinear filtering as specified in global data.
- if (TheGlobalData && TheGlobalData->m_bilinearTerrainTex || TheGlobalData->m_trilinearTerrainTex) {
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_MINFILTER, D3DTEXF_LINEAR);
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_MAGFILTER, D3DTEXF_LINEAR);
- } else {
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_MINFILTER, D3DTEXF_POINT);
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_MAGFILTER, D3DTEXF_POINT);
- }
- if (TheGlobalData && TheGlobalData->m_trilinearTerrainTex) {
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_MIPFILTER, D3DTEXF_LINEAR);
- } else {
- DX8Wrapper::Set_DX8_Texture_Stage_State(stage, D3DTSS_MIPFILTER, D3DTEXF_POINT);
- }
- DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP);
- // Now setup the texture pipeline.
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 0, D3DTSS_TEXCOORDINDEX, 0 );
- DX8Wrapper::Set_DX8_Render_State(D3DRS_ALPHABLENDENABLE,true);
- DX8Wrapper::Set_DX8_Render_State(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
- DX8Wrapper::Set_DX8_Render_State(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );
- DX8Wrapper::Set_DX8_Texture_Stage_State( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
- }
|