From 211b406c3db62a2ef5d19c94d0df9af3257eaabf Mon Sep 17 00:00:00 2001
From: Anakin <Battlefrontler@t-online.de>
Date: Fri, 21 Oct 2016 18:56:34 +0200
Subject: [PATCH] dynamical decide whether to use the given texture or solid
 red

---
 MshViewer/Header/Object.h             |  1 +
 MshViewer/Source/Object.cpp           | 19 ++++++++++++++++---
 MshViewer/Source/OpenGlController.cpp | 22 ++++++++++++++++------
 3 files changed, 33 insertions(+), 9 deletions(-)

diff --git a/MshViewer/Header/Object.h b/MshViewer/Header/Object.h
index f1a17f7..a81c3e1 100644
--- a/MshViewer/Header/Object.h
+++ b/MshViewer/Header/Object.h
@@ -71,5 +71,6 @@ private:
 public:
 	std::vector<GLfloat> getVertex() const;
 	std::vector<GLfloat> getUV() const;
+	std::list<std::string> getTexture() const;
 
 };
diff --git a/MshViewer/Source/Object.cpp b/MshViewer/Source/Object.cpp
index 1840247..296d416 100644
--- a/MshViewer/Source/Object.cpp
+++ b/MshViewer/Source/Object.cpp
@@ -148,9 +148,10 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list<ChunkHeader*>& c
 		if (!strcmp("PRNT", (*it)->name))
 		{
 			fsMesh.seekg((*it)->position);
-			char tempName[33] = { 0 };
-			fsMesh.read(reinterpret_cast<char*>(&tempName[0]), (*it)->size > 32 ? 32 : (*it)->size);
-			dataDestination->parent = tempName;
+			char* buffer = new char[(*it)->size];
+			fsMesh.read(buffer, (*it)->size); 
+			dataDestination->parent = buffer;
+			delete buffer;
 			continue;
 		}
 
@@ -160,6 +161,7 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list<ChunkHeader*>& c
 			char* buffer = new char[(*it)->size];
 			fsMesh.read(buffer, (*it)->size);
 			dataDestination->name = buffer;
+			delete buffer;
 			continue;
 		}
 
@@ -373,6 +375,7 @@ void Object::analyseClthChunks(Modl * dataDestination, std::list<ChunkHeader*>&
 			char* buffer = new char[(*it)->size];
 			fsMesh.read(buffer, (*it)->size);
 			dataDestination->texture = buffer;
+			delete buffer;
 			continue;
 		}
 
@@ -491,6 +494,16 @@ std::vector<GLfloat> Object::getUV() const
 	return tempData;
 }
 
+std::list<std::string> Object::getTexture() const
+{
+	std::list<std::string> tempData;
+
+	for (std::list<Modl*>::const_iterator it = lModls.begin(); it != lModls.end(); it++)
+		tempData.push_back((*it)->texture);
+
+	return tempData;
+}
+
 
 /////////////////////////////////////////////////////////////////////////
 // public functions
diff --git a/MshViewer/Source/OpenGlController.cpp b/MshViewer/Source/OpenGlController.cpp
index 62622dc..ce141d8 100644
--- a/MshViewer/Source/OpenGlController.cpp
+++ b/MshViewer/Source/OpenGlController.cpp
@@ -296,12 +296,22 @@ void OpenGLController::loadMsh(const char * path)
 	glGenTextures(1, &gluiTextureID);
 	glBindTexture(GL_TEXTURE_2D, gluiTextureID);
 
-// if texture && open ok, then
-	//TextureTGA tempTex(TEXTURE_NAME);
-	//glTexImage2D(GL_TEXTURE_2D, 0, tempTex.hasAlpha() ? GL_RGBA : GL_RGB, tempTex.getWidth(), tempTex.getHeight(), 0, tempTex.hasAlpha() ? GL_BGRA : GL_BGR, GL_UNSIGNED_BYTE, tempTex.getData().data());
-// else
-	GLubyte solidColor[4] = { 255,0,0,255 };
-	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA ,1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const GLvoid*)solidColor);
+	std::list<std::string> listTextures;
+
+
+	try
+	{
+		if (listTextures.empty())
+			throw std::invalid_argument("no texture names");
+
+		TextureTGA tempTex(listTextures.front().c_str());
+		glTexImage2D(GL_TEXTURE_2D, 0, tempTex.hasAlpha() ? GL_RGBA : GL_RGB, tempTex.getWidth(), tempTex.getHeight(), 0, tempTex.hasAlpha() ? GL_BGRA : GL_BGR, GL_UNSIGNED_BYTE, tempTex.getData().data());
+	}
+	catch (std::invalid_argument e)
+	{
+		GLubyte solidColor[4] = { 255,0,0,255 };
+		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const GLvoid*)solidColor);
+	}
 
 
 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
-- 
GitLab