display all models from a mesh,

problem, apply different model matrices for each model,
solution give a list of all models to ogl controller, where they are handled,
next steps: find solution for wrong rotation + draw ALL models instead of only the first
This commit is contained in:
Anakin
2016-10-30 14:22:08 +01:00
parent a8bfacfd73
commit e509b1e11c
5 changed files with 153 additions and 37 deletions

View File

@@ -8,7 +8,6 @@
#include "shader.hpp"
#include "import.h"
#include "Texture.h"
#include "Object.h"
#define VERTEX_SHADER "Shader/VertexTextureShader.mv2shdr"
#define FRAGMENT_SHADER "Shader/FragmentTextureShader.mv2shdr"
@@ -167,7 +166,10 @@ glm::mat4 OpenGLController::getMVPMatrix()
glm::vec3(dTranslationX, dTranslationY, dTranslationZ - 1),
glm::vec3(0, 1, 0)
);
m4x4Model = glm::mat4(1.0f);
//m4x4Model = glm::mat4(1.0f);
//TODO for all
m4x4Model = vModels.front()->m4x4Translation;
m4x4Model = glm::rotate(m4x4Model, fRotationX, glm::vec3(1, 0, 0));
m4x4Model = glm::rotate(m4x4Model, fRotationY, glm::vec3(0, 1, 0));
m4x4Model = glm::rotate(m4x4Model, fRotationZ, glm::vec3(0, 0, 1));
@@ -247,7 +249,8 @@ void OpenGLController::updateScene()
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
//draw objects
glDrawArrays(GL_TRIANGLES, 0, ui32MeshSize);
//// TODO: for all
glDrawArrays(GL_TRIANGLES, 0, vModels.front()->meshSize);
//close attributes
glDisableVertexAttribArray(0);
@@ -285,10 +288,13 @@ void OpenGLController::loadMsh(const char * path)
try
{
Object obj(path);
vfVertices = obj.getVertex();
vfUV = obj.getUV();
listTextures = obj.getTexture();
ui32MeshSize = obj.getSize().front();
vModels = obj.getModels();
//vfVertices = obj.getVertex();
//vfUV = obj.getUV();
//listTextures = obj.getTexture();
//ui32MeshSize = obj.getSize();
}
catch (std::invalid_argument e)
{
@@ -301,21 +307,39 @@ void OpenGLController::loadMsh(const char * path)
try
{
if (listTextures.empty())
throw std::invalid_argument("no texture names");
////TODO: for all
if (vModels.front()->texture == "")
throw std::invalid_argument("no texture name");
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());
TextureTGA tempTex(std::string(tempPath + vModels.front()->texture).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);
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);
@@ -325,18 +349,46 @@ void OpenGLController::loadMsh(const char * path)
glGenerateMipmap(GL_TEXTURE_2D);
glBindBuffer(GL_ARRAY_BUFFER, gluiVertexBufferID);
////TODO: for all
std::vector<GLfloat> tempVertex;
for (unsigned int i = 0; i < vModels.front()->meshSize; i++)
{
tempVertex.push_back((GLfloat)vModels.front()->vertex[vModels.front()->mesh[i] * 3]);
tempVertex.push_back((GLfloat)vModels.front()->vertex[vModels.front()->mesh[i] * 3 + 1]);
tempVertex.push_back((GLfloat)vModels.front()->vertex[vModels.front()->mesh[i] * 3 + 2]);
}
glBufferData(
GL_ARRAY_BUFFER,
sizeof(vfVertices) * vfVertices.size(),
vfVertices.data(),
sizeof(tempVertex) * tempVertex.size(),
tempVertex.data(),
GL_STATIC_DRAW
);
////TODO: for all
std::vector<GLfloat> tempUV;
if (vModels.front()->uv == NULL)
{
for (unsigned int i = 0; i < vModels.front()->meshSize; i++)
tempUV.push_back(1.0);
}
else
{
for (unsigned int i = 0; i < vModels.front()->meshSize; i++)
{
tempUV.push_back((GLfloat)vModels.front()->uv[vModels.front()->mesh[i] * 2]);
tempUV.push_back((GLfloat)vModels.front()->uv[vModels.front()->mesh[i] * 2 + 1]);
}
}
glBindBuffer(GL_ARRAY_BUFFER, gluiUVBufferID);
glBufferData(
GL_ARRAY_BUFFER,
sizeof(vfUV) * vfUV.size(),
vfUV.data(),
sizeof(tempUV) * tempUV.size(),
tempUV.data(),
GL_STATIC_DRAW
);