Comp Form: Week 9

Meshes, smoothing. . .

//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.)
swk9creenshot_1.png

const int meshW=60;

const int meshH=60;

Vec3d mesh[meshW][meshH];void drawMesh(bool shadeOn){

for (int i=0;i

 	for (int j=0;j

Vec3d A=mesh[i][j];

 		Vec3d B=mesh[i+1][j];

 		Vec3d C=mesh[i+1][j+1];

Vec3d AB=B-A;

 		Vec3d AC=C-A;

 		Vec3d N= AB.cross(AC);

 		Vec3d normN=N/N.length();

Vec3d L(mouseX-600, mouseY-500, 0);

Vec3d AL=L-A;

 		Vec3d normL=AL/AL.length();

float shade=normL.dot(normN);

if (shadeOn) glBegin(GL_TRIANGLES);

 		else glBegin(GL_LINE_LOOP);

 		glColor3f(shade,shade,shade);

glVertex3f(A.x,A.y,A.z);

 		glVertex3f(B.x,B.y,B.z);

 		glVertex3f(C.x,C.y,C.z);

 		glEnd();

A=mesh[i][j];

 		B=mesh[i+1][j+1];

 		C=mesh[i][j+1];

if (shadeOn) glBegin(GL_TRIANGLES);

 		else glBegin(GL_LINE_LOOP);

 		glColor3f(shade,shade,shade);

 		glVertex3f(A.x,A.y,A.z);

 		glVertex3f(B.x,B.y,B.z);

 		glVertex3f(C.x,C.y,C.z);

 		glEnd();

}

 }

}

void draw3030(){

for (int i=0;i

for (int j=0;j

mesh[i][j].x=i*20-200;

mesh[i][j].y=-200;

mesh[i][j].z=j*20;

}

}

drawMesh(false);

}

//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.
wk9screenshot_2.png

void drawShadedTriangle(){	for (int i=0;i

 	for (int j=0;j

 		mesh[i][j].x=i*20-200;

 		mesh[i][j].y=-200;

 		mesh[i][j].z=j*20;

 	}

 }

 drawMesh(true);

}

//Problem 3. Create a function that makes a mountain out of your mesh.

wk9screenshot_6.png

void drawMountainMesh(){

 //pow(2.0,-1.0*x*x);//bell curve ish	for (int i=0;i

 	for (int j=0;j

 		mesh[i][j].x=i*20-600;

 			mesh[i][j].z=j*20-300;

 			mesh[i][j].y=-100-dist3f(mesh[i][j].x,0,mesh[i][j].z,mouseX,mouseY,0);

}

 }

//glPushMatrix();

 //glRotatef(count*(windowH/2-mouseY)/300,1,0,0);

 //glRotatef(count*(windowW/2-mouseX)/300,0,0,1);

drawMesh(false);

//count++;

 //glPopMatrix();

}

//Problem 4. Create a function that produces waves in one direction (along x) in your mesh.
wk9screenshot_3.png

void drawWaveMesh(){	for (int i=0;i

 	for (int j=0;j

 		mesh[i][j].x=i*20-200;

 		mesh[i][j].y=sin(i)*20-200;

 		mesh[i][j].z=j*20;

 	}

 }

 drawMesh(true);

}

//Problem 5. Create a function that produces waves in two directions (along x and z ) in your mesh.
wk9screenshot_4.png

void draw2WaveMesh(){

 for (int i=0;i<meshw;i++){>

 	for (int j=0;j<meshh;j++){>

 		mesh[i][j].x=i*20-200;

 		mesh[i][j].y=sin(i)*20+sin(j)*10-200;

 		mesh[i][j].z=j*20;

 	}

 }</meshh;j++){></meshw;i++){>	drawMesh(true);

}

//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.
wk9screenshot_5.png

int cc=0;

void drawRandomMesh(int height, float smoothing){

 int randomHeight;	for (int i=0;i

 	for (int j=0;j

if(cc

 			randomHeight=rand()%height;

mesh[i][j].x=i*20-200;

 		mesh[i][j].y=randomHeight-200;

 		mesh[i][j].z=j*20;

 		cc++;

}

 	}

 }

 for (int i=1;i

 	for (int j=1;j

 			mesh[i][j].y=mesh[i-1][j].y*smoothing+mesh[i+1][j].y*smoothing+mesh[i][j].y*(1-smoothing*4)+mesh[i][j+1].y*smoothing+mesh[i][j-1].y*smoothing;

 	}

 }

 drawMesh(true);

}