Skip to main content
Move c++ and glsl from title to tags, plus minor typos
Source Link

NormalMap problems: bumps work, but shade does not c++/glsl

I've been trying to figure this out for a few days now, but I just cantcan't get it right. IveI've read multiple tutorials and possible explanations, I even watched a java tutorial without finding the solution. This is what I do:

I only got this result: //the shaders are updated as I went along, so the ones posted above work (for me anyway)the shaders are updated as I went along, so the ones posted above work (for me anyway)

Which has 2 obvious errors: When I rotate the object, the shading doesntdoesn't follow the lightsource+goeslightsource and goes from dark to full bright, and, it seems to have dents at seams where the cube-sphere wraps. The normalmapping seems to work thothough, but again I struggle with the shading. Is there an obvious error here?

NormalMap problems: bumps work, but shade does not c++/glsl

I've been trying to figure this out for a few days now, but I just cant get it right. Ive read multiple tutorials and possible explanations, I even watched a java tutorial without finding the solution. This is what I do:

I only got this result: //the shaders are updated as I went along, so the ones posted above work (for me anyway)

Which has 2 obvious errors: When I rotate the object, the shading doesnt follow the lightsource+goes from dark to full bright, and, it seems to have dents at seams where the cube-sphere wraps. The normalmapping seems to work tho, but again I struggle with the shading. Is there an obvious error here?

NormalMap problems: bumps work, but shade does not

I've been trying to figure this out for a few days now, but I just can't get it right. I've read multiple tutorials and possible explanations, I even watched a java tutorial without finding the solution. This is what I do:

I only got this result: the shaders are updated as I went along, so the ones posted above work (for me anyway)

Which has 2 obvious errors: When I rotate the object, the shading doesn't follow the lightsource and goes from dark to full bright, and, it seems to have dents at seams where the cube-sphere wraps. The normalmapping seems to work though, but again I struggle with the shading. Is there an obvious error here?

edited tags
Link
added 592 characters in body
Source Link
Charlie
  • 203
  • 1
  • 7
//this parsing has not been updated, unlike the shaders, correct parsing in the answer below
float f;
vec3 edge1, edge2, tangentbuf;
vec2 deltaUV1, deltaUV2;
//for each face, x=vert,y=uv, z=normal
for (size_t i = 0; i < totalrefs.size(); i += 9)
{
    edge1 = verticeref[totalrefs[i + 3] - 1] - verticeref[totalrefs[i] - 1];
    edge2 = verticeref[totalrefs[i + 6] - 1] - verticeref[totalrefs[i] - 1];
    deltaUV1 = texkoordref[totalrefs[i + 4] - 1] - texkoordref[totalrefs[i + 1] - 1];
    deltaUV2 = texkoordref[totalrefs[i + 7] - 1] - texkoordref[totalrefs[i + 1] - 1];
    f = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV2.x * deltaUV1.y);

    tangentbuf = f*vec3(
        (deltaUV2.y * edge1.x - deltaUV1.y * edge2.x),
        (deltaUV2.y * edge1.y - deltaUV1.y * edge2.y),
        (deltaUV2.y * edge1.z - deltaUV1.y * edge2.z));

    normalize(tangentref[tangentcount++]);
    tangentref[totalrefs[i] - 1] += vec4(normalize(tangentbuf), 1);
    tangentref[totalrefs[i + 3] - 1] += vec4(normalize(tangentbuf), 1);
    tangentref[totalrefs[i + 6] - 1] += vec4(normalize(tangentbuf), 1);
}

//for (auto &x : tangentref)x = normalize(x / x.w); //find average
for (auto &x : tangentref)x = normalize(x); //just normalize is enough?
#version 400
layout ( location = 0 ) in vec3 vertex_position;
layout ( location = 1 ) in vec2 tex_cord;
layout ( location = 2 ) in vec3 vertex_normal;
layout ( location = 3 ) in vec3 vertex_tangent;

uniform mat4 model; //model matrix, aka model 2 world

uniform mat4 modelviewmatrix; //view*model matrix
uniform mat3 normalmatrix; // transpose/invert(view*model)
uniform mat4 priv_mat; //projection-view-model pre multiplied

uniform vec4 LightPosition; //after view*lightpos, akaie viewspace

out vec2 fUV; //fragmentvec3 uvlightdir;
out vec3 lightdir;viewdir;
out //tangentvec2 lightdirfUV;
out vec3 viewdir; //tangent viewdirposition;

void main()
{
    gl_Position=priv_mat*vec4(vertex_position,1.0);
    fUV=tex_cord;
    position=(model*vec4(vertex_position,1.0)).xyz;
    len=length(vertex_position);

    // Transform normal and tangent to eye space
    vec3 normn = normalize(normalmatrix*vertex_normalnormalmatrix * vertex_normal);
    vec3 tangt = normalize(normalmatrix*vec3normalmatrix * vec3(vertex_tangent));
         tangt = normalize(tang t- dot(tangt, normn) * norm*n);  //Gramm-Schmidt process
    vec3 binormalbitangent = normalize( cross(tang n,norm t ) );
 
    //matrixmat3 fortbn transformation= totranspose(mat3(t,bitangent,n)); tangent//why spacetranspose??

    mat3vec3 tbn=mat3pos=vec3(tang,binormalmodelviewmatrix*vec4(vertex_position,norm1.0));

    //turn Transform light dir. and view dir. to tangent space
    vec3 poslightdir = vec3(modelviewmatrix*vec4normalize(vertex_position,1.0));
   tbn lightdir* = normalize(tbn*(LightPosition.xyz - pos));
    viewdir  =tbn*normalize(-pos); //tangent view

    fUVviewdir = tex_cord; //uv
    gl_Positiontbn =* priv_mat*vec4normalize(vertex_position,1.0-pos); //model
}
#version 400

in vec2float fUV;len;
in vec3 lightdir; //tang light
in vec3 viewdir; //tang view
in vec2 fUV;
in vec3 position;

uniform sampler2D png_tex;
uniform sampler2D bump_map; 

uniform vec4 LightPosition;
uniform vec3 LightIntensity;

uniform vec3 Kd;            // Diffuse reflectivity
uniform vec3 Ka;            // Ambient reflectivity
uniform vec3 Ks;            // Specular reflectivity
uniform float Shininess;    // Specular shininess factor

out vec4 Color;
vec3 phongModel( vec3 norm, vec3 diffr );

void main()
{
    vec4 b_normal = (255.0/128.0) * texture( bump_map, fUV )-1.0; // Lookup the normal from the normal map
    vec4 texColor = texture( png_tex, fUV ); // The color texture is used as the diffuse reflectivity
    Color = vec4( phongModel(b_normal.xyz, texColor.rgb), 1.0 );
}

vec3 phongmodelphongModel( vec3 norm, vec3 diffr ) 
{
    vec3 r=reflectr = reflect( -lightdir, norm );
    vec3 ambient = LightIntensity*Ka;LightIntensity * Ka;
    float sdotnsDotN = max( dot(lightdir, norm), 0.0 );
    vec3 diffuse = LightIntensity*diffr*sdotn;Kd*LightIntensity * diffr * sDotN;
    vec3 spec = vec3(0.0);
    if(sdotn>0 sDotN > 0.0 )
        spec = LightIntensity*Ks*powLightIntensity * Ks * pow( max( dot(r,viewdir), 0.0),Shininess);
    return ambient+diffuse+spec;
}

void main()
{
    vec4 bump_normal=texture(bump_map, fUVShininess );
    vec4 tex_color=texture(png_tex,fUV);
 return ambient + Colordiffuse =+ vec4(phongmodel(bump_normal.xyz,tex_color.rgb),1.0);spec;
}

I only getgot this result: //the shaders are updated as I went along, so the ones posted above work (for me anyway)

float f;
vec3 edge1, edge2, tangentbuf;
vec2 deltaUV1, deltaUV2;
//for each face, x=vert,y=uv, z=normal
for (size_t i = 0; i < totalrefs.size(); i += 9)
{
    edge1 = verticeref[totalrefs[i + 3] - 1] - verticeref[totalrefs[i] - 1];
    edge2 = verticeref[totalrefs[i + 6] - 1] - verticeref[totalrefs[i] - 1];
    deltaUV1 = texkoordref[totalrefs[i + 4] - 1] - texkoordref[totalrefs[i + 1] - 1];
    deltaUV2 = texkoordref[totalrefs[i + 7] - 1] - texkoordref[totalrefs[i + 1] - 1];
    f = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV2.x * deltaUV1.y);

    tangentbuf = f*vec3(
        (deltaUV2.y * edge1.x - deltaUV1.y * edge2.x),
        (deltaUV2.y * edge1.y - deltaUV1.y * edge2.y),
        (deltaUV2.y * edge1.z - deltaUV1.y * edge2.z));

    normalize(tangentref[tangentcount++]);
    tangentref[totalrefs[i] - 1] += vec4(normalize(tangentbuf), 1);
    tangentref[totalrefs[i + 3] - 1] += vec4(normalize(tangentbuf), 1);
    tangentref[totalrefs[i + 6] - 1] += vec4(normalize(tangentbuf), 1);
}

//for (auto &x : tangentref)x = normalize(x / x.w); //find average
for (auto &x : tangentref)x = normalize(x); //just normalize is enough?
#version 400
layout ( location = 0 ) in vec3 vertex_position;
layout ( location = 1 ) in vec2 tex_cord;
layout ( location = 2 ) in vec3 vertex_normal;
layout ( location = 3 ) in vec3 vertex_tangent;

uniform mat4 modelviewmatrix; //view*model matrix
uniform mat3 normalmatrix; // transpose/invert(view*model)
uniform mat4 priv_mat; //projection-view-model pre multiplied

uniform vec4 LightPosition; //after view*lightpos, aka viewspace

out vec2 fUV; //fragment uv
out vec3 lightdir; //tangent lightdir
out vec3 viewdir; //tangent viewdir

void main()
{
    vec3 norm = normalize(normalmatrix*vertex_normal);
    vec3 tang = normalize(normalmatrix*vec3(vertex_tangent));
         tang = normalize(tang - dot(tang, norm) * norm); //Gramm-Schmidt
    vec3 binormal = normalize(cross(tang,norm));
 
    //matrix for transformation to tangent space
    mat3 tbn=mat3(tang,binormal,norm);

    //turn light dir and view dir to tangent space
    vec3 pos = vec3(modelviewmatrix*vec4(vertex_position,1.0));
    lightdir = normalize(tbn*(LightPosition.xyz-pos));
    viewdir  =tbn*normalize(-pos); //tangent view

    fUV = tex_cord; //uv
    gl_Position = priv_mat*vec4(vertex_position,1.0); //model
}
#version 400

in vec2 fUV;
in vec3 lightdir; //tang light
in vec3 viewdir; //tang view

uniform sampler2D png_tex;
uniform sampler2D bump_map;

uniform vec3 LightIntensity;

uniform vec3 Kd;            // Diffuse reflectivity
uniform vec3 Ka;            // Ambient reflectivity
uniform vec3 Ks;            // Specular reflectivity
uniform float Shininess;    // Specular shininess factor

out vec4 Color;

vec3 phongmodel(vec3 norm, vec3 diffr){
    vec3 r=reflect(-lightdir,norm);
    vec3 ambient = LightIntensity*Ka;
    float sdotn = max(dot(lightdir,norm),0.0);
    vec3 diffuse = LightIntensity*diffr*sdotn;
    vec3 spec = vec3(0.0);
    if(sdotn>0.0)
        spec = LightIntensity*Ks*pow(max(dot(r,viewdir),0.0),Shininess);
    return ambient+diffuse+spec;
}

void main()
{
    vec4 bump_normal=texture(bump_map, fUV);
    vec4 tex_color=texture(png_tex,fUV);
    Color = vec4(phongmodel(bump_normal.xyz,tex_color.rgb),1.0);
}

I only get this result:

//this parsing has not been updated, unlike the shaders, correct parsing in the answer below
float f;
vec3 edge1, edge2, tangentbuf;
vec2 deltaUV1, deltaUV2;
//for each face, x=vert,y=uv, z=normal
for (size_t i = 0; i < totalrefs.size(); i += 9)
{
    edge1 = verticeref[totalrefs[i + 3] - 1] - verticeref[totalrefs[i] - 1];
    edge2 = verticeref[totalrefs[i + 6] - 1] - verticeref[totalrefs[i] - 1];
    deltaUV1 = texkoordref[totalrefs[i + 4] - 1] - texkoordref[totalrefs[i + 1] - 1];
    deltaUV2 = texkoordref[totalrefs[i + 7] - 1] - texkoordref[totalrefs[i + 1] - 1];
    f = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV2.x * deltaUV1.y);

    tangentbuf = f*vec3(
        (deltaUV2.y * edge1.x - deltaUV1.y * edge2.x),
        (deltaUV2.y * edge1.y - deltaUV1.y * edge2.y),
        (deltaUV2.y * edge1.z - deltaUV1.y * edge2.z));

    normalize(tangentref[tangentcount++]);
    tangentref[totalrefs[i] - 1] += vec4(normalize(tangentbuf), 1);
    tangentref[totalrefs[i + 3] - 1] += vec4(normalize(tangentbuf), 1);
    tangentref[totalrefs[i + 6] - 1] += vec4(normalize(tangentbuf), 1);
}

//for (auto &x : tangentref)x = normalize(x / x.w); //find average
for (auto &x : tangentref)x = normalize(x); //just normalize is enough?
#version 400
layout ( location = 0 ) in vec3 vertex_position;
layout ( location = 1 ) in vec2 tex_cord;
layout ( location = 2 ) in vec3 vertex_normal;
layout ( location = 3 ) in vec3 vertex_tangent;

uniform mat4 model; //model matrix, aka model 2 world

uniform mat4 modelviewmatrix; //view*model matrix
uniform mat3 normalmatrix; // transpose/invert(view*model)
uniform mat4 priv_mat; //projection-view-model pre multiplied

uniform vec4 LightPosition; //after view*lightpos, ie viewspace

out vec3 lightdir;
out vec3 viewdir;
out vec2 fUV;
out vec3 position;

void main()
{
    gl_Position=priv_mat*vec4(vertex_position,1.0);
    fUV=tex_cord;
    position=(model*vec4(vertex_position,1.0)).xyz;
    len=length(vertex_position);

    // Transform normal and tangent to eye space
    vec3 n = normalize(normalmatrix * vertex_normal);
    vec3 t = normalize(normalmatrix * vec3(vertex_tangent));
         t = normalize(t-dot(t,n)*n);  //Gramm-Schmidt process
    vec3 bitangent = normalize( cross( n, t ) );
    mat3 tbn = transpose(mat3(t,bitangent,n)); //why transpose??

    vec3 pos=vec3(modelviewmatrix*vec4(vertex_position,1.0));

    // Transform light dir. and view dir. to tangent space
    lightdir = normalize( tbn *  (LightPosition.xyz - pos) );
    viewdir = tbn * normalize(-pos);
}
#version 400

in float len;
in vec3 lightdir; //tang light
in vec3 viewdir; //tang view
in vec2 fUV;
in vec3 position;

uniform sampler2D png_tex;
uniform sampler2D bump_map; 

uniform vec4 LightPosition;
uniform vec3 LightIntensity;

uniform vec3 Kd;            // Diffuse reflectivity
uniform vec3 Ka;            // Ambient reflectivity
uniform vec3 Ks;            // Specular reflectivity
uniform float Shininess;    // Specular shininess factor

out vec4 Color;
vec3 phongModel( vec3 norm, vec3 diffr );

void main()
{
    vec4 b_normal = (255.0/128.0) * texture( bump_map, fUV )-1.0; // Lookup the normal from the normal map
    vec4 texColor = texture( png_tex, fUV ); // The color texture is used as the diffuse reflectivity
    Color = vec4( phongModel(b_normal.xyz, texColor.rgb), 1.0 );
}

vec3 phongModel( vec3 norm, vec3 diffr ) 
{
    vec3 r = reflect( -lightdir, norm );
    vec3 ambient = LightIntensity * Ka;
    float sDotN = max( dot(lightdir, norm), 0.0 );
    vec3 diffuse = Kd*LightIntensity * diffr * sDotN;
    vec3 spec = vec3(0.0);
    if( sDotN > 0.0 )
        spec = LightIntensity * Ks * pow( max( dot(r,viewdir), 0.0 ), Shininess );
    return ambient + diffuse + spec;
}

I only got this result: //the shaders are updated as I went along, so the ones posted above work (for me anyway)

edited body
Source Link
Charlie
  • 203
  • 1
  • 7
Loading
added 8 characters in body
Source Link
Charlie
  • 203
  • 1
  • 7
Loading
added 8 characters in body
Source Link
Charlie
  • 203
  • 1
  • 7
Loading
Source Link
Charlie
  • 203
  • 1
  • 7
Loading