/
of3dPrimitives.cpp
1093 lines (875 loc) · 34.9 KB
/
of3dPrimitives.cpp
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
//
// of3dPrimitive.cpp
// openFrameworksLib
//
// Created by Nick Hardeman on 9/14/12.
//
//
#include "of3dPrimitives.h"
#include "ofGraphics.h"
#include "ofRectangle.h"
#include "ofVboMesh.h"
#include "ofTexture.h"
#include "of3dUtils.h"
using std::vector;
using std::shared_ptr;
of3dPrimitive::of3dPrimitive()
:usingVbo(true)
,mesh(new ofVboMesh)
{
setScale(1.0, 1.0, 1.0);
}
//----------------------------------------------------------
of3dPrimitive::~of3dPrimitive() {
}
//----------------------------------------------------------
of3dPrimitive::of3dPrimitive(const of3dPrimitive & mom):ofNode(mom){
texCoords = mom.texCoords;
usingVbo = mom.usingVbo;
if(usingVbo){
mesh = std::make_shared<ofVboMesh>();
}else{
mesh = std::make_shared<ofMesh>();
}
*mesh = *mom.mesh;
}
//----------------------------------------------------------
of3dPrimitive::of3dPrimitive(const ofMesh & mesh)
:usingVbo(true)
,mesh(new ofVboMesh(mesh)){
}
//----------------------------------------------------------
of3dPrimitive & of3dPrimitive::operator=(const of3dPrimitive & mom){
if(&mom!=this){
(*(ofNode*)this)=mom;
texCoords = mom.texCoords;
setUseVbo(mom.usingVbo);
*mesh = *mom.mesh;
}
return *this;
}
// GETTERS //
//----------------------------------------------------------
ofMesh* of3dPrimitive::getMeshPtr() {
return mesh.get();
}
//----------------------------------------------------------
ofMesh& of3dPrimitive::getMesh() {
return *mesh;
}
//----------------------------------------------------------
const ofMesh* of3dPrimitive::getMeshPtr() const{
return mesh.get();
}
//----------------------------------------------------------
const ofMesh& of3dPrimitive::getMesh() const{
return *mesh;
}
//----------------------------------------------------------
glm::vec4* of3dPrimitive::getTexCoordsPtr() {
return& texCoords;
}
//----------------------------------------------------------
glm::vec4& of3dPrimitive::getTexCoords() {
return texCoords;
}
//----------------------------------------------------------
const glm::vec4* of3dPrimitive::getTexCoordsPtr() const{
return& texCoords;
}
//----------------------------------------------------------
const glm::vec4& of3dPrimitive::getTexCoords() const{
return texCoords;
}
//----------------------------------------------------------
vector<ofIndexType> of3dPrimitive::getIndices( int startIndex, int endIndex ) const {
vector<ofIndexType> indices;
indices.assign( getMesh().getIndices().begin()+startIndex, getMesh().getIndices().begin()+endIndex );
return indices;
}
//----------------------------------------------------------
bool of3dPrimitive::hasScaling() const{
glm::vec3 scale = getScale();
return (scale.x != 1.f || scale.y != 1.f || scale.z != 1.f);
}
//----------------------------------------------------------
bool of3dPrimitive::hasNormalsEnabled() const {
return getMesh().hasNormals();
}
//----------------------------------------------------------
void of3dPrimitive::enableNormals() {
getMesh().enableNormals();
}
//----------------------------------------------------------
void of3dPrimitive::enableTextures() {
getMesh().enableTextures();
}
//----------------------------------------------------------
void of3dPrimitive::enableColors() {
getMesh().enableColors();
}
//----------------------------------------------------------
void of3dPrimitive::disableNormals() {
getMesh().disableNormals();
}
//----------------------------------------------------------
void of3dPrimitive::disableTextures() {
getMesh().disableTextures();
}
//----------------------------------------------------------
void of3dPrimitive::disableColors() {
getMesh().disableColors();
}
// SETTERS //
//----------------------------------------------------------
void of3dPrimitive::mapTexCoords( float u1, float v1, float u2, float v2 ) {
auto prevTcoord = getTexCoords();
for(std::size_t j = 0; j < getMesh().getNumTexCoords(); j++ ) {
auto tcoord = getMesh().getTexCoord(j);
tcoord.x = ofMap(tcoord.x, prevTcoord.x, prevTcoord.z, u1, u2);
tcoord.y = ofMap(tcoord.y, prevTcoord.y, prevTcoord.w, v1, v2);
getMesh().setTexCoord(j, tcoord);
}
texCoords = {u1, v1, u2, v2};
}
//----------------------------------------------------------
void of3dPrimitive::mapTexCoordsFromTexture( const ofTexture& inTexture ) {
bool bNormalized = true;
#ifndef TARGET_OPENGLES
bNormalized = (inTexture.getTextureData().textureTarget!=GL_TEXTURE_RECTANGLE_ARB);
#endif
const ofTextureData& tdata = inTexture.getTextureData();
if(bNormalized){
mapTexCoords( 0, 0, tdata.tex_t, tdata.tex_u );
}else{
mapTexCoords(0, 0, inTexture.getWidth(), inTexture.getHeight());
}
auto tcoords = getTexCoords();
mapTexCoords(tcoords.x, tcoords.y, tcoords.z, tcoords.w);
}
//----------------------------------------------------------
void of3dPrimitive::normalizeAndApplySavedTexCoords() {
auto tcoords = getTexCoords();
// when a new mesh is created, it uses normalized tex coords, we need to reset them
// but save the ones used previously //
texCoords = {0.f, 0.f, 1.f, 1.f};
mapTexCoords(tcoords.x, tcoords.y, tcoords.z, tcoords.w);
}
//--------------------------------------------------------------
void of3dPrimitive::drawVertices() const{
draw(OF_MESH_POINTS);
}
//--------------------------------------------------------------
void of3dPrimitive::drawWireframe() const{
draw(OF_MESH_WIREFRAME);
}
//--------------------------------------------------------------
void of3dPrimitive::drawFaces() const{
draw(OF_MESH_FILL);
}
//--------------------------------------------------------------
void of3dPrimitive::draw(ofPolyRenderMode renderType) const{
ofGetCurrentRenderer()->draw(*this, renderType);
}
//--------------------------------------------------------------
void of3dPrimitive::draw() const{
draw(OF_MESH_FILL);
}
//--------------------------------------------------------------
void of3dPrimitive::drawNormals(float length, bool bFaceNormals) const{
ofNode::transformGL(ofGetCurrentRenderer().get());
if(getMesh().usingNormals()) {
const auto& normals = getMesh().getNormals();
const auto& vertices = getMesh().getVertices();
glm::vec3 normal;
glm::vec3 vert;
normalsMesh.setMode( OF_PRIMITIVE_LINES );
normalsMesh.getVertices().resize( normals.size() * 2);
if(bFaceNormals) {
for(size_t i = 0; i < normals.size(); i++ ) {
if(i % 3 == 0) {
vert = (vertices[i]+vertices[i+1]+vertices[i+2]) / 3;
} else if(i % 3 == 1) {
vert = (vertices[i-1]+vertices[i]+vertices[i+1]) / 3;
} else if ( i % 3 == 2) {
vert = (vertices[i-2]+vertices[i-1]+vertices[i]) / 3;
}
normalsMesh.setVertex(i*2, vert);
normal = glm::normalize(toGlm(normals[i]));
normal *= length;
normalsMesh.setVertex(i*2+1, vert+normal);
}
} else {
for(size_t i = 0; i < normals.size(); i++) {
vert = vertices[i];
normal = glm::normalize(toGlm(normals[i]));
normalsMesh.setVertex( i*2, vert);
normal *= length;
normalsMesh.setVertex(i*2+1, vert+normal);
}
}
normalsMesh.draw();
} else {
ofLogWarning("of3dPrimitive") << "drawNormals(): mesh normals are disabled";
}
ofNode::restoreTransformGL(ofGetCurrentRenderer().get());
}
//--------------------------------------------------------------
void of3dPrimitive::drawAxes(float a_size) const{
ofNode::transformGL(ofGetCurrentRenderer().get());
ofDrawAxis(a_size);
ofNode::restoreTransformGL(ofGetCurrentRenderer().get());
}
//--------------------------------------------------------------
void of3dPrimitive::setUseVbo(bool useVbo){
if(useVbo!=usingVbo){
shared_ptr<ofMesh> newMesh;
if(useVbo){
newMesh = std::make_shared<ofVboMesh>();
}else{
newMesh = std::make_shared<ofMesh>();
}
*newMesh = *mesh;
mesh = newMesh;
}
usingVbo = useVbo;
}
//--------------------------------------------------------------
bool of3dPrimitive::isUsingVbo() const{
return usingVbo;
}
// PLANE PRIMITIVE //
//--------------------------------------------------------------
ofPlanePrimitive::ofPlanePrimitive() {
texCoords = {0.f, 0.f, 1.f, 1.f};
set( 200, 100, 6, 3);
}
//--------------------------------------------------------------
ofPlanePrimitive::ofPlanePrimitive(float width, float height, int columns, int rows, ofPrimitiveMode mode) {
texCoords = {0.f, 0.f, 1.f, 1.f};
set(width, height, columns, rows, mode);
}
//--------------------------------------------------------------
ofPlanePrimitive::~ofPlanePrimitive() {}
//--------------------------------------------------------------
void ofPlanePrimitive::set(float _width, float _height, int columns, int rows, ofPrimitiveMode mode) {
width = _width;
height = _height;
resolution = { columns, rows };
getMesh() = ofMesh::plane( getWidth(), getHeight(), getResolution().x, getResolution().y, mode );
normalizeAndApplySavedTexCoords();
}
//--------------------------------------------------------------
void ofPlanePrimitive::set( float _width, float height ) {
width = _width;
setHeight(height);
}
//--------------------------------------------------------------
void ofPlanePrimitive::setWidth( float _width ) {
width = _width;
setResolution( getResolution().x, getResolution().y );
}
//--------------------------------------------------------------
void ofPlanePrimitive::setHeight(float _height) {
height = _height;
setResolution( getResolution().x, getResolution().y );
}
//--------------------------------------------------------------
void ofPlanePrimitive::resizeToTexture( ofTexture& inTexture, float scale ) {
set(inTexture.getWidth() * scale, inTexture.getHeight() * scale);
mapTexCoordsFromTexture( inTexture );
}
//--------------------------------------------------------------
void ofPlanePrimitive::setColumns( int columns ) {
setResolution( columns, getNumRows() );
}
//--------------------------------------------------------------
void ofPlanePrimitive::setRows( int rows ) {
setResolution( getNumColumns(), rows );
}
//--------------------------------------------------------------
void ofPlanePrimitive::setResolution( int columns, int rows ) {
resolution = { columns, rows };
ofPrimitiveMode mode = getMesh().getMode();
set( getWidth(), getHeight(), getResolution().x, getResolution().y, mode );
}
//--------------------------------------------------------------
void ofPlanePrimitive::setMode(ofPrimitiveMode mode) {
ofPrimitiveMode currMode = getMesh().getMode();
if( mode != currMode )
set( getWidth(), getHeight(), getResolution().x, getResolution().y, mode );
}
//--------------------------------------------------------------
int ofPlanePrimitive::getNumColumns() const {
return (int)resolution.x;
}
//--------------------------------------------------------------
int ofPlanePrimitive::getNumRows() const {
return (int)resolution.y;
}
//--------------------------------------------------------------
glm::vec2 ofPlanePrimitive::getResolution() const {
return resolution;
}
//--------------------------------------------------------------
float ofPlanePrimitive::getWidth() const {
return width;
}
//--------------------------------------------------------------
float ofPlanePrimitive::getHeight() const {
return height;
}
// SPHERE PRIMITIVE //
//----------------------------------------------------------
ofSpherePrimitive::ofSpherePrimitive() {
texCoords = {0.f, 0.f, 1.f, 1.f};
radius = 20;
setResolution( 16 );
}
//----------------------------------------------------------
ofSpherePrimitive::ofSpherePrimitive( float _radius, int res, ofPrimitiveMode mode ) {
radius = _radius;
texCoords = {0.f, 0.f, 1.f, 1.f};
setResolution( res );
}
//----------------------------------------------------------
ofSpherePrimitive::~ofSpherePrimitive() {
}
//----------------------------------------------------------
void ofSpherePrimitive::set( float _radius, int res, ofPrimitiveMode mode ) {
radius = _radius;
resolution = res;
getMesh() = ofMesh::sphere( getRadius(), getResolution(), mode );
normalizeAndApplySavedTexCoords();
}
//----------------------------------------------------------
void ofSpherePrimitive::setResolution( int res ) {
resolution = res;
ofPrimitiveMode mode = getMesh().getMode();
set(getRadius(), getResolution(), mode );
}
//----------------------------------------------------------
void ofSpherePrimitive::setMode( ofPrimitiveMode mode ) {
ofPrimitiveMode currMode = getMesh().getMode();
if(currMode != mode)
set(getRadius(), getResolution(), mode );
}
//----------------------------------------------------------
void ofSpherePrimitive::setRadius(float _radius) {
radius = _radius;
setResolution( getResolution() );
}
//----------------------------------------------------------
float ofSpherePrimitive::getRadius() const {
return radius;
}
//----------------------------------------------------------
int ofSpherePrimitive::getResolution() const {
return resolution;
}
// ICO SPHERE //
//----------------------------------------------------------
ofIcoSpherePrimitive::ofIcoSpherePrimitive() {
texCoords = {0.f, 0.f, 1.f, 1.f};
radius = 20;
setResolution( 2 );
}
//----------------------------------------------------------
ofIcoSpherePrimitive::ofIcoSpherePrimitive( float _radius, int iterations ) {
texCoords = {0.f, 0.f, 1.f, 1.f};
radius = _radius;
setResolution( iterations );
}
//----------------------------------------------------------
ofIcoSpherePrimitive::~ofIcoSpherePrimitive() {
}
//----------------------------------------------------------
void ofIcoSpherePrimitive::set(float _radius, int res ) {
radius = _radius;
setResolution(res);
}
//----------------------------------------------------------
void ofIcoSpherePrimitive::setResolution( int iterations ) {
// store the number of iterations in the resolution //
resolution = iterations;
getMesh() = ofMesh::icosphere( getRadius(), getResolution() );
normalizeAndApplySavedTexCoords();
}
//----------------------------------------------------------
void ofIcoSpherePrimitive::setMode( ofPrimitiveMode mode ) {
// ofIcoSpherePrimitive only works with OF_PRIMITIVE_TRIANGLES //
setResolution( getResolution() );
}
//----------------------------------------------------------
void ofIcoSpherePrimitive::setRadius(float _radius) {
radius = _radius;
setResolution( getResolution() );
}
//----------------------------------------------------------
float ofIcoSpherePrimitive::getRadius() const {
return radius;
}
//----------------------------------------------------------
int ofIcoSpherePrimitive::getResolution() const {
return resolution;
}
//--------------------------------------------------------------
ofCylinderPrimitive::ofCylinderPrimitive() {
texCoords = {0.f, 0.f, 1.f, 1.f};
set( 60, 80, 6, 3, 2, true );
}
//--------------------------------------------------------------
ofCylinderPrimitive::ofCylinderPrimitive( float radius, float height, int radiusSegments, int heightSegments, int capSegments, bool bCapped, ofPrimitiveMode mode ) {
texCoords = {0.f, 0.f, 1.f, 1.f};
set( radius, height, radiusSegments, heightSegments, capSegments, bCapped, mode );
}
//--------------------------------------------------------------
ofCylinderPrimitive::~ofCylinderPrimitive() {}
//--------------------------------------------------------------
void ofCylinderPrimitive::set(float _radius, float _height, int radiusSegments, int heightSegments, int capSegments, bool _bCapped, ofPrimitiveMode mode) {
radius = _radius;
height = _height;
bCapped = _bCapped;
resolution = {radiusSegments, heightSegments, capSegments};
int resX = std::max(getResolution().x,0.0f);
int resY = std::max(getResolution().y-1,0.0f);
int resZ = std::max(getResolution().z-1,0.0f);
int indexStep = 2;
if(mode == OF_PRIMITIVE_TRIANGLES) {
indexStep = 6;
resX = std::max(radiusSegments+1,0);
resY = std::max(heightSegments+1,2);
resZ = std::max(capSegments+1,0);
}
if( mode == OF_PRIMITIVE_TRIANGLES ) {
// 0 -> top cap
strides[0][0] = 0;
// the first triangle is not added, so subtract 3
strides[0][1] = (resX-1) * (resZ-1) * indexStep - ((resX-1) * 3);
if( strides[0][1] < 0 ) {strides[0][1] = 0;}
// 1 -> cylinder //
if(bCapped) {
strides[1][0] = strides[0][0] + strides[0][1];
} else {
strides[1][0] = 0;
}
strides[1][1] = (resX-1) * (resY-1) * indexStep;
// 2 -> bottom cap
strides[2][0] = strides[1][0] + strides[1][1];
if( resZ > 2 ) {
strides[2][1] = (resX-1) * (resZ-1) * indexStep;
} else {
strides[2][1] = (resX-1) * (resZ-1) * 3;
}
} else {
// 0 -> top cap
strides[0][0] = 0;
strides[0][1] = (resX+1) * (resZ+1) * indexStep;
// 1 -> cylinder //
if(bCapped) {
strides[1][0] = strides[0][0] + strides[0][1];
} else {
strides[1][0] = 0;
}
strides[1][1] = (resX+1) * (resY+1) * indexStep;
// 2 -> bottom cap
strides[2][0] = strides[1][0] + strides[1][1];
strides[2][1] = (resX+1) * (resZ+1) * indexStep;
}
getMesh() = ofMesh::cylinder( getRadius(), getHeight(), getResolution().x, getResolution().y, getResolution().z, getCapped(), mode );
normalizeAndApplySavedTexCoords();
}
//--------------------------------------------------------------
void ofCylinderPrimitive::set( float _radius, float height, bool _bCapped ) {
radius = _radius;
bCapped = _bCapped;
setHeight( height );
}
//--------------------------------------------------------------
void ofCylinderPrimitive::setRadius( float _radius ) {
radius = _radius;
setResolution( getResolution().x, getResolution().y, getResolution().z );
}
//--------------------------------------------------------------
void ofCylinderPrimitive::setHeight( float _height ) {
height = _height;
setResolution(getResolution().x, getResolution().y, getResolution().z);
}
//--------------------------------------------------------------
void ofCylinderPrimitive::setCapped(bool _bCapped) {
bCapped = _bCapped;
setResolution( getResolution().x, getResolution().y, getResolution().z );
}
//--------------------------------------------------------------
void ofCylinderPrimitive::setResolutionRadius( int radiusRes ) {
setResolution( radiusRes, getResolutionHeight(), getResolutionCap() );
}
//--------------------------------------------------------------
void ofCylinderPrimitive::setResolutionHeight( int heightRes ) {
setResolution( getResolutionRadius(), heightRes, getResolutionCap() );
}
//--------------------------------------------------------------
void ofCylinderPrimitive::setResolutionCap( int capRes ) {
setResolution( getResolutionRadius(), getResolutionHeight(), capRes );
}
//--------------------------------------------------------------
void ofCylinderPrimitive::setResolution( int radiusSegments, int heightSegments, int capSegments ) {
ofPrimitiveMode mode = getMesh().getMode();
set( getRadius(), getHeight(), radiusSegments, heightSegments, capSegments, getCapped(), mode );
}
//----------------------------------------------------------
void ofCylinderPrimitive::setMode( ofPrimitiveMode mode ) {
ofPrimitiveMode currMode = getMesh().getMode();
if(currMode != mode) {
set( getRadius(), getHeight(), getResolution().x, getResolution().y, getResolution().z, getCapped(), mode );
}
}
//--------------------------------------------------------------
void ofCylinderPrimitive::setTopCapColor( ofColor color ) {
getMesh().setColorForIndices( strides[0][0], strides[0][0]+strides[0][1], color );
}
//--------------------------------------------------------------
void ofCylinderPrimitive::setCylinderColor( ofColor color ) {
getMesh().setColorForIndices( strides[1][0], strides[1][0]+strides[1][1], color );
}
//--------------------------------------------------------------
void ofCylinderPrimitive::setBottomCapColor( ofColor color ) {
getMesh().setColorForIndices( strides[2][0], strides[2][0]+strides[2][1], color );
}
//--------------------------------------------------------------
vector<ofIndexType> ofCylinderPrimitive::getTopCapIndices() const {
return of3dPrimitive::getIndices( strides[0][0], strides[0][0] + strides[0][1] );
}
//--------------------------------------------------------------
ofMesh ofCylinderPrimitive::getTopCapMesh() const {
return getMesh().getMeshForIndices( strides[0][0], strides[0][0]+strides[0][1] );
}
//--------------------------------------------------------------
vector<ofIndexType> ofCylinderPrimitive::getCylinderIndices() const {
return of3dPrimitive::getIndices( strides[1][0], strides[1][0] + strides[1][1] );
}
//--------------------------------------------------------------
ofMesh ofCylinderPrimitive::getCylinderMesh() const {
return getMesh().getMeshForIndices( strides[1][0], strides[1][0]+strides[1][1] );
}
//--------------------------------------------------------------
vector<ofIndexType> ofCylinderPrimitive::getBottomCapIndices() const {
return of3dPrimitive::getIndices( strides[2][0], strides[2][0] + strides[2][1] );
}
//--------------------------------------------------------------
ofMesh ofCylinderPrimitive::getBottomCapMesh() const {
return getMesh().getMeshForIndices( strides[2][0], strides[2][0]+strides[2][1] );
}
//--------------------------------------------------------------
int ofCylinderPrimitive::getResolutionRadius() const {
return (int)resolution.x;
}
//--------------------------------------------------------------
int ofCylinderPrimitive::getResolutionHeight() const {
return (int)resolution.y;
}
//--------------------------------------------------------------
int ofCylinderPrimitive::getResolutionCap() const {
return (int)resolution.z;
}
//--------------------------------------------------------------
glm::vec3 ofCylinderPrimitive::getResolution() const {
return resolution;
}
//--------------------------------------------------------------
float ofCylinderPrimitive::getHeight() const {
return height;
}
//--------------------------------------------------------------
float ofCylinderPrimitive::getRadius() const {
return radius;
}
//--------------------------------------------------------------
bool ofCylinderPrimitive::getCapped() const {
return bCapped;
}
// Cone Primitive //
//--------------------------------------------------------------
ofConePrimitive::ofConePrimitive() {
texCoords = {0.f, 0.f, 1.f, 1.f};
set( 20, 70, 8, 3, 2 );
}
//--------------------------------------------------------------
ofConePrimitive::ofConePrimitive( float radius, float height, int radiusSegments, int heightSegments, int capSegments, ofPrimitiveMode mode ) {
texCoords = {0.f, 0.f, 1.f, 1.f};
set( radius, height, radiusSegments, heightSegments, capSegments, mode );
}
//--------------------------------------------------------------
ofConePrimitive::~ofConePrimitive() {}
//--------------------------------------------------------------
void ofConePrimitive::set( float _radius, float _height, int radiusSegments, int heightSegments, int capSegments, ofPrimitiveMode mode ) {
radius = _radius;
height = _height;
resolution = {radiusSegments, heightSegments, capSegments};
int resX = std::max(getResolution().x, 0.0f);
int resY = std::max(getResolution().y-1, 0.0f);
int resZ = std::max(getResolution().z-1, 0.0f);
int indexStep = 2;
if(mode == OF_PRIMITIVE_TRIANGLES) {
indexStep = 6;
resX = std::max(radiusSegments+1, 0);
resY = std::max(heightSegments+1, 2);
resZ = std::max(capSegments+1, 0);
if(resZ < 2) {resZ = 0;}
}
if(mode == OF_PRIMITIVE_TRIANGLES) {
strides[0][0] = 0;
strides[0][1] = (resX-1) * (resY-1) * indexStep - ((resX-1) * 3);
if( strides[0][1] < 0 ) {strides[0][1] = 0;}
// now add the cap
strides[1][0] = strides[0][0] + strides[0][1];
strides[1][1] = (resZ-1) * (resX-1) * indexStep;
} else {
strides[0][0] = 0;
strides[0][1] = (resX+1)*(resY+1) * indexStep;
strides[1][0] = strides[0][0] + strides[0][1];
strides[1][1] = (resX+1)*(resZ+1) * indexStep;
}
getMesh() = ofMesh::cone( getRadius(), getHeight(), getResolution().x, getResolution().y, getResolution().z, mode );
normalizeAndApplySavedTexCoords();
}
//--------------------------------------------------------------
void ofConePrimitive::set( float _radius, float _height ) {
radius = _radius;
height = _height;
setResolution( getResolution().x, getResolution().y, getResolution().z );
}
//--------------------------------------------------------------
void ofConePrimitive::setResolutionRadius( int radiusRes ) {
setResolution( radiusRes, getResolutionHeight(), getResolutionCap() );
}
//--------------------------------------------------------------
void ofConePrimitive::setResolutionHeight( int heightRes ) {
setResolution( getResolutionRadius(), heightRes, getResolutionCap() );
}
//--------------------------------------------------------------
void ofConePrimitive::setResolutionCap( int capRes ) {
setResolution( getResolutionRadius(), getResolutionHeight(), capRes );
}
//--------------------------------------------------------------
void ofConePrimitive::setResolution( int radiusRes, int heightRes, int capRes ) {
ofPrimitiveMode mode = getMesh().getMode();
set( getRadius(), getHeight(), radiusRes, heightRes, capRes, mode );
}
//----------------------------------------------------------
void ofConePrimitive::setMode( ofPrimitiveMode mode ) {
ofPrimitiveMode currMode = getMesh().getMode();
if(currMode != mode)
set( getRadius(), getHeight(), getResolution().x, getResolution().y, getResolution().z, mode );
}
//--------------------------------------------------------------
void ofConePrimitive::setRadius( float _radius ) {
radius = _radius;
setResolution(getResolution().x, getResolution().y, getResolution().z);
}
//--------------------------------------------------------------
void ofConePrimitive::setHeight(float _height) {
height = _height;
setResolution(getResolution().x, getResolution().y, getResolution().z);
}
//--------------------------------------------------------------
void ofConePrimitive::setTopColor( ofColor color ) {
getMesh().setColorForIndices( strides[0][0], strides[0][0]+strides[0][1], color );
}
//--------------------------------------------------------------
void ofConePrimitive::setCapColor( ofColor color ) {
getMesh().setColorForIndices( strides[1][0], strides[1][0]+strides[1][1], color );
}
//--------------------------------------------------------------
vector<ofIndexType> ofConePrimitive::getConeIndices() const {
return of3dPrimitive::getIndices(strides[0][0], strides[0][0]+strides[0][1]);
}
//--------------------------------------------------------------
ofMesh ofConePrimitive::getConeMesh() const {
return getMesh().getMeshForIndices( strides[0][0], strides[0][0] + strides[0][1] );
}
//--------------------------------------------------------------
vector<ofIndexType> ofConePrimitive::getCapIndices() const {
return of3dPrimitive::getIndices( strides[1][0], strides[1][0] + strides[1][1] );
}
//--------------------------------------------------------------
ofMesh ofConePrimitive::getCapMesh() const {
return getMesh().getMeshForIndices( strides[1][0], strides[1][0] + strides[1][1] );
}
//--------------------------------------------------------------
int ofConePrimitive::getResolutionRadius() const {
return (int)resolution.x;
}
//--------------------------------------------------------------
int ofConePrimitive::getResolutionHeight() const {
return (int)resolution.y;
}
//--------------------------------------------------------------
int ofConePrimitive::getResolutionCap() const {
return (int)resolution.z;
}
//--------------------------------------------------------------
glm::vec3 ofConePrimitive::getResolution() const {
return resolution;
}
//--------------------------------------------------------------
float ofConePrimitive::getRadius() const {
return radius;
}
//--------------------------------------------------------------
float ofConePrimitive::getHeight() const {
return height;
}
// BOX PRIMITIVE //
//--------------------------------------------------------------
ofBoxPrimitive::ofBoxPrimitive() {
texCoords = {0.f, 0.f, 1.f, 1.f};
set(100, 100, 100, 2, 2, 2);
}
//--------------------------------------------------------------
ofBoxPrimitive::ofBoxPrimitive( float width, float height, float depth, int resWidth, int resHeight, int resDepth ) {
texCoords = {0.f, 0.f, 1.f, 1.f};
set(width, height, depth, resWidth, resHeight, resDepth );
}
//--------------------------------------------------------------
ofBoxPrimitive::~ofBoxPrimitive() {}
//--------------------------------------------------------------
void ofBoxPrimitive::set( float width, float height, float depth, int resWidth, int resHeight, int resDepth) {
size.x = width;
size.y = height;
size.z = depth;
resolution = {resWidth, resHeight, resDepth};
int resX = getResolution().x;
int resY = getResolution().y;
int resZ = getResolution().z;
//FRONT, resY, resX
strides[ SIDE_FRONT ][0] = 0;
strides[ SIDE_FRONT ][1] = (resY)*(resX)*6;
//RIGHT, resY, resZ
strides[ SIDE_RIGHT ][0] = strides[ SIDE_FRONT ][0] + strides[ SIDE_FRONT ][1];
strides[ SIDE_RIGHT ][1] = (resY)*(resZ)*6;
//LEFT, resY, resZ
strides[ SIDE_LEFT ][0] = strides[ SIDE_RIGHT ][0] + strides[ SIDE_RIGHT ][1];
strides[ SIDE_LEFT ][1] = (resY)*(resZ)*6;
//BACK, resY, resX
strides[ SIDE_BACK ][0] = strides[ SIDE_LEFT ][0] + strides[ SIDE_LEFT ][1];
strides[ SIDE_BACK ][1] = (resY)*(resX)*6;
//TOP, resZ, resX
strides[ SIDE_TOP ][0] = strides[ SIDE_BACK ][0] + strides[ SIDE_BACK ][1];
strides[ SIDE_TOP ][1] = (resZ)*(resX)*6;
//BOTTOM, resZ, resX
strides[ SIDE_BOTTOM ][0] = strides[ SIDE_TOP ][0]+strides[ SIDE_TOP ][1];
strides[ SIDE_BOTTOM ][1] = (resZ)*(resX)*6;
getMesh() = ofMesh::box( getWidth(), getHeight(), getDepth(), getResolution().x, getResolution().y, getResolution().z );
normalizeAndApplySavedTexCoords();
}
//--------------------------------------------------------------
void ofBoxPrimitive::set( float width, float height, float depth ) {
set( width, height, depth, getResolution().x, getResolution().y, getResolution().z );
}
//--------------------------------------------------------------
void ofBoxPrimitive::set( float size ) {
set( size, size, size );
}
//--------------------------------------------------------------
void ofBoxPrimitive::setWidth( float a_width ) {
size.x = a_width;
set( getWidth(), getHeight(), getDepth() );
}
//--------------------------------------------------------------
void ofBoxPrimitive::setHeight( float a_height ) {
size.y = a_height;
set( getWidth(), getHeight(), getDepth() );
}
//--------------------------------------------------------------
void ofBoxPrimitive::setDepth( float a_depth ) {
size.z = a_depth;
set( getWidth(), getHeight(), getDepth() );
}
//--------------------------------------------------------------
void ofBoxPrimitive::resizeToTexture( ofTexture& inTexture ) {
set(inTexture.getWidth(), inTexture.getHeight(), inTexture.getWidth());
mapTexCoordsFromTexture( inTexture );
}
//--------------------------------------------------------------
vector<ofIndexType> ofBoxPrimitive::getSideIndices( int sideIndex ) const {
if(sideIndex < 0 || sideIndex >= SIDES_TOTAL) {
ofLogWarning("ofBoxPrimitive") << "getSideIndices(): faceIndex out of bounds, returning SIDE_FRONT";
sideIndex = SIDE_FRONT;
}