From 4f52510ff95a58a59b4ab33c234aac8352a548f4 Mon Sep 17 00:00:00 2001
From: Anakin <Battlefrontler@t-online.de>
Date: Sat, 22 Oct 2016 15:35:06 +0200
Subject: [PATCH] texture is now correctly detected and used, there seams to be
 a problem with the UV. They are not correctly

---
 MshViewer/Source/Object.cpp              |  11 ++++++++---
 MshViewer/Source/OpenGlController.cpp    |  14 +++++++++-----
 MshViewer/main.cpp                       |   2 +-
 Release/Msh/cubeTex.msh                  | Bin 0 -> 1376 bytes
 Release/{Textures => Msh}/texture32R.tga | Bin
 5 files changed, 18 insertions(+), 9 deletions(-)
 create mode 100644 Release/Msh/cubeTex.msh
 rename Release/{Textures => Msh}/texture32R.tga (100%)

diff --git a/MshViewer/Source/Object.cpp b/MshViewer/Source/Object.cpp
index 4d5a5b4..316c74b 100644
--- a/MshViewer/Source/Object.cpp
+++ b/MshViewer/Source/Object.cpp
@@ -135,9 +135,14 @@ void Object::analyseMsh2Chunks(std::list<ChunkHeader*>& chunkList)
 	{
 		if (!strcmp("MATL", (*it)->name))
 		{
+			// "useless" information how many MATD follow
+			fsMesh.seekg((*it)->position);
+			std::uint32_t tempMatdCount;
+			fsMesh.read(reinterpret_cast<char*>(&tempMatdCount), sizeof(std::uint32_t));
+
 			// get all MATD from MATL list
 			std::list<ChunkHeader*> tempMatlChunks;
-			loadChunks(tempMatlChunks, (*it)->position, (*it)->size);
+			loadChunks(tempMatlChunks, fsMesh.tellg(), (*it)->size - 4);
 
 			// evaluate MATL subchunks
 			for (std::list<ChunkHeader*>::iterator it = tempMatlChunks.begin(); it != tempMatlChunks.end(); it++)
@@ -383,13 +388,13 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list<ChunkHeader*>&
 			fsMesh.seekg((*it)->position);
 			std::uint32_t tempIndex;
 			fsMesh.read(reinterpret_cast<char*>(&tempIndex), sizeof(tempIndex));
-			if (vTextures.size() < tempIndex - 1)
+			if (vTextures.size() <= tempIndex)
 			{
 				std::cout << "warning texture index <" << tempIndex << "> unknown" << std::endl;
 				dataDestination->texture = "";
 				continue;
 			}
-			dataDestination->texture = vTextures[tempIndex - 1];
+			dataDestination->texture = vTextures[tempIndex];
 			continue;
 		}
 
diff --git a/MshViewer/Source/OpenGlController.cpp b/MshViewer/Source/OpenGlController.cpp
index 2515ba3..28ace6e 100644
--- a/MshViewer/Source/OpenGlController.cpp
+++ b/MshViewer/Source/OpenGlController.cpp
@@ -12,7 +12,7 @@
 
 #define VERTEX_SHADER "Shader/VertexTextureShader.mv2shdr"
 #define FRAGMENT_SHADER "Shader/FragmentTextureShader.mv2shdr"
-#define TEXTURE_NAME "Textures/texture32R.tga"
+//#define TEXTURE_NAME "Textures/texture32R.tga"
 
 /////////////////////////////////////////////////////////////////////////
 // public constructor/destructor
@@ -281,11 +281,13 @@ void OpenGLController::loadMsh(const char * path)
 	gluiSamplerID = glGetUniformLocation(gluiShaderPrgmID, "textureSampler");
 
 	// get data
+	std::list<std::string> listTextures;
 	try
 	{
 		Object obj(path);
 		vfVertices = obj.getVertex();
 		vfUV = obj.getUV();
+		listTextures = obj.getTexture();
 	}
 	catch (std::invalid_argument e)
 	{
@@ -296,15 +298,17 @@ void OpenGLController::loadMsh(const char * path)
 	glGenTextures(1, &gluiTextureID);
 	glBindTexture(GL_TEXTURE_2D, gluiTextureID);
 
-	std::list<std::string> listTextures;
-
-
 	try
 	{
 		if (listTextures.empty())
 			throw std::invalid_argument("no texture names");
 
-		TextureTGA tempTex(listTextures.front().c_str());
+		std::string tempPath = path;
+
+		while (tempPath.back() != '/' && tempPath.back() != '\\')
+			tempPath.pop_back();
+
+		TextureTGA tempTex(std::string(tempPath + 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)
diff --git a/MshViewer/main.cpp b/MshViewer/main.cpp
index bcda1d0..fb2fff4 100644
--- a/MshViewer/main.cpp
+++ b/MshViewer/main.cpp
@@ -28,7 +28,7 @@ openGL:
 
 	OpenGLController *scene = OpenGLController::getInstance();
 
-	scene->loadMsh("..\\Release\\Msh\\cube.msh");
+	scene->loadMsh("..\\Release\\Msh\\cubeTex.msh");
 
 	do {
 		scene->updateScene();
diff --git a/Release/Msh/cubeTex.msh b/Release/Msh/cubeTex.msh
new file mode 100644
index 0000000000000000000000000000000000000000..c5ebab9a16c691eeb72bf9bd66a3e752a4899786
GIT binary patch
literal 1376
zcmeZpbqR`KWnl0P_Av5bWnc*Q^m9vLU|{fb^mP?sU|<MNPR&b=k8%xhttiMZDoF*&
zxdl1;@-Q$kFfuSOfcOjy9~_;W{3CQ2&_RPesz`$a0|NwK-+b4>*D=JW0cwt~V~9%$
z)LxL;zKJENMVW~?ApI_mA&w>t3=GD`#`ZAGzyLBE70;SE(+-9q@*Yl(Awf<oAVU}!
z7(yZpTm%>x7)nwrN=l1Tjg5lzO41V<7<~O*e0*3K7<@w_13+p)*w@b`0z`xS2nq`h
z1_p-Y(xg<7QEoo&!5}#hJ0!@_PXok2aSvDkN`Tx5Wil`@xV!rMK44~GASb+nUEO_`
zFf%atI)-?{><{n{_Ay{&V2}h`(Qtr)0gho|4H5&fK{PssiNk1+JTe=^2VsyH2*cRO
zG>i?Bhhdl)It`PD(I9ykhKa#wkT^OUB!-MZ^2mG;8zct9AU+7g_#hh0_Y3kx3O8`z
zF)%PR?1!<ivniDanU0(uaIvYa4x77SVlW!TPEfXNfaD>NI2eQZp<xC-XBgn=4Jm&k
z{E~fjvi1H8YBtY4FS1=UVVy0A526jF=3B2aNU#_A{KxjjggX0=lJl)Wv`9pq{hND<
z_LJTw+UI-LfoTx`x=)7v{-p<P!_#W)mD8JTKs1OCb`6*S*$KiR4v0p^3=AOqLGA$A
zw-RJ;L><_CkhwbP%{DOq1&0I$_%JXqsDO=OY+zzwZeVF(WngPyZ{T3yY~X6(X5eYy
zZQx_zZxCn@WDsg#Y!GG;X<%t!W)N*)Z(w5(Yv5|&WDsxQZQx;$Xb@=NXK?m01R2Qy
E0I2f|!~g&Q

literal 0
HcmV?d00001

diff --git a/Release/Textures/texture32R.tga b/Release/Msh/texture32R.tga
similarity index 100%
rename from Release/Textures/texture32R.tga
rename to Release/Msh/texture32R.tga
-- 
GitLab