Jump to content
  • Advertisement
Sign in to follow this  
MARS_999

Need some help with Reflection code GLSL

This topic is 4846 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 have tried and looked over this code and can't figure out what is wrong with it? I need help from someone who is more knowledgeable than myself.
//vertex shader
varying vec4 waterTex0, waterTex1, waterTex2, waterTex3, waterTex4;
uniform vec4 viewpos, lightpos;
uniform float time, time2;
//unit 0 = water_reflection
//unit 1 = water_refraction
//unit 2 = water_normalmap
//unit 3 = water_dudvmap
//unit 4 = water_depthmap

void main(void)
{
    vec4 mpos, temp;
    vec4 tangent = vec4(-1.0, .0, .0, .0);
    vec4 binormal = vec4(.0, .0, -1.0, .0);
    vec4 norm = vec4(.0, 1.0, .0, .0);
    mat4 mvp = gl_ModelViewProjectionMatrix;
    mat4 mtx = gl_TextureMatrix[0];

    waterTex0 = gl_MultiTexCoord0;
    waterTex1 = gl_MultiTexCoord1;
    waterTex2 = gl_MultiTexCoord2;
    waterTex3 = gl_MultiTexCoord3;
    waterTex4 = gl_MultiTexCoord4;

    temp = viewpos - gl_Vertex;
    waterTex4.x = dot(temp, tangent);
    waterTex4.y = dot(temp, binormal);
    waterTex4.z = dot(temp, norm);
    waterTex4.w = 1.0;
    
    temp = lightpos - gl_Vertex;//SUB temp, lightpos, vertex.position;

    waterTex0.x = dot(temp, tangent);
    waterTex0.y = dot(temp, binormal);
    waterTex0.z = dot(temp, norm);
    waterTex0.w = 1.0;

    mpos.x = dot(mvp[0], gl_Vertex);
    mpos.y = dot(mvp[1], gl_Vertex);
    mpos.z = dot(mvp[2], gl_Vertex);
    mpos.w = dot(mvp[3], gl_Vertex);
       
    waterTex1.x = waterTex0.x;
    waterTex1.y = waterTex0.y + time;
    waterTex1.z = waterTex0.z;
    waterTex1.w = waterTex0.w;

    waterTex2.x = waterTex0.x;
    waterTex2.y = waterTex0.y + time2;
    waterTex2.z = waterTex0.z;
    waterTex2.w = waterTex0.w;

    waterTex3.x = dot(mtx[0], mpos);
    waterTex3.y = dot(mtx[1], mpos);
    waterTex3.z = dot(mtx[2], mpos);
    waterTex3.w = dot(mtx[3], mpos);
    
    gl_Position = ftransform();
}

//fragment shader
uniform sampler2D water_normalmap, water_reflection, water_refraction, water_dudvmap, water_depthmap;
varying vec4 waterTex0, waterTex1, waterTex2, waterTex3, waterTex4;
uniform vec4 waterColor, waterDepth;
//unit 0 = water_reflection
//unit 1 = water_refraction
//unit 2 = water_normalmap
//unit 3 = water_dudvmap
//unit 4 = water_depthmap

void main(void)
{
     const vec4 sca = vec4(005, .005, .005, .005);
     const vec4 sca2 = vec4(.02, .02, .02, .02);
     const vec4 tscale = vec4(.5, .5, .5, .5);
     const vec4 two = vec4(2.0, 2.0, 2.0, 2.0);
     const vec4 mone = vec4(-1.0, -1.0, -1.0, -1.0);
   /*  
     const vec4 cyan = vec4(.0, .2, .0, 1.0);
     const vec4 dcyan = vec4(1.0, 1.0, 1.0, 1.0);
     const vec4 cyf = vec4(.2, .2, .2, 1.0);
*/
     const float exponent = 128.0;

     vec4 lightTS = normalize(waterTex0);
     vec4 viewt = normalize(waterTex4);
     vec4 disdis = texture2D(water_dudvmap, vec2(waterTex2 * tscale)) * sca2;
     vec4 tmp = waterTex1 + disdis;
     vec4 fdist = (texture2D(water_normalmap, tmp.xy) * two) + mone;
     vec4 nmap = normalize(texture2D(water_dudvmap, tmp.xy));
     vec4 lref = reflect(nmap, lightTS);

     tmp = vec4(1.0 / waterTex3.w);
     
     vec3 projCoord = vec3(waterTex3) * vec3(tmp);
     projCoord += vec3(1.0);
     projCoord *= vec3(0.5);
     projCoord += fdist.xyz;
     projCoord = clamp(projCoord, 0.001, 0.999);

     vec4 refl = texture2D(water_reflection, projCoord.xy);
     vec4 refr = texture2D(water_refraction, projCoord.xy);
     vec4 wdepth = texture2D(water_depthmap, projCoord.xy) * waterDepth;
 
     wdepth = pow(wdepth, vec4(4.0));
     vec4 invdepth = 1.0 - wdepth;
  
     float specular = dot(lref, viewt);
     specular = pow(specular, exponent);
     
     float invfres = dot(nmap, viewt);
     float fres = 1.0 - invfres;
     //float fres = 1.0 - dot(nmap, viewt);
     //float invfres = 1.0 - fres;

     //calculate reflection and refraction
     refr = refr * invfres;
     refr = refr * invdepth;
     tmp = waterColor * wdepth;
     tmp = tmp * invfres;
     //tmp = tmp  + refl;
     refr = tmp + refr;
     refl = refl * fres;     //was commented out

     //add reflection and refraction
     tmp = refl + refr;

     //add specular highlight
    //gl_FragColor = tmp + specular; //when this is uncommented I get black water
    gl_FragColor = tmp; //comment out this when using ADD statement above
}

screenshot http://img293.imageshack.us/my.php?image=untitled1copy9pl.jpg

Share this post


Link to post
Share on other sites
Advertisement
Ah come on no one here has any idea??? I have looked at the code over and over and it looks right. It must have something to do with the math that is above my head...

Share this post


Link to post
Share on other sites
Seeing as this has gone unanswered for so long, and even a small suggestion may assist you in cracking this problem... (correct me if I'm wrong in anything I say)

Having recently done battle with similar issues setting up my ocean reflection/refraction shaders, I can suggest the following:

1) Simplify your case. Start off with getting reflection working. If reflection works, you are assured that you are working in the right space, and the extra stuff should be easy. Which leads onto the main suggestion...

2) Closely examine your space logic. Reflection calculations need to be done in eye space. This means your camera -> vertex vector and surface normal (at the very least) must be in eye space before you feed them into reflect().

You seem to be using the Texture matrices to pass in the appropriate transformation matrices (and you have not supplied any code for how you are setting these texture matrices - I would assume you're using TexGen and applying the scale by 2 / translate).

I would recommend closely examining this matrix - if the numbers aren't right, reflection is gonna be messed up, and looks stretched out like it seems to be in your screenshot.

If you're having trouble getting appropriate values into your texture matrices, I recommend calculating the reflection entirely in the vertex/pixel shader.

Hope this helps.

Hew T.

Share this post


Link to post
Share on other sites
Thanks for the help. I had a few things messed up I was sending the projCoord to the reflection and refraction texture lookups without bring in my normalmap into the equation and I forgot to do some math on the normalmap and normalize the other normalmap. Whew. As of now it looks correct, but I have to do something about my water looking to much like my sky... Any ideas??

Thanks for the reply...

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!