- change mesh_calc_normals to set vertices with len(no)==0

to normalised coordinate (convention in blender, helps with
   halo)
 - removed vertexnormals(), vertexnormals_mesh()
 - removed CTX_NO_NOR_RECALC (always assume already calculated)
 - change NMesh.c to call mesh_calc_normals
 - chance load_editMesh to call mesh_calc_normals after done
   converting instead of using editmesh normals
 - update recalc_editnormals to also calc vertex normals (whats
   4 more adds and a sqrt among friends)

Its hard to believe, but it just might be the case that there
are only two places mesh normals are calculated now (renderer
and kernel)
This commit is contained in:
2005-07-23 19:03:43 +00:00
parent 948f27c0d8
commit fb651ddb4a
10 changed files with 25 additions and 326 deletions

View File

@@ -1193,7 +1193,11 @@ void mesh_calc_normals(MVert *mverts, int numVerts, MFace *mfaces, int numFaces,
MVert *mv= &mverts[i];
float *no= tnorms[i];
Normalise(no);
if (Normalise(no)==0.0) {
VECCOPY(no, mv->co);
Normalise(no);
}
mv->no[0]= (short)(no[0]*32767.0);
mv->no[1]= (short)(no[1]*32767.0);
mv->no[2]= (short)(no[2]*32767.0);

View File

@@ -88,7 +88,6 @@ extern int EM_nvertices_selected(void);
extern int faceselectedAND(struct EditFace *efa, int flag);
extern void recalc_editnormals(void);
extern void flip_editnormals(void);
extern void vertexnormals(int testflip);
/* ******************* editmesh_mods.c */

View File

@@ -38,7 +38,6 @@ extern void join_mesh(void);
extern void fasterdraw(void);
extern void slowerdraw(void);
extern void vertexnormals_mesh(Mesh *me);
extern void sort_faces(void);

View File

@@ -57,7 +57,6 @@
#define CTX_TEXTURE 1
#define CTX_EDGE 2
#define CTX_NO_PET 4
#define CTX_NO_NOR_RECALC 8
void initTransform(int mode, int context);
void Transform(void);

View File

@@ -47,7 +47,6 @@
#include "BIF_editdeform.h"
#include "BIF_editkey.h" /* insert_meshkey */
#include "BIF_meshtools.h" /* current loc of vertexnormals_mesh() */
#include "BIF_editview.h"
#include "BKE_deform.h"
@@ -1325,7 +1324,7 @@ static PyObject *NMesh_update( PyObject *self, PyObject *a, PyObject *kwd )
}
if( recalc_normals )
vertexnormals_mesh( mesh );
mesh_calc_normals(mesh->mvert, mesh->totvert, mesh->mface, mesh->totface, NULL);
mesh_update( mesh, nmesh->object );
@@ -2875,7 +2874,7 @@ static PyObject *M_NMesh_PutRaw( PyObject * self, PyObject * args )
if (mesh->dvert) check_dverts(mesh, old_totvert);
if( recalc_normals )
vertexnormals_mesh( mesh );
mesh_calc_normals(mesh->mvert, mesh->totvert, mesh->mface, mesh->totface, NULL);
mesh_update( mesh, nmesh->object );

View File

@@ -915,9 +915,6 @@ void load_editMesh(void)
/* eve->f2 : being used in vertexnormals */
edge_drawflags();
/* this sets efa->puno, punoflag (for vertex normal & projection) */
vertexnormals( (me->flag & ME_NOPUNOFLIP)==0 );
eed= em->edges.first;
while(eed) {
totedge++;
@@ -1235,6 +1232,9 @@ void load_editMesh(void)
error("Sticky was too small");
}
}
mesh_calc_normals(me->mvert, me->totvert, me->mface, me->totface, NULL);
waitcursor(0);
}

View File

@@ -1306,149 +1306,6 @@ static int check_vnormal_flip(float *n, float *vnorm)
}
void vertexnormals(int testflip)
{
EditMesh *em = G.editMesh;
Mesh *me;
EditVert *eve;
EditFace *efa;
float n1[3], n2[3], n3[3], n4[3], co[4], *temp;
float xn, yn, zn;
float len, area;
if(G.obedit && G.obedit->type==OB_MESH) {
me= G.obedit->data;
if((me->flag & ME_TWOSIDED)==0) testflip= 0;
}
if(G.totvert==0) return;
if(G.totface==0) {
/* fake vertex normals for 'halo puno'! */
eve= em->verts.first;
while(eve) {
VECCOPY(eve->no, eve->co);
Normalise( (float *)eve->no);
eve= eve->next;
}
return;
}
/* clear normals, clear flag */
eve= em->verts.first;
while(eve) {
eve->no[0]= eve->no[1]= eve->no[2]= 0.0;
eve->f2= 0;
eve= eve->next;
}
/* check for vertices being shared by both solid and smooth face,
these get vertexnormal of smooth face normal only */
for(efa= em->faces.first; efa; efa= efa->next) {
if(efa->flag & ME_SMOOTH) {
efa->v1->f2 |= 1; efa->v2->f2 |= 1; efa->v3->f2 |= 1;
if(efa->v4) efa->v4->f2 |= 1;
}
else {
efa->v1->f2 |= 2; efa->v2->f2 |= 2; efa->v3->f2 |= 2;
if(efa->v4) efa->v4->f2 |= 2;
}
}
/* calculate cosine angles and add to vertex normal */
for(efa= em->faces.first; efa; efa= efa->next) {
VecSubf(n1, efa->v2->co, efa->v1->co);
VecSubf(n2, efa->v3->co, efa->v2->co);
Normalise(n1);
Normalise(n2);
if(efa->v4==0) {
VecSubf(n3, efa->v1->co, efa->v3->co);
Normalise(n3);
//area= AreaT3Dfl(efa->v1->co, efa->v2->co, efa->v3->co);
//if(area!=0.0) area=1.0/area;
//area= sqrt(area);
area= 1.0;
co[0]= area*saacos(-n3[0]*n1[0]-n3[1]*n1[1]-n3[2]*n1[2]);
co[1]= area*saacos(-n1[0]*n2[0]-n1[1]*n2[1]-n1[2]*n2[2]);
co[2]= area*saacos(-n2[0]*n3[0]-n2[1]*n3[1]-n2[2]*n3[2]);
}
else {
VecSubf(n3, efa->v4->co, efa->v3->co);
VecSubf(n4, efa->v1->co, efa->v4->co);
Normalise(n3);
Normalise(n4);
//area= AreaQ3Dfl(efa->v1->co, efa->v2->co, efa->v3->co, efa->v4->co);
//if(area!=0.0) area=1.0/area;
//area= sqrt(area);
area= 1.0;
co[0]= area*saacos(-n4[0]*n1[0]-n4[1]*n1[1]-n4[2]*n1[2]);
co[1]= area*saacos(-n1[0]*n2[0]-n1[1]*n2[1]-n1[2]*n2[2]);
co[2]= area*saacos(-n2[0]*n3[0]-n2[1]*n3[1]-n2[2]*n3[2]);
co[3]= area*saacos(-n3[0]*n4[0]-n3[1]*n4[1]-n3[2]*n4[2]);
}
if(efa->v1->f2!=3 || (efa->flag & ME_SMOOTH)) {
temp= efa->v1->no;
if(testflip && check_vnormal_flip(efa->n, temp) ) co[0]= -co[0];
temp[0]+= co[0]*efa->n[0];
temp[1]+= co[0]*efa->n[1];
temp[2]+= co[0]*efa->n[2];
}
if(efa->v2->f2!=3 || (efa->flag & ME_SMOOTH)) {
temp= efa->v2->no;
if(testflip && check_vnormal_flip(efa->n, temp) ) co[1]= -co[1];
temp[0]+= co[1]*efa->n[0];
temp[1]+= co[1]*efa->n[1];
temp[2]+= co[1]*efa->n[2];
}
if(efa->v3->f2!=3 || (efa->flag & ME_SMOOTH)) {
temp= efa->v3->no;
if(testflip && check_vnormal_flip(efa->n, temp) ) co[2]= -co[2];
temp[0]+= co[2]*efa->n[0];
temp[1]+= co[2]*efa->n[1];
temp[2]+= co[2]*efa->n[2];
}
if(efa->v4 && (efa->v4->f2!=3 || (efa->flag & ME_SMOOTH))) {
temp= efa->v4->no;
if(testflip && check_vnormal_flip(efa->n, temp) ) co[3]= -co[3];
temp[0]+= co[3]*efa->n[0];
temp[1]+= co[3]*efa->n[1];
temp[2]+= co[3]*efa->n[2];
}
}
/* normalise vertex normals */
eve= em->verts.first;
while(eve) {
len= Normalise(eve->no);
if(len==0.0) {
VECCOPY(eve->no, eve->co);
Normalise( eve->no);
}
eve= eve->next;
}
/* vertex normal flip-flags for shade (render) */
efa= em->faces.first;
while(efa) {
/* projection for cubemap! */
xn= fabs(efa->n[0]);
yn= fabs(efa->n[1]);
zn= fabs(efa->n[2]);
efa= efa->next;
}
}
void flipface(EditFace *efa)
{
if(efa->v4) {
@@ -1491,18 +1348,29 @@ void recalc_editnormals(void)
{
EditMesh *em = G.editMesh;
EditFace *efa;
EditVert *eve;
efa= em->faces.first;
while(efa) {
for(eve= em->verts.first; eve; eve=eve->next) {
eve->no[0] = eve->no[1] = eve->no[2] = 0.0;
}
for(efa= em->faces.first; efa; efa=efa->next) {
if(efa->v4) {
CalcNormFloat4(efa->v1->co, efa->v2->co, efa->v3->co, efa->v4->co, efa->n);
CalcCent4f(efa->cent, efa->v1->co, efa->v2->co, efa->v3->co, efa->v4->co);
VecAddf(efa->v4->no, efa->v4->no, efa->n);
}
else {
CalcNormFloat(efa->v1->co, efa->v2->co, efa->v3->co, efa->n);
CalcCent3f(efa->cent, efa->v1->co, efa->v2->co, efa->v3->co);
}
efa= efa->next;
VecAddf(efa->v1->no, efa->v1->no, efa->n);
VecAddf(efa->v2->no, efa->v2->no, efa->n);
VecAddf(efa->v3->no, efa->v3->no, efa->n);
}
for(eve= em->verts.first; eve; eve=eve->next) {
Normalise(eve->no);
}
}

View File

@@ -587,7 +587,7 @@ void extrude_mesh(void)
/* individual faces? */
BIF_TransformSetUndo("Extrude");
if(nr==2) {
initTransform(TFM_SHRINKFATTEN, CTX_NO_PET|CTX_NO_NOR_RECALC);
initTransform(TFM_SHRINKFATTEN, CTX_NO_PET);
Transform();
}
else {
@@ -2188,9 +2188,6 @@ void esubdivideflag(int flag, float rad, int beauty, int numcuts, int seltype)
BLI_ghash_insert(gh, eed, templist);
}
}
vertexnormals(0);
// Now for each face in the mesh we need to figure out How many edges were cut
@@ -5117,12 +5114,6 @@ void subdivideflag(int flag, float rad, int beauty)
}
}
if(beauty & B_SMOOTH) {
vertexnormals(0); /* no1*/
}
/* make new normal and put in edge, clear flag! needed for face creation part below */
eed= em->edges.first;
while(eed) {

View File

@@ -39,7 +39,6 @@ void join_mesh(void);
void fasterdraw(void);
void slowerdraw(void);
void vertexnormals_mesh(Mesh *me);
void sort_faces(void);
*/
@@ -533,163 +532,6 @@ void slowerdraw(void) /* reset fasterdraw */
allqueue(REDRAWVIEW3D, 0);
}
/* ***************** */
/* this one for NOT in editmode
(only used by external modules, that is, until now by the
python NMesh module)
TODO: Probably it's better to convert the mesh into a EditMesh, call
vertexnormals() and convert it back to a Mesh again.
*/
static int contrpuntnorm(float *n, float *puno) /* dutch: check vertex normal */
{
float inp;
inp= n[0]*puno[0]+n[1]*puno[1]+n[2]*puno[2];
/* angles 90 degrees: dont flip */
if(inp> -0.000001) return 0;
return 1;
}
void vertexnormals_mesh(Mesh *me)
{
MVert *mvert;
MFace *mface;
float n1[3], n2[3], n3[3], n4[3], co[4], *temp;
float xn, yn, zn, *normals;
float *v1, *v2, *v3, *v4, len, vnor[3];
int a, testflip;
if(me->totvert==0) return;
testflip= (me->flag & ME_NOPUNOFLIP)==0;
if((me->flag & ME_TWOSIDED)==0) testflip= 0; /* large angles */
if(me->totface==0) {
/* fake vertex normals for 'halopuno' (render option) */
mvert= me->mvert;
for(a=0; a<me->totvert; a++, mvert++) {
VECCOPY(n1, mvert->co);
Normalise(n1);
mvert->no[0]= 32767.0*n1[0];
mvert->no[1]= 32767.0*n1[1];
mvert->no[2]= 32767.0*n1[2];
}
return;
}
normals= MEM_callocN(me->totvert*3*sizeof(float), "normals");
/* calculate cosine angles, and add to vertex normal */
mface= me->mface;
mvert= me->mvert;
for(a=0; a<me->totface; a++, mface++) {
if(mface->v3==0) continue;
v1= (mvert+mface->v1)->co;
v2= (mvert+mface->v2)->co;
v3= (mvert+mface->v3)->co;
v4= (mvert+mface->v4)->co;
VecSubf(n1, v2, v1);
VecSubf(n2, v3, v2);
Normalise(n1);
Normalise(n2);
if(mface->v4==0) {
VecSubf(n3, v1, v3);
Normalise(n3);
co[0]= saacos(-n3[0]*n1[0]-n3[1]*n1[1]-n3[2]*n1[2]);
co[1]= saacos(-n1[0]*n2[0]-n1[1]*n2[1]-n1[2]*n2[2]);
co[2]= saacos(-n2[0]*n3[0]-n2[1]*n3[1]-n2[2]*n3[2]);
}
else {
VecSubf(n3, v4, v3);
VecSubf(n4, v1, v4);
Normalise(n3);
Normalise(n4);
co[0]= saacos(-n4[0]*n1[0]-n4[1]*n1[1]-n4[2]*n1[2]);
co[1]= saacos(-n1[0]*n2[0]-n1[1]*n2[1]-n1[2]*n2[2]);
co[2]= saacos(-n2[0]*n3[0]-n2[1]*n3[1]-n2[2]*n3[2]);
co[3]= saacos(-n3[0]*n4[0]-n3[1]*n4[1]-n3[2]*n4[2]);
}
CalcNormFloat(v1, v2, v3, vnor);
temp= normals+3*mface->v1;
if(testflip && contrpuntnorm(vnor, temp) ) co[0]= -co[0];
temp[0]+= co[0]*vnor[0];
temp[1]+= co[0]*vnor[1];
temp[2]+= co[0]*vnor[2];
temp= normals+3*mface->v2;
if(testflip && contrpuntnorm(vnor, temp) ) co[1]= -co[1];
temp[0]+= co[1]*vnor[0];
temp[1]+= co[1]*vnor[1];
temp[2]+= co[1]*vnor[2];
temp= normals+3*mface->v3;
if(testflip && contrpuntnorm(vnor, temp) ) co[2]= -co[2];
temp[0]+= co[2]*vnor[0];
temp[1]+= co[2]*vnor[1];
temp[2]+= co[2]*vnor[2];
if(mface->v4) {
temp= normals+3*mface->v4;
if(testflip && contrpuntnorm(vnor, temp) ) co[3]= -co[3];
temp[0]+= co[3]*vnor[0];
temp[1]+= co[3]*vnor[1];
temp[2]+= co[3]*vnor[2];
}
}
/* normalize vertex normals */
mvert= me->mvert;
for(a=0; a<me->totvert; a++, mvert++) {
len= Normalise(normals+3*a);
if(len!=0.0) {
VECCOPY(n1, normals+3*a);
Normalise(n1);
mvert->no[0]= 32767.0*n1[0];
mvert->no[1]= 32767.0*n1[1];
mvert->no[2]= 32767.0*n1[2];
}
}
/* vertex normal flipping flags, for during render */
mface= me->mface;
mvert= me->mvert;
for(a=0; a<me->totface; a++, mface++) {
if(mface->v3==0) continue;
v1= (mvert+mface->v1)->co;
v2= (mvert+mface->v2)->co;
v3= (mvert+mface->v3)->co;
CalcNormFloat(v1, v2, v3, vnor);
/* proj for cubemap! */
xn= fabs(vnor[0]);
yn= fabs(vnor[1]);
zn= fabs(vnor[2]);
}
MEM_freeN(normals);
}
/* ********************** SORT FACES ******************* */
static void permutate(void *list, int num, int size, int *index)

View File

@@ -1590,8 +1590,6 @@ void createTransData(TransInfo *t)
else if (G.obedit) {
t->ext = NULL;
if (G.obedit->type == OB_MESH) {
if(t->mode==TFM_SHRINKFATTEN && (t->context & CTX_NO_NOR_RECALC)==0)
vertexnormals(0);
createTransEditVerts(t);
}
else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) {