Week 8 Assignment
Yasmin Elayat
Problem 1. Create a 30 x 30 mesh using a two-dimensional array of Vec3D's. Then create a function that draws your mesh as a wireframe. (Be sure to use "const int" variables represent the width and height of your mesh, instead of hard-coding it always to be 30 x 30.)
|
const int meshW = 30;
const int meshH = 30;
Vec3d mesh[meshW][meshH];
void drawMeshWireFrame() {
glColor3f(1,1,1);
glPointSize(2.0);
glBegin(GL_POINTS);
//set up mesh array
for(int x =0; x < meshW; x++) {
for(int z =0; z < meshH; z++){
float y =-10; //-10 + sin(x);//10*sin(x);
mesh[x][z].x =x*20;
mesh[x][z].y = y*20; //15*sin(x) - 200 + cos(y + mouseX*0.1)*10; //
mesh[x][z].z =z*20;
glVertex3f(mesh[x][z].x-300, mesh[x][z].y, mesh[x][z].z);
}
}
glEnd();
}
|
 |
Problem 2. Create a function that draws your mesh a solid surface and sets the color of each face according to the simple lighting model discussed in class.
|
void drawShadedMesh()
{
for(int x =0; x < 30-1; x++) {
for(int z =0; z <30-1; z++){
Vec3d A = mesh[x][z];
Vec3d B = mesh[x+1][z] ;
Vec3d C = mesh[x+1][z+1];
drawShadedTriangle(A, B, C);
A = mesh[x][z];
B = mesh[x+1][z+1] ;
C = mesh[x][z+1];
drawShadedTriangle(A, B, C);
}
}
}
|
 |
Problem 3. Create a function that makes a mountain out of your mesh.
|
void drawMountainMesh()
{
glColor3f(1,1,1);
glPointSize(2.0);
glBegin(GL_POINTS);
//set up mesh array
for(int x =0; x < meshW; x++) {
for(int z =0; z < meshH; z++){
//Y = Amplitude * sin( X * frequency + phase ); or...
//Y = Amplitude * sin( X * (2.0*PI/ wavelength) + phase );
float y =-10 + 20*sin(x * (2.0*PI/ 50));//10*sin(x);
mesh[x][z].x =(x*20) - 300;
mesh[x][z].y = y*20; //15*sin(x) - 200 + cos(y + mouseX*0.1)*10; //
mesh[x][z].z =z*20;
glVertex3f(mesh[x][z].x, mesh[x][z].y, mesh[x][z].z);
}
}
glEnd();
// drawShadedMesh();
}
|
 |
Problem 4. Create a function that produces waves in one direction (along x) in your mesh.
|
void drawUniDirWav() {
glColor3f(1,1,1);
glPointSize(2.0);
glBegin(GL_POINTS);
//set up mesh array
for(int x =0; x < meshW; x++) {
for(int z =0; z < meshH; z++){
float y =-10 + sin(x);//10*sin(x);
mesh[x][z].x =(x*20)-300;
mesh[x][z].y = y*20; //15*sin(x) - 200 + cos(y + mouseX*0.1)*10; //
mesh[x][z].z =z*20;
glVertex3f(mesh[x][z].x, mesh[x][z].y, mesh[x][z].z);
}
}
glEnd();
drawShadedMesh();
}
|
 |
Problem 5. Create a function that produces waves in two directions (along x and z ) in your mesh.
|
void drawDualDirWav() {
// glPushMatrix();
// glRotatef(mouseY, 1,0,0);
glColor3f(1,1,1);
glPointSize(2.0);
glBegin(GL_POINTS);
//set up mesh array
for(int x =0; x < meshW; x++) {
for(int z =0; z < meshH; z++){
float y =-10 + sin(x) + sin(z);//10*sin(x);
mesh[x][z].x =(x*20)-300;
mesh[x][z].y = y*20; //15*sin(x) - 200 + cos(y + mouseX*0.1)*10; //
mesh[x][z].z =z*20;
glVertex3f(mesh[x][z].x, mesh[x][z].y, mesh[x][z].z);
}
}
glEnd();
drawShadedMesh();
// glPopMatrix();
}
|
 |
Problem 6. Create a function that adds random variation to your mesh. Then using a two-dimensional smoothing function, smooth out the mesh. The smoothing function for each point should take into acount the four points directly connected to it. It may also include the other four points that are diagonally related to it. The inputs to this function should be the height of the random variation, and the level of smoothing.
|
void drawRandomMesh() {
srand ( 10 );
// glPushMatrix();
// glRotatef(mouseY, 1,0,0);
glColor3f(1,1,1);
glPointSize(2.0);
glBegin(GL_POINTS);
//set up mesh array
for(int x =0; x < meshW; x++) {
for(int z =0; z < meshH; z++){
float y = (rand()%100) + sin(x) + sin(z);//10*sin(x);
mesh[x][z].x =(x*20)-300;
mesh[x][z].y = y-100; //15*sin(x) - 200 + cos(y + mouseX*0.1)*10; //
mesh[x][z].z =z*20;
glVertex3f(mesh[x][z].x, mesh[x][z].y, mesh[x][z].z);
}
}
glEnd();
drawShadedMesh();
// glPopMatrix();
}
void smoothRandomMesh()
{
srand ( 10 );
// glPushMatrix();
// glRotatef(mouseY, 1,0,0);
glColor3f(1,1,1);
glPointSize(2.0);
glBegin(GL_POINTS);
//set up mesh array
for(int x =0; x < meshW; x++) {
for(int z =0; z < meshH; z++){
float y = (rand()%100) + sin(x) + sin(z);//10*sin(x);
mesh[x][z].x =(x*20)-300;
mesh[x][z].y = y-100; //15*sin(x) - 200 + cos(y + mouseX*0.1)*10; //
mesh[x][z].z =z*20;
glVertex3f(mesh[x][z].x, mesh[x][z].y, mesh[x][z].z);
}
}
glEnd();
float w1 = 0.25;
float w2 = 0.5;
float w3 = 0.25;
glColor3f(1,0,0);
glBegin(GL_POINTS);
for(int x =0; x < meshW; x++) {
for(int z =1; z < meshH; z++){
Vec3d smoothPt;
//current point + prev + next
smoothPt.x = mesh[x][z].x*w2 + mesh[x][z-1].x*w1 + mesh[x][z+1].x*w3;
smoothPt.y = mesh[x][z].y*w2 + mesh[x][z-1].y*w2 + mesh[x][z+1].y*w2;
smoothPt.z = mesh[x][z].z*w2 + mesh[x][z-1].z*w2 + mesh[x][z+1].z*w2;
glVertex3f(smoothPt.x, smoothPt.y, smoothPt.z);
mesh[x][z].x =smoothPt.x ;
mesh[x][z].y = smoothPt.y;
mesh[x][z].z =smoothPt.z;
}
}
glEnd();
drawShadedMesh();
// glPopMatrix();
}
|
 |
|