我正在尝试为OpenGL 3.3
中的项目创建天空盒。该项目是用C ++编写的。我遇到的问题是尽管加载了一个立方体贴图纹理并且绑定了采样器我得到了黑色像素,就像在图片中一样(立方体中有法线指向它):
color from sampler
我正在使用采样器进行操作是一个问题,因为当我使用顶点坐标作为颜色时,它可以很好地工作: color from postition
我搜索过类似的问题,但没有一个答案对我有用。
以下是代码的有趣部分:
#version 330 core
layout (location = 0) in vec3 position;
out vec3 TexCoords;
uniform mat4 mvp;
void main()
{
gl_Position = mvp * vec4(position, 1.0);
TexCoords = position;
}
#version 330 core
in vec3 TexCoords;
out vec4 color;
uniform samplerCube skybox;
void main()
{
color = texture(skybox, TexCoords);
}
CubeTexture::CubeTexture(GLenum texture, std::vector<const GLchar*> faces)
{
this->texture = texture;
glGenTextures(1, &texture_id);
glActiveTexture(texture);
int width, height;
unsigned char* image;
glBindTexture(GL_TEXTURE_CUBE_MAP, texture_id);
for(GLuint i = 0; i < faces.size(); i++)
{
image = SOIL_load_image(faces[i], &width, &height, 0, SOIL_LOAD_RGB);
if (image == nullptr)
throw std::runtime_error(std::string("Failed to load texture file"));
glTexImage2D(
GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0,
GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image
);
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
}
void CubeTexture::bind(GLuint shader_program_id, const char* name, unsigned int location)
{
glActiveTexture(texture);
glBindTexture(GL_TEXTURE_CUBE_MAP, texture_id);
glUniform1i(glGetUniformLocation(shader_program_id, name), location);
}
// ...
std::vector<const GLchar*> faces;
faces.push_back("res/textures/skybox/right.png");
faces.push_back("res/textures/skybox/left.png");
faces.push_back("res/textures/skybox/top.png");
faces.push_back("res/textures/skybox/bottom.png");
faces.push_back("res/textures/skybox/back.png");
faces.push_back("res/textures/skybox/front.png");
CubeTexture skybox_texture(GL_TEXTURE0, faces);
// ... in the main loop ...
glUseProgram(skybox_program_id);
skybox_texture.bind(skybox_program_id, "skybox", 0);