working on changing the texture names to materials,

problems with call by value/reference
This commit is contained in:
Anakin
2017-01-15 12:26:15 +01:00
parent f469dff656
commit 6ead5d7bc6
8 changed files with 125 additions and 77 deletions

View File

@@ -1,5 +1,5 @@
#include "..\Header\MshFile.h"
#include "..\Header\tga.h"
// helper function to save data from file to any variable type
#define F2V(variableName) reinterpret_cast<char*>(&variableName)
@@ -160,7 +160,7 @@ void MshFile::analyseMsh2Chunks(std::list<ChunkHeader*>& chunkList)
std::list<ChunkHeader*> tmp_matdChunks;
loadChunks(tmp_matdChunks, it->position, it->size);
m_textureNames->push_back("");
m_materials->push_back(Material());
// analyse MATD subchunks
analyseMatdChunks(tmp_matdChunks);
@@ -218,12 +218,19 @@ void MshFile::analyseMatdChunks(std::list<ChunkHeader*>& chunkList)
{
if (!strcmp("TX0D", it->name))
{
// get the texture name
m_file.seekg(it->position);
char* buffer = new char[it->size + 1];
*buffer = { 0 };
m_file.read(buffer, it->size);
m_textureNames->back() = buffer;
m_materials->back().name = buffer;
delete[] buffer;
// load the texture
if (m_materials->back().name.isEmpty())
loadTexture(m_materials->back().texture, "");
else
loadTexture(m_materials->back().texture, m_filepath + "/" + m_materials->back().name);
}
}
}
@@ -514,14 +521,24 @@ void MshFile::analyseClthChunks(Model * dataDestination, std::list<ChunkHeader*>
// search if it is already known
bool tmp_found(false);
int index = m_textureNames->indexOf(QString::fromStdString(buffer));
if (index != -1)
new_segment->textureIndex = index;
else
for (unsigned int index = 0; index < m_materials->size(); index++)
{
m_textureNames->push_back(QString::fromStdString(buffer));
new_segment->textureIndex = m_textureNames->size() - 1;
if (m_materials->at(index).name == QString(buffer))
{
tmp_found = true;
new_segment->textureIndex = index;
break;
}
}
if(!tmp_found)
{
m_materials->push_back(Material());
m_materials->back().name = QString(buffer);
//TODO: load texture;
loadTexture(m_materials->back().texture, m_filepath + "/" + m_materials->back().name);
new_segment->textureIndex = m_materials->size() - 1;
}
delete[] buffer;
@@ -605,6 +622,39 @@ void MshFile::readUV(Segment * dataDestination, std::streampos position)
m_file.read(F2V(dataDestination->vertices[i].texCoord[j]), sizeof(float));
}
void MshFile::loadTexture(QOpenGLTexture * destination, QString filepath)
{
bool loadSuccess(false);
QImage img;
if (filepath.isEmpty())
{
loadSuccess = true;
img = QImage(1, 1, QImage::Format_RGB32);
img.fill(Qt::red);
}
else
img = loadTga(filepath, loadSuccess);
if (!loadSuccess)
emit sendMessage("WARNING: texture not found or corrupted: " + m_materials->back().name, 1);
// Load image to OglTexture
QOpenGLTexture* new_texture = new QOpenGLTexture(img.mirrored());
// Set nearest filtering mode for texture minification
new_texture->setMinificationFilter(QOpenGLTexture::Nearest);
// Set bilinear filtering mode for texture magnification
new_texture->setMagnificationFilter(QOpenGLTexture::Linear);
// Wrap texture coordinates by repeating
// f.ex. texture coordinate (1.1, 1.2) is same as (0.1, 0.2)
new_texture->setWrapMode(QOpenGLTexture::Repeat);
destination = new_texture;
}
QMatrix4x4 MshFile::getParentMatrix(std::string parent) const
{
QMatrix4x4 matrix;