# OpenGL Cube map troubles

## Recommended Posts

Hey all, I'm having a bit of trouble trying to get a cubemap to work with OpenGL and GLSL. It seems to upload it just fine, at least GlIntercept outputs the cubemap texture in the way I would expect it too. However, when I try to sample the texture all I seem to get is just plain black no matter which coordinates I use to sample. The source texture has essentially no black at all.

I have been tearing my hair over this for the past few days and I'm wondering if any of you could spot the likely stupid mistake I've made?

[source lang="cpp"]namespace
{

SDL_Surface* createCubeMapFace(SDL_Surface* in, int faceStartX, int faceStartY, int width, int height)
{
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
SDL_Surface* bufferSurface = SDL_CreateRGBSurface(0, width, height, 32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000);

#else
SDL_Surface* bufferSurface = SDL_CreateRGBSurface(0, width, height, 32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
#endif

SDL_Rect rect;

rect.w = width;
rect.h = height;
rect.x = faceStartX;
rect.y = faceStartY;

SDL_Rect dest;
dest.x = 0;
dest.y = 0;

SDL_FillRect(bufferSurface, &dest, SDL_MapRGBA(bufferSurface->format, 0, 0xFF, 0, 0xFF));
SDL_BlitSurface(in, &rect, bufferSurface, &dest);

return bufferSurface;

}

void uploadCubeMapFace(GLenum cubeMapFace, SDL_Surface* face, bool generateMipMaps)
{
glTexImage2D(cubeMapFace, 0, GL_RGBA, face->w, face->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, face->pixels );
tryLogError();
if (generateMipMaps) gluBuild2DMipmaps(cubeMapFace, GL_RGBA, face->w , face->h, GL_RGBA,GL_UNSIGNED_BYTE, face->pixels);
tryLogError();
}

SDL_Surface* front,
SDL_Surface* right,
SDL_Surface* far,
SDL_Surface* top,
SDL_Surface* bottom,
bool mipmapped)
{

GLuint id;
glGenTextures(1, &id);

glEnable(GL_TEXTURE_CUBE_MAP);
glBindTexture(GL_TEXTURE_CUBE_MAP, id);
tryLogError();

glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);

//Create cube map

glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
return id;
}
// Loads a Texture into VideoRAM and returns a texture id
{

assert(image->getWidth() % 4 == 0);
assert(image->getHeight() % 3 == 0);
assert(image->getWidth()/4 == image->getHeight()/3);

int sideWidth = image->getWidth()/4;
int sideHeight = image->getHeight()/3;

auto left = createCubeMapFace(image->getInternalSurface(), sideWidth*0, sideHeight*1, sideWidth, sideHeight);
auto front = createCubeMapFace(image->getInternalSurface(), sideWidth*1, sideHeight*1, sideWidth, sideHeight);
auto right = createCubeMapFace(image->getInternalSurface(), sideWidth*2, sideHeight*1, sideWidth, sideHeight);
auto far = createCubeMapFace(image->getInternalSurface(), sideWidth*3, sideHeight*1, sideWidth, sideHeight);
auto top = createCubeMapFace(image->getInternalSurface(), sideWidth*1, sideHeight*0, sideWidth, sideHeight);
auto bottom = createCubeMapFace(image->getInternalSurface(), sideWidth*1, sideHeight*2, sideWidth, sideHeight);

front,
right,
far,
top,
bottom,
generateMipMaps);
SDL_FreeSurface(left);
SDL_FreeSurface(front);
SDL_FreeSurface(right);
SDL_FreeSurface(far);
SDL_FreeSurface(top);
SDL_FreeSurface(bottom);

return retVal;

}
}[/source]

Code used to sent the cubemap id to the shader program, I have checked that the id is correct and that the part of the of the code is executed as it should as well as having a unique sampler number.

[source lang="cpp"]void ShaderProgram::setTexture(String variableName, int samplerNo, ICubeMap* texture)
{
assert(this->programId == activeProgram);
assert(samplerNo >= 0);

glEnable(GL_TEXTURE_CUBE_MAP);
glBindTexture(GL_TEXTURE_CUBE_MAP, texture->getOpenGLId());

glActiveTexture(GL_TEXTURE0 + samplerNo);

GLuint loc = glGetUniformLocation(this->programId, variableName.getStringUtf8());
glUniform1i(loc, samplerNo);
}[/source]

Finally, the minimimalistic test fragment shader:

[source lang="cpp"]#version 330

uniform samplerCube cubeMapTexture;

in vec4 rawNormal;

out vec4 fragmentColour;

void main(void)
{
fragmentColour = texture(cubeMapTexture, vec3(rawNormal.xyz));

}[/source]
The vertex shader works as it should, if I hard set the colour in the fragment shader the object appears as expected.

I would be thankful for any help.

##### Share on other sites
I am not sure about this, but I believe gluBuild2DMipmaps does not accept cube map faces as texture targets and thus cannot build cube map faces. If this is the case, then your call to gluBuild2DMipmaps will fail and thus leave the texture object in an incomplete, and therefore invalid, state since you have specified the minification filtering as a mipmap filter but only provided the base level texture. Either don't use mipmap filtering, or generate your mipmaps with the current glGenerateMipmap function instead.

##### Share on other sites
[quote name='Brother Bob' timestamp='1353707858' post='5003587']
I am not sure about this, but I believe gluBuild2DMipmaps does not accept cube map faces as texture targets and thus cannot build cube map faces. If this is the case, then your call to gluBuild2DMipmaps will fail and thus leave the texture object in an incomplete, and therefore invalid, state since you have specified the minification filtering as a mipmap filter but only provided the base level texture. Either don't use mipmap filtering, or generate your mipmaps with the current glGenerateMipmap function instead.
[/quote]

Looking at the documentation, you seem to be correct. Though removing any reference to mipmaps (the function and the filtering methods set to just GL_LINEAR instead) does not seem to correct the issue though. All samples are still pure black.

##### Share on other sites
I don't see anything obvious other than what I mentioned. Check for errors codes to see if anything is wrong. Keep in mind that some functions indicate errors by return value instead of an error state from glGetError, such as querying a uniform location, so make sure you check return values for functions as well.

##### Share on other sites
[quote name='Brother Bob' timestamp='1353709872' post='5003594']
I don't see anything obvious other than what I mentioned. Check for errors codes to see if anything is wrong. Keep in mind that some functions indicate errors by return value instead of an error state from glGetError, such as querying a uniform location, so make sure you check return values for functions as well.
[/quote]

I should have done it from the beginning, especially as I usually program fairly defensivly. But I went and added error checking to all OpenGL calls and it seems that the shader program is unable to find the cubemap sampler. Strange, it is used so it should not be optimized away and I double checked the name after discovering this. No trouble there, are there any known causes for this issue?

Edit: Nevermind, was reporting for a different shader program. It finds it just fine for this, but still black an no clear errors... Edited by PAndersson

##### Share on other sites

[source lang="cpp"]uniform samplerCube cubeTest;
uniform samplerCube terrainNoiseTexture;

void main(void)
{
fragmentColour.rbg = texture(terrainNoiseTexture, vec3(rawNormal.xyz)).rrr;
fragmentColour.rbg += texture(cubeTest, vec3(rawNormal.xyz)).rrr;
fragmentColour.a = 1;
}[/source]

I can suddenly read from the black cubemap as expected....

Maybe I just should update my drivers...
Still nothing. So what could cause a cubemap to be all blick until I sample another cubemap first? Edited by PAndersson

##### Share on other sites
So if you use that exact code and just comment out one of the two texture samplers, then it stops working? Reduce your application to an absolute minimum and show the complete code.

##### Share on other sites
[quote name='Brother Bob' timestamp='1353757833' post='5003718']
So if you use that exact code and just comment out one of the two texture samplers, then it stops working? Reduce your application to an absolute minimum and show the complete code.
[/quote]

All right, have not had any more success with this (to be fair, not a lot of time to work on it either) so I will see what I can do. The application currently uses A LOT of infrastructure and getting rid of that will be a major effort...

##### Share on other sites
Finally got some more time to look into the issue and managed to find and solve the problem.

The setTexture method should look like this:

[source lang="cpp"] void ShaderProgram::setTexture(String variableName, int samplerNo, ICubeMap* texture)
{
assert(this->programId == this->activeProgram);
assert(samplerNo >= 0);

//Prepere texture
glCheckedCall( glActiveTexture(GL_TEXTURE0 + samplerNo) );

glCheckedCall( glEnable(GL_TEXTURE_CUBE_MAP) );
glCheckedCall( glBindTexture(GL_TEXTURE_CUBE_MAP, texture->getOpenGLId()) );

GLint loc = glCheckedCall( glGetUniformLocation(this->programId, variableName.getStringUtf8()) );
if (loc < 0) syslog.Error(String("Failed to find uniform cubemap name \"") + variableName + "\" in program " + this->getProgramID());
glCheckedCall( glUniform1i(loc, samplerNo) );

}[/source]
As in my previous post, I bound the texture before activating it thus effectivly messing with other texture units. Simply swapping the order in which the function calls are made solved everything.

Thank you for your help, and this just cements my sentiment that state-based interfaces are evil Edited by PAndersson

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627671
• Total Posts
2978551
• ### Similar Content

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• 11
• 10
• 10
• 12
• 22