-1

我正在使用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;
4

1 回答 1

1

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

事实上,glGenTextures (...)它并不像你想象的那样工作。nImages如果is > 1,您应该传递一个数组。该数组将保存nImages-many 纹理对象名称。您绑定每一个并将图像数据单独上传到 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);
    }
}
于 2015-03-13T02:32:47.273 回答