SOIL - 获取图像尺寸

SOIL - Getting Image Dimensions

我决定使用 SOIL 加载图像,以便在我的项目中使用 OpenGL。我有这个方法,它加载一个图像和 returns 一个 GLTexture,它是一个包含 GLuint textureid 和两个整数宽度和高度的结构:

GLTexture loadTexture(const char *filePath) {
GLTexture texture = {};
int width;
int height;
unsigned char *data;

//Load Image File Directly into an OpenGL Texture
texture.id = SOIL_load_OGL_texture
    (
    filePath,
    SOIL_LOAD_AUTO,
    SOIL_CREATE_NEW_ID,
    SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
    );

//Error Checking (Load Process)
if (texture.id == 0) {
    fatalError("SOIL Loading Error!");
}

//Generate and Bind Texture
glGenTextures(1, &(texture.id));
glBindTexture(GL_TEXTURE_2D, texture.id);

//Get Width, Height and Data of Image
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, &data);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, &data);
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);
glGenerateMipmap(GL_TEXTURE_2D);

//Unbind Texture
glBindTexture(GL_TEXTURE_2D, 0);

//Return Texture
texture.width = width;
texture.height = height;
return texture;

}

据我所知,glGetTexLevelParameteriv() 应该 return 绑定到宽度和高度的纹理宽度,但是每当我加载图像时,这个 returns 0.

我应该填写宽度和高度作为该方法的参数,还是可以通过 OpenGL 获取它们?

SOIL_load_OGL_texture 生成的纹理 ID 在

中被覆盖
glGenTextures(1, &(texture.id));

行(glGenTextures 创建一个新纹理并将 id 存储在 &(texture.id) 中)。之后的所有操作都在新创建的纹理上进行。由于这个新纹理是空的,所以宽度和高度都是0.

我不确定你想在这里实现什么,但如果你只想加载纹理,那么这段代码可能有效:

texture.id = SOIL_load_OGL_texture    (...);

//Error Checking (Load Process)
if (texture.id == 0) {
    fatalError("SOIL Loading Error!");
}

//Just bind and do not create a new texture
glBindTexture(GL_TEXTURE_2D, texture.id);

glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);

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);
glGenerateMipmap(GL_TEXTURE_2D);

//Unbind Texture
glBindTexture(GL_TEXTURE_2D, 0);

//Return Texture
texture.width = width;
texture.height = height;
return texture;