perspective issuses with parallax mapping

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

Recommended Posts

Hello, i have a demo program, defferred shading + parallax steep maping(is it?), it works on distance, but when you are close to the surface parallax acts very ugly.

Everyhting is normalized, i am calculating in world coordinates.

#version 150

uniform mat4 projection;
uniform mat4 camera;
uniform mat4 model;

uniform vec3 camera_pos;

in vec3 vert_pos;
in vec2 vert_tex_coord;
in vec3 vert_normal;

in vec3 vert_tangent;
in vec3 vert_bitangent;

out vec3 frag_pos;
out vec3 frag_normal;
out vec2 frag_tex_coord;

out vec3 frag_tangent;
out vec3 frag_bitangent;

out vec3 frag_to_camera;

void main()
{
mat3 normal_mat = mat3(transpose(inverse(model)));

// applying model transformation

vec3 position = vec3(model * vec4(vert_pos, 1));

vec3 normal = normalize(normal_mat * vert_normal);

vec3 tangent = normalize(normal_mat * vert_tangent);
vec3 bitangent = normalize(normal_mat * vert_bitangent);

vec3 to_camera = normalize(camera_pos - position);

// out
gl_Position = projection * camera * model * vec4(vert_pos, 1);

frag_pos = position;
frag_normal = normal;
frag_tex_coord = vert_tex_coord;

frag_tangent = tangent;
frag_bitangent = bitangent;

frag_to_camera = vec3(dot(to_camera, tangent)
, dot (to_camera, bitangent)
, dot (to_camera, normal));

}

#version 150

in vec3 frag_normal;
in vec2 frag_tex_coord;
in vec3 frag_pos;

in vec3 frag_tangent;
in vec3 frag_bitangent;

in vec3 frag_to_camera;

uniform sampler2D normal_tex;
uniform sampler2D diff_tex;
uniform sampler2D spec_tex;
uniform sampler2D disp_tex;

out vec4 fbo_position;
out vec4 fbo_normal;
out vec4 fbo_diff;
out vec4 fbo_spec;

const float scale = 0.1;
const float n_steps = 32.0;

const float REFINE_EPS = 0.0001;
const int MAX_ITER = 10;

void main()
{
vec3 to_camera = normalize(frag_to_camera);
vec3 normal = normalize(frag_normal);

vec3 tangent = normalize(frag_tangent);
vec3 bitangent = normalize(frag_bitangent);

// setup layers loop
float step = 1.0 / n_steps;
vec2 tex_step = -1.0 * to_camera.xy * scale / (n_steps * to_camera.z);

float current_layer = 1.0;
vec2 cur_tex_coord = frag_tex_coord.xy;

float height = texture2D(disp_tex, cur_tex_coord).r;

// find correct layer
while(height < current_layer)
{
current_layer -= step;
cur_tex_coord += tex_step;
height = texture2D(disp_tex, cur_tex_coord).r;
}

// interpolate
vec2 prev_tex_coord = cur_tex_coord - tex_step;

float h_cur = height;
float h_prev = texture2D(disp_tex, prev_tex_coord).r;

float l_cur = current_layer;
float l_prev = current_layer + step;

float d1 = l_prev - h_prev;
float d2 = h_cur - l_cur;

vec2 tex_coord = prev_tex_coord + tex_step * d1 / (d1 + d2);

// altering parallax
vec3 tex_normal = (2.0*texture2D(normal_tex, tex_coord).xyz - vec3(1.0));

vec3 pos = frag_pos
+ normal * (texture2D(disp_tex, tex_coord).r - 1) * scale;

// forming output
fbo_position.x = 1.0 / (1.0 + exp(pos.x));
fbo_position.y = 1.0 / (1.0 + exp(pos.y));
fbo_position.z = 1.0 / (1.0 + exp(pos.z));

fbo_normal = vec4(normalize(tex_normal.x * tangent
+ tex_normal.y * bitangent
+ tex_normal.z * normal), 0);

fbo_diff = texture(diff_tex, tex_coord);
fbo_spec = texture(spec_tex, tex_coord);
}



it is not optimal, but i want to get working system first.

did anyone encountered similar problems? any help will be appreciated.

And another question, how is this method called?)

in different places it described as relief mapping/parallax mapping/parallax occlusion mapping..

Share on other sites

apparantly i found the error - i normalized normalize(camera_pos - position); in vertex shader and inerpolation screwed the results somehow

Edited by noname01

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 28
• 16
• 10
• 10
• 11
• Forum Statistics

• Total Topics
634106
• Total Posts
3015547
×