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

@ -6,6 +6,13 @@
#include <QOpenGLTexture>
#include <QVector>
struct DrawInformation {
unsigned int offset;
unsigned int size;
unsigned int textureIndex;
QMatrix4x4 modelMatrix;
};
class GeometryEngine : protected QOpenGLFunctions
{
public:
@ -16,9 +23,7 @@ private:
QOpenGLBuffer m_arrayBuf;
QOpenGLBuffer m_indexBuf;
QVector<QOpenGLTexture*> m_textures;
QVector<Model*>* m_models = Q_NULLPTR;
QVector<std::string>* m_textureNames = Q_NULLPTR; //TODO: remove, use it local and only hold the textures itself
QVector<DrawInformation> m_drawList;
void initCubeGeometry();
void initTexture();

View File

@ -5,5 +5,6 @@
</qresource>
<qresource prefix="/images">
<file>cube.png</file>
<file>icon.ico</file>
</qresource>
</RCC>

View File

@ -10,6 +10,6 @@ varying vec2 v_texcoord;
void main()
{
// Set fragment color from texture
gl_FragColor = texture2D(texture, v_texcoord);
// Set fragment color from texture
gl_FragColor = texture2D(texture, v_texcoord);
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 264 KiB

After

Width:  |  Height:  |  Size: 264 KiB

View File

@ -4,7 +4,8 @@ precision mediump int;
precision mediump float;
#endif
uniform mat4 mvp_matrix;
uniform mat4 vp_matrix;
uniform mat4 m_matrix;
attribute vec4 a_position;
attribute vec2 a_texcoord;
@ -13,10 +14,10 @@ varying vec2 v_texcoord;
void main()
{
// Calculate vertex position in screen space
gl_Position = mvp_matrix * a_position;
// Calculate vertex position in screen space
gl_Position = vp_matrix * m_matrix * a_position;
// Pass texture coordinate to fragment shader
// Value will be automatically interpolated to fragments inside polygon faces
v_texcoord = a_texcoord;
// Pass texture coordinate to fragment shader
// Value will be automatically interpolated to fragments inside polygon faces
v_texcoord = a_texcoord;
}

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);