openframeworks/openFrameworks — pull request #1819 by NickHardeman (2013-01-18 08:57:53+00:00)
Title: Feature 3d graphics

This PR adds 3d primitives to the OF core. Different dimensions, resolutions and texture coords can be specified. The following 3d primitives have been added/modified:

ofPlanePrimitive
ofSpherePrimitive
ofIcoSpherePrimitive
ofCylinderPrimitive
ofConePrimitive
ofBoxPrimitive

An example app that demonstrates the added functionality is in *examples/gl/3DPrimitivesExample*
https://github.com/NickHardeman/openFrameworks/tree/feature-3dGraphics/examples/gl/3DPrimitivesExample

The primitives all extend ofNode, so functionality like setPosition and setRotation are inherited.

Functions are available to change the dimensions / resolution of the created primitive. Though it does re-create the mesh.
```c++
sphere.setRadius( width );
plane.set( width*1.5, height*1.5 );
```

Also allows you to get a mesh directly via
https://github.com/NickHardeman/openFrameworks/blob/feature-3dGraphics/libs/openFrameworks/3d/of3dPrimitives.h#L300-L306
```c++
ofMesh ofGetPlaneMesh(float width, float height, int columns, int rows, ofPrimitiveMode mode=OF_PRIMITIVE_TRIANGLE_STRIP);
ofMesh ofGetSphereMesh(float radius, int res, ofPrimitiveMode mode=OF_PRIMITIVE_TRIANGLE_STRIP );
ofMesh ofGetIcosahedronMesh(float radius);
ofMesh ofGetIcoSphereMesh(float radius, int iterations);
ofMesh ofGetCylinderMesh( float radius, float height, int radiusSegments, int heightSegments, int numCapSegments=2, bool bCapped = true, ofPrimitiveMode mode=OF_PRIMITIVE_TRIANGLE_STRIP );
ofMesh ofGetConeMesh( float radius, float height, int radiusSegments, int heightSegments, ofPrimitiveMode mode=OF_PRIMITIVE_TRIANGLE_STRIP );
ofMesh ofGetBoxMesh( float width, float height, float depth, int resX, int resY, int resZ );
```

The primitive provides normalized texture coords upon creation, but this can easily be changed with
https://github.com/NickHardeman/openFrameworks/blob/feature-3dGraphics/libs/openFrameworks/3d/of3dPrimitives.h#L57
```c++
void setTexCoords( float u1, float v1, float u2, float v2 );
```
or by just passing in an arb or non-arb texture
https://github.com/NickHardeman/openFrameworks/blob/feature-3dGraphics/libs/openFrameworks/3d/of3dPrimitives.h#L63
```c++
void setTexCoordsFromTexture( ofTexture& inTexture );
```

The base class also provides useful functions like:
https://github.com/NickHardeman/openFrameworks/blob/feature-3dGraphics/libs/openFrameworks/3d/of3dPrimitives.h#L78
```c++
ofVec3f getFaceNormal(of3dTriangle& tri);
vector getUniqueTriangles();
vector getFaceNormals( bool perVetex=false);
void setFromTriangles( vector& tris, bool bUseFaceNormal=false );
void smoothNormals( float angle );
```

A couple helper functions on each primitive include:
https://github.com/NickHardeman/openFrameworks/blob/feature-3dGraphics/libs/openFrameworks/3d/of3dPrimitives.h#L128
```c++
//ofPlanePrimitive
void resizeToTexture( ofTexture& inTexture, float scale=1.f );
```

https://github.com/NickHardeman/openFrameworks/blob/feature-3dGraphics/libs/openFrameworks/3d/of3dPrimitives.h#L196-L205
```c++
// ofCylinderPrimitive
void setTopCapColor( ofColor color );
void setCylinderColor( ofColor color );
void setBottomCapColor( ofColor color );
vector getTopCapIndicies();
ofMesh getTopCapMesh();
vector getCylinderIndicies();
ofMesh getCylinderMesh();
vector getBottomCapIndicies();
ofMesh getBottomCapMesh();
```

https://github.com/NickHardeman/openFrameworks/blob/feature-3dGraphics/libs/openFrameworks/3d/of3dPrimitives.h#L233-L239
```c++
// ofConePrimitve
void setTopColor( ofColor color );
void setCapColor( ofColor color );
vector getConeIndicies();
ofMesh getConeMesh();
vector getCapIndicies();
ofMesh getCapMesh();
```
https://github.com/NickHardeman/openFrameworks/blob/feature-3dGraphics/libs/openFrameworks/3d/of3dPrimitives.h#L274-L286
```c++
// ofBoxPrimitive
void setWidth( float a_width );
void setHeight( float a_height );
void setDepth( float a_depth );

void resizeToTexture( ofTexture& inTexture );

vector getSideIndicies( int sideIndex );
ofMesh getSideMesh( int sideIndex );

void setResolution( int res ); // same resolution for all sides //
void setResolution( int resX, int resY, int resZ );
void setMode( ofPrimitiveMode mode );
void setSideColor( int sideIndex, ofColor color );
```

This PR also adds of3dGraphics.h and .cpp. moving 3d primitive drawing like ofSphere and ofBox into separate files.
https://github.com/NickHardeman/openFrameworks/blob/feature-3dGraphics/libs/openFrameworks/graphics/of3dGraphics.h
This allows for quick drawing and getting/setting of resolutions for cached meshes.

The GL and Cairo renderers also had to be modified to draw an ofPrimitiveBase accordingly
https://github.com/NickHardeman/openFrameworks/blob/feature-3dGraphics/libs/openFrameworks/gl/ofGLRenderer.cpp#L131-L143
```c++
void ofGLRenderer::draw( ofPrimitiveBase& model, ofPolyRenderMode renderType );
```

*Note: I removed ofBox from the customDraw function inside ofNode since ofBox was conflicting with it, since the primitive extends ofNode.*
https://github.com/NickHardeman/openFrameworks/blob/feature-3dGraphics/libs/openFrameworks/3d/ofNode.h#L141