gpt4 book ai didi

c++ - SOIL - 获取图像尺寸

转载 作者:行者123 更新时间:2023-11-30 03:50:08 25 4
gpt4 key购买 nike

我决定使用 SOIL 加载图像,以便在我的项目中使用 OpenGL。我有这个方法,它加载一个图像并返回一个 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() 应该返回绑定(bind)到宽度和高度的纹理宽度,但每当我加载图像时,它都会返回 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;

关于c++ - SOIL - 获取图像尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32019503/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com