-
Notifications
You must be signed in to change notification settings - Fork 217
/
GVRSphereSceneObject.java
1031 lines (885 loc) · 39.5 KB
/
GVRSphereSceneObject.java
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
/* Copyright 2015 Samsung Electronics Co., LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gearvrf.scene_objects;
import java.util.concurrent.Future;
import org.gearvrf.GVRMaterial;
import org.gearvrf.GVRSceneObject;
import org.gearvrf.GVRRenderData;
import org.gearvrf.GVRContext;
import org.gearvrf.GVRMesh;
import org.gearvrf.GVRTexture;
import org.gearvrf.utility.Log;
public class GVRSphereSceneObject extends GVRSceneObject {
@SuppressWarnings("unused")
private static final String TAG = Log.tag(GVRSphereSceneObject.class);
private static final int STACK_NUMBER = 18;
private static final int SLICE_NUMBER = 36;
private float[] vertices;
private float[] normals;
private float[] texCoords;
private char[] indices;
private int vertexCount = 0;
private int texCoordCount = 0;
private char indexCount = 0;
private char triangleCount = 0;
/**
* Constructs a sphere scene object with a radius of 1 and 18 stacks, and 36
* slices.
*
* The sphere's triangles and normals are facing out and the same texture
* will be applied to each side of the sphere.
*
* @param gvrContext
* current {@link GVRContext}
*/
public GVRSphereSceneObject(GVRContext gvrContext) {
super(gvrContext);
generateSphereObject(gvrContext, STACK_NUMBER, SLICE_NUMBER, true,
new GVRMaterial(gvrContext), 1);
}
/**
* Constructs a sphere scene object with a radius of 1 and 18 stacks, and 36
* slices.
*
* The sphere's triangles and normals are facing either in or out and the
* same texture will be applied to each side of the sphere.
*
* @param gvrContext
* current {@link GVRContext}
*
* @param facingOut
* whether the triangles and normals should be facing in or
* facing out.
*/
public GVRSphereSceneObject(GVRContext gvrContext, boolean facingOut) {
super(gvrContext);
generateSphereObject(gvrContext, STACK_NUMBER, SLICE_NUMBER, facingOut,
new GVRMaterial(gvrContext), 1);
}
/**
* Constructs a sphere scene object 18 stacks, and 36
* slices.
*
* The sphere's triangles and normals are facing either in or out and the
* same texture will be applied to each side of the sphere.
*
* @param gvrContext
* current {@link GVRContext}
*
* @param facingOut
* whether the triangles and normals should be facing in or
* facing out.
* @param radius
* sets the sphere with the radius parameter. Radius must be > 0
* otherwise, set it to the default of 1
*/
public GVRSphereSceneObject(GVRContext gvrContext, boolean facingOut, float radius) {
super(gvrContext);
if (radius < 0) radius = 1;
generateSphereObject(gvrContext, STACK_NUMBER, SLICE_NUMBER, facingOut,
new GVRMaterial(gvrContext), radius);
}
/**
* Constructs a sphere scene object with a radius of 1 and 18 stacks, and 36
* slices.
*
* The sphere's triangles and normals are facing either in or out and the
* same texture will be applied to each side of the sphere.
*
* @param gvrContext
* current {@link GVRContext}
*
* @param stackNumber
* the number of stacks for the sphere. It should be equal or
* greater than 3.
*
* @param sliceNumber
* the number of slices for the sphere. It should be equal or
* greater than 4.
*
* @param facingOut
* whether the triangles and normals should be facing in or
* facing out.
*/
public GVRSphereSceneObject(GVRContext gvrContext, int stackNumber, int sliceNumber, boolean facingOut) {
super(gvrContext);
generateSphereObject(gvrContext, stackNumber, sliceNumber, facingOut, new GVRMaterial(gvrContext), 1);
}
/**
* Constructs a sphere scene object with a radius of 1 and 18 stacks, and 36
* slices.
*
* The sphere's triangles and normals are facing either in or out and the
* same texture will be applied to each side of the sphere.
*
* @param gvrContext
* current {@link GVRContext}
*
* @param facingOut
* whether the triangles and normals should be facing in or
* facing out.
*
* @param texture
* the texture for the sphere.
*/
public GVRSphereSceneObject(GVRContext gvrContext, boolean facingOut,
GVRTexture texture) {
super(gvrContext);
GVRMaterial material = new GVRMaterial(gvrContext);
material.setMainTexture(texture);
generateSphereObject(gvrContext, STACK_NUMBER, SLICE_NUMBER, facingOut,
material, 1);
}
/**
* Constructs a sphere scene object with a radius of 1 and 18 stacks, and 36
* slices.
*
* The sphere's triangles and normals are facing either in or out and the
* same texture will be applied to each side of the sphere.
*
* @param gvrContext
* current {@link GVRContext}
*
* @param stackNumber
* the number of stacks for the sphere. It should be equal or
* greater than 3.
*
* @param sliceNumber
* the number of slices for the sphere. It should be equal or
* greater than 4.
*
* @param facingOut
* whether the triangles and normals should be facing in or
* facing out.
*
* @param texture
* the texture for the sphere.
*/
public GVRSphereSceneObject(GVRContext gvrContext, int stackNumber, int sliceNumber, boolean facingOut, GVRTexture texture) {
super(gvrContext);
GVRMaterial material = new GVRMaterial(gvrContext);
material.setMainTexture(texture);
generateSphereObject(gvrContext, stackNumber, sliceNumber, facingOut, material, 1);
}
/**
* Constructs a sphere scene object with a radius of 1 and 18 stacks, and 36
* slices.
*
* The sphere's triangles and normals are facing either in or out and the
* same material will be applied to each side of the sphere.
*
* @param gvrContext
* current {@link GVRContext}
*
* @param facingOut
* whether the triangles and normals should be facing in or
* facing out.
*
* @param material
* the material for the sphere.
*/
public GVRSphereSceneObject(GVRContext gvrContext, boolean facingOut,
GVRMaterial material) {
super(gvrContext);
generateSphereObject(gvrContext, STACK_NUMBER, SLICE_NUMBER, facingOut,
material, 1);
}
/**
* Constructs a sphere scene object with a radius of 1 and 18 stacks, and 36
* slices.
*
* The sphere's triangles and normals are facing either in or out and the
* same material will be applied to each side of the sphere.
*
* @param gvrContext
* current {@link GVRContext}
*
* @param facingOut
* whether the triangles and normals should be facing in or
* facing out.
*
* @param material
* the material for the sphere.
* @param radius
* sets the sphere with the radius parameter. Radius must be > 0
* otherwise, set it to the default of 1
*/
public GVRSphereSceneObject(GVRContext gvrContext, boolean facingOut,
GVRMaterial material, float radius)
{
super(gvrContext);
generateSphereObject(gvrContext, STACK_NUMBER, SLICE_NUMBER, facingOut,
material, radius);
}
/**
* Constructs a sphere scene object with a radius of 1 and user specified
* stack and slice numbers.
*
* The sphere's triangles and normals are facing either in or out and the
* same material will be applied to each side of the sphere.
*
* @param gvrContext
* current {@link GVRContext}
*
* @param stackNumber
* the number of stacks for the sphere. It should be equal or
* greater than 3.
*
* @param sliceNumber
* the number of slices for the sphere. It should be equal or
* greater than 4.
*
* @param facingOut
* whether the triangles and normals should be facing in or
* facing out.
*
* @param material
* the material for the sphere.
*/
public GVRSphereSceneObject(GVRContext gvrContext, int stackNumber,
int sliceNumber, boolean facingOut, GVRMaterial material) {
super(gvrContext);
// assert sliceNumber>=4
if (sliceNumber < 4) {
throw new IllegalArgumentException(
"Slice number should be equal or greater than 4.");
}
// assert stackNumber>=3
if (stackNumber < 3) {
throw new IllegalArgumentException(
"Stack number should be equal or greater than 3.");
}
generateSphereObject(gvrContext, stackNumber, sliceNumber, facingOut,
material, 1);
}
/**
* Constructs a sphere scene object with a radius of 1 and user specified
* stack and slice numbers. The sphere is subdivided into MxN meshes, where M=sliceSegmengNumber and N=(stackSegmentNumber+2) are specified by user.
*
* The sphere's triangles and normals are facing either in or out and the
* same material will be applied to each side of the sphere.
*
* @param gvrContext
* current {@link GVRContext}
*
* @param stackNumber
* the number of stacks for the sphere. It should be equal or
* greater than 3.
*
* @param sliceNumber
* the number of slices for the sphere. It should be equal or
* greater than 4.
*
* @param facingOut
* whether the triangles and normals should be facing in or
* facing out.
*
* @param material
* the material for the sphere.
*
* @param stackSegmentNumber
* the segment number along vertical direction (i.e. stacks).
* Note neither top cap nor bottom cap are subdivided along
* vertical direction. So number of stacks in body part (i.e.
* stackNumber-2) should be divisible by stackSegmentNumber.
*
* @param sliceSegmentNumber
* the segment number along horizontal direction (i.e. slices).
* Number of slices (i.e. sliceNumber) should be divisible by
* sliceSegmentNumber.
*/
public GVRSphereSceneObject(GVRContext gvrContext, int stackNumber,
int sliceNumber, boolean facingOut, GVRMaterial material,
int stackSegmentNumber, int sliceSegmentNumber) {
super(gvrContext);
// assert stackNumber>=3
if (stackNumber < 3) {
throw new IllegalArgumentException(
"Stack number should be equal or greater than 3.");
}
// assert sliceNumber>=4
if (sliceNumber < 4) {
throw new IllegalArgumentException(
"Slice number should be equal or greater than 4.");
}
// assert for valid stackSegmentNumber
if ((stackNumber - 2) % stackSegmentNumber != 0) {
throw new IllegalArgumentException(
"(stackNumber-2) should be divisible by stackSegmentNumber.");
}
// assert for valid sliceSegmentNumber
if (sliceNumber % sliceSegmentNumber != 0) {
throw new IllegalArgumentException(
"sliceNumber should be divisible by sliceSegmentNumber.");
}
generateComplexSphereObject(gvrContext, stackNumber, sliceNumber,
facingOut, material, stackSegmentNumber, sliceSegmentNumber);
}
private void generateSphereObject(GVRContext gvrContext, int stackNumber,
int sliceNumber, boolean facingOut, GVRMaterial material, float radius) {
generateSphere(stackNumber, sliceNumber, facingOut);
// multiply by radius > 0
//float radius = 1;
for (int i = 0; i < vertices.length; i++) {
vertices[i] *= radius;
}
GVRMesh mesh = new GVRMesh(gvrContext, "float3 a_position float2 a_texcoord float3 a_normal");
mesh.setVertices(vertices);
mesh.setNormals(normals);
mesh.setTexCoords(texCoords);
mesh.setIndices(indices);
GVRRenderData renderData = new GVRRenderData(gvrContext, material);
attachComponent(renderData);
renderData.setMesh(mesh);
}
private void generateSphere(int stackNumber, int sliceNumber,
boolean facingOut) {
int capVertexNumber = 3 * sliceNumber;
int bodyVertexNumber = 4 * sliceNumber * (stackNumber - 2);
int vertexNumber = (2 * capVertexNumber) + bodyVertexNumber;
int triangleNumber = (2 * capVertexNumber)
+ (6 * sliceNumber * (stackNumber - 2));
vertices = new float[3 * vertexNumber];
normals = new float[3 * vertexNumber];
texCoords = new float[2 * vertexNumber];
indices = new char[triangleNumber];
// bottom cap
createCap(stackNumber, sliceNumber, false, facingOut);
// body
createBody(stackNumber, sliceNumber, facingOut);
// top cap
createCap(stackNumber, sliceNumber, true, facingOut);
}
private void createCap(int stackNumber, int sliceNumber, boolean top,
boolean facingOut) {
float stackPercentage0;
float stackPercentage1;
if (!top) {
stackPercentage0 = ((float) (stackNumber - 1) / stackNumber);
stackPercentage1 = 1.0f;
} else {
stackPercentage0 = (1.0f / stackNumber);
stackPercentage1 = 0.0f;
}
float t0 = stackPercentage0;
float t1 = stackPercentage1;
double theta0 = stackPercentage0 * Math.PI;
double theta1 = stackPercentage1 * Math.PI;
double cosTheta0 = Math.cos(theta0);
double sinTheta0 = Math.sin(theta0);
double cosTheta1 = Math.cos(theta1);
double sinTheta1 = Math.sin(theta1);
for (int slice = 0; slice < sliceNumber; slice++) {
float slicePercentage0 = ((float) (slice) / sliceNumber);
float slicePercentage1 = ((float) (slice + 1) / sliceNumber);
double phi0 = slicePercentage0 * 2.0 * Math.PI;
double phi1 = slicePercentage1 * 2.0 * Math.PI;
float s0, s1;
if (facingOut) {
s0 = 1 - slicePercentage0;
s1 = 1 - slicePercentage1;
} else {
s0 = slicePercentage0;
s1 = slicePercentage1;
}
float s2 = (s0 + s1) / 2.0f;
double cosPhi0 = Math.cos(phi0);
double sinPhi0 = Math.sin(phi0);
double cosPhi1 = Math.cos(phi1);
double sinPhi1 = Math.sin(phi1);
float x0 = (float) (sinTheta0 * cosPhi0);
float y0 = (float) cosTheta0;
float z0 = (float) (sinTheta0 * sinPhi0);
float x1 = (float) (sinTheta0 * cosPhi1);
float y1 = (float) cosTheta0;
float z1 = (float) (sinTheta0 * sinPhi1);
float x2 = (float) (sinTheta1 * cosPhi0);
float y2 = (float) cosTheta1;
float z2 = (float) (sinTheta1 * sinPhi0);
vertices[vertexCount + 0] = x0;
vertices[vertexCount + 1] = y0;
vertices[vertexCount + 2] = z0;
vertices[vertexCount + 3] = x1;
vertices[vertexCount + 4] = y1;
vertices[vertexCount + 5] = z1;
vertices[vertexCount + 6] = x2;
vertices[vertexCount + 7] = y2;
vertices[vertexCount + 8] = z2;
if (facingOut) {
normals[vertexCount + 0] = x0;
normals[vertexCount + 1] = y0;
normals[vertexCount + 2] = z0;
normals[vertexCount + 3] = x1;
normals[vertexCount + 4] = y1;
normals[vertexCount + 5] = z1;
normals[vertexCount + 6] = x2;
normals[vertexCount + 7] = y2;
normals[vertexCount + 8] = z2;
} else {
normals[vertexCount + 0] = -x0;
normals[vertexCount + 1] = -y0;
normals[vertexCount + 2] = -z0;
normals[vertexCount + 3] = -x1;
normals[vertexCount + 4] = -y1;
normals[vertexCount + 5] = -z1;
normals[vertexCount + 6] = -x2;
normals[vertexCount + 7] = -y2;
normals[vertexCount + 8] = -z2;
}
texCoords[texCoordCount + 0] = s0;
texCoords[texCoordCount + 1] = t0;
texCoords[texCoordCount + 2] = s1;
texCoords[texCoordCount + 3] = t0;
texCoords[texCoordCount + 4] = s2;
texCoords[texCoordCount + 5] = t1;
if ((facingOut && top) || (!facingOut && !top)) {
indices[indexCount + 0] = (char) (triangleCount + 1);
indices[indexCount + 1] = (char) (triangleCount + 0);
indices[indexCount + 2] = (char) (triangleCount + 2);
} else {
indices[indexCount + 0] = (char) (triangleCount + 0);
indices[indexCount + 1] = (char) (triangleCount + 1);
indices[indexCount + 2] = (char) (triangleCount + 2);
}
vertexCount += 9;
texCoordCount += 6;
indexCount += 3;
triangleCount += 3;
}
}
private void createBody(int stackNumber, int sliceNumber, boolean facingOut) {
for (int stack = 1; stack < stackNumber - 1; stack++) {
float stackPercentage0 = ((float) (stack) / stackNumber);
float stackPercentage1 = ((float) (stack + 1) / stackNumber);
float t0 = stackPercentage0;
float t1 = stackPercentage1;
double theta0 = stackPercentage0 * Math.PI;
double theta1 = stackPercentage1 * Math.PI;
double cosTheta0 = Math.cos(theta0);
double sinTheta0 = Math.sin(theta0);
double cosTheta1 = Math.cos(theta1);
double sinTheta1 = Math.sin(theta1);
for (int slice = 0; slice < sliceNumber; slice++) {
float slicePercentage0 = ((float) (slice) / sliceNumber);
float slicePercentage1 = ((float) (slice + 1) / sliceNumber);
double phi0 = slicePercentage0 * 2.0 * Math.PI;
double phi1 = slicePercentage1 * 2.0 * Math.PI;
float s0, s1;
if (facingOut) {
s0 = 1.0f - slicePercentage0;
s1 = 1.0f - slicePercentage1;
} else {
s0 = slicePercentage0;
s1 = slicePercentage1;
}
double cosPhi0 = Math.cos(phi0);
double sinPhi0 = Math.sin(phi0);
double cosPhi1 = Math.cos(phi1);
double sinPhi1 = Math.sin(phi1);
float x0 = (float) (sinTheta0 * cosPhi0);
float y0 = (float) cosTheta0;
float z0 = (float) (sinTheta0 * sinPhi0);
float x1 = (float) (sinTheta0 * cosPhi1);
float y1 = (float) cosTheta0;
float z1 = (float) (sinTheta0 * sinPhi1);
float x2 = (float) (sinTheta1 * cosPhi0);
float y2 = (float) cosTheta1;
float z2 = (float) (sinTheta1 * sinPhi0);
float x3 = (float) (sinTheta1 * cosPhi1);
float y3 = (float) cosTheta1;
float z3 = (float) (sinTheta1 * sinPhi1);
vertices[vertexCount + 0] = x0;
vertices[vertexCount + 1] = y0;
vertices[vertexCount + 2] = z0;
vertices[vertexCount + 3] = x1;
vertices[vertexCount + 4] = y1;
vertices[vertexCount + 5] = z1;
vertices[vertexCount + 6] = x2;
vertices[vertexCount + 7] = y2;
vertices[vertexCount + 8] = z2;
vertices[vertexCount + 9] = x3;
vertices[vertexCount + 10] = y3;
vertices[vertexCount + 11] = z3;
if (facingOut) {
normals[vertexCount + 0] = x0;
normals[vertexCount + 1] = y0;
normals[vertexCount + 2] = z0;
normals[vertexCount + 3] = x1;
normals[vertexCount + 4] = y1;
normals[vertexCount + 5] = z1;
normals[vertexCount + 6] = x2;
normals[vertexCount + 7] = y2;
normals[vertexCount + 8] = z2;
normals[vertexCount + 9] = x3;
normals[vertexCount + 10] = y3;
normals[vertexCount + 11] = z3;
} else {
normals[vertexCount + 0] = -x0;
normals[vertexCount + 1] = -y0;
normals[vertexCount + 2] = -z0;
normals[vertexCount + 3] = -x1;
normals[vertexCount + 4] = -y1;
normals[vertexCount + 5] = -z1;
normals[vertexCount + 6] = -x2;
normals[vertexCount + 7] = -y2;
normals[vertexCount + 8] = -z2;
normals[vertexCount + 9] = -x3;
normals[vertexCount + 10] = -y3;
normals[vertexCount + 11] = -z3;
}
texCoords[texCoordCount + 0] = s0;
texCoords[texCoordCount + 1] = t0;
texCoords[texCoordCount + 2] = s1;
texCoords[texCoordCount + 3] = t0;
texCoords[texCoordCount + 4] = s0;
texCoords[texCoordCount + 5] = t1;
texCoords[texCoordCount + 6] = s1;
texCoords[texCoordCount + 7] = t1;
// one quad looking from outside toward center
//
// @formatter:off
//
// s1 --> s0
//
// t0 1-----0
// | | |
// v | |
// t1 3-----2
//
// @formatter:on
//
// Note that tex_coord t increase from top to bottom because the
// texture image is loaded upside down.
if (facingOut) {
indices[indexCount + 0] = (char) (triangleCount + 0);
indices[indexCount + 1] = (char) (triangleCount + 1);
indices[indexCount + 2] = (char) (triangleCount + 2);
indices[indexCount + 3] = (char) (triangleCount + 2);
indices[indexCount + 4] = (char) (triangleCount + 1);
indices[indexCount + 5] = (char) (triangleCount + 3);
} else {
indices[indexCount + 0] = (char) (triangleCount + 0);
indices[indexCount + 1] = (char) (triangleCount + 2);
indices[indexCount + 2] = (char) (triangleCount + 1);
indices[indexCount + 3] = (char) (triangleCount + 2);
indices[indexCount + 4] = (char) (triangleCount + 3);
indices[indexCount + 5] = (char) (triangleCount + 1);
}
vertexCount += 12;
texCoordCount += 8;
indexCount += 6;
triangleCount += 4;
}
}
}
private void generateComplexSphereObject(GVRContext gvrContext,
int stackNumber, int sliceNumber, boolean facingOut,
GVRMaterial material, int stackSegmentNumber, int sliceSegmentNumber) {
// bottom cap
createComplexCap(gvrContext, stackNumber, sliceNumber, false,
facingOut, material, sliceSegmentNumber);
// body
createComplexBody(gvrContext, stackNumber, sliceNumber, facingOut,
material, stackSegmentNumber, sliceSegmentNumber);
// top cap
createComplexCap(gvrContext, stackNumber, sliceNumber, true, facingOut,
material, sliceSegmentNumber);
// attached an empty renderData for parent object, so that we can set
// some common properties
GVRRenderData renderData = new GVRRenderData(gvrContext, material);
attachComponent(renderData);
}
private void createComplexCap(GVRContext gvrContext, int stackNumber,
int sliceNumber, boolean top, boolean facingOut,
GVRMaterial material, int sliceSegmentNumber) {
int slicePerSegment = sliceNumber / sliceSegmentNumber;
int vertexNumber = 3 * slicePerSegment;
vertices = new float[3 * vertexNumber];
normals = new float[3 * vertexNumber];
texCoords = new float[2 * vertexNumber];
indices = new char[vertexNumber];
vertexCount = 0;
texCoordCount = 0;
indexCount = 0;
triangleCount = 0;
int sliceCounter = 0;
float stackPercentage0;
float stackPercentage1;
if (!top) {
stackPercentage0 = ((float) (stackNumber - 1) / stackNumber);
stackPercentage1 = 1.0f;
} else {
stackPercentage0 = (1.0f / stackNumber);
stackPercentage1 = 0.0f;
}
float t0 = stackPercentage0;
float t1 = stackPercentage1;
double theta0 = stackPercentage0 * Math.PI;
double theta1 = stackPercentage1 * Math.PI;
double cosTheta0 = Math.cos(theta0);
double sinTheta0 = Math.sin(theta0);
double cosTheta1 = Math.cos(theta1);
double sinTheta1 = Math.sin(theta1);
for (int slice = 0; slice < sliceNumber; slice++) {
float slicePercentage0 = ((float) (slice) / sliceNumber);
float slicePercentage1 = ((float) (slice + 1) / sliceNumber);
double phi0 = slicePercentage0 * 2.0 * Math.PI;
double phi1 = slicePercentage1 * 2.0 * Math.PI;
float s0, s1;
if (facingOut) {
s0 = 1 - slicePercentage0;
s1 = 1 - slicePercentage1;
} else {
s0 = slicePercentage0;
s1 = slicePercentage1;
}
float s2 = (s0 + s1) / 2.0f;
double cosPhi0 = Math.cos(phi0);
double sinPhi0 = Math.sin(phi0);
double cosPhi1 = Math.cos(phi1);
double sinPhi1 = Math.sin(phi1);
float x0 = (float) (sinTheta0 * cosPhi0);
float y0 = (float) cosTheta0;
float z0 = (float) (sinTheta0 * sinPhi0);
float x1 = (float) (sinTheta0 * cosPhi1);
float y1 = (float) cosTheta0;
float z1 = (float) (sinTheta0 * sinPhi1);
float x2 = (float) (sinTheta1 * cosPhi0);
float y2 = (float) cosTheta1;
float z2 = (float) (sinTheta1 * sinPhi0);
vertices[vertexCount + 0] = x0;
vertices[vertexCount + 1] = y0;
vertices[vertexCount + 2] = z0;
vertices[vertexCount + 3] = x1;
vertices[vertexCount + 4] = y1;
vertices[vertexCount + 5] = z1;
vertices[vertexCount + 6] = x2;
vertices[vertexCount + 7] = y2;
vertices[vertexCount + 8] = z2;
if (facingOut) {
normals[vertexCount + 0] = x0;
normals[vertexCount + 1] = y0;
normals[vertexCount + 2] = z0;
normals[vertexCount + 3] = x1;
normals[vertexCount + 4] = y1;
normals[vertexCount + 5] = z1;
normals[vertexCount + 6] = x2;
normals[vertexCount + 7] = y2;
normals[vertexCount + 8] = z2;
} else {
normals[vertexCount + 0] = -x0;
normals[vertexCount + 1] = -y0;
normals[vertexCount + 2] = -z0;
normals[vertexCount + 3] = -x1;
normals[vertexCount + 4] = -y1;
normals[vertexCount + 5] = -z1;
normals[vertexCount + 6] = -x2;
normals[vertexCount + 7] = -y2;
normals[vertexCount + 8] = -z2;
}
texCoords[texCoordCount + 0] = s0;
texCoords[texCoordCount + 1] = t0;
texCoords[texCoordCount + 2] = s1;
texCoords[texCoordCount + 3] = t0;
texCoords[texCoordCount + 4] = s2;
texCoords[texCoordCount + 5] = t1;
if ((facingOut && top) || (!facingOut && !top)) {
indices[indexCount + 0] = (char) (triangleCount + 1);
indices[indexCount + 1] = (char) (triangleCount + 0);
indices[indexCount + 2] = (char) (triangleCount + 2);
} else {
indices[indexCount + 0] = (char) (triangleCount + 0);
indices[indexCount + 1] = (char) (triangleCount + 1);
indices[indexCount + 2] = (char) (triangleCount + 2);
}
sliceCounter++;
if (sliceCounter == slicePerSegment) {
GVRMesh mesh = new GVRMesh(gvrContext, "float3 a_position float2 a_texcoord float3 a_normal");
mesh.setVertices(vertices);
mesh.setNormals(normals);
mesh.setTexCoords(texCoords);
mesh.setIndices(indices);
GVRSceneObject childObject = new GVRSceneObject(gvrContext, mesh, material);
addChildObject(childObject);
sliceCounter = 0;
vertexCount = 0;
texCoordCount = 0;
indexCount = 0;
triangleCount = 0;
} else {
vertexCount += 9;
texCoordCount += 6;
indexCount += 3;
triangleCount += 3;
}
}
}
private void createComplexBody(GVRContext gvrContext, int stackNumber,
int sliceNumber, boolean facingOut, GVRMaterial material,
int stackSegmentNumber, int sliceSegmentNumber) {
int stackPerSegment = (stackNumber - 2) / stackSegmentNumber;
int slicePerSegment = sliceNumber / sliceSegmentNumber;
int vertexNumber = 4 * stackPerSegment * slicePerSegment;
int triangleNumber = 6 * stackPerSegment * slicePerSegment;
vertices = new float[3 * vertexNumber];
normals = new float[3 * vertexNumber];
texCoords = new float[2 * vertexNumber];
indices = new char[triangleNumber];
vertexCount = 0;
texCoordCount = 0;
indexCount = 0;
triangleCount = 0;
for (int stackSegment = 0; stackSegment < stackSegmentNumber; stackSegment++) {
for (int sliceSegment = 0; sliceSegment < sliceSegmentNumber; sliceSegment++) {
for (int stack = stackSegment * stackPerSegment + 1; stack < (stackSegment+1) * stackPerSegment + 1; stack++) {
float stackPercentage0 = ((float) (stack) / stackNumber);
float stackPercentage1 = ((float) (stack + 1) / stackNumber);
float t0 = stackPercentage0;
float t1 = stackPercentage1;
double theta0 = stackPercentage0 * Math.PI;
double theta1 = stackPercentage1 * Math.PI;
double cosTheta0 = Math.cos(theta0);
double sinTheta0 = Math.sin(theta0);
double cosTheta1 = Math.cos(theta1);
double sinTheta1 = Math.sin(theta1);
for (int slice = sliceSegment * slicePerSegment; slice < (sliceSegment+1) * slicePerSegment; slice++) {
float slicePercentage0 = ((float) (slice) / sliceNumber);
float slicePercentage1 = ((float) (slice + 1) / sliceNumber);
double phi0 = slicePercentage0 * 2.0 * Math.PI;
double phi1 = slicePercentage1 * 2.0 * Math.PI;
float s0, s1;
if (facingOut) {
s0 = 1.0f - slicePercentage0;
s1 = 1.0f - slicePercentage1;
} else {
s0 = slicePercentage0;
s1 = slicePercentage1;
}
double cosPhi0 = Math.cos(phi0);
double sinPhi0 = Math.sin(phi0);
double cosPhi1 = Math.cos(phi1);
double sinPhi1 = Math.sin(phi1);
float x0 = (float) (sinTheta0 * cosPhi0);
float y0 = (float) cosTheta0;
float z0 = (float) (sinTheta0 * sinPhi0);
float x1 = (float) (sinTheta0 * cosPhi1);
float y1 = (float) cosTheta0;
float z1 = (float) (sinTheta0 * sinPhi1);
float x2 = (float) (sinTheta1 * cosPhi0);
float y2 = (float) cosTheta1;
float z2 = (float) (sinTheta1 * sinPhi0);
float x3 = (float) (sinTheta1 * cosPhi1);
float y3 = (float) cosTheta1;
float z3 = (float) (sinTheta1 * sinPhi1);
vertices[vertexCount + 0] = x0;
vertices[vertexCount + 1] = y0;
vertices[vertexCount + 2] = z0;
vertices[vertexCount + 3] = x1;
vertices[vertexCount + 4] = y1;
vertices[vertexCount + 5] = z1;
vertices[vertexCount + 6] = x2;
vertices[vertexCount + 7] = y2;
vertices[vertexCount + 8] = z2;
vertices[vertexCount + 9] = x3;
vertices[vertexCount + 10] = y3;
vertices[vertexCount + 11] = z3;
if (facingOut) {
normals[vertexCount + 0] = x0;
normals[vertexCount + 1] = y0;
normals[vertexCount + 2] = z0;
normals[vertexCount + 3] = x1;
normals[vertexCount + 4] = y1;
normals[vertexCount + 5] = z1;
normals[vertexCount + 6] = x2;
normals[vertexCount + 7] = y2;
normals[vertexCount + 8] = z2;
normals[vertexCount + 9] = x3;
normals[vertexCount + 10] = y3;
normals[vertexCount + 11] = z3;
} else {
normals[vertexCount + 0] = -x0;
normals[vertexCount + 1] = -y0;
normals[vertexCount + 2] = -z0;
normals[vertexCount + 3] = -x1;
normals[vertexCount + 4] = -y1;
normals[vertexCount + 5] = -z1;
normals[vertexCount + 6] = -x2;
normals[vertexCount + 7] = -y2;
normals[vertexCount + 8] = -z2;
normals[vertexCount + 9] = -x3;
normals[vertexCount + 10] = -y3;
normals[vertexCount + 11] = -z3;
}
texCoords[texCoordCount + 0] = s0;
texCoords[texCoordCount + 1] = t0;
texCoords[texCoordCount + 2] = s1;
texCoords[texCoordCount + 3] = t0;
texCoords[texCoordCount + 4] = s0;
texCoords[texCoordCount + 5] = t1;
texCoords[texCoordCount + 6] = s1;
texCoords[texCoordCount + 7] = t1;
// one quad looking from outside toward center
//
// @formatter:off
//
// s1 --> s0
//
// t0 1-----0
// | | |
// v | |
// t1 3-----2
//
// @formatter:on
//
// Note that tex_coord t increase from top to bottom because the
// texture image is loaded upside down.
if (facingOut) {
indices[indexCount + 0] = (char) (triangleCount + 0);
indices[indexCount + 1] = (char) (triangleCount + 1);
indices[indexCount + 2] = (char) (triangleCount + 2);
indices[indexCount + 3] = (char) (triangleCount + 2);
indices[indexCount + 4] = (char) (triangleCount + 1);
indices[indexCount + 5] = (char) (triangleCount + 3);
} else {
indices[indexCount + 0] = (char) (triangleCount + 0);
indices[indexCount + 1] = (char) (triangleCount + 2);
indices[indexCount + 2] = (char) (triangleCount + 1);