diff options
Diffstat (limited to 'src/graphics/engine/modelfile.cpp')
-rw-r--r-- | src/graphics/engine/modelfile.cpp | 1205 |
1 files changed, 823 insertions, 382 deletions
diff --git a/src/graphics/engine/modelfile.cpp b/src/graphics/engine/modelfile.cpp index 9d7a389..2049749 100644 --- a/src/graphics/engine/modelfile.cpp +++ b/src/graphics/engine/modelfile.cpp @@ -23,120 +23,42 @@ #include "common/ioutils.h" #include "common/logger.h" #include "common/stringutils.h" +#include "graphics/engine/engine.h" #include "math/geometry.h" #include <string.h> #include <fstream> +#include <sstream> -//! How big the triangle vector is by default -const int TRIANGLE_PREALLOCATE_COUNT = 2000; - -/** - \struct ModelHeader - \brief Header info for model file +/* + * NOTE: #ifndef checking for MODELFILE_NO_ENGINE + * is provided in this module to conditionally + * disable dependence on CEngine. */ -struct ModelHeader -{ - //! Revision number - int revision; - //! Version number - int version; - //! Total number of vertices - int totalVertices; - //! Reserved area - int reserved[10]; - - ModelHeader() - { - memset(this, 0, sizeof(*this)); - } -}; -struct OldModelTriangle1 -{ - char used; - char selected; - Gfx::Vertex p1; - Gfx::Vertex p2; - Gfx::Vertex p3; - Gfx::Material material; - char texName[20]; - float min; - float max; - - OldModelTriangle1() - { - memset(this, 0, sizeof(*this)); - } -}; - -struct OldModelTriangle2 -{ - char used; - char selected; - Gfx::Vertex p1; - Gfx::Vertex p2; - Gfx::Vertex p3; - Gfx::Material material; - char texName[20]; - float min; - float max; - long state; - short reserved1; - short reserved2; - short reserved3; - short reserved4; - OldModelTriangle2() - { - memset(this, 0, sizeof(*this)); - } -}; - - -struct NewModelTriangle -{ - char used; - char selected; - Gfx::VertexTex2 p1; - Gfx::VertexTex2 p2; - Gfx::VertexTex2 p3; - Gfx::Material material; - char texName[20]; - float min; - float max; - long state; - short texNum2; - short reserved2; - short reserved3; - short reserved4; +//! How big the triangle vector is by default +const int TRIANGLE_PREALLOCATE_COUNT = 2000; - NewModelTriangle() - { - memset(this, 0, sizeof(*this)); - } -}; -Gfx::Vertex ReadBinaryVertex(std::istream &stream) +bool ReadBinaryVertex(std::istream& stream, Gfx::Vertex& vertex) { - Gfx::Vertex result; - - result.coord.x = IOUtils::ReadBinaryFloat(stream); - result.coord.y = IOUtils::ReadBinaryFloat(stream); - result.coord.z = IOUtils::ReadBinaryFloat(stream); - result.normal.x = IOUtils::ReadBinaryFloat(stream); - result.normal.y = IOUtils::ReadBinaryFloat(stream); - result.normal.z = IOUtils::ReadBinaryFloat(stream); - result.texCoord.x = IOUtils::ReadBinaryFloat(stream); - result.texCoord.y = IOUtils::ReadBinaryFloat(stream); - - return result; + vertex.coord.x = IOUtils::ReadBinaryFloat(stream); + vertex.coord.y = IOUtils::ReadBinaryFloat(stream); + vertex.coord.z = IOUtils::ReadBinaryFloat(stream); + vertex.normal.x = IOUtils::ReadBinaryFloat(stream); + vertex.normal.y = IOUtils::ReadBinaryFloat(stream); + vertex.normal.z = IOUtils::ReadBinaryFloat(stream); + vertex.texCoord.x = IOUtils::ReadBinaryFloat(stream); + vertex.texCoord.y = IOUtils::ReadBinaryFloat(stream); + + return !stream.fail(); } -void WriteBinaryVertex(Gfx::Vertex vertex, std::ostream &stream) +bool WriteBinaryVertex(Gfx::Vertex vertex, std::ostream& stream) { IOUtils::WriteBinaryFloat(vertex.coord.x, stream); IOUtils::WriteBinaryFloat(vertex.coord.y, stream); @@ -146,27 +68,27 @@ void WriteBinaryVertex(Gfx::Vertex vertex, std::ostream &stream) IOUtils::WriteBinaryFloat(vertex.normal.z, stream); IOUtils::WriteBinaryFloat(vertex.texCoord.x, stream); IOUtils::WriteBinaryFloat(vertex.texCoord.y, stream); + + return !stream.fail(); } -Gfx::VertexTex2 ReadBinaryVertexTex2(std::istream &stream) +bool ReadBinaryVertexTex2(std::istream& stream, Gfx::VertexTex2& vertex) { - Gfx::VertexTex2 result; - - result.coord.x = IOUtils::ReadBinaryFloat(stream); - result.coord.y = IOUtils::ReadBinaryFloat(stream); - result.coord.z = IOUtils::ReadBinaryFloat(stream); - result.normal.x = IOUtils::ReadBinaryFloat(stream); - result.normal.y = IOUtils::ReadBinaryFloat(stream); - result.normal.z = IOUtils::ReadBinaryFloat(stream); - result.texCoord.x = IOUtils::ReadBinaryFloat(stream); - result.texCoord.y = IOUtils::ReadBinaryFloat(stream); - result.texCoord2.x = IOUtils::ReadBinaryFloat(stream); - result.texCoord2.y = IOUtils::ReadBinaryFloat(stream); - - return result; + vertex.coord.x = IOUtils::ReadBinaryFloat(stream); + vertex.coord.y = IOUtils::ReadBinaryFloat(stream); + vertex.coord.z = IOUtils::ReadBinaryFloat(stream); + vertex.normal.x = IOUtils::ReadBinaryFloat(stream); + vertex.normal.y = IOUtils::ReadBinaryFloat(stream); + vertex.normal.z = IOUtils::ReadBinaryFloat(stream); + vertex.texCoord.x = IOUtils::ReadBinaryFloat(stream); + vertex.texCoord.y = IOUtils::ReadBinaryFloat(stream); + vertex.texCoord2.x = IOUtils::ReadBinaryFloat(stream); + vertex.texCoord2.y = IOUtils::ReadBinaryFloat(stream); + + return !stream.fail(); } -void WriteBinaryVertexTex2(Gfx::VertexTex2 vertex, std::ostream &stream) +bool WriteBinaryVertexTex2(Gfx::VertexTex2 vertex, std::ostream& stream) { IOUtils::WriteBinaryFloat(vertex.coord.x, stream); IOUtils::WriteBinaryFloat(vertex.coord.y, stream); @@ -178,38 +100,83 @@ void WriteBinaryVertexTex2(Gfx::VertexTex2 vertex, std::ostream &stream) IOUtils::WriteBinaryFloat(vertex.texCoord.y, stream); IOUtils::WriteBinaryFloat(vertex.texCoord2.x, stream); IOUtils::WriteBinaryFloat(vertex.texCoord2.y, stream); + + return !stream.fail(); } -Gfx::Material ReadBinaryMaterial(std::istream &stream) +bool ReadTextVertexTex2(const std::string& text, Gfx::VertexTex2& vertex) { - Gfx::Material result; + std::stringstream stream; + stream.str(text); - result.diffuse.r = IOUtils::ReadBinaryFloat(stream); - result.diffuse.g = IOUtils::ReadBinaryFloat(stream); - result.diffuse.b = IOUtils::ReadBinaryFloat(stream); - result.diffuse.a = IOUtils::ReadBinaryFloat(stream); + std::string what; - result.ambient.r = IOUtils::ReadBinaryFloat(stream); - result.ambient.g = IOUtils::ReadBinaryFloat(stream); - result.ambient.b = IOUtils::ReadBinaryFloat(stream); - result.ambient.a = IOUtils::ReadBinaryFloat(stream); + stream >> what; + if (what != "c") + return false; - result.specular.r = IOUtils::ReadBinaryFloat(stream); - result.specular.g = IOUtils::ReadBinaryFloat(stream); - result.specular.b = IOUtils::ReadBinaryFloat(stream); - result.specular.a = IOUtils::ReadBinaryFloat(stream); + stream >> vertex.coord.x >> vertex.coord.y >> vertex.coord.z; - /* emissive.r = */ IOUtils::ReadBinaryFloat(stream); - /* emissive.g = */ IOUtils::ReadBinaryFloat(stream); - /* emissive.b = */ IOUtils::ReadBinaryFloat(stream); - /* emissive.a = */ IOUtils::ReadBinaryFloat(stream); + stream >> what; + if (what != "n") + return false; + + stream >> vertex.normal.x >> vertex.normal.y >> vertex.normal.z; + + stream >> what; + if (what != "t1") + return false; - /* power = */ IOUtils::ReadBinaryFloat(stream); + stream >> vertex.texCoord.x >> vertex.texCoord.y; - return result; + stream >> what; + if (what != "t2") + return false; + + stream >> vertex.texCoord2.x >> vertex.texCoord2.y; + + return !stream.fail(); +} + +bool WriteTextVertexTex2(const Gfx::VertexTex2& vertex, std::ostream& stream) +{ + stream << "c " << vertex.coord.x << " " << vertex.coord.y << " " << vertex.coord.z; + stream << " n " << vertex.normal.x << " " << vertex.normal.y << " " << vertex.normal.z; + stream << " t1 " << vertex.texCoord.x << " " << vertex.texCoord.y; + stream << " t2 " << vertex.texCoord2.x << " " << vertex.texCoord2.y; + stream << std::endl; + + return !stream.fail(); +} + +bool ReadBinaryMaterial(std::istream& stream, Gfx::Material& material) +{ + material.diffuse.r = IOUtils::ReadBinaryFloat(stream); + material.diffuse.g = IOUtils::ReadBinaryFloat(stream); + material.diffuse.b = IOUtils::ReadBinaryFloat(stream); + material.diffuse.a = IOUtils::ReadBinaryFloat(stream); + + material.ambient.r = IOUtils::ReadBinaryFloat(stream); + material.ambient.g = IOUtils::ReadBinaryFloat(stream); + material.ambient.b = IOUtils::ReadBinaryFloat(stream); + material.ambient.a = IOUtils::ReadBinaryFloat(stream); + + material.specular.r = IOUtils::ReadBinaryFloat(stream); + material.specular.g = IOUtils::ReadBinaryFloat(stream); + material.specular.b = IOUtils::ReadBinaryFloat(stream); + material.specular.a = IOUtils::ReadBinaryFloat(stream); + + /* emissive.r = */ IOUtils::ReadBinaryFloat(stream); + /* emissive.g = */ IOUtils::ReadBinaryFloat(stream); + /* emissive.b = */ IOUtils::ReadBinaryFloat(stream); + /* emissive.a = */ IOUtils::ReadBinaryFloat(stream); + + /* power = */ IOUtils::ReadBinaryFloat(stream); + + return !stream.fail(); } -void WriteBinaryMaterial(Gfx::Material material, std::ostream &stream) +bool WriteBinaryMaterial(const Gfx::Material& material, std::ostream& stream) { IOUtils::WriteBinaryFloat(material.diffuse.r, stream); IOUtils::WriteBinaryFloat(material.diffuse.g, stream); @@ -232,13 +199,120 @@ void WriteBinaryMaterial(Gfx::Material material, std::ostream &stream) /* emissive.a */ IOUtils::WriteBinaryFloat(0.0f, stream); /* power */ IOUtils::WriteBinaryFloat(0.0f, stream); + + return !stream.fail(); +} + +bool ReadTextMaterial(const std::string& text, Gfx::Material& material) +{ + std::stringstream stream; + stream.str(text); + + std::string what; + + stream >> what; + if (what != "dif") + return false; + + stream >> material.diffuse.r + >> material.diffuse.g + >> material.diffuse.b + >> material.diffuse.a; + + stream >> what; + if (what != "amb") + return false; + + stream >> material.ambient.r + >> material.ambient.g + >> material.ambient.b + >> material.ambient.a; + + stream >> what; + if (what != "spc") + return false; + + stream >> material.specular.r + >> material.specular.g + >> material.specular.b + >> material.specular.a; + + return !stream.fail(); +} + +bool WriteTextMaterial(const Gfx::Material& material, std::ostream& stream) +{ + stream << "dif " << material.diffuse.r + << " " << material.diffuse.g + << " " << material.diffuse.b + << " " << material.diffuse.a; + + stream << " amb " << material.ambient.r + << " " << material.ambient.g + << " " << material.ambient.b + << " " << material.ambient.a; + + stream << " spc " << material.specular.r + << " " << material.specular.g << " " + << material.specular.b << " " + << material.specular.a; + + stream << std::endl; + + return !stream.fail(); } -Gfx::ModelTriangle::ModelTriangle() +template<typename T> +bool ReadLineValue(std::istream& stream, const std::string& prefix, T& value) { - min = 0.0f; - max = 0.0f; - state = 0L; + std::string line; + while (true) + { + if (stream.eof() || stream.fail()) + return false; + + std::getline(stream, line); + if (!line.empty() && line[0] != '#') + break; + } + + std::stringstream s; + s.str(line); + + std::string what; + s >> what; + if (what != prefix) + return false; + + s >> value; + + return true; +} + +bool ReadLineString(std::istream& stream, const std::string& prefix, std::string& value) +{ + std::string line; + while (true) + { + if (stream.eof() || stream.fail()) + return false; + + std::getline(stream, line); + if (!line.empty() && line[0] != '#') + break; + } + + std::stringstream s; + s.str(line); + + std::string what; + s >> what; + if (what != prefix) + return false; + + std::getline(s, value); + + return true; } @@ -246,7 +320,9 @@ Gfx::CModelFile::CModelFile(CInstanceManager* iMan) { m_iMan = iMan; +#ifndef MODELFILE_NO_ENGINE m_engine = static_cast<CEngine*>(m_iMan->SearchInstance(CLASS_ENGINE)); +#endif m_triangles.reserve(TRIANGLE_PREALLOCATE_COUNT); } @@ -255,71 +331,171 @@ Gfx::CModelFile::~CModelFile() { } -std::string Gfx::CModelFile::GetError() + +/******************************************************* + Deprecated formats + *******************************************************/ + +/** + * \struct OldModelHeader + * \brief Colobot binary model header info + * + * @deprecated + */ +struct OldModelHeader { - return m_error; -} + //! Revision number + int revision; + //! Version number + int version; + //! Total number of triangles + int totalTriangles; + //! Reserved area + int reserved[10]; + + OldModelHeader() + { + memset(this, 0, sizeof(*this)); + } +}; + + +/** + * \struct OldModelTriangle1 + * \brief Colobot binary model file version 1 + * + * @deprecated + */ +struct OldModelTriangle1 +{ + char used; + char selected; + Gfx::Vertex p1; + Gfx::Vertex p2; + Gfx::Vertex p3; + Gfx::Material material; + char texName[20]; + float min; + float max; + OldModelTriangle1() + { + memset(this, 0, sizeof(*this)); + } +}; + +/** + * \struct OldModelTriangle2 + * \brief Colobot binary model file version 2 + * + * @deprecated + */ +struct OldModelTriangle2 +{ + char used; + char selected; + Gfx::Vertex p1; + Gfx::Vertex p2; + Gfx::Vertex p3; + Gfx::Material material; + char texName[20]; + float min; + float max; + long state; + short reserved1; + short reserved2; + short reserved3; + short reserved4; + OldModelTriangle2() + { + memset(this, 0, sizeof(*this)); + } +}; + +/** + * \struct OldModelTriangle3 + * \brief Colobot binary model file version 3 + * + * @deprecated + */ +struct OldModelTriangle3 +{ + char used; + char selected; + Gfx::VertexTex2 p1; + Gfx::VertexTex2 p2; + Gfx::VertexTex2 p3; + Gfx::Material material; + char texName[20]; + float min; + float max; + long state; + short texNum2; + short reserved2; + short reserved3; + short reserved4; + + OldModelTriangle3() + { + memset(this, 0, sizeof(*this)); + } +}; -bool Gfx::CModelFile::ReadModel(const std::string &filename, bool edit, bool meta) +bool Gfx::CModelFile::ReadModel(const std::string& fileName) { m_triangles.clear(); - m_error = ""; std::ifstream stream; - stream.open(filename.c_str(), std::ios_base::in | std::ios_base::binary); - if (! stream.good()) + stream.open(fileName.c_str(), std::ios_base::in | std::ios_base::binary); + if (!stream.good()) { - m_error = std::string("Could not open file '") + filename + std::string("'"); + GetLogger()->Error("Could not open file '%s'\n", fileName.c_str()); return false; } - return ReadModel(stream, edit, meta); + return ReadModel(stream); } -bool Gfx::CModelFile::ReadModel(std::istream &stream, bool edit, bool meta) +bool Gfx::CModelFile::ReadModel(std::istream& stream) { m_triangles.clear(); - m_error = ""; - // FIXME: for now, reading models only from files, not metafile - - ModelHeader header; + OldModelHeader header; header.revision = IOUtils::ReadBinary<4, int>(stream); header.version = IOUtils::ReadBinary<4, int>(stream); - header.totalVertices = IOUtils::ReadBinary<4, int>(stream); + header.totalTriangles = IOUtils::ReadBinary<4, int>(stream); for (int i = 0; i < 10; ++i) header.reserved[i] = IOUtils::ReadBinary<4, int>(stream); - if (! stream.good()) + if (!stream.good()) { - m_error = "Error reading model file header"; + GetLogger()->Error("Error reading model file header\n"); return false; } // Old model version #1 if ( (header.revision == 1) && (header.version == 0) ) { - for (int i = 0; i < header.totalVertices; ++i) + for (int i = 0; i < header.totalTriangles; ++i) { OldModelTriangle1 t; t.used = IOUtils::ReadBinary<1, char>(stream); t.selected = IOUtils::ReadBinary<1, char>(stream); - t.p1 = ReadBinaryVertex(stream); - t.p2 = ReadBinaryVertex(stream); - t.p3 = ReadBinaryVertex(stream); + ReadBinaryVertex(stream, t.p1); + ReadBinaryVertex(stream, t.p2); + ReadBinaryVertex(stream, t.p3); - t.material = ReadBinaryMaterial(stream); + ReadBinaryMaterial(stream, t.material); stream.read(t.texName, 20); t.min = IOUtils::ReadBinaryFloat(stream); t.max = IOUtils::ReadBinaryFloat(stream); - if (! stream.good()) + if (stream.fail()) { - m_error = "Error reading model data"; + GetLogger()->Error("Error reading model data\n"); return false; } @@ -338,17 +514,17 @@ bool Gfx::CModelFile::ReadModel(std::istream &stream, bool edit, bool meta) } else if ( header.revision == 1 && header.version == 1 ) { - for (int i = 0; i < header.totalVertices; ++i) + for (int i = 0; i < header.totalTriangles; ++i) { OldModelTriangle2 t; t.used = IOUtils::ReadBinary<1, char>(stream); t.selected = IOUtils::ReadBinary<1, char>(stream); - t.p1 = ReadBinaryVertex(stream); - t.p2 = ReadBinaryVertex(stream); - t.p3 = ReadBinaryVertex(stream); + ReadBinaryVertex(stream, t.p1); + ReadBinaryVertex(stream, t.p2); + ReadBinaryVertex(stream, t.p3); - t.material = ReadBinaryMaterial(stream); + ReadBinaryMaterial(stream, t.material); stream.read(t.texName, 20); t.min = IOUtils::ReadBinaryFloat(stream); t.max = IOUtils::ReadBinaryFloat(stream); @@ -359,9 +535,9 @@ bool Gfx::CModelFile::ReadModel(std::istream &stream, bool edit, bool meta) t.reserved3 = IOUtils::ReadBinary<2, short>(stream); t.reserved4 = IOUtils::ReadBinary<2, short>(stream); - if (! stream.good()) + if (stream.fail()) { - m_error = "Error reading model data"; + GetLogger()->Error("Error reading model data\n"); return false; } @@ -381,19 +557,19 @@ bool Gfx::CModelFile::ReadModel(std::istream &stream, bool edit, bool meta) } else { - for (int i = 0; i < header.totalVertices; ++i) + for (int i = 0; i < header.totalTriangles; ++i) { - NewModelTriangle t; + OldModelTriangle3 t; t.used = IOUtils::ReadBinary<1, char>(stream); t.selected = IOUtils::ReadBinary<1, char>(stream); /* padding */ IOUtils::ReadBinary<2, unsigned int>(stream); - t.p1 = ReadBinaryVertexTex2(stream); - t.p2 = ReadBinaryVertexTex2(stream); - t.p3 = ReadBinaryVertexTex2(stream); + ReadBinaryVertexTex2(stream, t.p1); + ReadBinaryVertexTex2(stream, t.p2); + ReadBinaryVertexTex2(stream, t.p3); - t.material = ReadBinaryMaterial(stream); + ReadBinaryMaterial(stream, t.material); stream.read(t.texName, 20); t.min = IOUtils::ReadBinaryFloat(stream); t.max = IOUtils::ReadBinaryFloat(stream); @@ -404,9 +580,9 @@ bool Gfx::CModelFile::ReadModel(std::istream &stream, bool edit, bool meta) t.reserved3 = IOUtils::ReadBinary<2, short>(stream); t.reserved4 = IOUtils::ReadBinary<2, short>(stream); - if (! stream.good()) + if (stream.fail()) { - m_error = "Error reading model data"; + GetLogger()->Error("Error reading model data\n"); return false; } @@ -417,15 +593,26 @@ bool Gfx::CModelFile::ReadModel(std::istream &stream, bool edit, bool meta) triangle.material = t.material; triangle.tex1Name = std::string(t.texName); - char tex2Name[20] = { 0 }; triangle.min = t.min; triangle.max = t.max; triangle.state = t.state; + triangle.variableTex2 = t.texNum2 == 1; + + if (triangle.tex1Name == "plant.png") + triangle.state |= Gfx::ENG_RSTATE_ALPHA; + + if (!triangle.variableTex2 && t.texNum2 != 0) + { + if (t.texNum2 >= 1 && t.texNum2 <= 10) + triangle.state |= Gfx::ENG_RSTATE_DUAL_BLACK; - if (t.texNum2 != 0) - sprintf(tex2Name, "dirty%.2d.tga", t.texNum2); // hardcoded as in the original code + if (t.texNum2 >= 11 && t.texNum2 <= 20) + triangle.state |= Gfx::ENG_RSTATE_DUAL_WHITE; - triangle.tex2Name = std::string(tex2Name); + char tex2Name[20] = { 0 }; + sprintf(tex2Name, "dirty%.2d.png", t.texNum2); // hardcoded as in original code + triangle.tex2Name = tex2Name; + } m_triangles.push_back(triangle); } @@ -433,97 +620,70 @@ bool Gfx::CModelFile::ReadModel(std::istream &stream, bool edit, bool meta) for (int i = 0; i < static_cast<int>( m_triangles.size() ); ++i) { - m_triangles[i].tex1Name = StrUtils::Replace(m_triangles[i].tex1Name, "bmp", "tga"); + // All extensions are now png + m_triangles[i].tex1Name = StrUtils::Replace(m_triangles[i].tex1Name, "bmp", "png"); + m_triangles[i].tex1Name = StrUtils::Replace(m_triangles[i].tex1Name, "tga", "png"); + + m_triangles[i].tex2Name = StrUtils::Replace(m_triangles[i].tex2Name, "bmp", "png"); + m_triangles[i].tex2Name = StrUtils::Replace(m_triangles[i].tex2Name, "tga", "png"); - GetLogger()->Info("ModelTriangle %d\n", i+1); + GetLogger()->Trace("ModelTriangle %d\n", i+1); std::string s1 = m_triangles[i].p1.ToString(); - GetLogger()->Info(" p1: %s\n", s1.c_str()); + GetLogger()->Trace(" p1: %s\n", s1.c_str()); std::string s2 = m_triangles[i].p2.ToString(); - GetLogger()->Info(" p2: %s\n", s2.c_str()); + GetLogger()->Trace(" p2: %s\n", s2.c_str()); std::string s3 = m_triangles[i].p3.ToString(); - GetLogger()->Info(" p3: %s\n", s3.c_str()); + GetLogger()->Trace(" p3: %s\n", s3.c_str()); std::string d = m_triangles[i].material.diffuse.ToString(); std::string a = m_triangles[i].material.ambient.ToString(); std::string s = m_triangles[i].material.specular.ToString(); - GetLogger()->Info(" mat: d: %s a: %s s: %s\n", d.c_str(), a.c_str(), s.c_str()); + GetLogger()->Trace(" mat: d: %s a: %s s: %s\n", d.c_str(), a.c_str(), s.c_str()); - GetLogger()->Info(" tex1: %s tex2: %s\n", m_triangles[i].tex1Name.c_str(), m_triangles[i].tex2Name.c_str()); - GetLogger()->Info(" min: %.2f max: %.2f\n", m_triangles[i].min, m_triangles[i].max); - GetLogger()->Info(" state: %ld\n", m_triangles[i].state); + GetLogger()->Trace(" tex1: %s tex2: %s\n", m_triangles[i].tex1Name.c_str(), + m_triangles[i].variableTex2 ? "(variable)" : m_triangles[i].tex2Name.c_str()); + GetLogger()->Trace(" min: %.2f max: %.2f\n", m_triangles[i].min, m_triangles[i].max); + GetLogger()->Trace(" state: %ld\n", m_triangles[i].state); } - /* - if (! edit) - { - float limit[2]; - limit[0] = m_engine->RetLimitLOD(0); // frontier AB as config - limit[1] = m_engine->RetLimitLOD(1); // frontier BC as config - - // Standard frontiers -> config. - for (int i = 0; i < m_triangles.size(); ++i) - { - if ( m_triangles[i].min == 0.0f && - m_triangles[i].max == 100.0f ) // resolution A ? - { - m_triangles[i].max = limit[0]; - } - else if ( m_triangles[i].min == 100.0f && - m_triangles[i].max == 200.0f ) // resolution B ? - { - m_triangles[i].min = limit[0]; - m_triangles[i].max = limit[1]; - } - else if ( m_triangles[i].min == 200.0f && - m_triangles[i].max == 1000000.0f ) // resolution C ? - { - m_triangles[i].min = limit[1]; - } - } - }*/ - return true; } -bool Gfx::CModelFile::WriteModel(const std::string &filename) +bool Gfx::CModelFile::WriteModel(const std::string& fileName) { - m_error = ""; - std::ofstream stream; - stream.open(filename.c_str(), std::ios_base::out | std::ios_base::binary); - if (! stream.good()) + stream.open(fileName.c_str(), std::ios_base::out | std::ios_base::binary); + if (!stream.good()) { - m_error = std::string("Could not open file '") + filename + std::string("'"); + GetLogger()->Error("Could not open file '%s'\n", fileName.c_str()); return false; } return WriteModel(stream); } -bool Gfx::CModelFile::WriteModel(std::ostream &stream) +bool Gfx::CModelFile::WriteModel(std::ostream& stream) { - m_error = ""; - if (m_triangles.size() == 0) { - m_error = "Empty model"; + GetLogger()->Error("Empty model\n"); return false; } - ModelHeader header; + OldModelHeader header; header.revision = 1; header.version = 2; - header.totalVertices = m_triangles.size(); + header.totalTriangles = m_triangles.size(); IOUtils::WriteBinary<4, int>(header.revision, stream); IOUtils::WriteBinary<4, int>(header.version, stream); - IOUtils::WriteBinary<4, int>(header.totalVertices, stream); + IOUtils::WriteBinary<4, int>(header.totalTriangles, stream); for (int i = 0; i < 10; ++i) IOUtils::WriteBinary<4, int>(header.reserved[i], stream); for (int i = 0; i < static_cast<int>( m_triangles.size() ); ++i) { - NewModelTriangle t; + OldModelTriangle3 t; t.used = true; @@ -536,14 +696,21 @@ bool Gfx::CModelFile::WriteModel(std::ostream &stream) t.min = m_triangles[i].min; t.max = m_triangles[i].max; t.state = m_triangles[i].state; + int no = 0; - sscanf(m_triangles[i].tex2Name.c_str(), "dirty%d.tga", &no); // hardcoded as in the original code + if (m_triangles[i].variableTex2) + no = 1; + else + sscanf(m_triangles[i].tex2Name.c_str(), "dirty%d.png", &no); // hardcoded as in the original code + t.texNum2 = no; IOUtils::WriteBinary<1, char>(t.used, stream); IOUtils::WriteBinary<1, char>(t.selected, stream); + /* padding */ IOUtils::WriteBinary<2, unsigned int>(0, stream); + WriteBinaryVertexTex2(t.p1, stream); WriteBinaryVertexTex2(t.p2, stream); WriteBinaryVertexTex2(t.p3, stream); @@ -563,215 +730,489 @@ bool Gfx::CModelFile::WriteModel(std::ostream &stream) return true; } -bool Gfx::CModelFile::ReadDXF(const std::string &filename, float min, float max) + +/******************************************************* + New formats + *******************************************************/ + +/** + * \struct NewModelHeader + * \brief Header for new binary model file + */ +struct NewModelHeader { - m_triangles.clear(); - m_error = ""; + //! File version (1, 2, ...) + int version; + //! Total number of triangles + int totalTriangles; + NewModelHeader() + { + version = 0; + totalTriangles = 0; + } +}; + +/** + * \struct NewModelTriangle1 + * \brief Triangle of new binary model file + * + * NOTE: at this time, it is identical to ModelTriangle struct, but it may change + * independently in the future. + */ +struct NewModelTriangle1 +{ + //! 1st vertex + Gfx::VertexTex2 p1; + //! 2nd vertex + Gfx::VertexTex2 p2; + //! 3rd vertex + Gfx::VertexTex2 p3; + //! Material + Gfx::Material material; + //! Name of 1st texture + std::string tex1Name; + //! Name of 2nd texture + std::string tex2Name; + //! If true, 2nd texture will be taken from current engine setting + bool variableTex2; + //! Min LOD threshold + float min; + //! Max LOD threshold + float max; + //! Rendering state to be set + int state; + + NewModelTriangle1() + { + variableTex2 = true; + min = max = 0.0f; + state = 0; + } +}; + + +bool Gfx::CModelFile::ReadTextModel(const std::string& fileName) +{ std::ifstream stream; - stream.open(filename.c_str(), std::ios_base::in); - if (! stream.good()) + stream.open(fileName.c_str(), std::ios_base::in); + if (!stream.good()) { - m_error = std::string("Couldn't open file '") + filename + std::string("'"); + GetLogger()->Error("Could not open file '%s'\n", fileName.c_str()); return false; } - return ReadDXF(stream, min, max); + return ReadTextModel(stream); } -bool Gfx::CModelFile::ReadDXF(std::istream &stream, float min, float max) +bool Gfx::CModelFile::ReadTextModel(std::istream& stream) { m_triangles.clear(); - m_error = ""; - if (! stream.good()) + NewModelHeader header; + + bool headOk = ReadLineValue<int>(stream, "version", header.version) && + ReadLineValue<int>(stream, "total_triangles", header.totalTriangles); + + if (!headOk || !stream.good()) { - m_error = "Invalid stream"; + GetLogger()->Error("Error reading model file header\n"); return false; } - // Input state - bool waitNumVertex = false; - bool waitNumFace = false; - bool waitVertexX = false; - bool waitVertexY = false; - bool waitVertexZ = false; - bool waitFaceX = false; - bool waitFaceY = false; - bool waitFaceZ = false; - - // Vertex array - std::vector<Math::Vector> vertices; - vertices.reserve(TRIANGLE_PREALLOCATE_COUNT); - - // Number of vertices & faces of the primitive to be read - int vertexNum = 0, faceNum = 0; - // Vertex coords - Math::Vector coords; - // Indexes of face (triangle) points - int p1 = 0, p2 = 0, p3 = 0; - - // Input line - std::string line; - while (! stream.eof() ) + // New model version 1 + if (header.version == 1) { - // Read line with command - std::getline(stream, line); - int command = StrUtils::FromString<int>(line); + for (int i = 0; i < header.totalTriangles; ++i) + { + NewModelTriangle1 t; + + std::string p1Text, p2Text, p3Text; + std::string matText; + char varTex2Ch = 0; + + bool triOk = ReadLineString(stream, "p1", p1Text) && + ReadTextVertexTex2(p1Text, t.p1) && + ReadLineString(stream, "p2", p2Text) && + ReadTextVertexTex2(p2Text, t.p2) && + ReadLineString(stream, "p3", p3Text) && + ReadTextVertexTex2(p3Text, t.p3) && + ReadLineString(stream, "mat", matText) && + ReadTextMaterial(matText, t.material) && + ReadLineValue<std::string>(stream, "tex1", t.tex1Name) && + ReadLineValue<std::string>(stream, "tex2", t.tex2Name) && + ReadLineValue<char>(stream, "var_tex2", varTex2Ch) && + ReadLineValue<float>(stream, "min", t.min) && + ReadLineValue<float>(stream, "max", t.max) && + ReadLineValue<int>(stream, "state", t.state); + + if (!triOk || stream.fail()) + { + GetLogger()->Error("Error reading model file header\n"); + return false; + } - // Read line with param - std::getline(stream, line); + t.variableTex2 = varTex2Ch == 'Y'; - bool ok = true; + Gfx::ModelTriangle triangle; + triangle.p1 = t.p1; + triangle.p2 = t.p2; + triangle.p3 = t.p3; + triangle.material = t.material; + triangle.tex1Name = t.tex1Name; + triangle.tex2Name = t.tex2Name; + triangle.variableTex2 = t.variableTex2; + triangle.min = t.min; + triangle.max = t.max; + triangle.state = t.state; - if (command == 66) - { - waitNumVertex = true; - } + m_triangles.push_back(triangle); - if ( command == 71 && waitNumVertex ) - { - waitNumVertex = false; - vertexNum = StrUtils::FromString<int>(line, &ok); - waitNumFace = true; + continue; } + } + else + { + GetLogger()->Error("Unknown model file version\n"); + return false; + } - if ( command == 72 && waitNumFace ) - { - waitNumFace = false; - faceNum = StrUtils::FromString<int>(line, &ok); - waitVertexX = true; - } + for (int i = 0; i < static_cast<int>( m_triangles.size() ); ++i) + { + GetLogger()->Trace("ModelTriangle %d\n", i+1); + std::string s1 = m_triangles[i].p1.ToString(); + GetLogger()->Trace(" p1: %s\n", s1.c_str()); + std::string s2 = m_triangles[i].p2.ToString(); + GetLogger()->Trace(" p2: %s\n", s2.c_str()); + std::string s3 = m_triangles[i].p3.ToString(); + GetLogger()->Trace(" p3: %s\n", s3.c_str()); - if ( command == 10 && waitVertexX ) - { - waitVertexX = false; - coords.x = StrUtils::FromString<float>(line, &ok); - waitVertexY = true; - } + std::string d = m_triangles[i].material.diffuse.ToString(); + std::string a = m_triangles[i].material.ambient.ToString(); + std::string s = m_triangles[i].material.specular.ToString(); + GetLogger()->Trace(" mat: d: %s a: %s s: %s\n", d.c_str(), a.c_str(), s.c_str()); + + GetLogger()->Trace(" tex1: %s tex2: %s\n", m_triangles[i].tex1Name.c_str(), m_triangles[i].tex2Name.c_str()); + GetLogger()->Trace(" min: %.2f max: %.2f\n", m_triangles[i].min, m_triangles[i].max); + GetLogger()->Trace(" state: %ld\n", m_triangles[i].state); + } + + return true; +} + +bool Gfx::CModelFile::WriteTextModel(const std::string &fileName) +{ + std::ofstream stream; + stream.open(fileName.c_str(), std::ios_base::out); + if (!stream.good()) + { + GetLogger()->Error("Could not open file '%s'\n", fileName.c_str()); + return false; + } - if ( command == 20 && waitVertexY ) + return WriteTextModel(stream); +} + +bool Gfx::CModelFile::WriteTextModel(std::ostream& stream) +{ + if (m_triangles.size() == 0) + { + GetLogger()->Error("Empty model\n"); + return false; + } + + NewModelHeader header; + + header.version = 1; + header.totalTriangles = m_triangles.size(); + + stream << "# Colobot text model" << std::endl; + stream << std::endl; + stream << "### HEAD" << std::endl; + stream << "version " << header.version << std::endl; + stream << "total_triangles " << header.totalTriangles << std::endl; + stream << std::endl; + stream << "### TRIANGLES" << std::endl; + + for (int i = 0; i < static_cast<int>( m_triangles.size() ); ++i) + { + NewModelTriangle1 t; + + t.p1 = m_triangles[i].p1; + t.p2 = m_triangles[i].p2; + t.p3 = m_triangles[i].p3; + t.material = m_triangles[i].material; + t.tex1Name = m_triangles[i].tex1Name; + t.tex2Name = m_triangles[i].tex2Name; + t.variableTex2 = m_triangles[i].variableTex2; + t.min = m_triangles[i].min; + t.max = m_triangles[i].max; + t.state = m_triangles[i].state; + + stream << "p1 "; + WriteTextVertexTex2(t.p1, stream); + stream << "p2 "; + WriteTextVertexTex2(t.p2, stream); + stream << "p3 "; + WriteTextVertexTex2(t.p3, stream); + stream << "mat "; + WriteTextMaterial(t.material, stream); + + stream << "tex1 " << t.tex1Name << std::endl; + stream << "tex2 " << t.tex2Name << std::endl; + stream << "var_tex2 " << (t.variableTex2 ? 'Y' : 'N') << std::endl; + stream << "min " << t.min << std::endl; + stream << "max " << t.max << std::endl; + stream << "state " << t.state << std::endl; + + stream << std::endl; + + if (stream.fail()) { - waitVertexY = false; - coords.y = StrUtils::FromString<float>(line, &ok); - waitVertexZ = true; + GetLogger()->Error("Error writing model file\n"); + return false; } + } - if ( command == 30 && waitVertexZ ) + return true; +} + +bool Gfx::CModelFile::ReadBinaryModel(const std::string& fileName) +{ + std::ifstream stream; + stream.open(fileName.c_str(), std::ios_base::in | std::ios_base::binary); + if (!stream.good()) + { + GetLogger()->Error("Could not open file '%s'\n", fileName.c_str()); + return false; + } + + return ReadBinaryModel(stream); +} + +bool Gfx::CModelFile::ReadBinaryModel(std::istream& stream) +{ + m_triangles.clear(); + + NewModelHeader header; + + header.version = IOUtils::ReadBinary<4, int>(stream); + header.totalTriangles = IOUtils::ReadBinary<4, int>(stream); + + if (!stream.good()) + { + GetLogger()->Error("Error reading model file header\n"); + return false; + } + + // New model version 1 + if (header.version == 1) + { + for (int i = 0; i < header.totalTriangles; ++i) { - waitVertexZ = false; - coords.z = StrUtils::FromString<float>(line, &ok); + NewModelTriangle1 t; + + ReadBinaryVertexTex2(stream, t.p1); + ReadBinaryVertexTex2(stream, t.p2); + ReadBinaryVertexTex2(stream, t.p3); + ReadBinaryMaterial(stream, t.material); + t.tex1Name = IOUtils::ReadBinaryString<1>(stream); + t.tex2Name = IOUtils::ReadBinaryString<1>(stream); + t.variableTex2 = IOUtils::ReadBinaryBool(stream); + t.min = IOUtils::ReadBinaryFloat(stream); + t.max = IOUtils::ReadBinaryFloat(stream); + t.state = IOUtils::ReadBinary<4, unsigned int>(stream); - vertexNum --; - if ( vertexNum >= 0 ) - { - Math::Vector p(coords.x, coords.z, coords.y); // permutation of Y and Z! - vertices.push_back(p); - waitVertexX = true; - } - else + if (stream.fail()) { - waitFaceX = true; + GetLogger()->Error("Error reading model data\n"); + return false; } - } - if ( command == 71 && waitFaceX ) - { - waitFaceX = false; - p1 = StrUtils::FromString<int>(line, &ok); - if ( p1 < 0 ) p1 = -p1; - waitFaceY = true; - } + Gfx::ModelTriangle triangle; + triangle.p1 = t.p1; + triangle.p2 = t.p2; + triangle.p3 = t.p3; + triangle.material = t.material; + triangle.tex1Name = t.tex1Name; + triangle.tex2Name = t.tex2Name; + triangle.variableTex2 = t.variableTex2; + triangle.min = t.min; + triangle.max = t.max; + triangle.state = t.state; - if ( command == 72 && waitFaceY ) - { - waitFaceY = false; - p2 = StrUtils::FromString<int>(line, &ok); - if ( p2 < 0 ) p2 = -p2; - waitFaceZ = true; + m_triangles.push_back(triangle); } + } + else + { + GetLogger()->Error("Unknown model file version\n"); + return false; + } - if ( command == 73 && waitFaceZ ) - { - waitFaceZ = false; - p3 = StrUtils::FromString<int>(line, &ok); - if ( p3 < 0 ) p3 = -p3; + for (int i = 0; i < static_cast<int>( m_triangles.size() ); ++i) + { + GetLogger()->Trace("ModelTriangle %d\n", i+1); + std::string s1 = m_triangles[i].p1.ToString(); + GetLogger()->Trace(" p1: %s\n", s1.c_str()); + std::string s2 = m_triangles[i].p2.ToString(); + GetLogger()->Trace(" p2: %s\n", s2.c_str()); + std::string s3 = m_triangles[i].p3.ToString(); + GetLogger()->Trace(" p3: %s\n", s3.c_str()); - faceNum --; - if ( faceNum >= 0 ) - { - assert( (p1-1 >= 0) && (p1-1 < static_cast<int>(vertices.size())) ); - assert( (p2-1 >= 0) && (p2-1 < static_cast<int>(vertices.size())) ); - assert( (p3-1 >= 0) && (p3-1 < static_cast<int>(vertices.size())) ); + std::string d = m_triangles[i].material.diffuse.ToString(); + std::string a = m_triangles[i].material.ambient.ToString(); + std::string s = m_triangles[i].material.specular.ToString(); + GetLogger()->Trace(" mat: d: %s a: %s s: %s\n", d.c_str(), a.c_str(), s.c_str()); - CreateTriangle(vertices[p3-1], vertices[p2-1], vertices[p1-1], min, max); - waitFaceX = true; - } - } + GetLogger()->Trace(" tex1: %s tex2: %s\n", m_triangles[i].tex1Name.c_str(), m_triangles[i].tex2Name.c_str()); + GetLogger()->Trace(" min: %.2f max: %.2f\n", m_triangles[i].min, m_triangles[i].max); + GetLogger()->Trace(" state: %ld\n", m_triangles[i].state); + } - if (! ok) + return true; +} + +bool Gfx::CModelFile::WriteBinaryModel(const std::string& fileName) +{ + std::ofstream stream; + stream.open(fileName.c_str(), std::ios_base::out | std::ios_base::binary); + if (!stream.good()) + { + GetLogger()->Error("Could not open file '%s'\n", fileName.c_str()); + return false; + } + + return WriteBinaryModel(stream); +} + +bool Gfx::CModelFile::WriteBinaryModel(std::ostream& stream) +{ + if (m_triangles.size() == 0) + { + GetLogger()->Error("Empty model\n"); + return false; + } + + NewModelHeader header; + + header.version = 1; + header.totalTriangles = m_triangles.size(); + + IOUtils::WriteBinary<4, int>(header.version, stream); + IOUtils::WriteBinary<4, int>(header.totalTriangles, stream); + + for (int i = 0; i < static_cast<int>( m_triangles.size() ); ++i) + { + NewModelTriangle1 t; + + t.p1 = m_triangles[i].p1; + t.p2 = m_triangles[i].p2; + t.p3 = m_triangles[i].p3; + t.material = m_triangles[i].material; + t.tex1Name = m_triangles[i].tex1Name; + t.tex2Name = m_triangles[i].tex2Name; + t.variableTex2 = m_triangles[i].variableTex2; + t.min = m_triangles[i].min; + t.max = m_triangles[i].max; + t.state = m_triangles[i].state; + + WriteBinaryVertexTex2(t.p1, stream); + WriteBinaryVertexTex2(t.p2, stream); + WriteBinaryVertexTex2(t.p3, stream); + WriteBinaryMaterial(t.material, stream); + IOUtils::WriteBinaryString<1>(t.tex1Name, stream); + IOUtils::WriteBinaryString<1>(t.tex2Name, stream); + IOUtils::WriteBinaryBool(t.variableTex2, stream); + IOUtils::WriteBinaryFloat(t.min, stream); + IOUtils::WriteBinaryFloat(t.max, stream); + IOUtils::WriteBinary<4, unsigned int>(t.state, stream); + + if (stream.fail()) { - m_error = "Error reading data"; + GetLogger()->Error("Error writing model file\n"); return false; } - } return true; } -bool Gfx::CModelFile::CreateEngineObject(int objRank, int addState) + +/******************************************************* + Other stuff + *******************************************************/ + +#ifndef MODELFILE_NO_ENGINE + +bool Gfx::CModelFile::CreateEngineObject(int objRank) { + std::vector<Gfx::VertexTex2> vs(3, Gfx::VertexTex2()); + + float limit[2]; + limit[0] = m_engine->GetLimitLOD(0); // frontier AB as config + limit[1] = m_engine->GetLimitLOD(1); // frontier BC as config + for (int i = 0; i < static_cast<int>( m_triangles.size() ); i++) { - int state = m_triangles[i].state; + // TODO move this to CEngine - /* TODO ??? - if (texName1 == "plant.png") - state |= Gfx::ENG_RSTATE_ALPHA; + float min = m_triangles[i].min; + float max = m_triangles[i].max; - if (m_triangles[i].tex2Name.empty()) + // Standard frontiers -> config + if (min == 0.0f && max == 100.0f) // resolution A ? + { + max = limit[0]; + } + else if (min == 100.0f && max == 200.0f) // resolution B ? { - int texNum = 0; + min = limit[0]; + max = limit[1]; + } + else if (min == 200.0f && max == 1000000.0f) // resolution C ? + { + min = limit[1]; + } - if ( m_triangles[i].texNum2 == 1 ) - { - texNum = m_engine->RetSecondTexture(); - } - else - { - texNum = m_triangles[i].texNum2; - } + int state = m_triangles[i].state; + std::string tex2Name = m_triangles[i].tex2Name; - if ( texNum >= 1 && texNum <= 10 ) - { - state |= D3DSTATEDUALb; - } - if ( texNum >= 11 && texNum <= 20 ) - { - state |= D3DSTATEDUALw; - } - sprintf(texName2, "dirty%.2d.tga", texNum); // ??? - }*/ + if (m_triangles[i].variableTex2) + { + int texNum = m_engine->GetSecondTexture(); - std::vector<Gfx::VertexTex2> vs; - vs.push_back(m_triangles[i].p1); - vs.push_back(m_triangles[i].p2); - vs.push_back(m_triangles[i].p3); + if (texNum >= 1 && texNum <= 10) + state |= Gfx::ENG_RSTATE_DUAL_BLACK; - m_engine->AddTriangles(objRank, vs, - m_triangles[i].material, - state + addState, - m_triangles[i].tex1Name, - m_triangles[i].tex2Name, - m_triangles[i].min, - m_triangles[i].max, false); + if (texNum >= 11 && texNum <= 20) + state |= Gfx::ENG_RSTATE_DUAL_WHITE; + + char name[20] = { 0 }; + sprintf(name, "dirty%.2d.png", texNum); + tex2Name = name; + } + + vs[0] = m_triangles[i].p1; + vs[1] = m_triangles[i].p2; + vs[2] = m_triangles[i].p3; + + bool ok = m_engine->AddTriangles(objRank, vs, + m_triangles[i].material, + state, + m_triangles[i].tex1Name, + tex2Name, + min, max, false); + if (!ok) + return false; } return true; } +#endif + void Gfx::CModelFile::Mirror() { for (int i = 0; i < static_cast<int>( m_triangles.size() ); i++) @@ -790,7 +1231,7 @@ void Gfx::CModelFile::Mirror() } } -std::vector<Gfx::ModelTriangle>& Gfx::CModelFile::GetTriangles() +const std::vector<Gfx::ModelTriangle>& Gfx::CModelFile::GetTriangles() { return m_triangles; } |