c++ - OpenGL 1.x 显示带有纹理的 GL_QUADS,纯色不

标签 c++ opengl sdl

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

OpenGL version=4.0.10243 Compatibility Profile Context, vendor=ATI Technologies Inc., renderer=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 包装器代码。这是我的绑定(bind)代码,以防万一有人发现它的怪癖:

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:看起来当我不做任何带纹理的四边形时,它就起作用了。有没有人看到绑定(bind)函数中可能破坏它的任何部分?

最佳答案

因此,事实证明我只需要在每次需要时添加一些逻辑来启用和禁用 GL_TEXTURE_2D,而不是仅仅将其绑定(bind)在 opengl 状态业务中。

关于c++ - OpenGL 1.x 显示带有纹理的 GL_QUADS,纯色不,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10868112/

相关文章:

c++ - 关于 C++ 中的 posix ftime

c++ - 为什么我不能移动我的 3D 立方体? OpenGL

java - 需要有关实现主飞行显示器的指导

c++ - SDL_PollEvent() 空闲时口吃?

linux - 64 位 Linux 上的 SDL 和 nasm

c++ - eclipse 和 boost unit_test_framework 使用 c++ 进行语法检查失败

c# - 如何调用 XML api

opengl - 为什么相机必须留在opengl的原点

c++ - winapifamily.h : No such file or directory

c++ - 是否有标准机制来检索 C 字符串的哈希值?