Commit ee8705f9 authored by Anakin's avatar Anakin

draw all models from vector,

window cosmetic,
next:
texture,
calculate modelmatrix from parent
parent c96d05de
...@@ -6,6 +6,13 @@ ...@@ -6,6 +6,13 @@
#include <QOpenGLTexture> #include <QOpenGLTexture>
#include <QVector> #include <QVector>
struct DrawInformation {
unsigned int offset;
unsigned int size;
unsigned int textureIndex;
QMatrix4x4 modelMatrix;
};
class GeometryEngine : protected QOpenGLFunctions class GeometryEngine : protected QOpenGLFunctions
{ {
public: public:
...@@ -16,9 +23,7 @@ private: ...@@ -16,9 +23,7 @@ private:
QOpenGLBuffer m_arrayBuf; QOpenGLBuffer m_arrayBuf;
QOpenGLBuffer m_indexBuf; QOpenGLBuffer m_indexBuf;
QVector<QOpenGLTexture*> m_textures; QVector<QOpenGLTexture*> m_textures;
QVector<DrawInformation> m_drawList;
QVector<Model*>* m_models = Q_NULLPTR;
QVector<std::string>* m_textureNames = Q_NULLPTR; //TODO: remove, use it local and only hold the textures itself
void initCubeGeometry(); void initCubeGeometry();
void initTexture(); void initTexture();
......
...@@ -5,5 +5,6 @@ ...@@ -5,5 +5,6 @@
</qresource> </qresource>
<qresource prefix="/images"> <qresource prefix="/images">
<file>cube.png</file> <file>cube.png</file>
<file>icon.ico</file>
</qresource> </qresource>
</RCC> </RCC>
...@@ -10,6 +10,6 @@ varying vec2 v_texcoord; ...@@ -10,6 +10,6 @@ varying vec2 v_texcoord;
void main() void main()
{ {
// Set fragment color from texture // Set fragment color from texture
gl_FragColor = texture2D(texture, v_texcoord); gl_FragColor = texture2D(texture, v_texcoord);
} }
QtMeshViewer/Resources/icon.ico

264 KB | W: | H:

QtMeshViewer/Resources/icon.ico

264 KB | W: | H:

QtMeshViewer/Resources/icon.ico
QtMeshViewer/Resources/icon.ico
QtMeshViewer/Resources/icon.ico
QtMeshViewer/Resources/icon.ico
  • 2-up
  • Swipe
  • Onion skin
...@@ -4,7 +4,8 @@ precision mediump int; ...@@ -4,7 +4,8 @@ precision mediump int;
precision mediump float; precision mediump float;
#endif #endif
uniform mat4 mvp_matrix; uniform mat4 vp_matrix;
uniform mat4 m_matrix;
attribute vec4 a_position; attribute vec4 a_position;
attribute vec2 a_texcoord; attribute vec2 a_texcoord;
...@@ -13,10 +14,10 @@ varying vec2 v_texcoord; ...@@ -13,10 +14,10 @@ varying vec2 v_texcoord;
void main() void main()
{ {
// Calculate vertex position in screen space // Calculate vertex position in screen space
gl_Position = mvp_matrix * a_position; gl_Position = vp_matrix * m_matrix * a_position;
// Pass texture coordinate to fragment shader // Pass texture coordinate to fragment shader
// Value will be automatically interpolated to fragments inside polygon faces // Value will be automatically interpolated to fragments inside polygon faces
v_texcoord = a_texcoord; v_texcoord = a_texcoord;
} }
...@@ -35,11 +35,14 @@ GeometryEngine::~GeometryEngine() ...@@ -35,11 +35,14 @@ GeometryEngine::~GeometryEngine()
void GeometryEngine::initCubeGeometry() void GeometryEngine::initCubeGeometry()
{ {
QVector<Model*>* models;
QVector<VertexData> vertexData;
QVector<GLuint> indexData;
try try
{ {
MshFile file("..\\Release\\Msh\\cubeTex.msh"); MshFile file("..\\Release\\Msh\\sphere.msh");
m_models = file.getModels(); models = file.getModels();
//TODO use models local, apply MVP directly to the vertex, save size and tex index info //TODO use models local, apply MVP directly to the vertex, save size and tex index info
//TODO: handle the textures //TODO: handle the textures
...@@ -50,13 +53,40 @@ void GeometryEngine::initCubeGeometry() ...@@ -50,13 +53,40 @@ void GeometryEngine::initCubeGeometry()
auto msg = e.what(); 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 // Transfer vertex data to VBO 0
m_arrayBuf.bind(); 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 // Transfer index data to VBO 1
m_indexBuf.bind(); 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 // load the texture
initTexture(); initTexture();
...@@ -86,12 +116,12 @@ void GeometryEngine::initTexture() ...@@ -86,12 +116,12 @@ void GeometryEngine::initTexture()
void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program) void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program)
{ {
// Setup
// Tell OpenGL which VBOs to use // Tell OpenGL which VBOs to use
m_arrayBuf.bind(); m_arrayBuf.bind();
m_indexBuf.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); program->setUniformValue("texture", 0);
// Offset for position // Offset for position
...@@ -110,6 +140,20 @@ void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program) ...@@ -110,6 +140,20 @@ void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program)
program->enableAttributeArray(texcoordLocation); program->enableAttributeArray(texcoordLocation);
program->setAttributeBuffer(texcoordLocation, GL_FLOAT, offset, 2, sizeof(VertexData)); program->setAttributeBuffer(texcoordLocation, GL_FLOAT, offset, 2, sizeof(VertexData));
// Draw cube geometry using indices from VBO 1 // Paint
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
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)));
}
} }
...@@ -12,9 +12,11 @@ MainWindow::MainWindow(QWidget *parent) ...@@ -12,9 +12,11 @@ MainWindow::MainWindow(QWidget *parent)
format.setDepthBufferSize(24); format.setDepthBufferSize(24);
QSurfaceFormat::setDefaultFormat(format); 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");
} }
......
...@@ -142,8 +142,8 @@ void OglViewerWidget::paintGL() ...@@ -142,8 +142,8 @@ void OglViewerWidget::paintGL()
view.translate(m_translation); view.translate(m_translation);
view.rotate(m_rotation); view.rotate(m_rotation);
// Set modelview-projection matrix // Set view-projection matrix
m_program.setUniformValue("mvp_matrix", m_projection * view); m_program.setUniformValue("vp_matrix", m_projection * view);
// Draw cube geometry // Draw cube geometry
m_dataEngine->drawGeometry(&m_program); m_dataEngine->drawGeometry(&m_program);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment