integrated mesh import class into OpenGL,
Problem with STRP, indices are not logical
This commit is contained in:
parent
b2f174d15f
commit
9700638548
|
@ -3,6 +3,7 @@
|
|||
#include <list>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <gl\glew.h>
|
||||
|
||||
enum Mtyp {
|
||||
null,
|
||||
|
@ -23,7 +24,6 @@ struct Modl {
|
|||
std::string name;
|
||||
std::string parent;
|
||||
Mtyp type;
|
||||
std::uint32_t zeroBaseIndex;
|
||||
std::uint32_t renderFlags;
|
||||
struct {
|
||||
float scale[3];
|
||||
|
@ -36,6 +36,11 @@ struct Modl {
|
|||
float data2;
|
||||
float data3;
|
||||
} swci;
|
||||
std::string texture;
|
||||
float* vertex;
|
||||
float* uv;
|
||||
std::uint32_t meshSize;
|
||||
std::uint32_t* mesh;
|
||||
};
|
||||
|
||||
|
||||
|
@ -53,12 +58,19 @@ private:
|
|||
|
||||
|
||||
private:
|
||||
void setModlDefault(Modl* model);
|
||||
void loadChunks(std::list<ChunkHeader*> &destination, std::streampos start, const std::uint32_t end);
|
||||
void analyseModlChunks(Modl* dataDestination, std::list<ChunkHeader*> &chunkList);
|
||||
void analyseGeomChunks(Modl* dataDestination, std::list<ChunkHeader*> &chunkList);
|
||||
void analyseSegmChunks(Modl* dataDestination, std::list<ChunkHeader*> &chunkList);
|
||||
void analyseClthChunks(Modl* dataDestination, std::list<ChunkHeader*> &chunkList);
|
||||
void readVertex(Modl* dataDestination, std::streampos position);
|
||||
void readUV(Modl* dataDestination, std::streampos position);
|
||||
void readMesh(Modl* dataDestination, std::streampos position);
|
||||
|
||||
|
||||
public:
|
||||
std::vector<GLfloat> getVertex() const;
|
||||
std::vector<GLfloat> getUV() const;
|
||||
|
||||
};
|
||||
|
|
|
@ -69,6 +69,32 @@ Object::~Object()
|
|||
/////////////////////////////////////////////////////////////////////////
|
||||
// private functions
|
||||
|
||||
void Object::setModlDefault(Modl * model)
|
||||
{
|
||||
model->name = "";
|
||||
model->parent = "";
|
||||
model->type = null;
|
||||
model->renderFlags = -1;
|
||||
model->tran.scale[0] = 0;
|
||||
model->tran.scale[1] = 0;
|
||||
model->tran.scale[2] = 0;
|
||||
model->tran.rotation[0] = 0;
|
||||
model->tran.rotation[1] = 0;
|
||||
model->tran.rotation[2] = 0;
|
||||
model->tran.rotation[3] = 0;
|
||||
model->tran.translation[0] = 0;
|
||||
model->tran.translation[1] = 0;
|
||||
model->tran.translation[2] = 0;
|
||||
model->swci.type = -1;
|
||||
model->swci.data1 = -1;
|
||||
model->swci.data2 = -1;
|
||||
model->swci.data3 = -1;
|
||||
model->texture = "";
|
||||
model->vertex = NULL;
|
||||
model->uv = NULL;
|
||||
model->mesh = NULL;
|
||||
}
|
||||
|
||||
void Object::loadChunks(std::list<ChunkHeader*>& destination, std::streampos start, const std::uint32_t end)
|
||||
{
|
||||
// jump to first chunk
|
||||
|
@ -116,12 +142,6 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list<ChunkHeader*>& c
|
|||
dataDestination->type = (Mtyp)tempType;
|
||||
}
|
||||
|
||||
if (!strcmp("MNDX", (*it)->name))
|
||||
{
|
||||
fsMesh.seekg((*it)->position);
|
||||
fsMesh.read(reinterpret_cast<char*>(&dataDestination->zeroBaseIndex), sizeof(dataDestination->zeroBaseIndex));
|
||||
}
|
||||
|
||||
if (!strcmp("PRNT", (*it)->name))
|
||||
{
|
||||
fsMesh.seekg((*it)->position);
|
||||
|
@ -133,9 +153,9 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list<ChunkHeader*>& c
|
|||
if (!strcmp("NAME", (*it)->name))
|
||||
{
|
||||
fsMesh.seekg((*it)->position);
|
||||
char tempName[33] = { 0 };
|
||||
fsMesh.read(reinterpret_cast<char*>(&tempName[0]), (*it)->size > 32 ? 32 : (*it)->size);
|
||||
dataDestination->name = tempName;
|
||||
char* buffer = new char[(*it)->size];
|
||||
fsMesh.read(buffer, (*it)->size);
|
||||
dataDestination->name = buffer;
|
||||
}
|
||||
|
||||
if (!strcmp("FLGS", (*it)->name))
|
||||
|
@ -211,8 +231,7 @@ void Object::analyseGeomChunks(Modl * dataDestination, std::list<ChunkHeader*>&
|
|||
delete tempCursor;
|
||||
}
|
||||
}
|
||||
|
||||
if (!strcmp("CLTH", (*it)->name))
|
||||
else if (!strcmp("CLTH", (*it)->name))
|
||||
{
|
||||
// get all subchunks
|
||||
std::list<ChunkHeader*> tempClthChunks;
|
||||
|
@ -263,10 +282,7 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list<ChunkHeader*>&
|
|||
|
||||
if (!strcmp("POSL", (*it)->name))
|
||||
{
|
||||
fsMesh.seekg((*it)->position);
|
||||
// list of vertex coordinates
|
||||
// long int - 4 - number of coordinates stored in this list
|
||||
// float[3][] - 12 each - XYZ coordinates
|
||||
readVertex(dataDestination, (*it)->position);
|
||||
}
|
||||
|
||||
if (!strcmp("NRML", (*it)->name))
|
||||
|
@ -279,14 +295,12 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list<ChunkHeader*>&
|
|||
|
||||
if (!strcmp("UV0L", (*it)->name))
|
||||
{
|
||||
fsMesh.seekg((*it)->position);
|
||||
// List of UV
|
||||
// long int - 4 - number of UV
|
||||
// float[2][] - 8 each - UV coordinate
|
||||
readUV(dataDestination, (*it)->position);
|
||||
}
|
||||
|
||||
if (!strcmp("STRP", (*it)->name))
|
||||
{
|
||||
readMesh(dataDestination, (*it)->position);
|
||||
fsMesh.seekg((*it)->position);
|
||||
/*
|
||||
List of triangles strips. The start of a strip is indicated by 2 entries
|
||||
|
@ -309,35 +323,71 @@ void Object::analyseClthChunks(Modl * dataDestination, std::list<ChunkHeader*>&
|
|||
if (!strcmp("CTEX", (*it)->name))
|
||||
{
|
||||
fsMesh.seekg((*it)->position);
|
||||
// texture name with extension (how long could it be??)
|
||||
// ascii
|
||||
char* buffer = new char[(*it)->size];
|
||||
fsMesh.read(buffer, (*it)->size);
|
||||
dataDestination->texture = buffer;
|
||||
}
|
||||
|
||||
if (!strcmp("CPOS", (*it)->name))
|
||||
{
|
||||
fsMesh.seekg((*it)->position);
|
||||
// list of Vertex coordinates
|
||||
// long int (4) number of vertex
|
||||
// float[3][] (12 each) XYZ coordinates
|
||||
}
|
||||
readVertex(dataDestination, (*it)->position);
|
||||
|
||||
if (!strcmp("CUV0", (*it)->name))
|
||||
{
|
||||
fsMesh.seekg((*it)->position);
|
||||
// list of UV coordinates
|
||||
// long int (4) number of UV Coordinates
|
||||
// float[2][] (8 each) UV coordinate
|
||||
}
|
||||
readUV(dataDestination, (*it)->position);
|
||||
|
||||
if (!strcmp("CMSH", (*it)->name))
|
||||
{
|
||||
fsMesh.seekg((*it)->position);
|
||||
// cloth tirangles
|
||||
// long int (4) number of points
|
||||
// long int[3][] (16 each) triangle points defined CCW
|
||||
readMesh(dataDestination, (*it)->position);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void Object::readVertex(Modl* dataDestination, std::streampos position)
|
||||
{
|
||||
std::uint32_t tempSize;
|
||||
fsMesh.seekg(position);
|
||||
fsMesh.read(reinterpret_cast<char*>(&tempSize), sizeof(tempSize));
|
||||
|
||||
dataDestination->vertex = new float[tempSize * 3];
|
||||
|
||||
for (unsigned int i = 0; i < tempSize; i += 3)
|
||||
{
|
||||
fsMesh.read(reinterpret_cast<char*>(&dataDestination->vertex[i]), sizeof(float));
|
||||
fsMesh.read(reinterpret_cast<char*>(&dataDestination->vertex[i + 1]), sizeof(float));
|
||||
fsMesh.read(reinterpret_cast<char*>(&dataDestination->vertex[i + 2]), sizeof(float));
|
||||
}
|
||||
}
|
||||
|
||||
void Object::readUV(Modl* dataDestination, std::streampos position)
|
||||
{
|
||||
std::uint32_t tempSize;
|
||||
fsMesh.seekg(position);
|
||||
fsMesh.read(reinterpret_cast<char*>(&tempSize), sizeof(tempSize));
|
||||
|
||||
dataDestination->uv = new float[tempSize * 2];
|
||||
|
||||
for (unsigned int i = 0; i < tempSize; i += 2)
|
||||
{
|
||||
fsMesh.read(reinterpret_cast<char*>(&dataDestination->uv[i]), sizeof(float));
|
||||
fsMesh.read(reinterpret_cast<char*>(&dataDestination->uv[i + 1]), sizeof(float));
|
||||
}
|
||||
}
|
||||
|
||||
void Object::readMesh(Modl* dataDestination, std::streampos position)
|
||||
{
|
||||
fsMesh.seekg(position);
|
||||
fsMesh.read(reinterpret_cast<char*>(&dataDestination->meshSize), sizeof(dataDestination->meshSize));
|
||||
|
||||
dataDestination->mesh = new std::uint32_t[dataDestination->meshSize * 3];
|
||||
|
||||
for (unsigned int i = 0; i < dataDestination->meshSize; i += 3)
|
||||
{
|
||||
fsMesh.read(reinterpret_cast<char*>(&dataDestination->mesh[i]), sizeof(std::uint32_t));
|
||||
fsMesh.read(reinterpret_cast<char*>(&dataDestination->mesh[i + 1]), sizeof(std::uint32_t));
|
||||
fsMesh.read(reinterpret_cast<char*>(&dataDestination->mesh[i + 2]), sizeof(std::uint32_t));
|
||||
}
|
||||
|
||||
|
||||
for (unsigned int i = 0; i < dataDestination->meshSize; i += 3)
|
||||
std::cout << dataDestination->mesh[i] << " " << dataDestination->mesh[i + 1] << " " << dataDestination->mesh[i + 2] << std::endl;
|
||||
|
||||
}
|
||||
|
||||
|
@ -345,6 +395,33 @@ void Object::analyseClthChunks(Modl * dataDestination, std::list<ChunkHeader*>&
|
|||
/////////////////////////////////////////////////////////////////////////
|
||||
// public getter
|
||||
|
||||
std::vector<GLfloat> Object::getVertex() const
|
||||
{
|
||||
std::vector<GLfloat> tempData;
|
||||
|
||||
for (std::list<Modl*>::const_iterator it = lModls.begin(); it != lModls.end(); it++)
|
||||
{
|
||||
for (int i = 0; i < (*it)->meshSize; i++)
|
||||
{
|
||||
|
||||
int tempIndex = (*it)->mesh[i];
|
||||
tempData.push_back((GLfloat)(*it)->vertex[(*it)->mesh[i]]);
|
||||
}
|
||||
}
|
||||
|
||||
return tempData;
|
||||
}
|
||||
|
||||
std::vector<GLfloat> Object::getUV() const
|
||||
{
|
||||
std::vector<GLfloat> tempData;
|
||||
|
||||
for (std::list<Modl*>::const_iterator it = lModls.begin(); it != lModls.end(); it++)
|
||||
for (int i = 0; i < (*it)->meshSize; i++)
|
||||
tempData.push_back((GLfloat)(*it)->uv[(*it)->mesh[i]]);
|
||||
|
||||
return tempData;
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#include "shader.hpp"
|
||||
#include "import.h"
|
||||
#include "Texture.h"
|
||||
#include "Object.h"
|
||||
|
||||
#define VERTEX_SHADER "Shader/VertexTextureShader.mv2shdr"
|
||||
#define FRAGMENT_SHADER "Shader/FragmentTextureShader.mv2shdr"
|
||||
|
@ -280,8 +281,21 @@ void OpenGLController::loadMsh(const char * path)
|
|||
gluiSamplerID = glGetUniformLocation(gluiShaderPrgmID, "textureSampler");
|
||||
|
||||
// get data
|
||||
vfVertices = loadData();
|
||||
vfUV = loadUV();
|
||||
try
|
||||
{
|
||||
Object obj(path);
|
||||
vfVertices = obj.getVertex();
|
||||
|
||||
vfUV = obj.getUV();
|
||||
}
|
||||
catch (std::invalid_argument e)
|
||||
{
|
||||
MessageBox(NULL, e.what(), "MeshViewer 2.0 Error", MB_OK | MB_ICONERROR);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
//vfVertices = loadData();
|
||||
//vfUV = loadUV();
|
||||
|
||||
glGenTextures(1, &gluiTextureID);
|
||||
glBindTexture(GL_TEXTURE_2D, gluiTextureID);
|
||||
|
|
|
@ -28,7 +28,7 @@ openGL:
|
|||
|
||||
OpenGLController *scene = OpenGLController::getInstance();
|
||||
|
||||
scene->loadMsh("test.msh");
|
||||
scene->loadMsh("..\\Release\\Msh\\cube.msh");
|
||||
|
||||
do {
|
||||
scene->updateScene();
|
||||
|
|
Loading…
Reference in New Issue