2016-12-24 15:03:37 +00:00
|
|
|
#include "..\Header\GeometryEngine.h"
|
2016-12-30 11:36:05 +00:00
|
|
|
#include "..\Header\MshFile.h"
|
2017-01-04 13:35:27 +00:00
|
|
|
#include "..\Header\OglViewerWidget.h"
|
|
|
|
#include "..\Header\MainWindow.h"
|
2017-01-29 10:35:43 +00:00
|
|
|
#include "..\Header\OutputDevice.h"
|
2017-01-07 14:59:16 +00:00
|
|
|
#include <QRegExp>
|
2016-12-24 15:03:37 +00:00
|
|
|
|
2016-12-29 13:06:25 +00:00
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2017-01-28 15:54:36 +00:00
|
|
|
// constructor/destructor
|
2016-12-29 13:06:25 +00:00
|
|
|
|
2017-01-04 13:35:27 +00:00
|
|
|
GeometryEngine::GeometryEngine(QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, m_indexBuf(QOpenGLBuffer::IndexBuffer)
|
2017-01-28 15:54:36 +00:00
|
|
|
, m_defaultMaterial(FileInterface::getDefaultMaterial())
|
2016-12-24 15:03:37 +00:00
|
|
|
{
|
2016-12-29 12:37:15 +00:00
|
|
|
initializeOpenGLFunctions();
|
2016-12-24 15:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GeometryEngine::~GeometryEngine()
|
|
|
|
{
|
add exe icon,
signal-slot for openFile,
resetView on openFile,
cleanup when open a new file,
triangulate quad poly now correctly, more not working, bug from previous version,
next:
fix 5,6,.. triangulation,
triClothMan, IC Helmet, still buggy,
2017-01-02 11:21:32 +00:00
|
|
|
clearData();
|
2017-01-23 11:17:26 +00:00
|
|
|
delete m_defaultMaterial->texture0;
|
2017-01-20 15:26:58 +00:00
|
|
|
delete m_defaultMaterial;
|
2016-12-24 15:03:37 +00:00
|
|
|
}
|
|
|
|
|
2016-12-29 13:06:25 +00:00
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2017-01-28 15:54:36 +00:00
|
|
|
// functions
|
2016-12-29 13:06:25 +00:00
|
|
|
|
2017-01-07 14:59:16 +00:00
|
|
|
void GeometryEngine::clearData()
|
|
|
|
{
|
|
|
|
if (m_arrayBuf.isCreated())
|
|
|
|
m_arrayBuf.destroy();
|
|
|
|
if (m_indexBuf.isCreated())
|
|
|
|
m_indexBuf.destroy();
|
|
|
|
|
2017-01-15 11:26:15 +00:00
|
|
|
if (m_materials != Q_NULLPTR)
|
|
|
|
{
|
|
|
|
for (auto it : *m_materials)
|
2017-01-23 11:29:10 +00:00
|
|
|
{
|
|
|
|
if (it.texture0 != Q_NULLPTR)
|
|
|
|
delete it.texture0;
|
|
|
|
|
|
|
|
if (it.texture1 != Q_NULLPTR)
|
|
|
|
delete it.texture1;
|
|
|
|
}
|
2017-01-15 11:26:15 +00:00
|
|
|
m_materials->clear();
|
|
|
|
delete m_materials;
|
|
|
|
}
|
2017-01-07 14:59:16 +00:00
|
|
|
m_drawList.clear();
|
|
|
|
}
|
|
|
|
|
2017-02-05 15:57:12 +00:00
|
|
|
void GeometryEngine::setupPipeline(QOpenGLShaderProgram *program)
|
2016-12-29 12:37:15 +00:00
|
|
|
{
|
|
|
|
// Offset for position
|
|
|
|
quintptr offset = 0;
|
2016-12-24 15:03:37 +00:00
|
|
|
|
2016-12-29 12:37:15 +00:00
|
|
|
// Tell OpenGL programmable pipeline how to locate vertex position data
|
|
|
|
int vertexLocation = program->attributeLocation("a_position");
|
|
|
|
program->enableAttributeArray(vertexLocation);
|
|
|
|
program->setAttributeBuffer(vertexLocation, GL_FLOAT, offset, 3, sizeof(VertexData));
|
2016-12-24 15:03:37 +00:00
|
|
|
|
2016-12-29 12:37:15 +00:00
|
|
|
// Offset for texture coordinate
|
|
|
|
offset += sizeof(QVector3D);
|
2016-12-24 15:03:37 +00:00
|
|
|
|
2016-12-29 12:37:15 +00:00
|
|
|
// Tell OpenGL programmable pipeline how to locate vertex texture coordinate data
|
|
|
|
int texcoordLocation = program->attributeLocation("a_texcoord");
|
|
|
|
program->enableAttributeArray(texcoordLocation);
|
|
|
|
program->setAttributeBuffer(texcoordLocation, GL_FLOAT, offset, 2, sizeof(VertexData));
|
2016-12-24 15:03:37 +00:00
|
|
|
|
2017-02-05 15:57:12 +00:00
|
|
|
//Offset for vertexNormal
|
2017-01-08 13:41:53 +00:00
|
|
|
offset += sizeof(QVector2D);
|
|
|
|
|
|
|
|
// Tell OpenGL programmable pipeline how to locate vertex normal data
|
2017-02-05 15:57:12 +00:00
|
|
|
int vertNormLocation = program->attributeLocation("a_normal");
|
|
|
|
program->enableAttributeArray(vertNormLocation);
|
|
|
|
program->setAttributeBuffer(vertNormLocation, GL_FLOAT, offset, 3, sizeof(VertexData));
|
|
|
|
|
|
|
|
//Offset for polygonNormal
|
|
|
|
offset += sizeof(QVector3D);
|
|
|
|
|
|
|
|
// Tell OpenGL programmable pipeline how to locate polygon normal data
|
|
|
|
int polyNormLocation = program->attributeLocation("a_polyNorm");
|
|
|
|
program->enableAttributeArray(polyNormLocation);
|
|
|
|
program->setAttributeBuffer(polyNormLocation, GL_FLOAT, offset, 3, sizeof(VertexData));
|
|
|
|
|
|
|
|
//Offset for polygonTangent
|
|
|
|
offset += sizeof(QVector3D);
|
|
|
|
|
|
|
|
// Tell OpenGL programmable pipeline how to locate polygon tangent data
|
|
|
|
int polyTanLocation = program->attributeLocation("a_polyTan");
|
|
|
|
program->enableAttributeArray(polyTanLocation);
|
|
|
|
program->setAttributeBuffer(polyTanLocation, GL_FLOAT, offset, 3, sizeof(VertexData));
|
|
|
|
|
|
|
|
//Offset for polygonBitangent
|
|
|
|
offset += sizeof(QVector3D);
|
|
|
|
|
|
|
|
// Tell OpenGL programmable pipeline how to locate polygon bitangent data
|
|
|
|
int polyBiTanLocation = program->attributeLocation("a_polyBiTan");
|
|
|
|
program->enableAttributeArray(polyBiTanLocation);
|
|
|
|
program->setAttributeBuffer(polyBiTanLocation, GL_FLOAT, offset, 3, sizeof(VertexData));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program)
|
|
|
|
{
|
|
|
|
if (!m_arrayBuf.isCreated() || !m_indexBuf.isCreated())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Setup
|
|
|
|
// Tell OpenGL which VBOs to use
|
|
|
|
m_arrayBuf.bind();
|
|
|
|
m_indexBuf.bind();
|
|
|
|
|
|
|
|
// Allways normalize by this
|
|
|
|
QMatrix4x4 normMatrix;
|
|
|
|
float maxExtent = std::max(std::max(m_boundings.extents[0], m_boundings.extents[1]), m_boundings.extents[2]);
|
|
|
|
normMatrix.scale(1 / maxExtent);
|
|
|
|
normMatrix.translate(-m_boundings.center[0], -m_boundings.center[1], -m_boundings.center[2]);
|
2017-02-06 13:53:05 +00:00
|
|
|
program->setUniformValue("normalizeModel", normMatrix);
|
2017-02-05 15:57:12 +00:00
|
|
|
|
2017-02-05 19:10:05 +00:00
|
|
|
// Allways use texture unit 0 and 1
|
2017-02-06 13:53:05 +00:00
|
|
|
program->setUniformValue("tx0", 0);
|
|
|
|
program->setUniformValue("tx1", 1);
|
2017-01-08 13:41:53 +00:00
|
|
|
|
2017-02-05 15:57:12 +00:00
|
|
|
//setup the pipeline
|
|
|
|
setupPipeline(program);
|
|
|
|
|
2017-01-28 15:54:36 +00:00
|
|
|
// Paint
|
2016-12-31 11:31:38 +00:00
|
|
|
|
|
|
|
for (auto& it : m_drawList)
|
|
|
|
{
|
2017-01-15 14:51:12 +00:00
|
|
|
bool tmp_transparent(false);
|
2017-01-22 13:37:06 +00:00
|
|
|
bool tmp_specular(false);
|
2017-02-05 19:10:05 +00:00
|
|
|
bool tmp_normalmap(false);
|
2017-02-06 13:53:05 +00:00
|
|
|
bool tmp_glow(false);
|
2017-01-26 18:44:12 +00:00
|
|
|
float shininess(0.0);
|
2017-01-19 16:57:50 +00:00
|
|
|
QVector3D specularColor;
|
2017-01-15 14:51:12 +00:00
|
|
|
|
2016-12-31 11:31:38 +00:00
|
|
|
// bind the correct texture
|
2017-01-28 15:54:36 +00:00
|
|
|
if (it.textureIndex < (unsigned)m_materials->size() && m_materials->at(it.textureIndex).texture0 != Q_NULLPTR)
|
2017-01-15 14:51:12 +00:00
|
|
|
{
|
2017-02-05 19:10:05 +00:00
|
|
|
m_materials->at(it.textureIndex).texture0->bind(0);
|
2017-01-15 14:51:12 +00:00
|
|
|
tmp_transparent = m_materials->at(it.textureIndex).transparent;
|
2017-01-22 13:37:06 +00:00
|
|
|
tmp_specular = m_materials->at(it.textureIndex).flags[7];
|
2017-01-19 16:57:50 +00:00
|
|
|
shininess = m_materials->at(it.textureIndex).shininess;
|
2017-01-20 15:26:58 +00:00
|
|
|
specularColor = m_materials->at(it.textureIndex).specularColor.toVector3D();
|
2017-02-05 19:10:05 +00:00
|
|
|
|
|
|
|
if (m_materials->at(it.textureIndex).rendertype == 27 || m_materials->at(it.textureIndex).rendertype == 28)
|
|
|
|
{
|
|
|
|
if (m_materials->at(it.textureIndex).texture1 != Q_NULLPTR)
|
|
|
|
{
|
|
|
|
tmp_normalmap = true;
|
|
|
|
m_materials->at(it.textureIndex).texture1->bind(1);
|
|
|
|
}
|
|
|
|
}
|
2017-02-06 13:53:05 +00:00
|
|
|
|
|
|
|
if (m_materials->at(it.textureIndex).flags[0] || m_materials->at(it.textureIndex).flags[1] || m_materials->at(it.textureIndex).rendertype == 1)
|
|
|
|
tmp_glow = true;
|
2017-01-15 14:51:12 +00:00
|
|
|
}
|
2016-12-31 12:04:03 +00:00
|
|
|
else
|
2017-01-15 14:51:12 +00:00
|
|
|
{
|
2017-02-05 19:10:05 +00:00
|
|
|
m_defaultMaterial->texture0->bind(0);
|
2017-01-20 15:26:58 +00:00
|
|
|
tmp_transparent = m_defaultMaterial->transparent;
|
2017-01-15 14:51:12 +00:00
|
|
|
}
|
2017-02-05 19:10:05 +00:00
|
|
|
|
2016-12-31 11:31:38 +00:00
|
|
|
// Set model matrix
|
2017-02-06 13:53:05 +00:00
|
|
|
program->setUniformValue("modelMatrix", it.modelMatrix);
|
2016-12-31 11:31:38 +00:00
|
|
|
|
2017-01-17 19:18:04 +00:00
|
|
|
// Set normal matrix
|
2017-02-06 13:53:05 +00:00
|
|
|
program->setUniformValue("normalMatrix", (normMatrix * it.modelMatrix).normalMatrix());
|
2017-01-15 14:51:12 +00:00
|
|
|
|
2017-01-19 16:57:50 +00:00
|
|
|
// set some material attributes
|
2017-02-06 13:53:05 +00:00
|
|
|
program->setUniformValue("material.shininess", shininess);
|
|
|
|
program->setUniformValue("material.specularColor", specularColor);
|
|
|
|
program->setUniformValue("material.isTransparent", tmp_transparent);
|
|
|
|
program->setUniformValue("material.hasSpecularmap", tmp_specular);
|
|
|
|
program->setUniformValue("material.hasNormalmap", tmp_normalmap);
|
|
|
|
program->setUniformValue("material.isGlow", tmp_glow);
|
2017-01-19 16:57:50 +00:00
|
|
|
|
2016-12-31 11:31:38 +00:00
|
|
|
// Draw cube geometry using indices from VBO 1
|
2017-01-16 15:41:52 +00:00
|
|
|
glDrawElements(GL_TRIANGLES, it.size, GL_UNSIGNED_INT, (void*)(it.offset * sizeof(GLuint)));
|
2017-01-28 15:54:36 +00:00
|
|
|
|
2016-12-31 11:31:38 +00:00
|
|
|
}
|
2016-12-24 15:03:37 +00:00
|
|
|
}
|
2017-01-04 13:35:27 +00:00
|
|
|
|
2017-01-28 15:54:36 +00:00
|
|
|
void GeometryEngine::loadFile(QString filePath)
|
|
|
|
{
|
|
|
|
// cleanup old stuff and recreate buffers
|
|
|
|
clearData();
|
|
|
|
m_arrayBuf.create();
|
|
|
|
m_indexBuf.create();
|
|
|
|
|
|
|
|
//reset view
|
|
|
|
emit requestResetView();
|
2017-01-29 10:35:43 +00:00
|
|
|
OutputDevice::getInstance()->print("loading file..", 0);
|
2017-01-28 15:54:36 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
QVector<Model*>* models;
|
|
|
|
QVector<VertexData> vertexData;
|
|
|
|
QVector<GLuint> indexData;
|
|
|
|
|
|
|
|
// open file and get the information
|
2017-01-29 10:35:43 +00:00
|
|
|
MshFile file(filePath);
|
2017-01-28 15:54:36 +00:00
|
|
|
|
|
|
|
models = file.getModels();
|
|
|
|
m_materials = file.getMaterials();
|
|
|
|
m_boundings = file.getBoundingBox();
|
|
|
|
|
|
|
|
// collect data
|
|
|
|
unsigned int indexOffset(0);
|
|
|
|
unsigned int vertexOffset(0);
|
|
|
|
for (auto& modelIterator : *models)
|
|
|
|
{
|
|
|
|
for (auto& segmentIterator : modelIterator->segmList)
|
|
|
|
{
|
|
|
|
// get draw information
|
|
|
|
DrawInformation new_info;
|
|
|
|
new_info.offset = indexOffset;
|
|
|
|
new_info.size = segmentIterator->indices.size();
|
|
|
|
new_info.textureIndex = segmentIterator->textureIndex;
|
|
|
|
new_info.modelMatrix = modelIterator->m4x4Translation;
|
|
|
|
new_info.modelMatrix.rotate(modelIterator->quadRotation);
|
|
|
|
|
|
|
|
// add offset to indices, no need to do it for the first one (maybe it's very big)
|
|
|
|
if (vertexOffset != 0)
|
|
|
|
for (auto& it : segmentIterator->indices)
|
|
|
|
it += vertexOffset;
|
|
|
|
|
|
|
|
// save data
|
|
|
|
vertexData += segmentIterator->vertices;
|
|
|
|
indexData += segmentIterator->indices;
|
|
|
|
|
|
|
|
if (segmentIterator->textureIndex < (unsigned) m_materials->size() && m_materials->at(segmentIterator->textureIndex).transparent)
|
|
|
|
m_drawList.push_back(new_info);
|
|
|
|
else
|
|
|
|
m_drawList.push_front(new_info);
|
|
|
|
|
|
|
|
// update offset
|
|
|
|
indexOffset += new_info.size;
|
|
|
|
vertexOffset += segmentIterator->vertices.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transfer vertex data to VBO 0
|
|
|
|
m_arrayBuf.bind();
|
|
|
|
m_arrayBuf.allocate(vertexData.data(), vertexData.size() * sizeof(VertexData));
|
|
|
|
|
|
|
|
// Transfer index data to VBO 1
|
|
|
|
m_indexBuf.bind();
|
|
|
|
m_indexBuf.allocate(indexData.data(), indexData.size() * sizeof(GLuint));
|
|
|
|
|
|
|
|
emit requestUpdate();
|
2017-01-29 10:35:43 +00:00
|
|
|
OutputDevice::getInstance()->print("done..", 0);
|
2017-01-29 14:19:20 +00:00
|
|
|
OutputDevice::getInstance()->setFileInfo(filePath.right(filePath.size() - filePath.lastIndexOf(QRegExp("/|\\\\")) - 1), m_materials, vertexData.size(), indexData.size() / 3);
|
2017-01-28 15:54:36 +00:00
|
|
|
}
|
|
|
|
catch (std::invalid_argument e)
|
|
|
|
{
|
|
|
|
clearData();
|
2017-01-29 10:35:43 +00:00
|
|
|
OutputDevice::getInstance()->print(QString(e.what()), 2);
|
2017-01-28 15:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|