简体   繁体   English

使用Qt的OpenGL故障(而不是GLUT)

[英]OpenGL glitch with Qt (and not with GLUT)

I was trying some simple (and old) opengl tutorial from this website . 我在这个网站上尝试了一些简单(和旧)的opengl教程。 Compiling them in gcc works nice. 在gcc中编译它们很好。 Now I've tried to put them where i really need: in a Qt Application inside a QGLWidget. 现在我已经尝试将它们放在我真正需要的地方:在QGLWidget中的Qt应用程序中。 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: 我在Mac OS / X 10.8,Qt 4.8和QtCreator,我正在编译基本的GLUT教程:

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: 该程序主要是相同的,一部分来自替换一些GLUT与Qt ..无论如何,这里是cpp文件:

#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. PS。 the same Qt program under Ubuntu 12.04 has no problem Ubuntu 12.04下的同一Qt程序没问题

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. 两边都有一个10像素的边框未定义。 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. 现在,因为您可能使用一些未定义的数组数据初始化纹理(我没有看到texture作为数据参数传递给glTexImage2D进行初始化),这将产生一些垃圾。 BTW: You can safely initialize a texture by passing a null pointer to glTexImage2D's data parameter. 顺便说一句:您可以通过将空指针传递给glTexImage2D的数据参数来安全地初始化纹理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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