我的反思错了。我已经改变了很多代码,但是我没有找到错误。如果你可以帮助我会很棒。
纹理生成: 代码:
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 );
遍历所有面(0&lt; = p_face&gt; = 6)并在此之后绘制: 代码:
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(使用的世界位置): 代码:
vec3 camRay = normalize( v_PosW - u_CameraCenter );
vec3 wNormal = normalize( v_NormalW );
gl_FragColor = textureCube( u_CubeMap, normalize( reflect( camRay, wNormal ) ) );
谢谢!
更新1 屏幕截图:(非反射球体不反映在另一个球体中)