Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Need help with parallax mapping (C++/OpenGL/GLSL)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 anogio   Members   -  Reputation: 120

Like
0Likes
Like

Posted 06 June 2014 - 09:42 AM

New user here, so HI!

 

As the title suggests, I could use some assistance in implementing basic parallax occlusion mapping.

 

The image below shows the issue I am having:

parallaxerror.jpg

 

The wall on the right is where the artefact is (I've increased the parallax scale to make the artefact more visible). 

The mesh used is a simple plane. The wall on the left is rotation 90 degrees on the X axis, and is totally fine, but the wall on the right is rotated -90 degrees. I have tried rotating the wall +90 on X and then 180 on Y to orientate it correctly, which produces the same artefact.

 

I have followed the tutorials I found very closely, but as the image shows, the uv offsetting on the right wall is incorrect, and I can't figure out why.

 

My method is to transform the texture based normal out of tangent space, into world space (which you will see in the shader source below), and not modify the view vector in any way.

 

I could really use some help with this, as I really don't understand what is causing this error.

 

Shader source:

// -----------------------------------------------------------------------------------------------------------

// parallax vertex shader
#version 330

//per vertex inputs stored on GPU memory
in vec3 in_Position;
in vec3 in_Normal;
in vec2 in_TexCoord;
in vec4 tangent;

//per mesh data sent at rendering time
uniform mat4 model; //object to world space transformations
uniform mat4 view;    //world to eye space transformations
uniform mat4 projection; //eye space to screen space
uniform mat3 normalMat; //for transforming normals in

uniform vec4 lightPosition; //light position in world space
uniform vec3 eyePosition; //eye position in world space

//outputs to fragment shader
out VertexData
{
vec3 position; //vertex position in eye space
vec3 ex_L; //light vector
vec3 ex_V; //view direction
vec3 ex_N;
mat3 tbn; //TBN matrix for tangent/world transformations
vec2 ex_TexCoord;
float dist;
}    vertex;

void main(void)
{
//view space position for lighting calculations
vertex.position = vec3(model * vec4(in_Position,1.0));

//calculate view vector and light vector in world space
vertex.ex_V = normalize(eyePosition-vertex.position);
vertex.ex_L = lightPosition.xyz - vertex.position;

vertex.ex_TexCoord = in_TexCoord;

//now calculate tbn matrix, and transform the view & light vectors into tangent space
vertex.ex_N = (model * vec4( in_Normal,0.0)).xyz;
vec3 tan = (model * vec4( tangent.xyz,0.0)).xyz;
vec3 BiTan = cross(vertex.ex_N, tangent.xyz)*tangent.w;
vertex.dist = length(vertex.ex_L);

vertex.tbn = mat3(tan,BiTan,vertex.ex_N);


gl_Position = projection * view * vec4(vertex.position,1.0);

}
//End vertex shader
// ------------------------------------------------------------------------------------------------------------------------

// parallax fragment shader
#version 330

// Some drivers require the following
precision highp float;

struct lightStruct
{
vec4 ambient;
vec4 diffuse;
vec4 specular;
vec4 position;
float radius;
};

struct materialStruct
{
vec4 ambient;
vec4 diffuse;
vec4 specular;
vec4 emissive;
float shininess;
};
//uniforms sent at render time
uniform lightStruct light;
uniform materialStruct material;
uniform vec2 scaleBias;

//texture information

uniform sampler2D colourMap;
uniform sampler2D normalMap;
uniform sampler2D specularMap;
uniform sampler2D heightMap;
uniform sampler2D glossMap;

//inputs from vertex shader stage
in VertexData
{
vec3 position; //vertex position in eye space
vec3 ex_L; //light vector
vec3 ex_V; //view direction
vec3 ex_N;
mat3 tbn; //TBN matrix for tangent/world transformations
vec2 ex_TexCoord;
float dist;
}    vertex;
//final fragment colour
layout(location = 0) out vec4 out_Color;

void main(void) {
//calculate halfway vector for blinn phong
vec3 Half = normalize(vertex.ex_V + normalize(vertex.ex_L));
vec2 texCoord;
vec3 diffuseTexel ;
vec3 specularTexel;
float gloss;
vec3 normal;

//sample textures
//first get our parallax values
float height = texture(heightMap,vertex.ex_TexCoord.st).r;

height = height * scaleBias.r - scaleBias.g;

texCoord = vertex.ex_TexCoord.st + (normalize(vertex.ex_V).xy * height);

diffuseTexel = texture(colourMap,texCoord.st).rgb;

specularTexel = texture(specularMap,texCoord.st).rgb;



//normal maps are encoded with values between 0.0 and 0.5 being negative, so need to remove the encoding.
normal = vertex.tbn * normalize(texture(normalMap,texCoord.st).rgb * 2.0 - 1.0);

gloss = texture(glossMap,texCoord.st).r;

float lambert = dot(normal, normalize(vertex.ex_L));
vec3 litColour = vec3(0.0,0.0,0.0);
//if light is worth calculating
if (lambert > 0.0)
{
vec3 R = normalize(-reflect(vertex.ex_L,normal));
float spectral = pow(max(dot(R,vertex.ex_V),0.0),material.shininess );
litColour += light.diffuse.xyz * material.diffuse.xyz * lambert;
litColour += light.specular.xyz * material.specular.xyz * spectral * gloss;
float attenuation = 1.0 + (0.01 * vertex.dist*vertex.dist) + (0.01 * vertex.dist);
attenuation = 1.0/attenuation;

litColour *= attenuation;
}
out_Color = vec4( litColour *diffuseTexel*specularTexel ,1.0);
}

// End fragment shader
//---------------------------------------------------------------------------------------------------
 
I really hope someone can help with this.
Thanks in advance.


Sponsor:

#2 JohnnyCode   Members   -  Reputation: 373

Like
2Likes
Like

Posted 06 June 2014 - 11:09 AM

 

texCoord = vertex.ex_TexCoord.st + (normalize(vertex.ex_V).xy * height);

 

notice that vertex.ex_V is a world space vector, so it changes when you change the world matrix of object (rotate like you did). And now notice that

vertex.ex_TexCoord.st  is a object space vector that never changes. You now see the mistake, you should have the vertex.ex_V (the difference vector between world eye and world vertex position) in object space.

 

Do not transform vertex postion to world space, but keep it in object space and subtract the world eye position transformed by world inverse from it. This will yield object space view vector, to correctly shift texture coordinates with.

 

You can compute a world space inverse very easily and effectively by trannsponing rotation 3x3 part and multiplying 4th column.xyz (the position) by -1.



#3 anogio   Members   -  Reputation: 120

Like
0Likes
Like

Posted 06 June 2014 - 06:14 PM

Yes I see what you mean. Though I'm puzzled why no tutorial I've ever looked at does this, but then every tutorial I've seen is on a single mesh, rather than many, and usually done in eye space. I'll try it out and let you know how I get on.

 

Thanks!

 

Edit: After trying a few different things out, I discovered that the reason it wasn't being displayed correctly was that the view vector needs to be in tangent, not object space. I still thank you for your input, as it led me to the correct solution. If I could up vote you twice, I would!.


Edited by anogio, 07 June 2014 - 02:15 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS