[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [29888] branches/soc-2010-nicolasbishop: Added a new mask operator to create a mask from a texture.

Nicholas Bishop nicholasbishop at gmail.com
Sat Jul 3 08:34:07 CEST 2010


Revision: 29888
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=29888
Author:   nicholasbishop
Date:     2010-07-03 08:34:07 +0200 (Sat, 03 Jul 2010)

Log Message:
-----------
Added a new mask operator to create a mask from a texture.

* Moved get_texture value out of MOD_util and into BKE texture
* Added a new pbvh function similar to get_grids but for faces
* The new operator should work for ORCO and UV mapping
* UI is just a new menu item in the drop-down menu next to the texture slots list
* Also fixed a crash in the mask_set operator where sculpt's PBVH wasn't updated

Modified Paths:
--------------
    branches/soc-2010-nicolasbishop/release/scripts/ui/properties_texture.py
    branches/soc-2010-nicolasbishop/source/blender/blenkernel/BKE_customdata.h
    branches/soc-2010-nicolasbishop/source/blender/blenkernel/BKE_texture.h
    branches/soc-2010-nicolasbishop/source/blender/blenkernel/intern/texture.c
    branches/soc-2010-nicolasbishop/source/blender/blenlib/BLI_pbvh.h
    branches/soc-2010-nicolasbishop/source/blender/blenlib/intern/pbvh.c
    branches/soc-2010-nicolasbishop/source/blender/editors/sculpt_paint/paint_intern.h
    branches/soc-2010-nicolasbishop/source/blender/editors/sculpt_paint/paint_mask.c
    branches/soc-2010-nicolasbishop/source/blender/editors/sculpt_paint/paint_ops.c
    branches/soc-2010-nicolasbishop/source/blender/modifiers/intern/MOD_util.c
    branches/soc-2010-nicolasbishop/source/blender/modifiers/intern/MOD_util.h
    branches/soc-2010-nicolasbishop/source/blender/modifiers/intern/MOD_wave.c

Modified: branches/soc-2010-nicolasbishop/release/scripts/ui/properties_texture.py
===================================================================
--- branches/soc-2010-nicolasbishop/release/scripts/ui/properties_texture.py	2010-07-03 05:31:06 UTC (rev 29887)
+++ branches/soc-2010-nicolasbishop/release/scripts/ui/properties_texture.py	2010-07-03 06:34:07 UTC (rev 29888)
@@ -32,6 +32,7 @@
 
         layout.operator("texture.slot_copy", icon='COPYDOWN')
         layout.operator("texture.slot_paste", icon='PASTEDOWN')
+        layout.operator("paint.mask_from_texture")
 
 
 class TEXTURE_MT_envmap_specials(bpy.types.Menu):

Modified: branches/soc-2010-nicolasbishop/source/blender/blenkernel/BKE_customdata.h
===================================================================
--- branches/soc-2010-nicolasbishop/source/blender/blenkernel/BKE_customdata.h	2010-07-03 05:31:06 UTC (rev 29887)
+++ branches/soc-2010-nicolasbishop/source/blender/blenkernel/BKE_customdata.h	2010-07-03 06:34:07 UTC (rev 29888)
@@ -193,7 +193,7 @@
 /* return the data from the first layer matching the parameters  */
 /* these all return NULL if no such layer is found               */
 
-/* returns the data from the first layer matching type */
+/* returns the data from the active layer matching type */
 void *CustomData_get_layer(const struct CustomData *data, int type);
 
 /* returns the data from the nth layer matching type */

Modified: branches/soc-2010-nicolasbishop/source/blender/blenkernel/BKE_texture.h
===================================================================
--- branches/soc-2010-nicolasbishop/source/blender/blenkernel/BKE_texture.h	2010-07-03 05:31:06 UTC (rev 29887)
+++ branches/soc-2010-nicolasbishop/source/blender/blenkernel/BKE_texture.h	2010-07-03 06:34:07 UTC (rev 29888)
@@ -44,6 +44,7 @@
 struct PointDensity;
 struct Tex;
 struct TexMapping;
+struct TexResult;
 struct VoxelData;
 struct World;
 
@@ -108,5 +109,7 @@
 
 int     BKE_texture_dependsOnTime(const struct Tex *texture);
 
+void get_texture_value(struct Tex *texture, float *tex_co, struct TexResult *texres);
+
 #endif
 

Modified: branches/soc-2010-nicolasbishop/source/blender/blenkernel/intern/texture.c
===================================================================
--- branches/soc-2010-nicolasbishop/source/blender/blenkernel/intern/texture.c	2010-07-03 05:31:06 UTC (rev 29887)
+++ branches/soc-2010-nicolasbishop/source/blender/blenkernel/intern/texture.c	2010-07-03 06:34:07 UTC (rev 29888)
@@ -69,7 +69,9 @@
 #include "BKE_node.h"
 #include "BKE_animsys.h"
 
+#include "RE_shader_ext.h"
 
+
 /* ------------------------------------------------------------------------- */
 
 /* All support for plugin textures: */
@@ -1234,3 +1236,20 @@
 }
 
 /* ------------------------------------------------------------------------- */
+
+void get_texture_value(Tex *texture, float *tex_co, TexResult *texres)
+{
+	int result_type;
+
+	result_type = multitex_ext(texture, tex_co, NULL, NULL, 0, texres);
+
+	/* if the texture gave an RGB value, we assume it didn't give a valid
+	* intensity, so calculate one (formula from do_material_tex).
+	* if the texture didn't give an RGB value, copy the intensity across
+	*/
+	if(result_type & TEX_RGB)
+		texres->tin = (0.35f * texres->tr + 0.45f * texres->tg
+				+ 0.2f * texres->tb);
+	else
+		texres->tr = texres->tg = texres->tb = texres->tin;
+}

Modified: branches/soc-2010-nicolasbishop/source/blender/blenlib/BLI_pbvh.h
===================================================================
--- branches/soc-2010-nicolasbishop/source/blender/blenlib/BLI_pbvh.h	2010-07-03 05:31:06 UTC (rev 29887)
+++ branches/soc-2010-nicolasbishop/source/blender/blenlib/BLI_pbvh.h	2010-07-03 06:34:07 UTC (rev 29888)
@@ -122,6 +122,8 @@
 void BLI_pbvh_node_mark_update(PBVHNode *node);
 void BLI_pbvh_node_set_flags(PBVHNode *node, void *data);
 
+void BLI_pbvh_node_get_faces(PBVH *bvh, PBVHNode *node,
+			     int **face_indices, int *totnode);
 void BLI_pbvh_node_get_grids(PBVH *bvh, PBVHNode *node,
 	int **grid_indices, int *totgrid, int *maxgrid, int *gridsize,
 	struct DMGridData ***griddata, struct DMGridAdjacency **gridadj, struct GridKey **gridkey);

Modified: branches/soc-2010-nicolasbishop/source/blender/blenlib/intern/pbvh.c
===================================================================
--- branches/soc-2010-nicolasbishop/source/blender/blenlib/intern/pbvh.c	2010-07-03 05:31:06 UTC (rev 29887)
+++ branches/soc-2010-nicolasbishop/source/blender/blenlib/intern/pbvh.c	2010-07-03 06:34:07 UTC (rev 29888)
@@ -1171,6 +1171,19 @@
 	}
 }
 
+void BLI_pbvh_node_get_faces(PBVH *bvh, PBVHNode *node,
+			     int **face_indices, int *totnode)
+{
+	if(bvh->grids) {
+		if(face_indices) *face_indices= NULL;
+		if(totnode) *totnode= 0;
+	}
+	else {
+		if(face_indices) *face_indices= node->prim_indices;
+		if(totnode) *totnode= node->totprim;
+	}
+}
+
 void BLI_pbvh_node_get_grids(PBVH *bvh, PBVHNode *node, int **grid_indices, int *totgrid, int *maxgrid, int *gridsize, DMGridData ***griddata, DMGridAdjacency **gridadj, GridKey **gridkey)
 {
 	if(bvh->grids) {

Modified: branches/soc-2010-nicolasbishop/source/blender/editors/sculpt_paint/paint_intern.h
===================================================================
--- branches/soc-2010-nicolasbishop/source/blender/editors/sculpt_paint/paint_intern.h	2010-07-03 05:31:06 UTC (rev 29887)
+++ branches/soc-2010-nicolasbishop/source/blender/editors/sculpt_paint/paint_intern.h	2010-07-03 06:34:07 UTC (rev 29888)
@@ -126,7 +126,6 @@
 void PAINT_OT_mask_layer_add(struct wmOperatorType *ot);
 void PAINT_OT_mask_layer_remove(struct wmOperatorType *ot);
 
-/* For now this is just temporary stuff to test masking */
 typedef enum {
 	MASKING_CLEAR,
 	MASKING_FILL,
@@ -134,6 +133,7 @@
 	MASKING_RANDOM,
 } MaskSetMode;
 void PAINT_OT_mask_set(struct wmOperatorType *ot);
+void PAINT_OT_mask_from_texture(struct wmOperatorType *ot);
 
 #endif /* ED_PAINT_INTERN_H */
 

Modified: branches/soc-2010-nicolasbishop/source/blender/editors/sculpt_paint/paint_mask.c
===================================================================
--- branches/soc-2010-nicolasbishop/source/blender/editors/sculpt_paint/paint_mask.c	2010-07-03 05:31:06 UTC (rev 29887)
+++ branches/soc-2010-nicolasbishop/source/blender/editors/sculpt_paint/paint_mask.c	2010-07-03 06:34:07 UTC (rev 29888)
@@ -8,6 +8,7 @@
 #include "DNA_mesh_types.h"
 #include "DNA_meshdata_types.h"
 #include "DNA_object_types.h"
+#include "DNA_material_types.h"
 
 #include "RNA_access.h"
 #include "RNA_define.h"
@@ -23,15 +24,272 @@
 #include "BKE_mesh.h"
 #include "BKE_multires.h"
 #include "BKE_paint.h"
+#include "BKE_texture.h"
 
 #include "BLI_listbase.h"
+#include "BLI_math.h"
 #include "BLI_pbvh.h"
 
 #include "ED_mesh.h"
 #include "ED_sculpt.h"
 #include "paint_intern.h"
 #include "sculpt_intern.h"
+ 
+#include "RE_render_ext.h"
+#include "RE_shader_ext.h"
 
+/* for redraw, just need to update the pbvh's vbo buffers */
+static void paintmask_redraw(bContext *C)
+{
+	Object *ob = CTX_data_active_object(C);
+
+	paint_refresh_mask_display(ob);
+
+	WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);	
+}
+
+/* For now masking requires sculpt mode */
+static int mask_poll(bContext *C)
+{
+	Object *ob = CTX_data_active_object(C);
+	
+	return ob && get_mesh(ob) && ob->sculpt;
+}
+
+static int mask_active_poll(bContext *C)
+{
+	Object *ob = CTX_data_active_object(C);
+
+	if(mask_poll(C)) {
+		Mesh *me = get_mesh(ob);
+		return CustomData_get_active_layer_index(&me->vdata, CD_PAINTMASK) != -1;
+	}
+
+	return 0;
+}
+
+static float get_tex_mask_strength(MTex *tex_slot, float *uv, float vco[3])
+{
+	float texvec[3] = {0, 0, 0};
+	TexResult texres;
+	int mapping = tex_slot->texco;
+	
+	if(mapping == TEXCO_UV && !uv)
+		mapping = TEXCO_ORCO;
+	
+	switch(tex_slot->texco) {
+	case TEXCO_UV:
+		texvec[0] = uv[0] * 2 - 1;
+		texvec[1] = uv[1] * 2 - 1;
+		break;
+	default:
+		copy_v3_v3(texvec, vco);
+	}
+	
+	memset(&texres, 0, sizeof(TexResult));
+	get_texture_value(tex_slot->tex, texvec, &texres);
+	
+	return texres.tin;
+}
+
+/* Set the value of a single mask element from either a UV or a coord */
+#define SET_MASK(elem, uv)						\
+	GRIDELEM_MASK(elem, gridkey)[active] =				\
+		get_tex_mask_strength(tex_slot, uv,			\
+				      GRIDELEM_CO(elem, gridkey))	\
+
+/* Fill active mask layer of entire grid from either MTFaces or coords */
+static void mask_grid_from_tex(DMGridData *grid, int gridsize,
+			       GridKey *gridkey, MTFace *mtface,
+			       MTex *tex_slot, int active)
+{
+	int x, y, boundary;
+
+	boundary = gridsize - 2;
+
+	for(y = 0; y <= boundary; ++y) {
+		for(x = 0; x <= boundary; ++x) {
+			SET_MASK(GRIDELEM_AT(grid, y*gridsize+x, gridkey),
+				 mtface ? mtface->uv[0] : NULL);
+
+			/* Do the edge of the grid separately because the UV
+			   grid is one element smaller on each side compared
+			   to the vert-data grid */
+			if(x == boundary && y == boundary) {
+				SET_MASK(GRIDELEM_AT(grid, (y+1)*gridsize+x+1, gridkey),
+					 mtface ? mtface->uv[2] : NULL);
+			}
+			if(x == boundary) {
+				SET_MASK(GRIDELEM_AT(grid, y*gridsize+x+1, gridkey),
+					 mtface ? mtface->uv[3] : NULL);
+			}
+			if(y == boundary) {
+				SET_MASK(GRIDELEM_AT(grid, (y+1)*gridsize+x, gridkey),
+					 mtface ? mtface->uv[1] : NULL);
+			}
+						
+			if(mtface) ++mtface;
+		}
+	}
+}
+
+static void mask_face_from_tex(MFace *f, MVert *mvert, MTFace *mtface,
+			       float *pmask, MTex *tex_slot, int active)
+{
+	int S = f->v4 ? 4 : 3;
+	int i;
+
+	/* Masks are per-vertex, not per-face-corner, so the mask
+	   value at each vertex comes from one arbitrary face
+	   corner; not averaged or otherwise combined yet */
+	for(i = 0; i < S; ++i) {
+		int vndx = (&f->v1)[i];
+		float *vco = mvert[vndx].co;
+				
+		pmask[vndx] = get_tex_mask_strength(tex_slot,
+				mtface ? mtface->uv[i] : NULL, vco);
+	}
+}
+
+static int paint_mask_from_texture_exec(bContext *C, wmOperator *op)
+{
+	struct Scene *scene;
+	Object *ob;
+	Mesh *me;
+	struct MultiresModifierData *mmd;
+	SculptSession *ss;
+	MTex *tex_slot;
+	DerivedMesh *dm = NULL;
+	PBVH *pbvh;
+	MTFace *mtfaces = NULL;
+	PBVHNode **nodes;
+	int totnode, n, i, active;
+	
+	tex_slot = CTX_data_pointer_get_type(C, "texture_slot",
+					     &RNA_TextureSlot).data;
+	
+	scene = CTX_data_scene(C);
+	ob = CTX_data_active_object(C);
+	ss = ob->sculpt;
+	me = get_mesh(ob);
+	mmd = paint_multires_active(scene, ob);
+	
+	sculpt_undo_push_begin(ss, "Paint mask from texture");
+

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list