Skip to content

Commit

Permalink
Merge pull request #1 from arturoc/3dprimitives
Browse files Browse the repository at this point in the history
of3dPrimitive: fix vboMesh rendering + make it optional and default
  • Loading branch information
NickHardeman committed Jan 31, 2013
2 parents 465d0db + 77a5083 commit 9cbbaa9
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 116 deletions.
89 changes: 66 additions & 23 deletions libs/openFrameworks/3d/of3dPrimitives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,50 @@
#include "of3dPrimitives.h"
#include "ofGraphics.h"

ofPrimitiveBase::ofPrimitiveBase() {
ofPrimitiveBase::ofPrimitiveBase()
:usingVbo(true)
,_mesh(new ofVboMesh)
{
setScale(1.0, 1.0, 1.0);
}

//----------------------------------------------------------
ofPrimitiveBase::~ofPrimitiveBase() {

}

//----------------------------------------------------------
ofPrimitiveBase::ofPrimitiveBase(const ofPrimitiveBase & mom){
_texCoords = mom._texCoords;
usingVbo = mom.usingVbo;
if(usingVbo){
_mesh = ofPtr<ofMesh>(new ofVboMesh);
}else{
_mesh = ofPtr<ofMesh>(new ofMesh);
}
*_mesh = *mom._mesh;
_resolution = mom._resolution;
}

//----------------------------------------------------------
ofPrimitiveBase & ofPrimitiveBase::operator=(const ofPrimitiveBase & mom){
if(&mom!=this){
_texCoords = mom._texCoords;
setUseVbo(mom.usingVbo);
*_mesh = *mom._mesh;
_resolution = mom._resolution;
}
return *this;
}

// GETTERS //
//----------------------------------------------------------
ofMesh* ofPrimitiveBase::getMeshPtr() {
return& _mesh;
return _mesh.get();
}
//----------------------------------------------------------
ofMesh& ofPrimitiveBase::getMesh() {
return _mesh;
return *_mesh;
}

//----------------------------------------------------------
Expand Down Expand Up @@ -170,36 +199,33 @@ void ofPrimitiveBase::draw(ofPolyRenderMode renderType) {
void ofPrimitiveBase::drawNormals(float length, bool bFaceNormals) {
ofNode::transformGL();

float fixLength = getScale().length();

if(getMesh().usingNormals()) {
vector<ofVec3f>& normals = getMesh().getNormals();
vector<ofVec3f>& vertices = getMesh().getVertices();
ofVec3f normal;
ofVec3f vert;

ofMesh tempMesh;
tempMesh.setMode( OF_PRIMITIVE_LINES );
tempMesh.getVertices().assign( normals.size() * 2, ofVec3f() );
normalsMesh.setMode( OF_PRIMITIVE_LINES );
normalsMesh.getVertices().resize( normals.size() * 2);

if(bFaceNormals) {
for(int i = 0; i < normals.size(); i += 3) {
for(int i = 0; i < (int)normals.size(); i += 3) {
vert = (vertices[i+0]+vertices[i+1]+vertices[i+2]) / 3;
tempMesh.setVertex(i*2, vert);
normalsMesh.setVertex(i*2, vert);
normal = normals[i].getNormalized();
normal *= length;
tempMesh.setVertex(i*2+1, normal+vert);
normalsMesh.setVertex(i*2+1, normal+vert);
}
} else {
for(int i = 0; i < normals.size(); i++) {
for(int i = 0; i < (int)normals.size(); i++) {
vert = vertices[i];
normal = normals[i].normalized();
tempMesh.setVertex( i*2, vert);
normalsMesh.setVertex( i*2, vert);
normal *= length;
tempMesh.setVertex(i*2+1, normal+vert);
normalsMesh.setVertex(i*2+1, normal+vert);
}
}
tempMesh.draw();
normalsMesh.draw();
} else {
ofLog(OF_LOG_WARNING, "ofPrimitiveBase :: drawNormals()") << " : mesh normals are disabled";
}
Expand All @@ -216,6 +242,23 @@ void ofPrimitiveBase::drawAxes(float a_size) {
}


void ofPrimitiveBase::setUseVbo(bool useVbo){
if(useVbo!=usingVbo){
ofPtr<ofMesh> newMesh;
if(useVbo){
newMesh = ofPtr<ofMesh>(new ofVboMesh);
}else{
newMesh = ofPtr<ofMesh>(new ofMesh);
}
*newMesh = *_mesh;
_mesh = newMesh;
}
usingVbo = useVbo;
}

bool ofPrimitiveBase::isUsingVbo(){
return usingVbo;
}

// PLANE PRIMITIVE //
//--------------------------------------------------------------
Expand All @@ -240,9 +283,9 @@ void ofPlanePrimitive::set(float width, float height, int columns, int rows, ofP
_height = height;
ofPrimitiveBase::setResolution(columns, rows, 0);

_mesh.clear();
_mesh->clear();
//_mesh = ofGetPlaneMesh( getWidth(), getHeight(), getResolution().x, getResolution().y, mode );
_mesh = ofMesh::plane( getWidth(), getHeight(), getResolution().x, getResolution().y, mode );
*_mesh = ofMesh::plane( getWidth(), getHeight(), getResolution().x, getResolution().y, mode );

normalizeAndApplySavedTexCoords();

Expand Down Expand Up @@ -336,7 +379,7 @@ void ofSpherePrimitive::set(float radius, int res, ofPrimitiveMode mode ) {
ofPrimitiveBase::setResolution(res, res, res);
getMesh().clear();
//_mesh = ofGetSphereMesh( getRadius(), getResolution().x, mode );
_mesh = ofMesh::sphere( getRadius(), getResolution().x, mode );
*_mesh = ofMesh::sphere( getRadius(), getResolution().x, mode );

normalizeAndApplySavedTexCoords();
}
Expand Down Expand Up @@ -415,7 +458,7 @@ void ofIcoSpherePrimitive::setResolution( int resX, int resY, int resZ ) {
getMesh().clear();
// store the number of iterations in the resolution //
//_mesh = ofGetIcoSphereMesh( getRadius(), getResolution().x );
_mesh = ofMesh::icosphere( getRadius(), getResolution().x );
*_mesh = ofMesh::icosphere( getRadius(), getResolution().x );
normalizeAndApplySavedTexCoords();
}

Expand Down Expand Up @@ -497,7 +540,7 @@ void ofCylinderPrimitive::set(float radius, float height, int radiusSegments, in

getMesh().clear();
//_mesh = ofGetCylinderMesh( getRadius(), getHeight(), getResolution().x, getResolution().y, getResolution().z, getCapped(), mode );
_mesh = ofMesh::cylinder( getRadius(), getHeight(), getResolution().x, getResolution().y, getResolution().z, getCapped(), mode );
*_mesh = ofMesh::cylinder( getRadius(), getHeight(), getResolution().x, getResolution().y, getResolution().z, getCapped(), mode );

normalizeAndApplySavedTexCoords();

Expand Down Expand Up @@ -681,7 +724,7 @@ void ofConePrimitive::set( float radius, float height, int radiusSegments, int h

getMesh().clear();
//_mesh = ofGetConeMesh( getRadius(), getHeight(), getResolution().x, getResolution().y, getResolution().z, mode );
_mesh = ofMesh::cone( getRadius(), getHeight(), getResolution().x, getResolution().y, getResolution().z, mode );
*_mesh = ofMesh::cone( getRadius(), getHeight(), getResolution().x, getResolution().y, getResolution().z, mode );

normalizeAndApplySavedTexCoords();

Expand Down Expand Up @@ -865,9 +908,9 @@ void ofBoxPrimitive::set( float width, float height, float depth, int resWidth,
_vertices[SIDE_BOTTOM][0] = _vertices[SIDE_TOP][0] + _vertices[SIDE_TOP][1];
_vertices[SIDE_BOTTOM][1] = resZ * resX;

_mesh.clear();
_mesh->clear();
//_mesh = ofGetBoxMesh( getWidth(), getHeight(), getDepth(), getResolution().x, getResolution().y, getResolution().z );
_mesh = ofMesh::box( getWidth(), getHeight(), getDepth(), getResolution().x, getResolution().y, getResolution().z );
*_mesh = ofMesh::box( getWidth(), getHeight(), getDepth(), getResolution().x, getResolution().y, getResolution().z );

normalizeAndApplySavedTexCoords();
}
Expand Down
11 changes: 9 additions & 2 deletions libs/openFrameworks/3d/of3dPrimitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//
#pragma once

#include "ofMesh.h"
#include "ofVboMesh.h"
#include "ofRectangle.h"
#include "ofNode.h"
#include "ofTexture.h"
Expand All @@ -28,6 +28,9 @@ class ofPrimitiveBase : public ofNode {
ofPrimitiveBase();
virtual ~ofPrimitiveBase();

ofPrimitiveBase(const ofPrimitiveBase & mom);
ofPrimitiveBase & operator=(const ofPrimitiveBase & mom);

void mapTexCoords( float u1, float v1, float u2, float v2 );
//void setTexCoords( int meshindex, float u1, float v1, float u2, float v2 );
// does not store texture. Creates tex coords from texture, if texture is
Expand Down Expand Up @@ -68,13 +71,17 @@ class ofPrimitiveBase : public ofNode {
void drawNormals( float length, bool bFaceNormals=false );
void drawAxes(float a_size);

void setUseVbo(bool useVbo);
bool isUsingVbo();
protected:

// useful when creating a new model, since it uses normalized tex coords //
void normalizeAndApplySavedTexCoords();

ofMesh _mesh;
ofVec4f _texCoords;
bool usingVbo;
ofPtr<ofMesh> _mesh;
ofMesh normalsMesh;

ofVec3f _resolution;
vector<ofIndexType> getIndices( int startIndex, int endIndex );
Expand Down
2 changes: 0 additions & 2 deletions libs/openFrameworks/3d/ofMesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,6 @@ class ofMesh{
static ofMesh cone(float radius, float height, int radiusSegments=12, int heightSegments=6, int capSegments=2, ofPrimitiveMode mode=OF_PRIMITIVE_TRIANGLE_STRIP);
static ofMesh box(float width, float height, float depth, int resX=4, int resY=4, int resZ=4);


protected:
virtual void draw(ofPolyRenderMode renderType);

private:
Expand Down
4 changes: 2 additions & 2 deletions libs/openFrameworks/gl/ofGLRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,13 @@ void ofGLRenderer::draw(ofMesh & vertexData, ofPolyRenderMode renderType, bool u
}

//----------------------------------------------------------
void ofGLRenderer::draw( ofPrimitiveBase& model, ofPolyRenderMode renderType ) {
void ofGLRenderer::draw( ofPrimitiveBase& model, ofPolyRenderMode renderType) {
bool normalsEnabled = glIsEnabled( GL_NORMALIZE );
if(model.hasScaling() && model.hasNormalsEnabled()) {
if(!normalsEnabled) glEnable( GL_NORMALIZE );
}

draw( model.getMesh(), renderType, model.getMesh().usingColors(), model.getMesh().usingTextures(), model.getMesh().usingNormals() );
model.getMesh().draw(renderType);

if(model.hasScaling() && model.hasNormalsEnabled()) {
if(!normalsEnabled) glDisable( GL_NORMALIZE );
Expand Down
2 changes: 1 addition & 1 deletion libs/openFrameworks/gl/ofGLRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ofGLRenderer: public ofBaseRenderer{
void update();
void draw(ofMesh & vertexData, bool useColors=true, bool useTextures=true, bool useNormals = true);
void draw(ofMesh & vertexData, ofPolyRenderMode renderType, bool useColors=true, bool useTextures = true, bool useNormals=true);
void draw( ofPrimitiveBase& model, ofPolyRenderMode renderType );
void draw( ofPrimitiveBase& model, ofPolyRenderMode renderType);
void draw(ofPolyline & poly);
void draw(ofPath & path);
void draw(vector<ofPoint> & vertexData, ofPrimitiveMode drawMode);
Expand Down
Loading

0 comments on commit 9cbbaa9

Please sign in to comment.