【问题标题】:OpenGL 1.x Displaying GL_QUADS with texture works, solid colour doesn'tOpenGL 1.x 显示带有纹理的 GL_QUADS 有效,纯色无效
【发布时间】:2012-06-04 06:44:16
【问题描述】:

首先,我的 OpenGL 信息(我在运行时获取的):

OpenGL 版本=4.0.10243 兼容性配置文件上下文,供应商=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:看起来当我不做任何带纹理的四边形时,它可以工作。有没有人看到绑定函数中可能破坏它的任何部分?

【问题讨论】:

  • 你在哪里设置你的纹理环境(glTexEnv)的东西?您在哪里激活和停用纹理?

标签: c++ opengl sdl


【解决方案1】:

所以,我只需要在每次需要时添加一些启用和禁用 GL_TEXTURE_2D 的逻辑,而不是仅仅将其绑定在 opengl 状态业务中。

【讨论】:

  • 我会建议这个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多