fasten code,
fixed vertex crash, still crashes while getting UV data, vertex are not displayed correctly
This commit is contained in:
parent
9700638548
commit
e583c361ec
|
@ -66,7 +66,6 @@ private:
|
|||
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:
|
||||
|
|
|
@ -93,6 +93,7 @@ void Object::setModlDefault(Modl * model)
|
|||
model->vertex = NULL;
|
||||
model->uv = NULL;
|
||||
model->mesh = NULL;
|
||||
model->meshSize = 0;
|
||||
}
|
||||
|
||||
void Object::loadChunks(std::list<ChunkHeader*>& destination, std::streampos start, const std::uint32_t end)
|
||||
|
@ -140,6 +141,7 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list<ChunkHeader*>& c
|
|||
std::uint32_t tempType;
|
||||
fsMesh.read(reinterpret_cast<char*>(&tempType), sizeof(tempType));
|
||||
dataDestination->type = (Mtyp)tempType;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!strcmp("PRNT", (*it)->name))
|
||||
|
@ -148,6 +150,7 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list<ChunkHeader*>& c
|
|||
char tempName[33] = { 0 };
|
||||
fsMesh.read(reinterpret_cast<char*>(&tempName[0]), (*it)->size > 32 ? 32 : (*it)->size);
|
||||
dataDestination->parent = tempName;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!strcmp("NAME", (*it)->name))
|
||||
|
@ -156,12 +159,14 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list<ChunkHeader*>& c
|
|||
char* buffer = new char[(*it)->size];
|
||||
fsMesh.read(buffer, (*it)->size);
|
||||
dataDestination->name = buffer;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!strcmp("FLGS", (*it)->name))
|
||||
{
|
||||
fsMesh.seekg((*it)->position);
|
||||
fsMesh.read(reinterpret_cast<char*>(&dataDestination->renderFlags), sizeof(dataDestination->renderFlags));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!strcmp("TRAN", (*it)->name))
|
||||
|
@ -177,6 +182,7 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list<ChunkHeader*>& c
|
|||
fsMesh.read(reinterpret_cast<char*>(&dataDestination->tran.translation[0]), sizeof(float));
|
||||
fsMesh.read(reinterpret_cast<char*>(&dataDestination->tran.translation[1]), sizeof(float));
|
||||
fsMesh.read(reinterpret_cast<char*>(&dataDestination->tran.translation[2]), sizeof(float));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!strcmp("GEOM", (*it)->name))
|
||||
|
@ -195,6 +201,8 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list<ChunkHeader*>& c
|
|||
tempGeomChunks.pop_front();
|
||||
delete tempCursor;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!strcmp("SWCI", (*it)->name))
|
||||
|
@ -204,6 +212,7 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list<ChunkHeader*>& c
|
|||
fsMesh.read(reinterpret_cast<char*>(&dataDestination->swci.data1), sizeof(dataDestination->swci.data1));
|
||||
fsMesh.read(reinterpret_cast<char*>(&dataDestination->swci.data2), sizeof(dataDestination->swci.data2));
|
||||
fsMesh.read(reinterpret_cast<char*>(&dataDestination->swci.data3), sizeof(dataDestination->swci.data3));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -230,8 +239,10 @@ void Object::analyseGeomChunks(Modl * dataDestination, std::list<ChunkHeader*>&
|
|||
tempSegmChunks.pop_front();
|
||||
delete tempCursor;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
else if (!strcmp("CLTH", (*it)->name))
|
||||
|
||||
if (!strcmp("CLTH", (*it)->name))
|
||||
{
|
||||
// get all subchunks
|
||||
std::list<ChunkHeader*> tempClthChunks;
|
||||
|
@ -247,6 +258,7 @@ void Object::analyseGeomChunks(Modl * dataDestination, std::list<ChunkHeader*>&
|
|||
tempClthChunks.pop_front();
|
||||
delete tempCursor;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -270,7 +282,7 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list<ChunkHeader*>&
|
|||
> short int - 2 - Second reference into an edge. In all example .msh files I've seen this always refers to the same vertex as the first edge reference
|
||||
> short int - 2 - MAX_VALUE of short integers (65535). Indicates the end of this edge
|
||||
*/
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!strcmp("MATI", (*it)->name))
|
||||
|
@ -278,11 +290,13 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list<ChunkHeader*>&
|
|||
fsMesh.seekg((*it)->position);
|
||||
// material index index into MATL
|
||||
// long int - 4 - material index
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!strcmp("POSL", (*it)->name))
|
||||
{
|
||||
readVertex(dataDestination, (*it)->position);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!strcmp("NRML", (*it)->name))
|
||||
|
@ -291,17 +305,49 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list<ChunkHeader*>&
|
|||
// List of normals
|
||||
// long int - 4 - number of normal vectores stored in this list
|
||||
// float[3][] - 12 each - UVW vector for each vertex
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!strcmp("UV0L", (*it)->name))
|
||||
{
|
||||
readUV(dataDestination, (*it)->position);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!strcmp("STRP", (*it)->name))
|
||||
{
|
||||
readMesh(dataDestination, (*it)->position);
|
||||
fsMesh.seekg((*it)->position);
|
||||
|
||||
fsMesh.seekg((*it)->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)
|
||||
{
|
||||
std::uint16_t tempValue[3];
|
||||
fsMesh.read(reinterpret_cast<char*>(&tempValue[0]), sizeof(std::uint16_t));
|
||||
fsMesh.read(reinterpret_cast<char*>(&tempValue[1]), sizeof(std::uint16_t));
|
||||
fsMesh.read(reinterpret_cast<char*>(&tempValue[2]), sizeof(std::uint16_t));
|
||||
|
||||
if (!(tempValue[0] >> 15 && tempValue[1] >> 15 && !(tempValue[2] >> 15)))
|
||||
throw std::invalid_argument("invalid file. go and triangulate!");
|
||||
|
||||
tempValue[0] = (std::uint16_t(tempValue[0] << 1) >> 1);
|
||||
tempValue[1] = (std::uint16_t(tempValue[1] << 1) >> 1);
|
||||
|
||||
dataDestination->mesh[i] = (std::uint32_t)tempValue[0];
|
||||
dataDestination->mesh[i + 1] = (std::uint32_t)tempValue[1];
|
||||
dataDestination->mesh[i + 2] = (std::uint32_t)tempValue[2];
|
||||
|
||||
}
|
||||
|
||||
std::cout << "vertices" << std::endl;
|
||||
for (int i = 0; i < dataDestination->meshSize; i += 3)
|
||||
std::cout << dataDestination->mesh[i] << " - " << dataDestination->mesh[i + 1] << " - " << dataDestination->mesh[i + 2] << std::endl;
|
||||
|
||||
continue;
|
||||
|
||||
/*
|
||||
List of triangles strips. The start of a strip is indicated by 2 entries
|
||||
with a high bit set (0x8000 or 32768 added). Triangles are listed CW, CCW,
|
||||
|
@ -326,16 +372,36 @@ void Object::analyseClthChunks(Modl * dataDestination, std::list<ChunkHeader*>&
|
|||
char* buffer = new char[(*it)->size];
|
||||
fsMesh.read(buffer, (*it)->size);
|
||||
dataDestination->texture = buffer;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!strcmp("CPOS", (*it)->name))
|
||||
{
|
||||
readVertex(dataDestination, (*it)->position);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!strcmp("CUV0", (*it)->name))
|
||||
{
|
||||
readUV(dataDestination, (*it)->position);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!strcmp("CMSH", (*it)->name))
|
||||
readMesh(dataDestination, (*it)->position);
|
||||
{
|
||||
fsMesh.seekg((*it)->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));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -369,25 +435,10 @@ void Object::readUV(Modl* dataDestination, std::streampos position)
|
|||
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;
|
||||
std::cout << "UV" << std::endl;
|
||||
for (int i = 0; i < dataDestination->meshSize; i += 3)
|
||||
std::cout << dataDestination->mesh[i] << " - " << dataDestination->mesh[i + 1] << " - " << dataDestination->mesh[i + 2] << std::endl;
|
||||
|
||||
}
|
||||
|
||||
|
@ -401,11 +452,11 @@ std::vector<GLfloat> Object::getVertex() const
|
|||
|
||||
for (std::list<Modl*>::const_iterator it = lModls.begin(); it != lModls.end(); it++)
|
||||
{
|
||||
for (int i = 0; i < (*it)->meshSize; i++)
|
||||
for (int i = 0; i < (*it)->meshSize; i += 3)
|
||||
{
|
||||
|
||||
int tempIndex = (*it)->mesh[i];
|
||||
tempData.push_back((GLfloat)(*it)->vertex[(*it)->mesh[i]]);
|
||||
tempData.push_back((GLfloat)(*it)->vertex[(*it)->mesh[i] + 1]);
|
||||
tempData.push_back((GLfloat)(*it)->vertex[(*it)->mesh[i] + 2]);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -417,8 +468,14 @@ std::vector<GLfloat> Object::getUV() const
|
|||
std::vector<GLfloat> tempData;
|
||||
|
||||
for (std::list<Modl*>::const_iterator it = lModls.begin(); it != lModls.end(); it++)
|
||||
{
|
||||
if ((*it)->uv == NULL)
|
||||
continue;
|
||||
for (int i = 0; i < (*it)->meshSize; i++)
|
||||
{
|
||||
tempData.push_back((GLfloat)(*it)->uv[(*it)->mesh[i]]);
|
||||
}
|
||||
}
|
||||
|
||||
return tempData;
|
||||
}
|
||||
|
|
|
@ -286,7 +286,8 @@ void OpenGLController::loadMsh(const char * path)
|
|||
Object obj(path);
|
||||
vfVertices = obj.getVertex();
|
||||
|
||||
vfUV = obj.getUV();
|
||||
vfUV = loadUV();
|
||||
//vfUV = obj.getUV();
|
||||
}
|
||||
catch (std::invalid_argument e)
|
||||
{
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
//goto openGL;
|
||||
goto openGL;
|
||||
|
||||
try {
|
||||
Object obj("..\\Release\\Msh\\cube.msh");
|
||||
|
|
Loading…
Reference in New Issue