开发者

Strange behavior with QT and OpenGL

When trying to draw on an OpenGL widget with QT, the window display ends up rather distorted. All that's being done is the screen is cleared.

Strange behavior with QT and OpenGL

#include "glwidget.h"

// Mainwidget is a sub-class of GLWidget

GLWidget::GLWidget(QWidget *parent) :
    QGLWidget(parent)
{
}

void GLWidget::resizeGL(int width,int height)
{
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, width, height, 0, -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}

GLWidget::~GLWidget()
{
    ;
}

#include <QtGui>
#include "mainwidget.h"

MainWidget::MainWidget()
{
    //this->showFullScreen();
    derp = 100;
}

void MainWidget::paintGL()
{
    glClearColor(0, 0, 0, 0);
    //drawTriangl开发者_如何学Pythone(0, 0, 100, 100, derp, 0);
    derp = rand()%500;
}

void MainWidget::initializeGL()
{
    this->resizeGL(800, 600);
}

void MainWidget::drawTriangle(int x1,int y1, int x2, int y2, int x3, int y3)
{
    glBegin(GL_TRIANGLES);
    glVertex3f(x1, y1, 0.0f);
    glVertex3f(x2, y2, 0.0f);
    glVertex3f(x3, y3, 0.0f);
glEnd();
}

#include <QtGui>
#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QWidget(parent)
{
    QGridLayout *layout = new QGridLayout;
    QTimer *timer = new QTimer(this);
    MainWidget *View = new MainWidget();
    layout->addWidget(View, 0, 0);
    setLayout(layout);
    connect(timer, SIGNAL(timeout()), View, SLOT(paintGL()));
    timer->start(50);
}


Try clearing the color buffer occasionally. Note glClearColor() just latches some state and doesn't actually clear any buffers.


I guess, the backend is receiving garbage into the draw buffers. Using glClear might help

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜