Slightly altered rules for calculating vertexnormals. By only averaging
face normals from faces actually set 'smooth', the result looks much more
nice (for example on a cylinder with caps solid). Vertex normals not being
used by smooth faces are set to the face normal direction.

Shows both in editor as rendering.
This commit is contained in:
2004-12-19 14:19:20 +00:00
parent ca5784a7ce
commit b0162a19fd
2 changed files with 145 additions and 110 deletions

View File

@@ -129,7 +129,6 @@ static Material *give_render_material(Object *ob, int nr);
static void split_u_renderfaces(int startvlak, int startvert, int usize, int plek, int cyclu); static void split_u_renderfaces(int startvlak, int startvert, int usize, int plek, int cyclu);
static void split_v_renderfaces(int startvlak, int startvert, int usize, int vsize, int plek, int cyclu, int cyclv); static void split_v_renderfaces(int startvlak, int startvert, int usize, int vsize, int plek, int cyclu, int cyclv);
static int contrpuntnormr(float *n, float *puno); static int contrpuntnormr(float *n, float *puno);
static void normalenrender(int startvert, int startvlak);
static void as_addvert(VertRen *v1, VlakRen *vlr); static void as_addvert(VertRen *v1, VlakRen *vlr);
static void as_freevert(VertRen *ver); static void as_freevert(VertRen *ver);
static void autosmooth(int startvert, int startvlak, int degr); static void autosmooth(int startvert, int startvlak, int degr);
@@ -453,7 +452,7 @@ static int contrpuntnormr(float *n, float *puno)
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
static void normalenrender(int startvert, int startvlak) static void calc_vertexnormals(int startvert, int startvlak)
{ {
VlakRen *vlr; VlakRen *vlr;
VertRen *ver, *adrve1, *adrve2, *adrve3, *adrve4; VertRen *ver, *adrve1, *adrve2, *adrve3, *adrve4;
@@ -469,7 +468,7 @@ static void normalenrender(int startvert, int startvlak)
/* calculate cos of angles and point-masses */ /* calculate cos of angles and point-masses */
for(a= startvlak; a<R.totvlak; a++) { for(a= startvlak; a<R.totvlak; a++) {
vlr= RE_findOrAddVlak(a); vlr= RE_findOrAddVlak(a);
if(vlr->flag & ME_SMOOTH) {
adrve1= vlr->v1; adrve1= vlr->v1;
adrve2= vlr->v2; adrve2= vlr->v2;
adrve3= vlr->v3; adrve3= vlr->v3;
@@ -499,6 +498,7 @@ static void normalenrender(int startvert, int startvlak)
*(tfl++)= saacos(-n3[0]*n4[0]-n3[1]*n4[1]-n3[2]*n4[2]); *(tfl++)= saacos(-n3[0]*n4[0]-n3[1]*n4[1]-n3[2]*n4[2]);
} }
} }
}
/* clear all vertex normals */ /* clear all vertex normals */
for(a=startvert; a<R.totvert; a++) { for(a=startvert; a<R.totvert; a++) {
@@ -512,6 +512,8 @@ static void normalenrender(int startvert, int startvlak)
for(a=startvlak; a<R.totvlak; a++) { for(a=startvlak; a<R.totvlak; a++) {
vlr= RE_findOrAddVlak(a); vlr= RE_findOrAddVlak(a);
if(vlr->flag & ME_SMOOTH) {
adrve1= vlr->v1; adrve1= vlr->v1;
adrve2= vlr->v2; adrve2= vlr->v2;
adrve3= vlr->v3; adrve3= vlr->v3;
@@ -551,6 +553,23 @@ static void normalenrender(int startvert, int startvlak)
*(temp) +=fac*vlr->n[2]; *(temp) +=fac*vlr->n[2];
} }
} }
}
/* do solid faces */
for(a=startvlak; a<R.totvlak; a++) {
vlr= RE_findOrAddVlak(a);
if((vlr->flag & ME_SMOOTH)==0) {
float *f1= vlr->v1->n;
if(f1[0]==0.0 && f1[1]==0.0 && f1[2]==0.0) VECCOPY(f1, vlr->n);
f1= vlr->v2->n;
if(f1[0]==0.0 && f1[1]==0.0 && f1[2]==0.0) VECCOPY(f1, vlr->n);
f1= vlr->v3->n;
if(f1[0]==0.0 && f1[1]==0.0 && f1[2]==0.0) VECCOPY(f1, vlr->n);
if(vlr->v4) {
f1= vlr->v4->n;
if(f1[0]==0.0 && f1[1]==0.0 && f1[2]==0.0) VECCOPY(f1, vlr->n);
}
}
}
/* normalise vertex normals */ /* normalise vertex normals */
for(a=startvert; a<R.totvert; a++) { for(a=startvert; a<R.totvert; a++) {
@@ -1548,7 +1567,7 @@ static void init_render_mesh(Object *ob)
} }
if(do_puno) { if(do_puno) {
normalenrender(totverto, totvlako); calc_vertexnormals(totverto, totvlako);
do_puno= 0; do_puno= 0;
} }
@@ -1560,7 +1579,7 @@ static void init_render_mesh(Object *ob)
do_puno= 1; do_puno= 1;
} }
if(do_puno) normalenrender(totverto, totvlako); if(do_puno) calc_vertexnormals(totverto, totvlako);
mesh_modifier(ob, 'e'); // end mesh_modifier(ob, 'e'); // end
} }
@@ -3151,7 +3170,7 @@ static void do_displacement(Object *ob, int startface, int numface, int startver
} }
/* Recalc vertex normals */ /* Recalc vertex normals */
normalenrender(startvert, startface); calc_vertexnormals(startvert, startface);
} }
static void displace_render_face(VlakRen *vlr, float *scale) static void displace_render_face(VlakRen *vlr, float *scale)

View File

@@ -1284,6 +1284,7 @@ void vertexnormals(int testflip)
/* calculate cosine angles and add to vertex normal */ /* calculate cosine angles and add to vertex normal */
efa= em->faces.first; efa= em->faces.first;
while(efa) { while(efa) {
if(efa->flag & ME_SMOOTH) {
VecSubf(n1, efa->v2->co, efa->v1->co); VecSubf(n1, efa->v2->co, efa->v1->co);
VecSubf(n2, efa->v3->co, efa->v2->co); VecSubf(n2, efa->v3->co, efa->v2->co);
Normalise(n1); Normalise(n1);
@@ -1335,9 +1336,24 @@ void vertexnormals(int testflip)
temp[1]+= co[3]*efa->n[1]; temp[1]+= co[3]*efa->n[1];
temp[2]+= co[3]*efa->n[2]; temp[2]+= co[3]*efa->n[2];
} }
}
efa= efa->next; efa= efa->next;
} }
/* check solid faces */
for(efa= em->faces.first; efa; efa= efa->next) {
if((efa->flag & ME_SMOOTH)==0) {
f1= efa->v1->no;
if(f1[0]==0.0 && f1[1]==0.0 && f1[2]==0.0) VECCOPY(f1, efa->n);
f1= efa->v2->no;
if(f1[0]==0.0 && f1[1]==0.0 && f1[2]==0.0) VECCOPY(f1, efa->n);
f1= efa->v3->no;
if(f1[0]==0.0 && f1[1]==0.0 && f1[2]==0.0) VECCOPY(f1, efa->n);
if(efa->v4) {
f1= efa->v4->no;
if(f1[0]==0.0 && f1[1]==0.0 && f1[2]==0.0) VECCOPY(f1, efa->n);
}
}
}
/* normalise vertex normals */ /* normalise vertex normals */
eve= em->verts.first; eve= em->verts.first;