From ed66d77b69e62f27e83d52d0d76a7257c7913a3d Mon Sep 17 00:00:00 2001
From: Anakin <Battlefrontler@t-online.de>
Date: Mon, 7 Nov 2016 12:06:55 +0100
Subject: [PATCH] still trying to draw instanced

---
 MshViewer/Header/OpenGLController.h   |  2 +-
 MshViewer/Shader/TextureShader.vert   |  4 ++--
 MshViewer/Source/OpenGlController.cpp | 31 ++++++++++++++++-----------
 MshViewer/main.cpp                    |  2 +-
 4 files changed, 22 insertions(+), 17 deletions(-)

diff --git a/MshViewer/Header/OpenGLController.h b/MshViewer/Header/OpenGLController.h
index fcfce57..f6ec8e8 100644
--- a/MshViewer/Header/OpenGLController.h
+++ b/MshViewer/Header/OpenGLController.h
@@ -94,7 +94,7 @@ private:
 	void startGLEW();
 	void setCallbackFunctions();
 
-	glm::mat4 getMVPMatrix();
+	glm::mat4 getMVPMatrix(unsigned int index);
 
 
 ////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/MshViewer/Shader/TextureShader.vert b/MshViewer/Shader/TextureShader.vert
index 20731d1..2bf5f0d 100644
--- a/MshViewer/Shader/TextureShader.vert
+++ b/MshViewer/Shader/TextureShader.vert
@@ -12,12 +12,12 @@ layout(location = 2) in mat4 mvp;
 out vec2 UV;
 
 // Values that stay constant for the whole mesh.
-//uniform mat4 MVP;
+uniform mat4 MVP;
 
 void main(){
 
 	// Output position of the vertex, in clip space : MVP * position
-	gl_Position =  mvp * vec4(vertexPosition_modelspace, 1);
+	gl_Position =  MVP * vec4(vertexPosition_modelspace, 1);
 
 	UV = vertexUV;
 
diff --git a/MshViewer/Source/OpenGlController.cpp b/MshViewer/Source/OpenGlController.cpp
index 11d3139..cbd1b6a 100644
--- a/MshViewer/Source/OpenGlController.cpp
+++ b/MshViewer/Source/OpenGlController.cpp
@@ -188,7 +188,7 @@ void OpenGLController::setCallbackFunctions()
 	glfwSetKeyCallback(pWindow, keyPress);
 }
 
-glm::mat4 OpenGLController::getMVPMatrix()
+glm::mat4 OpenGLController::getMVPMatrix(unsigned int index)
 {
 	// Projection
 	glm::mat4 m4x4Projection = glm::perspective(fFOV, float(iWidth) / float(iHeight), fMinView, fMaxView);
@@ -202,7 +202,7 @@ glm::mat4 OpenGLController::getMVPMatrix()
 
 	// Model
 	//TODO for all
-	glm::mat4 m4x4Model = vModels.front()->m4x4Translation;
+	glm::mat4 m4x4Model = vModels[index]->m4x4Translation;
 	
 	// User controlled rotation
 	glm::mat4 m4x4ModelRot = glm::mat4(1.0f);
@@ -266,23 +266,28 @@ void OpenGLController::updateScene()
 	// use shader prgm
 	glUseProgram(gluiShaderPrgmID);
 
-	// fill vector with MVPs of all Models (only 1 for testing)
-	std::vector<glm::mat4> mvpMatrices;
-	mvpMatrices.push_back(getMVPMatrix());
-
-	// give the MVPs to the shader
-	glBindBuffer(GL_UNIFORM_BUFFER, gluiInstanceBufferID);
-	glBufferSubData(GL_UNIFORM_BUFFER, sizeof(glm::mat4) * mvpMatrices.size(), NULL, mvpMatrices.data());
-	glBindBuffer(GL_UNIFORM_BUFFER, 0);
-
 	// bind texture in texture unit 0
 	glActiveTexture(GL_TEXTURE0);
 	glBindTexture(GL_TEXTURE_2D, gluiTextureID);
 	// tell sampler to use texture unit 0
 	glUniform1i(gluiSamplerID, 0);
 
-	//draw objects hardcoded only 1 instance for testing
-	glDrawArraysInstanced(GL_TRIANGLES, 0, vModels.front()->meshSize, 1);
+	int instanceOffset(0);
+
+	for (unsigned int modelIndex = 0; modelIndex < vModels.size(); modelIndex++)
+	{
+		// give the MVPs to the shader
+		glBindBuffer(GL_UNIFORM_BUFFER, gluiInstanceBufferID);
+		glBufferSubData(GL_UNIFORM_BUFFER, sizeof(glm::mat4), NULL, &getMVPMatrix(0));
+		glBindBuffer(GL_UNIFORM_BUFFER, 0);
+
+		//glUniformMatrix4fv(gluiMatrixID, 1, GL_FALSE, &getMVPMatrix(modelIndex)[0][0]);
+
+		//draw objects hardcoded only 1 instance for testing
+		glDrawArraysInstanced(GL_TRIANGLES, instanceOffset, vModels[modelIndex]->meshSize, vModels.size());
+
+		instanceOffset += vModels[modelIndex]->meshSize;
+	}
 
 	glfwSwapBuffers(pWindow);
 	glfwPollEvents();
diff --git a/MshViewer/main.cpp b/MshViewer/main.cpp
index 149bd44..e282837 100644
--- a/MshViewer/main.cpp
+++ b/MshViewer/main.cpp
@@ -38,7 +38,7 @@ int main(int argc, char** argv)
 
 openGL:
 
-	scene->loadMsh("..\\Release\\Msh\\cubeTrans.msh");
+	scene->loadMsh("..\\Release\\Msh\\houseWOnull.msh");
 
 	do {
 		scene->updateScene();
-- 
GitLab