简体   繁体   English

使用OpenGL和QT进行纹理映射 - C ++

[英]Texture mapping with OpenGL and QT - C++

I'm having trouble getting my texture to map to a quad using OpenGL and Qt. 我无法使用OpenGL和Qt将纹理映射到四边形。 I've looked at several other SO threads, but a lot of the function calls have to be used slightly differently for me to compile (Qt Verison 4.8.6). 我已经查看了其他几个SO线程,但是很多函数调用必须以稍微不同的方式用于编译(Qt Verison 4.8.6)。 Here's my relevant code, right now all that happens is a window is displayed with a black background, but nothing else. 这是我的相关代码,现在所发生的一切都是一个带有黑色背景的窗口,但没有别的。

void LoadGLTextures( const char * name )
{
    QImage img;

    if(!img.load("resources/Green_Dragon.bmp")){
        std::cerr << "ERROR in loading image" << std::endl;
    }

    QImage t = QGLWidget::convertToGLFormat(img);

    glGenTextures(1, &texture[0]);
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glTexImage2D(GL_TEXTURE_2D, 0, 3, t.width(), t.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, t.bits());

    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    glBindTexture( GL_TEXTURE_2D, 0 );
}



void GLWidget::initializeGL()
{


    qglClearColor(qtBlack.dark());
    glEnable(GL_CULL_FACE);
    glShadeModel(GL_SMOOTH);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_MULTISAMPLE);
    static GLfloat lightPosition[4] = { 0.5, 5.0, 7.0, 1.0 };
    glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
    cameraPos = 0;
    glEnable(GL_TEXTURE_2D);
    LoadGLTextures("resources/Green_Dragon.jpeg");

}

void GLWidget::paintGL()
{
    glClearColor( 0.0f, 0.0f, 0.0f, 0.0f);
    glClear( GL_COLOR_BUFFER_BIT );


    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    glShadeModel( GL_FLAT );
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
    glColor3f(0.5, 0.5, 0);
    glBindTexture(GL_TEXTURE_2D, texture[0]);

    glBegin(GL_QUADS);
    glTexCoord2f(0.0f, 1.0f); glVertex2f(-0.5f, 0.5f);  // vertex 1
    glTexCoord2f(0.0f, 0.0f); glVertex2f(-0.5f, -0.5f); // vertex 2
    glTexCoord2f(1.0f, 0.0f); glVertex2f(0.5f, -0.5f); // vertex 3
    glTexCoord2f(1.0f, 1.0f); glVertex2f(0.5f, 0.5f); // vertex 4
    glEnd();
    glDisable(GL_TEXTURE_2D);

    glFlush();
}

Looking at your comment, the answer is fairly clear now, sorry that I missed it in the question comments. 看看你的评论,答案现在已经相当清楚了,很抱歉我在问题评论中错过了它。

You are seeing a black screen because you are enabling depth testing, but you are not clearing the depth buffer between frames. 您正在看黑屏,因为您正在启用深度测试,但您没有清除帧之间的深度缓冲区。 Therefore, the previous frame's depth buffer values remain in place, and the depth tests fail for all subsequent frames (note that the default depth function is GL_LESS ). 因此,前一帧的深度缓冲区值保持不变,并且所有后续帧的深度测试都会失败(请注意, 默认深度函数为GL_LESS )。

You may leave depth testing enabled. 您可以启用深度测试。 The correct solution is to clear your depth buffer in addition to your color buffer before each render. 正确的解决方案是在每次渲染之前清除深度缓冲区以及颜色缓冲区。 You have: 你有:

glClear( GL_COLOR_BUFFER_BIT );

But you need: 但你需要:

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

See also: glClear() 另请参见: glClear()

删除glEnable(GL_DEPTH_TEST)解决了这个问题,以防万一遇到类似的问题。

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

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