| 1 | // tCubemap.cpp  |
| 2 | //  |
| 3 | // A cubemap is simply a collection of 6 tTextures. By convention each tTexture has layers of the same pixel format. A  |
| 4 | // tCubemap may be created from a dds file (assuming the dds file contains a cubemap) or from multiple tTextures. When  |
| 5 | // construction from a dds is performed, it chooses a different image to load into the tTexture for each cubemap side.  |
| 6 | // When constructing from tTextures directly it is the user's responsibility to supply the necessary tTextures in the  |
| 7 | // correct format before constructing a tCubemap. tCubemaps can also save and load themselves into a tChunk-based  |
| 8 | // format. They do this by looping through their tTextures and calling their individual Save/Load functions.  |
| 9 | //  |
| 10 | // ATI has a tool called CubeMapGen 1.1 which is useful for making your own cube map dds files. Its installer also  |
| 11 | // installs some example dds cube maps to play with. The CubeMapGen tool (as of v1.1) does NOT generate valid DXT1  |
| 12 | // compressed cubemap files. The header is incorrect and the data is not compressed.  |
| 13 | //  |
| 14 | // nVidia's DDS tools can also generate dds cubemaps straight from photoshop. Just pull down the combo-box that usually  |
| 15 | // says "2D TEXTURE" and there'll be an option for cubemaps. The nVidia dds plugin for photoshop does not have this  |
| 16 | // issues with texture compression and cubemaps.  |
| 17 | //  |
| 18 | // The order of the images for a cubemap are +x -x +y -y +z -z. In the CubeMapGen tool the option for OpenGL and DX  |
| 19 | // coord systems only affects the export of cube map faces -- it does not, apparently, affect the output dds cubemap.  |
| 20 | // i.e. It seems the faces should always be specified using a LH coord system. The OpenGL calls for specifying a  |
| 21 | // cubemap are also left handed which is inconsistent with other parts of the OpenGL API.  |
| 22 | //  |
| 23 | // Copyright (c) 2006, 2017, 2019, 2020 Tristan Grimmer.  |
| 24 | // Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby  |
| 25 | // granted, provided that the above copyright notice and this permission notice appear in all copies.  |
| 26 | //  |
| 27 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL  |
| 28 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,  |
| 29 | // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN  |
| 30 | // AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR  |
| 31 | // PERFORMANCE OF THIS SOFTWARE.  |
| 32 |   |
| 33 | #include "Image/tCubemap.h"  |
| 34 | using namespace tImage;  |
| 35 |   |
| 36 |   |
| 37 | bool tCubemap::IsValid() const  |
| 38 | {  |
| 39 | for (int side = 0; side < int(tSide::NumSides); side++)  |
| 40 | if ((Sides[side].Side != tSide::Invalid) && Sides[side].Texture.IsValid())  |
| 41 | return true;  |
| 42 |   |
| 43 | return false;  |
| 44 | }  |
| 45 |   |
| 46 |   |
| 47 | bool tCubemap::AllSidesOpaque() const  |
| 48 | {  |
| 49 | for (int side = 0; side < int(tSide::NumSides); side++)  |
| 50 | if ((Sides[side].Side != tSide::Invalid) && Sides[side].Texture.IsValid() && !Sides[side].Texture.IsOpaque())  |
| 51 | return false;  |
| 52 |   |
| 53 | return true;  |
| 54 | }  |
| 55 |   |
| 56 |   |
| 57 | void tCubemap::Clear()  |
| 58 | {  |
| 59 | for (int side = 0; side < int(tSide::NumSides); side++)  |
| 60 | {  |
| 61 | Sides[side].Side = tSide::Invalid;  |
| 62 | Sides[side].Texture.Clear();  |
| 63 | }  |
| 64 | }  |
| 65 |   |
| 66 |   |
| 67 | bool tCubemap::Load(const tString& ddsFile, bool reverseRowOrder)  |
| 68 | {  |
| 69 | Clear();  |
| 70 | if ((tSystem::tGetFileType(ddsFile) != tSystem::tFileType::DDS) || !tSystem::tFileExists(ddsFile))  |
| 71 | return false;  |
| 72 |   |
| 73 | tImageDDS dds(ddsFile, reverseRowOrder);  |
| 74 | if (!dds.IsValid() || !dds.IsCubemap())  |
| 75 | return false;  |
| 76 |   |
| 77 | return Set(dds);  |
| 78 | }  |
| 79 |   |
| 80 |   |
| 81 | bool tCubemap::Set(tImageDDS& dds)  |
| 82 | {  |
| 83 | Clear();  |
| 84 | if (!dds.IsValid() || !dds.IsCubemap())  |
| 85 | return false;  |
| 86 |   |
| 87 | tList<tLayer> layerSet[tImageDDS::tSurfIndex_NumSurfaces];  |
| 88 | tStaticAssert(int(tSide::NumSides) == tImageDDS::tSurfIndex_NumSurfaces);  |
| 89 | dds.StealCubemapLayers(layerSet);  |
| 90 |   |
| 91 | for (int side = 0; side < int(tSide::NumSides); side++)  |
| 92 | {  |
| 93 | Sides[side].Side = tSide(side);  |
| 94 | Sides[side].Texture.Set(layerSet[side]);  |
| 95 | }  |
| 96 |   |
| 97 | if (!IsValid())  |
| 98 | {  |
| 99 | Clear();  |
| 100 | return false;  |
| 101 | }  |
| 102 |   |
| 103 | return true;  |
| 104 | }  |
| 105 |   |
| 106 |   |
| 107 | bool tCubemap::Load  |
| 108 | (  |
| 109 | const tString& imageFilePosX, const tString& imageFileNegX,  |
| 110 | const tString& imageFilePosY, const tString& imageFileNegY,  |
| 111 | const tString& imageFilePosZ, const tString& imageFileNegZ,  |
| 112 | bool generateMipMaps, tPixelFormat pixelFormat, tTexture::tQuality quality, int forceWidth, int forceHeight  |
| 113 | )  |
| 114 | {  |
| 115 | Clear();  |
| 116 | tPicture posX(imageFilePosX); tPicture negX(imageFileNegX);  |
| 117 | tPicture posY(imageFilePosY); tPicture negY(imageFileNegY);  |
| 118 | tPicture posZ(imageFilePosZ); tPicture negZ(imageFileNegZ);  |
| 119 | return Set  |
| 120 | (  |
| 121 | posX, negX, posY, negY, posZ, negZ,  |
| 122 | generateMipMaps, pixelFormat, quality, forceWidth, forceHeight  |
| 123 | );  |
| 124 | }  |
| 125 |   |
| 126 |   |
| 127 | bool tCubemap::Set  |
| 128 | (  |
| 129 | tPicture& imagePosX, tPicture& imageNegX,  |
| 130 | tPicture& imagePosY, tPicture& imageNegY,  |
| 131 | tPicture& imagePosZ, tPicture& imageNegZ,  |
| 132 | bool generateMipMaps, tPixelFormat pixelFormat, tTexture::tQuality quality, int forceWidth, int forceHeight  |
| 133 | )  |
| 134 | {  |
| 135 | Clear();  |
| 136 |   |
| 137 | tTexture textures[int(tSide::NumSides)];  |
| 138 | textures[0].Set(imagePosX, generateMipMaps, pixelFormat, quality, forceWidth, forceHeight);  |
| 139 | textures[1].Set(imageNegX, generateMipMaps, pixelFormat, quality, forceWidth, forceHeight);  |
| 140 | textures[2].Set(imagePosY, generateMipMaps, pixelFormat, quality, forceWidth, forceHeight);  |
| 141 | textures[3].Set(imageNegY, generateMipMaps, pixelFormat, quality, forceWidth, forceHeight);  |
| 142 | textures[4].Set(imagePosZ, generateMipMaps, pixelFormat, quality, forceWidth, forceHeight);  |
| 143 | textures[5].Set(imageNegZ, generateMipMaps, pixelFormat, quality, forceWidth, forceHeight);  |
| 144 |   |
| 145 | for (int side = 0; side < int(tSide::NumSides); side++)  |
| 146 | {  |
| 147 | Sides[side].Side = textures[side].IsValid() ? tSide(side) : tSide::Invalid;  |
| 148 |   |
| 149 | // Steal the layers from the texture and give them to the cubemap.  |
| 150 | tList<tLayer> layers;  |
| 151 | textures[side].StealLayers(layers);  |
| 152 | Sides[side].Texture.Set(layers);  |
| 153 | }  |
| 154 |   |
| 155 | if (!IsValid())  |
| 156 | {  |
| 157 | Clear();  |
| 158 | return false;  |
| 159 | }  |
| 160 |   |
| 161 | return true;  |
| 162 | }  |
| 163 |   |
| 164 |   |
| 165 | tTexture* tCubemap::GetSide(tSide side)  |
| 166 | {  |
| 167 | if (!IsValid())  |
| 168 | return nullptr;  |
| 169 |   |
| 170 | return &Sides[int(side)].Texture;  |
| 171 | }  |
| 172 |   |
| 173 |   |
| 174 | void tCubemap::Save(tChunkWriter& chunk) const  |
| 175 | {  |
| 176 | chunk.Begin(tChunkID::Image_Cubemap);  |
| 177 | {  |
| 178 | chunk.Begin(tChunkID::Image_CubemapProperties);  |
| 179 | {  |
| 180 | chunk.Write( AllSidesOpaque() );  |
| 181 | }  |
| 182 | chunk.End();  |
| 183 |   |
| 184 | chunk.Begin(tChunkID::Image_CubemapSides);  |
| 185 | {  |
| 186 | for (int side = 0; side < int(tSide::NumSides); side++)  |
| 187 | {  |
| 188 | if ((Sides[side].Side == tSide::Invalid) || !Sides[side].Texture.IsValid())  |
| 189 | continue;  |
| 190 |   |
| 191 | chunk.Begin(tChunkID::Image_CubemapSide);  |
| 192 | {  |
| 193 | chunk.Begin(tChunkID::Image_CubemapSideProperties);  |
| 194 | {  |
| 195 | chunk.Write(tSide(side));  |
| 196 | }  |
| 197 | chunk.End();  |
| 198 |   |
| 199 | Sides[side].Texture.Save(chunk);  |
| 200 | }  |
| 201 | chunk.End();  |
| 202 | }  |
| 203 | }  |
| 204 | chunk.End();  |
| 205 | }  |
| 206 | chunk.End();  |
| 207 | }  |
| 208 |   |
| 209 |   |
| 210 | void tCubemap::Load(const tChunk& chunk)  |
| 211 | {  |
| 212 | Clear();  |
| 213 | if (chunk.ID() != tChunkID::Image_Cubemap)  |
| 214 | return;  |
| 215 |   |
| 216 | for (tChunk ch = chunk.First(); ch.IsValid(); ch = ch.Next())  |
| 217 | {  |
| 218 | switch (ch.ID())  |
| 219 | {  |
| 220 | case tChunkID::Image_CubemapProperties:  |
| 221 | {  |
| 222 | bool allOpaque = false;  |
| 223 | ch.GetItem(allOpaque);  |
| 224 | break;  |
| 225 | }  |
| 226 |   |
| 227 | case tChunkID::Image_CubemapSides:  |
| 228 | {  |
| 229 | for (tChunk sideChunk = ch.First(); sideChunk.IsValid(); sideChunk = sideChunk.Next())  |
| 230 | {  |
| 231 | tAssert(sideChunk.ID() == tChunkID::Image_CubemapSide)  |
| 232 | tSide side = tSide::Invalid;  |
| 233 | for (tChunk sideSub = sideChunk.First(); sideSub.IsValid(); sideSub = sideSub.Next())  |
| 234 | {  |
| 235 | switch (sideSub.ID())  |
| 236 | {  |
| 237 | case tChunkID::Image_CubemapSideProperties:  |
| 238 | sideSub.GetItem(side);  |
| 239 | break;  |
| 240 |   |
| 241 | case tChunkID::Image_Texture:  |
| 242 | tAssert(side != tSide::Invalid);  |
| 243 | Sides[int(side)].Side = side;  |
| 244 | Sides[int(side)].Texture.Load(sideSub);  |
| 245 | break;  |
| 246 | }  |
| 247 | }  |
| 248 | }  |
| 249 | break;  |
| 250 | }  |
| 251 | }  |
| 252 | }  |
| 253 | }  |
| 254 |   |
| 255 |   |
| 256 | bool tCubemap::operator==(const tCubemap& src) const  |
| 257 | {  |
| 258 | if (!IsValid() || !src.IsValid())  |
| 259 | return false;  |
| 260 |   |
| 261 | for (int side = 0; side < int(tSide::NumSides); side++)  |
| 262 | {  |
| 263 | if (Sides[side].Side != src.Sides[side].Side)  |
| 264 | return false;  |
| 265 |   |
| 266 | if (Sides[side].Texture != src.Sides[side].Texture)  |
| 267 | return false;  |
| 268 | }  |
| 269 |   |
| 270 | return true;  |
| 271 | }  |
| 272 | |