PDA

View Full Version : Cube Envoirement Map OpengGl es 2.0



Uncle Kane
06-14-2014, 01:32 AM
Hi,
i have read many tutorials about envoirement mapping for reflections, in some of them they are creating for every side of the cube one frame buffer in others they use one for the whole cube. Which is the right approach?

I am using java and Android 4.0.

I have follwing code and I am getting the glError 1282 on setting follwing:

glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + p_face, m_cubeMap[0], 0 );

The code for cube map generation is:


m_cubeMap = new int[1];
m_cubeFB = new int[1];
m_cubeDB = new int[1];

// create the cube map
glGenTextures ( 1, m_cubeMap, 0 );
glBindTexture ( GL_TEXTURE_CUBE_MAP, m_cubeMap[0] );

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 );
glTexParameteri( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR );


glGenFramebuffers( 1, m_cubeFB, 0 );
glBindFramebuffer( GL_FRAMEBUFFER, m_cubeFB[0] );

for( int i = 0; i < 6; i++ ) {
glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, m_cubeMapSize, m_cubeMapSize, 0, GL_RGB, GL_UNSIGNED_BYTE, null );
}

glGenRenderbuffers ( 1, m_cubeDB, 0 );
glBindRenderbuffer ( GL_RENDERBUFFER, m_cubeDB[0] );
glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, m_cubeMapSize, m_cubeMapSize );

glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_cubeFB[0] );
glFramebufferTexture2D ( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X, m_cubeMap[0], 0 );

glBindFramebuffer ( GL_FRAMEBUFFER, 0 );
glBindRenderbuffer( GL_RENDERBUFFER, 0 );
glBindTexture ( GL_TEXTURE_CUBE_MAP, 0 );


if( LoggerConfig.ON )
checkGlError( "InitRefCubeMap()" );


Before I iterate through all faces I set follwing state:


glBindFramebuffer( GL_FRAMEBUFFER, m_cubeFB[0] );
glViewport( 0, 0, m_cubeMapSize, m_cubeMapSize );
For drawing every Face i, and where the Error will happen:

glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + p_face, m_cubeMap[0], 0 );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

if( LoggerConfig.ON )
checkGlError( "DrawRefCubeMap() Face " + p_face );


It would be great if you can help me. Thanks!

reto.koradi
06-14-2014, 02:06 PM
I already answered this on another site, but just to avoid keeping the question open here: The last argument to glFrameBufferRenderbuffer() is wrong. It should be m_cubeDB[0] instead of m_cubeFB[0].

Uncle Kane
06-16-2014, 05:48 AM
Hi, Thank you. But the same error is still there. If I am accessing the texture for writing (glFramebufferTexture2D) the glError 1282 will happen. The interessting thing is, that the error will happen after the first face. So GL_TEXTURE_CUBE_MAP_POSITIVE_X is fine, but GL_TEXTURE_CUBE_MAP_POSITIVE_X + 1 will throw the error.

Uncle Kane
06-16-2014, 06:02 AM
Do I need maybe one Framebuffer for every side of the cube?

debonair
06-16-2014, 11:49 PM
you need to bind same FBO for every side of the cube. it would be somewhat like:


glBindFramebuffer(GL_FRAMEBUFFER,FboObject);
for (face = 1; face < 6; face++) {
Draw();
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, textureObject, 0);
}

Uncle Kane
06-18-2014, 10:04 AM
Another device shows better debug information. On rendering I am getting following shader error:
06-18 17:58:53.093: V/ShaderHelper(11910): Log:Validate: Sampler 'u_CubeMap' and 'u_ShadowMap' are of different types, but share texture unit 0.
But I am using different texture units for every different texture? If I remove all shadow map code, it will show the same error with another loaded texture.

debonair
06-18-2014, 11:00 AM
check your glActiveTexture() and glUniform() calls for samplers when you attach it to both the textures.

Uncle Kane
06-19-2014, 03:07 AM
I am not setting the cube_map for every material. So the uniform is 0, when not set. But i am controling the usage with other uniforms, so no acces without setting the proper uniforms. Could this be a problem?

Uncle Kane
06-19-2014, 03:18 AM
If I am using the texture units beginning from 1. Zero will never used and the warning from shader validation will not apear. But the problem still exists.

Uncle Kane
07-12-2014, 12:10 PM
The Problem was, that i a have used the same shader for rendering the cube map and for displaying it. Even if the uniform of the cube map was not set and checked with another boolean uniform before usage, the fragment shader crashed. After using a simple phong shader the problem is gone.