move texture to GeometryEngine
This commit is contained in:
@@ -2,12 +2,10 @@
|
||||
|
||||
#include <QMouseEvent>
|
||||
#include <math.h>
|
||||
#include <iostream>
|
||||
|
||||
OglViewerWidget::OglViewerWidget(QWidget *parent) :
|
||||
QOpenGLWidget(parent),
|
||||
geometries(0),
|
||||
texture(0)
|
||||
m_dataEngine(0)
|
||||
{
|
||||
setFocus();
|
||||
}
|
||||
@@ -17,8 +15,7 @@ OglViewerWidget::~OglViewerWidget()
|
||||
// Make sure the context is current when deleting the texture
|
||||
// and the buffers.
|
||||
makeCurrent();
|
||||
delete texture;
|
||||
delete geometries;
|
||||
delete m_dataEngine;
|
||||
doneCurrent();
|
||||
}
|
||||
|
||||
@@ -75,7 +72,6 @@ void OglViewerWidget::initializeGL()
|
||||
glClearColor(0, 0, 0, 1);
|
||||
|
||||
initShaders();
|
||||
initTextures();
|
||||
|
||||
// Enable depth buffer
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
@@ -83,45 +79,29 @@ void OglViewerWidget::initializeGL()
|
||||
// Enable back face culling
|
||||
glEnable(GL_CULL_FACE);
|
||||
|
||||
geometries = new GeometryEngine;
|
||||
m_dataEngine = new GeometryEngine;
|
||||
|
||||
}
|
||||
|
||||
void OglViewerWidget::initShaders()
|
||||
{
|
||||
// Compile vertex shader
|
||||
if (!program.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/vshader.glsl"))
|
||||
if (!m_program.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/vshader.glsl"))
|
||||
close();
|
||||
|
||||
// Compile fragment shader
|
||||
if (!program.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/fshader.glsl"))
|
||||
if (!m_program.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/fshader.glsl"))
|
||||
close();
|
||||
|
||||
// Link shader pipeline
|
||||
if (!program.link())
|
||||
if (!m_program.link())
|
||||
close();
|
||||
|
||||
// Bind shader pipeline for use
|
||||
if (!program.bind())
|
||||
if (!m_program.bind())
|
||||
close();
|
||||
}
|
||||
|
||||
void OglViewerWidget::initTextures()
|
||||
{
|
||||
// Load cube.png image
|
||||
texture = new QOpenGLTexture(QImage(":images/cube.png").mirrored());
|
||||
|
||||
// Set nearest filtering mode for texture minification
|
||||
texture->setMinificationFilter(QOpenGLTexture::Nearest);
|
||||
|
||||
// Set bilinear filtering mode for texture magnification
|
||||
texture->setMagnificationFilter(QOpenGLTexture::Linear);
|
||||
|
||||
// Wrap texture coordinates by repeating
|
||||
// f.ex. texture coordinate (1.1, 1.2) is same as (0.1, 0.2)
|
||||
texture->setWrapMode(QOpenGLTexture::Repeat);
|
||||
}
|
||||
|
||||
void OglViewerWidget::resizeGL(int w, int h)
|
||||
{
|
||||
// Calculate aspect ratio
|
||||
@@ -142,19 +122,14 @@ void OglViewerWidget::paintGL()
|
||||
// Clear color and depth buffer
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
texture->bind();
|
||||
|
||||
// Calculate model view transformation
|
||||
QMatrix4x4 matrix;
|
||||
matrix.translate(0.0, 0.0, -5.0);
|
||||
matrix.rotate(m_rotation);
|
||||
QMatrix4x4 view;
|
||||
view.translate(0.0, 0.0, -5.0);
|
||||
view.rotate(m_rotation);
|
||||
|
||||
// Set modelview-projection matrix
|
||||
program.setUniformValue("mvp_matrix", m_projection * matrix);
|
||||
|
||||
// Use texture unit 0 which contains cube.png
|
||||
program.setUniformValue("texture", 0);
|
||||
m_program.setUniformValue("mvp_matrix", m_projection * view);
|
||||
|
||||
// Draw cube geometry
|
||||
geometries->drawCubeGeometry(&program);
|
||||
m_dataEngine->drawGeometry(&m_program);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user