12 Commits

Author SHA1 Message Date
Anakin
9abd285239 new release version,
Features:
- reset view before loading new mesh
- reset view when pressing space
- clustered msh can be displayed
- shadow mesh, nulls, bones, hidden things are no longer displayed
- now mulitpolygons can be displayed
- fasten and improved code
Bugs:
- cluster mixed up textures,
- at most mesh only every 2nd triangle is displayed,
- triangulation is not very good (DarthDUCK's Proton charge)
2016-11-20 17:00:17 +01:00
Anakin
bcdc17c362 shorten code,
tested multipoly, there are some problems, look at the README.md,
2016-11-20 16:56:56 +01:00
Anakin
b3a8b4bb81 triangulation implemented,
now need to test it
2016-11-20 16:46:49 +01:00
Anakin
f81f03353d cloth stores the data in vectors, too;
removed the old unused code
2016-11-20 13:09:02 +01:00
Anakin
9ac1a25954 fixed the uv problem 2016-11-20 12:38:22 +01:00
Anakin
57df0a2e15 use different variables to store the data. The aim is to handle even not triangulated mesh files.
At the moment there is a problem with the UV using the new method
2016-11-20 12:26:23 +01:00
Anakin
927ce1cd0a fixed warnings 2016-11-16 14:18:11 +01:00
Anakin
f4d8018f8f workaround: don't read Data from bones, nulls, shadow and hidden,
new implementation of segm read strp to not only read triangles,
needs to be included in the buffer fill function/draw function
2016-11-16 14:00:51 +01:00
Anakin
806024f4f9 faster method for texture handling 2016-11-13 15:46:52 +01:00
Anakin
0f379ba04a ignore null, bones, shadow mesh and hidden things 2016-11-13 12:47:19 +01:00
Anakin
8929717c9f handle clustered models 2016-11-13 12:15:33 +01:00
Anakin
bbe657d030 reset view before loading a new file,
reset view when pressing space
2016-11-12 13:53:49 +01:00
10 changed files with 265 additions and 124 deletions

View File

@@ -20,17 +20,20 @@ struct ChunkHeader {
std::streampos position;
};
struct Segment {
std::uint32_t textureIndex = 0;
float* vertex = nullptr;
float* uv = nullptr;
std::vector<std::vector<std::uint32_t>*> meshIndices; // indices into vertex array
};
struct Modl {
std::string name = "";
std::string parent = "";
Mtyp type = null;
std::int32_t renderFlags = -1;
glm::mat4 m4x4Translation = glm::mat4(1.0f);
std::string texture = "";
float* vertex = nullptr;
float* uv = nullptr;
std::uint32_t* mesh = nullptr;
std::uint32_t meshSize = 0;
std::vector<Segment*> segmLst;
};
@@ -55,11 +58,12 @@ private:
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 readVertex(Segment* dataDestination, std::streampos position);
void readUV(Segment* dataDestination, std::streampos position);
public:
std::vector<Modl*> getModels() const;
std::vector<std::string> getTextureList() const;
};

View File

@@ -116,6 +116,7 @@ public:
void addTransX(double value);
void addTransY(double value);
void addTransZ(double value);
void resetView();
// main routine
GLFWwindow* getWindow() const;

View File

@@ -369,43 +369,20 @@ void Object::analyseGeomChunks(Modl * dataDestination, std::list<ChunkHeader*>&
void Object::analyseSegmChunks(Modl * dataDestination, std::list<ChunkHeader*>& chunkList)
{
Segment* tempData = new Segment;
for (std::list<ChunkHeader*>::iterator it = chunkList.begin(); it != chunkList.end(); it++)
{
/*if (!strcmp("SHDW", (*it)->name))
{
fsMesh.seekg((*it)->position);
/* shadow mesh geometry
long int - 4 - number of vertex positions
float[3][] - 12 each - vertex positions (XYZ)
long int - 4 - number of edges
short int[4][] - 8 each - edge the following 4 entries from one edge
> short int - 2 - vertex index of this edge, referes to the vertex list
> short int - 2 - Reference into an edge. Defines the target vertex (the local edge vertex of the referenced edge) to which the edge should be dran from the local vertex
> 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))
{
fsMesh.seekg((*it)->position);
std::uint32_t tempIndex;
fsMesh.read(reinterpret_cast<char*>(&tempIndex), sizeof(tempIndex));
if (vTextures.size() <= tempIndex)
{
std::cout << "warning texture index <" << tempIndex << "> unknown" << std::endl;
dataDestination->texture = "";
continue;
}
dataDestination->texture = vTextures[tempIndex];
fsMesh.read(reinterpret_cast<char*>(&tempData->textureIndex), sizeof(tempData->textureIndex));
continue;
}
if (!strcmp("POSL", (*it)->name))
{
readVertex(dataDestination, (*it)->position);
readVertex(tempData, (*it)->position);
continue;
}
@@ -420,44 +397,83 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list<ChunkHeader*>&
if (!strcmp("UV0L", (*it)->name))
{
readUV(dataDestination, (*it)->position);
readUV(tempData, (*it)->position);
continue;
}
if (!strcmp("STRP", (*it)->name))
{
// don't get null, bone, shadowMesh and hidden mesh indices
if (dataDestination->type == null ||
dataDestination->type == bone ||
dataDestination->type == shadowMesh ||
dataDestination->renderFlags == 1)
continue;
// jump to the data section and read the size;
std::uint32_t tempSize;
fsMesh.seekg((*it)->position);
fsMesh.read(reinterpret_cast<char*>(&tempSize), sizeof(tempSize));
fsMesh.seekg((*it)->position);
fsMesh.read(reinterpret_cast<char*>(&dataDestination->meshSize), sizeof(dataDestination->meshSize));
dataDestination->mesh = new std::uint32_t[dataDestination->meshSize];
for (unsigned int i = 0; i < dataDestination->meshSize; i += 3)
int highBitCount(0);
std::vector<uint32_t>* tempPoly = new std::vector<uint32_t>;
for (unsigned int i = 0; i < tempSize; i++)
{
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));
// ReadData
std::uint16_t tempValue;
fsMesh.read(reinterpret_cast<char*>(&tempValue), sizeof(std::uint16_t));
if (!(tempValue[0] >> 15 && tempValue[1] >> 15 && !(tempValue[2] >> 15)))
throw std::invalid_argument("invalid file. go and triangulate!");
// Check for highbit
if (tempValue >> 15)
{
highBitCount++;
tempValue = (std::uint16_t(tempValue << 1) >> 1);
}
tempValue[0] = (std::uint16_t(tempValue[0] << 1) >> 1);
tempValue[1] = (std::uint16_t(tempValue[1] << 1) >> 1);
tempPoly->push_back((std::uint32_t)tempValue);
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];
}
// new Polygon found
if (highBitCount == 2)
{
// reset highBitCount
highBitCount = 0;
// remove the last two values..
std::uint32_t saveData[2];
for (int i = 0; i < 2; i++)
{
saveData[i] = tempPoly->back();
tempPoly->pop_back();
}
// ..and save them in the new vector
std::vector<uint32_t>* newPointer = new std::vector<uint32_t>;
for (int i = 1; i >= 0; i--)
newPointer->push_back(saveData[i]);
// save the old vector and set the pointer to the new one
tempData->meshIndices.push_back(tempPoly);
tempPoly = newPointer;
} // if high bit set
} // for all values
// save the last values (where no 2 high bits follow);
tempData->meshIndices.push_back(tempPoly);
// kick the first element, it's empty as a reason of the algo above;
tempData->meshIndices.erase(tempData->meshIndices.begin());
continue;
}
}
dataDestination->segmLst.push_back(tempData);
}
void Object::analyseClthChunks(Modl * dataDestination, std::list<ChunkHeader*>& chunkList)
{
Segment* tempData = new Segment;
for (std::list<ChunkHeader*>::iterator it = chunkList.begin(); it != chunkList.end(); it++)
{
if (!strcmp("CTEX", (*it)->name))
@@ -466,42 +482,71 @@ void Object::analyseClthChunks(Modl * dataDestination, std::list<ChunkHeader*>&
char* buffer = new char[(*it)->size];
*buffer = { 0 };
fsMesh.read(buffer, (*it)->size);
dataDestination->texture = buffer;
bool tempFound(false);
for (unsigned int index = 0; index < vTextures.size(); index++)
{
if (!strcmp(buffer, vTextures[index].c_str()))
{
tempData->textureIndex = index;
tempFound = true;
break;
}
}
if (!tempFound)
{
vTextures.push_back(std::string(buffer));
tempData->textureIndex = vTextures.size() - 1;
}
delete buffer;
continue;
}
if (!strcmp("CPOS", (*it)->name))
{
readVertex(dataDestination, (*it)->position);
readVertex(tempData, (*it)->position);
continue;
}
if (!strcmp("CUV0", (*it)->name))
{
readUV(dataDestination, (*it)->position);
readUV(tempData, (*it)->position);
continue;
}
if (!strcmp("CMSH", (*it)->name))
{
// jump to the data section and read the size;
std::uint32_t tempSize;
fsMesh.seekg((*it)->position);
fsMesh.read(reinterpret_cast<char*>(&dataDestination->meshSize), sizeof(dataDestination->meshSize));
fsMesh.read(reinterpret_cast<char*>(&tempSize), sizeof(tempSize));
dataDestination->mesh = new std::uint32_t[dataDestination->meshSize * 3];
std::vector<uint32_t>* tempPoly;
for (unsigned int i = 0; i < dataDestination->meshSize; i += 3)
// for every triangle..
for (unsigned int i = 0; i < tempSize; 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));
tempPoly = new std::vector<uint32_t>;
// ..get the 3 indices and save them
for (int j = 0; j < 3; j++)
{
std::uint32_t tempValue;
fsMesh.read(reinterpret_cast<char*>(&tempValue), sizeof(std::uint32_t));
tempPoly->push_back(tempValue);
}
tempData->meshIndices.push_back(tempPoly);
}
continue;
}
}
dataDestination->segmLst.push_back(tempData);
}
void Object::readVertex(Modl* dataDestination, std::streampos position)
void Object::readVertex(Segment* dataDestination, std::streampos position)
{
std::uint32_t tempSize;
fsMesh.seekg(position);
@@ -513,7 +558,7 @@ void Object::readVertex(Modl* dataDestination, std::streampos position)
fsMesh.read(reinterpret_cast<char*>(&dataDestination->vertex[i]), sizeof(float));
}
void Object::readUV(Modl* dataDestination, std::streampos position)
void Object::readUV(Segment* dataDestination, std::streampos position)
{
std::uint32_t tempSize;
fsMesh.seekg(position);
@@ -534,6 +579,11 @@ std::vector<Modl*> Object::getModels() const
return vModls;
}
std::vector<std::string> Object::getTextureList() const
{
return vTextures;
}
/////////////////////////////////////////////////////////////////////////
// public functions

View File

@@ -114,9 +114,25 @@ void OpenGLController::deleteVectors()
Modl* cursor = vModels.back();
vModels.pop_back();
delete cursor->uv;
delete cursor->mesh;
delete cursor->vertex;
while (!cursor->segmLst.empty())
{
Segment* segmCuror = cursor->segmLst.back();
cursor->segmLst.pop_back();
delete segmCuror->uv;
delete segmCuror->vertex;
while (!segmCuror->meshIndices.empty())
{
std::vector<std::uint32_t>* meshCursor = segmCuror->meshIndices.back();
meshCursor->clear();
segmCuror->meshIndices.pop_back();
delete meshCursor;
}
delete segmCuror;
}
delete cursor;
}
@@ -269,6 +285,16 @@ void OpenGLController::addTransZ(double value)
dTranslationZ += value;
}
void OpenGLController::resetView()
{
dTranslationX = 0;
dTranslationY = 0;
dTranslationZ = 5;
fRotationX = 0;
fRotationY = 0;
fRotationZ = 0;
}
void OpenGLController::updateScene()
{
// get new matrices
@@ -287,26 +313,46 @@ void OpenGLController::updateScene()
for (unsigned int modelIndex = 0; modelIndex < vModels.size(); modelIndex++)
{
// give texture to the shader
glTexImage2D(GL_TEXTURE_2D,
0,
vTextures[modelIndex]->alpha ? GL_RGBA : GL_RGB,
vTextures[modelIndex]->width,
vTextures[modelIndex]->height,
0,
vTextures[modelIndex]->alpha ? GL_BGRA : GL_BGR,
GL_UNSIGNED_BYTE,
vTextures[modelIndex]->data->data()
);
// skip null, bones, shadowMesh, hidden things (don't increase textrue index!!)
if (vModels[modelIndex]->type == null ||
vModels[modelIndex]->type == bone ||
vModels[modelIndex]->type == shadowMesh ||
vModels[modelIndex]->renderFlags == 1)
continue;
glGenerateMipmap(GL_TEXTURE_2D);
for (auto& segIt : vModels[modelIndex]->segmLst)
{
// give texture to the shader
std::uint32_t tempTexIndex = segIt->textureIndex >= vTextures.size() ? vTextures.size() - 1 : segIt->textureIndex;
glTexImage2D(
GL_TEXTURE_2D,
0,
vTextures[tempTexIndex]->alpha ? GL_RGBA : GL_RGB,
vTextures[tempTexIndex]->width,
vTextures[tempTexIndex]->height,
0,
vTextures[tempTexIndex]->alpha ? GL_BGRA : GL_BGR,
GL_UNSIGNED_BYTE,
vTextures[tempTexIndex]->data->data()
);
// give the MVP to the shader
glUniformMatrix4fv(gluiMatrixID, 1, GL_FALSE, &getMVPMatrix(modelIndex)[0][0]);
glGenerateMipmap(GL_TEXTURE_2D);
glDrawArrays(GL_TRIANGLES, instanceOffset, vModels[modelIndex]->meshSize);
// give the MVP to the shader
glUniformMatrix4fv(gluiMatrixID, 1, GL_FALSE, &getMVPMatrix(modelIndex)[0][0]);
instanceOffset += vModels[modelIndex]->meshSize;
// calculate the number of vertex
unsigned int vertexCount(0);
for (auto& it : segIt->meshIndices)
vertexCount += (it->size() - 2) * 3;
glDrawArrays(GL_TRIANGLES, instanceOffset, vertexCount);
// increase the offset
instanceOffset += vertexCount;
}
}
glfwSwapBuffers(pWindow);
@@ -318,42 +364,65 @@ void OpenGLController::loadMsh(const char * path)
// clean up old stuff first
deleteVectors();
std::vector<std::string> tempTexList;
// get all models
try
{
Object obj(path);
vModels = obj.getModels();
tempTexList = obj.getTextureList();
}
catch (std::invalid_argument e)
{
MessageBox(NULL, e.what(), "MeshViewer 2.0 Error", MB_OK | MB_ICONERROR);
exit(1);
return; exit(1);
}
// collect vertex data of all models
std::vector<Vertex> tempBufferData;
for (auto& it : vModels)
for (auto& modIt : vModels) // for every model chunk
{
for (unsigned int i = 0; i < it->meshSize; i++)
for (auto& segIt : modIt->segmLst) // for every cluster
{
Vertex tempVertex;
tempVertex.position[0] = (GLfloat)it->vertex[it->mesh[i] * 3];
tempVertex.position[1] = (GLfloat)it->vertex[it->mesh[i] * 3 + 1];
tempVertex.position[2] = (GLfloat)it->vertex[it->mesh[i] * 3 + 2];
if (it->uv == NULL)
for (auto& mshIt : segIt->meshIndices) // for every polygon
{
tempVertex.uv[0] = 1.0;
tempVertex.uv[1] = 1.0;
}
else
{
tempVertex.uv[0] = (GLfloat)it->uv[it->mesh[i] * 2];
tempVertex.uv[1] = (GLfloat)it->uv[it->mesh[i] * 2 + 1];
}
if (mshIt->size() >= 3) // multipoly
{
// for every triangle of the multi polygon
for (unsigned int tri = 0; tri < mshIt->size() - 2; tri++)
{
// for every edge of the triangle
for (int triEdge = 0; triEdge < 3; triEdge++)
{
Vertex tempVertex;
// every edge has 3 coordinates
for (int j = 0; j < 3; j++)
tempVertex.position[j] = (GLfloat)segIt->vertex[(*mshIt)[triEdge > 0 ? tri + triEdge : 0] * 3 + j];
tempBufferData.push_back(tempVertex);
// and 2 UV
if (segIt->uv == NULL)
{
tempVertex.uv[0] = 1.0;
tempVertex.uv[1] = 1.0;
}
else
{
for (int j = 0; j < 2; j++)
tempVertex.uv[j] = (GLfloat)segIt->uv[(*mshIt)[triEdge > 0 ? tri + triEdge : 0] * 2 + j];
}
tempBufferData.push_back(tempVertex);
}
}
}
else // this shouldn't happen (polygon with less then 3 vertex)
{
deleteVectors();
MessageBox(NULL, "You have polygons with less then 3 vertices!", "MeshViewer 2.0 Error", MB_OK | MB_ICONERROR);
return;
}
}
}
}
@@ -368,21 +437,20 @@ void OpenGLController::loadMsh(const char * path)
glBindBuffer(GL_ARRAY_BUFFER, 0);
// get textures
for (auto& it : vModels)
// get textures path
std::string tempPath = path;
while (tempPath.back() != '/' && tempPath.back() != '\\')
tempPath.pop_back();
// load all textures;
for (auto& texIt : tempTexList)
{
textureData* tempData = new textureData;
try
{
if (it->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 + it->texture).c_str());
TextureTGA tempTex(std::string(tempPath + texIt).c_str());
tempData->alpha = tempTex.hasAlpha();
tempData->width = tempTex.getWidth();
@@ -400,4 +468,15 @@ void OpenGLController::loadMsh(const char * path)
vTextures.push_back(tempData);
}
// add a solid default color at the end (maybe there is an invalid index later)
textureData* tempData = new textureData;
GLubyte solidColor[4] = { 0, 0, 255, 255 };
tempData->alpha = true;
tempData->width = 1;
tempData->height = 1;
tempData->data = new std::vector<std::uint8_t>({ 0, 0, 255, 255 });
vTextures.push_back(tempData);
tempTexList.clear();
}

View File

@@ -56,7 +56,7 @@ TextureTGA::TextureTGA(const char * filePath)
{
std::uint8_t tempChunkHeader;
std::uint8_t tempData[5];
int tempByteIndex = 0;
unsigned int tempByteIndex = 0;
std::size_t tempPixelIndex = 0;
do {

View File

@@ -78,6 +78,8 @@ void mouseWheel(GLFWwindow *window, double xoffset, double yoffset)
void keyPress(GLFWwindow *window, int key, int scancode, int action, int mods)
{
OpenGLController* controller = reinterpret_cast<OpenGLController*>(glfwGetWindowUserPointer(window));
if (action == GLFW_PRESS || action == GLFW_REPEAT)
{
switch (key)
@@ -93,6 +95,9 @@ void keyPress(GLFWwindow *window, int key, int scancode, int action, int mods)
case GLFW_KEY_PLUS_GER: case GLFW_KEY_KP_ADD:
mouse.speed += 0.1;
break;
case GLFW_KEY_SPACE:
controller->resetView();
break;
default:
break;
}
@@ -102,6 +107,10 @@ void keyPress(GLFWwindow *window, int key, int scancode, int action, int mods)
void dragNdrop(GLFWwindow* window, int count, const char** paths)
{
OpenGLController* controller = reinterpret_cast<OpenGLController*>(glfwGetWindowUserPointer(window));
if(count > 0)
controller->loadMsh(paths[0]);
if (count < 1)
return;
controller->resetView();
controller->loadMsh(paths[0]);
}

View File

@@ -17,7 +17,7 @@ int main(int argc, char** argv)
else
scene = OpenGLController::getInstance();
scene->loadMsh("..\\Release\\Msh\\structured.msh");
scene->loadMsh("..\\Release\\Msh\\cluster.msh");
do {
scene->updateScene();

View File

@@ -13,11 +13,11 @@ Feel free to use my code the way you like. But remember i used some public libra
licence, too.
### To Do
- crashing sometimes - no idea why,
- bones are not triangulated,
- nulls are not triangulated,
- crash when loading trooper,
- display cloth testing
- cluster use wrong textures
- use normals or fix ccw and cw
- multipoly triangulation not 100% good
- cloth testing
- optional display bones, shadow, collision,...
- integrate into a software:
-> gui open file ( + drag and drop),
-> list all msh under a directory,
@@ -25,6 +25,4 @@ licence, too.
-> display colisions,
-> lights,
-> handle render flags,
- draw more then one model,
- use different textures for one or multiple models,
- take a look at destruction

BIN
Release/Msh/cluster.msh Normal file

Binary file not shown.

Binary file not shown.