-
Notifications
You must be signed in to change notification settings - Fork 108
/
b3dMain.c
1478 lines (1315 loc) · 43.7 KB
/
b3dMain.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
/****************************************************************************
* PROJECT: Balloon 3D Graphics Subsystem for Squeak
* FILE: b3dMain.c
* CONTENT: Main rasterizer body
*
* AUTHOR: Andreas Raab (ar)
* ADDRESS: Walt Disney Imagineering, Glendale, CA
* EMAIL: andreasr@wdi.disney.com
* RCSID: $Id$
*
* NOTES:
*
*
*****************************************************************************/
#include <stdio.h> /* printf() */
#include <stdlib.h> /* exit() */
#if 0
# include <assert.h> /* assert() */
#endif
#include "b3d.h"
#include "b3dAlloc.h"
#ifndef NULL
# define NULL ((void*)0)
#endif
#if !defined(SQUEAK_BUILTIN_PLUGIN) && !defined(NDEBUG)
/* A warning for sqAssert.h. Ideally we would use the one in the interpreter.
* This is written crappily because we don't yet have warning exported properly
* to dlls on Windows. Do we export warning via __declspec or not?
*/
void
warning(char *s) { /* Print an error message but don't necessarily exit. */
# if 0
if (erroronwarn) error(s);
if (warnpid)
printf("\n%s pid %ld\n", s, (long)warnpid);
else
# endif
printf("\n%s\n", s);
}
#endif
#ifdef B3D_PROFILE
unsigned int b3dObjSetupTime;
unsigned int b3dMapObjectTime;
unsigned int b3dVertexOrderTime;
unsigned int b3dSortFaceTime;
#endif
/* helpers */
#define rasterPosX rasterPos[0]
#define rasterPosY rasterPos[1]
#define rasterPosZ rasterPos[2]
#define rasterPosW rasterPos[3]
#define windowPosX windowPos[0]
#define windowPosY windowPos[1]
#define texCoordS texCoord[0]
#define texCoordT texCoord[1]
#define redValue cc.color[RED_INDEX]
#define greenValue cc.color[GREEN_INDEX]
#define blueValue cc.color[BLUE_INDEX]
#define alphaValue cc.color[ALPHA_INDEX]
/* globals */
B3DRasterizerState *currentState;
B3DActiveEdgeTable *aet;
B3DPrimitiveEdgeList *addedEdges;
B3DEdgeAllocList *edgeAlloc;
B3DFaceAllocList *faceAlloc;
B3DAttrAllocList *attrAlloc;
int nFaces = 0;
int maxFaces = 0;
int maxEdges = 0;
/*************************************************************/
/*************************************************************/
/*************************************************************/
void b3dAbort(char *msg){
printf("%s\n", msg);
exit(-1);
}
void b3dValidateEdgeOrder(B3DPrimitiveEdgeList *list)
{
int i;
if(list->size)
if(list->data[0]->leftFace == list->data[0]->rightFace) {
b3dAbort("Left face == right face");
}
for(i=1; i<list->size; i++) {
if(list->data[i-1]->xValue > list->data[i]->xValue) {
b3dAbort("Edge list is broken");
}
if(list->data[i]->leftFace == list->data[i]->rightFace) {
b3dAbort("Left face == right face");
}
}
}
void b3dValidateAETOrder(B3DActiveEdgeTable *list)
{
int i;
if(list->size)
if(list->data[0]->leftFace == list->data[0]->rightFace) {
b3dAbort("Left face == right face");
}
for(i=1; i<list->size; i++) {
if(list->data[i-1]->xValue > list->data[i]->xValue) {
b3dAbort("Edge list is broken");
}
if(list->data[i]->leftFace == list->data[i]->rightFace) {
b3dAbort("Left face == right face");
}
}
}
/*************************************************************/
/*************************************************************/
/*************************************************************/
/* b3dInitializeFace:
Allocate a new primitive face based on the given vertices.
Do the necessary initial setup, but don't set up any drawing attributes yet.
Return the newly created face.
NOTE: May cause allocation of one face!
*/
B3DPrimitiveFace *b3dInitializeFace(B3DPrimitiveVertex *v0,
B3DPrimitiveVertex *v1,
B3DPrimitiveVertex *v2,
B3DTexture *texture,
int attrFlags)
{
B3DPrimitiveFace *face;
/* Compute major and minor reference edges */
{
float majorDx = v2->rasterPosX - v0->rasterPosX;
float majorDy = v2->rasterPosY - v0->rasterPosY;
float minorDx = v1->rasterPosX - v0->rasterPosX;
float minorDy = v1->rasterPosY - v0->rasterPosY;
float area = (majorDx * minorDy) - (minorDx * majorDy);
if(area > -0.001 && area < 0.001) return NULL;
/* Now that we know the face is valid, do the actual allocation */
b3dAllocFace(faceAlloc, face);
if(!face)
return NULL;
face->v0 = v0;
face->v1 = v1;
face->v2 = v2;
face->leftEdge = NULL;
face->rightEdge = NULL;
face->attributes = NULL;
face->oneOverArea = (float) (1.0 / area);
face->majorDx = majorDx;
face->majorDy = majorDy;
face->minorDx = minorDx;
face->minorDy = minorDy;
face->texture = texture;
face->flags |= attrFlags & (B3D_ATTR_MASK << B3D_ATTR_SHIFT);
{ /* Compute dzdx and dzdy */
float majorDz = v2->rasterPosZ - v0->rasterPosZ;
float minorDz = v1->rasterPosZ - v0->rasterPosZ;
face->dzdx = face->oneOverArea * ((majorDz * minorDy) - (minorDz * majorDy));
face->dzdy = face->oneOverArea * ((majorDx * minorDz) - (minorDx * majorDz));
}
}
{/* Compute minZ/maxZ */
float z0 = v0->rasterPosZ;
float z1 = v1->rasterPosZ;
float z2 = v2->rasterPosZ;
if(z0 <= z1) {
if(z1 <= z2) {
face->minZ = z0;
face->maxZ = z2;
} else if(z0 <= z2) {
face->minZ = z0;
face->maxZ = z1;
} else {
face->minZ = z2;
face->maxZ = z1;
}
} else if(z2 <= z1) {
face->minZ = z2;
face->maxZ = z0;
} else if(z0 <= z2) {
face->minZ = z1;
face->maxZ = z0;
} else {
face->minZ = z1;
face->maxZ = z0;
}
} /* End of minZ/maxZ */
return face;
}
/* b3dInitializePass2:
Do a second initialization pass if the face is known to be visible.
*/
int b3dInitializePass2(B3DPrimitiveFace *face)
{
double majorDv, minorDv, baseValue;
double dvdx, dvdy;
B3DPrimitiveAttribute *attr;
B3DPrimitiveVertex *v0 = face->v0;
B3DPrimitiveVertex *v1 = face->v1;
B3DPrimitiveVertex *v2 = face->v2;
{
int ok;
b3dAllocAttrib(attrAlloc, face, ok);
if(!ok) return 0; /* NOT initalized */
}
attr = face->attributes;
assert(attr);
if(face->flags & B3D_FACE_RGB) {
/* Setup RGB interpolation */
majorDv = v2->redValue - v0->redValue;
minorDv = v1->redValue - v0->redValue;
dvdx = face->oneOverArea * ((majorDv * face->minorDy) - (minorDv * face->majorDy));
dvdy = face->oneOverArea * ((minorDv * face->majorDx) - (majorDv * face->minorDx));
attr->value = (float) v0->redValue;
attr->dvdx = (float) dvdx;
attr->dvdy = (float) dvdy;
attr = attr->next;
majorDv = v2->greenValue - v0->greenValue;
minorDv = v1->greenValue - v0->greenValue;
dvdx = face->oneOverArea * ((majorDv * face->minorDy) - (minorDv * face->majorDy));
dvdy = face->oneOverArea * ((minorDv * face->majorDx) - (majorDv * face->minorDx));
attr->value = (float) v0->greenValue;
attr->dvdx = (float) dvdx;
attr->dvdy = (float) dvdy;
attr = attr->next;
majorDv = v2->blueValue - v0->blueValue;
minorDv = v1->blueValue - v0->blueValue;
dvdx = face->oneOverArea * ((majorDv * face->minorDy) - (minorDv * face->majorDy));
dvdy = face->oneOverArea * ((minorDv * face->majorDx) - (majorDv * face->minorDx));
attr->value = (float) v0->blueValue;
attr->dvdx = (float) dvdx;
attr->dvdy = (float) dvdy;
attr = attr->next;
}
if(face->flags & B3D_FACE_ALPHA) {
/* Setup alpha interpolation */
majorDv = v2->alphaValue - v0->alphaValue;
minorDv = v1->alphaValue - v0->alphaValue;
dvdx = face->oneOverArea * ((majorDv * face->minorDy) - (minorDv * face->majorDy));
dvdy = face->oneOverArea * ((minorDv * face->majorDx) - (majorDv * face->minorDx));
attr->value = (float) v0->alphaValue;
attr->dvdx = (float) dvdx;
attr->dvdy = (float) dvdy;
attr = attr->next;
}
if(face->flags & B3D_FACE_STW) {
/* Setup texture coordinate interpolation */
double w0 = v0->rasterPosW;
double w1 = v1->rasterPosW;
double w2 = v2->rasterPosW;
majorDv = w2 - w0;
minorDv = w1 - w0;
dvdx = face->oneOverArea * ((majorDv * face->minorDy) - (minorDv * face->majorDy));
dvdy = face->oneOverArea * ((minorDv * face->majorDx) - (majorDv * face->minorDx));
attr->value = (float) w0;
attr->dvdx = (float) dvdx;
attr->dvdy = (float) dvdy;
attr = attr->next;
baseValue = v0->texCoordS * w0;
majorDv = (v2->texCoordS * w2) - baseValue;
minorDv = (v1->texCoordS * w1) - baseValue;
dvdx = face->oneOverArea * ((majorDv * face->minorDy) - (minorDv * face->majorDy));
dvdy = face->oneOverArea * ((minorDv * face->majorDx) - (majorDv * face->minorDx));
attr->value = (float) baseValue;
attr->dvdx = (float) dvdx;
attr->dvdy = (float) dvdy;
attr = attr->next;
baseValue = v0->texCoordT * w0;
majorDv = (v2->texCoordT * w2) - baseValue;
minorDv = (v1->texCoordT * w1) - baseValue;
dvdx = face->oneOverArea * ((majorDv * face->minorDy) - (minorDv * face->majorDy));
dvdy = face->oneOverArea * ((minorDv * face->majorDx) - (majorDv * face->minorDx));
attr->value = (float) baseValue;
attr->dvdx = (float) dvdx;
attr->dvdy = (float) dvdy;
attr = attr->next;
}
face->flags |= B3D_FACE_INITIALIZED;
return 1;
}
/* b3dInitializeEdge:
Initialize the incremental values of the given edge.
*/
/* INLINE b3dInitializeEdge(edge) */
void b3dInitializeEdge(B3DPrimitiveEdge *edge)
{
assert(edge);
assert(edge->nLines);
edge->xValue = edge->v0->windowPosX;
edge->zValue = edge->v0->rasterPosZ;
if(edge->nLines > 1) {
edge->xIncrement = (edge->v1->windowPosX - edge->v0->windowPosX) / edge->nLines;
edge->zIncrement = (edge->v1->rasterPosZ - edge->v0->rasterPosZ) / (float) edge->nLines;
} else {
edge->xIncrement = (edge->v1->windowPosX - edge->v0->windowPosX);
edge->zIncrement = (edge->v1->rasterPosZ - edge->v0->rasterPosZ);
}
}
/* --INLINE-- */
/*************************************************************/
/*************************************************************/
/*************************************************************/
/* b3dFirstIndexForInserting:
Return the first possible index for inserting an edge with the given x value.
*/
int b3dFirstIndexForInserting(B3DPrimitiveEdgeList *list, int xValue)
{
int low, high, index;
low = 0;
high = list->size-1;
while(low <= high) {
index = (low + high) >> 1;
if(list->data[index]->xValue <= xValue)
low = index+1;
else
high = index-1;
}
index = low;
while(index > 0 && (list->data[index-1]->xValue) == xValue)
index--;
return index;
}
/* b3dAddEdgeBeforeIndex:
Insert the edge to the list before the given index.
*/
/* INLINE b3dAddEdgeBeforeIndex(list, edge, index) */
void b3dAddEdgeBeforeIndex(B3DPrimitiveEdgeList *list, B3DPrimitiveEdge *edge, int index)
{
int i;
if(b3dDebug)
if(list->size == list->max)
b3dAbort("No more space for adding edges");
assert( (list->size == index) || (list->data[index]->xValue >= edge->xValue));
for(i=list->size-1; i >= index; i--)
list->data[i+1] = list->data[i];
list->data[index] = edge;
list->size++;
}
/* --INLINE-- */
/* b3d2AddEdgesBeforeIndex:
Insert the two edge to the list before the given index.
*/
/* INLINE b3dAdd2EdgesBeforeIndex(list, edge1, edge2, index) */
void b3dAdd2EdgesBeforeIndex(B3DPrimitiveEdgeList *list,
B3DPrimitiveEdge *edge1,
B3DPrimitiveEdge *edge2,
int index)
{
int i;
if(b3dDebug)
if(list->size+1 >= list->max)
b3dAbort("No more space for adding edges");
assert( edge1->xValue == edge2->xValue);
assert( (list->size == index) || (list->data[index]->xValue >= edge1->xValue));
for(i=list->size-1; i >= index; i--)
list->data[i+2] = list->data[i];
list->data[index] = edge1;
list->data[index+1] = edge2;
list->size += 2;
}
/* --INLINE-- */
/* b3dAdjustFaceEdges:
Assign left and right edges to the given face.
*/
/* INLINE b3dAdjustFaceEdges(face, edge1, edge2) */
void b3dAdjustFaceEdges(B3DPrimitiveFace *face, B3DPrimitiveEdge *edge1, B3DPrimitiveEdge *edge2)
{
assert(face);
assert(edge1);
assert(edge2);
if(edge1->xValue == edge2->xValue) {
if(edge1->xIncrement <= edge2->xIncrement) {
face->leftEdge = edge1;
face->rightEdge = edge2;
} else {
face->leftEdge = edge2;
face->rightEdge = edge1;
}
} else {
if(edge1->xValue <= edge2->xValue) {
face->leftEdge = edge1;
face->rightEdge = edge2;
} else {
face->leftEdge = edge2;
face->rightEdge = edge1;
}
}
}
/* --INLINE-- */
/* b3dAddLowerEdgeFromFace:
Add a new lower edge from the given face.
NOTE: oldEdge may be NULL!
NOTE: May cause allocation of one edge!
*/
B3DPrimitiveEdge *b3dAddLowerEdgeFromFace(B3DPrimitiveFace *face, B3DPrimitiveEdge *oldEdge)
{
B3DPrimitiveVertex *v0 = face->v0;
B3DPrimitiveVertex *v1 = face->v1;
B3DPrimitiveVertex *v2 = face->v2;
int xValue = v1->windowPosX;
int index;
/* Search the list of added edges to merge the edges from the face */
index = b3dFirstIndexForInserting(addedEdges, xValue);
for(;index<addedEdges->size; index++) {
B3DPrimitiveEdge *edge = addedEdges->data[index];
if(edge->xValue != xValue) break;
if(edge->rightFace) continue;
if((edge->v0 == v1 && edge->v1 == v2) || /* The simple test*/
/* The complex test */
(edge->v0->windowPosX == v1->windowPosX &&
edge->v0->windowPosY == v1->windowPosY &&
edge->v0->rasterPosZ == v1->rasterPosZ &&
edge->v1->windowPosX == v2->windowPosX &&
edge->v1->windowPosY == v2->windowPosY &&
edge->v1->rasterPosZ == v2->rasterPosZ)) {
/* Found the edge */
if(face->leftEdge == oldEdge)
face->leftEdge = edge;
else
face->rightEdge = edge;
edge->rightFace = face;
return edge;
}
}
/* Need to create a new edge.
NOTE: Index already points to the right insertion point.
*/
{
B3DPrimitiveEdge *minorEdge;
int nLines = (v2->windowPosY >> B3D_FixedToIntShift) - (v1->windowPosY >> B3D_FixedToIntShift);
if(!nLines) return NULL; /* Edge is horizontal */
b3dAllocEdge(edgeAlloc, minorEdge);
if(!minorEdge)
return NULL;
minorEdge->v0 = v1;
minorEdge->v1 = v2;
minorEdge->nLines = nLines;
minorEdge->leftFace = face;
minorEdge->rightFace = NULL;
if(face->leftEdge == oldEdge)
face->leftEdge = minorEdge;
else
face->rightEdge = minorEdge;
b3dInitializeEdge(minorEdge);
b3dAddEdgeBeforeIndex(addedEdges, minorEdge, index);
return minorEdge;
}
/* NOT REACHED */
}
/* b3dAddEdgesFromFace:
Add the two new edges from the given primitive face.
NOTE: May cause allocation of two edges (but not three)!
*/
void b3dAddEdgesFromFace(B3DPrimitiveFace *face, int yValue)
{
int needMajor = 1;
int needMinor = 1;
B3DPrimitiveEdge *majorEdge = NULL;
B3DPrimitiveEdge *minorEdge = NULL;
B3DPrimitiveVertex *v0 = face->v0;
B3DPrimitiveVertex *v1 = face->v1;
B3DPrimitiveVertex *v2 = face->v2;
int xValue = v0->windowPosX;
int index;
/* Search the list of added edges to merge the edges from the face */
index = b3dFirstIndexForInserting(addedEdges, xValue);
for(;index<addedEdges->size; index++) {
B3DPrimitiveEdge *edge = addedEdges->data[index];
if(edge->xValue != xValue) break;
if(edge->rightFace) continue;
if(edge->v0 != v0 &&
(edge->v0->windowPosY != v0->windowPosY ||
edge->v0->rasterPosZ != v0->rasterPosZ)) continue;
/* If we come to this point the edge might be usable for merging the face */
if(needMajor && /* Test only if major edge is needed */
(edge->v1 == v2 || /* Simple test */
/* A more complex test */
(edge->v1->windowPosX == v2->windowPosX &&
edge->v1->windowPosY == v2->windowPosY &&
edge->v1->rasterPosZ == v2->rasterPosZ))) {
/* Yepp. That's the new major */
majorEdge = edge;
majorEdge->rightFace = face;
majorEdge->flags |= B3D_EDGE_RIGHT_MAJOR;
if(b3dDoStats) nFaces++;
if(!needMinor) {
b3dAdjustFaceEdges(face, majorEdge, minorEdge);
return; /* done */
}
needMajor = 0;
} else if(needMinor && /* Test only if minor edge is needed */
(edge->v1 == v1 || /* Simple test */
/* A more complex test */
(edge->v1->windowPosX == v1->windowPosX &&
edge->v1->windowPosY == v1->windowPosY &&
edge->v1->rasterPosZ == v1->rasterPosZ))) {
/* Yepp. That's the new minor */
minorEdge = edge;
minorEdge->rightFace = face;
minorEdge->flags |= B3D_EDGE_CONTINUE_RIGHT;
if(!needMajor) {
b3dAdjustFaceEdges(face, majorEdge, minorEdge);
return; /* done */
}
needMinor = 0;
}
}
/* Need to create new edges.
Note: index already points to the right insertion point in addedEdges
*/
if(needMajor) {
int nLines = (v2->windowPosY >> B3D_FixedToIntShift) - (v0->windowPosY >> B3D_FixedToIntShift);
if(!nLines) {
/* The major edge is horizontal. */
b3dFreeFace(faceAlloc, face);
return;
}
b3dAllocEdge(edgeAlloc, majorEdge);
if(b3dDebug)
if(!majorEdge) b3dAbort("Edge allocation failed");
majorEdge->v0 = v0;
majorEdge->v1 = v2;
majorEdge->nLines = nLines;
majorEdge->leftFace = face;
majorEdge->rightFace = NULL;
majorEdge->flags |= B3D_EDGE_LEFT_MAJOR;
b3dInitializeEdge(majorEdge);
if(b3dDoStats) nFaces++;
}
if(needMinor) {
int nLines = (v1->windowPosY >> B3D_FixedToIntShift) - (v0->windowPosY >> B3D_FixedToIntShift);
if(!nLines) {
/* Note: If the (upper) minor edge is horizontal, use the lower one.
Note: The lower edge cannot be horizontal if the major edge isn't
*/
if(needMajor) {
b3dAddEdgeBeforeIndex(addedEdges, majorEdge, index);
}
minorEdge = b3dAddLowerEdgeFromFace(face,NULL);
if(b3dDebug)
if(!minorEdge || minorEdge->nLines == 0)
b3dAbort("minor edge is horizontal");
b3dAdjustFaceEdges(face, majorEdge, minorEdge);
return;
}
b3dAllocEdge(edgeAlloc, minorEdge);
if(b3dDebug)
if(!minorEdge) b3dAbort("Edge allocation failed");
minorEdge->v0 = v0;
minorEdge->v1 = v1;
minorEdge->nLines = nLines;
minorEdge->leftFace = face;
minorEdge->rightFace = NULL;
minorEdge->flags |= B3D_EDGE_CONTINUE_LEFT;
b3dInitializeEdge(minorEdge);
}
/* Add the newly created edges to addedEdges */
if(needMinor && needMajor) {
b3dAdd2EdgesBeforeIndex(addedEdges, majorEdge, minorEdge, index);
} else if(needMajor) {
b3dAddEdgeBeforeIndex(addedEdges, majorEdge, index);
} else {
b3dAddEdgeBeforeIndex(addedEdges, minorEdge, index);
}
b3dAdjustFaceEdges(face, majorEdge, minorEdge);
}
/* b3dRemoveAETEdge:
Remove the given edge from the AET.
NOTE: May cause allocation of two edges!
*/
/* INLINE b3dRemoveAETEdge(aet, edge, yValue, aetPos) */
void b3dRemoveAETEdge(B3DActiveEdgeTable *aet, B3DPrimitiveEdge *edge, int yValue, int aetPos)
{
/* Remove edge and add lower edges if necessary */
int j;
B3DPrimitiveEdge **aetData = aet->data;
assert(aetData[aetPos] == edge);
if(b3dDebug)
if( (edge->v1->windowPosY >> B3D_FixedToIntShift) != yValue )
b3dAbort("Edge exceeds range");
/* Remove the edge and adjust the stuff */
for(j=aetPos+1; j < aet->size; j++) aetData[j-1] = aetData[j];
aet->size--;
/* Add new lower edges */
if(edge->flags & B3D_EDGE_CONTINUE_LEFT) {
b3dAddLowerEdgeFromFace(edge->leftFace, edge);
}
if(edge->flags & B3D_EDGE_CONTINUE_RIGHT) {
b3dAddLowerEdgeFromFace(edge->rightFace, edge);
}
if(edge->flags & B3D_EDGE_LEFT_MAJOR) {
/* Free left face */
b3dFreeAttrib(attrAlloc, edge->leftFace);
b3dFreeFace(faceAlloc, edge->leftFace);
if(b3dDoStats) nFaces--;
}
if(edge->flags & B3D_EDGE_RIGHT_MAJOR) {
/* Free right face */
b3dFreeAttrib(attrAlloc, edge->rightFace);
b3dFreeFace(faceAlloc, edge->rightFace);
if(b3dDoStats) nFaces--;
}
/* And free old edge */
b3dFreeEdge(edgeAlloc, edge);
}
/* --INLINE-- */
/* b3dMergeAETEdgesFrom:
Merge the edges from the given source into the AET.
*/
void b3dMergeAETEdgesFrom(B3DActiveEdgeTable *aet, B3DPrimitiveEdgeList *src)
{
int srcIndex, aetIndex, outIndex, i;
B3DPrimitiveEdge *srcEdge, *aetEdge;
assert(aet);
assert(src);
assert(src->size);
assert(aet->size + src->size <= aet->max);
if(!aet->size) {
for(i=0; i<src->size; i++) aet->data[i] = src->data[i];
aet->size += src->size;
return;
}
/* Merge the input by stepping backwards through the aet and checking each edge */
outIndex = aet->size + src->size - 1;
srcIndex = src->size-1;
aetIndex = aet->size-1;
srcEdge = src->data[srcIndex];
aetEdge = aet->data[aetIndex];
aet->size += src->size;
while(1) {
if(srcEdge->xValue >= aetEdge->xValue) {
/* output srcEdge */
aet->data[outIndex--] = srcEdge;
if(!srcIndex--) return;
srcEdge = src->data[srcIndex];
} else {
/* output aetEdge */
aet->data[outIndex--] = aetEdge;
if(!aetIndex--) {
for(i=0; i <= srcIndex; i++) aet->data[i] = src->data[i];
return;
}
aetEdge = aet->data[aetIndex];
}
}
}
/* INLINE b3dAdvanceAETEdge(edge, aetData, aetStart) */
void b3dAdvanceAETEdge(B3DPrimitiveEdge *edge,
B3DPrimitiveEdge **aetData,
int aetStart)
{
/* Advance to next scan line */
edge->zValue += edge->zIncrement;
edge->xValue += edge->xIncrement;
/* Check if AET sort order is okay */
if(aetStart && aetData[aetStart-1]->xValue > edge->xValue) {
/* Must resort rightEdge */
int xValue = edge->xValue;
int j = aetStart;
/* Move the edge left */
while(j>0 && aetData[j-1]->xValue > xValue) {
aetData[j] = aetData[j-1];
j--;
}
aetData[j] = edge;
}
}
/* --INLINE-- */
/*************************************************************/
/*************************************************************/
/*************************************************************/
#ifdef DEBUG
double zValueAt(B3DPrimitiveFace *face, double xValue, double yValue)
{
return
(face->v0->rasterPosZ +
(((double)xValue - face->v0->rasterPosX) * face->dzdx) +
(((double)yValue - face->v0->rasterPosY) * face->dzdy));
}
#else
#define zValueAt(face, xValue, yValue) \
((face)->v0->rasterPosZ + \
(((double)(xValue) - (face)->v0->rasterPosX) * (face)->dzdx) +\
(((double)(yValue) - (face)->v0->rasterPosY) * (face)->dzdy))
#endif
/*************************************************************/
/*************************************************************/
/*************************************************************/
int b3dComputeIntersection(B3DPrimitiveFace *frontFace,
B3DPrimitiveFace *backFace,
int yValue,
int errorValue)
{
double dx1 = frontFace->rightEdge->xValue - frontFace->leftEdge->xValue;
double dz1 = frontFace->rightEdge->zValue - frontFace->leftEdge->zValue;
double dx2 = backFace->rightEdge->xValue - backFace->leftEdge->xValue;
double dz2 = backFace->rightEdge->zValue - backFace->leftEdge->zValue;
double px = backFace->leftEdge->xValue - frontFace->leftEdge->xValue;
double pz = backFace->leftEdge->zValue - frontFace->leftEdge->zValue;
double det = (dx1 * dz2) - (dx2 * dz1);
if(det == 0.0) return errorValue;
{
double det2 = ((px * dz2) - (pz * dx2)) / det;
return frontFace->leftEdge->xValue + (int)(dx1 * det2);
}
/* not reached */
}
/* b3dCheckIntersectionOfFaces:
Compute the possible intersection of frontFace and backFace.
Store the result in nextIntersection if it is before any other
intersection. Return true if other intersections tests should
be performed, false otherwise.
*/
int b3dCheckIntersectionOfFaces(B3DPrimitiveFace *frontFace,
B3DPrimitiveFace *backFace,
int yValue,
B3DPrimitiveEdge *leftEdge,
B3DPrimitiveEdge *nextIntersection)
{
double frontZ, backZ;
int xValue, rightX;
/* Check if the backFace is completely behind the front face */
if(backFace->minZ >= frontFace->maxZ) return 0; /* abort */
/* Check if front and back face share any edges */
if(frontFace->leftEdge == backFace->leftEdge) return 1; /* proceed */
if(frontFace->rightEdge == backFace->rightEdge) return 1; /* proceed */
/* Check if either front or back face are less than 1 pixel wide */
if( (frontFace->leftEdge->xValue >> B3D_FixedToIntShift) ==
(frontFace->rightEdge->xValue >> B3D_FixedToIntShift)) return 0; /* abort */
if( (backFace->leftEdge->xValue >> B3D_FixedToIntShift) ==
(backFace->rightEdge->xValue >> B3D_FixedToIntShift)) return 1; /* proceed */
/* Choose the right x value of either front or back face,
whichever is less (this is so we sample inside both faces) */
if(frontFace->rightEdge->xValue <= backFace->rightEdge->xValue) {
rightX = frontFace->rightEdge->xValue;
frontZ = frontFace->rightEdge->zValue;
backZ = zValueAt(backFace, rightX * B3D_FixedToFloat, yValue);
} else {
rightX = backFace->rightEdge->xValue;
backZ = backFace->rightEdge->zValue;
frontZ = zValueAt(frontFace, rightX * B3D_FixedToFloat, yValue);
}
if(backZ < frontZ) {
/* possible intersection found */
xValue = b3dComputeIntersection(frontFace, backFace, yValue, leftEdge->xValue);
if(xValue > rightX) xValue = rightX;
/* Ignore intersections at or before the leftEdge's x value. Important. */
if((xValue >> B3D_FixedToIntShift) <= (leftEdge->xValue >> B3D_FixedToIntShift))
xValue = ((leftEdge->xValue >> B3D_FixedToIntShift) + 1) << B3D_IntToFixedShift;
if(xValue < nextIntersection->xValue) {
nextIntersection->xValue = xValue;
nextIntersection->leftFace = frontFace;
nextIntersection->rightFace = backFace;
}
}
return 1;
}
/* b3dAdjustIntersections:
Compute the possible intersections of the current front face
with all active faces. Store the next intersection if any.
*/
/* INLINE b3dAdjustIntersections(fillList, yValue, topEdge, nextIntersection) */
void b3dAdjustIntersections(B3DFillList *fillList,
int yValue,
B3DPrimitiveEdge *topEdge,
B3DPrimitiveEdge *nextIntersection)
{
B3DPrimitiveFace *frontFace = fillList->firstFace;
if(frontFace) {
B3DPrimitiveFace *backFace = frontFace->nextFace;
int proceed = 1;
while(backFace && proceed) {
proceed = b3dCheckIntersectionOfFaces(frontFace, backFace, yValue, topEdge, nextIntersection);
backFace = backFace->nextFace;
}
}
}
/* --INLINE-- */
/*************************************************************/
/*************************************************************/
/*************************************************************/
void b3dValidateFillList(B3DFillList *list)
{
B3DPrimitiveFace *firstFace = list->firstFace;
B3DPrimitiveFace *lastFace = list->lastFace;
B3DPrimitiveFace *face;
if(!firstFace && !lastFace) return;
if(firstFace->prevFace)
b3dAbort("Bad fill list");
if(lastFace->nextFace)
b3dAbort("Bad fill list");
face = firstFace;
while(face != lastFace)
face = face->nextFace;
/* Validate sort order */
if(firstFace == lastFace)
return; /* 0 or 1 element */
face = firstFace->nextFace;
while(face->nextFace) {
if(face->minZ > face->nextFace->minZ)
b3dAbort("Fill list sorting problem");
face = face->nextFace;
}
}
/* INLINE b3dAddFirstFill(fillList, aFace) */
void b3dAddFirstFill(B3DFillList *fillList, B3DPrimitiveFace *aFace)
{
B3DPrimitiveFace *firstFace = fillList->firstFace;
if(firstFace)
firstFace->prevFace = aFace;
else
fillList->lastFace = aFace;
aFace->nextFace = firstFace;
aFace->prevFace = NULL;
fillList->firstFace = aFace;
if(b3dDebug) b3dValidateFillList(fillList);
}
/* --INLINE-- */
/* INLINE b3dAddLastFill(fillList, aFace) */
void b3dAddLastFill(B3DFillList *fillList, B3DPrimitiveFace *aFace)
{
B3DPrimitiveFace *lastFace = fillList->lastFace;
if(lastFace)
lastFace->nextFace = aFace;
else
fillList->firstFace = aFace;
aFace->prevFace = lastFace;
aFace->nextFace = NULL;
fillList->lastFace = aFace;
if(b3dDebug) b3dValidateFillList(fillList);
}
/* --INLINE-- */
/* INLINE b3dRemoveFill(fillList, aFace) */
void b3dRemoveFill(B3DFillList *fillList, B3DPrimitiveFace *aFace)
{
if(b3dDebug) b3dValidateFillList(fillList);
if(aFace->prevFace)
aFace->prevFace->nextFace = aFace->nextFace;
else
fillList->firstFace = aFace->nextFace;
if(aFace->nextFace)
aFace->nextFace->prevFace = aFace->prevFace;
else
fillList->lastFace = aFace->prevFace;
}
/* --INLINE-- */
/* INLINE b3dInsertBeforeFill(fillList, aFace, otherFace) */
void b3dInsertBeforeFill(B3DFillList *fillList, B3DPrimitiveFace *aFace, B3DPrimitiveFace *otherFace)
{
assert(otherFace != fillList->firstFace);
aFace->nextFace = otherFace;
aFace->prevFace = otherFace->prevFace;
aFace->prevFace->nextFace = aFace;
otherFace->prevFace = aFace;
if(b3dDebug) b3dValidateFillList(fillList);
}
/* --INLINE-- */
/* INLINE b3dAddFrontFill(fillList, aFace) */
void b3dAddFrontFill(B3DFillList *fillList, B3DPrimitiveFace *aFace)
{
B3DPrimitiveFace *firstFace = fillList->firstFace;
if(firstFace != fillList->lastFace) {
/* Meaning that we must find the new position for the old front face */
B3DPrimitiveFace *backFace = firstFace->nextFace;
float minZ = firstFace->minZ;
while(backFace && backFace->minZ < minZ)
backFace = backFace->nextFace;
/* Insert firstFace before backFace */
if(firstFace->nextFace != backFace) {
B3DPrimitiveFace *tempFace = firstFace;
b3dRemoveFill(fillList, tempFace);
if(backFace) {
b3dInsertBeforeFill(fillList, tempFace, backFace);
} else {
b3dAddLastFill(fillList, tempFace);
}
}
}
b3dAddFirstFill(fillList, aFace);
if(b3dDebug) b3dValidateFillList(fillList);
}
/* --INLINE-- */
/* INLINE b3dAddBackFill(fillList, aFace) */
void b3dAddBackFill(B3DFillList *fillList, B3DPrimitiveFace *aFace)
{
B3DPrimitiveFace *firstFace = fillList->firstFace;
B3DPrimitiveFace *lastFace = fillList->lastFace;
B3DPrimitiveFace *face;
float minZ = aFace->minZ;
assert(firstFace);
if(firstFace == lastFace || minZ >= lastFace->minZ) {
b3dAddLastFill(fillList, aFace);
} else {
/* Try an estimation on how to search */
if(minZ <= (firstFace->minZ + lastFace->minZ) * 0.5) {
/* search front to back */
face = firstFace->nextFace;
while(face->minZ < minZ) face = face->nextFace;
} else {
/* search back to front */
face = lastFace->prevFace; /* already checked if lastFace->minZ <= minZ */
while(face->minZ > minZ) face = face->prevFace;
face = face->nextFace;
}
b3dInsertBeforeFill(fillList, aFace, face);
}