OpenGL 4.3 - CubeMap仅在绑定为GL_TEXTURE_2D时才有效

时间:2015-11-15 17:01:20

标签: opengl opengl-4 texturing skybox

我正在尝试在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;
}

Fragement Shader:

#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);

不知道该怎么做。这对我来说似乎很模糊。 谢谢! 马克

0 个答案:

没有答案