| 1 | // tMaterial.cpp  |
| 2 | //  |
| 3 | // This file implements a tScene material. The parameters represent a legacy Blinn-Phong material which more or less  |
| 4 | // matches the fixed function pipeline of OpenGL. More advanced materials/shader-models are handled by explicitly  |
| 5 | // setting the ShaderFile member to point to a shader, in which case the Blinn-Phong parameters are ignored.  |
| 6 | //  |
| 7 | // Copyright (c) 2006, 2017 Tristan Grimmer.  |
| 8 | // Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby  |
| 9 | // granted, provided that the above copyright notice and this permission notice appear in all copies.  |
| 10 | //  |
| 11 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL  |
| 12 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,  |
| 13 | // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN  |
| 14 | // AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR  |
| 15 | // PERFORMANCE OF THIS SOFTWARE.  |
| 16 | #include "Scene/tMaterial.h"  |
| 17 | namespace tScene  |
| 18 | {  |
| 19 |   |
| 20 |   |
| 21 | void tMaterial::Clear()  |
| 22 | {  |
| 23 | tObject::Clear();  |
| 24 | Shininess = 0.0f;  |
| 25 | Reflectivity = 0.0f;  |
| 26 | Diffusion = 0.0f;  |
| 27 | TextureDiffuse.Clear();  |
| 28 | TextureNormalMap.Clear();  |
| 29 | TextureA.Clear();  |
| 30 | TextureB.Clear();  |
| 31 | TextureC.Clear();  |
| 32 | TextureD.Clear();  |
| 33 | TextureE.Clear();  |
| 34 | ShaderFile.Clear();  |
| 35 | SpecularColour.MakeBlack();  |
| 36 | ReflectedColour.MakeBlack();  |
| 37 | DiffuseColour.MakeBlack();  |
| 38 | Transparency.MakeBlack();  |
| 39 | AmbientColour.MakeBlack();  |
| 40 | Incandescence.MakeBlack();  |
| 41 | UseDiffuseColour = false;  |
| 42 | }  |
| 43 |   |
| 44 |   |
| 45 | bool tMaterial::IsFunctionallySame(const tMaterial& mat) const  |
| 46 | {  |
| 47 | // Attributes always must be the same for a true return value.  |
| 48 | if (!tObject::IsFunctionallySame(mat)) return false;  |
| 49 | if (!ShaderFile.IsEmpty() && (ShaderFile == mat.ShaderFile))  |
| 50 | return true;  |
| 51 |   |
| 52 | if (AmbientColour != mat.AmbientColour) return false;  |
| 53 | if (DiffuseColour != mat.DiffuseColour) return false;  |
| 54 | if (SpecularColour != mat.SpecularColour) return false;  |
| 55 |   |
| 56 | if (Shininess != mat.Shininess) return false;  |
| 57 | if (Reflectivity != mat.Reflectivity) return false;  |
| 58 | if (Diffusion != mat.Diffusion) return false;  |
| 59 |   |
| 60 | if (ReflectedColour != mat.ReflectedColour) return false;  |
| 61 | if (Transparency != mat.Transparency) return false;  |
| 62 | if (Incandescence != mat.Incandescence) return false;  |
| 63 | if (UseDiffuseColour != mat.UseDiffuseColour) return false;  |
| 64 |   |
| 65 | if (TextureDiffuse != mat.TextureDiffuse) return false;  |
| 66 | if (TextureNormalMap != mat.TextureNormalMap) return false;  |
| 67 |   |
| 68 | if (TextureA != mat.TextureA) return false;  |
| 69 | if (TextureB != mat.TextureB) return false;  |
| 70 | if (TextureC != mat.TextureC) return false;  |
| 71 | if (TextureD != mat.TextureD) return false;  |
| 72 | if (TextureE != mat.TextureE) return false;  |
| 73 |   |
| 74 | return true;  |
| 75 | }  |
| 76 |   |
| 77 |   |
| 78 | void tMaterial::Save(tChunkWriter& chunk) const  |
| 79 | {  |
| 80 | chunk.Begin(tChunkID::Scene_Material);  |
| 81 | {  |
| 82 | tObject::Save(chunk);  |
| 83 |   |
| 84 | chunk.Begin(tChunkID::Scene_MaterialProperties);  |
| 85 | {  |
| 86 | chunk.Write(AmbientColour);  |
| 87 | chunk.Write(DiffuseColour);  |
| 88 | chunk.Write(SpecularColour);  |
| 89 | chunk.Write(Shininess);  |
| 90 | chunk.Write(Reflectivity);  |
| 91 | chunk.Write(Diffusion);  |
| 92 | chunk.Write(ReflectedColour);  |
| 93 | chunk.Write(Transparency);  |
| 94 | chunk.Write(Incandescence);  |
| 95 | chunk.Write(UseDiffuseColour);  |
| 96 | }  |
| 97 | chunk.End();  |
| 98 |   |
| 99 | if (!TextureDiffuse.IsEmpty())  |
| 100 | {  |
| 101 | chunk.Begin(tChunkID::Scene_MaterialTextureDiffuse);  |
| 102 | chunk.Write(TextureDiffuse);  |
| 103 | chunk.End();  |
| 104 | }  |
| 105 |   |
| 106 | if (!TextureNormalMap.IsEmpty())  |
| 107 | {  |
| 108 | chunk.Begin(tChunkID::Scene_MaterialTextureNormalMap);  |
| 109 | chunk.Write(TextureNormalMap);  |
| 110 | chunk.End();  |
| 111 | }  |
| 112 |   |
| 113 | if (!TextureA.IsEmpty())  |
| 114 | {  |
| 115 | chunk.Begin(tChunkID::Scene_MaterialTextureA);  |
| 116 | chunk.Write(TextureA);  |
| 117 | chunk.End();  |
| 118 | }  |
| 119 |   |
| 120 | if (!TextureB.IsEmpty())  |
| 121 | {  |
| 122 | chunk.Begin(tChunkID::Scene_MaterialTextureB);  |
| 123 | chunk.Write(TextureB);  |
| 124 | chunk.End();  |
| 125 | }  |
| 126 |   |
| 127 | if (!TextureC.IsEmpty())  |
| 128 | {  |
| 129 | chunk.Begin(tChunkID::Scene_MaterialTextureC);  |
| 130 | chunk.Write(TextureC);  |
| 131 | chunk.End();  |
| 132 | }  |
| 133 |   |
| 134 | if (!TextureD.IsEmpty())  |
| 135 | {  |
| 136 | chunk.Begin(tChunkID::Scene_MaterialTextureD);  |
| 137 | chunk.Write(TextureD);  |
| 138 | chunk.End();  |
| 139 | }  |
| 140 |   |
| 141 | if (!TextureE.IsEmpty())  |
| 142 | {  |
| 143 | chunk.Begin(tChunkID::Scene_MaterialTextureE);  |
| 144 | chunk.Write(TextureE);  |
| 145 | chunk.End();  |
| 146 | }  |
| 147 |   |
| 148 | if (!ShaderFile.IsEmpty())  |
| 149 | {  |
| 150 | chunk.Begin(tChunkID::Scene_MaterialShaderFile);  |
| 151 | chunk.Write(ShaderFile);  |
| 152 | chunk.End();  |
| 153 | }  |
| 154 | }  |
| 155 | chunk.End();  |
| 156 | }  |
| 157 |   |
| 158 |   |
| 159 | void tMaterial::Load(const tChunk& matChunk)  |
| 160 | {  |
| 161 | Clear();  |
| 162 | tAssert(matChunk.ID() == tChunkID::Scene_Material);  |
| 163 |   |
| 164 | for (tChunk chunk = matChunk.First(); chunk.Valid(); chunk = chunk.Next())  |
| 165 | {  |
| 166 | switch (chunk.ID())  |
| 167 | {  |
| 168 | case tChunkID::Scene_Object:  |
| 169 | tObject::Load(chunk);  |
| 170 | break;  |
| 171 |   |
| 172 | case tChunkID::Scene_MaterialProperties:  |
| 173 | chunk.GetItem(AmbientColour);  |
| 174 | chunk.GetItem(DiffuseColour);  |
| 175 | chunk.GetItem(SpecularColour);  |
| 176 | chunk.GetItem(Shininess);  |
| 177 | chunk.GetItem(Reflectivity);  |
| 178 | chunk.GetItem(Diffusion);  |
| 179 | chunk.GetItem(ReflectedColour);  |
| 180 | chunk.GetItem(Transparency);  |
| 181 | chunk.GetItem(Incandescence);  |
| 182 | chunk.GetItem(UseDiffuseColour);  |
| 183 | break;  |
| 184 |   |
| 185 | case tChunkID::Scene_MaterialTextureDiffuse:  |
| 186 | chunk.GetItem(TextureDiffuse);  |
| 187 | break;  |
| 188 |   |
| 189 | case tChunkID::Scene_MaterialTextureNormalMap:  |
| 190 | chunk.GetItem(TextureNormalMap);  |
| 191 | break;  |
| 192 |   |
| 193 | case tChunkID::Scene_MaterialTextureA:  |
| 194 | chunk.GetItem(TextureA);  |
| 195 | break;  |
| 196 |   |
| 197 | case tChunkID::Scene_MaterialTextureB:  |
| 198 | chunk.GetItem(TextureB);  |
| 199 | break;  |
| 200 |   |
| 201 | case tChunkID::Scene_MaterialTextureC:  |
| 202 | chunk.GetItem(TextureC);  |
| 203 | break;  |
| 204 |   |
| 205 | case tChunkID::Scene_MaterialTextureD:  |
| 206 | chunk.GetItem(TextureD);  |
| 207 | break;  |
| 208 |   |
| 209 | case tChunkID::Scene_MaterialTextureE:  |
| 210 | chunk.GetItem(TextureE);  |
| 211 | break;  |
| 212 |   |
| 213 | case tChunkID::Scene_MaterialShaderFile:  |
| 214 | chunk.GetItem(ShaderFile);  |
| 215 | break;  |
| 216 | }  |
| 217 | }  |
| 218 | }  |
| 219 |   |
| 220 |   |
| 221 | tMaterial& tMaterial::operator=(const tMaterial& src)  |
| 222 | {  |
| 223 | if (this == &src)  |
| 224 | return *this;  |
| 225 |   |
| 226 | tObject::operator=(src);  |
| 227 |   |
| 228 | AmbientColour = src.AmbientColour;  |
| 229 | DiffuseColour = src.DiffuseColour;  |
| 230 | SpecularColour = src.SpecularColour;  |
| 231 |   |
| 232 | Shininess = src.Shininess;  |
| 233 | Reflectivity = src.Reflectivity;  |
| 234 | Diffusion = src.Diffusion;  |
| 235 |   |
| 236 | ReflectedColour = src.ReflectedColour;  |
| 237 | Transparency = src.Transparency;  |
| 238 | Incandescence = src.Incandescence;  |
| 239 | UseDiffuseColour = src.UseDiffuseColour;  |
| 240 |   |
| 241 | TextureDiffuse = src.TextureDiffuse;  |
| 242 | TextureNormalMap = src.TextureNormalMap;  |
| 243 |   |
| 244 | TextureA = src.TextureA;  |
| 245 | TextureB = src.TextureB;  |
| 246 | TextureC = src.TextureC;  |
| 247 | TextureD = src.TextureD;  |
| 248 | TextureE = src.TextureE;  |
| 249 |   |
| 250 | ShaderFile = src.ShaderFile;  |
| 251 | return *this;  |
| 252 | }  |
| 253 |   |
| 254 |   |
| 255 | } |