Very weird problem with lighting

Started by
2 comments, last by _WeirdCat_ 4 years, 6 months ago

Reason i ask is that i can't see any bug here:

Thing is i have a problem when i translate and rotate model with a one model matrix. Normals stay the same, no matter how i rotate my model.

 

What i think my shader does:

calculate new normal given vertex normal and model (world) matrix (rotation and translation)

Calculate new vertex position with vertex pos * model matrix

Calculate output fragment coord with vertex pos * ((model * view) * projection matrix)

 

Model rotates and changes position but it still lits faces that shouldnt like light is following the model itself (always relative to it)


attribute vec3 Vpos;
attribute vec3 Vnormal;
//(model * view) * projection
uniform vec4 MVP1;
uniform vec4 MVP2;
uniform vec4 MVP3;
uniform vec4 MVP4;
//model mat rot and translation
uniform vec4 WM1;
uniform vec4 WM2;
uniform vec4 WM3;
uniform vec4 WM4;

uniform vec3 LPOS;
uniform vec3 LDIFF;
uniform vec3 LAMB;

float dp43(vec4 matrow, vec3 p)
{
return ( (matrow.x*p.x) + (matrow.y*p.y) + (matrow.z*p.z) + matrow.w );
}


float dp33(vec4 matrow, vec3 p)
{
return matrow.x*p.x + matrow.y*p.y + matrow.z*p.z;
}


vec3 vectorAB( vec3 A, vec3 B)
{
return B - A;
}

varying highp vec4 fragColor;
void main()
{
vec4 vertexClip;

vertexClip.x = dp43(MVP1, Vpos);
vertexClip.y = dp43(MVP2, Vpos);
vertexClip.z = dp43(MVP3, Vpos);
vertexClip.w = dp43(MVP4, Vpos);

vec3 normal;
normal.x = dp33(WM1, Vnormal);
normal.y = dp33(WM2, Vnormal);
normal.z = dp33(WM3, Vnormal);
//normal = normalize(normal);

vec3 vertex_pos;
vertex_pos.x = dp43(WM1, Vpos);
vertex_pos.y = dp43(WM2, Vpos);
vertex_pos.z = dp43(WM3, Vpos);

vec3 light_vert = normalize( vectorAB( LPOS, vertex_pos ) );
float intensity = clamp(-dot(light_vert, Vnormal), 0.0, 1.0);


vec3 res_col = clamp( LAMB + LDIFF*intensity, 0.0, 1.0);
fragColor = vec4(res_col, 1.0);
gl_Position = vertexClip;







}

Here is a model drawing routine (no rotations made in geometric center of the model since rotations have pivot here)


void Draw(TShaderObject * shader)
{
shader->SetLightPos(vec3(9000.0, 9000.0, 0.0));
shader->SetLightDiffuse(vec3(1.0, 1.0, 1.0));
shader->SetLightAmbient(vec3(0.7, 0.7, 0.7));
	for (int i=0; i < 15; i++)
	{
		mat4 mm;
		mm = bones[i]->m_rel;
		AI_INTERACTIVE_MODEL * p = bones[i]->parent;
		while ( p != 0 )
		{
			mm = mm * p->m_rel;
			p = p->parent;
		}
	mat4 MVP = (mm * ACTUAL_VIEW) * ACTUAL_PROJECTION;
	shader->SendMVPtoShader(MVP);
	shader->SendWorldMatrixtoShader(mm);
	bones[i]->model->DrawSimpleModel(shader);
	}
}

 

Advertisement
5 hours ago, _WeirdCat_ said:


vec3 normal;
normal.x = dp33(WM1, Vnormal);
normal.y = dp33(WM2, Vnormal);
normal.z = dp33(WM3, Vnormal);
//normal = normalize(normal);

 

You calculate normal, but it's not being outputted so the result is just thrown away (or optimized out).

Possibly this part should be using normal instead of Vnormal:


float intensity = clamp(-dot(light_vert, Vnormal), 0.0, 1.0);

 

Thanks!! Silly thing is that sometimes you can't see such simple things in your code :)

This topic is closed to new replies.

Advertisement