OpenGL 1.x使用纹理显示GL_QUADS,纯色不显示

时间:2012-06-03 05:27:16

标签: c++ opengl sdl

首先,我的OpenGL信息(我在运行时抓住了它):

  

OpenGL version = 4.0.10243兼容性配置文件上下文,vendor = ATI   Technologies Inc.,渲染器= AMD Radeon HD 6650M

所以我正在为一些我正在研究的项目开发一个gui / hud渲染系统,我遇到了一个有趣的问题。我可以使用以下代码显示纹理GL_QUADS而不会出现问题:

this->m_buffer->openglBindTexture();

SDL::Rect tc( this->m_buffer->getOpenGLTextureCoords() );           

glBegin( GL_QUADS );

    glColor4fv( (SDL::Color("white")).toGLColor4vf( 1.0 ) );
    glTexCoord2f( tc.topleft().x, tc.topleft().y );
    glVertex3f( this->boundary().bottomleft().x, this->boundary().bottomleft().y, this->m_z );

    glTexCoord2f( tc.topright().x, tc.topright().y );
    glVertex3f( this->boundary().bottomright().x, this->boundary().bottomright().y, this->m_z );

    glTexCoord2f( tc.bottomright().x, tc.bottomright().y );
    glVertex3f( this->boundary().topright().x, this->boundary().topright().y, this->m_z );

    glTexCoord2f( tc.bottomleft().x, tc.bottomleft().y );
    glVertex3f( this->boundary().topleft().x, this->boundary().topleft().y, this->m_z );

glEnd();

其中一些使用我自己的围绕SDL + OpenGL的包装器代码。这是我的绑定代码,以防有人抓住它的怪癖:

void Surface::openglBindTexture( void )
{
    int glerror = 0;
    if( !this->m_hastexture ) {
        int bpp;
        Uint32 Rmask, Gmask, Bmask, Amask;
        SDL_PixelFormatEnumToMasks(
            SDL_PIXELFORMAT_ABGR8888, &bpp,
            &Rmask, &Gmask, &Bmask, &Amask
        );

        int glw = this->w_pow2();
        int glh = this->h_pow2();

        /* Create surface that will hold pixels passed into OpenGL. */
        SDL_Surface *img_rgba8888 = SDL_CreateRGBSurface(0,
            glw, glh, bpp,
            Rmask, Gmask, Bmask, Amask
        );

        SDL_SetSurfaceAlphaMod( this->m_surface, 0xFF );
        SDL_SetSurfaceBlendMode( this->m_surface, SDL_BLENDMODE_NONE );

        SDL_BlitSurface( this->m_surface, NULL, img_rgba8888, NULL );

        glGenTextures( 1, &this->m_texture );
        glBindTexture( GL_TEXTURE_2D, this->m_texture );
        glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, glw, glh, 0, GL_RGBA, GL_UNSIGNED_BYTE, img_rgba8888->pixels );
        glerror = GLException::glError(); if( glerror != 0 ) throw new GLException( "Surface::openglBindTexture::glTexImage2D", glerror, __FILE__, __LINE__ );

SDL_FreeSurface(img_rgba8888);
                glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // Linear Filtering
                glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // Linear Filtering
        glerror = GLException::glError();
        if( glerror != 0 ) throw new GLException( "Surface::openglBindTexture::Importing raw texture", glerror, __FILE__, __LINE__ );
        this->m_hastexture = true;
    }
    glBindTexture( GL_TEXTURE_2D, this->m_texture );
}

这是我试图用来在opengl中显示一个简单的彩色四边形的代码:

glBegin( GL_QUADS );
    glColor3f( 1.0f, 1.0f, 1.0f );
    glVertex3f( this->boundary().topleft().x, this->boundary().topleft().y, this->m_z );
    glColor3f( 1.0f, 1.0f, 1.0f );
    glVertex3f( this->boundary().topright().x, this->boundary().topright().y, this->m_z );
    glColor3f( 1.0f, 1.0f, 1.0f );
    glVertex3f( this->boundary().bottomright().x, this->boundary().bottomright().y, this->m_z );
    glColor3f( 1.0f, 1.0f, 1.0f );
    glVertex3f( this->boundary().bottomleft().x, this->boundary().bottomleft().y, this->m_z );
glEnd();

这没有任何吸引力。我完全难过,因为我可以看到所有纹理四边形没有问题。

编辑:我应该注意,我做了一些完整性检查,边界坐标都正确且可见。

编辑2 :看起来当我不做任何纹理四边形时,它可以正常工作。有没有人看到绑定函数中的任何部分可能会破坏它?

1 个答案:

答案 0 :(得分:1)

所以,事实证明我只是需要添加一些逻辑来在每次需要时启用和禁用GL_TEXTURE_2D,而不是仅仅将其保持在opengl状态业务中。