Jump to content
  • Advertisement
Sign in to follow this  
Vincent_M

OpenGL Reflection Mapping

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to make reflections using OpenGL ES 2.0. I've almost implemented environment mapping, but as I understand it, as my camera's eye position and orientation changes over time, the angle that the reflection will change, but the image won't change correctly. Looks like the stencil buffer could be used for this, however, I'm thinking there might be a better way with GLSL. I've done some searching, and not sure if I should do a pure-shader approach, or use stencil buffers yet.

I'm looking more for direction on where to go than code. I should be able to find that out on my own once I know what I'm looking for.

Share this post


Link to post
Share on other sites
Advertisement
If you are using a cubemap, the reflected image will be rotated correctly as you move your camera and change the angle of your view. I am not sure what your problem is.
Other than that, you need to set the appropriate texture matrix and transform your cubemap texture coordinates by the matrix from within the shader.
Take the inverse of your view matrix, 0 out the positional components, and set that as the texture matrix for your cubemap texture slot.


L. Spiro

Share this post


Link to post
Share on other sites
Ok, as soon as I get my textures generating correctly, I'll do that. Quick recap though, this is a table of the past transformations:

world space = worldMat * a_vertex
eye space = viewMat * worldMat * a_vertex
clip space = projMat * viewMat * worldMat * a_vertex



Here's some code I found for environment maps:



//VERTEX SHADER
varying vec3 vTexCoord;
void main(void)
{
// Normal in Eye Space
vec3 vEyeNormal = gl_NormalMatrix * gl_Normal;
// Vertex position in Eye Space
vec4 vVert4 = gl_ModelViewMatrix * gl_Vertex;
vec3 vEyeVertex = normalize(vVert4.xyz / vVert4.w);
vec4 vCoords = vec4(reflect(vEyeVertex, vEyeNormal), 0.0);
// Rotate by flipped camera
vCoords = gl_ModelViewMatrixInverse * vCoords;
vTexCoord.xyz = normalize(vCoords.xyz);
// Don't forget to transform the geometry!
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}



//FRAGMENT SHADER
uniform samplerCube cubeMap;
varying vec3 vTexCoord;
void main (void)
{
gl_FragColor = texture(cubeMap, vTexCoord);
}


Looks like it properly transforms the normals so they can be used as cubemap coordinates.

I'll post back with the results when I have an hour of free time.

Share this post


Link to post
Share on other sites
I finally had the chance to implement the environment mapping shader I got onto a pillar, but I don't believe it turned out correctly.

Attached is a screenshot of what I currently have. Does this seem right?


EDIT: I realized that this is not correct. In fact, my cube map wasn't setup properly. I'll have to get that rendering right before I can continue.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!