[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [47003] trunk/blender: 3D View: add Backface Culling option, to hide faces when seen from the back side,

Brecht Van Lommel brechtvanlommel at pandora.be
Fri May 25 11:26:47 CEST 2012


Revision: 47003
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=47003
Author:   blendix
Date:     2012-05-25 09:26:47 +0000 (Fri, 25 May 2012)
Log Message:
-----------
3D View: add Backface Culling option, to hide faces when seen from the back side,
found in the Display panel.

Patch by Simon Kirk and Irie Shinsuke, refactored to also work for non-mesh objects
and avoid globals.

Modified Paths:
--------------
    trunk/blender/release/scripts/startup/bl_ui/space_view3d.py
    trunk/blender/source/blender/editors/space_view3d/drawobject.c
    trunk/blender/source/blender/gpu/intern/gpu_draw.c
    trunk/blender/source/blender/makesdna/DNA_view3d_types.h
    trunk/blender/source/blender/makesrna/intern/rna_space.c

Modified: trunk/blender/release/scripts/startup/bl_ui/space_view3d.py
===================================================================
--- trunk/blender/release/scripts/startup/bl_ui/space_view3d.py	2012-05-25 09:06:15 UTC (rev 47002)
+++ trunk/blender/release/scripts/startup/bl_ui/space_view3d.py	2012-05-25 09:26:47 UTC (rev 47003)
@@ -2391,6 +2391,8 @@
             col.prop(gs, "material_mode", text="")
             col.prop(view, "show_textured_solid")
 
+        col.prop(view, "show_backface_culling")            
+
         layout.separator()
 
         region = view.region_quadview

Modified: trunk/blender/source/blender/editors/space_view3d/drawobject.c
===================================================================
--- trunk/blender/source/blender/editors/space_view3d/drawobject.c	2012-05-25 09:06:15 UTC (rev 47002)
+++ trunk/blender/source/blender/editors/space_view3d/drawobject.c	2012-05-25 09:26:47 UTC (rev 47003)
@@ -3610,7 +3610,13 @@
 		else if (ob->modifiers.first || obedit->modifiers.first) {}
 		else drawlinked = 1;
 	}
-	
+
+	/* backface culling */
+	if (v3d->flag2 & V3D_BACKFACE_CULLING) {
+		glEnable(GL_CULL_FACE);
+		glCullFace(GL_BACK);
+	}
+
 	if (ob == obedit || drawlinked) {
 		DerivedMesh *finalDM, *cageDM;
 		
@@ -3669,6 +3675,9 @@
 			}
 		}
 	}
+
+	if (v3d->flag2 & V3D_BACKFACE_CULLING)
+		glDisable(GL_CULL_FACE);
 	
 	return retval;
 }
@@ -3939,7 +3948,17 @@
 	const short solid = (dt > OB_WIRE);
 	int retval = 0;
 
+	/* backface culling */
+	if(v3d->flag2 & V3D_BACKFACE_CULLING) {
+		/* not all displists use same in/out normal direction convention */
+		glEnable(GL_CULL_FACE);
+		glCullFace((ob->type == OB_MBALL) ? GL_BACK : GL_FRONT);
+	}
+
 	if (drawCurveDerivedMesh(scene, v3d, rv3d, base, dt) == 0) {
+		if (v3d->flag2 & V3D_BACKFACE_CULLING)
+			glDisable(GL_CULL_FACE);
+
 		return 0;
 	}
 
@@ -4045,6 +4064,9 @@
 			break;
 	}
 	
+	if (v3d->flag2 & V3D_BACKFACE_CULLING)
+		glDisable(GL_CULL_FACE);
+
 	return retval;
 }
 

Modified: trunk/blender/source/blender/gpu/intern/gpu_draw.c
===================================================================
--- trunk/blender/source/blender/gpu/intern/gpu_draw.c	2012-05-25 09:06:15 UTC (rev 47002)
+++ trunk/blender/source/blender/gpu/intern/gpu_draw.c	2012-05-25 09:26:47 UTC (rev 47003)
@@ -1017,6 +1017,8 @@
 	float (*gviewmat)[4];
 	float (*gviewinv)[4];
 
+	int backface_culling;
+
 	GPUBlendMode *alphablend;
 	GPUBlendMode alphablend_fixed[FIXEDMAT];
 	int use_alpha_pass, is_alpha_pass;
@@ -1085,6 +1087,8 @@
 	GMS.lastretval = -1;
 	GMS.lastalphablend = GPU_BLEND_SOLID;
 
+	GMS.backface_culling = (v3d->flag2 & V3D_BACKFACE_CULLING);
+
 	GMS.gob = ob;
 	GMS.gscene = scene;
 	GMS.totmat= ob->totcol+1; /* materials start from 1, default material is 0 */
@@ -1248,6 +1252,13 @@
 				alphablend= mat->game.alpha_blend;
 
 			if (GMS.is_alpha_pass) glDepthMask(1);
+
+			if (GMS.backface_culling) {
+				if(mat->game.flag)
+					glEnable(GL_CULL_FACE);
+				else
+					glDisable(GL_CULL_FACE);
+			}
 		}
 		else {
 			/* or do fixed function opengl material */
@@ -1283,6 +1294,9 @@
 	GMS.lastretval= 1;
 
 	if (GMS.gboundmat) {
+		if (GMS.backface_culling)
+			glDisable(GL_CULL_FACE);
+
 		if (GMS.is_alpha_pass) glDepthMask(0);
 		GPU_material_unbind(GPU_material_from_blender(GMS.gscene, GMS.gboundmat));
 		GMS.gboundmat= NULL;

Modified: trunk/blender/source/blender/makesdna/DNA_view3d_types.h
===================================================================
--- trunk/blender/source/blender/makesdna/DNA_view3d_types.h	2012-05-25 09:06:15 UTC (rev 47002)
+++ trunk/blender/source/blender/makesdna/DNA_view3d_types.h	2012-05-25 09:26:47 UTC (rev 47003)
@@ -266,6 +266,7 @@
 #define V3D_SHOW_RECONSTRUCTION		128
 #define V3D_SHOW_CAMERAPATH		256
 #define V3D_SHOW_BUNDLENAME		512
+#define V3D_BACKFACE_CULLING	1024
 
 /* View3D->around */
 #define V3D_CENTER		 0

Modified: trunk/blender/source/blender/makesrna/intern/rna_space.c
===================================================================
--- trunk/blender/source/blender/makesrna/intern/rna_space.c	2012-05-25 09:06:15 UTC (rev 47002)
+++ trunk/blender/source/blender/makesrna/intern/rna_space.c	2012-05-25 09:26:47 UTC (rev 47003)
@@ -1567,6 +1567,11 @@
 	RNA_def_property_ui_text(prop, "Textured Solid", "Display face-assigned textures in solid view");
 	RNA_def_property_update(prop, NC_SPACE | ND_SPACE_VIEW3D, NULL);
 
+	prop = RNA_def_property(srna, "show_backface_culling", PROP_BOOLEAN, PROP_NONE);
+	RNA_def_property_boolean_sdna(prop, NULL, "flag2", V3D_BACKFACE_CULLING);
+	RNA_def_property_ui_text(prop, "Backface Culling", "Use back face culling to hide the back side of faces");
+	RNA_def_property_update(prop, NC_SPACE | ND_SPACE_VIEW3D, NULL);
+
 	prop = RNA_def_property(srna, "lock_camera", PROP_BOOLEAN, PROP_NONE);
 	RNA_def_property_boolean_sdna(prop, NULL, "flag2", V3D_LOCK_CAMERA);
 	RNA_def_property_ui_text(prop, "Lock Camera to View", "Enable view navigation within the camera view");




More information about the Bf-blender-cvs mailing list