关于OpenGL glGeneratemipmap和glGetTexImage的问题

时间:2016-05-17 09:28:45

标签: opengl mipmaps

我形成一个256 * 256的图像,我想使用glGeneratemipmap函数生成3级mipmap,然后使用glGetTexImage函数获取2级mipmap数据,我想要的数据应该是64 * 64,但它变成了128 * 128,这是我的代码,代码有什么问题,你能帮助我吗?

 // image data:
int bytePerline1 = 256 * 24 / 8;
unsigned char *mipmapImage=new unsigned char[256*bytePerline1];
for (int i = 0; i < 256; i++)
    for (int j = 0; j < 256; j++)
    {
        mipmapImage[i*bytePerline1 + j * 3 + 0] = 255;
        mipmapImage[i*bytePerline1 + j * 3 + 1] = 255;
        mipmapImage[i*bytePerline1 + j * 3 + 2] = 0;

    }
int glwidth = 256;
int glheight = 256;
glEnable(GL_TEXTURE_2D);
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, glwidth, glheight, 0, GL_RGB, GL_UNSIGNED_BYTE, mipmapImage);//load the data to gpu
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_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 3);
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
glGenerateMipmap(GL_TEXTURE_2D);//generate mipmap
int bytePerLine2 = (64 * 24) / 8;
unsigned char *result = new unsigned char[bytePerLine2 * 64];
glGetTexImage(GL_TEXTURE_2D, 2, GL_RGB, GL_UNSIGNED_BYTE, result);//get the 2rd level mipmap data

我使用QT的QImage类来形成图像。这是代码:

int bytePerLine2 = (64 * 24) / 8;
unsigned char *result = new unsigned char[bytePerLine2 * 64];
glGetTexImage(GL_TEXTURE_2D, 2, GL_RGB, GL_UNSIGNED_BYTE, result);//get the 2rd level mipmap data
if (result != NULL)
{
    int bytePerLine3 = (64 * 24 + 31) / 8;
    unsigned char * result1 = new unsigned char[bytePerLine3 * 64];
    for (int i = 0; i < 64; i++)
        for (int j = 0; j < 64; j++)
        {
            result1[i*bytePerLine3 + j * 3] = *result;
            result1[i*bytePerLine3 + j * 3+1] = *(result+1);
            result1[i*bytePerLine3 + j * 3 + 2] = *(result + 2);
            result = result + 3;
        }
    QImage *image = new QImage(result1, 64, 64, bytePerLine3, QImage::Format_RGB888);
    char * path = "C:\\Users\\hao\\Desktop\\temp1.jpg";
    image->save(path);
    delete image;
    image = nullptr;
}

0 个答案:

没有答案