From f2a3a1e2a67064e6f4369f6540db51e780c080ba Mon Sep 17 00:00:00 2001 From: Lukas Joergensen Date: Tue, 23 Sep 2014 12:56:48 +0200 Subject: [PATCH 1/3] Enable RGB format for DDS files getting copied to bitmap. --- Engine/source/gfx/D3D9/gfxD3D9TextureObject.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Engine/source/gfx/D3D9/gfxD3D9TextureObject.cpp b/Engine/source/gfx/D3D9/gfxD3D9TextureObject.cpp index ff9c69096..bdf0ef449 100644 --- a/Engine/source/gfx/D3D9/gfxD3D9TextureObject.cpp +++ b/Engine/source/gfx/D3D9/gfxD3D9TextureObject.cpp @@ -205,8 +205,8 @@ bool GFXD3D9TextureObject::copyToBmp(GBitmap* bmp) // check format limitations // at the moment we only support RGBA for the source (other 4 byte formats should // be easy to add though) - AssertFatal(mFormat == GFXFormatR8G8B8A8, "copyToBmp: invalid format"); - if (mFormat != GFXFormatR8G8B8A8) + AssertFatal(mFormat == GFXFormatR8G8B8A8 || mFormat == GFXFormatR8G8B8, "copyToBmp: invalid format"); + if (mFormat != GFXFormatR8G8B8A8 && mFormat != GFXFormatR8G8B8) return false; PROFILE_START(GFXD3D9TextureObject_copyToBmp); From 431d8a9b66dc29e3df91f999b7dcc500004af707 Mon Sep 17 00:00:00 2001 From: LukasPJ Date: Tue, 23 Sep 2014 07:49:27 +0200 Subject: [PATCH 2/3] Terrain baseTex support multiple formats DDS, PNG, JPG or NONE --- Engine/source/terrain/terrData.cpp | 5 +++-- Engine/source/terrain/terrData.h | 22 ++++++++++++++++++++++ Engine/source/terrain/terrRender.cpp | 25 +++++++++++++++++++------ 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/Engine/source/terrain/terrData.cpp b/Engine/source/terrain/terrData.cpp index 9db97f28a..1dd89d7b3 100644 --- a/Engine/source/terrain/terrData.cpp +++ b/Engine/source/terrain/terrData.cpp @@ -186,6 +186,7 @@ TerrainBlock::TerrainBlock() mCell( NULL ), mCRC( 0 ), mBaseTexSize( 1024 ), + mBaseTexFormat( TerrainBlock::JPG ), mBaseMaterial( NULL ), mDefaultMatInst( NULL ), mBaseTexScaleConst( NULL ), @@ -961,7 +962,7 @@ String TerrainBlock::_getBaseTexCacheFileName() const { Torque::Path basePath( mTerrFileName ); basePath.setFileName( basePath.getFileName() + "_basetex" ); - basePath.setExtension( "dds" ); + basePath.setExtension( formatToExtension(mBaseTexFormat) ); return basePath.getFullPath(); } @@ -1200,7 +1201,7 @@ void TerrainBlock::unpackUpdate(NetConnection* con, BitStream *stream) { mBaseTexSize = baseTexSize; if ( isProperlyAdded() ) - _updateBaseTexture( false ); + _updateBaseTexture( NONE ); } U32 lightMapSize; diff --git a/Engine/source/terrain/terrData.h b/Engine/source/terrain/terrData.h index 967bfa0d1..b3433dfb1 100644 --- a/Engine/source/terrain/terrData.h +++ b/Engine/source/terrain/terrData.h @@ -74,6 +74,26 @@ protected: NextFreeMask = Parent::NextFreeMask << 6, }; + enum BaseTexFormat + { + NONE, DDS, PNG, JPG + }; + + static const char* formatToExtension(BaseTexFormat format) + { + switch (format) + { + case DDS: + return "dds"; + case PNG: + return "png"; + case JPG: + return "jpg"; + default: + return ""; + } + }; + Box3F mBounds; /// @@ -132,6 +152,8 @@ protected: /// The desired size for the base texture. U32 mBaseTexSize; + BaseTexFormat mBaseTexFormat; + /// TerrCell *mCell; diff --git a/Engine/source/terrain/terrRender.cpp b/Engine/source/terrain/terrRender.cpp index 4b03ac812..61b844380 100644 --- a/Engine/source/terrain/terrRender.cpp +++ b/Engine/source/terrain/terrRender.cpp @@ -178,7 +178,7 @@ bool TerrainBlock::_initBaseShader() return true; } -void TerrainBlock::_updateBaseTexture( bool writeToCache ) +void TerrainBlock::_updateBaseTexture(bool writeToCache) { if ( !mBaseShader && !_initBaseShader() ) return; @@ -290,7 +290,14 @@ void TerrainBlock::_updateBaseTexture( bool writeToCache ) GFX->endScene(); /// Do we cache this sucker? - if ( writeToCache ) + if (mBaseTexFormat == NONE || !writeToCache) + { + // We didn't cache the result, so set the base texture + // to the render target we updated. This should be good + // for realtime painting cases. + mBaseTex = blendTex; + } + else if (mBaseTexFormat == DDS) { String cachePath = _getBaseTexCacheFileName(); @@ -327,10 +334,16 @@ void TerrainBlock::_updateBaseTexture( bool writeToCache ) } else { - // We didn't cache the result, so set the base texture - // to the render target we updated. This should be good - // for realtime painting cases. - mBaseTex = blendTex; + FileStream stream; + if (!stream.open(_getBaseTexCacheFileName(), Torque::FS::File::Write)) + { + mBaseTex = blendTex; + return; + } + + GBitmap bitmap(blendTex->getWidth(), blendTex->getHeight(), false, GFXFormatR8G8B8); + blendTex->copyToBmp(&bitmap); + bitmap.writeBitmap(formatToExtension(mBaseTexFormat), stream); } } From ef11b565bf17e49be99ac35e31ee54b80148af98 Mon Sep 17 00:00:00 2001 From: Lukas Joergensen Date: Tue, 23 Sep 2014 13:40:26 +0200 Subject: [PATCH 3/3] Expose new formats to the editor. --- Engine/source/terrain/terrData.cpp | 37 ++++++++++++++++++++++++++++++ Engine/source/terrain/terrData.h | 7 +++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/Engine/source/terrain/terrData.cpp b/Engine/source/terrain/terrData.cpp index 1dd89d7b3..913232298 100644 --- a/Engine/source/terrain/terrData.cpp +++ b/Engine/source/terrain/terrData.cpp @@ -174,6 +174,18 @@ ConsoleFunction(getTerrainUnderWorldPoint, S32, 2, 4, "(Point3F x/y/z) Gets the } +typedef TerrainBlock::BaseTexFormat baseTexFormat; +DefineEnumType(baseTexFormat); + +ImplementEnumType(baseTexFormat, + "Description\n" + "@ingroup ?\n\n") +{ TerrainBlock::NONE, "NONE", "No cached terrain.\n" }, +{ TerrainBlock::DDS, "DDS", "Cache the terrain in a DDS format.\n" }, +{ TerrainBlock::PNG, "PNG", "Cache the terrain in a PNG format.\n" }, +{ TerrainBlock::JPG, "JPG", "Cache the terrain in a JPG format.\n" }, +EndImplementEnumType; + TerrainBlock::TerrainBlock() : mSquareSize( 1.0f ), mCastShadows( true ), @@ -270,6 +282,27 @@ bool TerrainBlock::_setBaseTexSize( void *obj, const char *index, const char *da return false; } +bool TerrainBlock::_setBaseTexFormat(void *obj, const char *index, const char *data) +{ + TerrainBlock *terrain = static_cast(obj); + + EngineEnumTable eTable = _baseTexFormat::_sEnumTable; + + for (U8 i = 0; i < eTable.getNumValues(); i++) + { + if (strcasecmp(eTable[i].mName, data) == 0) + { + terrain->mBaseTexFormat = (BaseTexFormat)eTable[i].mInt; + terrain->_updateMaterials(); + terrain->_updateLayerTexture(); + terrain->_updateBaseTexture(true); + break; + } + } + + return false; +} + bool TerrainBlock::_setLightMapSize( void *obj, const char *index, const char *data ) { TerrainBlock *terrain = static_cast(obj); @@ -1105,6 +1138,10 @@ void TerrainBlock::initPersistFields() &TerrainBlock::_setBaseTexSize, &defaultProtectedGetFn, "Size of base texture size per meter." ); + addProtectedField("baseTexFormat", TYPEID(), Offset(mBaseTexFormat, TerrainBlock), + &TerrainBlock::_setBaseTexFormat, &defaultProtectedGetFn, + ""); + addProtectedField( "lightMapSize", TypeS32, Offset( mLightMapSize, TerrainBlock ), &TerrainBlock::_setLightMapSize, &defaultProtectedGetFn, "Light map dimensions in pixels." ); diff --git a/Engine/source/terrain/terrData.h b/Engine/source/terrain/terrData.h index b3433dfb1..91618df73 100644 --- a/Engine/source/terrain/terrData.h +++ b/Engine/source/terrain/terrData.h @@ -74,6 +74,8 @@ protected: NextFreeMask = Parent::NextFreeMask << 6, }; +public: + enum BaseTexFormat { NONE, DDS, PNG, JPG @@ -94,6 +96,8 @@ protected: } }; +protected: + Box3F mBounds; /// @@ -235,7 +239,8 @@ protected: // Protected fields static bool _setTerrainFile( void *obj, const char *index, const char *data ); static bool _setSquareSize( void *obj, const char *index, const char *data ); - static bool _setBaseTexSize( void *obj, const char *index, const char *data ); + static bool _setBaseTexSize(void *obj, const char *index, const char *data); + static bool _setBaseTexFormat(void *obj, const char *index, const char *data); static bool _setLightMapSize( void *obj, const char *index, const char *data ); public: