当我运行我的应用程序时,矩形的颜色为红色(这没关系),但是当我移动或调整窗口大小时,颜色会变为白色。 我不明白为什么会出现这种行为。
顶点着色器
attribute vec3 position;
void main(void) {
gl_Position = gl_ModelViewProjectionMatrix * vec4(position, 1.0);
}
片段着色器
void main(void) {
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
来自glwindow.cpp的主渲染功能
void GLWindow::render(QPainter *painter)
{
Q_UNUSED(painter);
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
GLfloat vertexes[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, 0.5f, 0.0,
0.5f, 0.5f, 0.0f
};
glTranslatef(0.0f, 0.0f, -1.00f);
unsigned int va;
glGenBuffers(1, &va);
glBindBuffer(GL_ARRAY_BUFFER, va);
glEnableVertexAttribArray(0);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexes), vertexes, GL_STATIC_DRAW);
glVertexAttribPointer(program->attributeLocation("position"), 3, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_TRIANGLE_STRIP, 0, sizeof(vertexes)/(3*sizeof(GLfloat)));
glDisableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDeleteBuffers(1, &va);
}
glwindow.cpp (http://qt-project.org/doc/qt-5/qtgui-openglwindow-example.html)
#include "glwindow.h"
#include <iostream>
#include <QApplication>
using namespace std;
GLWindow::GLWindow(QObject *parent)
: QWindow((QWindow *)parent)
, m_update_pending(false)
, m_animating(false)
, m_context(0)
, m_device(0)
{
setWidth(640);
setHeight(480);
setTitle("OpenGL window");
setSurfaceType(QWindow::OpenGLSurface);
}
void GLWindow::render(QPainter *painter)
{
Q_UNUSED(painter);
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
GLfloat vertexes[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, 0.5f, 0.0,
0.5f, 0.5f, 0.0f
};
glTranslatef(0.0f, 0.0f, -1.00f);
unsigned int va;
glGenBuffers(0, &va);
glBindBuffer(GL_ARRAY_BUFFER, va);
glEnableVertexAttribArray(0);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexes), vertexes, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_TRIANGLE_STRIP, 0, sizeof(vertexes)/(3*sizeof(GLfloat)));
glDisableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDeleteBuffers(1, &va);
}
void GLWindow::initialize()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double aspect = (double)width()/height();
glFrustum(-1.0, 1.0, -(2/(2*aspect)), (2/(2*aspect)), 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
program = new QOpenGLShaderProgram(m_context);
program->addShaderFromSourceFile(QOpenGLShader::Vertex, "/Users/Krab/projects/qtProjects/gl/vertex.vert");
program->addShaderFromSourceFile(QOpenGLShader::Fragment, "/Users/Krab/projects/qtProjects/gl/fragment.frag");
program->link();
program->bind();
}
void GLWindow::render()
{
if (!m_device)
m_device = new QOpenGLPaintDevice;
m_device->setSize(size());
QPainter painter(m_device);
render(&painter);
}
void GLWindow::renderLater()
{
if (!m_update_pending) {
m_update_pending = true;
QApplication::postEvent(this, new QEvent(QEvent::UpdateRequest));
}
}
bool GLWindow::event(QEvent *event)
{
switch (event->type()) {
case QEvent::UpdateRequest:
m_update_pending = false;
renderNow();
return true;
default:
return QWindow::event(event);
}
}
void GLWindow::exposeEvent(QExposeEvent *event)
{
Q_UNUSED(event);
if (isExposed())
renderNow();
}
void GLWindow::renderNow()
{
if (!isExposed())
return;
bool needsInitialize = false;
if (!m_context) {
m_context = new QOpenGLContext(this);
m_context->setFormat(requestedFormat());
m_context->create();
needsInitialize = true;
}
m_context->makeCurrent(this);
if (needsInitialize) {
initializeOpenGLFunctions();
initialize();
}
render();
m_context->swapBuffers(this);
if (m_animating)
renderLater();
}
void GLWindow::setAnimating(bool animating)
{
m_animating = animating;
if (animating)
renderLater();
}
GLWindow::~GLWindow() {
}