Sergey K.

09-14-2006, 12:16 PM

I've got a strange rendering problem with my reflections. I render the world into

an offscreen framebuffer and then apply a texture to an object. But it seems to

be oriented somehow wrong. I guess it could be a problem with a correct modelview

matrix calculation, but i was unable to fix it. Could anybody provide an idea what

is going wrong and how to fix it?

http://www.linderdaum.com/Bugs/bug_refl1.jpg

http://www.linderdaum.com/Bugs/bug_refl2.jpg

Texture matrix calculation looks as follows:

LMatrix4 clGLRenderDevice::ProjectReflectionTexture( const LMatrix4& Projection, const LMatrix4& ModelView ) const

{

LMatrix4 Scale;

LMatrix4 Translate;

Translate.TranslationMatrix( LVector3(0.5f, 0.5f, 0.0f) );

Scale.ScaleMatrix( LVector3(0.5f, 0.5f, 1.0f) );

LMatrix4 TexProjectionMatrix = ModelView * Projection * Scale * Translate;

return TexProjectionMatrix;

}Vertex program:

varying vec3 vScaledPosition;

varying vec3 vViewVec;

varying vec3 vNormalES;

varying vec2 TexCoord0;

varying vec4 TexCoord1;

void main()

{

gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

/* Just output model coordinates for this so marble doesn't swim all over */

vScaledPosition = gl_Vertex.xyz * 0.5;

/* Put position and normal in eye space */

vViewVec = vec3(gl_ModelViewMatrix * gl_Vertex);

vViewVec.z *=-1.0;

vNormalES = gl_NormalMatrix * gl_Normal;

TexCoord0 = vec2(gl_MultiTexCoord0);

TexCoord1 = gl_TextureMatrix[0] * gl_Vertex;

}Fragment program:

uniform sampler2D Texture0;

uniform sampler3D Texture1;

uniform sampler2D Texture2;

varying vec3 vScaledPosition;

varying vec3 vViewVec;

varying vec3 vNormalES;

varying vec2 TexCoord0;

varying vec4 TexCoord1;

void main()

{

vec4 color = vec4(1.0, 0.942717, 0.125152, 1.0);

vec4 light_pos = vec4( 5.6, 100.0, 2.6, 1.0 );

vec4 light_color = vec4( 1.0, 1.0, 1.0, 1.0 );

vec4 lightDir = vec4( 0.408250, 0.408250, 0.8165, 0.0 );

float board = float( texture2D(Texture0, TexCoord0) );

float noisy = texture3D(Texture1, vScaledPosition).x;

/* Base marble color */

float marble = (0.2 + 5.0 * abs(noisy - 0.5));

vec3 normal = normalize(vNormalES);

/* Simple lighting */

vec3 reflectionVec = reflect(-normalize(vViewVec), normal);

float diffuse = 0.5 * dot(lightDir.xyz, normal) + 0.5;

float specular = pow(clamp(dot(reflectionVec, lightDir.xyz), 0.0, 1.0), 24.0);

vec4 ReflectionColor = texture2DProj( Texture2, TexCoord1 );

vec4 BoardColor = vec4(diffuse * board) + vec4(0.2 * marble) + vec4(marble * specular);

gl_FragColor = 0.7 * BoardColor + 0.3 * ReflectionColor;

}

an offscreen framebuffer and then apply a texture to an object. But it seems to

be oriented somehow wrong. I guess it could be a problem with a correct modelview

matrix calculation, but i was unable to fix it. Could anybody provide an idea what

is going wrong and how to fix it?

http://www.linderdaum.com/Bugs/bug_refl1.jpg

http://www.linderdaum.com/Bugs/bug_refl2.jpg

Texture matrix calculation looks as follows:

LMatrix4 clGLRenderDevice::ProjectReflectionTexture( const LMatrix4& Projection, const LMatrix4& ModelView ) const

{

LMatrix4 Scale;

LMatrix4 Translate;

Translate.TranslationMatrix( LVector3(0.5f, 0.5f, 0.0f) );

Scale.ScaleMatrix( LVector3(0.5f, 0.5f, 1.0f) );

LMatrix4 TexProjectionMatrix = ModelView * Projection * Scale * Translate;

return TexProjectionMatrix;

}Vertex program:

varying vec3 vScaledPosition;

varying vec3 vViewVec;

varying vec3 vNormalES;

varying vec2 TexCoord0;

varying vec4 TexCoord1;

void main()

{

gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

/* Just output model coordinates for this so marble doesn't swim all over */

vScaledPosition = gl_Vertex.xyz * 0.5;

/* Put position and normal in eye space */

vViewVec = vec3(gl_ModelViewMatrix * gl_Vertex);

vViewVec.z *=-1.0;

vNormalES = gl_NormalMatrix * gl_Normal;

TexCoord0 = vec2(gl_MultiTexCoord0);

TexCoord1 = gl_TextureMatrix[0] * gl_Vertex;

}Fragment program:

uniform sampler2D Texture0;

uniform sampler3D Texture1;

uniform sampler2D Texture2;

varying vec3 vScaledPosition;

varying vec3 vViewVec;

varying vec3 vNormalES;

varying vec2 TexCoord0;

varying vec4 TexCoord1;

void main()

{

vec4 color = vec4(1.0, 0.942717, 0.125152, 1.0);

vec4 light_pos = vec4( 5.6, 100.0, 2.6, 1.0 );

vec4 light_color = vec4( 1.0, 1.0, 1.0, 1.0 );

vec4 lightDir = vec4( 0.408250, 0.408250, 0.8165, 0.0 );

float board = float( texture2D(Texture0, TexCoord0) );

float noisy = texture3D(Texture1, vScaledPosition).x;

/* Base marble color */

float marble = (0.2 + 5.0 * abs(noisy - 0.5));

vec3 normal = normalize(vNormalES);

/* Simple lighting */

vec3 reflectionVec = reflect(-normalize(vViewVec), normal);

float diffuse = 0.5 * dot(lightDir.xyz, normal) + 0.5;

float specular = pow(clamp(dot(reflectionVec, lightDir.xyz), 0.0, 1.0), 24.0);

vec4 ReflectionColor = texture2DProj( Texture2, TexCoord1 );

vec4 BoardColor = vec4(diffuse * board) + vec4(0.2 * marble) + vec4(marble * specular);

gl_FragColor = 0.7 * BoardColor + 0.3 * ReflectionColor;

}