我在opengl方面没有太多经验,所以如果问题很愚蠢,请原谅。
我试图通过在我的类 glView 中翻译 Qmatrix4x4 字段来使用 mouseMoveEvent 移动三角形。当我什么都没做(透视,用矩阵翻译)并且只是调用m_program->setUniformValue(m_matrixUniform, m_matrix)
一切正常时,但如果我只是这样做this->m_matrix=matrix;
(矩阵只是 Qmatrix4x4 变量)然后m_program->setUniformValue(m_matrixUniform, m_matrix)
方法不起作用。
在我看来,我在更新小部件(我只是调用 update())或所有着色器逻辑方面都有一些问题。谢谢!
//Here is glView.h
#ifndef GLVIEW_H
#define GLVIEW_H
#include <QtOpenGL>
#include <QGLWidget>
#include <QOpenGLWidget>
#include <QtGui/QWindow>
#include <QtGui/QOpenGLFunctions>
#include <QMouseEvent>
//Class for drawing triangles
class glView : public QOpenGLWidget, public QOpenGLFunctions
{
public:
glView(float left, float right, float bot, float top, float minz, float maxz)
: QOpenGLWidget(), QOpenGLFunctions(), f_left(left), f_right(right), f_bot(bot), f_top(top), f_minz(minz), f_maxz(maxz)
{}
virtual~glView()
{}
void initializeGL() override;
void resizeGL(int w, int h)override;
void paintGL()override;
void mousePressEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
void keyPressEvent(QKeyEvent* pe) Q_DECL_OVERRIDE;
private:
struct CFace{
int v1,v2,v3;
};
GLuint m_posAttr;
GLuint m_colAttr;
GLuint m_matrixUniform;
QOpenGLShaderProgram *m_program;
int m_frame = 0;
QMatrix4x4 m_matrix;
QPoint mPrevMousePos;
};
#endif // GLVIEW_H
和cpp
//Here is glView.cpp
#include <qopengl.h>
#include "glView.h"
#include <QtGui/QGuiApplication>
#include <QtGui/QMatrix4x4>
#include <QtGui/QOpenGLShaderProgram>
#include <QMouseEvent>
static const char *vertexShaderSource =
"attribute highp vec4 posAttr;\n"
"attribute lowp vec4 colAttr;\n"
"varying lowp vec4 col;\n"
"uniform highp mat4 matrix;\n"
"void main() {\n"
" col = colAttr;\n"
" gl_Position = matrix*posAttr;\n"
"}\n";
static const char *fragmentShaderSource =
"varying lowp vec4 col;\n"
"void main() {\n"
" gl_FragColor = col;\n"
"}\n";
//------------------------------------------------------------------------------
void glView::initializeGL()
{
m_program = new QOpenGLShaderProgram(this);
m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
m_program->link();
m_posAttr = m_program->attributeLocation("posAttr");
m_colAttr = m_program->attributeLocation("colAttr");
m_matrixUniform = m_program->uniformLocation("matrix");
}
//------------------------------------------------------------------------------
void glView::paintGL()
{
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
const qreal retinaScale = devicePixelRatio();
f->glViewport(0, 0, width() * retinaScale, height() * retinaScale);
f->glClear(GL_COLOR_BUFFER_BIT);
m_program->bind();
QMatrix4x4 matrix;
//Here is the problem!
//matrix.perspective(0.0f, 4.0f/3.0f, 0.1f, 100.0f);
//matrix.translate(-5, -5, 0);
//???
this->m_matrix=matrix;
m_program->setUniformValue(m_matrixUniform, m_matrix);
GLfloat vertices[] = {
0.0f, 0.707f,
-0.5f, -0.5f,
0.5f, -0.5f
};
GLfloat colors[] = {
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f
};
f->glVertexAttribPointer(m_posAttr, 2, GL_FLOAT, GL_FALSE, 0, vertices);
f->glVertexAttribPointer(m_colAttr, 3, GL_FLOAT, GL_FALSE, 0, colors);
f->glEnableVertexAttribArray(0);
f->glEnableVertexAttribArray(1);
f->glDrawArrays(GL_TRIANGLES, 0, 3);
f->glDisableVertexAttribArray(1);
f->glDisableVertexAttribArray(0);
m_program->release();
++m_frame;
}
void glView::mousePressEvent(QMouseEvent *event)
{
mPrevMousePos = event->pos();
QOpenGLWidget::mousePressEvent(event);
}
void glView::mouseMoveEvent(QMouseEvent *event)
{
auto diff = event->pos() - mPrevMousePos;
//??
this->m_matrix.translate(diff.x()/(width()*0.5), -diff.y()/(height()*0.5));
mPrevMousePos = event->pos();
//?? m_program->setUniformValue(m_matrixUniform, m_matrix);
update();
QOpenGLWidget::mouseMoveEvent(event);
}
void glView::mouseReleaseEvent(QMouseEvent *event)
{
QOpenGLWidget::mouseReleaseEvent(event);
}
void glView::keyPressEvent(QKeyEvent *pe)
{
switch (pe->key())
{
case Qt::Key_Up:
this->m_matrix.scale(QVector3D(2.0f,2.0f,1.0f));
break;
case Qt::Key_Down:
this->m_matrix.translate(QVector3D(0,-1,0));
break;
}
update();
QOpenGLWidget::keyPressEvent(pe); // Передать событие дальше
}