updatenormals() procedure logic ?

Blitz3D Forums/Blitz3D Beginners Area/updatenormals() procedure logic ?

RemiD(Posted 2016) [#1]
Hello,

if somebody here understands c++ (this is like chinese to me), can you please take a look at the updatenormals() procedure logic in the Blitz3d source code, and tell me if each vertex normal is calculated by taking into account all triangles normals which use this vertex ? and what is the formula to calculate the final vertex normal ?
something like :
;vertex normal (x,y,z) = 0
;for each triangle which uses this vertex
 ;vertex normal (x,y,z) = ( vertex normal (x,y,z) + triangle normal (x,y,z) ) / 2


Thanks,


RustyKristi(Posted 2016) [#2]
Why not just check the minib3d version since this blitz is right up your alley :-)

https://github.com/si-design/minib3d/blob/7443305cae4dbb462eee3dadb87657a6760e787c/inc/TSurface.bmx#L417-L508

it is commented out but maybe you could get something out of this.


RustyKristi(Posted 2016) [#3]
and this is the part you were looking for..


void Surface::updateNormals(){
	int k;
	map<Vector,Vector> norm_map;
	for( k=0;k<triangles.size();++k ){
		const Triangle &t=triangles[k];
		const Vector &v0=vertices[t.verts[0]].coords;
		const Vector &v1=vertices[t.verts[1]].coords;
		const Vector &v2=vertices[t.verts[2]].coords;
		Vector n=(v1-v0).cross(v2-v0);
		if( n.length()<=EPSILON ) continue;
		n.normalize();
		norm_map[v0]+=n;
		norm_map[v1]+=n;
		norm_map[v2]+=n;
	}
	for( k=0;k<vertices.size();++k ){
		Vertex *v=&vertices[k];
		v->normal=norm_map[v->coords].normalized();
	}
}



https://github.com/blitz-research/blitz3d/blob/647f304ef81387d8235b084bb1d9cb7ba5ff139e/blitz3d/surface.cpp#L63-L82


RemiD(Posted 2016) [#4]
@RustyKristi>>thanks, i will try to understand it


RustyKristi(Posted 2016) [#5]
no problem, I think the minib3d bmx version is not that far off if it executes and produces the same result as the original b3d c++ function.


Matty(Posted 2016) [#6]
It looks like from that code above each vertex has the normals of the triangles they are a part of added together and then normalised.

In effect this is averaging the triangle normals a vertex is part of to give a resulting normal for each vertex.


RemiD(Posted 2016) [#7]

each vertex has the normals of the triangles they are a part of added together and then normalised


so this would be something like :
;temp vector (x,y,z) = 0
;for each triangle which uses this vertex
 ;temp vector (x,y,z) = temp vector (x,y,z) + triangle normal (x,y,z)
;temp vector length = formula to calculate temp vector length
;vertex normal = temp vector (x,y,z) / temp vector length

?


Matty(Posted 2016) [#8]
Yep


RemiD(Posted 2016) [#9]
@Matty>>thanks

unfortunately this does not produce the normals that i want...