9 Commits

Author SHA1 Message Date
Anakin
1918d5c844 new release version,
Features:
- performance improvement,
- code improvement,
- move the model to the center,
- scale everything to 1,
- do not open a default file for release build
Bugs:
- cloth is not displayed correctly,
- turning in the wrong x direction when model is turned 180° in y direction,
- consular class corvette is not displayed,
- rep_weap_LJ-50 is not displayed correctly
2016-12-02 13:07:49 +01:00
Anakin
3118118953 some fixes 2016-12-02 13:00:38 +01:00
Anakin
9b3d12dfeb scale everything to 1,
cloth not displayed completely,
corvette is not displayed why?
2016-12-02 12:10:52 +01:00
Anakin
a47eefe92e move the model to the center,
clean up variables,
use delete[]
2016-12-01 19:51:11 +01:00
Anakin
1c0ed61b34 adjust boundingbox (only for SINF)
use else if
2016-11-30 17:05:39 +01:00
Anakin
ef2c341a1a using c++11 loops,
reading bbox for each geom,
added some TODO
use the bbox information
2016-11-28 14:04:09 +01:00
Anakin
5ab2f2eaf9 fixed the memory garbage problem 2016-11-26 15:39:59 +01:00
Anakin
5c5b9ac2f1 don't copy the model list. It can be very big - using pointer now,
garbage is not from the texture or object changes
2016-11-25 16:14:33 +01:00
Anakin
e1e8e165fe removed unused information from texture,
don't copy the data, give a pointer,
there is some data left, need to find out if it's from these changes
2016-11-24 19:40:17 +01:00
8 changed files with 240 additions and 214 deletions

View File

@@ -14,6 +14,12 @@ enum Mtyp {
shadowMesh = 6 shadowMesh = 6
}; };
struct Bbox {
float quaternion[4];
float center[3];
float extents[3];
};
struct ChunkHeader { struct ChunkHeader {
char name[5]; char name[5];
std::uint32_t size; std::uint32_t size;
@@ -24,7 +30,7 @@ struct Segment {
std::uint32_t textureIndex = 0; std::uint32_t textureIndex = 0;
float* vertex = nullptr; float* vertex = nullptr;
float* uv = nullptr; float* uv = nullptr;
std::vector<std::vector<std::uint32_t>*> meshIndices; // indices into vertex array std::vector<std::vector<std::uint32_t>> meshIndices; // indices into vertex array
}; };
struct Modl { struct Modl {
@@ -45,10 +51,10 @@ public:
private: private:
std::vector<Modl*> vModls; std::vector<Modl*>* vModls;
std::fstream fsMesh; std::fstream fsMesh;
std::vector<std::string> vTextures; std::vector<std::string> vTextures;
Bbox boundingBox;
private: private:
void loadChunks(std::list<ChunkHeader*> &destination, std::streampos start, const std::uint32_t end); void loadChunks(std::list<ChunkHeader*> &destination, std::streampos start, const std::uint32_t end);
@@ -63,7 +69,7 @@ private:
public: public:
std::vector<Modl*> getModels() const; std::vector<Modl*>* getModels() const;
std::vector<std::string> getTextureList() const; std::vector<std::string> getTextureList() const;
Bbox getBoundgBox() const;
}; };

View File

@@ -70,8 +70,9 @@ private:
// ======================================== // ========================================
// data // data
std::vector<Modl*> vModels; std::vector<Modl*>* vModels = NULL;
std::vector<textureData*> vTextures; std::vector<textureData*> vTextures;
Bbox sceneBoundingBox;
// transformation ========================= // transformation =========================
//values //values
@@ -80,7 +81,7 @@ private:
float fRotationZ = 0; float fRotationZ = 0;
double dTranslationX = 0; double dTranslationX = 0;
double dTranslationY = 0; double dTranslationY = 0;
double dTranslationZ = 5; double dTranslationZ = 3;
// ======================================== // ========================================

View File

@@ -9,22 +9,13 @@ public:
~TextureTGA(); ~TextureTGA();
private: private:
std::vector<std::uint8_t> vui8Pixels; std::vector<std::uint8_t>* vui8Pixels;
bool bCompressed; std::uint32_t ui32BpP;
std::uint32_t ui32IDLength;
bool bColorTabel;
std::uint32_t ui32PicType;
std::uint32_t ui32PaletteBegin;
std::uint32_t ui32PaletteLength;
std::uint32_t ui32PaletteBpP;
std::uint32_t ui32Width; std::uint32_t ui32Width;
std::uint32_t ui32Height; std::uint32_t ui32Height;
std::uint32_t ui32Size;
std::uint32_t ui32BpP;
std::uint32_t ui32Attribut;
public: public:
std::vector<std::uint8_t> getData() const; std::vector<std::uint8_t>* getData() const;
bool hasAlpha() const; bool hasAlpha() const;
std::uint32_t getWidth() const; std::uint32_t getWidth() const;
std::uint32_t getHeight() const; std::uint32_t getHeight() const;

View File

@@ -10,6 +10,8 @@
Object::Object(const char* path) Object::Object(const char* path)
{ {
vModls = new std::vector<Modl*>;
// open file // open file
fsMesh.open(path, std::ios::in | std::ios::binary); fsMesh.open(path, std::ios::in | std::ios::binary);
@@ -65,9 +67,6 @@ Object::~Object()
{ {
// clear texture list // clear texture list
vTextures.clear(); vTextures.clear();
// clear Model list (don't delete the elements)
vModls.clear();
} }
@@ -108,28 +107,57 @@ void Object::loadChunks(std::list<ChunkHeader*>& destination, std::streampos sta
void Object::analyseMsh2Chunks(std::list<ChunkHeader*>& chunkList) void Object::analyseMsh2Chunks(std::list<ChunkHeader*>& chunkList)
{ {
for (std::list<ChunkHeader*>::iterator it = chunkList.begin(); it != chunkList.end(); it++) for (auto& it : chunkList)
{ {
if (!strcmp("MATL", (*it)->name)) if (!strcmp("SINF", it->name))
{
std::list<ChunkHeader*> tempSinfChunks;
loadChunks(tempSinfChunks, it->position, it->size);
// evaluate MATL subchunks
for (auto& it : tempSinfChunks)
{
if (!strcmp("BBOX", it->name))
{
fsMesh.seekg(it->position);
// read in the quaternion
for (int i = 0; i < 4; i++)
fsMesh.read(reinterpret_cast<char*>(&boundingBox.quaternion[i]), sizeof(float));
//read in the center
for (int i = 0; i < 3; i++)
fsMesh.read(reinterpret_cast<char*>(&boundingBox.center[i]), sizeof(float));
//read in the extents
for (int i = 0; i < 3; i++)
fsMesh.read(reinterpret_cast<char*>(&boundingBox.extents[i]), sizeof(float));
}
}
for (ChunkHeader* it : tempSinfChunks)
delete it;
}
else if (!strcmp("MATL", it->name))
{ {
// "useless" information how many MATD follow // "useless" information how many MATD follow
fsMesh.seekg((*it)->position); fsMesh.seekg(it->position);
std::uint32_t tempMatdCount; fsMesh.seekg(sizeof(std::uint32_t), std::ios_base::cur);
fsMesh.read(reinterpret_cast<char*>(&tempMatdCount), sizeof(std::uint32_t));
// get all MATD from MATL list // get all MATD from MATL list
std::list<ChunkHeader*> tempMatlChunks; std::list<ChunkHeader*> tempMatlChunks;
loadChunks(tempMatlChunks, fsMesh.tellg(), (*it)->size - 4); loadChunks(tempMatlChunks, fsMesh.tellg(), it->size - 4);
// evaluate MATL subchunks // evaluate MATL subchunks
for (std::list<ChunkHeader*>::iterator it = tempMatlChunks.begin(); it != tempMatlChunks.end(); it++) for (auto& it : tempMatlChunks)
{ {
// This shouldn't be anything else than MATD // This shouldn't be anything else than MATD
if (!strcmp("MATD", (*it)->name)) if (!strcmp("MATD", it->name))
{ {
// get all subchunks from MATD // get all subchunks from MATD
std::list<ChunkHeader*> tempMatdChunks; std::list<ChunkHeader*> tempMatdChunks;
loadChunks(tempMatdChunks, (*it)->position, (*it)->size); loadChunks(tempMatdChunks, it->position, it->size);
vTextures.push_back(""); vTextures.push_back("");
@@ -153,17 +181,15 @@ void Object::analyseMsh2Chunks(std::list<ChunkHeader*>& chunkList)
tempMatlChunks.pop_front(); tempMatlChunks.pop_front();
delete tempCursor; delete tempCursor;
} }
continue;
} }
if (!strcmp("MODL", (*it)->name)) else if (!strcmp("MODL", it->name))
{ {
Modl* tempModl = new Modl; Modl* tempModl = new Modl;
// get all subchunks // get all subchunks
std::list<ChunkHeader*> tempChunks; std::list<ChunkHeader*> tempChunks;
loadChunks(tempChunks, (*it)->position, (*it)->size); loadChunks(tempChunks, it->position, it->size);
// evaluate MODL subchunks // evaluate MODL subchunks
analyseModlChunks(tempModl, tempChunks); analyseModlChunks(tempModl, tempChunks);
@@ -177,81 +203,75 @@ void Object::analyseMsh2Chunks(std::list<ChunkHeader*>& chunkList)
} }
// save Model data // save Model data
vModls.push_back(tempModl); vModls->push_back(tempModl);
continue;
} }
} }
} }
void Object::analyseMatdChunks(std::list<ChunkHeader*>& chunkList) void Object::analyseMatdChunks(std::list<ChunkHeader*>& chunkList)
{ {
for (std::list<ChunkHeader*>::iterator it = chunkList.begin(); it != chunkList.end(); it++) for (auto& it : chunkList)
{ {
//TX1D, TX2D, TX3D //TX1D, TX2D, TX3D
if (!strcmp("TX0D", (*it)->name)) if (!strcmp("TX0D", it->name))
{ {
fsMesh.seekg((*it)->position); fsMesh.seekg(it->position);
char* buffer = new char[(*it)->size + 1]; char* buffer = new char[it->size + 1];
*buffer = { 0 }; *buffer = { 0 };
fsMesh.read(buffer, (*it)->size); fsMesh.read(buffer, it->size);
vTextures.back() = buffer; vTextures.back() = buffer;
delete buffer; delete[] buffer;
continue;
} }
} }
} }
void Object::analyseModlChunks(Modl* dataDestination, std::list<ChunkHeader*>& chunkList) void Object::analyseModlChunks(Modl* dataDestination, std::list<ChunkHeader*>& chunkList)
{ {
for (std::list<ChunkHeader*>::iterator it = chunkList.begin(); it != chunkList.end(); it++) for (auto& it : chunkList)
{ {
if (!strcmp("MTYP", (*it)->name)) if (!strcmp("MTYP", it->name))
{ {
fsMesh.seekg((*it)->position); fsMesh.seekg(it->position);
std::uint32_t tempType; std::uint32_t tempType;
fsMesh.read(reinterpret_cast<char*>(&tempType), sizeof(tempType)); fsMesh.read(reinterpret_cast<char*>(&tempType), sizeof(tempType));
dataDestination->type = (Mtyp)tempType; dataDestination->type = (Mtyp)tempType;
continue;
} }
if (!strcmp("PRNT", (*it)->name)) else if (!strcmp("PRNT", it->name))
{ {
fsMesh.seekg((*it)->position); fsMesh.seekg(it->position);
char* buffer = new char[(*it)->size]; char* buffer = new char[it->size];
*buffer = { 0 }; *buffer = { 0 };
fsMesh.read(buffer, (*it)->size); fsMesh.read(buffer, it->size);
dataDestination->parent = buffer; dataDestination->parent = buffer;
delete buffer; delete[] buffer;
continue;
} }
if (!strcmp("NAME", (*it)->name)) else if (!strcmp("NAME", it->name))
{ {
fsMesh.seekg((*it)->position); fsMesh.seekg(it->position);
char* buffer = new char[(*it)->size]; char* buffer = new char[it->size];
*buffer = { 0 }; *buffer = { 0 };
fsMesh.read(buffer, (*it)->size); fsMesh.read(buffer, it->size);
dataDestination->name = buffer; dataDestination->name = buffer;
delete buffer; delete[] buffer;
continue;
} }
if (!strcmp("FLGS", (*it)->name)) else if (!strcmp("FLGS", it->name))
{ {
fsMesh.seekg((*it)->position); fsMesh.seekg(it->position);
fsMesh.read(reinterpret_cast<char*>(&dataDestination->renderFlags), sizeof(dataDestination->renderFlags)); fsMesh.read(reinterpret_cast<char*>(&dataDestination->renderFlags), sizeof(dataDestination->renderFlags));
continue;
} }
if (!strcmp("TRAN", (*it)->name)) else if (!strcmp("TRAN", it->name))
{ {
float tempScale[3]; float tempScale[3];
float tempRotation[4]; float tempRotation[4];
float tempTrans[3]; float tempTrans[3];
fsMesh.seekg((*it)->position); fsMesh.seekg(it->position);
//TODO: use loop
fsMesh.read(reinterpret_cast<char*>(&tempScale[0]), sizeof(float)); fsMesh.read(reinterpret_cast<char*>(&tempScale[0]), sizeof(float));
fsMesh.read(reinterpret_cast<char*>(&tempScale[1]), sizeof(float)); fsMesh.read(reinterpret_cast<char*>(&tempScale[1]), sizeof(float));
fsMesh.read(reinterpret_cast<char*>(&tempScale[2]), sizeof(float)); fsMesh.read(reinterpret_cast<char*>(&tempScale[2]), sizeof(float));
@@ -261,6 +281,7 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list<ChunkHeader*>& c
fsMesh.read(reinterpret_cast<char*>(&tempRotation[2]), sizeof(float)); fsMesh.read(reinterpret_cast<char*>(&tempRotation[2]), sizeof(float));
fsMesh.read(reinterpret_cast<char*>(&tempRotation[3]), sizeof(float)); fsMesh.read(reinterpret_cast<char*>(&tempRotation[3]), sizeof(float));
//TODO: make a function for this
//calculate x,y,z rotation //calculate x,y,z rotation
tempRotation[0] = atan2(2 * (tempRotation[0] * tempRotation[1] + tempRotation[2] * tempRotation[3]), tempRotation[0] = atan2(2 * (tempRotation[0] * tempRotation[1] + tempRotation[2] * tempRotation[3]),
1 - 2 * (pow(tempRotation[1], 2) + pow(tempRotation[2], 2))); 1 - 2 * (pow(tempRotation[1], 2) + pow(tempRotation[2], 2)));
@@ -300,14 +321,13 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list<ChunkHeader*>& c
glm::vec3(0, 0, 1) glm::vec3(0, 0, 1)
); );
continue;
} }
if (!strcmp("GEOM", (*it)->name)) else if (!strcmp("GEOM", it->name))
{ {
// get all subchunks // get all subchunks
std::list<ChunkHeader*> tempGeomChunks; std::list<ChunkHeader*> tempGeomChunks;
loadChunks(tempGeomChunks, (*it)->position, (*it)->size); loadChunks(tempGeomChunks, it->position, it->size);
// evaluate GEOM subchunks // evaluate GEOM subchunks
analyseGeomChunks(dataDestination, tempGeomChunks); analyseGeomChunks(dataDestination, tempGeomChunks);
@@ -319,21 +339,19 @@ void Object::analyseModlChunks(Modl* dataDestination, std::list<ChunkHeader*>& c
tempGeomChunks.pop_front(); tempGeomChunks.pop_front();
delete tempCursor; delete tempCursor;
} }
continue;
} }
} }
} }
void Object::analyseGeomChunks(Modl * dataDestination, std::list<ChunkHeader*>& chunkList) void Object::analyseGeomChunks(Modl * dataDestination, std::list<ChunkHeader*>& chunkList)
{ {
for (std::list<ChunkHeader*>::iterator it = chunkList.begin(); it != chunkList.end(); it++) for (auto& it : chunkList)
{ {
if (!strcmp("SEGM", (*it)->name)) if (!strcmp("SEGM", it->name))
{ {
// get all subchunks // get all subchunks
std::list<ChunkHeader*> tempSegmChunks; std::list<ChunkHeader*> tempSegmChunks;
loadChunks(tempSegmChunks, (*it)->position, (*it)->size); loadChunks(tempSegmChunks, it->position, it->size);
// evaluate SEGM subchunks // evaluate SEGM subchunks
analyseSegmChunks(dataDestination, tempSegmChunks); analyseSegmChunks(dataDestination, tempSegmChunks);
@@ -348,11 +366,11 @@ void Object::analyseGeomChunks(Modl * dataDestination, std::list<ChunkHeader*>&
continue; continue;
} }
if (!strcmp("CLTH", (*it)->name)) if (!strcmp("CLTH", it->name))
{ {
// get all subchunks // get all subchunks
std::list<ChunkHeader*> tempClthChunks; std::list<ChunkHeader*> tempClthChunks;
loadChunks(tempClthChunks, (*it)->position, (*it)->size); loadChunks(tempClthChunks, it->position, it->size);
// evaluate CLTH subchunks // evaluate CLTH subchunks
analyseClthChunks(dataDestination, tempClthChunks); analyseClthChunks(dataDestination, tempClthChunks);
@@ -373,39 +391,35 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list<ChunkHeader*>&
{ {
Segment* tempData = new Segment; Segment* tempData = new Segment;
for (std::list<ChunkHeader*>::iterator it = chunkList.begin(); it != chunkList.end(); it++) for (auto& it : chunkList)
{ {
if (!strcmp("MATI", (*it)->name)) if (!strcmp("MATI", it->name))
{ {
fsMesh.seekg((*it)->position); fsMesh.seekg(it->position);
fsMesh.read(reinterpret_cast<char*>(&tempData->textureIndex), sizeof(tempData->textureIndex)); fsMesh.read(reinterpret_cast<char*>(&tempData->textureIndex), sizeof(tempData->textureIndex));
continue;
} }
if (!strcmp("POSL", (*it)->name)) else if (!strcmp("POSL", it->name))
{ {
readVertex(tempData, (*it)->position); readVertex(tempData, it->position);
continue;
} }
/*if (!strcmp("NRML", (*it)->name)) /*else if (!strcmp("NRML", it->name))
{ {
fsMesh.seekg((*it)->position); fsMesh.seekg(it->position);
std::uint32_t tempSize; std::uint32_t tempSize;
fsMesh.read(reinterpret_cast<char*>(&tempSize), sizeof(tempSize)); fsMesh.read(reinterpret_cast<char*>(&tempSize), sizeof(tempSize));
// List of normals // List of normals
// long int - 4 - number of normal vectores stored in this list // long int - 4 - number of normal vectores stored in this list
// float[3][] - 12 each - UVW vector for each vertex // float[3][] - 12 each - UVW vector for each vertex
continue;
}*/ }*/
if (!strcmp("UV0L", (*it)->name)) else if (!strcmp("UV0L", it->name))
{ {
readUV(tempData, (*it)->position); readUV(tempData, it->position);
continue;
} }
if (!strcmp("STRP", (*it)->name)) else if (!strcmp("STRP", it->name))
{ {
// don't get null, bone, shadowMesh and hidden mesh indices // don't get null, bone, shadowMesh and hidden mesh indices
if (dataDestination->type == null || if (dataDestination->type == null ||
@@ -416,11 +430,11 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list<ChunkHeader*>&
// jump to the data section and read the size; // jump to the data section and read the size;
std::uint32_t tempSize; std::uint32_t tempSize;
fsMesh.seekg((*it)->position); fsMesh.seekg(it->position);
fsMesh.read(reinterpret_cast<char*>(&tempSize), sizeof(tempSize)); fsMesh.read(reinterpret_cast<char*>(&tempSize), sizeof(tempSize));
int highBitCount(0); int highBitCount(0);
std::vector<uint32_t>* tempPoly = new std::vector<uint32_t>; std::vector<uint32_t> tempPoly; // = new std::vector<uint32_t>;
for (unsigned int i = 0; i < tempSize; i++) for (unsigned int i = 0; i < tempSize; i++)
{ {
@@ -435,7 +449,7 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list<ChunkHeader*>&
tempValue = (std::uint16_t(tempValue << 1) >> 1); tempValue = (std::uint16_t(tempValue << 1) >> 1);
} }
tempPoly->push_back((std::uint32_t)tempValue); tempPoly.push_back((std::uint32_t)tempValue);
// new Polygon found // new Polygon found
if (highBitCount == 2) if (highBitCount == 2)
@@ -447,18 +461,17 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list<ChunkHeader*>&
std::uint32_t saveData[2]; std::uint32_t saveData[2];
for (int i = 0; i < 2; i++) for (int i = 0; i < 2; i++)
{ {
saveData[i] = tempPoly->back(); saveData[i] = tempPoly.back();
tempPoly->pop_back(); tempPoly.pop_back();
} }
// ..and save them in the new vector // ..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); tempData->meshIndices.push_back(tempPoly);
tempPoly = newPointer;
tempPoly.clear();
for (int i = 1; i >= 0; i--)
tempPoly.push_back(saveData[i]);
} // if high bit set } // if high bit set
} // for all values } // for all values
@@ -468,9 +481,9 @@ void Object::analyseSegmChunks(Modl * dataDestination, std::list<ChunkHeader*>&
// kick the first element, it's empty as a reason of the algo above; // kick the first element, it's empty as a reason of the algo above;
tempData->meshIndices.erase(tempData->meshIndices.begin()); tempData->meshIndices.erase(tempData->meshIndices.begin());
continue;
} }
} }
dataDestination->segmLst.push_back(tempData); dataDestination->segmLst.push_back(tempData);
} }
@@ -478,14 +491,14 @@ void Object::analyseClthChunks(Modl * dataDestination, std::list<ChunkHeader*>&
{ {
Segment* tempData = new Segment; Segment* tempData = new Segment;
for (std::list<ChunkHeader*>::iterator it = chunkList.begin(); it != chunkList.end(); it++) for (auto& it : chunkList)
{ {
if (!strcmp("CTEX", (*it)->name)) if (!strcmp("CTEX", it->name))
{ {
fsMesh.seekg((*it)->position); fsMesh.seekg(it->position);
char* buffer = new char[(*it)->size]; char* buffer = new char[it->size];
*buffer = { 0 }; *buffer = { 0 };
fsMesh.read(buffer, (*it)->size); fsMesh.read(buffer, it->size);
bool tempFound(false); bool tempFound(false);
@@ -505,46 +518,42 @@ void Object::analyseClthChunks(Modl * dataDestination, std::list<ChunkHeader*>&
tempData->textureIndex = vTextures.size() - 1; tempData->textureIndex = vTextures.size() - 1;
} }
delete buffer; delete[] buffer;
continue;
} }
if (!strcmp("CPOS", (*it)->name)) else if (!strcmp("CPOS", it->name))
{ {
readVertex(tempData, (*it)->position); readVertex(tempData, it->position);
continue;
} }
if (!strcmp("CUV0", (*it)->name)) else if (!strcmp("CUV0", it->name))
{ {
readUV(tempData, (*it)->position); readUV(tempData, it->position);
continue;
} }
if (!strcmp("CMSH", (*it)->name)) else if (!strcmp("CMSH", it->name))
{ {
// jump to the data section and read the size; // jump to the data section and read the size;
std::uint32_t tempSize; std::uint32_t tempSize;
fsMesh.seekg((*it)->position); fsMesh.seekg(it->position);
fsMesh.read(reinterpret_cast<char*>(&tempSize), sizeof(tempSize)); fsMesh.read(reinterpret_cast<char*>(&tempSize), sizeof(tempSize));
std::vector<uint32_t>* tempPoly; std::vector<uint32_t> tempPoly;
// for every triangle.. // for every triangle..
for (unsigned int i = 0; i < tempSize; i += 3) for (unsigned int i = 0; i < tempSize; i += 3)
{ {
tempPoly = new std::vector<uint32_t>; tempPoly.clear();
// ..get the 3 indices and save them // ..get the 3 indices and save them
for (int j = 0; j < 3; j++) for (int j = 0; j < 3; j++)
{ {
std::uint32_t tempValue; std::uint32_t tempValue;
fsMesh.read(reinterpret_cast<char*>(&tempValue), sizeof(std::uint32_t)); fsMesh.read(reinterpret_cast<char*>(&tempValue), sizeof(std::uint32_t));
tempPoly->push_back(tempValue); tempPoly.push_back(tempValue);
} }
tempData->meshIndices.push_back(tempPoly); tempData->meshIndices.push_back(tempPoly);
} }
continue;
} }
} }
dataDestination->segmLst.push_back(tempData); dataDestination->segmLst.push_back(tempData);
@@ -578,7 +587,7 @@ void Object::readUV(Segment* dataDestination, std::streampos position)
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
// public getter // public getter
std::vector<Modl*> Object::getModels() const std::vector<Modl*>* Object::getModels() const
{ {
return vModls; return vModls;
} }
@@ -588,6 +597,11 @@ std::vector<std::string> Object::getTextureList() const
return vTextures; return vTextures;
} }
Bbox Object::getBoundgBox() const
{
return boundingBox;
}
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
// public functions // public functions

View File

@@ -109,37 +109,43 @@ void OpenGLController::processInit()
void OpenGLController::deleteVectors() void OpenGLController::deleteVectors()
{ {
while (!vModels.empty()) //TODO: does .clear() work, too??
if (vModels != NULL)
{ {
Modl* cursor = vModels.back(); while (!vModels->empty())
vModels.pop_back();
while (!cursor->segmLst.empty())
{ {
Segment* segmCuror = cursor->segmLst.back(); Modl* cursor = vModels->back();
cursor->segmLst.pop_back(); vModels->pop_back();
delete segmCuror->uv; while (!cursor->segmLst.empty())
delete segmCuror->vertex;
while (!segmCuror->meshIndices.empty())
{ {
std::vector<std::uint32_t>* meshCursor = segmCuror->meshIndices.back(); Segment* segmCuror = cursor->segmLst.back();
meshCursor->clear(); cursor->segmLst.pop_back();
segmCuror->meshIndices.pop_back();
delete meshCursor; delete[] segmCuror->uv;
delete[] segmCuror->vertex;
while (!segmCuror->meshIndices.empty())
{
while (!segmCuror->meshIndices.back().empty())
segmCuror->meshIndices.back().pop_back();
segmCuror->meshIndices.pop_back();
}
delete segmCuror;
} }
delete segmCuror; delete cursor;
} }
delete vModels;
delete cursor;
} }
while (!vTextures.empty()) while (!vTextures.empty())
{ {
textureData* cursor = vTextures.back(); textureData* cursor = vTextures.back();
vTextures.pop_back(); vTextures.pop_back();
cursor->data->clear();
delete cursor->data; delete cursor->data;
delete cursor; delete cursor;
} }
@@ -207,16 +213,16 @@ glm::mat4 OpenGLController::getModelMatrix(unsigned int index)
{ {
glm::mat4 tempParentMatrix = glm::mat4(1.0f); glm::mat4 tempParentMatrix = glm::mat4(1.0f);
for (unsigned int loop = 0; loop < vModels.size(); loop++) for (unsigned int loop = 0; loop < vModels->size(); loop++)
{ {
if (!strcmp(vModels[index]->parent.c_str(), vModels[loop]->name.c_str())) if (!strcmp(vModels->at(index)->parent.c_str(), vModels->at(loop)->name.c_str()))
{ {
tempParentMatrix = getModelMatrix(loop); tempParentMatrix = getModelMatrix(loop);
break; break;
} }
} }
return tempParentMatrix * vModels[index]->m4x4Translation; return tempParentMatrix * vModels->at(index)->m4x4Translation;
} }
glm::mat4 OpenGLController::getMVPMatrix(unsigned int index) glm::mat4 OpenGLController::getMVPMatrix(unsigned int index)
@@ -237,8 +243,19 @@ glm::mat4 OpenGLController::getMVPMatrix(unsigned int index)
m4x4ModelRot = glm::rotate(m4x4ModelRot, fRotationY, glm::vec3(0, 1, 0)); m4x4ModelRot = glm::rotate(m4x4ModelRot, fRotationY, glm::vec3(0, 1, 0));
m4x4ModelRot = glm::rotate(m4x4ModelRot, fRotationZ, glm::vec3(0, 0, 1)); m4x4ModelRot = glm::rotate(m4x4ModelRot, fRotationZ, glm::vec3(0, 0, 1));
// move to center
glm::mat4 m4x4ModelCenter = glm::translate(
glm::mat4(1.0f),
glm::vec3(-sceneBoundingBox.center[0], -sceneBoundingBox.center[1], -sceneBoundingBox.center[2])
);
//scale to 1
float maxExtent = max(max(sceneBoundingBox.extents[0], sceneBoundingBox.extents[1]), sceneBoundingBox.extents[2]);
glm::mat4 m4x4Normalize = glm::mat4(1.0f);
m4x4Normalize = glm::scale(m4x4Normalize, glm::vec3(1/maxExtent, 1 / maxExtent, 1 / maxExtent));
// Return MVP // Return MVP
return m4x4Projection * m4x4View * m4x4ModelRot * getModelMatrix(index); return m4x4Projection * m4x4View * m4x4ModelRot * m4x4Normalize * m4x4ModelCenter * getModelMatrix(index);
} }
@@ -309,52 +326,54 @@ void OpenGLController::updateScene()
// tell sampler to use texture unit 0 // tell sampler to use texture unit 0
glUniform1i(gluiSamplerID, 0); glUniform1i(gluiSamplerID, 0);
int instanceOffset(0); if (vModels != NULL)
for (unsigned int modelIndex = 0; modelIndex < vModels.size(); modelIndex++)
{ {
// skip null, bones, shadowMesh, hidden things (don't increase textrue index!!) int instanceOffset(0);
if (vModels[modelIndex]->type == null ||
vModels[modelIndex]->type == bone ||
vModels[modelIndex]->type == shadowMesh ||
vModels[modelIndex]->renderFlags == 1)
continue;
for (auto& segIt : vModels[modelIndex]->segmLst) for (unsigned int modelIndex = 0; modelIndex < vModels->size(); modelIndex++)
{ {
// give texture to the shader // skip null, bones, shadowMesh, hidden things (don't increase textrue index!!)
std::uint32_t tempTexIndex = segIt->textureIndex >= vTextures.size() ? vTextures.size() - 1 : segIt->textureIndex; if (vModels->at(modelIndex)->type == null ||
vModels->at(modelIndex)->type == bone ||
vModels->at(modelIndex)->type == shadowMesh ||
vModels->at(modelIndex)->renderFlags == 1)
continue;
glTexImage2D( for (auto& segIt : vModels->at(modelIndex)->segmLst)
GL_TEXTURE_2D, {
0, // give texture to the shader
vTextures[tempTexIndex]->alpha ? GL_RGBA : GL_RGB, std::uint32_t tempTexIndex = segIt->textureIndex >= vTextures.size() ? vTextures.size() - 1 : segIt->textureIndex;
vTextures[tempTexIndex]->width,
vTextures[tempTexIndex]->height,
0,
vTextures[tempTexIndex]->alpha ? GL_BGRA : GL_BGR,
GL_UNSIGNED_BYTE,
vTextures[tempTexIndex]->data->data()
);
glGenerateMipmap(GL_TEXTURE_2D); 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 glGenerateMipmap(GL_TEXTURE_2D);
glUniformMatrix4fv(gluiMatrixID, 1, GL_FALSE, &getMVPMatrix(modelIndex)[0][0]);
// calculate the number of vertex // give the MVP to the shader
unsigned int vertexCount(0); glUniformMatrix4fv(gluiMatrixID, 1, GL_FALSE, &getMVPMatrix(modelIndex)[0][0]);
for (auto& it : segIt->meshIndices)
vertexCount += (it->size() - 2) * 3; // calculate the number of vertex
unsigned int vertexCount(0);
for (auto& it : segIt->meshIndices)
vertexCount += (it.size() - 2) * 3;
glDrawArrays(GL_TRIANGLES, instanceOffset, vertexCount); glDrawArrays(GL_TRIANGLES, instanceOffset, vertexCount);
// increase the offset // increase the offset
instanceOffset += vertexCount; instanceOffset += vertexCount;
}
} }
} }
glfwSwapBuffers(pWindow); glfwSwapBuffers(pWindow);
glfwPollEvents(); glfwPollEvents();
} }
@@ -372,26 +391,27 @@ void OpenGLController::loadMsh(const char * path)
Object obj(path); Object obj(path);
vModels = obj.getModels(); vModels = obj.getModels();
tempTexList = obj.getTextureList(); tempTexList = obj.getTextureList();
sceneBoundingBox = obj.getBoundgBox();
} }
catch (std::invalid_argument e) catch (std::invalid_argument e)
{ {
MessageBox(NULL, e.what(), "MeshViewer 2.0 Error", MB_OK | MB_ICONERROR); MessageBox(NULL, e.what(), "MeshViewer 2.0 Error", MB_OK | MB_ICONERROR);
return; exit(1); return;
} }
// collect vertex data of all models // collect vertex data of all models
std::vector<Vertex> tempBufferData; std::vector<Vertex> tempBufferData;
for (auto& modIt : vModels) // for every model chunk for (auto& modIt : *vModels) // for every model chunk
{ {
for (auto& segIt : modIt->segmLst) // for every cluster for (auto& segIt : modIt->segmLst) // for every cluster
{ {
for (auto& mshIt : segIt->meshIndices) // for every polygon for (auto& mshIt : segIt->meshIndices) // for every polygon
{ {
if (mshIt->size() >= 3) // multipoly if (mshIt.size() >= 3) // multipoly
{ {
// for every triangle of the multi polygon // for every triangle of the multi polygon
for (unsigned int tri = 0; tri < mshIt->size() - 2; tri++) for (unsigned int tri = 0; tri < mshIt.size() - 2; tri++)
{ {
// for every edge of the triangle // for every edge of the triangle
for (int triEdge = 0; triEdge < 3; triEdge++) for (int triEdge = 0; triEdge < 3; triEdge++)
@@ -399,7 +419,7 @@ void OpenGLController::loadMsh(const char * path)
Vertex tempVertex; Vertex tempVertex;
// every edge has 3 coordinates // every edge has 3 coordinates
for (int j = 0; j < 3; j++) for (int j = 0; j < 3; j++)
tempVertex.position[j] = (GLfloat)segIt->vertex[(*mshIt)[tri + triEdge - ((tri % 2) * (triEdge - 1) * 2)] * 3 + j]; tempVertex.position[j] = (GLfloat)segIt->vertex[mshIt[tri + triEdge - ((tri % 2) * (triEdge - 1) * 2)] * 3 + j];
// and 2 UV // and 2 UV
if (segIt->uv == NULL) if (segIt->uv == NULL)
@@ -410,7 +430,7 @@ void OpenGLController::loadMsh(const char * path)
else else
{ {
for (int j = 0; j < 2; j++) for (int j = 0; j < 2; j++)
tempVertex.uv[j] = (GLfloat)segIt->uv[(*mshIt)[tri + triEdge - ((tri % 2) * (triEdge - 1) * 2)] * 2 + j]; tempVertex.uv[j] = (GLfloat)segIt->uv[mshIt[tri + triEdge - ((tri % 2) * (triEdge - 1) * 2)] * 2 + j];
} }
tempBufferData.push_back(tempVertex); tempBufferData.push_back(tempVertex);
} }
@@ -436,6 +456,8 @@ void OpenGLController::loadMsh(const char * path)
); );
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
tempBufferData.clear();
// get textures path // get textures path
std::string tempPath = path; std::string tempPath = path;
@@ -455,7 +477,7 @@ void OpenGLController::loadMsh(const char * path)
tempData->alpha = tempTex.hasAlpha(); tempData->alpha = tempTex.hasAlpha();
tempData->width = tempTex.getWidth(); tempData->width = tempTex.getWidth();
tempData->height = tempTex.getHeight(); tempData->height = tempTex.getHeight();
tempData->data = new std::vector<std::uint8_t>(tempTex.getData()); tempData->data = tempTex.getData();
} }
catch (std::invalid_argument e) catch (std::invalid_argument e)
{ {

View File

@@ -13,35 +13,26 @@ TextureTGA::TextureTGA(const char * filePath)
std::uint8_t ui8x18Header[19] = { 0 }; std::uint8_t ui8x18Header[19] = { 0 };
fsPicture.read(reinterpret_cast<char*>(&ui8x18Header), sizeof(ui8x18Header)-1); fsPicture.read(reinterpret_cast<char*>(&ui8x18Header), sizeof(ui8x18Header)-1);
//get variables
vui8Pixels = new std::vector<std::uint8_t>;
bool bCompressed;
std::uint32_t ui32IDLength;
std::uint32_t ui32PicType;
std::uint32_t ui32PaletteLength;
std::uint32_t ui32Size;
// extract all information from header // extract all information from header
ui32IDLength = ui8x18Header[0]; ui32IDLength = ui8x18Header[0];
bColorTabel = ui8x18Header[1] == 1;
ui32PicType = ui8x18Header[2]; ui32PicType = ui8x18Header[2];
ui32PaletteBegin = ui8x18Header[4] * 0x100 + ui8x18Header[3];
ui32PaletteLength = ui8x18Header[6] * 0x100 + ui8x18Header[5]; ui32PaletteLength = ui8x18Header[6] * 0x100 + ui8x18Header[5];
ui32PaletteBpP = ui8x18Header[7];
ui32Width = ui8x18Header[13] * 0x100 + ui8x18Header[12]; ui32Width = ui8x18Header[13] * 0x100 + ui8x18Header[12];
ui32Height = ui8x18Header[15] * 0x100 + ui8x18Header[14]; ui32Height = ui8x18Header[15] * 0x100 + ui8x18Header[14];
ui32BpP = ui8x18Header[16]; ui32BpP = ui8x18Header[16];
ui32Attribut = ui8x18Header[17];
// calculate some more information // calculate some more information
ui32Size = ui32Width * ui32Height * ui32BpP/8; ui32Size = ui32Width * ui32Height * ui32BpP/8;
bCompressed = ui32PicType == 9 || ui32PicType == 10; bCompressed = ui32PicType == 9 || ui32PicType == 10;
vui8Pixels.resize(ui32Size); vui8Pixels->resize(ui32Size);
/* consol output of the header
std::cout << "Header\n"
<< "ID l<>nge: " << ui32IDLength << std::endl
<< "Farbtabelle: " << (int)bColorTabel << std::endl
<< "Bildtype: " << ui32PicType << std::endl
<< "Palletenbegin: " << ui32PaletteBegin << std::endl
<< "Palletenl<6E>ngen: " << ui32PaletteLength << std::endl
<< "Bits pro Palleteneintrag: " << ui32PaletteBpP << std::endl
<< "Breite: " << ui32Width << std::endl
<< "H<>he: " << ui32Height << std::endl
<< "Bit pro Pixel: " << ui32BpP << std::endl
<< "Bild Attribute: " << ui32Attribut << std::endl;*/
// jump to the data block // jump to the data block
fsPicture.seekg(ui32IDLength + ui32PaletteLength, std::ios_base::cur); fsPicture.seekg(ui32IDLength + ui32PaletteLength, std::ios_base::cur);
@@ -49,7 +40,7 @@ TextureTGA::TextureTGA(const char * filePath)
// If not compressed 24 or 32 bit // If not compressed 24 or 32 bit
if (ui32PicType == 2 && (ui32BpP == 24 || ui32BpP == 32)) if (ui32PicType == 2 && (ui32BpP == 24 || ui32BpP == 32))
{ {
fsPicture.read(reinterpret_cast<char*>(vui8Pixels.data()), ui32Size); fsPicture.read(reinterpret_cast<char*>(vui8Pixels->data()), ui32Size);
} }
// else if compressed 24 or 32 bit // else if compressed 24 or 32 bit
else if (ui32PicType == 10 && (ui32BpP == 24 || ui32BpP == 32)) // compressed else if (ui32PicType == 10 && (ui32BpP == 24 || ui32BpP == 32)) // compressed
@@ -70,10 +61,10 @@ TextureTGA::TextureTGA(const char * filePath)
for (int i = 0; i <= tempChunkHeader; i++) for (int i = 0; i <= tempChunkHeader; i++)
{ {
vui8Pixels[tempByteIndex++] = tempData[0]; vui8Pixels->at(tempByteIndex++) = tempData[0];
vui8Pixels[tempByteIndex++] = tempData[1]; vui8Pixels->at(tempByteIndex++) = tempData[1];
vui8Pixels[tempByteIndex++] = tempData[2]; vui8Pixels->at(tempByteIndex++) = tempData[2];
if(ui32BpP == 32) vui8Pixels[tempByteIndex++] = tempData[3]; if(ui32BpP == 32) vui8Pixels->at(tempByteIndex++) = tempData[3];
} }
} }
else // data count else // data count
@@ -85,10 +76,10 @@ TextureTGA::TextureTGA(const char * filePath)
{ {
fsPicture.read(reinterpret_cast<char*>(&tempData), ui32BpP/8); fsPicture.read(reinterpret_cast<char*>(&tempData), ui32BpP/8);
vui8Pixels[tempByteIndex++] = tempData[0]; vui8Pixels->at(tempByteIndex++) = tempData[0];
vui8Pixels[tempByteIndex++] = tempData[1]; vui8Pixels->at(tempByteIndex++) = tempData[1];
vui8Pixels[tempByteIndex++] = tempData[2]; vui8Pixels->at(tempByteIndex++) = tempData[2];
if (ui32BpP == 32) vui8Pixels[tempByteIndex++] = tempData[3]; if (ui32BpP == 32) vui8Pixels->at(tempByteIndex++) = tempData[3];
} }
} }
} while (tempByteIndex < ui32Size); } while (tempByteIndex < ui32Size);
@@ -105,10 +96,9 @@ TextureTGA::TextureTGA(const char * filePath)
TextureTGA::~TextureTGA() TextureTGA::~TextureTGA()
{ {
vui8Pixels.clear();
} }
std::vector<std::uint8_t> TextureTGA::getData() const std::vector<std::uint8_t>* TextureTGA::getData() const
{ {
return vui8Pixels; return vui8Pixels;
} }

View File

@@ -17,7 +17,9 @@ int main(int argc, char** argv)
else else
scene = OpenGLController::getInstance(); scene = OpenGLController::getInstance();
#ifdef _DEBUG
scene->loadMsh("..\\Release\\Msh\\quadPoly.msh"); scene->loadMsh("..\\Release\\Msh\\quadPoly.msh");
#endif // DEBUG
do { do {
scene->updateScene(); scene->updateScene();

Binary file not shown.