Skip to content

Commit d61574d

Browse files
authored
Add get sub meshes for OF_PRIMITIVE_TRIANGLES for cone and cylinder (#7926)
#changelog #3dPrimitives
1 parent 37b6178 commit d61574d

2 files changed

Lines changed: 72 additions & 127 deletions

File tree

libs/openFrameworks/3d/of3dPrimitives.cpp

Lines changed: 71 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -542,32 +542,49 @@ void ofCylinderPrimitive::set(float _radius, float _height, int radiusSegments,
542542
int indexStep = 2;
543543
if(mode == OF_PRIMITIVE_TRIANGLES) {
544544
indexStep = 6;
545-
resX = std::max(resX,0);
545+
resX = std::max(radiusSegments+1,0);
546+
resY = std::max(heightSegments+1,2);
547+
resZ = std::max(capSegments+1,0);
546548
}
547549

548-
// 0 -> top cap
549-
strides[0][0] = 0;
550-
strides[0][1] = (resX+1) * (resZ+1) * indexStep;
551-
vertices[0][0] = 0;
552-
vertices[0][1] = (getResolution().x+1) * (getResolution().z+1);
553-
554-
// 1 -> cylinder //
555-
if(bCapped) {
556-
strides[1][0] = strides[0][0] + strides[0][1];
557-
vertices[1][0] = vertices[0][0] + vertices[0][1];
558-
} else {
559-
strides[1][0] = 0;
560-
vertices[1][0] = 0;
561-
}
562-
strides[1][1] = (resX+1) * (resY+1) * indexStep;
563-
vertices[1][1] = (getResolution().x+1) * (getResolution().y+1);
564-
565-
// 2 -> bottom cap
566-
strides[2][0] = strides[1][0] + strides[1][1];
567-
strides[2][1] = (resX+1) * (resZ+1) * indexStep;
568-
vertices[2][0] = vertices[1][0]+vertices[1][1];
569-
vertices[2][1] = (getResolution().x+1) * (getResolution().z+1);
570-
550+
if( mode == OF_PRIMITIVE_TRIANGLES ) {
551+
// 0 -> top cap
552+
strides[0][0] = 0;
553+
// the first triangle is not added, so subtract 3
554+
strides[0][1] = (resX-1) * (resZ-1) * indexStep - ((resX-1) * 3);
555+
if( strides[0][1] < 0 ) {strides[0][1] = 0;}
556+
557+
// 1 -> cylinder //
558+
if(bCapped) {
559+
strides[1][0] = strides[0][0] + strides[0][1];
560+
} else {
561+
strides[1][0] = 0;
562+
}
563+
strides[1][1] = (resX-1) * (resY-1) * indexStep;
564+
// 2 -> bottom cap
565+
strides[2][0] = strides[1][0] + strides[1][1];
566+
567+
if( resZ > 2 ) {
568+
strides[2][1] = (resX-1) * (resZ-1) * indexStep;
569+
} else {
570+
strides[2][1] = (resX-1) * (resZ-1) * 3;
571+
}
572+
} else {
573+
// 0 -> top cap
574+
strides[0][0] = 0;
575+
strides[0][1] = (resX+1) * (resZ+1) * indexStep;
576+
577+
// 1 -> cylinder //
578+
if(bCapped) {
579+
strides[1][0] = strides[0][0] + strides[0][1];
580+
} else {
581+
strides[1][0] = 0;
582+
}
583+
strides[1][1] = (resX+1) * (resY+1) * indexStep;
584+
// 2 -> bottom cap
585+
strides[2][0] = strides[1][0] + strides[1][1];
586+
strides[2][1] = (resX+1) * (resZ+1) * indexStep;
587+
}
571588

572589
getMesh() = ofMesh::cylinder( getRadius(), getHeight(), getResolution().x, getResolution().y, getResolution().z, getCapped(), mode );
573590

@@ -624,31 +641,23 @@ void ofCylinderPrimitive::setResolution( int radiusSegments, int heightSegments,
624641
//----------------------------------------------------------
625642
void ofCylinderPrimitive::setMode( ofPrimitiveMode mode ) {
626643
ofPrimitiveMode currMode = getMesh().getMode();
627-
if(currMode != mode)
628-
set( getRadius(), getHeight(), getResolution().x, getResolution().y, getResolution().z, getCapped(), mode );
644+
if(currMode != mode) {
645+
set( getRadius(), getHeight(), getResolution().x, getResolution().y, getResolution().z, getCapped(), mode );
646+
}
629647
}
630648

631649
//--------------------------------------------------------------
632650
void ofCylinderPrimitive::setTopCapColor( ofColor color ) {
633-
if(getMesh().getMode() != OF_PRIMITIVE_TRIANGLE_STRIP) {
634-
ofLogWarning("ofCylinderPrimitive") << "setTopCapColor(): must be in triangle strip mode";
635-
}
636651
getMesh().setColorForIndices( strides[0][0], strides[0][0]+strides[0][1], color );
637652
}
638653

639654
//--------------------------------------------------------------
640655
void ofCylinderPrimitive::setCylinderColor( ofColor color ) {
641-
if(getMesh().getMode() != OF_PRIMITIVE_TRIANGLE_STRIP) {
642-
ofLogWarning("ofCylinderPrimitive") << "setCylinderMode(): must be in triangle strip mode";
643-
}
644656
getMesh().setColorForIndices( strides[1][0], strides[1][0]+strides[1][1], color );
645657
}
646658

647659
//--------------------------------------------------------------
648660
void ofCylinderPrimitive::setBottomCapColor( ofColor color ) {
649-
if(getMesh().getMode() != OF_PRIMITIVE_TRIANGLE_STRIP) {
650-
ofLogWarning("ofCylinderPrimitive") << "setBottomCapColor(): must be in triangle strip mode";
651-
}
652661
getMesh().setColorForIndices( strides[2][0], strides[2][0]+strides[2][1], color );
653662
}
654663

@@ -659,48 +668,27 @@ vector<ofIndexType> ofCylinderPrimitive::getTopCapIndices() const {
659668

660669
//--------------------------------------------------------------
661670
ofMesh ofCylinderPrimitive::getTopCapMesh() const {
662-
if(getMesh().getMode() != OF_PRIMITIVE_TRIANGLE_STRIP) {
663-
ofLogWarning("ofCylinderPrimitive") << "getTopCapMesh(): must be in triangle strip mode";
664-
return ofMesh();
665-
}
666-
return getMesh().getMeshForIndices( strides[0][0], strides[0][0]+strides[0][1],
667-
vertices[0][0], vertices[0][0]+vertices[0][1] );
671+
return getMesh().getMeshForIndices( strides[0][0], strides[0][0]+strides[0][1] );
668672
}
669673

670674
//--------------------------------------------------------------
671675
vector<ofIndexType> ofCylinderPrimitive::getCylinderIndices() const {
672-
if(getMesh().getMode() != OF_PRIMITIVE_TRIANGLE_STRIP) {
673-
ofLogWarning("ofCylinderPrimitive") << "getCylinderIndices(): must be in triangle strip mode";
674-
}
675676
return of3dPrimitive::getIndices( strides[1][0], strides[1][0] + strides[1][1] );
676677
}
677678

678679
//--------------------------------------------------------------
679680
ofMesh ofCylinderPrimitive::getCylinderMesh() const {
680-
if(getMesh().getMode() != OF_PRIMITIVE_TRIANGLE_STRIP) {
681-
ofLogWarning("ofCylinderPrimitive") << "setCylinderMesh(): must be in triangle strip mode";
682-
return ofMesh();
683-
}
684-
return getMesh().getMeshForIndices( strides[1][0], strides[1][0]+strides[1][1],
685-
vertices[1][0], vertices[1][0]+vertices[1][1] );
681+
return getMesh().getMeshForIndices( strides[1][0], strides[1][0]+strides[1][1] );
686682
}
687683

688684
//--------------------------------------------------------------
689685
vector<ofIndexType> ofCylinderPrimitive::getBottomCapIndices() const {
690-
if(getMesh().getMode() != OF_PRIMITIVE_TRIANGLE_STRIP) {
691-
ofLogWarning("ofCylinderPrimitive") << "getBottomCapIndices(): must be in triangle strip mode";
692-
}
693686
return of3dPrimitive::getIndices( strides[2][0], strides[2][0] + strides[2][1] );
694687
}
695688

696689
//--------------------------------------------------------------
697690
ofMesh ofCylinderPrimitive::getBottomCapMesh() const {
698-
if(getMesh().getMode() != OF_PRIMITIVE_TRIANGLE_STRIP) {
699-
ofLogWarning("ofCylinderPrimitive") << "getBottomCapMesh(): must be in triangle strip mode";
700-
return ofMesh();
701-
}
702-
return getMesh().getMeshForIndices( strides[2][0], strides[2][0]+strides[2][1],
703-
vertices[2][0], vertices[2][0]+vertices[2][1] );
691+
return getMesh().getMeshForIndices( strides[2][0], strides[2][0]+strides[2][1] );
704692
}
705693

706694
//--------------------------------------------------------------
@@ -771,18 +759,28 @@ void ofConePrimitive::set( float _radius, float _height, int radiusSegments, int
771759
int indexStep = 2;
772760
if(mode == OF_PRIMITIVE_TRIANGLES) {
773761
indexStep = 6;
774-
resX = std::max(resX-1, 0);
762+
resX = std::max(radiusSegments+1, 0);
763+
resY = std::max(heightSegments+1, 2);
764+
resZ = std::max(capSegments+1, 0);
765+
if(resZ < 2) {resZ = 0;}
775766
}
776-
777-
strides[ 0 ][0] = 0;
778-
strides[ 0 ][1] = (resX+1)*(resY+1) * indexStep;
779-
vertices[0][0] = 0;
780-
vertices[0][1] = (getResolution().x+1) * (getResolution().y+1);
781-
782-
strides[ 1 ][0] = strides[ 0 ][0] + strides[ 0 ][1];
783-
strides[ 1 ][1] = (resX+1)*(resZ+1) * indexStep;
784-
vertices[1][0] = vertices[0][0] + vertices[0][1];
785-
vertices[1][1] = (getResolution().x+1) * (getResolution().z+1);
767+
768+
if(mode == OF_PRIMITIVE_TRIANGLES) {
769+
strides[0][0] = 0;
770+
strides[0][1] = (resX-1) * (resY-1) * indexStep - ((resX-1) * 3);
771+
if( strides[0][1] < 0 ) {strides[0][1] = 0;}
772+
773+
// now add the cap
774+
strides[1][0] = strides[0][0] + strides[0][1];
775+
strides[1][1] = (resZ-1) * (resX-1) * indexStep;
776+
777+
} else {
778+
strides[0][0] = 0;
779+
strides[0][1] = (resX+1)*(resY+1) * indexStep;
780+
781+
strides[1][0] = strides[0][0] + strides[0][1];
782+
strides[1][1] = (resX+1)*(resZ+1) * indexStep;
783+
}
786784

787785
getMesh() = ofMesh::cone( getRadius(), getHeight(), getResolution().x, getResolution().y, getResolution().z, mode );
788786

@@ -839,62 +837,32 @@ void ofConePrimitive::setHeight(float _height) {
839837

840838
//--------------------------------------------------------------
841839
void ofConePrimitive::setTopColor( ofColor color ) {
842-
if(getMesh().getMode() != OF_PRIMITIVE_TRIANGLE_STRIP) {
843-
ofLogWarning("ofConePrimitive") << "setTopColor(): must be in triangle strip mode";
844-
}
845840
getMesh().setColorForIndices( strides[0][0], strides[0][0]+strides[0][1], color );
846841
}
847842

848843
//--------------------------------------------------------------
849844
void ofConePrimitive::setCapColor( ofColor color ) {
850-
if(getMesh().getMode() != OF_PRIMITIVE_TRIANGLE_STRIP) {
851-
ofLogWarning("ofConePrimitive") << "setCapColor(): must be in triangle strip mode";
852-
}
853845
getMesh().setColorForIndices( strides[1][0], strides[1][0]+strides[1][1], color );
854846
}
855847

856848
//--------------------------------------------------------------
857849
vector<ofIndexType> ofConePrimitive::getConeIndices() const {
858-
if(getMesh().getMode() != OF_PRIMITIVE_TRIANGLE_STRIP) {
859-
ofLogWarning("ofConePrimitive") << "getConeIndices(): must be in triangle strip mode";
860-
}
861850
return of3dPrimitive::getIndices(strides[0][0], strides[0][0]+strides[0][1]);
862851
}
863852

864853
//--------------------------------------------------------------
865854
ofMesh ofConePrimitive::getConeMesh() const {
866-
int startIndex = strides[0][0];
867-
int endIndex = startIndex + strides[0][1];
868-
869-
int startVertIndex = vertices[0][0];
870-
int endVertIndex = startVertIndex + vertices[0][1];
871-
if(getMesh().getMode() != OF_PRIMITIVE_TRIANGLE_STRIP) {
872-
ofLogWarning("ofConePrimitive") << "getConeMesh(): must be in triangle strip mode";
873-
return ofMesh();
874-
}
875-
return getMesh().getMeshForIndices( startIndex, endIndex, startVertIndex, endVertIndex );
855+
return getMesh().getMeshForIndices( strides[0][0], strides[0][0] + strides[0][1] );
876856
}
877857

878858
//--------------------------------------------------------------
879859
vector<ofIndexType> ofConePrimitive::getCapIndices() const {
880-
if(getMesh().getMode() != OF_PRIMITIVE_TRIANGLE_STRIP) {
881-
ofLogWarning("ofConePrimitive") << "getCapIndices(): must be in triangle strip mode";
882-
}
883860
return of3dPrimitive::getIndices( strides[1][0], strides[1][0] + strides[1][1] );
884861
}
885862

886863
//--------------------------------------------------------------
887864
ofMesh ofConePrimitive::getCapMesh() const {
888-
int startIndex = strides[1][0];
889-
int endIndex = startIndex + strides[1][1];
890-
891-
int startVertIndex = vertices[1][0];
892-
int endVertIndex = startVertIndex + vertices[1][1];
893-
if(getMesh().getMode() != OF_PRIMITIVE_TRIANGLE_STRIP) {
894-
ofLogWarning("ofConePrimitive") << "getCapMesh(): must be in triangle strip mode";
895-
return ofMesh();
896-
}
897-
return getMesh().getMeshForIndices( startIndex, endIndex, startVertIndex, endVertIndex );
865+
return getMesh().getMeshForIndices( strides[1][0], strides[1][0] + strides[1][1] );
898866
}
899867

900868
//--------------------------------------------------------------
@@ -963,38 +931,26 @@ void ofBoxPrimitive::set( float width, float height, float depth, int resWidth,
963931
//FRONT, resY, resX
964932
strides[ SIDE_FRONT ][0] = 0;
965933
strides[ SIDE_FRONT ][1] = (resY)*(resX)*6;
966-
vertices[SIDE_FRONT][0] = 0;
967-
vertices[SIDE_FRONT][1] = (resX+1) * (resY+1);
968934

969935
//RIGHT, resY, resZ
970936
strides[ SIDE_RIGHT ][0] = strides[ SIDE_FRONT ][0] + strides[ SIDE_FRONT ][1];
971937
strides[ SIDE_RIGHT ][1] = (resY)*(resZ)*6;
972-
vertices[SIDE_RIGHT][0] = vertices[SIDE_FRONT][0] + vertices[SIDE_FRONT][1];
973-
vertices[SIDE_RIGHT][1] = (resY+1) * (resZ+1);
974938

975939
//LEFT, resY, resZ
976940
strides[ SIDE_LEFT ][0] = strides[ SIDE_RIGHT ][0] + strides[ SIDE_RIGHT ][1];
977941
strides[ SIDE_LEFT ][1] = (resY)*(resZ)*6;
978-
vertices[SIDE_LEFT][0] = vertices[SIDE_RIGHT][0] + vertices[SIDE_RIGHT][1];
979-
vertices[SIDE_LEFT][1] = (resY+1) * (resZ+1);
980942

981943
//BACK, resY, resX
982944
strides[ SIDE_BACK ][0] = strides[ SIDE_LEFT ][0] + strides[ SIDE_LEFT ][1];
983945
strides[ SIDE_BACK ][1] = (resY)*(resX)*6;
984-
vertices[SIDE_BACK][0] = vertices[SIDE_LEFT][0] + vertices[SIDE_LEFT][1];
985-
vertices[SIDE_BACK][1] = (resY+1) * (resZ+1);
986946

987947
//TOP, resZ, resX
988948
strides[ SIDE_TOP ][0] = strides[ SIDE_BACK ][0] + strides[ SIDE_BACK ][1];
989949
strides[ SIDE_TOP ][1] = (resZ)*(resX)*6;
990-
vertices[SIDE_TOP][0] = vertices[SIDE_BACK][0] + vertices[SIDE_BACK][1];
991-
vertices[SIDE_TOP][1] = (resY+1) * (resZ+1);
992950

993951
//BOTTOM, resZ, resX
994952
strides[ SIDE_BOTTOM ][0] = strides[ SIDE_TOP ][0]+strides[ SIDE_TOP ][1];
995953
strides[ SIDE_BOTTOM ][1] = (resZ)*(resX)*6;
996-
vertices[SIDE_BOTTOM][0] = vertices[SIDE_TOP][0] + vertices[SIDE_TOP][1];
997-
vertices[SIDE_BOTTOM][1] = (resY+1) * (resZ+1);
998954

999955
getMesh() = ofMesh::box( getWidth(), getHeight(), getDepth(), getResolution().x, getResolution().y, getResolution().z );
1000956

@@ -1048,18 +1004,11 @@ vector<ofIndexType> ofBoxPrimitive::getSideIndices( int sideIndex ) const {
10481004

10491005
//--------------------------------------------------------------
10501006
ofMesh ofBoxPrimitive::getSideMesh( int sideIndex ) const {
1051-
10521007
if(sideIndex < 0 || sideIndex > SIDES_TOTAL) {
10531008
ofLogWarning("ofBoxPrimitive") << "getSideMesh(): faceIndex out of bounds, using SIDE_FRONT";
10541009
sideIndex = SIDE_FRONT;
10551010
}
1056-
int startIndex = strides[sideIndex][0];
1057-
int endIndex = startIndex+strides[sideIndex][1];
1058-
1059-
int startVertIndex = vertices[sideIndex][0];
1060-
int endVertIndex = startVertIndex + vertices[sideIndex][1];
1061-
1062-
return getMesh().getMeshForIndices( startIndex, endIndex, startVertIndex, endVertIndex );
1011+
return getMesh().getMeshForIndices( strides[sideIndex][0], strides[sideIndex][0]+strides[sideIndex][1] );
10631012
}
10641013

10651014
//--------------------------------------------------------------

libs/openFrameworks/3d/of3dPrimitives.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,6 @@ class ofCylinderPrimitive : public of3dPrimitive {
358358
float height;
359359
bool bCapped;
360360
int strides[3][2];
361-
int vertices[3][2];
362361
glm::vec3 resolution;
363362
};
364363

@@ -381,7 +380,7 @@ class ofCylinderPrimitive : public of3dPrimitive {
381380
/// cone.rotate(spinX, 1.0, 0.0, 0.0);
382381
/// cone.rotate(spinY, 0, 1.0, 0.0);
383382
///
384-
/// // get all the faces from the cpme, handy when you want to copy
383+
/// // get all the faces from the cone, handy when you want to copy
385384
/// // individual vertices or tweak them a little ;)
386385
/// vector<ofMeshFace> triangles = cone.getMesh().getUniqueFaces();
387386
///
@@ -462,7 +461,6 @@ class ofConePrimitive : public of3dPrimitive {
462461
glm::vec3 resolution;
463462

464463
int strides[2][2];
465-
int vertices[2][2];
466464
};
467465

468466

@@ -561,6 +559,4 @@ class ofBoxPrimitive : public of3dPrimitive {
561559
glm::vec3 resolution;
562560
// indices strides for faces //
563561
int strides[6][2];
564-
int vertices[6][2];
565-
566562
};

0 commit comments

Comments
 (0)