Week 9 Assignment (desc)
Younghyun Chung
Problem 1.
Add a function called drawWirefame to the Mesh class. This function should draw the Mesh as a wireframe.
|
// main.cpp
void drawW9_1( void )
{
glPushMatrix();
glRotatef( angle, 1,2,0.5 );
glScalef( 3,3,3 );
myMesh->drawWireframe();
glPopMatrix();
angle+= 0.1;
}
// Mesh.cpp
void Mesh::drawWireframe( void )
{
for( int i=0; i<gridW-1; i++ )
{
for( int j=0; j<gridH-1; j++ )
{
// glVertex3f( grid[i][j].x, grid[i][j].y, grid[i][j].z );
glColor3f( 0,0.5,0 );
glBegin( GL_LINE_LOOP );
glVertex3fv( (float*) &grid[i][j] );
glVertex3fv( (float*) &grid[i+1][j] );
glVertex3fv( (float*) &grid[i+1][j+1] );
glEnd();
glColor3f( 0,0.3,0 );
glBegin( GL_LINE_LOOP );
glVertex3fv( (float*) &grid[i][j] );
glVertex3fv( (float*) &grid[i][j+1] );
glVertex3fv( (float*) &grid[i+1][j+1] );
glEnd();
}
}
}
|
 :
|
Problem 2.
Add a function called drawShadedSolid to the Mesh class. This function should draw the Mesh as a solid form with shading.
|
void Mesh::drawShadedSolid( void )
{
for( int i=0; i<gridW-1; i++ )
{
for( int j=0; j<gridH-1; j++ )
{
Vec3d A = grid[i][j];
Vec3d B = grid[i+1][j];
Vec3d C = grid[i+1][j+1];
drawShadedTriangle( A, B, C );
A = grid[i][j];
B = grid[i+1][j+1];
C = grid[i][j+1];
drawShadedTriangle( A, B, C );
}
}
}
|
 :
|
Problem 3.
Add a function to the Mesh class called makeExtrusion. The function should take as inputs an array of points that define the extrusion shape, the number of points in the shape, and the depth of the extrusion. In order for this to work correctly, the number of points in the extrusion shape should equal either the width or height of the mesh.
|
void Mesh::makeExtrusion( Vec3d* shape, float depth )
{
for( int i=0; i<gridW; i++ )
{
for( int j=0; j<gridH; j++ )
{
grid[i][j].x = shape[i].x;
grid[i][j].y = shape[i].y;
grid[i][j].z = j * depth / gridH;
}
}
}
|
 :
|
Problem 4.
Add a function to the Mesh class called makeRevolution. The function should take as inputs an array of points that define the revolution profile, and the number of points in the profile. In order for this to work correctly, the number of points in the profile should equal either the width or height of the mesh.
|
void Mesh::makeRevolution( Vec3d* shape )
{
for( int i=0; i<gridW; i++ )
{
for( int j=0; j<gridH; j++ )
{
float radius = shape[i].x;
float angle = ( 2 * 3.14159 / ( gridH-1.0) ) * j;
grid[i][j].x = radius * cos( angle );
grid[i][j].y = shape[i].y;
grid[i][j].z = radius * sin( angle );
}
}
}
|
 :
|
Problem 5.
Add a function to the Mesh class called makeSphere. The function should take as input the radius of the sphere. The latitudinal and longitudinal divisions of the sphere will depend on the width and height of the mesh.
|
void Mesh::makeSphere( float radius )
{
for( int i=0; i<gridW; i++ )
{
for( int j=0; j<gridH; j++ )
{
float A_lat = 2* 3.14159 / ( gridW - 1.0 ) * i;
float A_long = 2 * 3.14159/ ( gridH - 1.0 ) * j;
grid[i][j] = Vec3d ( sin(A_lat) * cos(A_long),
sin(A_lat) * sin(A_long),
cos(A_lat) ) * radius;
}
}
}
|
 :
|
Problem 6.
This problem is optional. Add a function to the Mesh class called makeTorus. The function should take as inputs a primary radius and a secondary radius. You may choose to call this function makeDonut.
|
// http://en.wikipedia.org/wiki/Torus
void Mesh::makeTorus( float R, float r )
{
// R is the distance from the center of the tube
// to the center of the torus,
// r is the radius of the tube.
for( int i=0; i<gridW; i++ )
{
for( int j=0; j<gridH; j++ )
{
float u = 2 * 3.14159 / ( gridW - 1.0 ) * i;
float v = 2 * 3.14159 / ( gridH - 1.0 ) * j;
grid[i][j] = Vec3d ( ( R + r*cos(v) ) * cos(u),
( R + r*cos(v) ) * sin(u),
r * sin(v) );
}
}
}
|
 :
|
|