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

@@ -2,7 +2,6 @@
#include "..\Header\MshFile.h"
#include "..\Header\OglViewerWidget.h"
#include "..\Header\MainWindow.h"
#include "..\Header\tga.h"
#include <cmath>
#include <QRegExp>
@@ -26,39 +25,6 @@ GeometryEngine::~GeometryEngine()
/////////////////////////////////////////////////////////////////////////
// private functions
void GeometryEngine::loadTexture(QString filePath, QString fileName)
{
bool loadSuccess(false);
QImage img;
if (fileName.isEmpty())
{
loadSuccess = true;
img = QImage(1, 1, QImage::Format_RGB32);
img.fill(Qt::red);
}
else
img = loadTga(filePath + "/" + fileName, loadSuccess);
if (!loadSuccess)
emit sendMessage("WARNING: texture not found or corrupted: " + QString(fileName), 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);
m_textures.push_back(new_texture);
}
void GeometryEngine::clearData()
{
if (m_arrayBuf.isCreated())
@@ -66,9 +32,13 @@ void GeometryEngine::clearData()
if (m_indexBuf.isCreated())
m_indexBuf.destroy();
for (auto it : m_textures)
delete it;
m_textures.clear();
if (m_materials != Q_NULLPTR)
{
for (auto it : *m_materials)
delete it.texture;
m_materials->clear();
delete m_materials;
}
m_drawList.clear();
}
@@ -90,7 +60,6 @@ void GeometryEngine::loadFile(QString filePath)
try
{
QVector<Model*>* models;
QStringList* textureNames;
QVector<VertexData> vertexData;
QVector<GLuint> indexData;
@@ -98,10 +67,11 @@ void GeometryEngine::loadFile(QString filePath)
MshFile file(filePath, this);
models = file.getModels();
textureNames = file.getTextureNames();
m_materials = file.getMaterials();
m_boundings = file.getBoundingBox();
// collect data
//TODO: sort transparent faces
unsigned int indexOffset(0);
unsigned int vertexOffset(0);
for (auto& modelIterator : *models)
@@ -140,18 +110,12 @@ void GeometryEngine::loadFile(QString filePath)
m_indexBuf.bind();
m_indexBuf.allocate(indexData.data(), indexData.size() * sizeof(GLuint));
emit sendMessage("loading textures..", 0);
// load the textures
int split = filePath.lastIndexOf(QRegExp("/|\\\\"));
for (auto& it : *textureNames)
loadTexture(filePath.left(split), it);
loadTexture("", "");
//pushback a default material
m_materials->push_back(FileInterface::getDefaultMaterial());
emit requestUpdate();
emit sendMessage("done..", 0);
emit sendFileInfo(filePath.right(filePath.size() - split - 1), QStringList(*textureNames), vertexData.size(), indexData.size() / 3);
emit sendFileInfo(filePath.right(filePath.size() - filePath.lastIndexOf(QRegExp("/|\\\\")) - 1), m_materials, vertexData.size(), indexData.size() / 3);
}
catch (std::invalid_argument e)
{
@@ -209,10 +173,10 @@ void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program)
for (auto& it : m_drawList)
{
// bind the correct texture
if (it.textureIndex < m_textures.size())
m_textures.at(it.textureIndex)->bind();
if (it.textureIndex < m_materials->size())
m_materials->at(it.textureIndex).texture->bind();
else
m_textures.last()->bind();
m_materials->last().texture->bind();
// Set model matrix
program->setUniformValue("m_matrix", it.modelMatrix);