• FEATURED

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

## Cube map troubles

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

8 replies to this topic

Posted 23 November 2012 - 02:54 PM

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?

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 330uniform 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.

### #2Brother Bob  Moderators

Posted 23 November 2012 - 03:57 PM

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.

Posted 23 November 2012 - 04:15 PM

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.

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.

### #4Brother Bob  Moderators

Posted 23 November 2012 - 04:31 PM

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.

Posted 23 November 2012 - 06:31 PM

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.

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, 23 November 2012 - 06:53 PM.

Posted 23 November 2012 - 07:10 PM

[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, 23 November 2012 - 07:54 PM.

### #7Brother Bob  Moderators

Posted 24 November 2012 - 05:50 AM

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.

Posted 27 November 2012 - 06:42 PM

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.

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...

Posted 30 November 2012 - 06:16 PM

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, 30 November 2012 - 06:19 PM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.