Another fix for non-VBO flat-shading in sculpt mode, this time for non-multires meshes.
authorNicholas Bishop <nicholasbishop@gmail.com>
Tue, 21 Feb 2012 04:24:30 +0000 (04:24 +0000)
committerNicholas Bishop <nicholasbishop@gmail.com>
Tue, 21 Feb 2012 04:24:30 +0000 (04:24 +0000)
As with multires, this change calculates face normals rather than
using vertex normals when the node is flat-shaded.

Flat-shading with VBO on non-multires meshes is still wrong, but
fixing that would require larger changes to our vertex buffers.

source/blender/blenlib/intern/pbvh.c
source/blender/gpu/GPU_buffers.h
source/blender/gpu/intern/gpu_buffers.c

index 0e3cc51dea5c974b8419a3081eacc0e6e1acbd1f..56f0dff2847367ff9b092dbcf050be5c635cae26 100644 (file)
@@ -1087,7 +1087,8 @@ static void pbvh_update_draw_buffers(PBVH *bvh, PBVHNode **nodes, int totnode, i
                                                   bvh->verts,
                                                   node->vert_indices,
                                                   node->uniq_verts +
-                                                  node->face_verts);
+                                                  node->face_verts,
+                                  smooth);
                        }
 
                        node->flag &= ~PBVH_UpdateDrawBuffers;
index 945b9d819efd5b775fbb2d662cb3ea1b6a1ab5ea..5c567c311636be220e46453c857bf061b210dc9b 100644 (file)
@@ -163,7 +163,7 @@ GPU_Buffers *GPU_build_mesh_buffers(struct GHash *map,
                        struct MFace *mface, int *face_indices,
                        int totface, int uniq_verts);
 void GPU_update_mesh_buffers(GPU_Buffers *buffers, struct MVert *mvert,
-                       int *vert_indices, int totvert);
+                       int *vert_indices, int totvert, int smooth);
 GPU_Buffers *GPU_build_grid_buffers(struct DMGridData **grids,
        int *grid_indices, int totgrid, int gridsize);
 void GPU_update_grid_buffers(GPU_Buffers *buffers, struct DMGridData **grids,
index 006baac0df56b7c1909f873f6db25a8e95745adf..0a4970a6a916b11e4689559ccaa16cfbe104204a 100644 (file)
@@ -1298,7 +1298,7 @@ struct GPU_Buffers {
 };
 
 void GPU_update_mesh_buffers(GPU_Buffers *buffers, MVert *mvert,
-                       int *vert_indices, int totvert)
+                       int *vert_indices, int totvert, int smooth)
 {
        VertexBufferFormat *vert_data;
        int i;
@@ -1331,6 +1331,7 @@ void GPU_update_mesh_buffers(GPU_Buffers *buffers, MVert *mvert,
        }
 
        buffers->mvert = mvert;
+       buffers->smooth = smooth;
 }
 
 GPU_Buffers *GPU_build_mesh_buffers(GHash *map, MFace *mface,
@@ -1558,22 +1559,38 @@ GPU_Buffers *GPU_build_grid_buffers(DMGridData **UNUSED(grids), int *UNUSED(grid
 
 static void gpu_draw_buffers_legacy_mesh(GPU_Buffers *buffers)
 {
-       int i;
+       const MVert *mvert = buffers->mvert;
+       int i, j;
 
        for(i = 0; i < buffers->totface; ++i) {
                MFace *f = buffers->mface + buffers->face_indices[i];
+               int S = f->v4 ? 4 : 3;
+               unsigned int *fv = &f->v1;
 
                glBegin((f->v4)? GL_QUADS: GL_TRIANGLES);
-               glNormal3sv(buffers->mvert[f->v1].no);
-               glVertex3fv(buffers->mvert[f->v1].co);
-               glNormal3sv(buffers->mvert[f->v2].no);
-               glVertex3fv(buffers->mvert[f->v2].co);
-               glNormal3sv(buffers->mvert[f->v3].no);
-               glVertex3fv(buffers->mvert[f->v3].co);
-               if(f->v4) {
-                       glNormal3sv(buffers->mvert[f->v4].no);
-                       glVertex3fv(buffers->mvert[f->v4].co);
+
+               if(buffers->smooth) {
+                       for(j = 0; j < S; j++) {
+                               glNormal3sv(mvert[fv[j]].no);
+                               glVertex3fv(mvert[fv[j]].co);
+                       }
                }
+               else {
+                       float fno[3];
+
+                       /* calculate face normal */
+                       if(f->v4) {
+                               normal_quad_v3(fno, mvert[fv[0]].co, mvert[fv[1]].co,
+                                                          mvert[fv[2]].co, mvert[fv[3]].co);
+                       }
+                       else
+                               normal_tri_v3(fno, mvert[fv[0]].co, mvert[fv[1]].co, mvert[fv[2]].co);
+                       glNormal3fv(fno);
+                       
+                       for(j = 0; j < S; j++)
+                               glVertex3fv(mvert[fv[j]].co);
+               }
+               
                glEnd();
        }
 }