Simply reflections?

Ok, i begin to post the code :



com/jmex/effects/reflect/data/reflectionshader.vert



varying vec2 refrCoords;
varying vec2 normCoords;
varying vec4 viewCoords;
varying vec3 viewTangetSpace;

uniform vec3 tangent;
uniform vec3 binormal;
uniform float normalTranslation;

void main()
{
   // Because we have a flat plane for water we already know the vectors for tangent space
   vec3 normal = gl_NormalMatrix * gl_Normal;
   normal = normalize(normal);
   vec3 tangent2 = gl_NormalMatrix * tangent;
   tangent2 = normalize(tangent2);
   vec3 binormal2 = gl_NormalMatrix * binormal;
   binormal2 = normalize(binormal2);

   // Calculate the vector coming from the vertex to the camera
   vec4 v = gl_ModelViewMatrix * gl_Vertex;
   vec3 viewDir = -(v.xyz/v.w);
   viewDir = normalize(viewDir);

   // Compute tangent space for the view direction
   viewTangetSpace.x = dot(viewDir, tangent2);
   viewTangetSpace.y = dot(viewDir, binormal2);
   viewTangetSpace.z = dot(viewDir, normal);

   refrCoords = gl_MultiTexCoord0.xy * vec2(0.8);
   normCoords = gl_MultiTexCoord0.xy + vec2(0.0,normalTranslation);

   // This calculates our current projection coordinates
   viewCoords = ftransform();
   gl_Position = viewCoords;
}



com/jmex/effects/reflect/data/reflectionshader.frag


varying vec4 viewCoords;

uniform sampler2D reflection;

uniform int infrontof;

void main() {
   vec4 projCoord = viewCoords / viewCoords.q;
   projCoord = (projCoord + 1.0) * 0.5;
   if ( infrontof == 1 ) {
      projCoord.x = 1.0 - projCoord.x;
   }
   
   vec4 reflectionColor = texture2D(reflection, projCoord.xy);
   if ( infrontof == 0 ) {
      reflectionColor *= vec4(0.5,0.6,0.7,1.0);
   }
   
   gl_FragColor = reflectionColor;
}


com/jmex/effects/reflect/ReflectRenderPass.java



/*
 * Copyright (c) 2003-2007 jMonkeyEngine
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *

Updated.

The problem is another :

it does not reflects the shadows…

Without the normal :



Nope, the depth is gone but the normal stays.

Corrected the code, but, I left the depthmap : without the depthmap the reflection appears illuminated…

To make the vertical mirror I used this code :



Quad quad = new Quad("quad", 20, 50);
reflectRenderPass.setReflectPlane( new Plane( new Vector3f( 0.0f, 0.0f, 1.0f ), 0.0f ) );
reflectRenderPass.setTangent( new Vector3f( 1.0f, 0.0f, 0.0f ) );
reflectRenderPass.setBinormal( new Vector3f( 0.0f, 1.0f, 0.0f ) );
      
reflectRenderPass.setPlaneHeight(0);
      
Vector3f tmpVec = new Vector3f(0,0,1);
quad.getLocalRotation().multLocal( tmpVec );
reflectRenderPass.getNormal().set( tmpVec );

float dist = reflectRenderPass.getNormal().dot( quad.getLocalTranslation() );      
reflectRenderPass.setPlaneHeight( dist );
scene.attachChild(quad);      
reflectRenderPass.setReflectEffectOnSpatial(quad);

you don't need the refraction, depth or normalmap textures anymore either. can be removed from both the pass and the shaders

you need to modify the shader too…for pure reflection, this is all you need:



vec4 projCoord = viewCoords / viewCoords.q;
   projCoord = (projCoord + 1.0) * 0.5;
   if ( abovewater == 1 ) {
      projCoord.x = 1.0 - projCoord.x;
   }
   
   vec4 reflectionColor = texture2D(reflection, projCoord.xy);
   if ( abovewater == 0 ) {
      reflectionColor *= vec4(0.5,0.6,0.7,1.0);
   }
   
   gl_FragColor = reflectionColor;

the deth is not even uploaded to the shader in your code, so that should not affect anything…

you don't need the normal either…

and you changed the shader to this?



varying vec4 viewCoords;

uniform sampler2D reflection;

uniform int infrontof;

void main()
{
   vec4 projCoord = viewCoords / viewCoords.q;
   projCoord = (projCoord + 1.0) * 0.5;
   if ( infrontof == 1 ) {
      projCoord.x = 1.0 - projCoord.x;
   }
   
   vec4 reflectionColor = texture2D(reflection, projCoord.xy);
   if ( infrontof == 0 ) {
      reflectionColor *= vec4(0.5,0.6,0.7,1.0);
   }
   
   gl_FragColor = reflectionColor;
}

@elettrozero: Thanks for that cool effect!



I had no time to try it out yet, but one question bugs me: What happens if you see a mirror in the mirror?  :?

…your computer explodes. :o

don't do it! You could create a fluctuation in the space-time-continuum!!!

Fire the photon torpedoes!  XD

Why does the reflection (it's valid for water too), doesn't reflect the shadows ??

because the water/reflection doesnt support taking a complete renderpass(eg shadowrenderpass) to use when rendering into the reflectionmap…it only takes nodes, as it is now…

I was able to get reflection working  :smiley: but how would I add a texture to the spatial so it doesnt look like a mirror? I tried adding a wood texture with

setPassState(ts);

but it gets displayed in the reflection not on the spatial. ive also tried adding it in the texture state like this:

ts.setTexture(woodTex,0);

ts.setTexture(textureReflect, 1);

and had no luck, do I have to add a line in the shader code to get the texure to not be a reflection?



edit: I got it  :slight_smile: added

gl_TexCoord[0]  = gl_MultiTexCoord0;

to the vertex shader and

uniform sampler2D tex;

vec4 reflectionColor = 0.5 * (texture2D(reflection, projCoord.xy)+texture2D(tex,gl_TexCoord[0].st));

to the fragment shader

Strange thing…

Testing it I can see only characters map instead reflection… why?