Week 9 Assignment - Computational Forms
Lucas Longo
Problem 1. Add a function called drawWirefame to the Mesh class. This function should draw the Mesh as a wireframe. |
void Mesh::drawWireFrame()
{
glColor3f(1,1,0);
for (int i = 0; i < gridW-1; i++){
for (int j = 0; j < gridH-1; j++){
glColor3f(1,1,0);
glBegin(GL_LINE_LOOP);
glVertex3fv((float*) &grid[i][j]);
glVertex3fv((float*) &grid[i+1][j]);
glVertex3fv((float*) &grid[i+1][j+1]);
glEnd();
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(){
glBegin(GL_TRIANGLES);
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+1][z];
Vec3d C = grid[x+1][z+1];
Vec3d D = grid[x][z+1];
Vec3d AB = B - A;
Vec3d AC = C - A;
Vec3d AD = D - A;
Vec3d DC = C - D;
Vec3d N1 = AC.cross(AB);
Vec3d N2 = AD.cross(AC);
N1.normalize();
N2.normalize();
Vec3d L = Vec3d(400, 500
, 0);
L.normalize();
float shade1 = L.dot(N1);
float shade2 = L.dot(N2);
if (shade1 < 0)
shade1 = 0;
if (shade2 < 0)
shade2 = 0;
glColor3f(shade1, shade1, 0);
glVertex3fv((float*) &grid[x][z]);
glVertex3fv((float*) &grid[x+1][z]);
glColor3f(shade1, 0, 0);
glVertex3fv((float*) &grid[x+1][z+1]);
glColor3f(shade1, 0, 0);
glVertex3fv((float*) &grid[x][z]);
glVertex3fv((float*) &grid[x][z+1]);
glColor3f(shade1, shade1, 0);
glVertex3fv((float*) &grid[x+1][z+1]);
}
}
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. |
/****** called from main
Vec3d extrudeShape[10];
for (int i = 0; i < 10; i++){
extrudeShape[i].x = 10*i+sin(i)*10;
extrudeShape[i].y = sin(i)*10;
extrudeShape[i].z = 0;
}
myMesh3->makeExtrusion(extrudeShape, 10, 100);
myMesh3->drawShadedSolid();
******/
void Mesh::makeExtrusion(Vec3d extrudeShape[10], int numPoints, float depth){
for (int i = 0; i < gridW; i++){
for (int x = 0; x < gridH; x++){
glColor3f(1,1,0);
grid[i][x] = Vec3d(extrudeShape[i].x, extrudeShape[i].y, x * depth/numPoints);
}
}
}
void Mesh::drawShadedSolid(){
glBegin(GL_TRIANGLES);
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+1][z];
Vec3d C = grid[x+1][z+1];
Vec3d D = grid[x][z+1];
Vec3d AB = B - A;
Vec3d AC = C - A;
Vec3d AD = D - A;
Vec3d DC = C - D;
Vec3d N1 = AC.cross(AB);
Vec3d N2 = AD.cross(AC);
N1.normalize();
N2.normalize();
Vec3d L = Vec3d(400, 500
, 0);
L.normalize();
float shade1 = L.dot(N1);
float shade2 = L.dot(N2);
if (shade1 < 0)
shade1 = 0;
if (shade2 < 0)
shade2 = 0;
glColor3f(shade1, shade1, 0);
glVertex3fv((float*) &grid[x][z]);
glVertex3fv((float*) &grid[x+1][z]);
glColor3f(shade1, 0, 0);
glVertex3fv((float*) &grid[x+1][z+1]);
glColor3f(shade1, 0, 0);
glVertex3fv((float*) &grid[x][z]);
glVertex3fv((float*) &grid[x][z+1]);
glColor3f(shade1, shade1, 0);
glVertex3fv((float*) &grid[x+1][z+1]);
}
}
glEnd();
} |
|
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. |
/*** called from main
Vec3d revolveShape[10];
for (int i = 0; i < 10; i++){
revolveShape[i].x = 10*i+sin(i)*10;
revolveShape[i].y = sin(i)*10;
revolveShape[i].z = 0;
}
myMesh4->makeRevolution(revolveShape, 10);
myMesh4->drawShadedSolid();
***/
void Mesh::makeRevolution(Vec3d revolveShape[10], int numPoints){
for (int i = 0; i < numPoints; i++){
for (int j = 0; j < gridH; j++){
float ang = 2*PI / (gridH - 1) * j;
float radius = revolveShape[i].x;
float xn = radius * cos(ang) ;
float yn = revolveShape[i].y;
float zn = radius * sin(ang);
grid[i][j] = Vec3d(xn, yn, zn);
}
}
} |
|
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 angi = PI / (gridW - 1) * i;
float angj = 2*PI / (gridH - 1) * j;
float xn = radius * sin(angi) * cos(angj);
float yn = radius * sin(angi) * sin(angj);
float zn = radius * cos(angi);
grid[i][j] = Vec3d(xn, yn, zn);
}
}
} |
|
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.
|
/*** called from main
Vec3d revolveShape[100];
for (int i = 0; i < 100; i++){
float angle = 2 * PI / 99 * i;
revolveShape[i].x = 20*sin(angle);
revolveShape[i].y = 20*cos(angle);
revolveShape[i].z = 0;
}
myMesh6->makeTorus(revolveShape, 100, 50);
myMesh6->drawShadedSolid();
***/
void Mesh::makeTorus(Vec3d revolveShape[10], int numPoints, float innerRadius){
for (int i = 0; i < numPoints; i++){
for (int j = 0; j < gridH; j++){
float ang = 2*PI / (gridH - 1) * j;
float radius = revolveShape[i].x + innerRadius;
float xn = radius * cos(ang);
float yn = revolveShape[i].y;
float zn = radius * sin(ang);
grid[i][j] = Vec3d(xn, yn, zn);
}
}
} |
|
|