【问题标题】:Why are my textures showing up as fractal patterns with my openGL renderer?为什么我的纹理在我的 openGL 渲染器中显示为分形图案?
【发布时间】:2015-03-12 01:56:57
【问题描述】:

我正在研究如何使用 openGL 渲染纹理。我是其中的一部分并被卡住了。

我的目标是得到这张照片:http://i.imgur.com/d3kZTsn.png

这就是我所在的位置:http://i.imgur.com/uAV8q0W.png

以前有人见过这个问题吗?

if (tObject == 0)         // We don't yet have an OpenGL texture target
{
    // This code counts the number of images and if there are none simply 
    // returns without doing anything
    int nImages = 0;
    while (tName[nImages][0] != '\0' && nImages < MAX_IMAGES)
        nImages++;

    if (nImages < 1)
        return;

    // To Do
    //
    // Generate a texture object and place the object's value in the "tObject"
    // member, then bind the object to the 2D texture target

    glGenTextures(nImages, &tObject);
    glBindTexture(GL_TEXTURE_2D, tObject);

    for (int nImage = 0; nImage < nImages; nImage++)
    {
        // This code loads the texture using the windows library's "BitmapFile" object
        BitmapFile texture;
        if (!texture.Read(tName[nImage]))
            complain("Couldn't read texture %s", tName);

        GLuint srcFormat, targFormat;
        // To Do
        // 
        // First decide which format the texture is.  If the texture has 4 bytes
        // per pixel then it should be an RGBA texture, if it is 3 bytes per pixel
        // then it is an RGB image.  Notice though that the byte order for the BitmatFile
        // object is reversed, so you need to take that into account in the "source" format

        if( texture.BytesPerPixel() == 3 )
        {
            srcFormat = GL_BGR;
            targFormat = GL_RGB;
        }
        else
        {
            srcFormat = GL_BGRA;
            targFormat = GL_RGBA;
        }

        // Then you need to set the unpack alignment to tell OpenGL about the structure 
        // of the data in the image and send the data to OpenGL.  If there are multiple files
        // then we are manually creating a mipmap here and you will use the "level" parameter
        // of glTexImage2D to tell OpenGL which mipmap level is being set.  The levels are 
        // set in the same order as they are stored in the image list.

        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

        if( nImages > 1 )
        {
            glGenerateMipmap(GL_TEXTURE_2D);
        }

        glTexImage2D(GL_TEXTURE_2D, nImage, targFormat, texture.Width(), texture.Height(), 0, srcFormat, GL_UNSIGNED_BYTE, texture.ImageData());
    }
    // Finally, if there is only one image, you need to tell OpenGL to generate a mipmap

    if( nImages == 1)
    {
        glGenerateMipmap(GL_TEXTURE_2D);
    }
}   

// Here you need to bind the texture to the 2D texture target and set the texture parameters
// You need to set the wrap mode, the minification and magnification filters.

glBindTexture(GL_TEXTURE_2D, tObject);

glTexParameteri(tObject, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(tObject, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(tObject, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(tObject, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

// To Do 
//
// For advanced antialiasing set the number of anisotropic samples
GLERR;

【问题讨论】:

  • 你确定是纹理问题而不是纹理坐标?尝试显示坐标而不是最终颜色以检查它们是否正确。
  • 这最终解决了我的问题!将顶点属性传递给显卡时,我使用了错误的步幅大小。

标签: c++ opengl rendering texturing


【解决方案1】:

我不明白您调用glGenerateMipmap (...) 的逻辑。 glTexImage2D (...) 的第二个参数是纹理 LOD - glGenerateMipmap 将生成从 LOD 0 开始的整个 mip 金字塔。基本上,除了第一个和通过这样做,该循环的最后一次迭代。看起来你要么想要一个数组纹理,要么每个图像都应该是一个单独的纹理。

事实上,glGenTextures (...) 并不像你想象的那样工作。如果nImages > 1,您应该传递一个数组。该数组将保存nImages-许多纹理对象名称。您将每一个绑定并将图像数据单独上传到 LOD 0,然后您可以生成 mipmap。

以下内容解决了我刚才提到的所有内容:

GLuint* tObjects = NULL;

if (tObjects == NULL)      // We don't yet have any OpenGL textures
{
    // This code counts the number of images and if there are none simply 
    // returns without doing anything
    int nImages = 0;
    while (tName[nImages][0] != '\0' && nImages < MAX_IMAGES)
        nImages++;

    if (nImages < 1)
        return;

    tObjects = new GLuint [nImages];

    // To Do
    //
    // Generate multiple texture objects and place the object's values in the "tObjects"
    // member, then bind the object to the 2D texture target

    glGenTextures (nImages, tObjects);

    for (int nImage = 0; nImage < nImages; nImage++)
    {
        glBindTexture(GL_TEXTURE_2D, tObjects [nImage]);

        // This code loads the texture using the windows library's "BitmapFile" object
        BitmapFile texture;
        if (!texture.Read(tName[nImage]))
            complain("Couldn't read texture %s", tName);

        GLuint srcFormat, targFormat;
        // To Do
        // 
        // First decide which format the texture is.  If the texture has 4 bytes
        // per pixel then it should be an RGBA texture, if it is 3 bytes per pixel
        // then it is an RGB image.  Notice though that the byte order for the BitmatFile
        // object is reversed, so you need to take that into account in the "source" format

        if( texture.BytesPerPixel() == 3 )
        {
            srcFormat = GL_BGR;
            targFormat = GL_RGB;
        }
        else
        {
            srcFormat = GL_BGRA;
            targFormat = GL_RGBA;
        }

        // Then you need to set the unpack alignment to tell OpenGL about the structure 
        // of the data in the image and send the data to OpenGL.  If there are multiple files
        // then we are manually creating a mipmap here and you will use the "level" parameter
        // of glTexImage2D to tell OpenGL which mipmap level is being set.  The levels are 
        // set in the same order as they are stored in the image list.

        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

        glTexImage2D(GL_TEXTURE_2D, 0, targFormat, texture.Width(), texture.Height(), 0, srcFormat, GL_UNSIGNED_BYTE, texture.ImageData());

        glGenerateMipmap (GL_TEXTURE_2D);

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多