CompuForm Assigment 8: Intro to Lighting/Shading · posted by vaibhav bhawsar Nov 17, 21:23
Common Function to Generate Mesh
void generateMesh(int meshW, int meshH)
{
//gen a simple mesh of a certain width and height
for (int W=0; W<meshW ; W++)
{
for (int H=0; H<meshH ; H++)
{
float x = W - (meshW/2);
float y = 0;
float z = H - (meshH/2);
mesh[W][H] = Vec3d(x,y,z);
}
}
}
Common Function to Shade Mesh (unit triangle)
void shadedTriangle(Vec3d A, Vec3d B, Vec3d C, float Rf, float Gf, float Bf, float Af)
{
float shading;
//define vectors AB and AC
Vec3d AB = B-A;
Vec3d AC = C-A;
//and compute the cross product between them
//this is perpendicular to AB and AC
Vec3d N = AB.cross(AC);
//normalize the cross product
N.normalize();
//create the light vector and norm it
Vec3d L(mouseX,mouseY,0);
L.normalize();
//shading is the dot product, Light (L) is projected onto N
shading = L.dot(N);
if(shading <= 0) shading = 0;
//compose the final tone of color
glColor4f(Rf*shading,Gf*shading,Bf*shading, Af);
glPushMatrix();
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();
}
Problem 1: Create a 30 × 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 × 30.)
void drawMesh()
{
//draw the mesh
//draw along Height (depth z) while incrementing Width
for (int W=0; W<meshW ; W++)
{
glBegin(GL_LINE_STRIP);
for (int H=0; H<meshH ; H++)
{
Vec3d pt = mesh[W][H];
glVertex3f(pt.x,pt.y,pt.z);
}
glEnd();
}
//draw along WIDTH while incrementing HEIGHT (depth z)
for (int H=0; H<meshH ; H++)
{
glBegin(GL_LINE_STRIP);
for (int W=0; W<meshW ; W++)
{
Vec3d pt = mesh[W][H]; //draw along W
glVertex3f(pt.x,pt.y,pt.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 drawFillMesh()
{
for (int z=0; z < meshW-1 ; z++)
{
for (int x=0; x < meshH-1 ; x++)
{
Vec3d A = mesh[x][z];
Vec3d B = mesh[x][z+1];
Vec3d C = mesh[x+1][z+1];
shadedTriangle(A,B,C,.5,.01,1,.8);
A = mesh[x][z];
B = mesh[x+1][z+1];
C = mesh[x+1][z];
shadedTriangle(A,B,C,.5,.01,1,.8);
}
}
}
Problem 3: Create a function that makes a mountain out of your mesh.
generateMountain() is called instead of generateMesh() after which drawFillMesh() is called.
void generateMountain()
{
//gen a simple mesh of a certain width and height
for (int W=0; W<meshW ; W++)
{
for (int H=0; H<meshH ; H++)
{
float x = W - (meshW/2);
float z = H - (meshH/2);
float y = 5*(cos(x/5) + cos (z/5));
mesh[W][H] = Vec3d(x,y,z);
}
}
}
Problem 4: Create a function that produces waves in one direction (along x) in your mesh.
void generateXWave()
{
//gen a simple mesh of a certain width and height
float amplitude = 5.0;
float wavelength = 10.0;
for (int W=0; W<meshW ; W++)
{
for (int H=0; H<meshH ; H++)
{
float x = W - (meshW/2);
float z = H - (meshH/2);
float y = ( amplitude * d_cos( x * (180/wavelength)) );
y = fabs(y);
mesh[W][H] = Vec3d(x,y,z);
}
}
}
Problem 5: Create a function that produces waves in two directions (along x and z ) in your mesh.
void generateXYWave()
{
//gen a simple mesh of a certain width and height
for (int W=0; W<meshW ; W++)
{
for (int H=0; H<meshH ; H++)
{
float x = W - (meshW/2);
float z = H - (meshH/2);
float y = 2*(cos(x/2) + cos (z/5)) ;
y = fabs(y);
mesh[W][H] = Vec3d(x,y,z);
}
}
}
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 generateRandomMesh(int meshW, int meshH)
{
//gen a mesh of a certain width and random height using the values stored in randomPoints[] (Y)
for ( int W=0 ; W < meshW ; W++ )
{
for ( int H=0 ; H < meshH ; H++ )
{
float x = W - (meshW/2);
int phase = ((int)(p*0.8)-H) % meshW;
float y = -4;
if(W>1 && H>1 && W<meshW-1 && H<meshH-1)
{
y = randomPoints[W][phase];
}
//float y = randomPoints[W][H];
float z = H - (meshH/2);
mesh[W][H] = Vec3d(x,y,z);
}
}
//SMOOTH THE MESH
if(meshSmoothing)
{
for ( int sW=1 ; sW < meshW-1 ; sW++ )
{
for ( int sH=1 ; sH < meshH-1 ; sH++ )
{
Vec3d currentP = mesh[sW][sH];
Vec3d TopP = mesh[sW][sH-1];
Vec3d BottomP = mesh[sW][sH+1];
Vec3d LeftP = mesh[sW-1][sH];
Vec3d RightP = mesh[sW+1][sH];
Vec3d TopRightP = mesh[sW+1][sH-1];
Vec3d TopLeftP = mesh[sW-1][sH-1];
Vec3d BottomRightP = mesh[sW+1][sH+1];
Vec3d BottomLeftP = mesh[sW-1][sH+1];
Vec3d smoothP = currentP*0.2 +
TopP *0.1 +
BottomP * 0.1 +
LeftP * 0.1 +
RightP * 0.1 +
TopRightP * 0.1 +
TopLeftP * 0.1 +
BottomRightP * 0.1 +
BottomLeftP * 0.1 ;
mesh[sW][sH] = smoothP;
}
}
}
}
Problem 7 ?
Commenting is closed for this article.




