微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – OpenGL纹理形式SDLSurface太暗了

我的问题与这个 OpenGl goes too dark完全相同,但答案对我不起作用.我试图通过转换为纹理的表面来显示图像,结果太黑了:

原版的:

在openGL之后

左边是原版,右边是OpenGl img.

这是我的代码

void TexturedRect::draw(int scroll){

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glBindTexture(GL_TEXTURE_2D,_texture);

    glEnable(GL_TEXTURE_2D);
    glBegin(GL_QUADS);  //Begining the cube's drawing

    int x = this->getX();
    int y = this->getY();
    int w = this->getWidth();
    int h = this->getHeight();
    int z = this->getZ();

    /*
    (0,0) ------ (1,0)
      |            |
      |            |
    (0,1) ------ (1,1)
    */
    glTexCoord3i(0,1);glVertex3i(x + scroll,y,z);
    glTexCoord3i(_tv,1);glVertex3i(x + w * _tv + scroll,_tu,y + h * _tu,z);
    glTexCoord3i(0,z);

    glEnd();
    gldisable(GL_TEXTURE_2D);
}

void TexturedRect::createTextureFromSurface()
{
    SDL_Surface * surface = IMG_Load(filename.toStdString().c_str());
    // get the number of channels in the SDL surface
    GLint  nbOfColors = surface->format->BytesPerPixel;
    GLenum textureFormat = 0;

    switch (nbOfColors) {
    case 1:
        textureFormat = GL_ALPHA;
        break;
    case 3:     // no alpha channel
        if (surface->format->Rmask == 0x000000ff)
            textureFormat = GL_RGB;
        else
            textureFormat = GL_BGR;
        break;
    case 4:     // contains an alpha channel
        if (surface->format->Rmask == 0x000000ff)
            textureFormat = GL_RGBA;
        else
            textureFormat = GL_BGRA;
        break;
    default:
        qDebug() << "Warning: the image is not truecolor...";
        break;
    }

    glEnable( GL_TEXTURE_2D );
    // Have OpenGL generate a texture object handle for us
    glGenTextures( 1,&_texture );

    // Bind the texture object
    glBindTexture( GL_TEXTURE_2D,_texture );


    // Edit the texture object's image data using the information SDL_Surface gives us
    glTexImage2D( GL_TEXTURE_2D,nbOfColors,surface->w,surface->h,textureFormat,GL_UNSIGNED_BYTE,surface->pixels );

    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);

}

解决方法

您可能在代码中的其他位置设置了一些状态,当您希望禁用它来绘制此四边形时,该状态仍然处于启用状态.

尝试在glBindTexture(GL_TEXTURE_2D,_texture)之后添加以下内容;在你的绘图代码中(重要的是它在draw方法中完成而不是createTextureFromSurface方法):

gldisable(GL_BLEND);
gldisable(GL_LIGHTING);
glTexEnvi( GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE )
glColor3f(1.0f,1.0f,1.0f);

如果这样可行,您可以逐个注释掉它们,以确定导致问题的状态.绘制此四边形时禁用的任何状态都需要在绘制需要它的对象时重新启用.

原文地址:https://www.jb51.cc/c/117841.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐