简体   繁体   中英

OpenGL rendering with texture unclear

I'm using the latest version of OpenGL. But when I create textures using the following function is written in C++:

GLuint Texture::Generate2dTexture(int width, int height, char* data, int length)
{
   GLuint textureIndex;

   glGenTextures(1, &textureIndex);
   glBindTexture(GL_TEXTURE_2D, textureIndex);
   gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height, GL_BGR_EXT, GL_UNSIGNED_BYTE, data);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   return textureIndex;
}

and render using:

void Renderer::DrawRectangle(GLuint textureID, RECTD rect)
{
    glBindTexture(GL_TEXTURE_2D, textureID);
    glBegin(GL_QUADS);
    {
        glTexCoord2f(0.0f, 1.0f);
        glVertex2d(rect.left, rect.top);
        glTexCoord2f(0.0f, 0.0f);
        glVertex2d(rect.left, rect.bottom);
        glTexCoord2f(1.0f, 0.0f);
        glVertex2d(rect.right, rect.bottom);
        glTexCoord2f(1.0f, 1.0f);
        glVertex2d(rect.right, rect.top);
    }
    glEnd();
}

They appear unclear after rendering. It seems quite different from the appearance in the image viewer made by M$.

What cause that to happen and how to avoid the risk?

Check the dimensions of your image. Most image loaders will expect images to be presented in powers of 2. So, an 8x8 pixel image should look clear (as opposed to stretched out or garbled). Try clipping the image to 32x32 or 128x128 etc to see if that helps. If the image you want cannot be represented where height=width you can still adjust the canvas so it is something like 128x128 and then simply use the UV coordinates to take the portion of the image you want for your texture.

Also, I think if you disable bilinear filtering any blurring should be taken care of: glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

Please note this is not a definite answer - I don't have enough rep to comment...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM