# reflections with GLSL

This topic is 4743 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I'm afraid my problem is something that has been discussed much, but either I'm birdbrained or my setting does not fit in most cases. :\ Task: Take Cubemap and get correct reflection vectors for an object inside the map. The Cubemap is no problem at itselft, it works nice, but the Reflection-Vectors are. A took the folowing spectations: - When using simply gl_Vertex and I take a Cubemaplookup, the Cubemap is mapped on the Object. This works as expected with a simple thing like a sphere, but does not work good on complex objects like a VRML-Tree that uses a lot of internal transformations. - When using the modelview-transform gl_Vertex (pos=gl_ModelViewMatrx*gl_Vertex), the "Reflections" are screen-aligned, that means you never can see the "backside"-Reflection if you rotate the object, your always faced to one half of the map. (also understandable, since Vertices are always transformed in the same results to be shown on screen). - I tried some other methods, used often, but no one ever did it for me (using cam-position and so on). I'll have to add that I have to use a Library that traverses VRML-Trees, so I can not be sure what Object-Transformations are done. Thus, I would prefer a solution that only depends on the Camera-Transformation (means ModelView). Does anyone has an idea or got to a similar problem once? Any help would be highly appreciated! Thank you!

##### Share on other sites
This should be simple as I've done it tons of times;

a)1st mehtod (not so sure about it):

vec4 viewVector = gl_ModelViewMatrix * -gl_Vertex;
vec3 newNormal = gl_NormalMatrix * gl_Normal;
reflection = reflect(viewVector.xyz, newNormal);

reflection = gl_TextureMatrix[0]*reflection; //Where gl_TextureMatrix holds the inverse modelview matrix with zero translation components.

b)2nd method that I'm totally sure of the correct outcome:
Pass the cumulative transform matrix from the beginning of the stack all the way down to your shape transforms.

      uniform vec4  worldTransform0;      uniform vec4  worldTransform1;      uniform vec4  worldTransform2;      uniform vec4  worldTransform3;      uniform vec4  cameraPosition;        void main(void)      {                mat3 rotations   = mat3(worldTransform0.xyz,                                worldTransform1.xyz,                                worldTransform2.xyz);        vec4 worldSpaceVertexPos = vec4(worldTransform0.x * gl_Vertex.x +                                         worldTransform1.x * gl_Vertex.y +                                        worldTransform2.x * gl_Vertex.z +                                         worldTransform3.x,                                        worldTransform0.y * gl_Vertex.x +                                        worldTransform1.y * gl_Vertex.y +                                        worldTransform2.y * gl_Vertex.z +                                         worldTransform3.y,                                        worldTransform0.z * gl_Vertex.x +                                        worldTransform1.z * gl_Vertex.y +                                        worldTransform2.z * gl_Vertex.z +                                        worldTransform3.z, 0.0);               gl_TexCoord[0]      = gl_MultiTexCoord0;        gl_TexCoord[1]      = cameraPosition  -  worldSpaceVertexPos;        gl_TexCoord[2].xyz  = rotations*gl_Normal;        gl_Position         = ftransform();      }

• 40
• 15
• 10
• 23
• 19