• Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at \$59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.

Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!

# noname01

Member Since 27 Mar 2012
Offline Last Active Dec 19 2013 11:34 AM

### perspective issuses with parallax mapping

14 December 2013 - 01:25 PM

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..

### question about lambert lighting model

27 March 2012 - 07:02 AM

Hello, i have simple shader, that makes perpixel lighting from point light (lambert - max(dot(normal, light), 0), everything is normalized) and it works fine, but when i put the light very close to a surface - light area shrinks.
Mathmetically it seems to be correct, but this effect is ugly.
Attenuation factor helps on far distance, but not on close.
If needed, i can post the code, but i think it is about the lighting model. How to 'fix' this effect?

PARTNERS