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

@ -4,6 +4,7 @@
#include <fstream> #include <fstream>
#include <string> #include <string>
#include <gl\glew.h> #include <gl\glew.h>
#include <glm\gtc\matrix_transform.hpp>
enum Mtyp { enum Mtyp {
null, null,
@ -25,6 +26,7 @@ struct Modl {
std::string parent; std::string parent;
Mtyp type; Mtyp type;
std::uint32_t renderFlags; std::uint32_t renderFlags;
glm::mat4 m4x4Translation;
struct { struct {
float scale[3]; float scale[3];
float rotation[4]; float rotation[4];
@ -52,7 +54,7 @@ public:
private: private:
std::list<Modl*> lModls; std::vector<Modl*> vModls;
std::fstream fsMesh; std::fstream fsMesh;
std::vector<std::string> vTextures; std::vector<std::string> vTextures;
@ -73,7 +75,8 @@ private:
public: public:
std::vector<GLfloat> getVertex() const; std::vector<GLfloat> getVertex() const;
std::vector<GLfloat> getUV() const; std::vector<GLfloat> getUV() const;
std::list<std::uint32_t> getSize() const; std::uint32_t getSize() const;
std::list<std::string> getTexture() const; std::list<std::string> getTexture() const;
std::vector<Modl*> getModels() const;
}; };

View File

@ -4,6 +4,7 @@
#include <gl\glfw3.h> #include <gl\glfw3.h>
#include <glm\glm.hpp> #include <glm\glm.hpp>
#include <vector> #include <vector>
#include "Object.h"
class OpenGLController class OpenGLController
{ {
@ -50,6 +51,7 @@ private:
std::vector<GLfloat> vfVertices; std::vector<GLfloat> vfVertices;
std::vector<GLfloat> vfUV; std::vector<GLfloat> vfUV;
std::uint32_t ui32MeshSize; std::uint32_t ui32MeshSize;
std::vector<Modl*> vModels;
// transformation =============== // transformation ===============
//values //values

View File

@ -73,6 +73,7 @@ void Object::setModlDefault(Modl * model)
model->parent = ""; model->parent = "";
model->type = null; model->type = null;
model->renderFlags = -1; model->renderFlags = -1;
model->m4x4Translation = glm::mat4(1.0f);
model->tran.scale[0] = 0; model->tran.scale[0] = 0;
model->tran.scale[1] = 0; model->tran.scale[1] = 0;
model->tran.scale[2] = 0; model->tran.scale[2] = 0;
@ -196,7 +197,7 @@ void Object::analyseMsh2Chunks(std::list<ChunkHeader*>& chunkList)
} }
// save Model data // save Model data
lModls.push_back(tempModl); vModls.push_back(tempModl);
continue; continue;
} }
@ -265,17 +266,54 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list<ChunkHeader*>& c
if (!strcmp("TRAN", (*it)->name)) if (!strcmp("TRAN", (*it)->name))
{ {
float tempScale[3];
float tempRotation[4];
float tempTrans[3];
fsMesh.seekg((*it)->position); fsMesh.seekg((*it)->position);
fsMesh.read(reinterpret_cast<char*>(&dataDestination->tran.scale[0]), sizeof(float));
fsMesh.read(reinterpret_cast<char*>(&dataDestination->tran.scale[1]), sizeof(float)); fsMesh.read(reinterpret_cast<char*>(&tempScale[0]), sizeof(float));
fsMesh.read(reinterpret_cast<char*>(&dataDestination->tran.scale[2]), sizeof(float)); fsMesh.read(reinterpret_cast<char*>(&tempScale[1]), sizeof(float));
fsMesh.read(reinterpret_cast<char*>(&dataDestination->tran.rotation[0]), sizeof(float)); fsMesh.read(reinterpret_cast<char*>(&tempScale[2]), sizeof(float));
fsMesh.read(reinterpret_cast<char*>(&dataDestination->tran.rotation[1]), sizeof(float));
fsMesh.read(reinterpret_cast<char*>(&dataDestination->tran.rotation[2]), sizeof(float)); //TODO: rotation value is wrong
fsMesh.read(reinterpret_cast<char*>(&dataDestination->tran.rotation[3]), sizeof(float)); fsMesh.read(reinterpret_cast<char*>(&tempRotation[0]), sizeof(float));
fsMesh.read(reinterpret_cast<char*>(&dataDestination->tran.translation[0]), sizeof(float)); fsMesh.read(reinterpret_cast<char*>(&tempRotation[1]), sizeof(float));
fsMesh.read(reinterpret_cast<char*>(&dataDestination->tran.translation[1]), sizeof(float)); fsMesh.read(reinterpret_cast<char*>(&tempRotation[2]), sizeof(float));
fsMesh.read(reinterpret_cast<char*>(&dataDestination->tran.translation[2]), sizeof(float)); fsMesh.read(reinterpret_cast<char*>(&tempRotation[3]), sizeof(float));
fsMesh.read(reinterpret_cast<char*>(&tempTrans[0]), sizeof(float));
fsMesh.read(reinterpret_cast<char*>(&tempTrans[1]), sizeof(float));
fsMesh.read(reinterpret_cast<char*>(&tempTrans[2]), sizeof(float));
dataDestination->m4x4Translation = glm::scale(
dataDestination->m4x4Translation,
glm::vec3(tempScale[0], tempScale[1], tempScale[2])
);
dataDestination->m4x4Translation = glm::translate(
dataDestination->m4x4Translation,
glm::vec3(tempTrans[0], tempTrans[1], tempTrans[2])
);
dataDestination->m4x4Translation = glm::rotate(
dataDestination->m4x4Translation,
tempRotation[0],
glm::vec3(1, 0, 0)
);
dataDestination->m4x4Translation = glm::rotate(
dataDestination->m4x4Translation,
tempRotation[1],
glm::vec3(0, 1, 0)
);
dataDestination->m4x4Translation = glm::rotate(
dataDestination->m4x4Translation,
tempRotation[2],
glm::vec3(0, 0, 1)
);
continue; continue;
} }
@ -528,8 +566,11 @@ std::vector<GLfloat> Object::getVertex() const
{ {
std::vector<GLfloat> tempData; std::vector<GLfloat> tempData;
for (std::list<Modl*>::const_iterator it = lModls.begin(); it != lModls.end(); it++) for (std::vector<Modl*>::const_iterator it = vModls.begin(); it != vModls.end(); it++)
{ {
if ((*it)->renderFlags == 1)
continue;
for (unsigned int i = 0; i < (*it)->meshSize; i++) for (unsigned int i = 0; i < (*it)->meshSize; i++)
{ {
tempData.push_back((GLfloat)(*it)->vertex[(*it)->mesh[i] * 3]); tempData.push_back((GLfloat)(*it)->vertex[(*it)->mesh[i] * 3]);
@ -545,8 +586,11 @@ std::vector<GLfloat> Object::getUV() const
{ {
std::vector<GLfloat> tempData; std::vector<GLfloat> tempData;
for (std::list<Modl*>::const_iterator it = lModls.begin(); it != lModls.end(); it++) for (std::vector<Modl*>::const_iterator it = vModls.begin(); it != vModls.end(); it++)
{ {
if ((*it)->renderFlags == 1)
continue;
if ((*it)->uv == NULL) if ((*it)->uv == NULL)
{ {
for (unsigned int i = 0; i < (*it)->meshSize; i++) for (unsigned int i = 0; i < (*it)->meshSize; i++)
@ -563,12 +607,17 @@ std::vector<GLfloat> Object::getUV() const
return tempData; return tempData;
} }
std::list<std::uint32_t> Object::getSize() const std::uint32_t Object::getSize() const
{ {
std::list<std::uint32_t> tempData; std::uint32_t tempData(0);
for (std::list<Modl*>::const_iterator it = lModls.begin(); it != lModls.end(); it++) for (std::vector<Modl*>::const_iterator it = vModls.begin(); it != vModls.end(); it++)
tempData.push_back((*it)->meshSize); {
if ((*it)->renderFlags == 1)
continue;
tempData += (*it)->meshSize;
}
return tempData; return tempData;
} }
@ -577,12 +626,22 @@ std::list<std::string> Object::getTexture() const
{ {
std::list<std::string> tempData; std::list<std::string> tempData;
for (std::list<Modl*>::const_iterator it = lModls.begin(); it != lModls.end(); it++) for (std::vector<Modl*>::const_iterator it = vModls.begin(); it != vModls.end(); it++)
{
if ((*it)->renderFlags == 1)
continue;
tempData.push_back((*it)->texture); tempData.push_back((*it)->texture);
}
return tempData; return tempData;
} }
std::vector<Modl*> Object::getModels() const
{
return vModls;
}
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
// public functions // public functions

View File

@ -8,7 +8,6 @@
#include "shader.hpp" #include "shader.hpp"
#include "import.h" #include "import.h"
#include "Texture.h" #include "Texture.h"
#include "Object.h"
#define VERTEX_SHADER "Shader/VertexTextureShader.mv2shdr" #define VERTEX_SHADER "Shader/VertexTextureShader.mv2shdr"
#define FRAGMENT_SHADER "Shader/FragmentTextureShader.mv2shdr" #define FRAGMENT_SHADER "Shader/FragmentTextureShader.mv2shdr"
@ -167,7 +166,10 @@ glm::mat4 OpenGLController::getMVPMatrix()
glm::vec3(dTranslationX, dTranslationY, dTranslationZ - 1), glm::vec3(dTranslationX, dTranslationY, dTranslationZ - 1),
glm::vec3(0, 1, 0) 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, fRotationX, glm::vec3(1, 0, 0));
m4x4Model = glm::rotate(m4x4Model, fRotationY, glm::vec3(0, 1, 0)); m4x4Model = glm::rotate(m4x4Model, fRotationY, glm::vec3(0, 1, 0));
m4x4Model = glm::rotate(m4x4Model, fRotationZ, glm::vec3(0, 0, 1)); 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); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
//draw objects //draw objects
glDrawArrays(GL_TRIANGLES, 0, ui32MeshSize); //// TODO: for all
glDrawArrays(GL_TRIANGLES, 0, vModels.front()->meshSize);
//close attributes //close attributes
glDisableVertexAttribArray(0); glDisableVertexAttribArray(0);
@ -285,10 +288,13 @@ void OpenGLController::loadMsh(const char * path)
try try
{ {
Object obj(path); Object obj(path);
vfVertices = obj.getVertex();
vfUV = obj.getUV(); vModels = obj.getModels();
listTextures = obj.getTexture();
ui32MeshSize = obj.getSize().front(); //vfVertices = obj.getVertex();
//vfUV = obj.getUV();
//listTextures = obj.getTexture();
//ui32MeshSize = obj.getSize();
} }
catch (std::invalid_argument e) catch (std::invalid_argument e)
{ {
@ -301,21 +307,39 @@ void OpenGLController::loadMsh(const char * path)
try try
{ {
if (listTextures.empty()) ////TODO: for all
throw std::invalid_argument("no texture names"); if (vModels.front()->texture == "")
throw std::invalid_argument("no texture name");
std::string tempPath = path; std::string tempPath = path;
while (tempPath.back() != '/' && tempPath.back() != '\\') while (tempPath.back() != '/' && tempPath.back() != '\\')
tempPath.pop_back(); tempPath.pop_back();
TextureTGA tempTex(std::string(tempPath + listTextures.front()).c_str()); 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()); 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) catch (std::invalid_argument e)
{ {
GLubyte solidColor[4] = { 255, 0, 0, 255}; 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); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
@ -325,18 +349,46 @@ void OpenGLController::loadMsh(const char * path)
glGenerateMipmap(GL_TEXTURE_2D); glGenerateMipmap(GL_TEXTURE_2D);
glBindBuffer(GL_ARRAY_BUFFER, gluiVertexBufferID); 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( glBufferData(
GL_ARRAY_BUFFER, GL_ARRAY_BUFFER,
sizeof(vfVertices) * vfVertices.size(), sizeof(tempVertex) * tempVertex.size(),
vfVertices.data(), tempVertex.data(),
GL_STATIC_DRAW 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); glBindBuffer(GL_ARRAY_BUFFER, gluiUVBufferID);
glBufferData( glBufferData(
GL_ARRAY_BUFFER, GL_ARRAY_BUFFER,
sizeof(vfUV) * vfUV.size(), sizeof(tempUV) * tempUV.size(),
vfUV.data(), tempUV.data(),
GL_STATIC_DRAW GL_STATIC_DRAW
); );

View File

@ -28,7 +28,7 @@ openGL:
OpenGLController *scene = OpenGLController::getInstance(); OpenGLController *scene = OpenGLController::getInstance();
scene->loadMsh("..\\Release\\Msh\\cubeTex.msh"); scene->loadMsh("..\\Release\\Msh\\cubeTrans.msh");
do { do {
scene->updateScene(); scene->updateScene();