diff options
author | Brecht Van Lommel <brechtvanlommel@pandora.be> | 2011-03-20 16:35:35 +0300 |
---|---|---|
committer | Brecht Van Lommel <brechtvanlommel@pandora.be> | 2011-03-20 16:35:35 +0300 |
commit | 549b5e1222fcda5ca9b7d1892ca4d05e2c62c066 (patch) | |
tree | f69a4a833a3eb092530bb0d7959e43cf05ca0362 /source/blender/blenkernel | |
parent | a50cdf713a800c07629495f0d5e7124cddbbc2c9 (diff) |
Fix/change in normal computation, now the viewport uses the same angle
weighted normals as the render engine, and the render engine will copy
normals from the mesh rather than always recalculating them.
Subsurf/multires still use regular vertex normals, but they are expected
to be sufficiently high resolution to not need this.
This means that normal maps displayed in the viewport actually match the
render engine exactly and don't have artifacts due to this discrepancy.
It of course also avoids unexpected surprises where your render normals
look different than your viewport normals.
Subversion bumped to 4 for version patch to recalculate normals.
Patch by Morten Mikkelsen, with some small changes.
Diffstat (limited to 'source/blender/blenkernel')
-rw-r--r-- | source/blender/blenkernel/BKE_DerivedMesh.h | 2 | ||||
-rw-r--r-- | source/blender/blenkernel/BKE_blender.h | 2 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/cdderivedmesh.c | 38 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/mesh.c | 45 |
4 files changed, 59 insertions, 28 deletions
diff --git a/source/blender/blenkernel/BKE_DerivedMesh.h b/source/blender/blenkernel/BKE_DerivedMesh.h index 857c88a6004..55ade5fe5d9 100644 --- a/source/blender/blenkernel/BKE_DerivedMesh.h +++ b/source/blender/blenkernel/BKE_DerivedMesh.h @@ -206,7 +206,7 @@ struct DerivedMesh { /* Fill the array (of length .getNumVerts()) with all vertex locations */ void (*getVertCos)(DerivedMesh *dm, float (*cos_r)[3]); - /* Get vertex normal, undefined if index is not valid */ + /* Get smooth vertex normal, undefined if index is not valid */ void (*getVertNo)(DerivedMesh *dm, int index, float no_r[3]); /* Get a map of vertices to faces diff --git a/source/blender/blenkernel/BKE_blender.h b/source/blender/blenkernel/BKE_blender.h index 6cbc49e19c8..2bba079d605 100644 --- a/source/blender/blenkernel/BKE_blender.h +++ b/source/blender/blenkernel/BKE_blender.h @@ -44,7 +44,7 @@ extern "C" { * and keep comment above the defines. * Use STRINGIFY() rather then defining with quotes */ #define BLENDER_VERSION 256 -#define BLENDER_SUBVERSION 3 +#define BLENDER_SUBVERSION 4 #define BLENDER_MINVERSION 250 #define BLENDER_MINSUBVERSION 0 diff --git a/source/blender/blenkernel/intern/cdderivedmesh.c b/source/blender/blenkernel/intern/cdderivedmesh.c index 407d2ded085..b7034a7db8a 100644 --- a/source/blender/blenkernel/intern/cdderivedmesh.c +++ b/source/blender/blenkernel/intern/cdderivedmesh.c @@ -1798,7 +1798,7 @@ void CDDM_calc_normals(DerivedMesh *dm) int i; int numVerts = dm->numVertData; int numFaces = dm->numFaceData; - MFace *mf; + MFace *mfaces; MVert *mv; if(numVerts == 0) return; @@ -1817,27 +1817,43 @@ void CDDM_calc_normals(DerivedMesh *dm) NULL, dm->numFaceData); /* calculate face normals and add to vertex normals */ - mf = CDDM_get_faces(dm); - for(i = 0; i < numFaces; i++, mf++) { + mfaces = CDDM_get_faces(dm); + for(i = 0; i < numFaces; i++) { + MFace * mf = &mfaces[i]; float *f_no = face_nors[i]; if(mf->v4) - normal_quad_v3( f_no,mv[mf->v1].co, mv[mf->v2].co, mv[mf->v3].co, mv[mf->v4].co); + normal_quad_v3(f_no, mv[mf->v1].co, mv[mf->v2].co, mv[mf->v3].co, mv[mf->v4].co); else - normal_tri_v3( f_no,mv[mf->v1].co, mv[mf->v2].co, mv[mf->v3].co); + normal_tri_v3(f_no, mv[mf->v1].co, mv[mf->v2].co, mv[mf->v3].co); - add_v3_v3(temp_nors[mf->v1], f_no); - add_v3_v3(temp_nors[mf->v2], f_no); - add_v3_v3(temp_nors[mf->v3], f_no); - if(mf->v4) - add_v3_v3(temp_nors[mf->v4], f_no); + if((mf->flag&ME_SMOOTH)!=0) { + float *n4 = (mf->v4)? temp_nors[mf->v4]: NULL; + float *c4 = (mf->v4)? mv[mf->v4].co: NULL; + + accumulate_vertex_normals(temp_nors[mf->v1], temp_nors[mf->v2], temp_nors[mf->v3], n4, + f_no, mv[mf->v1].co, mv[mf->v2].co, mv[mf->v3].co, c4); + } + } + + for(i = 0; i < numFaces; i++) { + MFace * mf = &mfaces[i]; + + if((mf->flag&ME_SMOOTH)==0) { + float *f_no = face_nors[i]; + + if(is_zero_v3(temp_nors[mf->v1])) copy_v3_v3(temp_nors[mf->v1], f_no); + if(is_zero_v3(temp_nors[mf->v2])) copy_v3_v3(temp_nors[mf->v2], f_no); + if(is_zero_v3(temp_nors[mf->v3])) copy_v3_v3(temp_nors[mf->v3], f_no); + if(mf->v4 && is_zero_v3(temp_nors[mf->v4])) copy_v3_v3(temp_nors[mf->v4], f_no); + } } /* normalize vertex normals and assign */ for(i = 0; i < numVerts; i++, mv++) { float *no = temp_nors[i]; - if (normalize_v3(no) == 0.0) + if(normalize_v3(no) == 0.0f) normalize_v3_v3(no, mv->co); normal_float_to_short_v3(mv->no, no); diff --git a/source/blender/blenkernel/intern/mesh.c b/source/blender/blenkernel/intern/mesh.c index 247e48a5576..1e29f5c9d3f 100644 --- a/source/blender/blenkernel/intern/mesh.c +++ b/source/blender/blenkernel/intern/mesh.c @@ -1278,26 +1278,42 @@ void mesh_calc_normals(MVert *mverts, int numVerts, MFace *mfaces, int numFaces, float *fnors= MEM_callocN(sizeof(*fnors)*3*numFaces, "meshnormals"); int i; - for (i=0; i<numFaces; i++) { + for(i=0; i<numFaces; i++) { MFace *mf= &mfaces[i]; float *f_no= &fnors[i*3]; - if (mf->v4) - normal_quad_v3( f_no,mverts[mf->v1].co, mverts[mf->v2].co, mverts[mf->v3].co, mverts[mf->v4].co); + if(mf->v4) + normal_quad_v3(f_no, mverts[mf->v1].co, mverts[mf->v2].co, mverts[mf->v3].co, mverts[mf->v4].co); else - normal_tri_v3( f_no,mverts[mf->v1].co, mverts[mf->v2].co, mverts[mf->v3].co); - - add_v3_v3(tnorms[mf->v1], f_no); - add_v3_v3(tnorms[mf->v2], f_no); - add_v3_v3(tnorms[mf->v3], f_no); - if (mf->v4) - add_v3_v3(tnorms[mf->v4], f_no); + normal_tri_v3(f_no, mverts[mf->v1].co, mverts[mf->v2].co, mverts[mf->v3].co); + + if((mf->flag&ME_SMOOTH)!=0) { + float *n4 = (mf->v4)? tnorms[mf->v4]: NULL; + float *c4 = (mf->v4)? mverts[mf->v4].co: NULL; + + accumulate_vertex_normals(tnorms[mf->v1], tnorms[mf->v2], tnorms[mf->v3], n4, + f_no, mverts[mf->v1].co, mverts[mf->v2].co, mverts[mf->v3].co, c4); + } + } + + for(i=0; i<numFaces; i++) { + MFace *mf= &mfaces[i]; + + if((mf->flag&ME_SMOOTH)==0) { + float *f_no= &fnors[i*3]; + if(is_zero_v3(tnorms[mf->v1])) copy_v3_v3(tnorms[mf->v1], f_no); + if(is_zero_v3(tnorms[mf->v2])) copy_v3_v3(tnorms[mf->v2], f_no); + if(is_zero_v3(tnorms[mf->v3])) copy_v3_v3(tnorms[mf->v3], f_no); + if(mf->v4 && is_zero_v3(tnorms[mf->v4])) copy_v3_v3(tnorms[mf->v4], f_no); + } } - for (i=0; i<numVerts; i++) { + + /* following Mesh convention; we use vertex coordinate itself for normal in this case */ + for(i=0; i<numVerts; i++) { MVert *mv= &mverts[i]; float *no= tnorms[i]; - if (normalize_v3(no)==0.0) + if(normalize_v3(no) == 0.0f) normalize_v3_v3(no, mv->co); normal_float_to_short_v3(mv->no, no); @@ -1305,11 +1321,10 @@ void mesh_calc_normals(MVert *mverts, int numVerts, MFace *mfaces, int numFaces, MEM_freeN(tnorms); - if (faceNors_r) { + if(faceNors_r) *faceNors_r = fnors; - } else { + else MEM_freeN(fnors); - } } float (*mesh_getVertexCos(Mesh *me, int *numVerts_r))[3] |