Week 8 Assignment (desc)
Younghyun Chung
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.)
|
void drawW8_1( void )
{
glColor3f( 0,0,1 );
for( int x=0; x<meshW; x++ )
{
for( int y=0; y<meshH; y++ )
{
mesh[x][y].x = x*20 - 400 + mouseX*0.5;
mesh[x][y].y = 20*sin(x) - 200;
//mesh[x][y].y = 3 * fmod( x,4 ) - 200;
mesh[x][y].z = y*20 - 200 + mouseY*0.5;
}
}
for( int x=0; x<meshW-1; x++ )
{
for( int y=0; y<meshH-1; y++ )
{
Vec3d A = mesh[x][y];
Vec3d B = mesh[x+1][y+1];
Vec3d C = mesh[x][y+1];
glBegin( GL_LINES );
glVertex3f( A.x, A.y, A.z );
glVertex3f( B.x, B.y, B.z );
glEnd();
glBegin( GL_LINES );
glVertex3f( A.x, A.y, A.z );
glVertex3f( C.x, C.y, C.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 drawShadedTriangle ( Vec3d A, Vec3d B, Vec3d C )
{
//float newMouseX = mouseX - windowW/2;
//float newMouseY = mouseY - windowH/2;
glPushMatrix();
//glRotatef( angle, 0,1,0 );
// define vectors within triangle
Vec3d AB = B - A;
Vec3d AC = C - A;
// compute normal to triangle
Vec3d Normal = AB.cross( AC );
// normalize the normal ( make length = 1.0 )
Normal.normalize();
// set up lighting vector
Vec3d L( -mouseX+windowW/2,mouseY-windowH,0 );
L.normalize();
// compute shade
float shade = L.dot( Normal );
glColor3f( shade*0.5, shade*1, 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();
glPopMatrix();
angle += 0.0001;
}
void drawWireframeMesh()
{
for( int x=0; x<meshW-1; x++ )
{
for( int y=0; y<meshH-1; y++ )
{
Vec3d A = mesh[x][y];
Vec3d B = mesh[x+1][y];
Vec3d C = mesh[x+1][y+1];
drawShadedTriangle( A, B, C );
A = mesh[x][y];
B = mesh[x+1][y+1];
C = mesh[x][y+1];
drawShadedTriangle( A, B, C );
}
}
}
|
 :
|
Problem 3.
Create a function that makes a mountain out of your mesh.
|
void drawW8_3 ( void )
{
for( int x=10; x<=meshW-10; x++ )
{
for( int y=10; y<=meshH-10; y++ )
{
mesh[x][y].y = (15*sin(x) - 200 + cos(y)*10)+50;
}
}
drawWireframeMesh();
}
|
 :
|
Problem 4.
Create a function that produces waves in one direction (along x) in your mesh.
|
void drawW8_4( float alpha )
{
for( int x=0; x<meshW; x++ )
{
for( int y=0; y<meshH; y++ )
{
mesh[x][y].x = x*20 - 400 + mouseX*0.5;
mesh[x][y].y = 20*sin(x+alpha) - 200;
mesh[x][y].z = y*20 - 200 + mouseY*0.5;
}
}
drawWireframeMesh();
}
|
 :
|
Problem 5.
Create a function that produces waves in two directions (along x and z ) in your mesh.
|
void drawW8_5( float alpha )
{
for( int x=0; x<meshW; x++ )
{
for( int y=0; y<meshH; y++ )
{
mesh[x][y].x = x*20 - 400 + mouseX*0.5;
mesh[x][y].y = 20*sin(x+alpha) - 200 + 5*cos((y+alpha*5));
mesh[x][y].z = y*20 - 200 + mouseY*0.5;
}
}
drawWireframeMesh();
}
|
 :
|
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 drawW8_6 ( float height, float smooth )
{
for( int x=0; x<meshW; x++ )
{
for( int y=0; y<meshH; y++ )
{
mesh[x][y].x = mesh2[x][y].x;
mesh[x][y].y = clamp( mesh2[x][y].y, -1 * height - 200, 1 * height -200);
mesh[x][y].z = mesh2[x][y].z;
}
}
for( int x=1; x<meshW-1; x++ )
{
for( int y=1; y<meshH-1; y++ )
{
//center, up down right left
Vec3d C = mesh[x][y];
Vec3d U = mesh[x][y+1];
Vec3d D = mesh[x][y-1];
Vec3d R = mesh[x+1][y];
Vec3d L = mesh[x-1][y];
Vec3d D1 = mesh[x+1][y+1]; // diagnal 1,2,3,4
Vec3d D2 = mesh[x-1][y+1];
Vec3d D3 = mesh[x-1][y-1];
Vec3d D4 = mesh[x+1][y-1];
mesh[x][y].y = C.y * (1-smooth)
+ 0.15 * smooth * ( U.y + D.y + R.y + L.y )
+ 0.1 * smooth * ( D1.y + D2.y + D3.y + D4.y );
}
}
drawWireframeMesh();
}
|
 :
|
Problem 7.
Create a function that produces multiple vertical distortions in your mesh. Use the bell-curve function to distort the mesh about a series of points located around the mesh.
|
void drawW8_7 ( void )
{
for( int x=0; x<meshW; x++ )
{
for( int y=0; y<meshH; y++ )
{
mesh2[x][y].x = x*30 - 400 + mouseX*0.5;
mesh2[x][y].y = 5*sin(x) - 200 + 5*cos((y));
mesh2[x][y].z = y*30 - 300 + mouseY*0.5;
}
}
for( int x=0; x<meshW; x++ )
{
for( int y=0; y<meshH; y++ )
{
mesh[x][y].x = mesh2[x][y].x;
mesh[x][y].y = mesh2[x][y].y;
mesh[x][y].z = mesh2[x][y].z;
}
}
drawWireframeMesh();
}
void drawW8_7a ( void )
{
for( int x=0; x<meshW; x++ )
{
for( int y=0; y<meshH; y++ )
{
mesh[x][y].x = mesh2[x][y].x;
mesh[x][y].y = mesh2[x][y].y;
mesh[x][y].z = mesh2[x][y].z;
Vec3d distortP = Vec3d( 0, -100, 0 );
Vec3d dV = mesh[x][y] - distortP;
float distance = dV.length();
dV.normalize();
float distortY = dV.y * 300 * pow( 2.0, -1.0 * dV.y * dV.y );
mesh[x][y].y += distortY;
}
}
drawWireframeMesh();
}
|
 :
|
Problem 8.
This problem is optional. Create a function that uses a small grayscale image to distort the mesh. The image should be a 30 x 30 grayscale image saved in the RAW format in Photoshop. This requires some deft C programming, and will utilize the following functions: fopen, malloc, fread and fclose.
|
|
 :
|
|