使用 opengl 和 SOIL2 从纹理采样给出黑色模型
Sampling from a texture gives a black model using opengl and SOIL2
我正在尝试对模型进行纹理处理,但我得到的只是完全呈现为黑色的模型。
我使用 SOIL2 库将图像加载到内存中,然后
下面的代码在我的 Texture class.
中显示了 Load function
bool Texture::Load(std::string texturePath)
{
int channels = 0;
unsigned char* image = SOIL_load_image(texturePath.c_str(), &mWidth, &mHeight, &channels, SOIL_LOAD_AUTO);
if (image == nullptr)
{
std::cout << "Failed to load image " << texturePath;
return false;
}
int format = GL_RGB;
if (channels == 4)
{
format = GL_RGBA;
}
glGenTextures(1, &mTextureID);
glBindTexture(GL_TEXTURE_2D, mTextureID);
glGenerateMipmap(GL_TEXTURE_2D);
glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, format, mWidth, mHeight, 0, format, GL_UNSIGNED_BYTE, image);
// Free data
SOIL_free_image_data(image);
return true;
}
当我尝试调试代码时,我发现 图像指针 指向一个空数组,如下图 我不知道这是问题还是不是,但我发现它很奇怪,我很确定图像已成功加载,因为 mWidth 和 mHeight 具有正确的值。
我的 顶点着色器 :
#version 330 core
layout(location=0) in vec3 position ;
layout(location=1) in vec2 UVCoord ;
layout(location=2) in vec3 normal ;
uniform mat4 uWorldTransform ;
uniform mat4 uView ;
uniform mat4 uProjection ;
out vec2 textCoord ;
void main()
{
gl_Position = uProjection * uView * uWorldTransform * vec4(position, 1.0) ;
textCoord = UVCoord ;
}
和我的片段着色器
#version 330 core
in vec2 textCoord ;
out vec4 color ;
uniform sampler2D myTexture ;
void main()
{
color = texture(myTexture , textCoord) ;
}
我的渲染代码:
void Renderer::Draw()
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
std::vector<Actor*> actors = mGame->GetActors();
for (auto act : actors)
{
if (act->GetDrawable())
{
glm::mat4 worldTransform = act->GetWorldTransform();
Shader* shader = mShaders[act->GetMesh()->GetShaderName()];
VertexArrayObject* vao = act->GetMesh()->GetVAO();
Texture* text = act->GetTexture();
shader->Bind();
shader->SetMatrix4Uniform("uWorldTransform", worldTransform);
shader->SetMatrix4Uniform("uView", mView);
shader->SetMatrix4Uniform("uProjection", mProjection);
if (text)
{
text->Bind();
}
vao->Bind();
glDrawElements(GL_TRIANGLES, vao->GetEBOsize(), GL_UNSIGNED_INT, nullptr);
}
}
glfwSwapBuffers(mGame->GetWindow());
}
这是我在屏幕上得到的结果:
调用glGenerateMipmap()
after图片上传(glTexImage2D()
),否则它不会做任何有用的事情,因为没有任何图片数据在尚未从中生成 mipmap 的纹理。
或通过将 GL_TEXTURE_MIN_FILTER
设置为 GL_LINEAR
/GL_NEAREST
来禁用 mip 采样。
此外,请注意 GL_RGB
和 4
的默认值 GL_UNPACK_ALIGNMENT
。 1
是你通常想要的。
我正在尝试对模型进行纹理处理,但我得到的只是完全呈现为黑色的模型。 我使用 SOIL2 库将图像加载到内存中,然后 下面的代码在我的 Texture class.
中显示了 Load functionbool Texture::Load(std::string texturePath)
{
int channels = 0;
unsigned char* image = SOIL_load_image(texturePath.c_str(), &mWidth, &mHeight, &channels, SOIL_LOAD_AUTO);
if (image == nullptr)
{
std::cout << "Failed to load image " << texturePath;
return false;
}
int format = GL_RGB;
if (channels == 4)
{
format = GL_RGBA;
}
glGenTextures(1, &mTextureID);
glBindTexture(GL_TEXTURE_2D, mTextureID);
glGenerateMipmap(GL_TEXTURE_2D);
glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, format, mWidth, mHeight, 0, format, GL_UNSIGNED_BYTE, image);
// Free data
SOIL_free_image_data(image);
return true;
}
当我尝试调试代码时,我发现 图像指针 指向一个空数组,如下图 我不知道这是问题还是不是,但我发现它很奇怪,我很确定图像已成功加载,因为 mWidth 和 mHeight 具有正确的值。
我的 顶点着色器 :
#version 330 core
layout(location=0) in vec3 position ;
layout(location=1) in vec2 UVCoord ;
layout(location=2) in vec3 normal ;
uniform mat4 uWorldTransform ;
uniform mat4 uView ;
uniform mat4 uProjection ;
out vec2 textCoord ;
void main()
{
gl_Position = uProjection * uView * uWorldTransform * vec4(position, 1.0) ;
textCoord = UVCoord ;
}
和我的片段着色器
#version 330 core
in vec2 textCoord ;
out vec4 color ;
uniform sampler2D myTexture ;
void main()
{
color = texture(myTexture , textCoord) ;
}
我的渲染代码:
void Renderer::Draw()
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
std::vector<Actor*> actors = mGame->GetActors();
for (auto act : actors)
{
if (act->GetDrawable())
{
glm::mat4 worldTransform = act->GetWorldTransform();
Shader* shader = mShaders[act->GetMesh()->GetShaderName()];
VertexArrayObject* vao = act->GetMesh()->GetVAO();
Texture* text = act->GetTexture();
shader->Bind();
shader->SetMatrix4Uniform("uWorldTransform", worldTransform);
shader->SetMatrix4Uniform("uView", mView);
shader->SetMatrix4Uniform("uProjection", mProjection);
if (text)
{
text->Bind();
}
vao->Bind();
glDrawElements(GL_TRIANGLES, vao->GetEBOsize(), GL_UNSIGNED_INT, nullptr);
}
}
glfwSwapBuffers(mGame->GetWindow());
}
这是我在屏幕上得到的结果:
调用glGenerateMipmap()
after图片上传(glTexImage2D()
),否则它不会做任何有用的事情,因为没有任何图片数据在尚未从中生成 mipmap 的纹理。
或通过将 GL_TEXTURE_MIN_FILTER
设置为 GL_LINEAR
/GL_NEAREST
来禁用 mip 采样。
此外,请注意 GL_RGB
和 4
的默认值 GL_UNPACK_ALIGNMENT
。 1
是你通常想要的。