[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [16965] branches/etch-a-ton/source/blender : First sketching draft code.

Martin Poirier theeth at yahoo.com
Wed Oct 8 00:04:00 CEST 2008


Revision: 16965
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=16965
Author:   theeth
Date:     2008-10-08 00:04:00 +0200 (Wed, 08 Oct 2008)

Log Message:
-----------
First sketching draft code.

Left Click and drag for continuous strokes, left click for exact points, Right click to end strokes.

The view can be manipulated while drawing a stroke (between exact point).

Sketching is turned on all the time, this is mainly useful for testing, do not use seriously, nothing is saved, it gives memory errors on exit, yada yada

Modified Paths:
--------------
    branches/etch-a-ton/source/blender/src/drawview.c
    branches/etch-a-ton/source/blender/src/space.c

Added Paths:
-----------
    branches/etch-a-ton/source/blender/include/BDR_sketch.h
    branches/etch-a-ton/source/blender/include/BIF_sketch.h
    branches/etch-a-ton/source/blender/src/editarmature_sketch.c

Added: branches/etch-a-ton/source/blender/include/BDR_sketch.h
===================================================================
--- branches/etch-a-ton/source/blender/include/BDR_sketch.h	                        (rev 0)
+++ branches/etch-a-ton/source/blender/include/BDR_sketch.h	2008-10-07 22:04:00 UTC (rev 16965)
@@ -0,0 +1,29 @@
+/**
+ * $Id:  $
+ *
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+#ifndef BDR_SKETCH_H
+#define BDR_SKETCH_H
+
+void BDR_queueDrawSketch();
+void BDR_drawSketch();
+
+#endif /* BDR_SKETCH_H */

Added: branches/etch-a-ton/source/blender/include/BIF_sketch.h
===================================================================
--- branches/etch-a-ton/source/blender/include/BIF_sketch.h	                        (rev 0)
+++ branches/etch-a-ton/source/blender/include/BIF_sketch.h	2008-10-07 22:04:00 UTC (rev 16965)
@@ -0,0 +1,28 @@
+/**
+ * $Id:  $
+ *
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+#ifndef BIF_SKETCH_H
+#define BIF_SKETCH_H
+
+int BIF_paintSketch();
+
+#endif /* BIF_SKETCH_H */

Modified: branches/etch-a-ton/source/blender/src/drawview.c
===================================================================
--- branches/etch-a-ton/source/blender/src/drawview.c	2008-10-07 21:54:31 UTC (rev 16964)
+++ branches/etch-a-ton/source/blender/src/drawview.c	2008-10-07 22:04:00 UTC (rev 16965)
@@ -143,6 +143,7 @@
 #include "BDR_vpaint.h"
 #include "BDR_sculptmode.h"
 #include "BDR_gpencil.h"
+#include "BDR_sketch.h"
 
 #include "BSE_drawview.h"
 #include "BSE_filesel.h"
@@ -3264,6 +3265,8 @@
 	/* draw grease-pencil stuff */
 	if (v3d->flag2 & V3D_DISPGP)
 		draw_gpencil_3dview(sa, 1);
+		
+	BDR_drawSketch();
 	
 	persp(PERSP_WIN);  // set ortho
 

Added: branches/etch-a-ton/source/blender/src/editarmature_sketch.c
===================================================================
--- branches/etch-a-ton/source/blender/src/editarmature_sketch.c	                        (rev 0)
+++ branches/etch-a-ton/source/blender/src/editarmature_sketch.c	2008-10-07 22:04:00 UTC (rev 16965)
@@ -0,0 +1,475 @@
+/**
+ * $Id: $
+ *
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+#include <string.h>
+
+#include "MEM_guardedalloc.h"
+
+#include "DNA_listBase.h"
+#include "DNA_scene_types.h"
+
+#include "BLI_blenlib.h"
+#include "BLI_arithb.h"
+
+#include "BKE_utildefines.h"
+
+#include "BSE_view.h"
+
+#include "BIF_gl.h"
+#include "BIF_resources.h"
+#include "BIF_screen.h"
+#include "BIF_space.h"
+#include "BIF_mywindow.h"
+
+#include "mydevice.h"
+
+typedef enum SK_PType
+{
+	PT_CONTINUOUS,
+	PT_EXACT,
+} SK_PType;
+
+typedef struct SK_Point
+{
+	float p[3];
+	SK_PType type;
+} SK_Point;
+
+typedef struct SK_Stroke
+{
+	struct SK_Stroke *next, *prev;
+
+	SK_Point *points;
+	int nb_points;
+	int buf_size;
+} SK_Stroke;
+
+#define SK_Stroke_BUFFER_INIT_SIZE 20
+
+typedef struct SK_DrawData
+{
+	short mval[2];
+	short previous_mval[2];
+	SK_PType type;
+} SK_DrawData;
+
+typedef struct SK_Sketch
+{
+	ListBase	strokes;
+	SK_Stroke	*active_stroke;
+} SK_Sketch;
+
+SK_Sketch *GLOBAL_sketch = NULL;
+
+/**************************************************************/
+
+SK_Sketch* sk_createSketch()
+{
+	SK_Sketch *sketch;
+	
+	sketch = MEM_callocN(sizeof(SK_Sketch), "SK_Sketch");
+	
+	sketch->active_stroke = NULL;
+
+	sketch->strokes.first = NULL;
+	sketch->strokes.last = NULL;
+	
+	return sketch;
+}
+
+void sk_allocStrokeBuffer(SK_Stroke *stk)
+{
+	stk->points = MEM_callocN(sizeof(SK_Point) * stk->buf_size, "SK_Point buffer");
+}
+
+SK_Stroke* sk_createStroke()
+{
+	SK_Stroke *stk;
+	
+	stk = MEM_callocN(sizeof(SK_Stroke), "SK_Stroke");
+	
+	stk->nb_points = 0;
+	stk->buf_size = SK_Stroke_BUFFER_INIT_SIZE;
+	
+	sk_allocStrokeBuffer(stk);
+	
+	return stk;
+}
+
+void sk_shrinkStrokeBuffer(SK_Stroke *stk)
+{
+	if (stk->nb_points < stk->buf_size)
+	{
+		SK_Point *old_points = stk->points;
+		
+		stk->buf_size = stk->nb_points;
+
+		sk_allocStrokeBuffer(stk);		
+		
+		memcpy(stk->points, old_points, sizeof(SK_Point) * stk->nb_points);
+		
+		MEM_freeN(old_points);
+	}
+}
+
+void sk_growStrokeBuffer(SK_Stroke *stk)
+{
+	if (stk->nb_points == stk->buf_size)
+	{
+		SK_Point *old_points = stk->points;
+		
+		stk->buf_size *= 2;
+		
+		sk_allocStrokeBuffer(stk);
+		
+		memcpy(stk->points, old_points, sizeof(SK_Point) * stk->nb_points);
+		
+		MEM_freeN(old_points);
+	}
+}
+
+void sk_appendStrokePoint(SK_Stroke *stk, SK_Point *pt)
+{
+	sk_growStrokeBuffer(stk);
+	
+	memcpy(stk->points + stk->nb_points, pt, sizeof(SK_Point));
+	
+	stk->nb_points++;
+}
+
+/* Apply reverse Chaikin filter to simplify the polyline
+ * */
+void sk_filterStroke(SK_Stroke *stk)
+{
+	SK_Point *old_points = stk->points;
+	int nb_points = stk->nb_points;
+	int i, j;
+	
+	sk_allocStrokeBuffer(stk);
+	stk->nb_points = 0;
+	
+	for (i = 0, j = 0; i < nb_points; i++)
+	{
+		if (i - j == 3)
+		{
+			SK_Point pt;
+			float vec[3];
+			
+			pt.type = PT_CONTINUOUS;
+			pt.p[0] = 0;
+			pt.p[1] = 0;
+			pt.p[2] = 0;
+			
+			VECCOPY(vec, old_points[j].p);
+			VecMulf(vec, -0.25);
+			VecAddf(pt.p, pt.p, vec);
+			
+			VECCOPY(vec, old_points[j+1].p);
+			VecMulf(vec,  0.75);
+			VecAddf(pt.p, pt.p, vec);
+
+			VECCOPY(vec, old_points[j+2].p);
+			VecMulf(vec,  0.75);
+			VecAddf(pt.p, pt.p, vec);
+
+			VECCOPY(vec, old_points[j+3].p);
+			VecMulf(vec, -0.25);
+			VecAddf(pt.p, pt.p, vec);
+			
+			sk_appendStrokePoint(stk, &pt);
+
+			j += 2;
+		}
+		
+		if (old_points[i].type == PT_EXACT)
+		{
+			sk_appendStrokePoint(stk, old_points + i);
+			j = i;
+		}
+	} 
+	
+	MEM_freeN(old_points);
+
+	sk_shrinkStrokeBuffer(stk);
+}
+
+SK_Point *sk_lastStrokePoint(SK_Stroke *stk)
+{
+	SK_Point *pt = NULL;
+	
+	if (stk->nb_points > 0)
+	{
+		pt = stk->points + (stk->nb_points - 1);
+	}
+	
+	return pt;
+}
+
+void sk_drawStroke(SK_Stroke *stk)
+{
+	int i;
+	
+	glColor3f(1, 0.5, 0);
+	glBegin(GL_LINE_STRIP);
+	
+	for (i = 0; i < stk->nb_points; i++)
+	{
+		glVertex3fv(stk->points[i].p);
+	}
+	
+	glEnd();
+	
+	glColor3f(0, 0, 0);
+	glBegin(GL_POINTS);
+
+	for (i = 0; i < stk->nb_points; i++)
+	{
+		if (stk->points[i].type == PT_EXACT)
+		{
+			glVertex3fv(stk->points[i].p);
+		}
+	}
+
+	glEnd();
+
+	glColor3f(1, 1, 1);
+	glBegin(GL_POINTS);
+
+	for (i = 0; i < stk->nb_points; i++)
+	{
+		if (stk->points[i].type == PT_CONTINUOUS)
+		{
+			glVertex3fv(stk->points[i].p);
+		}
+	}
+
+	glEnd();
+}
+
+void sk_startStroke(SK_Sketch *sketch)
+{
+	SK_Stroke *stk = sk_createStroke();
+	
+	BLI_addtail(&sketch->strokes, stk);
+	sketch->active_stroke = stk;
+}
+
+void sk_endStroke(SK_Sketch *sketch)
+{
+	sk_shrinkStrokeBuffer(sketch->active_stroke);
+	sketch->active_stroke = NULL;
+}
+
+void sk_projectPaintData(SK_Stroke *stk, SK_DrawData *dd, float vec[3])
+{
+	/* copied from grease pencil, need fixing */	
+	SK_Point *last = sk_lastStrokePoint(stk);
+	short cval[2];
+	//float *fp = give_cursor();
+	float fp[3] = {0, 0, 0};
+	float dvec[3];
+	
+	if (last != NULL)
+	{
+		VECCOPY(fp, last->p);
+	}
+	
+	/* method taken from editview.c - mouse_cursor() */
+	project_short_noclip(fp, cval);
+	window_to_3d(dvec, cval[0] - dd->mval[0], cval[1] - dd->mval[1]);
+	VecSubf(vec, fp, dvec);
+}
+
+void sk_addStrokePoint(SK_Stroke *stk, SK_DrawData *dd)
+{
+	SK_Point pt;
+	
+	pt.type = dd->type;
+
+	sk_projectPaintData(stk, dd, pt.p);
+
+	sk_appendStrokePoint(stk, &pt);
+	
+	dd->type = PT_CONTINUOUS;
+	
+	dd->previous_mval[0] = dd->mval[0];
+	dd->previous_mval[1] = dd->mval[1];
+}
+
+void sk_endContinuousStroke(SK_Stroke *stk)
+{
+	stk->points[stk->nb_points - 1].type = PT_EXACT;
+}
+
+int sk_stroke_filtermval(SK_DrawData *dd)
+{
+	int retval = 0;
+	if (dd->mval[0] != dd->previous_mval[0] || dd->mval[1] != dd->previous_mval[1])
+	{
+		retval = 1;
+	}
+	
+	return retval;
+}
+
+void sk_initDrawData(SK_DrawData *dd)
+{
+	getmouseco_areawin(dd->mval);
+	dd->previous_mval[0] = -1;
+	dd->previous_mval[1] = -1;
+	dd->type = PT_EXACT;
+}
+
+/********************************************/
+
+void sk_queueRedrawSketch(SK_Sketch *sketch)
+{
+	if (sketch->active_stroke != NULL)
+	{
+		SK_Point *last = sk_lastStrokePoint(sketch->active_stroke);
+		
+		if (last != NULL)
+		{
+			allqueue(REDRAWVIEW3D, 0);
+		}
+	}
+}
+
+void sk_drawSketch(SK_Sketch *sketch)
+{
+	SK_Stroke *stk;
+	
+	glDisable(GL_DEPTH_TEST);
+
+	glLineWidth(BIF_GetThemeValuef(TH_VERTEX_SIZE));
+	glPointSize(BIF_GetThemeValuef(TH_VERTEX_SIZE));
+

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list