【问题标题】:I'm seeing artifacts when I attempt to rotate an image尝试旋转图像时看到伪影
【发布时间】:2010-11-14 12:49:42
【问题描述】:

这是之前的: http://img22.imageshack.us/img22/5310/beforedes.jpg znd 之后: http://img189.imageshack.us/img189/8890/afterr.jpg

编辑:: 现在我查看了 imageshack 上传的内容,这些人工制品减少了很多。但相信我,它们比这更明显。

我不明白为什么会这样。 Imageshack 将它们上传到 jpg,但在我的程序中,它们以 .tif 格式存在于图像文件夹中(使用 .tif 的原因是因为我无法获得任何其他图像来保持其透明部分)。

但无论如何,这些伪影会跟随图像的原始顶部,因为它会在除原始图像之外的任何位置旋转。

这是我加载图像的部分代码

GLuint texture;
GLenum texture_format;
GLint  nofcolors;
GLfloat spin;

bool Game::loadImage()
{
    SDL_Surface * surface; // this surface will tell us the details of the image

    if ( surface = SM.load_image("Images/tri2.tif") )
    {
        //get number of channels in the SDL surface
        nofcolors = surface->format->BytesPerPixel;

        //contains an alpha channel
        if ( nofcolors == 4 )
        {
            if ( surface->format->Rmask == 0x000000ff )
                texture_format =  GL_RGBA;
            else texture_format = GL_BGRA;
        }
        else if ( nofcolors == 3 ) //no alpha channel
        {
            if ( surface->format->Rmask == 0x000000ff )
                texture_format =  GL_RGB;
            else texture_format = GL_BGR;
        }

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

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

        // Set the texture’s stretching properties
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

        glTexImage2D( GL_TEXTURE_2D, 0, nofcolors, surface->w, surface->h, 0, texture_format, GL_UNSIGNED_BYTE, surface->pixels );

        glEnable(GL_TEXTURE_2D);
        glEnable(GL_BLEND);
        glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
    }
    else 
    {
        SDL_Quit();
        return false;
    }

    // Free the SDL_Surface only if it was successfully created
    if ( surface )
    {
        SDL_FreeSurface( surface );
        return true;
    }
    else return false;
}

void Game::drawImage()
{
    // Clear the screen before drawing
    glClear( GL_COLOR_BUFFER_BIT );

    glTranslatef( float(S_WIDTH/2), float(S_HEIGHT/2), 0.0f );
    glRotatef( spin, 0.0, 0.0, 1.0 );

    // Bind the texture to which subsequent calls refer to
    glBindTexture( GL_TEXTURE_2D, texture );

    glBegin( GL_QUADS );
    {
        // Top-left vertex (corner)
        glTexCoord2i( 0, 0 );
        glVertex3f( -64, 0, 0 );

        // Top-right vertex (corner)
        glTexCoord2i( 1, 0 );
        glVertex3f( 64, 0, 0 );

        // Bottom-right vertex (corner)
        glTexCoord2i( 1, 1 );
        glVertex3f( 64, 128, 0 );

        // Bottom-left vertex (corner)
        glTexCoord2i( 0, 1 );
        glVertex3f( -64, 128, 0 );
    }
    glEnd();

    glLoadIdentity();
    SDL_GL_SwapBuffers();
}

【问题讨论】:

  • 大声笑,当我加载这个页面时,我以为你的人工制品是我屏幕上的灰尘,我试着擦掉它。
  • PNG 格式无法在我的程序中正确加载透明度。除了 TIF 之外,其他任何东西也不会。这让我发疯了..

标签: c++ user-interface opengl


【解决方案1】:

看起来纹理设置为GL_WRAP。请改用GL_CLAMP_TO_EDGE

【讨论】:

  • 是的,它肯定是包装的。
  • 一两天前我刚刚启动了 OpenGL,我没有看到我输入 GL_WRAP 的任何地方......那么我应该把夹子放在哪里?
  • 在设置 min/mag 过滤器后插入以下内容:glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  • 啊,非常感谢。现在我要弄清楚图像中这种烦人的(缺乏)透明度。
【解决方案2】:

在 Game::loadImage 中,调用 glBindTexture 之后:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);        
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);        

您当前的设置是 GL_REPEAT,这是 OpenGL 的默认设置。

【讨论】:

    猜你喜欢
    • 2019-11-27
    • 2018-07-06
    • 2019-12-08
    • 1970-01-01
    • 2020-04-07
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    相关资源
    最近更新 更多