1

I am trying to implement a skybox in open gl. It doesn't work unless I bind It as GL_TEXTURE_2D

This is how I load my cube map:

// 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"); }

In the snippet below I bind the texture as a cube map. This does not work! I jus get a black square. However, if I would bind it to GL_TEXTURE_2D I get a textured cube (?).

 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;
 }

Rendering of my material:

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

My vertex shader:

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

I am failry new to OpenGl. Is this maybe a flagging Issue?

These are the only 3 flags I set at the start.

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

Don't really know what to go on here. This seems very obscure to me. Thanks! Marc

Marc HPunkt
  • 439
  • 3
  • 14
  • Is your vertex shader compiling successfully? The `gl_` prefix is reserved for built-in variables, and a compliant compiler should give you an error if you try to use it for your own variables. – Reto Koradi Nov 15 '15 at 17:45
  • hmm. yes the shader compiles. But i can try renaming the variable. Edit: renamed the variable - nothing changed – Marc HPunkt Nov 15 '15 at 17:56
  • 1
    Also, make sure that the images you load are square. – Reto Koradi Nov 15 '15 at 18:13
  • I just checked. The textures I load are 512x512 – Marc HPunkt Nov 15 '15 at 18:21
  • 1
    I recently has some difficulties with cube map (c#, openttk too) and the problem was that it absolutely needs 6 textures, one for each side, or the cube stays untextured. – j-p Nov 15 '15 at 20:16
  • My final working code is here: https://github.com/jpbruyere/GGL/tree/ottd/Tetra (opengl 3.3) – j-p Nov 15 '15 at 20:18
  • Thanks for the info! But I do supply 6 textures.. So maybe Im doing it wrong... Ill have a look at your code! – Marc HPunkt Nov 15 '15 at 20:25
  • 1
    Ah! It works now. Im my LoadCubeSide function I added an: glActiveTexture(GL_TEXTURE0); call before the texture binding. – Marc HPunkt Nov 15 '15 at 20:49

0 Answers0