-
Notifications
You must be signed in to change notification settings - Fork 108
/
Squeak3D.c
4254 lines (3884 loc) · 116 KB
/
Squeak3D.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
/* Automatically generated by
VMPluginCodeGenerator VMMaker.oscog-eem.2484 uuid: 63db7375-cf0b-4a19-86d9-9e4a4da68117
from
B3DEnginePlugin Balloon3D-Plugins-eem.15 uuid: d694149b-363b-4754-aa4a-7ef0cb2c5eab
*/
static char __buildInfo[] = "B3DEnginePlugin Balloon3D-Plugins-eem.15 uuid: d694149b-363b-4754-aa4a-7ef0cb2c5eab " __DATE__ ;
#include "config.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/* Default EXPORT macro that does nothing (see comment in sq.h): */
#define EXPORT(returnType) returnType
/* Do not include the entire sq.h file but just those parts needed. */
#include "sqConfig.h" /* Configuration options */
#include "sqVirtualMachine.h" /* The virtual machine proxy definition */
#include "sqPlatformSpecific.h" /* Platform specific definitions */
#define true 1
#define false 0
#define null 0 /* using 'null' because nil is predefined in Think C */
#ifdef SQUEAK_BUILTIN_PLUGIN
# undef EXPORT
# define EXPORT(returnType) static returnType
#endif
#include "b3d.h"
#include "sqMemoryAccess.h"
/*** Constants ***/
#define AmbientPart 0
#define DiffusePart 4
#define EmissionAlpha 15
#define EmissionBlue 14
#define EmissionGreen 13
#define EmissionRed 12
#define FlagAmbientPart 0x100
#define FlagAttenuated 4
#define FlagDiffusePart 0x200
#define FlagDirectional 2
#define FlagHasSpot 8
#define FlagM44Identity 1
#define FlagM44NoPerspective 2
#define FlagM44NoTranslation 4
#define FlagPositional 1
#define FlagSpecularPart 0x400
#define InAllMask 0x555
#define InBackBit 0x400
#define InBottomBit 64
#define InFrontBit 0x100
#define InLeftBit 1
#define InRightBit 4
#define InTopBit 16
#define MaterialShininess 16
#define MaterialSize 17
#define OutAllMask 0xAAA
#define OutBackBit 0x800
#define OutBottomBit 128
#define OutFrontBit 0x200
#define OutLeftBit 2
#define OutRightBit 8
#define OutTopBit 32
#define PrimLightAttenuationConstant 18
#define PrimLightAttenuationLinear 19
#define PrimLightAttenuationSquared 20
#define PrimLightDirection 15
#define PrimLightDirectionX 15
#define PrimLightDirectionY 16
#define PrimLightDirectionZ 17
#define PrimLightFlags 21
#define PrimLightPositionX 12
#define PrimLightPositionY 13
#define PrimLightPositionZ 14
#define PrimLightSize 32
#define PrimTypeMax 6
#define PrimVertexSize 16
#define PrimVtxClipFlags 13
#define PrimVtxColor32 12
#define PrimVtxNormal 3
#define PrimVtxNormalX 3
#define PrimVtxNormalY 4
#define PrimVtxNormalZ 5
#define PrimVtxPosition 0
#define PrimVtxPositionX 0
#define PrimVtxPositionY 1
#define PrimVtxPositionZ 2
#define PrimVtxRasterPosW 11
#define PrimVtxRasterPosX 8
#define PrimVtxRasterPosY 9
#define PrimVtxRasterPosZ 10
#define PrimVtxTexCoords 6
#define PrimVtxTexCoordU 6
#define PrimVtxTexCoordV 7
#define SpecularPart 8
#define SpotLightDeltaCos 24
#define SpotLightExponent 25
#define SpotLightMinCos 22
#define VBTrackAmbient 1
#define VBTrackDiffuse 2
#define VBTrackEmission 8
#define VBTrackSpecular 4
#define VBTwoSidedLighting 64
#define VBUseLocalViewer 128
#define VBVtxHasNormals 16
/*** Function Prototypes ***/
static sqInt addPartfromtrackFlagscale(float *lightPart, float *materialPart, sqInt vbTrackFlag, double scale);
static sqInt analyzeMatrix3x3Length(float *m);
static sqInt analyzeMatrix(float *m);
EXPORT(sqInt) b3dClipPolygon(void);
EXPORT(sqInt) b3dComputeMinIndexZ(void);
EXPORT(sqInt) b3dComputeMinZ(void);
EXPORT(sqInt) b3dDetermineClipFlags(void);
EXPORT(sqInt) b3dInitializeRasterizerState(void);
EXPORT(sqInt) b3dInitPrimitiveObject(void);
EXPORT(sqInt) b3dInplaceHouseHolderInvert(void);
EXPORT(sqInt) b3dLoadIndexArray(void);
EXPORT(sqInt) b3dLoadVertexBuffer(void);
EXPORT(sqInt) b3dMapVertexBuffer(void);
EXPORT(sqInt) b3dOrthoNormInverseMatrix(void);
EXPORT(sqInt) b3dPrimitiveNextClippedTriangle(void);
EXPORT(sqInt) b3dPrimitiveObjectSize(void);
EXPORT(sqInt) b3dPrimitiveTextureSize(void);
EXPORT(sqInt) b3dRasterizerVersion(void);
EXPORT(sqInt) b3dShaderVersion(void);
EXPORT(sqInt) b3dShadeVertexBuffer(void);
EXPORT(sqInt) b3dStartRasterizer(void);
EXPORT(sqInt) b3dTransformDirection(void);
EXPORT(sqInt) b3dTransformerVersion(void);
EXPORT(sqInt) b3dTransformMatrixWithInto(void);
EXPORT(sqInt) b3dTransformPoint(void);
EXPORT(sqInt) b3dTransformPrimitiveNormal(void);
EXPORT(sqInt) b3dTransformPrimitivePosition(void);
EXPORT(sqInt) b3dTransformPrimitiveRasterPosition(void);
EXPORT(sqInt) b3dTransformVertexBuffer(void);
EXPORT(sqInt) b3dTransposeMatrix(void);
static double backClipValueFromto(sqInt last, sqInt next);
static double bottomClipValueFromto(sqInt last, sqInt next);
static sqInt clipPolygonBackFromtocount(int *buf1, int *buf2, sqInt n);
static sqInt clipPolygonBottomFromtocount(int *buf1, int *buf2, sqInt n);
static sqInt clipPolygonFrontFromtocount(int *buf1, int *buf2, sqInt n);
static sqInt clipPolygonLeftFromtocount(int *buf1, int *buf2, sqInt n);
static sqInt clipPolygonRightFromtocount(int *buf1, int *buf2, sqInt n);
static sqInt clipPolygonTopFromtocount(int *buf1, int *buf2, sqInt n);
static sqInt clipPolygoncountwithmask(int *vtxArray, sqInt vtxCount, int *tempVtxArray, sqInt outMask);
static sqInt computeAttenuation(void);
static sqInt computeDirection(void);
static sqInt computeSpecularDirection(void);
static double computeSpotFactor(void);
static sqInt determineClipFlagscount(void *vtxArray, sqInt count);
static double dotProductOfFloatwithDouble(float *v1, double *v2);
static void* fetchLightSourceofObject(sqInt index, sqInt anArray);
static double frontClipValueFromto(sqInt last, sqInt next);
EXPORT(const char*) getModuleName(void);
EXPORT(sqInt) initialiseModule(void);
static sqInt interpolateFromtoatinto(float *last, float *next, double t, float *out);
static double inverseLengthOfDouble(double *aVector);
static double inverseLengthOfFloat(float *aVector);
static double leftClipValueFromto(sqInt last, sqInt next);
static sqInt loadObjectsFrom(sqInt stackIndex);
static sqInt loadPrimitiveLightSource(void);
static sqInt loadPrimitiveVertex(void);
static sqInt loadRasterizerState(sqInt stackIndex);
static sqInt loadTexturesFrom(sqInt stackIndex);
static sqInt loadTextureinto(sqInt textureOop, B3DTexture *destPtr);
static sqInt loadViewportFrom(sqInt stackIndex);
static sqInt mapVBofSizeinto(void *vtxArray, sqInt vtxCount, sqInt boxArray);
EXPORT(sqInt) moduleUnloaded(char *aModuleName);
EXPORT(sqInt) primitiveSetBitBltPlugin(void);
static sqInt processIndexedIDXofSizeidxArrayidxSize(float *vtxArray, sqInt vtxSize, int *idxArray, sqInt idxSize);
static double processIndexedofSizeidxArrayidxSize(float *vtxArray, sqInt vtxSize, int *idxArray, sqInt idxSize);
static sqInt processNonIndexedIDXofSize(float *vtxArray, sqInt vtxSize);
static double processNonIndexedofSize(float *vtxArray, sqInt vtxSize);
static double rightClipValueFromto(sqInt last, sqInt next);
EXPORT(sqInt) setInterpreter(struct VirtualMachine *anInterpreter);
static sqInt shadeVertex(void);
static sqInt stackLightArrayValue(sqInt stackIndex);
static void * stackMaterialValue(sqInt stackIndex);
static void* stackMatrix(sqInt index);
static void* stackPrimitiveIndexArrayofSizevalidateforVertexSize(sqInt stackIndex, sqInt nItems, sqInt aBool, sqInt maxIndex);
static void* stackPrimitiveVertexArrayofSize(sqInt index, sqInt nItems);
static void* stackPrimitiveVertex(sqInt index);
static sqInt storeObjectsInto(sqInt stackIndex);
static sqInt storePrimitiveVertex(void);
static double topClipValueFromto(sqInt last, sqInt next);
static sqInt transformMatrixwithinto(float *src, float *arg, float *dst);
static sqInt transformPrimitiveNormalbyrescale(float *pVertex, float *matrix, sqInt rescale);
static sqInt transformPrimitivePositionFasterby(float *pVertex, float *matrix);
static sqInt transformPrimitivePositionFastby(float *pVertex, float *matrix);
static sqInt transformPrimitivePositionby(float *pVertex, float *matrix);
static sqInt transformPrimitiveRasterPositionby(float *pVertex, float *matrix);
static sqInt transformVBcountbyandflags(float *vtxArray, sqInt vtxCount, float *modelViewMatrix, float *projectionMatrix, sqInt flags);
static void* vbLoadArraysize(sqInt oop, sqInt count);
/*** Variables ***/
static char bbPluginName[256] = "BitBltPlugin";
static sqInt copyBitsFn;
#if !defined(SQUEAK_BUILTIN_PLUGIN)
static sqInt (*booleanValueOf)(sqInt obj);
static sqInt (*byteSizeOf)(sqInt oop);
static sqInt (*classArray)(void);
static sqInt (*classBitmap)(void);
static sqInt (*classPoint)(void);
static sqInt (*clone)(sqInt oop);
static sqInt (*failed)(void);
static sqInt (*fetchClassOf)(sqInt oop);
static sqInt (*fetchIntegerofObject)(sqInt fieldIndex, sqInt objectPointer);
static sqInt (*fetchPointerofObject)(sqInt index, sqInt oop);
static void * (*firstIndexableField)(sqInt oop);
static sqInt (*floatObjectOf)(double aFloat);
static void * (*ioLoadFunctionFrom)(char *functionName, char *moduleName);
static sqInt (*isBytes)(sqInt oop);
static sqInt (*isPointers)(sqInt oop);
static sqInt (*isWords)(sqInt oop);
static sqInt (*methodArgumentCount)(void);
static sqInt (*nilObject)(void);
static sqInt (*pop)(sqInt nItems);
static sqInt (*popRemappableOop)(void);
static sqInt (*primitiveFail)(void);
static sqInt (*push)(sqInt object);
static sqInt (*pushBool)(sqInt trueOrFalse);
static sqInt (*pushFloat)(double f);
static sqInt (*pushInteger)(sqInt integerValue);
static sqInt (*pushRemappableOop)(sqInt oop);
static sqInt (*slotSizeOf)(sqInt oop);
static sqInt (*stackIntegerValue)(sqInt offset);
static sqInt (*stackObjectValue)(sqInt offset);
static sqInt (*stackValue)(sqInt offset);
static sqInt (*storePointerofObjectwithValue)(sqInt index, sqInt oop, sqInt valuePointer);
#else /* !defined(SQUEAK_BUILTIN_PLUGIN) */
extern sqInt booleanValueOf(sqInt obj);
extern sqInt byteSizeOf(sqInt oop);
extern sqInt classArray(void);
extern sqInt classBitmap(void);
extern sqInt classPoint(void);
extern sqInt clone(sqInt oop);
extern sqInt failed(void);
extern sqInt fetchClassOf(sqInt oop);
extern sqInt fetchIntegerofObject(sqInt fieldIndex, sqInt objectPointer);
extern sqInt fetchPointerofObject(sqInt index, sqInt oop);
extern void * firstIndexableField(sqInt oop);
extern sqInt floatObjectOf(double aFloat);
extern void * ioLoadFunctionFrom(char *functionName, char *moduleName);
extern sqInt isBytes(sqInt oop);
extern sqInt isPointers(sqInt oop);
extern sqInt isWords(sqInt oop);
extern sqInt methodArgumentCount(void);
extern sqInt nilObject(void);
extern sqInt pop(sqInt nItems);
extern sqInt popRemappableOop(void);
extern sqInt primitiveFail(void);
extern sqInt push(sqInt object);
extern sqInt pushBool(sqInt trueOrFalse);
extern sqInt pushFloat(double f);
extern sqInt pushInteger(sqInt integerValue);
extern sqInt pushRemappableOop(sqInt oop);
extern sqInt slotSizeOf(sqInt oop);
extern sqInt stackIntegerValue(sqInt offset);
extern sqInt stackObjectValue(sqInt offset);
extern sqInt stackValue(sqInt offset);
extern sqInt storePointerofObjectwithValue(sqInt index, sqInt oop, sqInt valuePointer);
extern
#endif
struct VirtualMachine* interpreterProxy;
static double l2vDirection[3];
static double l2vDistance;
static double l2vSpecDir[3];
static sqInt lightFlags;
static double lightScale;
static float* litVertex;
static sqInt loadBBFn;
static const char *moduleName =
#ifdef SQUEAK_BUILTIN_PLUGIN
"Squeak3D Balloon3D-Plugins-eem.15 (i)"
#else
"Squeak3D Balloon3D-Plugins-eem.15 (e)"
#endif
;
static float* primLight;
static float* primMaterial;
static B3DRasterizerState state;
static sqInt vbFlags;
static B3DPrimitiveViewport viewport;
static double vtxInColor[4];
static double vtxOutColor[4];
/* Add the given light part to the output color, scaled by the given scale
factor. If the given flag is set in vbFlags then load the part from the
primitive vertex
*/
/* B3DShaderPlugin>>#addPart:from:trackFlag:scale: */
static sqInt
addPartfromtrackFlagscale(float *lightPart, float *materialPart, sqInt vbTrackFlag, double scale)
{
double aPart;
double bPart;
double gPart;
double rPart;
if (vbFlags & vbTrackFlag) {
rPart = ((vtxInColor[0]) * (lightPart[0])) * scale;
gPart = ((vtxInColor[1]) * (lightPart[1])) * scale;
bPart = ((vtxInColor[2]) * (lightPart[2])) * scale;
aPart = ((vtxInColor[3]) * (lightPart[3])) * scale;
}
else {
/* Note: This should be pre-computed. */
rPart = ((materialPart[0]) * (lightPart[0])) * scale;
gPart = ((materialPart[1]) * (lightPart[1])) * scale;
bPart = ((materialPart[2]) * (lightPart[2])) * scale;
aPart = ((materialPart[3]) * (lightPart[3])) * scale;
}
vtxOutColor[0] = ((vtxOutColor[0]) + rPart);
vtxOutColor[1] = ((vtxOutColor[1]) + gPart);
vtxOutColor[2] = ((vtxOutColor[2]) + bPart);
vtxOutColor[3] = ((vtxOutColor[3]) + aPart);
return 0;
}
/* Check if the matrix scales normals to non-unit length. */
/* B3DTransformerPlugin>>#analyzeMatrix3x3Length: */
static sqInt
analyzeMatrix3x3Length(float *m)
{
double det;
det = (((((((m[0]) * (m[5])) * (m[10])) - (((m[2]) * (m[5])) * (m[8]))) + (((m[4]) * (m[9])) * (m[2]))) - (((m[6]) * (m[9])) * (m[0]))) + (((m[8]) * (m[1])) * (m[6]))) - (((m[10]) * (m[1])) * (m[4]));
return (det < 0.99)
|| (det > 1.01);
}
/* Analyze the matrix and return the appropriate flags */
/* B3DTransformerPlugin>>#analyzeMatrix: */
static sqInt
analyzeMatrix(float *m)
{
sqInt flags;
/* Check the perspective */
flags = 0;
if (((m[12]) == 0.0)
&& (((m[13]) == 0.0)
&& (((m[14]) == 0.0)
&& ((m[15]) == 1.0)))) {
/* Check translation */
flags = flags | FlagM44NoPerspective;
if (((m[3]) == 0.0)
&& (((m[7]) == 0.0)
&& ((m[11]) == 0.0))) {
/* Check for identity */
flags = flags | FlagM44NoTranslation;
if (((m[0]) == 1.0)
&& (((m[5]) == 1.0)
&& (((m[10]) == 1.0)
&& (((m[1]) == 0.0)
&& (((m[2]) == 0.0)
&& (((m[4]) == 0.0)
&& (((m[6]) == 0.0)
&& (((m[8]) == 0.0)
&& ((m[9]) == 0.0))))))))) {
flags = flags | FlagM44Identity;
}
}
}
return flags;
}
/* Primitive. Clip the polygon given in the vertexArray using the temporary
vertex array which is assumed to have sufficient size.
*/
/* B3DClipperPlugin>>#b3dClipPolygon */
EXPORT(sqInt)
b3dClipPolygon(void)
{
sqInt count;
sqInt count1;
sqInt outMask;
int *tempVtxArray;
int *vtxArray;
sqInt vtxCount;
if (!((methodArgumentCount()) == 4)) {
return primitiveFail();
}
outMask = stackIntegerValue(0);
vtxCount = stackIntegerValue(2);
vtxArray = stackPrimitiveVertexArrayofSize(3, vtxCount + 4);
tempVtxArray = stackPrimitiveVertexArrayofSize(1, vtxCount + 4);
if ((vtxArray == null)
|| ((tempVtxArray == null)
|| (failed()))) {
return primitiveFail();
}
vtxArray -= PrimVertexSize;
tempVtxArray -= PrimVertexSize;
/* begin clipPolygon:count:with:mask: */
if (outMask == OutLeftBit) {
count = clipPolygonLeftFromtocount(tempVtxArray, vtxArray, vtxCount);
goto l1;
}
if (outMask == OutRightBit) {
count = clipPolygonRightFromtocount(tempVtxArray, vtxArray, vtxCount);
goto l1;
}
if (outMask == OutTopBit) {
count = clipPolygonTopFromtocount(tempVtxArray, vtxArray, vtxCount);
goto l1;
}
if (outMask == OutBottomBit) {
count = clipPolygonBottomFromtocount(tempVtxArray, vtxArray, vtxCount);
goto l1;
}
if (outMask == OutFrontBit) {
count = clipPolygonFrontFromtocount(tempVtxArray, vtxArray, vtxCount);
goto l1;
}
if (outMask == OutBackBit) {
count = clipPolygonBackFromtocount(tempVtxArray, vtxArray, vtxCount);
goto l1;
}
count1 = vtxCount;
count1 = clipPolygonLeftFromtocount(vtxArray, tempVtxArray, count1);
if (count1 == 0) {
count = 0;
goto l1;
}
count1 = clipPolygonRightFromtocount(tempVtxArray, vtxArray, count1);
if (count1 == 0) {
count = 0;
goto l1;
}
count1 = clipPolygonTopFromtocount(vtxArray, tempVtxArray, count1);
if (count1 == 0) {
count = 0;
goto l1;
}
count1 = clipPolygonBottomFromtocount(tempVtxArray, vtxArray, count1);
if (count1 == 0) {
count = 0;
goto l1;
}
count1 = clipPolygonFrontFromtocount(vtxArray, tempVtxArray, count1);
if (count1 == 0) {
count = 0;
goto l1;
}
count1 = clipPolygonBackFromtocount(tempVtxArray, vtxArray, count1);
count = count1;
l1: /* end clipPolygon:count:with:mask: */;
pop(5);
pushInteger(count);
return 0;
}
/* Primitive. Compute and return the index for the minimal z value of all
objects in the vertex buffer.
*/
/* B3DPickerPlugin>>#b3dComputeMinIndexZ */
EXPORT(sqInt)
b3dComputeMinIndexZ(void)
{
sqInt i;
sqInt i1;
int *idxArray;
sqInt idxSize;
int index;
sqInt minIndex;
sqInt minIndex1;
sqInt minIndex2;
double minZ;
double minZ1;
sqInt primType;
float *vtxArray;
float *vtxPtr;
float *vtxPtr1;
sqInt vtxSize;
double wValue;
double wValue1;
double zValue;
double zValue1;
if (!((methodArgumentCount()) == 5)) {
return primitiveFail();
}
idxSize = stackIntegerValue(0);
vtxSize = stackIntegerValue(2);
primType = stackIntegerValue(4);
if (failed()) {
return null;
}
vtxArray = stackPrimitiveVertexArrayofSize(3, vtxSize);
idxArray = stackPrimitiveIndexArrayofSizevalidateforVertexSize(1, idxSize, 1, vtxSize);
if ((vtxArray == null)
|| ((idxArray == null)
|| (failed()))) {
return primitiveFail();
}
if ((primType < 1)
|| (primType > 6)) {
return primitiveFail();
}
if (primType <= 3) {
/* begin processNonIndexedIDX:ofSize: */
minZ = 10.0;
minIndex1 = 0;
vtxPtr = vtxArray;
for (i = 1; i <= vtxSize; i += 1) {
zValue = vtxPtr[PrimVtxRasterPosZ];
wValue = vtxPtr[PrimVtxRasterPosW];
if (!(wValue == 0.0)) {
zValue = zValue / wValue;
}
if ((minIndex1 == 0)
|| (zValue < minZ)) {
minIndex1 = i;
minZ = zValue;
}
}
minIndex = minIndex1;
}
else {
/* begin processIndexedIDX:ofSize:idxArray:idxSize: */
minZ1 = 10.0;
minIndex2 = 0;
for (i1 = 1; i1 <= idxSize; i1 += 1) {
index = idxArray[i1];
if (index > 0) {
vtxPtr1 = vtxArray + ((index - 1) * PrimVertexSize);
zValue1 = vtxPtr1[PrimVtxRasterPosZ];
wValue1 = vtxPtr1[PrimVtxRasterPosW];
if (!(wValue1 == 0.0)) {
zValue1 = zValue1 / wValue1;
}
if ((minIndex2 == 0)
|| (zValue1 < minZ1)) {
minIndex2 = i1;
minZ1 = zValue1;
}
}
}
minIndex = minIndex2;
}
if (!(failed())) {
pop(6);
pushInteger(minIndex);
}
return 0;
}
/* Primitive. Compute and return the minimal z value of all objects in the
vertex buffer.
*/
/* B3DPickerPlugin>>#b3dComputeMinZ */
EXPORT(sqInt)
b3dComputeMinZ(void)
{
sqInt i;
sqInt i1;
int *idxArray;
sqInt idxSize;
int index;
double minZ;
double minZ1;
double minZ2;
sqInt primType;
float *vtxArray;
float *vtxPtr;
float *vtxPtr1;
sqInt vtxSize;
double wValue;
double wValue1;
double zValue;
double zValue1;
if (!((methodArgumentCount()) == 5)) {
return primitiveFail();
}
idxSize = stackIntegerValue(0);
vtxSize = stackIntegerValue(2);
primType = stackIntegerValue(4);
if (failed()) {
return null;
}
vtxArray = stackPrimitiveVertexArrayofSize(3, vtxSize);
idxArray = stackPrimitiveIndexArrayofSizevalidateforVertexSize(1, idxSize, 1, vtxSize);
if ((vtxArray == null)
|| ((idxArray == null)
|| (failed()))) {
return primitiveFail();
}
if ((primType < 1)
|| (primType > 6)) {
return primitiveFail();
}
if (primType <= 3) {
/* begin processNonIndexed:ofSize: */
minZ1 = 10.0;
vtxPtr = vtxArray;
for (i = 1; i <= vtxSize; i += 1) {
zValue = vtxPtr[PrimVtxRasterPosZ];
wValue = vtxPtr[PrimVtxRasterPosW];
if (!(wValue == 0.0)) {
zValue = zValue / wValue;
}
if (zValue < minZ1) {
minZ1 = zValue;
}
}
minZ = minZ1;
}
else {
/* begin processIndexed:ofSize:idxArray:idxSize: */
minZ2 = 10.0;
for (i1 = 1; i1 <= idxSize; i1 += 1) {
index = idxArray[i1];
if (index > 0) {
vtxPtr1 = vtxArray + ((index - 1) * PrimVertexSize);
zValue1 = vtxPtr1[PrimVtxRasterPosZ];
wValue1 = vtxPtr1[PrimVtxRasterPosW];
if (!(wValue1 == 0.0)) {
zValue1 = zValue1 / wValue1;
}
if (zValue1 < minZ2) {
minZ2 = zValue1;
}
}
}
minZ = minZ2;
}
if (!(failed())) {
pop(6);
pushFloat(minZ);
}
return 0;
}
/* Primitive. Determine the clipping flags for all vertices. */
/* B3DClipperPlugin>>#b3dDetermineClipFlags */
EXPORT(sqInt)
b3dDetermineClipFlags(void)
{
sqInt flags;
sqInt fullMask;
sqInt i;
sqInt result;
void *vtxArray;
sqInt vtxCount;
float *vtxPtr;
double w;
double w2;
double x;
double y;
double z;
if (!((methodArgumentCount()) == 2)) {
return primitiveFail();
}
vtxCount = stackIntegerValue(0);
if (failed()) {
return null;
}
vtxArray = stackPrimitiveVertexArrayofSize(1, vtxCount);
if ((vtxArray == null)
|| (failed())) {
return primitiveFail();
}
/* begin determineClipFlags:count: */
vtxPtr = ((float *) vtxArray);
fullMask = InAllMask + OutAllMask;
for (i = 1; i <= vtxCount; i += 1) {
w = vtxPtr[PrimVtxRasterPosW];
w2 = 0.0 - w;
flags = 0;
x = vtxPtr[PrimVtxRasterPosX];
if (x >= w2) {
flags = flags | InLeftBit;
}
else {
flags = flags | OutLeftBit;
}
if (x <= w) {
flags = flags | InRightBit;
}
else {
flags = flags | OutRightBit;
}
y = vtxPtr[PrimVtxRasterPosY];
if (y >= w2) {
flags = flags | InBottomBit;
}
else {
flags = flags | OutBottomBit;
}
if (y <= w) {
flags = flags | InTopBit;
}
else {
flags = flags | OutTopBit;
}
z = vtxPtr[PrimVtxRasterPosZ];
if (z >= w2) {
flags = flags | InFrontBit;
}
else {
flags = flags | OutFrontBit;
}
if (z <= w) {
flags = flags | InBackBit;
}
else {
flags = flags | OutBackBit;
}
fullMask = fullMask & flags;
(((int *) vtxPtr))[PrimVtxClipFlags] = flags;
vtxPtr += PrimVertexSize;
}
result = fullMask;
if (!(failed())) {
pop(3);
pushInteger(result);
}
return 0;
}
/* Primitive. Initialize the primitive level objects of the given rasterizer. */
/* Check argument count */
/* B3DRasterizerPlugin>>#b3dInitializeRasterizerState */
EXPORT(sqInt)
b3dInitializeRasterizerState(void)
{
sqInt objOop;
sqInt stateOop;
if (!((methodArgumentCount()) == 0)) {
return primitiveFail();
}
stateOop = stackValue(0);
if (!((isPointers(stateOop))
&& ((slotSizeOf(stateOop)) >= 7))) {
return primitiveFail();
}
objOop = fetchPointerofObject(0, stateOop);
if (!((isWords(objOop))
&& ((b3dInitializeFaceAllocator(firstIndexableField(objOop), byteSizeOf(objOop))) == B3D_NO_ERROR))) {
return primitiveFail();
}
objOop = fetchPointerofObject(1, stateOop);
if (!((isWords(objOop))
&& ((b3dInitializeEdgeAllocator(firstIndexableField(objOop), byteSizeOf(objOop))) == B3D_NO_ERROR))) {
return primitiveFail();
}
objOop = fetchPointerofObject(2, stateOop);
if (!((isWords(objOop))
&& ((b3dInitializeAttrAllocator(firstIndexableField(objOop), byteSizeOf(objOop))) == B3D_NO_ERROR))) {
return primitiveFail();
}
objOop = fetchPointerofObject(3, stateOop);
if (!((isWords(objOop))
&& ((b3dInitializeAET(firstIndexableField(objOop), byteSizeOf(objOop))) == B3D_NO_ERROR))) {
return primitiveFail();
}
objOop = fetchPointerofObject(4, stateOop);
if (!((isWords(objOop))
&& ((b3dInitializeEdgeList(firstIndexableField(objOop), byteSizeOf(objOop))) == B3D_NO_ERROR))) {
return primitiveFail();
}
objOop = fetchPointerofObject(5, stateOop);
if (!((isWords(objOop))
&& ((b3dInitializeFillList(firstIndexableField(objOop), byteSizeOf(objOop))) == B3D_NO_ERROR))) {
return primitiveFail();
}
return 0;
}
/* B3DRasterizerPlugin>>#b3dInitPrimitiveObject */
EXPORT(sqInt)
b3dInitPrimitiveObject(void)
{
int *idxArray;
sqInt idxSize;
sqInt primitive;
void *primObj;
sqInt primOop;
sqInt primSize;
sqInt textureIndex;
int *vtxArray;
sqInt vtxSize;
/* Check argument count */
if (!((methodArgumentCount()) == 8)) {
return primitiveFail();
}
textureIndex = stackIntegerValue(0);
if (failed()) {
return null;
}
loadViewportFrom(1);
if (failed()) {
return null;
}
vtxSize = stackIntegerValue(4);
vtxArray = stackPrimitiveVertexArrayofSize(5, vtxSize);
if (vtxArray == null) {
return primitiveFail();
}
idxSize = stackIntegerValue(2);
idxArray = stackPrimitiveIndexArrayofSizevalidateforVertexSize(3, idxSize, 1, vtxSize);
if (idxArray == null) {
return primitiveFail();
}
primitive = stackIntegerValue(6);
if ((primitive < 1)
|| (primitive > PrimTypeMax)) {
return primitiveFail();
}
if (!((primitive == 3)
|| ((primitive == 5)
|| (primitive == 6)))) {
return primitiveFail();
}
primOop = stackObjectValue(7);
if (failed()) {
return null;
}
if (!(isWords(primOop))) {
return primitiveFail();
}
primObj = firstIndexableField(primOop);
/* Do the work */
primSize = byteSizeOf(primOop);
if (primitive == 3) {
if (b3dAddPolygonObject((void*) primObj, primSize, B3D_FACE_RGB, textureIndex, (B3DPrimitiveVertex*) vtxArray, vtxSize, &viewport) != B3D_NO_ERROR) {
return primitiveFail();
}
}
if (primitive == 5) {
if (b3dAddIndexedTriangleObject((void*) primObj, primSize, B3D_FACE_RGB, textureIndex, (B3DPrimitiveVertex*) vtxArray, vtxSize, (B3DInputFace*) idxArray, idxSize / 3, &viewport) != B3D_NO_ERROR) {
return primitiveFail();
}
}
if (primitive == 6) {
if (b3dAddIndexedQuadObject((void*) primObj, primSize, B3D_FACE_RGB, textureIndex, (B3DPrimitiveVertex*) vtxArray, vtxSize, (B3DInputQuad*) idxArray, idxSize / 4, &viewport) != B3D_NO_ERROR) {
return primitiveFail();
}
}
pop(9);
push(primOop);
return 0;
}
/* Primitive. Perform an inplace house holder matrix inversion */
/* B3DTransformerPlugin>>#b3dInplaceHouseHolderInvert */
EXPORT(sqInt)
b3dInplaceHouseHolderInvert(void)
{
double beta;
double d[4][4];
sqInt i;
sqInt j;
sqInt k;
double m[4][4];
sqInt r;
float *rcvr;
double s;
double sigma;
double sum;
double x[4][4] = { {1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1} };
rcvr = stackMatrix(0);
for (i = 0; i <= 3; i += 1) {
for (j = 0; j <= 3; j += 1) {
(m[i])[j] = (rcvr[(i * 4) + j]);
}
}
for (j = 0; j <= 3; j += 1) {
sigma = 0.0;
for (i = j; i <= 3; i += 1) {
sigma += ((m[i])[j]) * ((m[i])[j]);
}
if (sigma < 1.0e-10) {
return primitiveFail();
}
if (((m[j])[j]) < 0.0) {
s = sqrt(sigma);
}
else {
s = 0.0 - (sqrt(sigma));
}
for (r = 0; r <= 3; r += 1) {
(d[j])[r] = s;
}
beta = 1.0 / ((s * ((m[j])[j])) - sigma);
(m[j])[j] = (((m[j])[j]) - s);
for (k = (j + 1); k <= 3; k += 1) {
sum = 0.0;
for (i = j; i <= 3; i += 1) {
sum += ((m[i])[j]) * ((m[i])[k]);
}
sum = sum * beta;
for (i = j; i <= 3; i += 1) {
(m[i])[k] = (((m[i])[k]) + (((m[i])[j]) * sum));
}
}
for (r = 0; r <= 3; r += 1) {
sum = 0.0;
for (i = j; i <= 3; i += 1) {
sum += ((x[i])[r]) * ((m[i])[j]);
}
sum = sum * beta;
for (i = j; i <= 3; i += 1) {
(x[i])[r] = (((x[i])[r]) + (sum * ((m[i])[j])));
}
}
}
for (r = 0; r <= 3; r += 1) {
for (i = 3; i >= 0; i += -1) {
for (j = (i + 1); j <= 3; j += 1) {
(x[i])[r] = (((x[i])[r]) - (((x[j])[r]) * ((m[i])[j])));
}
(x[i])[r] = (((x[i])[r]) / ((d[i])[r]));
}
}
for (i = 0; i <= 3; i += 1) {
for (j = 0; j <= 3; j += 1) {
rcvr[(i * 4) + j] = (((float) ((x[i])[j])));
}
}
return 0;
}
/* Primitive. Load the given index array into the receiver.
NOTE: dstStart is a zero-based index. */
/* B3DVertexBufferPlugin>>#b3dLoadIndexArray */
EXPORT(sqInt)
b3dLoadIndexArray(void)
{
sqInt count;
sqInt dstArray;
int *dstPtr;
sqInt dstSize;
sqInt dstStart;
sqInt i;
int idx;
sqInt maxValue;
sqInt srcArray;
int *srcPtr;
sqInt vtxOffset;
/* Load the arguments */
vtxOffset = stackIntegerValue(0);
maxValue = stackIntegerValue(1);
count = stackIntegerValue(2);
srcArray = stackObjectValue(3);
dstStart = stackIntegerValue(4);
dstArray = stackObjectValue(5);
if (failed()) {
return null;
}
if (!(isWords(srcArray))) {
return primitiveFail();
}
if ((slotSizeOf(srcArray)) < count) {