anogio

06-05-2014, 12:44 PM

New user here, so Hi!

As the title suggests I am having some trouble implementing parallax mapping in GLSL.

The problem occurs when I apply a rotation on the Y axis to my mesh. A picture says a thousand words, so I prepared a screenshot for clarity:

1333

I've increased the scale for clarity, so ignore that particular artefact.

On the left side of the image, you can see that the offsetting is in the correct direction. However, on the right, it is incorrect, offsetting up instead of down, based on viewing angle. The only difference is that I am applying a -90 degree rotation to the model matrix instead of +90. I have also tried +90, then rotating 180 degrees on the Y axis, which yields the same results.

My shader code is as follows, if there is any mistakes I am making, please let me know:

// tangent.vert

#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);

}

// Phong fragment shader phong-tex.frag matched with phong-tex.vert

#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);

}

I'm really puzzled by this problem, and would really appreciate any advice you could offer.

Thank you in advance for your help.

As the title suggests I am having some trouble implementing parallax mapping in GLSL.

The problem occurs when I apply a rotation on the Y axis to my mesh. A picture says a thousand words, so I prepared a screenshot for clarity:

1333

I've increased the scale for clarity, so ignore that particular artefact.

On the left side of the image, you can see that the offsetting is in the correct direction. However, on the right, it is incorrect, offsetting up instead of down, based on viewing angle. The only difference is that I am applying a -90 degree rotation to the model matrix instead of +90. I have also tried +90, then rotating 180 degrees on the Y axis, which yields the same results.

My shader code is as follows, if there is any mistakes I am making, please let me know:

// tangent.vert

#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);

}

// Phong fragment shader phong-tex.frag matched with phong-tex.vert

#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);

}

I'm really puzzled by this problem, and would really appreciate any advice you could offer.

Thank you in advance for your help.