我无法在opengl中手动设置mipmap级别。这不会抛出错误,但不会显示纹理。取消注释glGenerateMipmap(GL_TEXTURE_2D)起作用了(这意味着它起作用了,mipmap级别不是我设置的)。
我的mipmap级别似乎是完整的,因为最后一个级别(12)是一个1x1的图像。你知道我会做错什么吗?
Texture::Texture(const std::string texture_levels[])
: m_FilePath(texture_levels[0]), m_LocalBuffer(nullptr), m_Width(0), m_Height(0), m_BPP(0)
{
stbi_set_flip_vertically_on_load(1);
GLCall(glGenTextures(1, &m_RendererID));
GLCall(glBindTexture(GL_TEXTURE_2D, m_RendererID));
GLCall(glTexParameteri(GL_TEXTURE\_2D, GL_TEXTURE_BASE_LEVEL, 0));
GLCall(glTexParameteri(GL_TEXTURE\_2D, G_TEXTURE_MAX_LEVEL, 12));
for (int i = 0; i < 13; i++) {
m_LocalBuffer = stbi_load(texture_levels[i].c_str(), &m_Width, &m_Height, &m_BPP, 4);
GLCall(glTexImage2D(GL_TEXTURE_2D, i, GL_RGBA8, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_LocalBuffer));
}
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR));
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
// glGenerateMipmap(GL_TEXTURE_2D);
GLCall(glBindTexture(GL_TEXTURE_2D, 0));
if (m_LocalBuffer)
stbi_image_free(m_LocalBuffer);
}
Texture::~Texture()
{
GLCall(glDeleteTextures(1, &m_RendererID));
}
void Texture::Bind(unsigned int slot) const
{
GLCall(glActiveTexture(GL_TEXTURE0 + slot));
GLCall(glBindTexture(GL_TEXTURE_2D, m_RendererID));
}
void Texture::Unbind()
{
GLCall(glBindTexture(GL_TEXTURE_2D, 0));
}在Main.cpp中:
std::string texture_levels[] =
{
"res/textures/istanbul/01.png",
"res/textures/istanbul/02.png",
"res/textures/istanbul/03.png",
"res/textures/istanbul/04.png",
"res/textures/istanbul/05.png",
"res/textures/istanbul/06.png",
"res/textures/istanbul/07.png",
"res/textures/istanbul/08.png",
"res/textures/istanbul/09.png",
"res/textures/istanbul/10.png",
"res/textures/istanbul/11.png",
"res/textures/istanbul/12.png",
"res/textures/istanbul/13.png"
};
Texture texture(texture_levels);
texture.Bind();发布于 2019-10-15 05:20:58
一个级别的mitmap的大小必须是前一个级别的一半,四舍五入。参见Mipmap completeness。
更具体地说,在级别i的mipmap的宽度、高度或深度的大小是2^i对纹理图像的相应大小的除法的组成部分。对于所有维度,最后一个mipmap级别的大小为1,因此mipmap级别的数量取决于纹理的大小,并且是log2(maxsize) + 1 (截断),其中maxsize是具有最大大小的纹理的尺寸。
该规范中的相关部分是OpenGL 4.6 API Core Profile Specification - 8.14.3 Mipmapping, page 265
https://stackoverflow.com/questions/58379509
复制相似问题