framework2

Форк
0
1144 строки · 38.4 Кб
1
//
2
//  of3dPrimitive.cpp
3
//  openFrameworksLib
4
//
5
//  Created by Nick Hardeman on 9/14/12.
6
//
7
//
8

9
#include "of3dPrimitives.h"
10
#include "ofGraphics.h"
11
#include "ofRectangle.h"
12
#include "ofVboMesh.h"
13
#include "ofTexture.h"
14
#include "of3dUtils.h"
15

16
using std::vector;
17
using std::shared_ptr;
18

19
of3dPrimitive::of3dPrimitive()
20
:usingVbo(true)
21
,mesh(new ofVboMesh)
22
{
23
    setScale(1.0, 1.0, 1.0);
24
}
25

26
//----------------------------------------------------------
27
of3dPrimitive::~of3dPrimitive() {
28
    
29
}
30

31
//----------------------------------------------------------
32
of3dPrimitive::of3dPrimitive(const of3dPrimitive & mom):ofNode(mom){
33
    texCoords = mom.texCoords;
34
    usingVbo = mom.usingVbo;
35
	if(usingVbo){
36
		mesh = std::make_shared<ofVboMesh>();
37
	}else{
38
		mesh = std::make_shared<ofMesh>();
39
	}
40
	*mesh = *mom.mesh;
41
}
42

43
//----------------------------------------------------------
44
of3dPrimitive::of3dPrimitive(const ofMesh & mesh)
45
:usingVbo(true)
46
,mesh(new ofVboMesh(mesh)){
47

48
}
49

50
//----------------------------------------------------------
51
of3dPrimitive & of3dPrimitive::operator=(const of3dPrimitive & mom){
52
	if(&mom!=this){
53
		(*(ofNode*)this)=mom;
54
		texCoords = mom.texCoords;
55
		setUseVbo(mom.usingVbo);
56
		*mesh = *mom.mesh;
57
	}
58
    return *this;
59
}
60

61
// GETTERS //
62
//----------------------------------------------------------
63
ofMesh* of3dPrimitive::getMeshPtr() {
64
    return mesh.get();
65
}
66

67
//----------------------------------------------------------
68
ofMesh& of3dPrimitive::getMesh() {
69
    return *mesh;
70
}
71

72
//----------------------------------------------------------
73
const ofMesh* of3dPrimitive::getMeshPtr() const{
74
    return mesh.get();
75
}
76

77
//----------------------------------------------------------
78
const ofMesh& of3dPrimitive::getMesh() const{
79
    return *mesh;
80
}
81

82
//----------------------------------------------------------
83
glm::vec4* of3dPrimitive::getTexCoordsPtr() {
84
    return& texCoords;
85
}
86

87
//----------------------------------------------------------
88
glm::vec4& of3dPrimitive::getTexCoords() {
89
    return texCoords;
90
}
91

92
//----------------------------------------------------------
93
const glm::vec4* of3dPrimitive::getTexCoordsPtr() const{
94
    return& texCoords;
95
}
96

97
//----------------------------------------------------------
98
const glm::vec4& of3dPrimitive::getTexCoords() const{
99
    return texCoords;
100
}
101

102
//----------------------------------------------------------
103
vector<ofIndexType> of3dPrimitive::getIndices( int startIndex, int endIndex ) const {
104
    vector<ofIndexType> indices;
105
    indices.assign( getMesh().getIndices().begin()+startIndex, getMesh().getIndices().begin()+endIndex );
106
    return indices;
107
}
108

109

110
//----------------------------------------------------------
111
bool of3dPrimitive::hasScaling()  const{
112
	glm::vec3 scale = getScale();
113
    return (scale.x != 1.f || scale.y != 1.f || scale.z != 1.f);
114
}
115
//----------------------------------------------------------
116
bool of3dPrimitive::hasNormalsEnabled() const {
117
    return getMesh().hasNormals();
118
}
119

120
//----------------------------------------------------------
121
void of3dPrimitive::enableNormals() {
122
    getMesh().enableNormals();
123
}
124
//----------------------------------------------------------
125
void of3dPrimitive::enableTextures() {
126
    getMesh().enableTextures();
127
}
128
//----------------------------------------------------------
129
void of3dPrimitive::enableColors() {
130
    getMesh().enableColors();
131
}
132
//----------------------------------------------------------
133
void of3dPrimitive::disableNormals() {
134
    getMesh().disableNormals();
135
}
136
//----------------------------------------------------------
137
void of3dPrimitive::disableTextures() {
138
    getMesh().disableTextures();
139
}
140
//----------------------------------------------------------
141
void of3dPrimitive::disableColors() {
142
    getMesh().disableColors();
143
}
144

145
// SETTERS //
146

147
//----------------------------------------------------------
148
void of3dPrimitive::mapTexCoords( float u1, float v1, float u2, float v2 ) {
149
	
150
	auto prevTcoord = getTexCoords();
151
    
152
	for(std::size_t j = 0; j < getMesh().getNumTexCoords(); j++ ) {
153
		auto tcoord = getMesh().getTexCoord(j);
154
        tcoord.x = ofMap(tcoord.x, prevTcoord.x, prevTcoord.z, u1, u2);
155
        tcoord.y = ofMap(tcoord.y, prevTcoord.y, prevTcoord.w, v1, v2);
156
        
157
        getMesh().setTexCoord(j, tcoord);
158
    }
159
    
160
	texCoords = {u1, v1, u2, v2};
161
}
162

163
//----------------------------------------------------------
164
void of3dPrimitive::mapTexCoordsFromTexture( const ofTexture& inTexture ) {
165
    bool bNormalized = true;
166
#ifndef TARGET_OPENGLES
167
    bNormalized = (inTexture.getTextureData().textureTarget!=GL_TEXTURE_RECTANGLE_ARB);
168
#endif
169
    
170
    const ofTextureData& tdata = inTexture.getTextureData();
171
	if(bNormalized){
172
        mapTexCoords( 0, 0, tdata.tex_t, tdata.tex_u );
173
	}else{
174
        mapTexCoords(0, 0, inTexture.getWidth(), inTexture.getHeight());
175
	}
176
    
177
	auto tcoords = getTexCoords();
178
	mapTexCoords(tcoords.x, tcoords.y, tcoords.z, tcoords.w);
179
}
180

181
//----------------------------------------------------------
182
void of3dPrimitive::normalizeAndApplySavedTexCoords() {
183
	auto tcoords = getTexCoords();
184
    // when a new mesh is created, it uses normalized tex coords, we need to reset them
185
    // but save the ones used previously //
186
	texCoords = {0.f, 0.f, 1.f, 1.f};
187
    mapTexCoords(tcoords.x, tcoords.y, tcoords.z, tcoords.w);
188
}
189

190
//--------------------------------------------------------------
191
void of3dPrimitive::drawVertices()  const{
192
	draw(OF_MESH_POINTS);
193
}
194

195
//--------------------------------------------------------------
196
void of3dPrimitive::drawWireframe()  const{
197
	draw(OF_MESH_WIREFRAME);
198
}
199

200
//--------------------------------------------------------------
201
void of3dPrimitive::drawFaces()  const{
202
	draw(OF_MESH_FILL);
203
}
204

205
//--------------------------------------------------------------
206
void of3dPrimitive::draw(ofPolyRenderMode renderType) const{
207
    ofGetCurrentRenderer()->draw(*this, renderType);
208
}
209

210
//--------------------------------------------------------------
211
void of3dPrimitive::draw() const{
212
	draw(OF_MESH_FILL);
213
}
214

215
//--------------------------------------------------------------
216
void of3dPrimitive::drawNormals(float length, bool bFaceNormals) const{
217
    ofNode::transformGL(ofGetCurrentRenderer().get());
218
    
219
    if(getMesh().usingNormals()) {
220
		const auto& normals    = getMesh().getNormals();
221
		const auto& vertices   = getMesh().getVertices();
222
		glm::vec3 normal;
223
		glm::vec3 vert;
224
        
225
        normalsMesh.setMode( OF_PRIMITIVE_LINES );
226
        normalsMesh.getVertices().resize( normals.size() * 2);
227
        
228
        if(bFaceNormals) {
229
			for(size_t i = 0; i < normals.size(); i++ ) {
230
                if(i % 3 == 0) {
231
                    vert = (vertices[i]+vertices[i+1]+vertices[i+2]) / 3;
232
                } else if(i % 3 == 1) {
233
                    vert = (vertices[i-1]+vertices[i]+vertices[i+1]) / 3;
234
                } else if ( i % 3 == 2) {
235
                    vert = (vertices[i-2]+vertices[i-1]+vertices[i]) / 3;
236
                }
237
                normalsMesh.setVertex(i*2, vert);
238
				normal = glm::normalize(toGlm(normals[i]));
239
                normal *= length;
240
				normalsMesh.setVertex(i*2+1, vert+normal);
241
            }
242
        } else {
243
			for(size_t i = 0; i < normals.size(); i++) {
244
                vert = vertices[i];
245
				normal = glm::normalize(toGlm(normals[i]));
246
                normalsMesh.setVertex( i*2, vert);
247
                normal *= length;
248
				normalsMesh.setVertex(i*2+1, vert+normal);
249
            }
250
        }
251
        normalsMesh.draw();
252
    } else {
253
        ofLogWarning("of3dPrimitive") << "drawNormals(): mesh normals are disabled";
254
    }
255
    
256
    
257
    ofNode::restoreTransformGL(ofGetCurrentRenderer().get());
258
}
259

260
//--------------------------------------------------------------
261
void of3dPrimitive::drawAxes(float a_size)  const{
262
    ofNode::transformGL(ofGetCurrentRenderer().get());
263
    ofDrawAxis(a_size);
264
    ofNode::restoreTransformGL(ofGetCurrentRenderer().get());
265
}
266

267
//--------------------------------------------------------------
268
void of3dPrimitive::setUseVbo(bool useVbo){
269
	if(useVbo!=usingVbo){
270
		shared_ptr<ofMesh> newMesh;
271
		if(useVbo){
272
			newMesh = std::make_shared<ofVboMesh>();
273
		}else{
274
			newMesh = std::make_shared<ofMesh>();
275
		}
276
		*newMesh = *mesh;
277
		mesh = newMesh;
278
	}
279
	usingVbo = useVbo;
280
}
281

282
//--------------------------------------------------------------
283
bool of3dPrimitive::isUsingVbo() const{
284
	return usingVbo;
285
}
286

287
// PLANE PRIMITIVE //
288
//--------------------------------------------------------------
289
ofPlanePrimitive::ofPlanePrimitive() {
290
	texCoords = {0.f, 0.f, 1.f, 1.f};
291
    set( 200, 100, 6, 3);
292
}
293

294
//--------------------------------------------------------------
295
ofPlanePrimitive::ofPlanePrimitive(float width, float height, int columns, int rows, ofPrimitiveMode mode) {
296
	texCoords = {0.f, 0.f, 1.f, 1.f};
297
    set(width, height, columns, rows, mode);
298
}
299

300
//--------------------------------------------------------------
301
ofPlanePrimitive::~ofPlanePrimitive() {}
302

303
//--------------------------------------------------------------
304
void ofPlanePrimitive::set(float _width, float _height, int columns, int rows, ofPrimitiveMode mode) {
305
    
306
    width  = _width;
307
    height = _height;
308
	resolution = { columns, rows };
309
    
310
    getMesh() = ofMesh::plane( getWidth(), getHeight(), getResolution().x, getResolution().y, mode );
311
    
312
    normalizeAndApplySavedTexCoords();
313
    
314
}
315

316
//--------------------------------------------------------------
317
void ofPlanePrimitive::set( float _width, float height ) {
318
    width = _width;
319
    setHeight(height);
320
}
321

322
//--------------------------------------------------------------
323
void ofPlanePrimitive::setWidth( float _width ) {
324
    width = _width;
325
    setResolution( getResolution().x, getResolution().y );
326
}
327

328
//--------------------------------------------------------------
329
void ofPlanePrimitive::setHeight(float _height) {
330
    height = _height;
331
    setResolution( getResolution().x, getResolution().y );
332
}
333

334
//--------------------------------------------------------------
335
void ofPlanePrimitive::resizeToTexture( ofTexture& inTexture, float scale ) {
336
    set(inTexture.getWidth() * scale, inTexture.getHeight() * scale);
337
    mapTexCoordsFromTexture( inTexture );
338
}
339

340
//--------------------------------------------------------------
341
void ofPlanePrimitive::setColumns( int columns ) {
342
    setResolution( columns, getNumRows() );
343
}
344

345
//--------------------------------------------------------------
346
void ofPlanePrimitive::setRows( int rows ) {
347
    setResolution( getNumColumns(), rows );
348
}
349

350
//--------------------------------------------------------------
351
void ofPlanePrimitive::setResolution( int columns, int rows ) {
352
	resolution = { columns, rows };
353
    ofPrimitiveMode mode = getMesh().getMode();
354
    
355
    set( getWidth(), getHeight(), getResolution().x, getResolution().y, mode );
356
}
357

358
//--------------------------------------------------------------
359
void ofPlanePrimitive::setMode(ofPrimitiveMode mode) {
360
    ofPrimitiveMode currMode = getMesh().getMode();
361
    
362
    if( mode != currMode )
363
        set( getWidth(), getHeight(), getResolution().x, getResolution().y, mode );
364
}
365

366
//--------------------------------------------------------------
367
int ofPlanePrimitive::getNumColumns() const {
368
    return (int)resolution.x;
369
}
370

371
//--------------------------------------------------------------
372
int ofPlanePrimitive::getNumRows() const {
373
    return (int)resolution.y;
374
}
375

376
//--------------------------------------------------------------
377
glm::vec2 ofPlanePrimitive::getResolution() const {
378
    return resolution;
379
}
380

381
//--------------------------------------------------------------
382
float ofPlanePrimitive::getWidth() const {
383
    return width;
384
}
385

386
//--------------------------------------------------------------
387
float ofPlanePrimitive::getHeight() const {
388
    return height;
389
}
390

391

392

393

394

395
// SPHERE PRIMITIVE //
396
//----------------------------------------------------------
397
ofSpherePrimitive::ofSpherePrimitive() {
398
	texCoords = {0.f, 0.f, 1.f, 1.f};
399
    radius = 20;
400
    setResolution( 16 );
401
}
402

403
//----------------------------------------------------------
404
ofSpherePrimitive::ofSpherePrimitive( float _radius, int res, ofPrimitiveMode mode ) {
405
	radius = _radius;
406
	texCoords = {0.f, 0.f, 1.f, 1.f};
407
    setResolution( res );
408
}
409

410
//----------------------------------------------------------
411
ofSpherePrimitive::~ofSpherePrimitive() {
412
    
413
}
414

415
//----------------------------------------------------------
416
void ofSpherePrimitive::set( float _radius, int res, ofPrimitiveMode mode ) {
417
    radius     = _radius;
418
    resolution = res;
419

420
    getMesh() = ofMesh::sphere( getRadius(), getResolution(), mode );
421
    
422
    normalizeAndApplySavedTexCoords();
423
}
424

425
//----------------------------------------------------------
426
void ofSpherePrimitive::setResolution( int res ) {
427
    resolution             = res;
428
    ofPrimitiveMode mode   = getMesh().getMode();
429
    
430
    set(getRadius(), getResolution(), mode );
431
}
432

433
//----------------------------------------------------------
434
void ofSpherePrimitive::setMode( ofPrimitiveMode mode ) {
435
    ofPrimitiveMode currMode = getMesh().getMode();
436
    if(currMode != mode)
437
        set(getRadius(), getResolution(), mode );
438
}
439

440
//----------------------------------------------------------
441
void ofSpherePrimitive::setRadius(float _radius) {
442
    radius = _radius;
443
    setResolution( getResolution() );
444
}
445

446
//----------------------------------------------------------
447
float ofSpherePrimitive::getRadius() const {
448
    return radius;
449
}
450

451
//----------------------------------------------------------
452
int ofSpherePrimitive::getResolution() const {
453
    return resolution;
454
}
455

456

457
// ICO SPHERE //
458
//----------------------------------------------------------
459
ofIcoSpherePrimitive::ofIcoSpherePrimitive() {
460
	texCoords = {0.f, 0.f, 1.f, 1.f};
461
    radius     = 20;
462
    setResolution( 2 );
463
}
464

465
//----------------------------------------------------------
466
ofIcoSpherePrimitive::ofIcoSpherePrimitive( float _radius, int iterations ) {
467
	texCoords = {0.f, 0.f, 1.f, 1.f};
468
    radius     = _radius;
469
    setResolution( iterations );
470
}
471

472
//----------------------------------------------------------
473
ofIcoSpherePrimitive::~ofIcoSpherePrimitive() {
474
    
475
}
476

477
//----------------------------------------------------------
478
void ofIcoSpherePrimitive::set(float _radius, int res ) {
479
    radius = _radius;
480
    setResolution(res);
481
}
482

483
//----------------------------------------------------------
484
void ofIcoSpherePrimitive::setResolution( int iterations ) {
485
    // store the number of iterations in the resolution //
486
    resolution = iterations;
487
    
488
    getMesh() = ofMesh::icosphere( getRadius(), getResolution() );
489
    normalizeAndApplySavedTexCoords();
490
}
491

492
//----------------------------------------------------------
493
void ofIcoSpherePrimitive::setMode( ofPrimitiveMode mode ) {
494
    // ofIcoSpherePrimitive only works with OF_PRIMITIVE_TRIANGLES //
495
    setResolution( getResolution() );
496
}
497

498
//----------------------------------------------------------
499
void ofIcoSpherePrimitive::setRadius(float _radius) {
500
    radius = _radius;
501
    setResolution( getResolution() );
502
}
503

504
//----------------------------------------------------------
505
float ofIcoSpherePrimitive::getRadius() const {
506
    return radius;
507
}
508

509
//----------------------------------------------------------
510
int ofIcoSpherePrimitive::getResolution() const {
511
    return resolution;
512
}
513

514

515

516
//--------------------------------------------------------------
517
ofCylinderPrimitive::ofCylinderPrimitive() {
518
	texCoords = {0.f, 0.f, 1.f, 1.f};
519
    set( 60, 80, 6, 3, 2, true );
520
}
521

522
//--------------------------------------------------------------
523
ofCylinderPrimitive::ofCylinderPrimitive( float radius, float height, int radiusSegments, int heightSegments, int capSegments, bool bCapped, ofPrimitiveMode mode ) {
524
	texCoords = {0.f, 0.f, 1.f, 1.f};
525
    set( radius, height, radiusSegments, heightSegments, capSegments, bCapped, mode );
526
}
527

528
//--------------------------------------------------------------
529
ofCylinderPrimitive::~ofCylinderPrimitive() {}
530

531
//--------------------------------------------------------------
532
void ofCylinderPrimitive::set(float _radius, float _height, int radiusSegments, int heightSegments, int capSegments, bool _bCapped, ofPrimitiveMode mode) {
533
    radius = _radius;
534
    height = _height;
535
    bCapped = _bCapped;
536
	resolution = {radiusSegments, heightSegments, capSegments};
537
    
538
    int resX = std::max(getResolution().x,0.0f);
539
    int resY = std::max(getResolution().y-1,0.0f);
540
    int resZ = std::max(getResolution().z-1,0.0f);
541
    
542
    int indexStep = 2;
543
    if(mode == OF_PRIMITIVE_TRIANGLES) {
544
        indexStep = 6;
545
        resX = std::max(resX,0);
546
    }
547
    
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
    
571
    
572
    getMesh() = ofMesh::cylinder( getRadius(), getHeight(), getResolution().x, getResolution().y, getResolution().z, getCapped(), mode );
573
    
574
    normalizeAndApplySavedTexCoords();
575
    
576
}
577

578
//--------------------------------------------------------------
579
void ofCylinderPrimitive::set( float _radius, float height, bool _bCapped ) {
580
    radius = _radius;
581
    bCapped = _bCapped;
582
    setHeight( height );
583
}
584

585
//--------------------------------------------------------------
586
void ofCylinderPrimitive::setRadius( float _radius ) {
587
    radius = _radius;
588
    setResolution( getResolution().x, getResolution().y, getResolution().z );
589
}
590

591
//--------------------------------------------------------------
592
void ofCylinderPrimitive::setHeight( float _height ) {
593
    height = _height;
594
    setResolution(getResolution().x, getResolution().y, getResolution().z);
595
}
596

597
//--------------------------------------------------------------
598
void ofCylinderPrimitive::setCapped(bool _bCapped) {
599
    bCapped = _bCapped;
600
    setResolution( getResolution().x, getResolution().y, getResolution().z );
601
}
602

603
//--------------------------------------------------------------
604
void ofCylinderPrimitive::setResolutionRadius( int radiusRes ) {
605
    setResolution( radiusRes, getResolutionHeight(), getResolutionCap() );
606
}
607

608
//--------------------------------------------------------------
609
void ofCylinderPrimitive::setResolutionHeight( int heightRes ) {
610
    setResolution( getResolutionRadius(), heightRes, getResolutionCap() );
611
}
612

613
//--------------------------------------------------------------
614
void ofCylinderPrimitive::setResolutionCap( int capRes ) {
615
    setResolution( getResolutionRadius(), getResolutionHeight(), capRes );
616
}
617

618
//--------------------------------------------------------------
619
void ofCylinderPrimitive::setResolution( int radiusSegments, int heightSegments, int capSegments ) {
620
    ofPrimitiveMode mode = getMesh().getMode();
621
    set( getRadius(), getHeight(), radiusSegments, heightSegments, capSegments, getCapped(), mode );
622
}
623

624
//----------------------------------------------------------
625
void ofCylinderPrimitive::setMode( ofPrimitiveMode mode ) {
626
    ofPrimitiveMode currMode = getMesh().getMode();
627
    if(currMode != mode)
628
        set( getRadius(), getHeight(), getResolution().x, getResolution().y, getResolution().z, getCapped(), mode );
629
}
630

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

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

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

655
//--------------------------------------------------------------
656
vector<ofIndexType> ofCylinderPrimitive::getTopCapIndices() const {
657
    return of3dPrimitive::getIndices( strides[0][0], strides[0][0] + strides[0][1] );
658
}
659

660
//--------------------------------------------------------------
661
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] );
668
}
669

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

678
//--------------------------------------------------------------
679
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] );
686
}
687

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

696
//--------------------------------------------------------------
697
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] );
704
}
705

706
//--------------------------------------------------------------
707
int ofCylinderPrimitive::getResolutionRadius() const {
708
    return (int)resolution.x;
709
}
710

711
//--------------------------------------------------------------
712
int ofCylinderPrimitive::getResolutionHeight() const {
713
    return (int)resolution.y;
714
}
715

716
//--------------------------------------------------------------
717
int ofCylinderPrimitive::getResolutionCap() const {
718
    return (int)resolution.z;
719
}
720

721
//--------------------------------------------------------------
722
glm::vec3 ofCylinderPrimitive::getResolution() const {
723
    return resolution;
724
}
725

726
//--------------------------------------------------------------
727
float ofCylinderPrimitive::getHeight() const {
728
    return height;
729
}
730

731
//--------------------------------------------------------------
732
float ofCylinderPrimitive::getRadius() const {
733
    return radius;
734
}
735

736
//--------------------------------------------------------------
737
bool ofCylinderPrimitive::getCapped() const {
738
    return bCapped;
739
}
740

741

742

743

744

745
// Cone Primitive //
746
//--------------------------------------------------------------
747
ofConePrimitive::ofConePrimitive() {
748
	texCoords = {0.f, 0.f, 1.f, 1.f};
749
    set( 20, 70, 8, 3, 2 );
750
}
751

752
//--------------------------------------------------------------
753
ofConePrimitive::ofConePrimitive( float radius, float height, int radiusSegments, int heightSegments, int capSegments, ofPrimitiveMode mode ) {
754
	texCoords = {0.f, 0.f, 1.f, 1.f};
755
    set( radius, height, radiusSegments, heightSegments, capSegments, mode );
756
}
757

758
//--------------------------------------------------------------
759
ofConePrimitive::~ofConePrimitive() {}
760

761
//--------------------------------------------------------------
762
void ofConePrimitive::set( float _radius, float _height, int radiusSegments, int heightSegments, int capSegments, ofPrimitiveMode mode ) {
763
    radius = _radius;
764
    height = _height;
765
	resolution = {radiusSegments, heightSegments, capSegments};
766
    
767
    int resX = std::max(getResolution().x, 0.0f);
768
    int resY = std::max(getResolution().y-1, 0.0f);
769
    int resZ = std::max(getResolution().z-1, 0.0f);
770
    
771
    int indexStep = 2;
772
    if(mode == OF_PRIMITIVE_TRIANGLES) {
773
        indexStep = 6;
774
        resX = std::max(resX-1, 0);
775
    }
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);
786
    
787
    getMesh() = ofMesh::cone( getRadius(), getHeight(), getResolution().x, getResolution().y, getResolution().z, mode );
788
    
789
    normalizeAndApplySavedTexCoords();
790
    
791
}
792

793
//--------------------------------------------------------------
794
void ofConePrimitive::set( float _radius, float _height ) {
795
    radius = _radius;
796
    height = _height;
797
    setResolution( getResolution().x, getResolution().y, getResolution().z );
798
}
799

800
//--------------------------------------------------------------
801
void ofConePrimitive::setResolutionRadius( int radiusRes ) {
802
    setResolution( radiusRes, getResolutionHeight(), getResolutionCap() );
803
}
804

805
//--------------------------------------------------------------
806
void ofConePrimitive::setResolutionHeight( int heightRes ) {
807
    setResolution( getResolutionRadius(), heightRes, getResolutionCap() );
808
}
809

810
//--------------------------------------------------------------
811
void ofConePrimitive::setResolutionCap( int capRes ) {
812
    setResolution( getResolutionRadius(), getResolutionHeight(), capRes );
813
}
814

815
//--------------------------------------------------------------
816
void ofConePrimitive::setResolution( int radiusRes, int heightRes, int capRes ) {
817
    ofPrimitiveMode mode = getMesh().getMode();
818
    set( getRadius(), getHeight(), radiusRes, heightRes, capRes, mode );
819
}
820

821
//----------------------------------------------------------
822
void ofConePrimitive::setMode( ofPrimitiveMode mode ) {
823
    ofPrimitiveMode currMode = getMesh().getMode();
824
    if(currMode != mode)
825
        set( getRadius(), getHeight(), getResolution().x, getResolution().y, getResolution().z, mode );
826
}
827

828
//--------------------------------------------------------------
829
void ofConePrimitive::setRadius( float _radius ) {
830
    radius = _radius;
831
    setResolution(getResolution().x, getResolution().y, getResolution().z);
832
}
833

834
//--------------------------------------------------------------
835
void ofConePrimitive::setHeight(float _height) {
836
    height = _height;
837
    setResolution(getResolution().x, getResolution().y, getResolution().z);
838
}
839

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

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

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

864
//--------------------------------------------------------------
865
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 );
876
}
877

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

886
//--------------------------------------------------------------
887
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 );
898
}
899

900
//--------------------------------------------------------------
901
int ofConePrimitive::getResolutionRadius() const {
902
    return (int)resolution.x;
903
}
904

905
//--------------------------------------------------------------
906
int ofConePrimitive::getResolutionHeight() const {
907
    return (int)resolution.y;
908
}
909

910
//--------------------------------------------------------------
911
int ofConePrimitive::getResolutionCap() const {
912
    return (int)resolution.z;
913
}
914

915
//--------------------------------------------------------------
916
glm::vec3 ofConePrimitive::getResolution() const {
917
    return resolution;
918
}
919

920
//--------------------------------------------------------------
921
float ofConePrimitive::getRadius() const {
922
    return radius;
923
}
924

925
//--------------------------------------------------------------
926
float ofConePrimitive::getHeight() const {
927
    return height;
928
}
929

930

931

932

933

934
// BOX PRIMITIVE //
935
//--------------------------------------------------------------
936
ofBoxPrimitive::ofBoxPrimitive() {
937
	texCoords = {0.f, 0.f, 1.f, 1.f};
938
    set(100, 100, 100, 2, 2, 2);
939
}
940

941
//--------------------------------------------------------------
942
ofBoxPrimitive::ofBoxPrimitive( float width, float height, float depth, int resWidth, int resHeight, int resDepth ) {
943
	texCoords = {0.f, 0.f, 1.f, 1.f};
944
    set(width, height, depth, resWidth, resHeight, resDepth );
945
}
946

947
//--------------------------------------------------------------
948
ofBoxPrimitive::~ofBoxPrimitive() {}
949

950
//--------------------------------------------------------------
951
void ofBoxPrimitive::set( float width, float height, float depth, int resWidth, int resHeight, int resDepth) {
952
    
953
    size.x = width;
954
    size.y = height;
955
    size.z = depth;
956
    
957
	resolution = {resWidth, resHeight, resDepth};
958
    
959
    int resX = getResolution().x;
960
    int resY = getResolution().y;
961
    int resZ = getResolution().z;
962
    
963
    //FRONT, resY, resX
964
    strides[ SIDE_FRONT ][0] = 0;
965
    strides[ SIDE_FRONT ][1] = (resY)*(resX)*6;
966
    vertices[SIDE_FRONT][0] = 0;
967
    vertices[SIDE_FRONT][1] = (resX+1) * (resY+1);
968
    
969
    //RIGHT, resY, resZ
970
    strides[ SIDE_RIGHT ][0] = strides[ SIDE_FRONT ][0] + strides[ SIDE_FRONT ][1];
971
    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);
974
    
975
    //LEFT, resY, resZ
976
    strides[ SIDE_LEFT ][0] = strides[ SIDE_RIGHT ][0] + strides[ SIDE_RIGHT ][1];
977
    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);
980
    
981
    //BACK, resY, resX
982
    strides[ SIDE_BACK ][0] = strides[ SIDE_LEFT ][0] + strides[ SIDE_LEFT ][1];
983
    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);
986
    
987
    //TOP, resZ, resX
988
    strides[ SIDE_TOP ][0] = strides[ SIDE_BACK ][0] + strides[ SIDE_BACK ][1];
989
    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);
992
    
993
    //BOTTOM, resZ, resX
994
    strides[ SIDE_BOTTOM ][0] = strides[ SIDE_TOP ][0]+strides[ SIDE_TOP ][1];
995
    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);
998
    
999
    getMesh() = ofMesh::box( getWidth(), getHeight(), getDepth(), getResolution().x, getResolution().y, getResolution().z );
1000
    
1001
    normalizeAndApplySavedTexCoords();
1002
}
1003

1004
//--------------------------------------------------------------
1005
void ofBoxPrimitive::set( float width, float height, float depth ) {
1006
    set( width, height, depth, getResolution().x, getResolution().y, getResolution().z );
1007
}
1008

1009
//--------------------------------------------------------------
1010
void ofBoxPrimitive::set( float size ) {
1011
    set( size, size, size );
1012
}
1013

1014
//--------------------------------------------------------------
1015
void ofBoxPrimitive::setWidth( float a_width ) {
1016
    size.x = a_width;
1017
    set( getWidth(), getHeight(), getDepth() );
1018
}
1019

1020
//--------------------------------------------------------------
1021
void ofBoxPrimitive::setHeight( float a_height ) {
1022
    size.y = a_height;
1023
    set( getWidth(), getHeight(), getDepth() );
1024
}
1025

1026
//--------------------------------------------------------------
1027
void ofBoxPrimitive::setDepth( float a_depth ) {
1028
    size.z = a_depth;
1029
    set( getWidth(), getHeight(), getDepth() );
1030
}
1031

1032
//--------------------------------------------------------------
1033
void ofBoxPrimitive::resizeToTexture( ofTexture& inTexture ) {
1034
    set(inTexture.getWidth(), inTexture.getHeight(), inTexture.getWidth());
1035
    mapTexCoordsFromTexture( inTexture );
1036
}
1037

1038
//--------------------------------------------------------------
1039
vector<ofIndexType> ofBoxPrimitive::getSideIndices( int sideIndex ) const {
1040
    
1041
    if(sideIndex < 0 || sideIndex >= SIDES_TOTAL) {
1042
        ofLogWarning("ofBoxPrimitive") << "getSideIndices(): faceIndex out of bounds, returning SIDE_FRONT";
1043
        sideIndex = SIDE_FRONT;
1044
    }
1045
    
1046
    return getIndices(strides[sideIndex][0], strides[sideIndex][0]+strides[sideIndex][1]);
1047
}
1048

1049
//--------------------------------------------------------------
1050
ofMesh ofBoxPrimitive::getSideMesh( int sideIndex ) const {
1051
    
1052
    if(sideIndex < 0 || sideIndex > SIDES_TOTAL) {
1053
        ofLogWarning("ofBoxPrimitive") << "getSideMesh(): faceIndex out of bounds, using SIDE_FRONT";
1054
        sideIndex = SIDE_FRONT;
1055
    }
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 );
1063
}
1064

1065
//--------------------------------------------------------------
1066
void ofBoxPrimitive::setResolution( int res ) {
1067
    setResolution(res, res, res);
1068
}
1069

1070
//--------------------------------------------------------------
1071
void ofBoxPrimitive::setResolutionWidth( int widthRes ) {
1072
    setResolution( widthRes, getResolutionHeight(), getResolutionDepth() );
1073
}
1074

1075
//--------------------------------------------------------------
1076
void ofBoxPrimitive::setResolutionHeight( int heightRes ) {
1077
    setResolution( getResolutionWidth(), heightRes, getResolutionDepth() );
1078
}
1079

1080
//--------------------------------------------------------------
1081
void ofBoxPrimitive::setResolutionDepth( int depthRes ) {
1082
    setResolution( getResolutionWidth(), getResolutionHeight(), depthRes );
1083
}
1084

1085
//--------------------------------------------------------------
1086
void ofBoxPrimitive::setResolution( int resWidth, int resHeight, int resDepth ) {
1087
	resolution = {resWidth, resHeight, resDepth};
1088
    set( getWidth(), getHeight(), getDepth() );
1089
}
1090

1091
//----------------------------------------------------------
1092
void ofBoxPrimitive::setMode( ofPrimitiveMode mode ) {
1093
    // only supports triangles //
1094
    setResolution( getResolution().x, getResolution().y, getResolution().z );
1095
}
1096

1097
//--------------------------------------------------------------
1098
void ofBoxPrimitive::setSideColor( int sideIndex, ofColor color ) {
1099
    if(sideIndex < 0 || sideIndex >= SIDES_TOTAL) {
1100
        ofLogWarning("ofBoxPrimitive") << "setSideColor(): sideIndex out of bounds, setting SIDE_FRONT";
1101
        sideIndex = SIDE_FRONT;
1102
    }
1103
    getMesh().setColorForIndices( strides[sideIndex][0], strides[sideIndex][0]+strides[sideIndex][1], color );
1104
}
1105

1106
//--------------------------------------------------------------
1107
int ofBoxPrimitive::getResolutionWidth() const {
1108
    return (int)resolution.x;
1109
}
1110

1111
//--------------------------------------------------------------
1112
int ofBoxPrimitive::getResolutionHeight() const {
1113
    return (int)resolution.y;
1114
}
1115

1116
//--------------------------------------------------------------
1117
int ofBoxPrimitive::getResolutionDepth() const {
1118
    return (int)resolution.z;
1119
}
1120

1121
//--------------------------------------------------------------
1122
glm::vec3 ofBoxPrimitive::getResolution() const {
1123
    return resolution;
1124
}
1125

1126
//--------------------------------------------------------------
1127
float ofBoxPrimitive::getWidth() const {
1128
    return size.x;
1129
}
1130

1131
//--------------------------------------------------------------
1132
float ofBoxPrimitive::getHeight() const {
1133
    return size.y;
1134
}
1135

1136
//--------------------------------------------------------------
1137
float ofBoxPrimitive::getDepth() const {
1138
    return size.z;
1139
}
1140

1141
//--------------------------------------------------------------
1142
glm::vec3 ofBoxPrimitive::getSize() const {
1143
    return size;
1144
}
1145

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.