Week 9 Assignment
Matt Parker
|
Problem 1. Add a function called drawWirefame to the Mesh class. This function should draw the Mesh as a wireframe.
|
Mesh::Mesh(int w, int h, float s)
{
width = w;
height = h;
spacing = s;
grid = new Vec3d*[w];
for(int x = 0; x < w; x++){
grid[x] = new Vec3d[h];
for(int y = 0; y < h; y++){
grid[x][y] = Vec3d(x * s, y * s, 0);
}
}
}
void Mesh::drawWireFrame(){
glLineWidth(3);
for(int x = 0; x < width; x++){
glBegin(GL_LINE_STRIP);
for(int y = 0; y < height; y++){
grid[x][y].drawVertex();
}
glEnd();
}
for(int y = 0; y < height; y++){
glBegin(GL_LINE_STRIP);
for(int x = 0; x < width; x++){
grid[x][y].drawVertex();
}
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 drawShadedTriangle ( Vec3d A, Vec3d B, Vec3d C )
{
// define vectors within triangle
Vec3d AB = B - A;
Vec3d AC = C - A;
// compute normal to triangle
Vec3d Normal = AB.crossProduct( AC );
// normalize the normal ( make length = 1.0 )
Normal = Normal/Normal.length();
// set up lighting vector
Vec3d L(0, 0, 10);
L = L/L.length();
// compute shade
float shade = L.dotProduct( Normal );
glColor3f( shade*100, shade*100, shade*0.5 );
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();
}
void Mesh::drawSolid(){
Vec3d light(0, 0, 1000);
light.normalize();
for(int x = 0; x < width - 1; x++){
for(int y = 0; y < height - 1; y++){
Vec3d vec1 = grid[x + 1][y] - grid[x][y];
Vec3d vec2 = grid[x][y + 1] - grid[x][y];
Vec3d normal = vec1.crossProduct(vec2);
normal.normalize();
glColor3f(normal.dotProduct(light)/3,
normal.dotProduct(light)/2,
normal.dotProduct(light)/3);
glBegin(GL_TRIANGLE_STRIP);
grid[x][y].drawVertex();
grid[x + 1][y].drawVertex();
grid[x][y + 1].drawVertex();
glEnd();
glBegin(GL_TRIANGLE_STRIP);
grid[x + 1][y + 1].drawVertex();
grid[x][y + 1].drawVertex();
grid[x + 1][y].drawVertex();
glEnd();
}
}
}
|
 |
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* vecs){
for(int x = 0; x < width; x++){
Vec3d vec = vecs[x];
for(int y = 0; y < height; y++){
grid[x][y] = Vec3d(vec.x, vec.y, y * spacing);
}
}
drawWireFrame();
}
|
 |
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.shading. |
void Mesh::makeRevolution(Vec3d* vecs, int numOfPts){
for(int x = 0; x < width; x++){
Vec3d vec = vecs[x];
Circle3d circ(vecs[x].x + 150, 20, Vec3d(0,0,0));
Vec3d* circVecs = circ.getVecs();
for(int y = 0; y < height; y++){
grid[x][y] = Vec3d(circVecs[y].x, vec.y, circVecs[y].y);
}
}
drawSolid();
drawWireFrame();
}
|
 |
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 angle = 400/ width - 1;
// Circle3d circ(width, width, Vec3d(0,0,0));
// Vec3d* vecs = circ.getVecs();
for(int x = 0; x < width; x++){
for(int y = 0; y < height; y++){
Vec3d vec(0,0,0);
vec.x = cos_deg(angle * x) * width * sin_deg(angle/2 * y);
vec.y = sin_deg(angle * x) * width * sin_deg(angle/2 * y);
vec.z = cos_deg(angle/2 * y) * width;
grid[x][y] = Vec3d(vec.x, vec.y, vec.z);
}
}
drawSolid();
}
|
 |
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. |
void Mesh::makeTorus(float innerRadius, float outerRadius){
float angle = 390/(width - 1);
for(int x = 0; x < width; x++){
for(int y = 0; y < height; y++){
Vec3d vec(0,0,0);
vec.x = (outerRadius + cos_deg(angle * x) * innerRadius) * cos_deg(angle * y);
vec.y = (outerRadius + cos_deg(angle * x) * innerRadius) * sin_deg(angle * y);
vec.z = sin_deg(angle * x) * innerRadius;
grid[x][y] = Vec3d(vec.x, vec.y, vec.z);
}
}
drawWireFrame();
}
|
 |
Mess Ups, but cool: |
> |
 |
|