Skip to content

Instantly share code, notes, and snippets.

@onedayitwillmake
Created April 8, 2011 23:35
Show Gist options
  • Save onedayitwillmake/910935 to your computer and use it in GitHub Desktop.
Save onedayitwillmake/910935 to your computer and use it in GitHub Desktop.
Generate geometry for a 3d plane and insert into ci::TriMesh
void QuadDistrustApp::createPlane()
{
_planeMesh = new ci::TriMesh();
_planeMesh->clear();
float segmentsW = 4;
float segmentsH = 4;
float width = 100;
float height = width;
std::vector< std::vector<ci::Vec3f> > grid;
for(int i = 0; i <= segmentsW; ++i)
{
std::vector<ci::Vec3f> row;
grid.push_back( row );
for(int j = 0; j <= segmentsH; ++j)
{
ci::Vec3f pos = ci::Vec3f(((float)i / segmentsW - 0.5f) * width, 0, ((float)j / segmentsH - 0.5f) * height);
grid[i].push_back( pos );
}
}
for(int i = 0; i < segmentsW; ++i) {
for( int j = 0; j < segmentsH; ++j) {
ci::Vec3f a = grid[i ][j ];
ci::Vec3f b = grid[i+1][j ];
ci::Vec3f c = grid[i ][j+1];
ci::Vec3f d = grid[i+1][j+1];
ci::ColorA color = ci::ColorA( ci::Rand::randFloat(), ci::Rand::randFloat(), ci::Rand::randFloat(), 0.8 );
std::cout << a << b << c << d << std::endl;
ci::Vec3f e0 = c - a;
ci::Vec3f e1 = c - b;
ci::Vec3f n = -e0.cross(e1).normalized();
_planeMesh->appendVertex( a );
_planeMesh->appendColorRGBA( color );
_planeMesh->appendNormal( n );
_planeMesh->appendVertex( b );
_planeMesh->appendColorRGBA( color );
_planeMesh->appendNormal( n );
_planeMesh->appendVertex( c );
_planeMesh->appendColorRGBA( color );
_planeMesh->appendNormal( n );
_planeMesh->appendVertex( d );
_planeMesh->appendColorRGBA( color );
_planeMesh->appendNormal( n );
int vertA = _planeMesh->getNumVertices() - 4;
int vertB = _planeMesh->getNumVertices() - 3;
int vertC = _planeMesh->getNumVertices() - 2;
int vertD = _planeMesh->getNumVertices() - 1;
_planeMesh->appendTriangle( vertA, vertB, vertC );
_planeMesh->appendTriangle( vertD, vertC, vertB );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment