forked from arduino/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPGraphics3D.java
More file actions
4379 lines (3428 loc) · 129 KB
/
PGraphics3D.java
File metadata and controls
4379 lines (3428 loc) · 129 KB
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
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2004-08 Ben Fry and Casey Reas
Copyright (c) 2001-04 Massachusetts Institute of Technology
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
package processing.core;
import java.awt.Toolkit;
import java.awt.image.*;
import java.util.*;
/**
* Subclass of PGraphics that handles 3D rendering.
* It can render 3D inside a browser window and requires no plug-ins.
* <p/>
* The renderer is mostly set up based on the structure of the OpenGL API,
* if you have questions about specifics that aren't covered here,
* look for reference on the OpenGL implementation of a similar feature.
* <p/>
* Lighting and camera implementation by Simon Greenwold.
*/
public class PGraphics3D extends PGraphics {
/** The depth buffer. */
public float[] zbuffer;
// ........................................................
/** The modelview matrix. */
public PMatrix3D modelview;
/** Inverse modelview matrix, used for lighting. */
public PMatrix3D modelviewInv;
/**
* Marks when changes to the size have occurred, so that the camera
* will be reset in beginDraw().
*/
protected boolean sizeChanged;
/** The camera matrix, the modelview will be set to this on beginDraw. */
public PMatrix3D camera;
/** Inverse camera matrix */
protected PMatrix3D cameraInv;
/** Camera field of view. */
public float cameraFOV;
/** Position of the camera. */
public float cameraX, cameraY, cameraZ;
public float cameraNear, cameraFar;
/** Aspect ratio of camera's view. */
public float cameraAspect;
/** Current projection matrix. */
public PMatrix3D projection;
//////////////////////////////////////////////////////////////
/**
* Maximum lights by default is 8, which is arbitrary for this renderer,
* but is the minimum defined by OpenGL
*/
public static final int MAX_LIGHTS = 8;
public int lightCount = 0;
/** Light types */
public int[] lightType;
/** Light positions */
//public float[][] lightPosition;
public PVector[] lightPosition;
/** Light direction (normalized vector) */
//public float[][] lightNormal;
public PVector[] lightNormal;
/** Light falloff */
public float[] lightFalloffConstant;
public float[] lightFalloffLinear;
public float[] lightFalloffQuadratic;
/** Light spot angle */
public float[] lightSpotAngle;
/** Cosine of light spot angle */
public float[] lightSpotAngleCos;
/** Light spot concentration */
public float[] lightSpotConcentration;
/** Diffuse colors for lights.
* For an ambient light, this will hold the ambient color.
* Internally these are stored as numbers between 0 and 1. */
public float[][] lightDiffuse;
/** Specular colors for lights.
Internally these are stored as numbers between 0 and 1. */
public float[][] lightSpecular;
/** Current specular color for lighting */
public float[] currentLightSpecular;
/** Current light falloff */
public float currentLightFalloffConstant;
public float currentLightFalloffLinear;
public float currentLightFalloffQuadratic;
//////////////////////////////////////////////////////////////
static public final int TRI_DIFFUSE_R = 0;
static public final int TRI_DIFFUSE_G = 1;
static public final int TRI_DIFFUSE_B = 2;
static public final int TRI_DIFFUSE_A = 3;
static public final int TRI_SPECULAR_R = 4;
static public final int TRI_SPECULAR_G = 5;
static public final int TRI_SPECULAR_B = 6;
static public final int TRI_COLOR_COUNT = 7;
// ........................................................
// Whether or not we have to worry about vertex position for lighting calcs
private boolean lightingDependsOnVertexPosition;
static final int LIGHT_AMBIENT_R = 0;
static final int LIGHT_AMBIENT_G = 1;
static final int LIGHT_AMBIENT_B = 2;
static final int LIGHT_DIFFUSE_R = 3;
static final int LIGHT_DIFFUSE_G = 4;
static final int LIGHT_DIFFUSE_B = 5;
static final int LIGHT_SPECULAR_R = 6;
static final int LIGHT_SPECULAR_G = 7;
static final int LIGHT_SPECULAR_B = 8;
static final int LIGHT_COLOR_COUNT = 9;
// Used to shuttle lighting calcs around
// (no need to re-allocate all the time)
protected float[] tempLightingContribution = new float[LIGHT_COLOR_COUNT];
// protected float[] worldNormal = new float[4];
/// Used in lightTriangle(). Allocated here once to avoid re-allocating
protected PVector lightTriangleNorm = new PVector();
// ........................................................
/**
* This is turned on at beginCamera, and off at endCamera
* Currently we don't support nested begin/end cameras.
* If we wanted to, this variable would have to become a stack.
*/
protected boolean manipulatingCamera;
float[][] matrixStack = new float[MATRIX_STACK_DEPTH][16];
float[][] matrixInvStack = new float[MATRIX_STACK_DEPTH][16];
int matrixStackDepth;
// These two matrices always point to either the modelview
// or the modelviewInv, but they are swapped during
// when in camera manipulation mode. That way camera transforms
// are automatically accumulated in inverse on the modelview matrix.
protected PMatrix3D forwardTransform;
protected PMatrix3D reverseTransform;
// Added by ewjordan for accurate texturing purposes. Screen plane is
// not scaled to pixel-size, so these manually keep track of its size
// from frustum() calls. Sorry to add public vars, is there a way
// to compute these from something publicly available without matrix ops?
// (used once per triangle in PTriangle with ENABLE_ACCURATE_TEXTURES)
protected float leftScreen;
protected float rightScreen;
protected float topScreen;
protected float bottomScreen;
protected float nearPlane; //depth of near clipping plane
/** true if frustum has been called to set perspective, false if ortho */
private boolean frustumMode = false;
/**
* Use PSmoothTriangle for rendering instead of PTriangle?
* Usually set by calling smooth() or noSmooth()
*/
static protected boolean s_enableAccurateTextures = false; //maybe just use smooth instead?
/** Used for anti-aliased and perspective corrected rendering. */
public PSmoothTriangle smoothTriangle;
// ........................................................
// pos of first vertex of current shape in vertices array
protected int shapeFirst;
// i think vertex_end is actually the last vertex in the current shape
// and is separate from vertexCount for occasions where drawing happens
// on endDraw() with all the triangles being depth sorted
protected int shapeLast;
// vertices may be added during clipping against the near plane.
protected int shapeLastPlusClipped;
// used for sorting points when triangulating a polygon
// warning - maximum number of vertices for a polygon is DEFAULT_VERTICES
protected int vertexOrder[] = new int[DEFAULT_VERTICES];
// ........................................................
// This is done to keep track of start/stop information for lines in the
// line array, so that lines can be shown as a single path, rather than just
// individual segments. Currently only in use inside PGraphicsOpenGL.
protected int pathCount;
protected int[] pathOffset = new int[64];
protected int[] pathLength = new int[64];
// ........................................................
// line & triangle fields (note that these overlap)
// static protected final int INDEX = 0; // shape index
static protected final int VERTEX1 = 0;
static protected final int VERTEX2 = 1;
static protected final int VERTEX3 = 2; // (triangles only)
/** used to store the strokeColor int for efficient drawing. */
static protected final int STROKE_COLOR = 1; // (points only)
static protected final int TEXTURE_INDEX = 3; // (triangles only)
//static protected final int STROKE_MODE = 2; // (lines only)
//static protected final int STROKE_WEIGHT = 3; // (lines only)
static protected final int POINT_FIELD_COUNT = 2; //4
static protected final int LINE_FIELD_COUNT = 2; //4
static protected final int TRIANGLE_FIELD_COUNT = 4;
// points
static final int DEFAULT_POINTS = 512;
protected int[][] points = new int[DEFAULT_POINTS][POINT_FIELD_COUNT];
protected int pointCount;
// lines
static final int DEFAULT_LINES = 512;
public PLine line; // used for drawing
protected int[][] lines = new int[DEFAULT_LINES][LINE_FIELD_COUNT];
protected int lineCount;
// triangles
static final int DEFAULT_TRIANGLES = 256;
public PTriangle triangle;
protected int[][] triangles =
new int[DEFAULT_TRIANGLES][TRIANGLE_FIELD_COUNT];
protected float triangleColors[][][] =
new float[DEFAULT_TRIANGLES][3][TRI_COLOR_COUNT];
protected int triangleCount; // total number of triangles
// cheap picking someday
//public int shape_index;
// ........................................................
static final int DEFAULT_TEXTURES = 3;
protected PImage[] textures = new PImage[DEFAULT_TEXTURES];
int textureIndex;
// ........................................................
DirectColorModel cm;
MemoryImageSource mis;
//////////////////////////////////////////////////////////////
public PGraphics3D() { }
//public void setParent(PApplet parent)
//public void setPrimary(boolean primary)
//public void setPath(String path)
/**
* Called in response to a resize event, handles setting the
* new width and height internally, as well as re-allocating
* the pixel buffer for the new size.
*
* Note that this will nuke any cameraMode() settings.
*
* No drawing can happen in this function, and no talking to the graphics
* context. That is, no glXxxx() calls, or other things that change state.
*/
public void setSize(int iwidth, int iheight) { // ignore
width = iwidth;
height = iheight;
width1 = width - 1;
height1 = height - 1;
allocate();
reapplySettings();
// init lights (in resize() instead of allocate() b/c needed by opengl)
lightType = new int[MAX_LIGHTS];
lightPosition = new PVector[MAX_LIGHTS];
lightNormal = new PVector[MAX_LIGHTS];
for (int i = 0; i < MAX_LIGHTS; i++) {
lightPosition[i] = new PVector();
lightNormal[i] = new PVector();
}
lightDiffuse = new float[MAX_LIGHTS][3];
lightSpecular = new float[MAX_LIGHTS][3];
lightFalloffConstant = new float[MAX_LIGHTS];
lightFalloffLinear = new float[MAX_LIGHTS];
lightFalloffQuadratic = new float[MAX_LIGHTS];
lightSpotAngle = new float[MAX_LIGHTS];
lightSpotAngleCos = new float[MAX_LIGHTS];
lightSpotConcentration = new float[MAX_LIGHTS];
currentLightSpecular = new float[3];
projection = new PMatrix3D();
modelview = new PMatrix3D();
modelviewInv = new PMatrix3D();
// modelviewStack = new float[MATRIX_STACK_DEPTH][16];
// modelviewInvStack = new float[MATRIX_STACK_DEPTH][16];
// modelviewStackPointer = 0;
forwardTransform = modelview;
reverseTransform = modelviewInv;
// init perspective projection based on new dimensions
cameraFOV = 60 * DEG_TO_RAD; // at least for now
cameraX = width / 2.0f;
cameraY = height / 2.0f;
cameraZ = cameraY / ((float) Math.tan(cameraFOV / 2.0f));
cameraNear = cameraZ / 10.0f;
cameraFar = cameraZ * 10.0f;
cameraAspect = (float)width / (float)height;
camera = new PMatrix3D();
cameraInv = new PMatrix3D();
// set this flag so that beginDraw() will do an update to the camera.
sizeChanged = true;
}
protected void allocate() {
//System.out.println(this + " allocating for " + width + " " + height);
//new Exception().printStackTrace();
pixelCount = width * height;
pixels = new int[pixelCount];
zbuffer = new float[pixelCount];
if (primarySurface) {
cm = new DirectColorModel(32, 0x00ff0000, 0x0000ff00, 0x000000ff);;
mis = new MemoryImageSource(width, height, pixels, 0, width);
mis.setFullBufferUpdates(true);
mis.setAnimated(true);
image = Toolkit.getDefaultToolkit().createImage(mis);
} else {
// when not the main drawing surface, need to set the zbuffer,
// because there's a possibility that background() will not be called
Arrays.fill(zbuffer, Float.MAX_VALUE);
}
line = new PLine(this);
triangle = new PTriangle(this);
smoothTriangle = new PSmoothTriangle(this);
}
//public void dispose()
////////////////////////////////////////////////////////////
//public boolean canDraw()
public void beginDraw() {
// need to call defaults(), but can only be done when it's ok
// to draw (i.e. for opengl, no drawing can be done outside
// beginDraw/endDraw).
if (!settingsInited) defaultSettings();
if (sizeChanged) {
// set up the default camera
camera();
// defaults to perspective, if the user has setup up their
// own projection, they'll need to fix it after resize anyway.
// this helps the people who haven't set up their own projection.
perspective();
// clear the flag
sizeChanged = false;
}
resetMatrix(); // reset model matrix
// reset vertices
vertexCount = 0;
modelview.set(camera);
modelviewInv.set(cameraInv);
// clear out the lights, they'll have to be turned on again
lightCount = 0;
lightingDependsOnVertexPosition = false;
lightFalloff(1, 0, 0);
lightSpecular(0, 0, 0);
/*
// reset lines
lineCount = 0;
if (line != null) line.reset(); // is this necessary?
pathCount = 0;
// reset triangles
triangleCount = 0;
if (triangle != null) triangle.reset(); // necessary?
*/
shapeFirst = 0;
// reset textures
Arrays.fill(textures, null);
textureIndex = 0;
normal(0, 0, 1);
}
/**
* See notes in PGraphics.
* If z-sorting has been turned on, then the triangles will
* all be quicksorted here (to make alpha work more properly)
* and then blit to the screen.
*/
public void endDraw() {
// no need to z order and render
// shapes were already rendered in endShape();
// (but can't return, since needs to update memimgsrc)
if (hints[ENABLE_DEPTH_SORT]) {
flush();
}
if (mis != null) {
mis.newPixels(pixels, cm, 0, width);
}
// mark pixels as having been updated, so that they'll work properly
// when this PGraphics is drawn using image().
updatePixels();
}
////////////////////////////////////////////////////////////
//protected void checkSettings()
protected void defaultSettings() {
super.defaultSettings();
manipulatingCamera = false;
forwardTransform = modelview;
reverseTransform = modelviewInv;
// set up the default camera
camera();
// defaults to perspective, if the user has setup up their
// own projection, they'll need to fix it after resize anyway.
// this helps the people who haven't set up their own projection.
perspective();
// easiest for beginners
textureMode(IMAGE);
emissive(0.0f);
specular(0.5f);
shininess(1.0f);
}
//protected void reapplySettings()
////////////////////////////////////////////////////////////
public void hint(int which) {
if (which == DISABLE_DEPTH_SORT) {
flush();
} else if (which == DISABLE_DEPTH_TEST) {
if (zbuffer != null) { // will be null in OpenGL and others
Arrays.fill(zbuffer, Float.MAX_VALUE);
}
}
super.hint(which);
}
//////////////////////////////////////////////////////////////
//public void beginShape()
public void beginShape(int kind) {
shape = kind;
// shape_index = shape_index + 1;
// if (shape_index == -1) {
// shape_index = 0;
// }
if (hints[ENABLE_DEPTH_SORT]) {
// continue with previous vertex, line and triangle count
// all shapes are rendered at endDraw();
shapeFirst = vertexCount;
shapeLast = 0;
} else {
// reset vertex, line and triangle information
// every shape is rendered at endShape();
vertexCount = 0;
if (line != null) line.reset(); // necessary?
lineCount = 0;
// pathCount = 0;
if (triangle != null) triangle.reset(); // necessary?
triangleCount = 0;
}
textureImage = null;
curveVertexCount = 0;
normalMode = NORMAL_MODE_AUTO;
// normalCount = 0;
}
//public void normal(float nx, float ny, float nz)
//public void textureMode(int mode)
public void texture(PImage image) {
textureImage = image;
if (textureIndex == textures.length - 1) {
textures = (PImage[]) PApplet.expand(textures);
}
if (textures[textureIndex] != null) { // ???
textureIndex++;
}
textures[textureIndex] = image;
}
public void vertex(float x, float y) {
// override so that the default 3D implementation will be used,
// which will pick up all 3D settings (e.g. emissive, ambient)
vertex(x, y, 0);
}
//public void vertex(float x, float y, float z)
public void vertex(float x, float y, float u, float v) {
// see vertex(x, y) for note
vertex(x, y, 0, u, v);
}
//public void vertex(float x, float y, float z, float u, float v)
//public void breakShape()
//public void endShape()
public void endShape(int mode) {
shapeLast = vertexCount;
shapeLastPlusClipped = shapeLast;
// don't try to draw if there are no vertices
// (fixes a bug in LINE_LOOP that re-adds a nonexistent vertex)
if (vertexCount == 0) {
shape = 0;
return;
}
// convert points from model (X/Y/Z) to camera space (VX/VY/VZ).
// Do this now because we will be clipping them on add_triangle.
endShapeModelToCamera(shapeFirst, shapeLast);
if (stroke) {
endShapeStroke(mode);
}
if (fill || textureImage != null) {
endShapeFill();
}
// transform, light, and clip
endShapeLighting(lightCount > 0 && fill);
// convert points from camera space (VX, VY, VZ) to screen space (X, Y, Z)
// (this appears to be wasted time with the OpenGL renderer)
endShapeCameraToScreen(shapeFirst, shapeLastPlusClipped);
// render shape and fill here if not saving the shapes for later
// if true, the shapes will be rendered on endDraw
if (!hints[ENABLE_DEPTH_SORT]) {
if (fill || textureImage != null) {
if (triangleCount > 0) {
renderTriangles(0, triangleCount);
if (raw != null) {
rawTriangles(0, triangleCount);
}
triangleCount = 0;
}
}
if (stroke) {
if (pointCount > 0) {
renderPoints(0, pointCount);
if (raw != null) {
rawPoints(0, pointCount);
}
pointCount = 0;
}
if (lineCount > 0) {
renderLines(0, lineCount);
if (raw != null) {
rawLines(0, lineCount);
}
lineCount = 0;
}
}
pathCount = 0;
}
shape = 0;
}
protected void endShapeModelToCamera(int start, int stop) {
for (int i = start; i < stop; i++) {
float vertex[] = vertices[i];
vertex[VX] =
modelview.m00*vertex[X] + modelview.m01*vertex[Y] +
modelview.m02*vertex[Z] + modelview.m03;
vertex[VY] =
modelview.m10*vertex[X] + modelview.m11*vertex[Y] +
modelview.m12*vertex[Z] + modelview.m13;
vertex[VZ] =
modelview.m20*vertex[X] + modelview.m21*vertex[Y] +
modelview.m22*vertex[Z] + modelview.m23;
vertex[VW] =
modelview.m30*vertex[X] + modelview.m31*vertex[Y] +
modelview.m32*vertex[Z] + modelview.m33;
// normalize
if (vertex[VW] != 0 && vertex[VW] != 1) {
vertex[VX] /= vertex[VW];
vertex[VY] /= vertex[VW];
vertex[VZ] /= vertex[VW];
}
vertex[VW] = 1;
}
}
protected void endShapeStroke(int mode) {
switch (shape) {
case POINTS:
{
int stop = shapeLast;
for (int i = shapeFirst; i < stop; i++) {
// if (strokeWeight == 1) {
addPoint(i);
// } else {
// addLineBreak(); // total overkill for points
// addLine(i, i);
// }
}
}
break;
case LINES:
{
// store index of first vertex
int first = lineCount;
int stop = shapeLast - 1;
//increment = (shape == LINES) ? 2 : 1;
// for LINE_STRIP and LINE_LOOP, make this all one path
if (shape != LINES) addLineBreak();
for (int i = shapeFirst; i < stop; i += 2) {
// for LINES, make a new path for each segment
if (shape == LINES) addLineBreak();
addLine(i, i+1);
}
// for LINE_LOOP, close the loop with a final segment
//if (shape == LINE_LOOP) {
if (mode == CLOSE) {
addLine(stop, lines[first][VERTEX1]);
}
}
break;
case TRIANGLES:
{
for (int i = shapeFirst; i < shapeLast-2; i += 3) {
addLineBreak();
//counter = i - vertex_start;
addLine(i+0, i+1);
addLine(i+1, i+2);
addLine(i+2, i+0);
}
}
break;
case TRIANGLE_STRIP:
{
// first draw all vertices as a line strip
int stop = shapeLast-1;
addLineBreak();
for (int i = shapeFirst; i < stop; i++) {
//counter = i - vertex_start;
addLine(i, i+1);
}
// then draw from vertex (n) to (n+2)
stop = shapeLast-2;
for (int i = shapeFirst; i < stop; i++) {
addLineBreak();
addLine(i, i+2);
}
}
break;
case TRIANGLE_FAN:
{
// this just draws a series of line segments
// from the center to each exterior point
for (int i = shapeFirst + 1; i < shapeLast; i++) {
addLineBreak();
addLine(shapeFirst, i);
}
// then a single line loop around the outside.
addLineBreak();
for (int i = shapeFirst + 1; i < shapeLast-1; i++) {
addLine(i, i+1);
}
// closing the loop
addLine(shapeLast-1, shapeFirst + 1);
}
break;
case QUADS:
{
for (int i = shapeFirst; i < shapeLast; i += 4) {
addLineBreak();
//counter = i - vertex_start;
addLine(i+0, i+1);
addLine(i+1, i+2);
addLine(i+2, i+3);
addLine(i+3, i+0);
}
}
break;
case QUAD_STRIP:
{
for (int i = shapeFirst; i < shapeLast - 3; i += 2) {
addLineBreak();
addLine(i+0, i+2);
addLine(i+2, i+3);
addLine(i+3, i+1);
addLine(i+1, i+0);
}
}
break;
case POLYGON:
{
// store index of first vertex
int stop = shapeLast - 1;
addLineBreak();
for (int i = shapeFirst; i < stop; i++) {
addLine(i, i+1);
}
if (mode == CLOSE) {
// draw the last line connecting back to the first point in poly
addLine(stop, shapeFirst); //lines[first][VERTEX1]);
}
}
break;
}
}
protected void endShapeFill() {
switch (shape) {
case TRIANGLE_FAN:
{
int stop = shapeLast - 1;
for (int i = shapeFirst + 1; i < stop; i++) {
addTriangle(shapeFirst, i, i+1);
}
}
break;
case TRIANGLES:
{
int stop = shapeLast - 2;
for (int i = shapeFirst; i < stop; i += 3) {
// have to switch between clockwise/counter-clockwise
// otherwise the feller is backwards and renderer won't draw
if ((i % 2) == 0) {
addTriangle(i, i+2, i+1);
} else {
addTriangle(i, i+1, i+2);
}
}
}
break;
case TRIANGLE_STRIP:
{
int stop = shapeLast - 2;
for (int i = shapeFirst; i < stop; i++) {
// have to switch between clockwise/counter-clockwise
// otherwise the feller is backwards and renderer won't draw
if ((i % 2) == 0) {
addTriangle(i, i+2, i+1);
} else {
addTriangle(i, i+1, i+2);
}
}
}
break;
case QUADS:
{
int stop = vertexCount-3;
for (int i = shapeFirst; i < stop; i += 4) {
// first triangle
addTriangle(i, i+1, i+2);
// second triangle
addTriangle(i, i+2, i+3);
}
}
break;
case QUAD_STRIP:
{
int stop = vertexCount-3;
for (int i = shapeFirst; i < stop; i += 2) {
// first triangle
addTriangle(i+0, i+2, i+1);
// second triangle
addTriangle(i+2, i+3, i+1);
}
}
break;
case POLYGON:
{
addPolygonTriangles();
}
break;
}
}
protected void endShapeLighting(boolean lights) {
if (lights) {
// If the lighting does not depend on vertex position and there is a single
// normal specified for this shape, go ahead and apply the same lighting
// contribution to every vertex in this shape (one lighting calc!)
if (!lightingDependsOnVertexPosition && normalMode == NORMAL_MODE_SHAPE) {
calcLightingContribution(shapeFirst, tempLightingContribution);
for (int tri = 0; tri < triangleCount; tri++) {
lightTriangle(tri, tempLightingContribution);
}
} else { // Otherwise light each triangle individually...
for (int tri = 0; tri < triangleCount; tri++) {
lightTriangle(tri);
}
}
} else {
for (int tri = 0; tri < triangleCount; tri++) {
int index = triangles[tri][VERTEX1];
copyPrelitVertexColor(tri, index, 0);
index = triangles[tri][VERTEX2];
copyPrelitVertexColor(tri, index, 1);
index = triangles[tri][VERTEX3];
copyPrelitVertexColor(tri, index, 2);
}
}
}
protected void endShapeCameraToScreen(int start, int stop) {
for (int i = start; i < stop; i++) {
float vx[] = vertices[i];
float ox =
projection.m00*vx[VX] + projection.m01*vx[VY] +
projection.m02*vx[VZ] + projection.m03*vx[VW];
float oy =
projection.m10*vx[VX] + projection.m11*vx[VY] +
projection.m12*vx[VZ] + projection.m13*vx[VW];
float oz =
projection.m20*vx[VX] + projection.m21*vx[VY] +
projection.m22*vx[VZ] + projection.m23*vx[VW];
float ow =
projection.m30*vx[VX] + projection.m31*vx[VY] +
projection.m32*vx[VZ] + projection.m33*vx[VW];
if (ow != 0 && ow != 1) {
ox /= ow; oy /= ow; oz /= ow;
}
vx[TX] = width * (1 + ox) / 2.0f;
vx[TY] = height * (1 + oy) / 2.0f;
vx[TZ] = (oz + 1) / 2.0f;
}
}
/////////////////////////////////////////////////////////////////////////////
// POINTS
protected void addPoint(int a) {
if (pointCount == points.length) {
int[][] temp = new int[pointCount << 1][LINE_FIELD_COUNT];
System.arraycopy(points, 0, temp, 0, lineCount);
points = temp;
}
points[pointCount][VERTEX1] = a;
//points[pointCount][STROKE_MODE] = strokeCap | strokeJoin;
points[pointCount][STROKE_COLOR] = strokeColor;
//points[pointCount][STROKE_WEIGHT] = (int) (strokeWeight + 0.5f); // hmm
pointCount++;
}
protected void renderPoints(int start, int stop) {
if (strokeWeight != 1) {
for (int i = start; i < stop; i++) {
float[] a = vertices[points[i][VERTEX1]];
renderLineVertices(a, a);
}
} else {
for (int i = start; i < stop; i++) {