简体   繁体   中英

OpenGL glitch with Qt (and not with GLUT)

I was trying some simple (and old) opengl tutorial from this website . Compiling them in gcc works nice. Now I've tried to put them where i really need: in a Qt Application inside a QGLWidget. And.. I have some glitch (colored pixels, the image should be all black and white). Don't know why.

See the attached images:

在此输入图像描述

I'm on a Mac OS/X 10.8, Qt 4.8 with QtCreator, and I'm compiling the basic GLUT tutorial with:

gcc -o test -Wall -W test.c -framework GLUT -framework OpenGL

The program is mainly the same, a part from replacing some GLUT vs Qt.. anyway, here is the cpp file:

#include "glwidget.h"

#include <QTimer>
#include <iostream>

GLWidget::GLWidget(QWidget *parent) :
    QGLWidget(parent)
{
    QTimer *t = new QTimer(this);
    connect(t, SIGNAL(timeout()), this, SLOT(update()));
    t->start(1000.0f/30.0f); // 30 FPS
}

void GLWidget::initializeGL()
{
    glShadeModel(GL_SMOOTH);

    glClearColor(1, 1, 1, 0);

    glClearDepth(1);
    glEnable(GL_DEPTH_TEST);

    glMatrixMode(GL_MODELVIEW);

    //glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    //glEnable(GL_LINE_SMOOTH);
    //glEnable(GL_POINT_SMOOTH);

    glLoadIdentity();

    glEnable(GL_TEXTURE_2D);
    glGenTextures(1, &texture_id);
    glBindTexture(GL_TEXTURE_2D, texture_id);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, SIZE, SIZE, 0, GL_RGB,
                 GL_UNSIGNED_BYTE, texture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

}

void GLWidget::resizeGL(int w, int h)
{
    h = h ? h : 1;

    glViewport(0, 0, w,h);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0f, (GLfloat)w/(GLfloat)h, 0.1, 8000.0f);
    glMatrixMode(GL_MODELVIEW);
}

float rotation=20;
void GLWidget::paintGL()
{
    makeCurrent();

    //glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
    //glLoadIdentity();

    glLoadIdentity();
    glTranslatef(0, 0, -10);
    glRotatef(rotation, 1, 0, 0);
    glRotatef(20   , 0, 1, 0);

    /* Define a view-port adapted to the texture */
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(20, 1, 5, 15);
    glViewport(0, 0, SIZE, SIZE);
    glMatrixMode(GL_MODELVIEW);

    /* Render to buffer */
    glClearColor(1, 1, 1, 0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    Cube();
    glFlush();

    /* Copy buffer to texture */
    glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 5, 5, 0, 0, SIZE - 10, SIZE - 10);

    /* Render to screen */
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(20, width() / (float) height(), 5, 15);
    glViewport(0, 0, width(), height());
    glMatrixMode(GL_MODELVIEW);
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    Cube();

    /* End */
    glFlush();

    /* Update again and again */
    rotation += 0.1;
}

void GLWidget::Cube()
{
    glBegin(GL_QUADS);

    glTexCoord2i(0, 0); glVertex3f(-1, -1, -1);
    glTexCoord2i(0, 1); glVertex3f(-1, -1,  1);
    glTexCoord2i(1, 1); glVertex3f(-1,  1,  1);
    glTexCoord2i(1, 0); glVertex3f(-1,  1, -1);

    glTexCoord2i(0, 0); glVertex3f( 1, -1, -1);
    glTexCoord2i(0, 1); glVertex3f( 1, -1,  1);
    glTexCoord2i(1, 1); glVertex3f( 1,  1,  1);
    glTexCoord2i(1, 0); glVertex3f( 1,  1, -1);

    glTexCoord2i(0, 0); glVertex3f(-1, -1, -1);
    glTexCoord2i(0, 1); glVertex3f(-1, -1,  1);
    glTexCoord2i(1, 1); glVertex3f( 1, -1,  1);
    glTexCoord2i(1, 0); glVertex3f( 1, -1, -1);

    glTexCoord2i(0, 0); glVertex3f(-1,  1, -1);
    glTexCoord2i(0, 1); glVertex3f(-1,  1,  1);
    glTexCoord2i(1, 1); glVertex3f( 1,  1,  1);
    glTexCoord2i(1, 0); glVertex3f( 1,  1, -1);

    glTexCoord2i(0, 0); glVertex3f(-1, -1, -1);
    glTexCoord2i(0, 1); glVertex3f(-1,  1, -1);
    glTexCoord2i(1, 1); glVertex3f( 1,  1, -1);
    glTexCoord2i(1, 0); glVertex3f( 1, -1, -1);

    glTexCoord2i(0, 0); glVertex3f(-1, -1,  1);
    glTexCoord2i(0, 1); glVertex3f(-1,  1,  1);
    glTexCoord2i(1, 1); glVertex3f( 1,  1,  1);
    glTexCoord2i(1, 0); glVertex3f( 1, -1,  1);

    glEnd();
}

ps. the same Qt program under Ubuntu 12.04 has no problem

I did see that already in the code you posted in your other question:

 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 5, 5, 0, 0, SIZE - 10, SIZE - 10); ^^^^ ^^^^ 

There's a border of 10 pixels left undefined on either side. In that demo it was done to give the texture some border, but technically it will leave anything already present in the texture untouched.

Now since you're likely initializing your texture with some undefined array data (I don't see texture passed as data parameter to glTexImage2D being initialized) this will create some garbage. BTW: You can safely initialize a texture by passing a null pointer to glTexImage2D's data parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM