Hi at all!
I'm trying to create a water effect in OpenGL.
In my Water class, I have two FBOs: one for reflection and one for refraction.
So, I render the scene flipped with the clip distance on the reflection FBO, and the refraction scene with the clip plane only on its FBO.
Then, I do a projective texture mapping in the fragment shader with some distortion using a DU/DV map, which is the derivative of a normal map (I downloaded one online).
I got it working, along with the fresnel effect (which I discovered to be very simple XD).
The problem is that on these texture appears some black pixels. I really don't know why!!
I thought that it was due to the texture itself, but they appear as I try to rise the distortion.
Just take a look:
This is the vertex shader:
#version 330 core
layout(location = 0) in vec3 vertexPos;
layout(location = 1) in vec2 vertexUV;
out vec4 clipSpace;
out vec2 textureCoords;
out vec3 cameraVector;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
uniform vec3 traslation;
uniform vec3 cameraPos;
void main()
{
vec4 worldPosition = vec4(vertexPos + traslation, 1.0);
mat4 MVP = projection * view * model;
gl_Position = MVP * worldPosition;
// Copy values in out variables
clipSpace = gl_Position;
textureCoords = vertexUV;
// Calculate the camera vector by subtracting two points in world space
cameraVector = cameraPos - worldPosition.xyz;
}
And this is the fragment shader:
#version 330 core
out vec4 color;
in vec4 clipSpace;
in vec2 textureCoords;
in float cameraAngle;
uniform sampler2D reflectionTexture;
uniform sampler2D refractionTexture;
uniform sampler2D DUDVmap;
uniform vec3 cameraVector;
uniform float time;
const float WAVE_STRENGTH = 0.0075;
void main()
{
// Normalized device space, divide all by the W component
vec2 ndc = (clipSpace.xy / clipSpace.w) * 0.5 + 0.5;
// Store coordinates - computationally is pretty useless
vec2 coords = vec2(ndc.x, ndc.y);
// Two distortion make the effect more real
vec2 distortion1 = texture2D(DUDVmap, vec2(textureCoords.x + time, textureCoords.y + time)).rg * 2.0 - 1.0;
vec2 distortion2 = texture2D(DUDVmap, vec2(- textureCoords.x + time, - textureCoords.y + time)).rg * 2.0 - 1.0;
vec2 totalDistortion = distortion1 + distortion2;
// Add distortion
coords += totalDistortion * WAVE_STRENGTH;
// Clamp distortion, because of NDC border
coords = clamp(coords, 0.001, 0.999);
// Get reflection and refraction color vector
vec4 reflectColor = texture2D(reflectionTexture, coords);
vec4 refractColor = texture2D(refractionTexture, coords);
// Calculate the cosine between the quad normal and camera vector
float theta = dot(normalize(cameraVector), vec3(0.0, 0.0, 1.0));
// Mix up the reflection with the refraction by the above factor
color = mix(reflectColor, refractColor, theta);
}
Please, help me. I don't know why this happens!
Thanks in advance!
EDIT: I SOLVED!!! This topic can be closed!
The problem was the wrapping of the texture. Now I set it to GL_CLAMP_TO_EDGE and now it's fine!