Now supporting multiple material per mesh. Simon changed glDrawElementsBaseVertex to glDrawElements in DrawFinalPass.cpp Hallelulijah!
This commit is contained in:
@@ -1,12 +1,19 @@
|
||||
#include "Rendering\RawModelCustom.h"
|
||||
#include "Rendering/RawModelCustom.h"
|
||||
|
||||
RawModel::RawModel(std::string fileName)
|
||||
RawModel::RawModel(std::string& fileName)
|
||||
{
|
||||
ReadMeshFile(fileName);
|
||||
ReadMaterialFile(fileName);
|
||||
}
|
||||
|
||||
void RawModel::ReadMeshFile(std::string filePath)
|
||||
{
|
||||
char* fileData;
|
||||
std::ifstream in(fileName.c_str(), std::ios_base::binary | std::ios_base::ate);
|
||||
filePath += ".mesh";
|
||||
std::ifstream in(filePath.c_str(), std::ios_base::binary | std::ios_base::ate);
|
||||
|
||||
if (!in.is_open()) {
|
||||
throw Resource::FailedLoadingException("Open file failed");
|
||||
throw Resource::FailedLoadingException("Open mesh file failed");
|
||||
}
|
||||
unsigned int fileByteSize = in.tellg();
|
||||
in.seekg(0, std::ios_base::beg);
|
||||
@@ -21,23 +28,13 @@ RawModel::RawModel(std::string fileName)
|
||||
ReadMesh(offset, fileData, fileByteSize);
|
||||
}
|
||||
delete fileData;
|
||||
MaterialGroup mat;
|
||||
mat.StartIndex = 0;
|
||||
mat.EndIndex = m_Indices.size() - 1;
|
||||
MaterialGroups.push_back(mat);
|
||||
}
|
||||
|
||||
void RawModel::ReadMeshFileHeader(unsigned int& offset, char* fileData, unsigned int& fileByteSize)
|
||||
{
|
||||
#ifdef BOOST_LITTLE_ENDIAN
|
||||
unsigned int test;
|
||||
test = *(unsigned int*)fileData;
|
||||
unsigned int* test2;
|
||||
test2 = (unsigned int*)fileData;
|
||||
|
||||
m_Vertices.resize(test);
|
||||
m_Vertices.resize(*(unsigned int*)(fileData + offset));
|
||||
offset += sizeof(unsigned int);
|
||||
test = *(unsigned int*)(fileData + offset);
|
||||
m_Indices.resize(*(unsigned int*)(fileData + offset));
|
||||
offset += sizeof(unsigned int);
|
||||
#else
|
||||
@@ -78,6 +75,115 @@ void RawModel::ReadIndices(unsigned int& offset, char* fileData, unsigned int& f
|
||||
#endif
|
||||
}
|
||||
|
||||
void RawModel::ReadMaterialFile(std::string filePath)
|
||||
{
|
||||
char* fileData;
|
||||
filePath += ".mtrl";
|
||||
std::ifstream in(filePath.c_str(), std::ios_base::binary | std::ios_base::ate);
|
||||
|
||||
if (!in.is_open()) {
|
||||
throw Resource::FailedLoadingException("Open material file failed");
|
||||
}
|
||||
unsigned int fileByteSize = in.tellg();
|
||||
in.seekg(0, std::ios_base::beg);
|
||||
|
||||
fileData = new char[fileByteSize];
|
||||
in.read(fileData, fileByteSize);
|
||||
in.close();
|
||||
|
||||
unsigned int offset = 0;
|
||||
if (fileByteSize > 0) {
|
||||
ReadMaterials(offset, fileData, fileByteSize);
|
||||
}
|
||||
delete fileData;
|
||||
}
|
||||
|
||||
void RawModel::ReadMaterials(unsigned int& offset, char* fileData, unsigned int& fileByteSize)
|
||||
{
|
||||
#ifdef BOOST_LITTLE_ENDIAN
|
||||
unsigned int* numMaterials = (unsigned int*)(fileData);
|
||||
MaterialGroups.reserve(*numMaterials);
|
||||
offset += sizeof(unsigned int);
|
||||
|
||||
for (int i = 0; i < *numMaterials; i++) {
|
||||
ReadMaterialSingle(offset, fileData, fileByteSize);
|
||||
}
|
||||
#else
|
||||
#endif
|
||||
}
|
||||
|
||||
void RawModel::ReadMaterialSingle(unsigned int &offset, char* fileData, unsigned int& fileByteSize)
|
||||
{
|
||||
MaterialGroup newMaterial;
|
||||
|
||||
#ifdef BOOST_LITTLE_ENDIAN
|
||||
|
||||
if (offset + sizeof(unsigned int) * 4 > fileByteSize) {
|
||||
throw Resource::FailedLoadingException("Reading Material texture names length failed");
|
||||
}
|
||||
|
||||
unsigned int* nameLengths = (unsigned int*)(fileData + offset);
|
||||
offset += sizeof(unsigned int) * 4;
|
||||
|
||||
if (offset + sizeof(float) * 2 > fileByteSize) {
|
||||
throw Resource::FailedLoadingException("Reading Material specular and reflection values failed");
|
||||
}
|
||||
|
||||
newMaterial.SpecularExponent = *(float*)(fileData + offset);
|
||||
offset += sizeof(float);
|
||||
newMaterial.ReflectionFactor = *(float*)(fileData + offset);
|
||||
offset += sizeof(float);
|
||||
|
||||
if (offset + sizeof(unsigned int) * 2 > fileByteSize) {
|
||||
throw Resource::FailedLoadingException("Reading Material start and end index values failed");
|
||||
}
|
||||
|
||||
newMaterial.StartIndex = *(unsigned int*)(fileData + offset);
|
||||
offset += sizeof(unsigned int);
|
||||
newMaterial.EndIndex = *(unsigned int*)(fileData + offset);
|
||||
offset += sizeof(unsigned int);
|
||||
|
||||
if (nameLengths[0] > 0) {
|
||||
if (offset + nameLengths[0] > fileByteSize) {
|
||||
throw Resource::FailedLoadingException("Reading Material texture path failed");
|
||||
}
|
||||
|
||||
newMaterial.TexturePath = (fileData + offset);
|
||||
offset += nameLengths[0];
|
||||
}
|
||||
|
||||
if (nameLengths[1] > 0) {
|
||||
if (offset + nameLengths[1] > fileByteSize) {
|
||||
throw Resource::FailedLoadingException("Reading Material NormalMap path failed");
|
||||
}
|
||||
|
||||
newMaterial.NormalMapPath = (fileData + offset);
|
||||
offset += nameLengths[1];
|
||||
}
|
||||
|
||||
if (nameLengths[2] > 0) {
|
||||
if (offset + nameLengths[2] > fileByteSize) {
|
||||
throw Resource::FailedLoadingException("Reading Material SpecularMap path failed");
|
||||
}
|
||||
|
||||
newMaterial.SpecularMapPath = (fileData + offset);
|
||||
offset += nameLengths[2];
|
||||
}
|
||||
|
||||
if (nameLengths[3] > 0) {
|
||||
if (offset + nameLengths[3] > fileByteSize) {
|
||||
throw Resource::FailedLoadingException("Reading Material IncandescenceMap path failed");
|
||||
}
|
||||
|
||||
newMaterial.IncandescenceMapPath = (fileData + offset);
|
||||
offset += nameLengths[3];
|
||||
}
|
||||
|
||||
#else
|
||||
#endif
|
||||
|
||||
MaterialGroups.push_back(newMaterial);
|
||||
}
|
||||
|
||||
RawModel::~RawModel()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user