OpenGL Environment mapping Reflection

2019-09-14 19:30发布

my reflections are wrong. I have changed a lot of the code, but I didn't find the error. It would be great if you can help me.

Texture generation: Code :

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

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

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

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] );
glFramebufferTexture2D   ( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X, m_cubeMap[0], 0 );

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

    // check status
    if( glCheckFramebufferStatus( GL_FRAMEBUFFER ) != GL_FRAMEBUFFER_COMPLETE ) {
    if( LoggerConfig.ON )
        Log.e( "MESH", "CubeMap Framebuffer incomplete: " + glCheckFramebufferStatus( GL_FRAMEBUFFER ) );

    return;
}

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

Iterate through all faces ( 0 <= p_face >= 6) and draw after this: Code :

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

glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
glClear( GL_COLOR_BUFFER_BIT );

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

float mvMatrix[] = new float[16];

switch( p_face ) {
case POSITIVE_X:
    setLookAtM( mvMatrix, 0, 
        0.0f,  0.0f,  0.0f,
        1.0f,  0.0f,  0.0f, 
        0.0f,  1.0f,  0.0f ); 
    break;

case NEGATIVE_X:
    setLookAtM( mvMatrix, 0, 
        0.0f,  0.0f,  0.0f,
       -1.0f,  0.0f,  0.0f, 
        0.0f,  1.0f,  0.0f ); 
    break;

case POSITIVE_Y:
    setLookAtM( mvMatrix, 0, 
        0.0f,  0.0f,  0.0f,
        0.0f,  1.0f,  0.0f, 
        0.0f,  0.0f,  1.0f  ); 
    break;

case NEGATIVE_Y:
    setLookAtM( mvMatrix, 0, 
        0.0f,  0.0f,  0.0f,
        0.0f, -1.0f,  0.0f, 
        0.0f,  0.0f,  1.0f ); 
    break;

case POSITIVE_Z:
    setLookAtM( mvMatrix, 0, 
        0.0f,  0.0f,  0.0f,
        0.0f,  0.0f,  1.0f, 
        0.0f,  1.0f,  0.0f  ); 
    break;

case NEGATIVE_Z:
    setLookAtM( mvMatrix, 0, 
        0.0f,  0.0f,  0.0f,
        0.0f,  0.0f, -1.0f, 
        0.0f,  1.0f,  0.0f ); 
    break;

default:
    return;
};

translateM( mvMatrix, 0, -m_cubeCenter[0], -m_cubeCenter[1], -m_cubeCenter[2] );

float projMatrix[] = new float[16];
perspectiveM( projMatrix, 0, 90.0f, 1.0f, 0.01f, 100.0f );  // will the near clip plane size sufficient?

float mvpMatrix[] = new float[16];
multiplyMM( mvpMatrix, 0, projMatrix, 0, mvMatrix, 0 );

float[] MVMatrix_In = new float[16];
float[] normalMat   = new float[16];
invertM( MVMatrix_In,  0, mvMatrix,    0 );
transposeM( normalMat, 0, MVMatrix_In, 0 );

glUniformMatrix4fv( m_simplePhongShader.m_uMVMatrix,     1, false, mvMatrix,  0 ); 
glUniformMatrix4fv( m_simplePhongShader.m_uMVPMatrix,    1, false, mvpMatrix, 0 ); 
glUniformMatrix4fv( m_simplePhongShader.m_uNormalMatrix, 1, false, normalMat, 0 );

Fragement shader (world positions used): Code :

vec3 camRay  = normalize( v_PosW - u_CameraCenter );
vec3 wNormal = normalize( v_NormalW );
gl_FragColor = textureCube( u_CubeMap, normalize( reflect( camRay, wNormal ) ) );

Thank you!

Update 1 Screenshot: (the unreflective sphere are not reflected in the other sphere) one reflective sphere

0条回答
登录 后发表回答