draw all models from vector,

window cosmetic,
next:
texture,
calculate modelmatrix from parent
This commit is contained in:
Anakin
2016-12-31 12:31:38 +01:00
parent c96d05decb
commit ee8705f9e4
8 changed files with 76 additions and 23 deletions

View File

@@ -35,11 +35,14 @@ GeometryEngine::~GeometryEngine()
void GeometryEngine::initCubeGeometry()
{
QVector<Model*>* models;
QVector<VertexData> vertexData;
QVector<GLuint> indexData;
try
{
MshFile file("..\\Release\\Msh\\cubeTex.msh");
m_models = file.getModels();
MshFile file("..\\Release\\Msh\\sphere.msh");
models = file.getModels();
//TODO use models local, apply MVP directly to the vertex, save size and tex index info
//TODO: handle the textures
@@ -50,13 +53,40 @@ void GeometryEngine::initCubeGeometry()
auto msg = e.what();
}
// collect data
unsigned int offsetCount(0);
for (auto& modelIterator : *models)
{
for (auto& segmentIterator : modelIterator->segmList)
{
// get draw information
DrawInformation new_info;
new_info.offset = offsetCount;
new_info.size = segmentIterator->indices.size();
new_info.textureIndex = segmentIterator->textureIndex;
new_info.modelMatrix = modelIterator->m4x4Translation;
// add offset to indices
for (auto& it : segmentIterator->indices)
it += new_info.offset;
// save data
vertexData += segmentIterator->vertices;
indexData += segmentIterator->indices;
m_drawList.push_back(new_info);
// update offset
offsetCount += new_info.size;
}
}
// Transfer vertex data to VBO 0
m_arrayBuf.bind();
m_arrayBuf.allocate(m_models->first()->segmList.front()->vertices.data(), m_models->first()->segmList.front()->vertices.size() * sizeof(VertexData));
m_arrayBuf.allocate(vertexData.data(),vertexData.size() * sizeof(VertexData));
// Transfer index data to VBO 1
m_indexBuf.bind();
m_indexBuf.allocate(m_models->first()->segmList.front()->indices.data(), m_models->first()->segmList.front()->indices.size() * sizeof(GLuint));
m_indexBuf.allocate(indexData.data(), indexData.size() * sizeof(GLuint));
// load the texture
initTexture();
@@ -86,12 +116,12 @@ void GeometryEngine::initTexture()
void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program)
{
// Setup
// Tell OpenGL which VBOs to use
m_arrayBuf.bind();
m_indexBuf.bind();
m_textures.first()->bind();
// Use texture unit 0 which contains cube.png
// Allways use texture unit 0
program->setUniformValue("texture", 0);
// Offset for position
@@ -110,6 +140,20 @@ void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program)
program->enableAttributeArray(texcoordLocation);
program->setAttributeBuffer(texcoordLocation, GL_FLOAT, offset, 2, sizeof(VertexData));
// Draw cube geometry using indices from VBO 1
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
// Paint
for (auto& it : m_drawList)
{
// bind the correct texture
if (it.textureIndex < m_textures.size())
m_textures.last()->bind();
else
m_textures.at(it.textureIndex)->bind();
// Set model matrix
program->setUniformValue("m_matrix", it.modelMatrix);
// Draw cube geometry using indices from VBO 1
glDrawElements(GL_TRIANGLES, it.size, GL_UNSIGNED_INT, (void*)(it.offset * sizeof(GLuint)));
}
}

View File

@@ -12,9 +12,11 @@ MainWindow::MainWindow(QWidget *parent)
format.setDepthBufferSize(24);
QSurfaceFormat::setDefaultFormat(format);
this->setCentralWidget(new OglViewerWidget(this));
setCentralWidget(new OglViewerWidget(this));
ui->statusBar->showMessage("haha vbgf");
setWindowIcon(QIcon(":/images/icon.ico"));
setWindowTitle("Mesh Viewer");
ui->statusBar->showMessage("pre-alpha");
}

View File

@@ -142,8 +142,8 @@ void OglViewerWidget::paintGL()
view.translate(m_translation);
view.rotate(m_rotation);
// Set modelview-projection matrix
m_program.setUniformValue("mvp_matrix", m_projection * view);
// Set view-projection matrix
m_program.setUniformValue("vp_matrix", m_projection * view);
// Draw cube geometry
m_dataEngine->drawGeometry(&m_program);