4
* ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version. The Blender
10
* Foundation also sells licenses for use in proprietary software under
11
* the Blender License. See http://www.blender.org/BL/ for information
14
* This program is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU General Public License for more details.
19
* You should have received a copy of the GNU General Public License
20
* along with this program; if not, write to the Free Software Foundation,
21
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
* The Original Code is Copyright (C) 2004 by NaN Holding BV.
24
* All rights reserved.
26
* The Original Code is: all of this file.
28
* Contributor(s): none yet.
30
* ***** END GPL/BL DUAL LICENSE BLOCK *****
42
#include "MEM_guardedalloc.h"
45
#include "DNA_mesh_types.h"
46
#include "DNA_meshdata_types.h"
47
#include "DNA_object_types.h"
48
#include "DNA_scene_types.h"
49
#include "DNA_screen_types.h"
50
#include "DNA_view3d_types.h"
52
#include "BLI_blenlib.h"
53
#include "BLI_arithb.h"
54
#include "BLI_editVert.h"
56
#include "BKE_displist.h"
57
#include "BKE_global.h"
58
#include "BKE_library.h"
60
#include "BKE_object.h"
61
#include "BKE_utildefines.h"
63
#include "BIF_editmesh.h"
64
#include "BIF_graphics.h"
65
#include "BIF_interface.h"
66
#include "BIF_mywindow.h"
67
#include "BIF_screen.h"
68
#include "BIF_space.h"
69
#include "BIF_toolbox.h"
70
#include "BIF_transform.h"
72
#include "BDR_editobject.h"
82
static float icovert[12][3] = {
84
{144.72, -105.144,-89.443},
85
{-55.277, -170.128,-89.443},
87
{-55.277,170.128,-89.443},
88
{144.72,105.144,-89.443},
89
{55.277,-170.128,89.443},
90
{-144.72,-105.144,89.443},
91
{-144.72,105.144,89.443},
92
{55.277,170.128,89.443},
96
static short icoface[20][3] = {
119
void addvert_mesh(void)
121
EditMesh *em = G.editMesh;
123
float *curs, mat[3][3],imat[3][3];
126
if(G.scene->selectmode==SCE_SELECT_FACE) return;
130
Mat3CpyMat4(mat, G.obedit->obmat);
135
if(v1->f & SELECT) break;
140
/* prevent there are more selected */
141
EM_clear_flag_all(SELECT);
146
VECCOPY(eve->co, curs);
149
VecSubf(eve->co, eve->co, G.obedit->obmat[3]);
151
Mat3MulVecfl(imat, eve->co);
155
addedgelist(v1, eve, NULL);
160
BIF_undo_push("Add vertex");
161
allqueue(REDRAWVIEW3D, 0);
162
makeDispList(G.obedit);
164
while(get_mbut()&R_MOUSE);
168
/* selected faces get hidden edges */
169
static void make_fgon(void)
171
EditMesh *em = G.editMesh;
175
float *nor=NULL; // reference
178
ret= pupmenu("FGon %t|Make|Clear");
182
for(efa= em->faces.first; efa; efa= efa->next) {
183
if(efa->f & SELECT) {
185
efa->e1->h &= ~EM_FGON;
186
efa->e2->h &= ~EM_FGON;
187
efa->e3->h &= ~EM_FGON;
188
if(efa->e4) efa->e4->h &= ~EM_FGON;
191
allqueue(REDRAWVIEW3D, 0);
192
EM_fgon_flags(); // redo flags and indices for fgons
193
makeDispList(G.obedit);
194
BIF_undo_push("Clear FGon");
198
/* tagging edges. rule is:
199
- edge used by exactly 2 selected faces
200
- no vertices allowed with only tagged edges (return)
201
- face normals are allowed to difffer
204
for(eed= em->edges.first; eed; eed= eed->next) {
205
eed->f1= 0; // amount of selected
206
eed->f2= 0; // amount of unselected
209
for(efa= em->faces.first; efa; efa= efa->next) {
210
if(efa->f & SELECT) {
211
if(nor==NULL) nor= efa->n;
212
if(efa->e1->f1 < 3) efa->e1->f1++;
213
if(efa->e2->f1 < 3) efa->e2->f1++;
214
if(efa->e3->f1 < 3) efa->e3->f1++;
215
if(efa->e4 && efa->e4->f1 < 3) efa->e4->f1++;
218
if(efa->e1->f2 < 3) efa->e1->f2++;
219
if(efa->e2->f2 < 3) efa->e2->f2++;
220
if(efa->e3->f2 < 3) efa->e3->f2++;
221
if(efa->e4 && efa->e4->f2 < 3) efa->e4->f2++;
224
// now eed->f1 becomes tagged edge
225
for(eed= em->edges.first; eed; eed= eed->next) {
226
if(eed->f1==2 && eed->f2==0) eed->f1= 1;
230
// no vertices allowed with only tagged edges
231
for(eve= em->verts.first; eve; eve= eve->next) eve->f1= 0;
232
for(eed= em->edges.first; eed; eed= eed->next) {
242
for(eve= em->verts.first; eve; eve= eve->next) {
243
if(eve->f1==1) break;
246
error("Cannot make polygon with interior vertices");
252
error("No faces selected to make FGon");
257
for(eed= em->edges.first; eed; eed= eed->next) {
265
error("Didn't find FGon to create");
268
Mesh *me= G.obedit->data;
269
// signal to save edges with ngon flags
271
me->medge= MEM_callocN(sizeof(MEdge), "fake mesh edge");
273
EM_fgon_flags(); // redo flags and indices for fgons
275
allqueue(REDRAWVIEW3D, 0);
276
makeDispList(G.obedit);
277
BIF_undo_push("Make FGon");
281
/* precondition; 4 vertices selected, check for 4 edges and create face */
282
static EditFace *addface_from_edges(void)
284
EditMesh *em = G.editMesh;
285
EditEdge *eed, *eedar[4]={NULL, NULL, NULL, NULL};
286
EditVert *v1=NULL, *v2=NULL, *v3=NULL, *v4=NULL;
289
/* find the 4 edges */
290
for(eed= em->edges.first; eed; eed= eed->next) {
291
if(eed->f & SELECT) {
292
if(eedar[0]==NULL) eedar[0]= eed;
293
else if(eedar[1]==NULL) eedar[1]= eed;
294
else if(eedar[2]==NULL) eedar[2]= eed;
303
/* find the 2 edges connected to first edge */
305
if( eedar[a]->v1 == v2) v3= eedar[a]->v2;
306
else if(eedar[a]->v2 == v2) v3= eedar[a]->v1;
307
else if( eedar[a]->v1 == v1) v4= eedar[a]->v2;
308
else if(eedar[a]->v2 == v1) v4= eedar[a]->v1;
311
/* verify if last edge exists */
314
if( eedar[a]->v1==v3 && eedar[a]->v2==v4) break;
315
if( eedar[a]->v2==v3 && eedar[a]->v1==v4) break;
318
return addfacelist(v1, v2, v3, v4, NULL, NULL);
325
void addedgeface_mesh(void)
327
EditMesh *em = G.editMesh;
328
EditVert *eve, *neweve[4];
333
if( (G.vd->lay & G.obedit->lay)==0 ) return;
335
/* how many selected ? */
336
if(G.scene->selectmode & SCE_SELECT_EDGE) {
337
/* in edge mode finding selected vertices means flushing down edge codes... */
338
/* can't make face with only edge selection info... */
342
for(eve= em->verts.first; eve; eve= eve->next) {
343
if(eve->f & SELECT) {
346
neweve[amount-1]= eve;
350
eed= addedgelist(neweve[0], neweve[1], NULL);
351
EM_select_edge(eed, 1);
352
BIF_undo_push("Add edge");
353
allqueue(REDRAWVIEW3D, 0);
355
makeDispList(G.obedit);
358
else if(amount > 4) {
363
error("Incorrect number of vertices to make edge/face");
367
efa= NULL; // check later
370
if(exist_face(neweve[0], neweve[1], neweve[2], 0)==0) {
372
efa= addfacelist(neweve[0], neweve[1], neweve[2], 0, NULL, NULL);
373
EM_select_face(efa, 1);
375
else error("The selected vertices already form a face");
378
if(exist_face(neweve[0], neweve[1], neweve[2], neweve[3])==0) {
381
/* remove trias if they exist, 4 cases.... */
382
if(exist_face(neweve[0], neweve[1], neweve[2], NULL)) tria++;
383
if(exist_face(neweve[0], neweve[1], neweve[3], NULL)) tria++;
384
if(exist_face(neweve[0], neweve[2], neweve[3], NULL)) tria++;
385
if(exist_face(neweve[1], neweve[2], neweve[3], NULL)) tria++;
387
if(tria==2) join_triangles();
390
/* if 4 edges exist, we just create the face, convex or not */
391
efa= addface_from_edges();
393
/* the order of vertices can be anything, three cases to check */
394
if( convex(neweve[0]->co, neweve[1]->co, neweve[2]->co, neweve[3]->co) ) {
395
efa= addfacelist(neweve[0], neweve[1], neweve[2], neweve[3], NULL, NULL);
397
else if( convex(neweve[0]->co, neweve[2]->co, neweve[3]->co, neweve[1]->co) ) {
398
efa= addfacelist(neweve[0], neweve[2], neweve[3], neweve[1], NULL, NULL);
400
else if( convex(neweve[0]->co, neweve[2]->co, neweve[1]->co, neweve[3]->co) ) {
401
efa= addfacelist(neweve[0], neweve[2], neweve[1], neweve[3], NULL, NULL);
403
else error("The selected vertices form a concave quad");
408
else error("The selected vertices already form a face");
411
if(efa) { // now we're calculating direction of normal
413
/* dot product view mat with normal, should give info! */
415
EM_select_face(efa, 1);
416
CalcNormFloat(efa->v1->co, efa->v2->co, efa->v3->co, efa->n);
418
inp= efa->n[0]*G.vd->viewmat[0][2] + efa->n[1]*G.vd->viewmat[1][2] + efa->n[2]*G.vd->viewmat[2][2];
420
if(inp < 0.0) flipface(efa);
421
BIF_undo_push("Add face");
425
allqueue(REDRAWVIEW3D, 0);
426
makeDispList(G.obedit);
430
void adduplicate_mesh(void)
437
adduplicateflag(SELECT);
441
BIF_TransformSetUndo("Add Duplicate");
442
initTransform(TFM_TRANSLATION, CTX_NO_PET);
448
void add_primitiveMesh(int type)
450
EditMesh *em = G.editMesh;
452
EditVert *eve, *v1=NULL, *v2, *v3, *v4=NULL, *vtop, *vdown;
453
float *curs, d, dia, phi, phid, cent[3], vec[3], imat[3][3], mat[3][3];
454
float q[4], cmat[3][3], nor[3]= {0.0, 0.0, 0.0};
455
static short tot=32, seg=32, subdiv=2;
456
short a, b, ext=0, fill=0, totoud, newob=0;
457
char *undostr="Add Primitive";
459
if(G.scene->id.lib) return;
461
/* this function also comes from an info window */
462
if ELEM(curarea->spacetype, SPACE_VIEW3D, SPACE_INFO); else return;
465
/* if editmode exists for other type, it exits */
466
check_editmode(OB_MESH);
468
if(G.f & (G_VERTEXPAINT+G_FACESELECT+G_TEXTUREPAINT)) {
469
G.f &= ~(G_VERTEXPAINT+G_FACESELECT+G_TEXTUREPAINT);
470
setcursor_space(SPACE_VIEW3D, CURSOR_EDIT);
473
/* if no obedit: new object and enter editmode */
475
/* add_object actually returns an object ! :-)
476
But it also stores the added object struct in
477
G.scene->basact->object (BASACT->object) */
479
add_object_draw(OB_MESH);
481
G.obedit= BASACT->object;
483
where_is_object(G.obedit);
486
setcursor_space(SPACE_VIEW3D, CURSOR_EDIT);
492
EM_clear_flag_all(SELECT);
494
totoud= tot; /* store, and restore when cube/plane */
496
/* imat and centre and size */
497
Mat3CpyMat4(mat, G.obedit->obmat);
501
cent[0]-= G.obedit->obmat[3][0];
502
cent[1]-= G.obedit->obmat[3][1];
503
cent[2]-= G.obedit->obmat[3][2];
506
Mat3CpyMat4(imat, G.vd->viewmat);
507
Mat3MulVecfl(imat, cent);
508
Mat3MulMat3(cmat, imat, mat);
514
/* ext==extrudeflag, tot==amount of vertices in basis */
521
if(newob) rename_id((ID *)G.obedit, "Plane");
522
if(newob) rename_id((ID *)me, "Plane");
529
if(newob) rename_id((ID *)G.obedit, "Cube");
530
if(newob) rename_id((ID *)me, "Cube");
534
if(button(&tot,3,100,"Vertices:")==0) return;
537
if(newob) rename_id((ID *)G.obedit, "Circle");
538
if(newob) rename_id((ID *)me, "Circle");
539
undostr="Add Circle";
541
case 5: /* cylinder */
542
if(button(&tot,3,100,"Vertices:")==0) return;
545
if(newob) rename_id((ID *)G.obedit, "Cylinder");
546
if(newob) rename_id((ID *)me, "Cylinder");
547
undostr="Add Cylinder";
550
if(button(&tot,3,100,"Vertices:")==0) return;
553
if(newob) rename_id((ID *)G.obedit, "Tube");
554
if(newob) rename_id((ID *)me, "Tube");
558
if(button(&tot,3,100,"Vertices:")==0) return;
561
if(newob) rename_id((ID *)G.obedit, "Cone");
562
if(newob) rename_id((ID *)me, "Cone");
566
if(button(&tot,2,100,"X res:")==0) return;
567
if(button(&seg,2,100,"Y res:")==0) return;
568
if(newob) rename_id((ID *)G.obedit, "Grid");
569
if(newob) rename_id((ID *)me, "Grid");
572
case 11: /* UVsphere */
573
if(button(&seg,3,100,"Segments:")==0) return;
574
if(button(&tot,3,100,"Rings:")==0) return;
575
if(newob) rename_id((ID *)G.obedit, "Sphere");
576
if(newob) rename_id((ID *)me, "Sphere");
577
undostr="Add UV Sphere";
579
case 12: /* Icosphere */
580
if(button(&subdiv,1,5,"Subdivision:")==0) return;
581
if(newob) rename_id((ID *)G.obedit, "Sphere");
582
if(newob) rename_id((ID *)me, "Sphere");
583
undostr="Add Ico Sphere";
585
case 13: /* Monkey */
586
if(newob) rename_id((ID *)G.obedit, "Suzanne");
587
if(newob) rename_id((ID *)me, "Suzanne");
588
undostr="Add Monkey";
592
dia= sqrt(2.0)*G.vd->grid;
598
if(type<10) { /* all types except grid, sphere... */
599
if(ext==0 && type!=7) d= 0;
602
vtop= vdown= v1= v2= 0;
603
for(b=0; b<=ext; b++) {
604
for(a=0; a<tot; a++) {
606
vec[0]= cent[0]+dia*sin(phi);
607
vec[1]= cent[1]+dia*cos(phi);
610
Mat3MulVecfl(imat, vec);
611
eve= addvertlist(vec);
621
/* centre vertices */
625
Mat3MulVecfl(imat,vec);
626
vdown= addvertlist(vec);
630
Mat3MulVecfl(imat,vec);
631
vtop= addvertlist(vec);
637
if(vtop) vtop->f= SELECT;
638
if(vdown) vdown->f= SELECT;
640
/* top and bottom face */
642
if(tot==4 && (type==0 || type==1)) {
644
if(ext) v4= v2->next->next;
646
addfacelist(v3, v1->next, v1, v3->next, NULL, NULL);
647
if(ext) addfacelist(v2, v2->next, v4, v4->next, NULL, NULL);
653
for(a=1; a<tot; a++) {
654
addfacelist(vdown, v3, v3->next, 0, NULL, NULL);
657
addfacelist(vtop, v4, v4->next, 0, NULL, NULL);
662
addfacelist(vdown, v3, v1, 0, NULL, NULL);
663
if(ext) addfacelist(vtop, v4, v2, 0, NULL, NULL);
667
else if(type==4) { /* we need edges for a circle */
670
addedgelist(v3, v3->next, NULL);
673
addedgelist(v3, v1, NULL);
679
for(a=1; a<tot; a++) {
680
addfacelist(v3, v3->next, v4->next, v4, NULL, NULL);
684
addfacelist(v3, v1, v2, v4, NULL, NULL);
686
else if(type==7) { /* cone */
688
for(a=1; a<tot; a++) {
689
addfacelist(vtop, v3->next, v3, 0, NULL, NULL);
692
addfacelist(vtop, v1, v3, 0, NULL, NULL);
695
if(type<2) tot= totoud;
698
else if(type==10) { /* grid */
700
eve= em->verts.first;
706
/* one segment first: the X axis */
708
phid= 2.0/((float)tot-1);
710
vec[0]= cent[0]+dia*phi;
711
vec[1]= cent[1]- dia;
713
Mat3MulVecfl(imat,vec);
714
eve= addvertlist(vec);
717
addedgelist(eve->prev, eve, NULL);
721
/* extrude and translate */
724
Mat3MulVecfl(imat, vec);
725
for(a=0;a<seg-1;a++) {
726
extrudeflag_vert(2, nor); // nor unused
727
translateflag(2, vec);
730
else if(type==11) { /* UVsphere */
732
/* clear all flags */
733
eve= em->verts.first;
739
/* one segment first */
742
for(a=0; a<=tot; a++) {
743
vec[0]= dia*sin(phi);
745
vec[2]= dia*cos(phi);
746
eve= addvertlist(vec);
749
else addedgelist(eve->prev, eve, NULL);
753
/* extrude and rotate */
760
for(a=0; a<seg; a++) {
761
extrudeflag_vert(2, nor); // nor unused
762
rotateflag(2, v1->co, cmat);
765
removedoublesflag(4, 0.0001);
767
/* and now do imat */
768
eve= em->verts.first;
770
if(eve->f & SELECT) {
771
VecAddf(eve->co,eve->co,cent);
772
Mat3MulVecfl(imat,eve->co);
777
else if(type==12) { /* Icosphere */
780
/* clear all flags */
781
eve= em->verts.first;
788
vec[0]= dia*icovert[a][0];
789
vec[1]= dia*icovert[a][1];
790
vec[2]= dia*icovert[a][2];
791
eva[a]= addvertlist(vec);
795
v1= eva[ icoface[a][0] ];
796
v2= eva[ icoface[a][1] ];
797
v3= eva[ icoface[a][2] ];
798
addfacelist(v1, v2, v3, 0, NULL, NULL);
802
for(a=1; a<subdiv; a++) subdivideflag(2, dia, 0);
803
/* and now do imat */
804
eve= em->verts.first;
807
VecAddf(eve->co,eve->co,cent);
808
Mat3MulVecfl(imat,eve->co);
812
} else if (type==13) { /* Monkey */
813
extern int monkeyo, monkeynv, monkeynf;
814
extern signed char monkeyf[][4];
815
extern signed char monkeyv[][3];
816
EditVert **tv= MEM_mallocN(sizeof(*tv)*monkeynv*2, "tv");
820
for (i=0; i<monkeynv; i++) {
822
v[0]= (monkeyv[i][0]+127)/128.0, v[1]= monkeyv[i][1]/128.0, v[2]= monkeyv[i][2]/128.0;
823
tv[i]= addvertlist(v);
825
tv[monkeynv+i]= (fabs(v[0]= -v[0])<0.001)?tv[i]:addvertlist(v);
826
tv[monkeynv+i]->f |= SELECT;
828
for (i=0; i<monkeynf; i++) {
829
efa= addfacelist(tv[monkeyf[i][0]+i-monkeyo], tv[monkeyf[i][1]+i-monkeyo], tv[monkeyf[i][2]+i-monkeyo], (monkeyf[i][3]!=monkeyf[i][2])?tv[monkeyf[i][3]+i-monkeyo]:NULL, NULL, NULL);
830
efa= addfacelist(tv[monkeynv+monkeyf[i][2]+i-monkeyo], tv[monkeynv+monkeyf[i][1]+i-monkeyo], tv[monkeynv+monkeyf[i][0]+i-monkeyo], (monkeyf[i][3]!=monkeyf[i][2])?tv[monkeynv+monkeyf[i][3]+i-monkeyo]:NULL, NULL, NULL);
835
/* and now do imat */
836
for(eve= em->verts.first; eve; eve= eve->next) {
837
if(eve->f & SELECT) {
838
VecAddf(eve->co,eve->co,cent);
839
Mat3MulVecfl(imat,eve->co);
842
recalc_editnormals();
845
// simple selection flush OK, based on fact it's a single model
846
EM_select_flush(); // flushes vertex -> edge -> face selection
848
if(type!=0 && type!=13) righthandfaces(1); // otherwise monkey has eyes in wrong direction...
851
allqueue(REDRAWINFO, 1); /* 1, because header->win==0! */
852
allqueue(REDRAWALL, 0);
853
makeDispList(G.obedit);
855
/* if a new object was created, it stores it in Mesh, for reload original data and undo */
856
if(newob) load_editMesh();
857
BIF_undo_push(undostr);