PDA

View Full Version : PerPixel Reflection troubles



Java Cool Dude
03-04-2005, 07:14 AM
As posted on Gamedev:
I've successfully implemented dynamic reflections in an OpenGL scene where I render the environment into a cubemap and then map onto an object. At first I had an issue where moving around the reflective object made the reflection map rotate towards the viewer, but plugging in the inverse modelview matrix in the first texture matrix seemed to fix that problem.
Then I decided to spice things up a bit by using GLSL to simulate a per-pixel reflection, things worked really well until I decided to rotate the reflective object a bit, that's when all hell broke loose: The reflection map seems to rotate by the same amount of the object instead of staying stationary, I tried multiplying few things by gl_TextureMatrix[0] but nothing seems to fix the said bug.

I'm posting my shaders code right here as well as links to the actual executables and the full source code with hope that someone out there can help me out.

PS: You can look into the Data/XML folder and check out the scenelayout.xml file that stores all the info describing the scene, you should be interested in the transforms affecting <Group name="ReflectiveGroup">

PS2: You can manually set shadersSupported to “false” at the end of “initialize” and look at how things are handled through the fixed OpenGL functionality.

<Shaders>

<VERTEX_SHADER>
<Uniform name = "cameraPosition" size = "4" type = "float"/>
<Uniform name = "bumpScale" size = "1" type = "float"/>

<RawData>
uniform vec4 cameraPosition;
varying mat3 normalMatrix;
uniform float bumpScale;

void main(void)
{
normalMatrix = mat3(gl_MultiTexCoord1.xyz*bumpScale,
gl_MultiTexCoord2.xyz*bumpScale,
gl_Normal);

gl_TexCoord[0] = gl_MultiTexCoord0;
gl_TexCoord[1] = cameraPosition - gl_Vertex;

gl_Position = ftransform();
}
</RawData>
</VERTEX_SHADER>

<FRAGMENT_SHADER>
<Uniform name = "Cube" size = "1" type = "int" x = "1"/>
<Uniform name = "Bump" size = "1" type = "int" x = "0"/>

<RawData>
uniform samplerCube Cube;
uniform sampler2D Bump;
varying mat3 normalMatrix;

void main(void)
{
vec3 viewDirection = normalize(gl_TexCoord[1].xyz),
bumpFragment = (texture2D(Bump, gl_TexCoord[0].xy)* 2.0 - 1.0).xyz,
normalVector = normalize(normalMatrix * bumpFragment ),
reflection = reflect(viewDirection, normalVector);
gl_FragColor = textureCube(Cube, -reflection);
}
</RawData>
</FRAGMENT_SHADER>
</Shaders> Binaries (http://www.realityflux.com/abba/C++/DynamicReflection/Application%20Reflection%20CM%20PPL.zip)
Source (http://www.realityflux.com/abba/C++/sEngine/SXMLEngine.zip)
http://www.realityflux.com/abba/C++/DynamicReflection/PPLRef.jpg