CompForm Assignment 9 · posted by vaibhav bhawsar Dec 31, 09:04
Problem 1: Add a function called drawWirefame to the Mesh class. This function should draw the Mesh as a wireframe.
void Mesh::drawWireFrame()
{
for ( int x=0 ; x < gridW-1 ; x++ )
{
for(int z =0 ; z < gridH-1 ; z++ )
{
//passing an array of three which
//out vec3d is into this particular opengl method
//glVertext3fv( (float*) &grid[i][j]);
//--------
Vec3d A = grid[x][z];
Vec3d B = grid[x][z+1];
Vec3d C = grid[x+1][z+1];
glBegin(GL_LINE_LOOP);
glVertex3f( A.x , A.y , A.z );
glVertex3f( B.x , B.y , B.z );
glVertex3f( C.x , C.y , C.z );
glEnd();
//--------
A = grid[x][z];
B = grid[x+1][z+1];
C = grid[x+1][z];
glBegin(GL_LINE_LOOP);
glVertex3f( A.x , A.y , A.z );
glVertex3f( B.x , B.y , B.z );
glVertex3f( C.x , C.y , C.z );
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()
{
for ( int x=0 ; x < gridW-1 ; x++ )
{
for ( int z=0 ; z < gridH-1 ; z++ )
{
Vec3d A = grid[x][z];
Vec3d B = grid[x][z+1];
Vec3d C = grid[x+1][z+1];
fillShadedTriangle(A,B,C);
A = grid[x][z];
B = grid[x+1][z+1];
C = grid[x+1][z];
fillShadedTriangle(A,B,C);
}
}
}
void Mesh :: fillShadedTriangle(Vec3d A, Vec3d B, Vec3d C)
{
float shading;
//define vectors AB and AC
Vec3d AB = B-A;
Vec3d AC = C-A;
//and compute the cross product between them
//this is perpendicular to AB and AC
Vec3d N = AB.cross(AC);
//normalize the cross product
N.normalize();
//copy the light vector and norm it
Vec3d L;
L = light;
L.normalize();
//shading is the dot product, Light (L) is projected onto N
shading = L.dot(N);
if(shading <= 0)
{
shading = 0.0;
}
//compose the final tone of color
glColor4f(shading*Red,shading*Green,shading*Blue, Alpha);
glPushMatrix();
glBegin(GL_TRIANGLES);
glVertex3f(A.x,A.y,A.z);
glVertex3f(B.x,B.y,B.z);
glVertex3f(C.x,C.y,C.z);
glEnd();
glPopMatrix();
}
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, int depth)
{
for ( int D =0 ; D < gridW ; D++ ) //for each depth
{
for ( int A = 0 ; A < gridH ; A++ ) //draw
{
float x = shape[A].x;
float z = depth / (gridH-1) * A; // D * depth; //this is what i was doing
float y = shape[A].y;
grid[D][A] = Vec3d(x,y,z);
}
}
}
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::makeRevolutionProfile(Vec3d* profile)
{
//should have a center vec3d so
//that you can move this thing around
float radius = 0;
for (int D =0 ; D < gridH ; D++) //number of rings
{
radius = profile[D].y; //set the x of the profile as the radius
for ( int A = 0 ; A < gridW ; A++ )
{
float ang = A / (float) (gridH-1) * 2 * PI; //npoints-1 so that compensate
float x = radius * cos (ang);
float y = profile[D].x;
float z = radius * sin (ang);
grid[D][A] = Vec3d(x,y,z);
}
}
}
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.
//--called in the init()
int sPoints = 60;
sphereMesh = new Mesh(sPoints,sPoints);
sphereMesh2 = new Mesh(sPoints,sPoints);
Vec3d* circle = generateSemiCircle(100,sPoints);
sphereMesh->setColor(1,.2,.2,.5);
sphereMesh->makeRevolutionProfile(circle);
sphereMesh2->setColor(.2,.2,.2,.8);
sphereMesh2->makeRevolutionProfile(circle);
//-----------helper func to generate a semicircle of points-----------
Vec3d* generateSemiCircle(float r, int nPoints)
{
Vec3d* p = new Vec3d[nPoints];
for ( int i=0 ; i < nPoints ; i++ )
{
float ang = PI / (nPoints-1) * i;
float x = r * cos(ang) ;
float y = r * sin(ang) ;
p[i] = Vec3d(x,y,0);
}
return p;
}
//----------called within displayFunc-----------
sphereMesh->drawSolid();
Comment
Commenting is closed for this article.




