Jump to content

planar reflection won't stay put


havenphillip
 Share

Recommended Posts

Hey Josh how did you get the planar reflections to stop following the camera on the water? Seems like no matter what I do if I nod the cam "yes" the reflection wont' stay put. This is what I have. I'm stuck here:


vec2 coord = gl_FragCoord.xy/buffersize;

if (isbackbuffer) coord.y = 1-coord.y;

vec4 reflectcol = texture(texture10,coord);

fragData0 = reflectcol;

Link to comment
Share on other sites

Try flipping signs until it starts working. That's what I usually do when I'm stuck. :D

Also remember, the reflection camera matrix is going to be the same as the render projection matrix, except the pitch is reversed. And the render camera's Y scale should be -1.

  • Like 1
  • Confused 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

The reflection camera also needs to draw the opposite side of all faces, since flipping one component of the scale reverses the triangle winding order.

  • Like 1
  • Confused 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

  • 4 months later...

This is what I have:

VERTEX:

void main()
{
    //entity matrix
    mat4 entitymatrix = entity.matrix[gl_InstanceID];
    mat4 entitymatrix_ = entitymatrix;
    entitymatrix_[0][3]=0.0;
    entitymatrix_[1][3]=0.0;
    entitymatrix_[2][3]=0.0;
    entitymatrix_[3][3]=1.0;
    
    //model position
    ex_vertexposition = entitymatrix_ * vec4(vertex_position,1);
    gl_Position = projectioncameramatrix * ex_vertexposition;

    //clip
    float cam = cameraposition.y;
    float c = 2 * (cam - ex_vertexposition.y);    
    vec4 e = ex_vertexposition;
    e.y += c;
    clipspace = projectioncameramatrix * e;

    //normals
    mat3 nmat = mat3(entitymatrix_);
    ex_normal = normalize(nmat * vertex_normal);
    ex_binormal = normalize(nmat * vertex_binormal);    
    ex_tangent = normalize(nmat * vertex_tangent);

    ex_texcoords0 = vertex_texcoords0;
}

 

FRAGMENT:

 

void main()
{

    //Normal
    vec3 normal = ex_normal;
    normal = normalize(texture(texture1,ex_texcoords0).xyz * 2.0 - 1.0);
    normal = ex_tangent * normal.x + ex_binormal * normal.y + ex_normal * normal.z;    
    normal = normalize(normal);

    vec4 clip = clipspace;
    clip.xyz *= vec3(1,-1,1);
    clip.x = (clip.x / clip.w) * 0.5 + 0.5;
    clip.y = (clip.y / clip.w) * 0.5 + 0.5;

    //Reflection
    vec4 reflection = texture(texture10,clip.xy)*0.8;

    //Diffuse Output
    fragData0 = reflection;
}

 

 

Link to comment
Share on other sites

You normally just need to do something like this:

 

vec3 screencoord = vec3(gl_FragCoord.x/buffersize.x,gl_FragCoord.y/buffersize.y,1.0);	
screencoord.y = 1.0 - screencoord.y;

vec3 reflectionvector = screencoord;
vec3 reflection = textureProj(texture10,(reflectionvector + normal)).rgb;

This is how i have done it.

  • Like 1
  • Intel® Core™ i7-8550U @ 1.80 Ghz 
  • 16GB RAM 
  • INTEL UHD Graphics 620
  • Windows 10 Pro 64-Bit-Version
Link to comment
Share on other sites

How do you create the reflection texture? And how do you assign it? I am very sure that this will not work from within the editor as the scripts are not updated (except postprocessing).

  • Intel® Core™ i7-8550U @ 1.80 Ghz 
  • 16GB RAM 
  • INTEL UHD Graphics 620
  • Windows 10 Pro 64-Bit-Version
Link to comment
Share on other sites

11 hours ago, klepto2 said:

How do you create the reflection texture? And how do you assign it? I am very sure that this will not work from within the editor as the scripts are not updated (except postprocessing).

Just using the shader from above.  That's why I'm wondering if I need to use a script like a Render function or something. It does look like that in game too. The one I was using has a similar problem it looks ok until I look down then I get this. I dont' know what it is but it looks like maybe it has to do with the buffer not fitting to the screen?

reflectionprob.jpg

Link to comment
Share on other sites

Ok i have analysed the problem. 

You currently try to use the actual backbuffer as the reflection buffer. This will not work as the backbuffer will always align with the actual camera, where the reflection must be a real reflection texture based on the reflection-physics. The code would look something like this:

auto matrix = camera->GetMatrix(); // Save curent camera matrix
ocean->Hide(); // Hide the ocean, so it doesn't hide reflection geometry
camera->SetRotation(camera->GetRotation() * Vec3(-1.0, 1.0, -1.0)); 
camera->SetPosition(camera->position.x, -camera->position.y + 2 * waterheight, camera->position.z);
World::GetCurrent()->graphicsdriver->clipplane[0] = Vec4(0, -1.0, 0, waterheight); // Setup a clipplane to clip everything which is under water, otherwise you can see items under water in the reflection.

// Render World to reflection texture
// Reset camera, clipplane and show ocean
camera->SetMatrix(matrix);
World::GetCurrent()->graphicsdriver->clipplane[0] = Vec4(0.0);
ocean->Show();

//Render the scene as normal

As the clipplanes are only availabe in c++, this will not completely work from lua.

  • Like 1
  • Intel® Core™ i7-8550U @ 1.80 Ghz 
  • 16GB RAM 
  • INTEL UHD Graphics 620
  • Windows 10 Pro 64-Bit-Version
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...