manage data in vectors

This commit is contained in:
Anakin 2016-12-29 14:06:25 +01:00
parent 0499982150
commit 82ce8ad72a
4 changed files with 97 additions and 69 deletions

View File

@ -4,6 +4,7 @@
#include <QOpenGLShaderProgram> #include <QOpenGLShaderProgram>
#include <QOpenGLBuffer> #include <QOpenGLBuffer>
#include <QOpenGLTexture> #include <QOpenGLTexture>
#include <QVector>
class GeometryEngine : protected QOpenGLFunctions class GeometryEngine : protected QOpenGLFunctions
{ {
@ -11,15 +12,15 @@ public:
GeometryEngine(); GeometryEngine();
virtual ~GeometryEngine(); virtual ~GeometryEngine();
void drawGeometry(QOpenGLShaderProgram *program);
private: private:
QOpenGLBuffer m_arrayBuf;
QOpenGLBuffer m_indexBuf;
QVector<QOpenGLTexture*> m_textures;
void initCubeGeometry(); void initCubeGeometry();
void initTexture(); void initTexture();
QOpenGLBuffer arrayBuf; public:
QOpenGLBuffer indexBuf; void drawGeometry(QOpenGLShaderProgram *program);
QOpenGLTexture *texture;
}; };

View File

@ -21,6 +21,19 @@ public:
explicit OglViewerWidget(QWidget *parent = 0); explicit OglViewerWidget(QWidget *parent = 0);
~OglViewerWidget(); ~OglViewerWidget();
private:
struct {
bool left = false;
bool right = false;
QVector2D position;
} m_mouse;
QOpenGLShaderProgram m_program;
GeometryEngine *m_dataEngine;
QMatrix4x4 m_projection;
QQuaternion m_rotation;
protected: protected:
void mousePressEvent(QMouseEvent *e) Q_DECL_OVERRIDE; void mousePressEvent(QMouseEvent *e) Q_DECL_OVERRIDE;
void mouseReleaseEvent(QMouseEvent *e) Q_DECL_OVERRIDE; void mouseReleaseEvent(QMouseEvent *e) Q_DECL_OVERRIDE;
@ -34,17 +47,6 @@ protected:
private: private:
void initShaders(); void initShaders();
private:
struct {
bool left = false;
bool right = false;
QVector2D position;
} m_mouse;
QOpenGLShaderProgram m_program;
GeometryEngine *m_dataEngine;
QMatrix4x4 m_projection;
QQuaternion m_rotation;
}; };

View File

@ -10,15 +10,18 @@ struct VertexData
QVector2D texCoord; QVector2D texCoord;
}; };
/////////////////////////////////////////////////////////////////////////
// public constructor/destructor
GeometryEngine::GeometryEngine() GeometryEngine::GeometryEngine()
: indexBuf(QOpenGLBuffer::IndexBuffer) : m_indexBuf(QOpenGLBuffer::IndexBuffer)
, texture(Q_NULLPTR)
{ {
initializeOpenGLFunctions(); initializeOpenGLFunctions();
// Generate 2 VBOs // Generate 2 VBOs
arrayBuf.create(); m_arrayBuf.create();
indexBuf.create(); m_indexBuf.create();
// Initializes cube geometry and transfers it to VBOs // Initializes cube geometry and transfers it to VBOs
initCubeGeometry(); initCubeGeometry();
@ -26,17 +29,23 @@ GeometryEngine::GeometryEngine()
GeometryEngine::~GeometryEngine() GeometryEngine::~GeometryEngine()
{ {
arrayBuf.destroy(); m_arrayBuf.destroy();
indexBuf.destroy(); m_indexBuf.destroy();
delete texture;
for (auto it : m_textures)
delete it;
m_textures.clear();
m_textures.squeeze();
} }
/////////////////////////////////////////////////////////////////////////
// private functions
void GeometryEngine::initCubeGeometry() void GeometryEngine::initCubeGeometry()
{ {
// For cube we would need only 8 vertices but we have to
// duplicate vertex for each face because texture coordinate QVector<VertexData> vertices = {
// is different.
VertexData vertices[] = {
// Vertex data for face 0 // Vertex data for face 0
{QVector3D(-1.0f, -1.0f, 1.0f), QVector2D(0.0f, 0.0f)}, // v0 {QVector3D(-1.0f, -1.0f, 1.0f), QVector2D(0.0f, 0.0f)}, // v0
{QVector3D( 1.0f, -1.0f, 1.0f), QVector2D(0.33f, 0.0f)}, // v1 {QVector3D( 1.0f, -1.0f, 1.0f), QVector2D(0.33f, 0.0f)}, // v1
@ -74,14 +83,7 @@ void GeometryEngine::initCubeGeometry()
{QVector3D( 1.0f, 1.0f, -1.0f), QVector2D(0.66f, 1.0f)} // v23 {QVector3D( 1.0f, 1.0f, -1.0f), QVector2D(0.66f, 1.0f)} // v23
}; };
// Indices for drawing cube faces using triangle strips. QVector<GLushort> indices = {
// Triangle strips can be connected by duplicating indices
// between the strips. If connecting strips have opposite
// vertex order then last index of the first strip and first
// index of the second strip needs to be duplicated. If
// connecting strips have same vertex order then only last
// index of the first strip needs to be duplicated.
GLushort indices[] = {
0,1,2, //vorne (4) 0,1,2, //vorne (4)
3,2,1, 3,2,1,
4,5,7, //rechts (1) 4,5,7, //rechts (1)
@ -94,41 +96,48 @@ void GeometryEngine::initCubeGeometry()
19,18,17, 19,18,17,
23,22,20, //oben (2)* 23,22,20, //oben (2)*
23,20,21 23,20,21
}; };
// Transfer vertex data to VBO 0 // Transfer vertex data to VBO 0
arrayBuf.bind(); m_arrayBuf.bind();
arrayBuf.allocate(vertices, 24 * sizeof(VertexData)); m_arrayBuf.allocate(vertices.data(), vertices.size() * sizeof(VertexData));
// Transfer index data to VBO 1 // Transfer index data to VBO 1
indexBuf.bind(); m_indexBuf.bind();
indexBuf.allocate(indices, 36 * sizeof(GLushort)); m_indexBuf.allocate(indices.data(), indices.size() * sizeof(GLushort));
// load the texture
initTexture(); initTexture();
} }
void GeometryEngine::initTexture() void GeometryEngine::initTexture()
{ {
// Load cube.png image // Load cube.png image
texture = new QOpenGLTexture(QImage(":images/cube.png").mirrored()); QOpenGLTexture* new_texture = new QOpenGLTexture(QImage(":images/cube.png").mirrored());
// Set nearest filtering mode for texture minification // Set nearest filtering mode for texture minification
texture->setMinificationFilter(QOpenGLTexture::Nearest); new_texture->setMinificationFilter(QOpenGLTexture::Nearest);
// Set bilinear filtering mode for texture magnification // Set bilinear filtering mode for texture magnification
texture->setMagnificationFilter(QOpenGLTexture::Linear); new_texture->setMagnificationFilter(QOpenGLTexture::Linear);
// Wrap texture coordinates by repeating // Wrap texture coordinates by repeating
// f.ex. texture coordinate (1.1, 1.2) is same as (0.1, 0.2) // f.ex. texture coordinate (1.1, 1.2) is same as (0.1, 0.2)
texture->setWrapMode(QOpenGLTexture::Repeat); new_texture->setWrapMode(QOpenGLTexture::Repeat);
m_textures.push_back(new_texture);
} }
/////////////////////////////////////////////////////////////////////////
// public functions
void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program) void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program)
{ {
// Tell OpenGL which VBOs to use // Tell OpenGL which VBOs to use
arrayBuf.bind(); m_arrayBuf.bind();
indexBuf.bind(); m_indexBuf.bind();
texture->bind(); m_textures.first()->bind();
// Use texture unit 0 which contains cube.png // Use texture unit 0 which contains cube.png
program->setUniformValue("texture", 0); program->setUniformValue("texture", 0);

View File

@ -3,6 +3,10 @@
#include <QMouseEvent> #include <QMouseEvent>
#include <math.h> #include <math.h>
/////////////////////////////////////////////////////////////////////////
// public constructor/destructor
OglViewerWidget::OglViewerWidget(QWidget *parent) : OglViewerWidget::OglViewerWidget(QWidget *parent) :
QOpenGLWidget(parent), QOpenGLWidget(parent),
m_dataEngine(0) m_dataEngine(0)
@ -19,6 +23,10 @@ OglViewerWidget::~OglViewerWidget()
doneCurrent(); doneCurrent();
} }
/////////////////////////////////////////////////////////////////////////
// protected functions
void OglViewerWidget::mousePressEvent(QMouseEvent *e) void OglViewerWidget::mousePressEvent(QMouseEvent *e)
{ {
// Save mouse press position // Save mouse press position
@ -83,25 +91,6 @@ void OglViewerWidget::initializeGL()
} }
void OglViewerWidget::initShaders()
{
// Compile vertex shader
if (!m_program.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/vshader.glsl"))
close();
// Compile fragment shader
if (!m_program.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/fshader.glsl"))
close();
// Link shader pipeline
if (!m_program.link())
close();
// Bind shader pipeline for use
if (!m_program.bind())
close();
}
void OglViewerWidget::resizeGL(int w, int h) void OglViewerWidget::resizeGL(int w, int h)
{ {
// Calculate aspect ratio // Calculate aspect ratio
@ -133,3 +122,30 @@ void OglViewerWidget::paintGL()
// Draw cube geometry // Draw cube geometry
m_dataEngine->drawGeometry(&m_program); m_dataEngine->drawGeometry(&m_program);
} }
/////////////////////////////////////////////////////////////////////////
// private functions
void OglViewerWidget::initShaders()
{
// Compile vertex shader
if (!m_program.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/vshader.glsl"))
close();
// Compile fragment shader
if (!m_program.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/fshader.glsl"))
close();
// Link shader pipeline
if (!m_program.link())
close();
// Bind shader pipeline for use
if (!m_program.bind())
close();
}
/////////////////////////////////////////////////////////////////////////
// public functions