-
Notifications
You must be signed in to change notification settings - Fork 2
/
draw3d.c
2581 lines (2283 loc) · 97.3 KB
/
draw3d.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "stdafx.h"
#include "LoftyCAD.h"
#include <stdio.h>
// A good-sized array of mouse move coordinates. Used to draw out arcs and beziers.
#define MAX_MOVES 400
static Point move_points[MAX_MOVES];
static int num_moves = 0;
static Plane temp_plane;
// Current drawn number increments for every invalidate and draw.
// (rollover after 2^32 invalidates - unlikely ever)
static unsigned int curr_drawn_no = 0;
// The halo list. Initialise to NULL here so rogue pointers don't escape into free lists.
ListHead halo = { NULL, NULL };
// Material array
Material materials[MAX_MATERIAL];
// Flag to turn drawing off while building display lists.
BOOL suppress_drawing = FALSE;
// Display lists for commonly drawn collections of objects.
// Flags to indicate each DL is valid and can be replayed.
#define DRAW_DL 3000
#define HL_DL 3001
#define SEL_DL 3002 // Problem: the selction is drawn differently depending on what is highlighted...
BOOL draw_dl_valid = FALSE; // The object tree
BOOL hl_dl_valid = FALSE; // The highlighted object
BOOL sel_dl_valid = FALSE; // The selected object(s)
// Object ID for the highlight DL, so it can be reused if the object is revisited unchanged
Object* hl_dl_obj = NULL;
#ifdef TIME_DRAWING
LARGE_INTEGER draw_clock_start, draw_clock_end, draw_clock, clock_freq;
int num_draws = 0;
#endif
// Set material and lighting up for the rendered view
void
SetMaterial(int mat, BOOL force_set)
{
static float front_mat_shininess[] = { 30.0f };
static float front_mat_ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
static float front_mat_diffuse[] = { 0.5f, 0.28f, 0.38f, 1.0f };
static float front_mat_specular[] = { 0.5f, 0.5f, 0.5f, 1.0f };
static float back_mat_shininess[] = { 50.0f };
static float back_mat_ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
static float back_mat_diffuse[] = { 1.0f, 1.0f, 0.2f, 1.0f };
static float back_mat_specular[] = { 0.5f, 0.5f, 0.2f, 1.0f };
static int curr_mat = -1;
if (mat != curr_mat || force_set)
{
if (mat == 0)
{
glMaterialfv(GL_FRONT, GL_SHININESS, front_mat_shininess);
glMaterialfv(GL_FRONT, GL_AMBIENT, front_mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, front_mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, front_mat_specular);
glMaterialfv(GL_BACK, GL_SHININESS, back_mat_shininess);
glMaterialfv(GL_BACK, GL_AMBIENT, back_mat_ambient);
glMaterialfv(GL_BACK, GL_DIFFUSE, back_mat_diffuse);
glMaterialfv(GL_BACK, GL_SPECULAR, back_mat_specular);
}
else
{
float col[4];
col[3] = 1.0f;
// Multiply material color by amb/diff separately
col[0] = materials[mat].color[0] * front_mat_ambient[0] * 2.0f;
col[1] = materials[mat].color[1] * front_mat_ambient[1] * 2.0f;
col[2] = materials[mat].color[2] * front_mat_ambient[2] * 2.0f;
glMaterialfv(GL_FRONT, GL_AMBIENT, col);
col[0] = materials[mat].color[0] * front_mat_diffuse[0] * 2.0f;
col[1] = materials[mat].color[1] * front_mat_diffuse[1] * 2.0f;
col[2] = materials[mat].color[2] * front_mat_diffuse[2] * 2.0f;
glMaterialfv(GL_FRONT, GL_DIFFUSE, col);
glMaterialf(GL_FRONT, GL_SHININESS, materials[mat].shiny);
}
curr_mat = mat;
}
}
// Some standard colors sent to GL.
// Color as an object of the given type.
void
color_as(OBJECT obj_type, double color_decay, BOOL construction, PRESENTATION pres, BOOL locked)
{
double r, g, b, a;
// This object is selected. Color it and all its components.
BOOL selected = pres & DRAW_SELECTED;
// This object is highlighted because the mouse is hovering on it.
BOOL highlighted = pres & DRAW_HIGHLIGHT;
// This object is highlighted because it is in the halo.
BOOL in_halo = pres & DRAW_HIGHLIGHT_HALO;
switch (obj_type)
{
case OBJ_GROUP:
case OBJ_VOLUME:
return; // no action here
case OBJ_POINT:
case OBJ_EDGE:
if (construction)
{
r = 0.3f;
g = 0.3f;
b = 0.5f;
}
else
{
r = g = b = 0.5f;
}
a = 1.0f;
if (selected)
r += 0.4f;
if (highlighted)
g += 0.4f;
// Halo treatment varies with the blend mode. Remember that halo edges are drawn (at least) twice.
if (in_halo)
{
switch (view_blend)
{
case BLEND_ALPHA:
g += 0.2f;
a = 0.5f * color_decay + 0.1f;
break;
case BLEND_MULTIPLY:
r = 0.9f - 0.2f * color_decay;
g = 1.0f;
b = 0.9f - 0.2f * color_decay;
break;
default: // opaque
g += 0.15f * color_decay + 0.05f;
break;
}
}
if (locked)
r = g = b = 0.8f;
break;
case OBJ_FACE:
if (construction)
{
r = 0.9f;
g = 0.9f;
b = 1.0f;
}
else
{
r = g = b = 0.75f;
}
a = 0.6f;
if (selected)
r += 0.2f;
if (highlighted)
g += 0.2f;
// Halo treatment varies with the blend mode. Remember that halo faces are drawn twice.
if (in_halo)
{
switch (view_blend)
{
case BLEND_ALPHA:
g += 0.2f;
a = 0.5f * color_decay + 0.1f;
break;
case BLEND_MULTIPLY:
r = 0.9f - 0.2f * color_decay;
g = 1.0f;
b = 0.9f - 0.2f * color_decay;
break;
default: // opaque
g += 0.15f * color_decay + 0.05f;
break;
}
}
if (locked)
r = g = b = 1.0f;
break;
}
glColor4d(r, g, b, a);
}
// Color a passed object.
void
color(Object* obj, BOOL construction, PRESENTATION pres, BOOL locked)
{
double color_decay = 1.0f;
if (pres & DRAW_HIGHLIGHT_HALO)
color_decay = 0;
if (obj->type == OBJ_FACE)
color_decay *= ((Face*)obj)->color_decay;
color_as(obj->type, color_decay, construction, pres, locked);
}
// Draw a single mesh triangle with normal and material index.
void
draw_triangle(void *arg, int mat, double x[3], double y[3], double z[3])
{
int i;
double A, B, C, length;
SetMaterial(mat, FALSE);
cross(x[1] - x[0], y[1] - y[0], z[1] - z[0], x[2] - x[0], y[2] - y[0], z[2] - z[0], &A, &B, &C);
length = sqrt(A * A + B * B + C * C);
if (!nz(length))
{
A /= length;
B /= length;
C /= length;
glNormal3d(A, B, C);
}
for (i = 0; i < 3; i++)
glVertex3d(x[i], y[i], z[i]);
}
// Helpers for all clipping tests: determine if a point is clipped out by any
// clipping plane that is in effect. One or two sided test depending on whether
// we're restricting picking to the clipping plane.
BOOL
clipped(Point* p)
{
double f;
if (!view_clipped)
return FALSE;
f = clip_plane.A * p->x + clip_plane.B * p->y + clip_plane.C * p->z + clip_plane.D;
if (draw_on_clip_plane)
return f < -tolerance || f > tolerance;
else
return f > tolerance;
}
// Determine if a point is clipped out by its coordinates. One sided test.
BOOL
clippedv(double x, double y, double z)
{
double f;
if (!view_clipped)
return FALSE;
f = clip_plane.A * x + clip_plane.B * y + clip_plane.C * z + clip_plane.D;
return f > 0;
}
// Determine if a bbox is partially clipped (lies across the clipping plane)
BOOL
is_bbox_clipped(Bbox* box)
{
int count = 0;
if (clippedv(box->xmin, box->ymin, box->zmin))
count++;
if (clippedv(box->xmax, box->ymin, box->zmin))
count++;
if (clippedv(box->xmin, box->ymax, box->zmin))
count++;
if (clippedv(box->xmax, box->ymax, box->zmin))
count++;
if (clippedv(box->xmin, box->ymin, box->zmax))
count++;
if (clippedv(box->xmax, box->ymin, box->zmax))
count++;
if (clippedv(box->xmin, box->ymax, box->zmax))
count++;
if (clippedv(box->xmax, box->ymax, box->zmax))
count++;
if (count == 0 || count == 8)
return FALSE;
return TRUE;
}
// Determine if a triangle is partially clipped (by its coordinates)
BOOL
is_tri_clipped(double x[3], double y[3], double z[3])
{
int count = 0;
if (clippedv(x[0], y[0], z[0]))
count++;
if (clippedv(x[1], y[1], z[1]))
count++;
if (clippedv(x[2], y[2], z[2]))
count++;
if (count == 0 || count == 3)
return FALSE;
return TRUE;
}
// Draw the clip plane intersection with a triangle (given either by point coords or by edges)
void
clip_triangle_by_coords(void* arg, int mat, double x[3], double y[3], double z[3])
{
int rc, count = 0;
Point pt, points[6];
Plane* plane = (Plane*)arg;
if (!is_tri_clipped(x, y, z))
return;
rc = intersect_segment_plane(x[0], y[0], z[0], x[1], y[1], z[1], plane, &pt);
if (rc == 1)
points[count++] = pt;
rc = intersect_segment_plane(x[1], y[1], z[1], x[2], y[2], z[2], plane, &pt);
if (rc == 1)
points[count++] = pt;
rc = intersect_segment_plane(x[2], y[2], z[2], x[0], y[0], z[0], plane, &pt);
if (rc == 1)
points[count++] = pt;
if (count == 2)
{
glVertex3d(points[0].x, points[0].y, points[0].z);
glVertex3d(points[1].x, points[1].y, points[1].z);
}
}
void
clip_triangle_by_edges(Face* f, Plane* plane)
{
int rc, count = 0;
Point pt, points[6];
Edge *e;
Point* p0, * p1;
e = f->edges[0];
p0 = e->endpoints[0];
p1 = e->endpoints[1];
rc = intersect_segment_plane(p0->x, p0->y, p0->z, p1->x, p1->y, p1->z, plane, &pt);
if (rc == 1)
points[count++] = pt;
e = f->edges[1];
p0 = e->endpoints[0];
p1 = e->endpoints[1];
rc = intersect_segment_plane(p0->x, p0->y, p0->z, p1->x, p1->y, p1->z, plane, &pt);
if (rc == 1)
points[count++] = pt;
e = f->edges[2];
p0 = e->endpoints[0];
p1 = e->endpoints[1];
rc = intersect_segment_plane(p0->x, p0->y, p0->z, p1->x, p1->y, p1->z, plane, &pt);
if (rc == 1)
points[count++] = pt;
if (count == 2)
{
glVertex3d(points[0].x, points[0].y, points[0].z);
glVertex3d(points[1].x, points[1].y, points[1].z);
}
}
// For volumes in the object tree, if triangles in their surface mesh intersect the
// clipping plane, draw line segments across them to represent the sliced surface.
void
draw_clip_intersection(Group *tree)
{
Object* obj;
Volume* vol;
Face* f;
// Go through the volumes in the tree and intersect each one
for (obj = tree->obj_list.head; obj != NULL; obj = obj->next)
{
switch (obj->type)
{
case OBJ_VOLUME:
vol = (Volume*)obj;
if (!is_bbox_clipped(&vol->bbox))
break;
// Make sure there's a triangle mesh. Only render if we can't get it any other
// way, to save time. Large triangle meshes do not need rendering.
if (vol->max_facetype == FACE_TRI)
{
color_as(OBJ_EDGE, 1.0f, TRUE, DRAW_PATH, FALSE);
glBegin(GL_LINES);
for (f = (Face*)vol->faces.head; f != NULL; f = (Face*)f->hdr.next)
clip_triangle_by_edges(f, &clip_plane);
glEnd();
}
else
{
if (!vol->mesh_valid)
{
for (f = (Face*)vol->faces.head; f != NULL; f = (Face*)f->hdr.next)
gen_view_list_surface(f);
vol->mesh_valid = TRUE;
}
color_as(OBJ_EDGE, 1.0f, TRUE, DRAW_PATH, FALSE);
glBegin(GL_LINES);
mesh_foreach_face_coords_mat(vol->mesh, clip_triangle_by_coords, &clip_plane);
glEnd();
}
break;
case OBJ_GROUP:
draw_clip_intersection((Group*)obj);
break;
}
}
}
// Draw any object. Control select/highlight colors per object type, how the parent is locked,
// and whether to draw components or just the top-level object, among other things.
void
draw_object(Object *obj, PRESENTATION pres, LOCK parent_lock)
{
int i;
Face *face;
Edge *edge;
ZPolyEdge* zedge;
ArcEdge *ae;
BezierEdge *be;
Point *p;
Object *o;
Volume *vol;
Group *group;
double dx, dy, dz;
BOOL locked, constr_edge, re_enable;
// This object is selected. Color it and all its components.
BOOL selected = pres & DRAW_SELECTED;
// This object is highlighted because the mouse is hovering on it.
BOOL highlighted = pres & DRAW_HIGHLIGHT;
// This object is highlighted because it is in the halo.
BOOL in_halo = pres & DRAW_HIGHLIGHT_HALO;
// We're drawing, so we want to see the snap targets even if they are locked. But only under the mouse.
BOOL snapping = pres & DRAW_HIGHLIGHT_LOCKED;
// Whether to show dimensions (all the time if highlighted or selected, but don't pass to components)
BOOL show_dims = obj->show_dims || (pres & DRAW_WITH_DIMENSIONS);
BOOL draw_components = !highlighted || !snapping;
switch (obj->type)
{
case OBJ_POINT:
locked = !(snapping || parent_lock < obj->type);
p = (Point *)obj;
if ((selected || highlighted) && locked)
return;
if (selected || highlighted)
{
double unit = zTrans / (-2 * half_size);
// Draw a square blob in the facing plane, so it's more easily seen
glDisable(GL_CULL_FACE);
glBegin(GL_POLYGON);
color(obj, FALSE, pres, locked);
switch (facing_index)
{
case PLANE_XY:
case PLANE_MINUS_XY:
dx = unit;
dy = unit;
dz = 0;
glVertex3d(p->x - dx, p->y - dy, p->z);
glVertex3d(p->x + dx, p->y - dy, p->z);
glVertex3d(p->x + dx, p->y + dy, p->z);
glVertex3d(p->x - dx, p->y + dy, p->z);
break;
case PLANE_YZ:
case PLANE_MINUS_YZ:
dx = 0;
dy = unit;
dz = unit;
glVertex3d(p->x, p->y - dy, p->z - dz);
glVertex3d(p->x, p->y + dy, p->z - dz);
glVertex3d(p->x, p->y + dy, p->z + dz);
glVertex3d(p->x, p->y - dy, p->z + dz);
break;
case PLANE_XZ:
case PLANE_MINUS_XZ:
dx = unit;
dy = 0;
dz = unit;
glVertex3d(p->x - dx, p->y, p->z - dz);
glVertex3d(p->x + dx, p->y, p->z - dz);
glVertex3d(p->x + dx, p->y, p->z + dz);
glVertex3d(p->x - dx, p->y, p->z + dz);
}
glEnd();
glEnable(GL_CULL_FACE);
}
else
{
// Just draw the point (if not locked), the picking will still work
if (!locked)
{
if (!(selected || highlighted))
{
if (p->drawn == curr_drawn_no)
return;
p->drawn = curr_drawn_no;
}
glBegin(GL_POINTS);
color(obj, FALSE, pres, locked);
glVertex3d(p->x, p->y, p->z);
glEnd();
}
}
break;
case OBJ_EDGE:
locked = !(snapping || parent_lock < obj->type);
edge = (Edge *)obj;
if ((edge->type & EDGE_CONSTRUCTION) && !view_constr)
return;
if ((selected || highlighted) && locked)
return;
constr_edge = (pres & DRAW_PATH) != 0 || (edge->type & EDGE_CONSTRUCTION) != 0;
// Disable blending here so highlight shows up with multiply-blending
re_enable = FALSE;
if (selected || highlighted /*|| in_halo*/ || (pres & DRAW_PATH) != 0)
{
re_enable = glIsEnabled(GL_BLEND);
glDisable(GL_BLEND);
}
else // normal drawing, check the drawn no. and don't draw shared edges twice
{
if (edge->drawn == curr_drawn_no /*&& !in_halo*/)
return;
edge->drawn = curr_drawn_no;
}
switch (edge->type & ~EDGE_CONSTRUCTION)
{
case EDGE_STRAIGHT:
glBegin(GL_LINES);
color(obj, constr_edge, pres, locked);
glVertex3d(edge->endpoints[0]->x, edge->endpoints[0]->y, edge->endpoints[0]->z);
glVertex3d(edge->endpoints[1]->x, edge->endpoints[1]->y, edge->endpoints[1]->z);
glEnd();
if (draw_components)
{
draw_object((Object *)edge->endpoints[0], (pres & ~DRAW_WITH_DIMENSIONS), parent_lock);
draw_object((Object *)edge->endpoints[1], (pres & ~DRAW_WITH_DIMENSIONS), parent_lock);
}
break;
case EDGE_ARC:
ae = (ArcEdge *)edge;
gen_view_list_arc(ae);
glBegin(GL_LINE_STRIP);
color(obj, constr_edge, pres, locked);
for (p = (Point *)edge->view_list.head; p != NULL; p = (Point *)p->hdr.next)
glVertex3d(p->x, p->y, p->z);
glEnd();
if (draw_components)
{
draw_object((Object *)ae->centre, (pres & ~DRAW_WITH_DIMENSIONS), parent_lock);
draw_object((Object *)edge->endpoints[0], (pres & ~DRAW_WITH_DIMENSIONS), parent_lock);
draw_object((Object *)edge->endpoints[1], (pres & ~DRAW_WITH_DIMENSIONS), parent_lock);
}
break;
case EDGE_BEZIER:
be = (BezierEdge *)edge;
gen_view_list_bez(be);
glBegin(GL_LINE_STRIP);
color(obj, constr_edge, pres, locked);
for (p = (Point *)edge->view_list.head; p != NULL; p = (Point *)p->hdr.next)
glVertex3d(p->x, p->y, p->z);
glEnd();
if (draw_components)
{
draw_object((Object *)edge->endpoints[0], (pres & ~DRAW_WITH_DIMENSIONS), parent_lock);
draw_object((Object *)edge->endpoints[1], (pres & ~DRAW_WITH_DIMENSIONS), parent_lock);
draw_object((Object *)be->ctrlpoints[0], (pres & ~DRAW_WITH_DIMENSIONS), parent_lock);
draw_object((Object *)be->ctrlpoints[1], (pres & ~DRAW_WITH_DIMENSIONS), parent_lock);
}
break;
case EDGE_ZPOLY:
zedge = (ZPolyEdge*)edge;
color(obj, constr_edge, pres, locked);
spaghetti(zedge, print_zmin, print_zmax);
break;
}
if (re_enable)
glEnable(GL_BLEND);
break;
case OBJ_FACE:
face = (Face *)obj;
if (face->vol != NULL)
locked = parent_lock > obj->type; // allow picking faces to get to the volume
else
locked = parent_lock >= obj->type;
if ((face->type & FACE_CONSTRUCTION) && !view_constr)
return;
if (face->vol == NULL || !materials[face->vol->material].hidden)
{
gen_view_list_face(face);
face_shade(rtess, face, pres, locked);
}
// Don't pass draw with dims down to sub-components, to minimise clutter
if (draw_components)
{
for (i = 0; i < face->n_edges; i++)
draw_object((Object *)face->edges[i], (pres & ~DRAW_WITH_DIMENSIONS), parent_lock);
}
if (face->text != NULL)
{
// Draw the origin and endpoint so it can be picked and moved (unless face is locked at face level)
draw_object((Object *)&face->text->origin, pres, parent_lock < LOCK_FACES ? LOCK_NONE : parent_lock);
draw_object((Object *)&face->text->endpt, pres, parent_lock < LOCK_FACES ? LOCK_NONE : parent_lock);
}
break;
case OBJ_VOLUME:
vol = (Volume *)obj;
if (view_rendered)
{
// Draw from the triangulated mesh for the volume. (only for incomplete meshes)
ASSERT(vol->mesh_valid, "Mesh is not up to date");
color(obj, FALSE, pres, FALSE);
glBegin(GL_TRIANGLES);
mesh_foreach_face_coords_mat(vol->mesh, draw_triangle, NULL);
glEnd();
}
else if (vol->max_facetype == FACE_TRI) // special cases for speed
{
ListHead elist = { NULL, NULL };
gen_view_list_vol(vol);
// Output triangle faces, putting their edges in a queue to do later.
// There will be no construction faces in any volume, so just color it once.
locked = parent_lock > OBJ_FACE;
color_as(OBJ_FACE, 1.0f, FALSE, pres, locked);
glBegin(GL_TRIANGLES);
for (face = (Face*)vol->faces.head; face != NULL; face = (Face*)face->hdr.next)
{
for (i = 0, p = (Point*)face->view_list.head; i < 3 && p != NULL; i++, p = (Point*)p->hdr.next)
glVertex3d(p->x, p->y, p->z);
if (draw_components)
{
for (i = 0; i < face->n_edges; i++)
{
edge = face->edges[i];
if (edge->drawn == curr_drawn_no)
continue;
edge->drawn = curr_drawn_no;
link_tail((Object*)edge, &elist);
}
}
}
glEnd();
// Draw all the edges. There will be no construction edges, so just color it once.
locked = parent_lock >= OBJ_EDGE;
color_as(OBJ_EDGE, 1.0f, FALSE, pres, locked);
glBegin(GL_LINES);
for (edge = (Edge*)elist.head; edge != NULL; edge = (Edge*)edge->hdr.next)
{
glVertex3d(edge->endpoints[0]->x, edge->endpoints[0]->y, edge->endpoints[0]->z);
glVertex3d(edge->endpoints[1]->x, edge->endpoints[1]->y, edge->endpoints[1]->z);
}
glEnd();
}
else
{
// Draw individual faces
gen_view_list_vol(vol);
for (face = (Face *)vol->faces.head; face != NULL; face = (Face *)face->hdr.next)
draw_object((Object *)face, (pres & ~DRAW_WITH_DIMENSIONS), parent_lock);
}
break;
case OBJ_GROUP:
// Draw from the triangulated mesh, and then draw any remaining
// volume meshes that were not completely merged.
group = (Group *)obj;
if (view_rendered)
{
// The object tree is a group, but it is never merged.
if (group->mesh != NULL && group->mesh_valid && !group->mesh_merged)
{
glBegin(GL_TRIANGLES);
mesh_foreach_face_coords_mat(group->mesh, draw_triangle, NULL);
glEnd();
}
if (!group->mesh_complete)
{
for (o = group->obj_list.head; o != NULL; o = o->next)
{
BOOL merged = FALSE;
if (o->type == OBJ_GROUP)
merged = ((Group*)o)->mesh_merged;
else if (o->type == OBJ_VOLUME)
merged = ((Volume*)o)->mesh_merged;
else
continue; // can't render edges, points, etc.
if (!merged)
draw_object(o, (pres & ~DRAW_WITH_DIMENSIONS), o->lock);
}
}
}
else
{
// Not a rendered view - just draw the thing no matter what. Take account of a locked group.
for (o = group->obj_list.head; o != NULL; o = o->next)
{
draw_object
(
o,
(pres & ~DRAW_WITH_DIMENSIONS),
parent_lock == LOCK_GROUP ? LOCK_GROUP : o->lock
);
}
}
break;
}
if (show_dims)
show_dims_on(obj, pres, parent_lock);
}
// When about to draw on an object:
// Assign a picked_plane, based on where the mouse has moved to. We still might
// not have a picked_plane after calling this, but we will be back.
void
assign_picked_plane(POINT pt)
{
Object *obj = Pick(pt.x, pt.y, FALSE);
if (obj == NULL)
{
// We may have started on an edge or at a point, which is not necessarily on the
// facing plane. Make a plane through the first point picked.
temp_plane = *facing_plane;
temp_plane.refpt = picked_point;
picked_plane = &temp_plane;
}
else if (obj->type == OBJ_FACE)
{
// Moved onto face - stay on its plane
picked_plane = &((Face *)obj)->normal;
picked_obj = obj;
}
else if (obj->type == OBJ_VOLUME || obj->type == OBJ_GROUP)
{
// go back to the raw picked obj (underlying face) to get a plane
if (raw_picked_obj != NULL && raw_picked_obj->type == OBJ_FACE)
{
picked_plane = &((Face *)raw_picked_obj)->normal;
picked_obj = raw_picked_obj;
}
}
// other picked obj types just wait till the mouse moves off them
}
// Draw the contents of the main window. Everything happens in here.
void CALLBACK
Draw(void)
{
float matRot[4][4];
POINT pt;
Object *obj;
PRESENTATION pres;
Object *highlight_obj = NULL;
if (suppress_drawing)
return;
if (app_state != STATE_NONE)
invalidate_dl();
// handle mouse movement actions.
// Highlight pick targets (use highlight_obj for this)
// If rendering, don't do any picks in here (enables smooth orbiting and spinning)
if (!left_mouse && !right_mouse && !view_rendered && !view_printer)
{
auxGetMouseLoc(&pt.x, &pt.y);
highlight_obj = Pick(pt.x, pt.y, FALSE);
// See if we are in the treeview window and have something to highlight from there
if (highlight_obj != NULL)
treeview_highlight = NULL;
else
highlight_obj = treeview_highlight;
// stop stale scaling directions from showing on hover
scaled = 0;
// Tailor feedback to the action (e.g. extruding faces). Some other faces or edges
// may be put into the halo list. Some types of objects will have their highlighting
// suppressed if the action cannot be performed upon them.
free_obj_list(&halo);
if (highlight_obj != NULL)
{
if (highlight_obj->type == OBJ_GROUP)
{
if (app_state == STATE_STARTING_EXTRUDE || app_state == STATE_STARTING_EXTRUDE_LOCAL)
highlight_obj = NULL;
}
else if (highlight_obj->type == OBJ_VOLUME && raw_picked_obj != NULL)
{
find_corner_edges
(
raw_picked_obj, // get the face
highlight_obj,
&halo
);
// If volume locked at face level, highlight the face if extruding
// TODO: only highlight faces that are legal to extrude
if (app_state == STATE_STARTING_EXTRUDE || app_state == STATE_STARTING_EXTRUDE_LOCAL)
{
if (extrudible(raw_picked_obj))
highlight_obj = raw_picked_obj;
else
highlight_obj = NULL;
}
}
else if (highlight_obj->type == OBJ_FACE)
{
find_corner_edges
(
highlight_obj,
find_parent_object(&object_tree, highlight_obj, FALSE),
&halo
);
if (app_state == STATE_STARTING_EXTRUDE || app_state == STATE_STARTING_EXTRUDE_LOCAL)
{
if (!extrudible(highlight_obj))
highlight_obj = NULL;
}
}
else if (highlight_obj->type == OBJ_EDGE)
{
// Mark out adjacent corner edges.
find_corner_edges
(
highlight_obj,
find_parent_object(&object_tree, highlight_obj, FALSE),
&halo
);
if (app_state == STATE_STARTING_EXTRUDE || app_state == STATE_STARTING_EXTRUDE_LOCAL)
{
highlight_obj = NULL;
}
else if (app_state == STATE_STARTING_SCALE || app_state == STATE_STARTING_ROTATE)
{
highlight_obj = NULL;
}
}
// If we're editing any object in a group, highlight the whole group softly
if (highlight_obj != NULL && parent_picked != NULL)
link_single(parent_picked, &halo);
}
// Set up the halo if we are doing halo highlighting for smooth extrusions, etc.
// view_halo controls this (but corner edges etc. are still put in the halo list for highlighting)
if
(
view_halo
&&
highlight_obj != NULL
&&
treeview_highlight == NULL
&&
highlight_obj->type == OBJ_FACE
)
calc_halo_params((Face*)highlight_obj, &halo);
}
else if (left_mouse && app_state != STATE_DRAGGING_SELECT)
{
// If we're performing a left mouse action, we are dragging an object
// and so picking is unreliable (there are always self-picks). Also, we
// need a full XYZ coordinate to perform snapping properly, and we're
// not necessarily snapping things at the mouse position. So we can't use
// picking here.
if (app_state == STATE_MOVING)
highlight_obj = find_in_neighbourhood(picked_obj, &object_tree);
else
highlight_obj = find_in_neighbourhood(curr_obj, &object_tree);
}
// Handle left mouse dragging actions. We must be moving or drawing,
// otherwise the trackball would have it and we wouldn't be here.
if (left_mouse)
{
auxGetMouseLoc(&pt.x, &pt.y);
// Use XYZ coordinates rather than mouse position deltas, as we may
// have snapped and we want to preserve the accuracy. Just use mouse
// position to check for gross movement.
if (pt.x != left_mouseX || pt.y != left_mouseY)
{
Point p1, p2, p3, p4;
Point *p00, *p01, *p02, *p03, *p04, *p05;
Point d1, d3, dn, da;
Edge *e;
ArcEdge *ae;
BezierEdge *be;
Plane grad0, grad1;
double dist;
Face *rf, *bf;
Plane norm;
Object *parent, *dummy;
// Flag that the mouse has been moved.
mouse_moved = TRUE;
switch (app_state)
{
case STATE_DRAGGING_SELECT:
// Each move, clear the selection, then pick all objects lying within
// the selection rectangle, adding their top-level parents to the selection.
clear_selection(&selection);
Pick_all_in_rect
(
(orig_left_mouseX + pt.x) / 2,
(orig_left_mouseY + pt.y) / 2,
abs(pt.x - orig_left_mouseX),
abs(pt.y - orig_left_mouseY)
);
break;
case STATE_MOVING:
// Move the selection, or an object, by a delta in XYZ within the facing plane
intersect_ray_plane(pt.x, pt.y, facing_plane, &new_point);
// NOTE: shift-dragging will drag a selection, unless you hold shift after mouse down.
if (key_status & AUX_SHIFT)
snap_to_angle(facing_plane, &picked_point, &new_point, 45);
else if (snapping_to_angle)
snap_to_angle(facing_plane, &picked_point, &new_point, angle_snap);
snap_to_grid(facing_plane, &new_point, key_status & AUX_CONTROL);
parent = find_top_level_parent(picked_obj);
// moving a single object (or group) under the cursor
// allow moving handles even if selected
if
(
!is_selected_direct(picked_obj, &dummy)
&&
(parent->type == OBJ_GROUP || parent->lock < parent->type)
)
{
move_obj
(
picked_obj,
new_point.x - last_point.x,
new_point.y - last_point.y,
new_point.z - last_point.z
);
// Move any corner edges/faces adjacent to edges of this face
move_corner_edges
(
&halo,
new_point.x - last_point.x,
new_point.y - last_point.y,
new_point.z - last_point.z
);