我正在尝试在 open gl 中实现一个天空盒。除非我将它绑定为 GL_TEXTURE_2D 否则它不起作用
这就是我加载立方体贴图的方式:
// http://www.antongerdelan.net/opengl/cubemaps.html
void SkyBoxMaterial::CreateCubeMap(const char* front, const char* back, const char* top, const char*bottom, const char* left, const char*right, GLuint *tex_cube){
glGenTextures(ONE, tex_cube);
assert(LoadCubeMapSide(*tex_cube, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, front));
assert(LoadCubeMapSide(*tex_cube, GL_TEXTURE_CUBE_MAP_POSITIVE_Z, back));
assert(LoadCubeMapSide(*tex_cube, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, top));
assert(LoadCubeMapSide(*tex_cube, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, bottom));
assert(LoadCubeMapSide(*tex_cube, GL_TEXTURE_CUBE_MAP_NEGATIVE_X, left));
assert(LoadCubeMapSide(*tex_cube, GL_TEXTURE_CUBE_MAP_POSITIVE_X, right));
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_R, GL_CLAMP_TO_EDGE);
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);
checkGLErrors("SKYBOX_MATERIAL::CreateCubeMap", "END"); }
在下面的片段中,我将纹理绑定为立方体贴图。这不起作用!我只是得到一个黑色方块。但是,如果我将它绑定到 GL_TEXTURE_2D 我会得到一个带纹理的立方体(?)。
bool SkyBoxMaterial::LoadCubeMapSide(GLuint texture, GLenum side_target, const char* fileName){
glBindTexture(GL_TEXTURE_CUBE_MAP, texture);
int width, height, n;
int force_channels = 4;
unsigned char* image_data = stbi_load(fileName, &width, &height, &n, force_channels);
glTexImage2D(side_target, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data);
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
free(image_data);
return true;
}
我的材质渲染:
void SkyBoxMaterial::Draw(){
glDepthMask(GL_FALSE);
glUseProgram(programID);
glBindVertexArray(VAO);
glActiveTexture(GL_TEXTURE0);
glUniform1i(glGetUniformLocation(programID, "skybox"), 0);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubeMapTexture);
glDrawArrays(geometry->getDrawMode(), 0, geometry->getNumVertices());
glBindVertexArray(CLEAR);
glUseProgram(CLEAR);
glDepthMask(GL_TRUE);
}
我的顶点着色器:
#version 430 core
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
layout (location = 0) in vec3 gl_vertex;
out vec3 texcoords;
void main(){
vec4 homogeneous_vertecies = vec4(gl_vertex,1.0);
gl_Position = projection*view*model*homogeneous_vertecies;
texcoords = gl_vertex;
}
片段着色器:
#version 430 core
in vec3 texcoords;
uniform samplerCube skybox;
// Ouput data
out vec4 color;
void main(){
color = texture(skybox,texcoords);
}
我对 OpenGL 很陌生。这可能是一个标记问题吗?
这是我在开始时设置的仅有的 3 个标志。
glEnable(GL_DEPTH_TEST);
// Cull triangles which normal is not towards the camera
glEnable(GL_CULL_FACE);
// Accept fragment if it closer to the camera than the former one
glDepthFunc(GL_LESS);
真的不知道这里该怎么办。这对我来说似乎很模糊。谢谢!马克