Comp Form: Week 10

Mesh class, extrusion, sphere. . .

//Problem 1. Add a function called drawWirefame to the Mesh class. This function should draw the Mesh as a wireframe.
w10screenshot_1.png

void mesh::drawWireframe()
{
	for (int i = 0; i<gridw-1;>
	{
		for (int j =0; j<gridh-1;>
		{

			//glVertex3f( grid[i][j].x, grid[i][j].y, grid[i][j].z ); // this is a hack to avoid 3fv below 

			glBegin( GL_LINE_LOOP );
			glVertex3fv( (float*) &amp;grid[i][j]);
			glVertex3fv( (float*) &amp;grid[i+1][j] );
			glVertex3fv( (float*) &amp;grid[i+1][j+1]);
			glEnd();

			glBegin( GL_LINE_LOOP );
			glVertex3fv( (float*) &amp;grid[i][j]  );
			glVertex3fv( (float*) &amp;grid[i][j+1] );
			glVertex3fv( (float*) &amp;grid[i+1][j+1] );
			glEnd();

		}
	}
}
</gridh-1;></gridw-1;>

//Problem 2. Add a function called drawShadedSolid to the Mesh class. This function should draw the Mesh as a solid form with shading.
w10screenshot_2.png

void mesh::drawShaded()
{
	for (int i = 0; i<gridw-1;>
	{
		for (int j =0; j<gridh-1;>
		{

			//glVertex3f( grid[i][j].x, grid[i][j].y, grid[i][j].z ); // this is a hack to avoid 3fv below 

			Vec3d A=grid[i][j];
			Vec3d B=grid[i+1][j];
			Vec3d C=grid[i+1][j+1];

			Vec3d AB=B-A;
			Vec3d AC=C-A;
			Vec3d N=AB.cross(AC);
			Vec3d normN=N/N.length();

			Vec3d L(100,-100,500);

			Vec3d AL=L-A;
			Vec3d normL=AL/AL.length();
			//Vec3d normL=L/L.length();

			float shade=normL.dot(normN);

			glColor3f(shade,shade,shade);

			glBegin( GL_TRIANGLES );
			glVertex3fv( (float*) &amp;grid[i][j]);
			glVertex3fv( (float*) &amp;grid[i+1][j] );
			glVertex3fv( (float*) &amp;grid[i+1][j+1]);
			glEnd();

			glBegin( GL_TRIANGLES );
			glVertex3fv( (float*) &amp;grid[i][j]  );
			glVertex3fv( (float*) &amp;grid[i][j+1] );
			glVertex3fv( (float*) &amp;grid[i+1][j+1] );
			glEnd();
		}
	}
}</gridh-1;></gridw-1;>

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

void mesh::makeExtrusion(Vec2d shape[], int n, int height)
{
	for (int i =0; i<n;>
	{
		for (int j =0; j<height;>
		{
			grid[i][j].x = shape[i].x;
			grid[i][j].y = shape[i].y;
			grid[i][j].z = j*5;

		}
	}
}
</height;></n;>

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

void mesh::makeRevolution(Vec2d shape[], int n, int rev)
{
	for (int i =0; i
	{
		for (int j =0; j
		{

			grid[i][j].x = shape[i].x;
			grid[i][j].y = shape[i].y*cos((float)j/rev*PI);
			grid[i][j].z = shape[i].y*sin((float)j/rev*PI);

		}
	}
}

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

void mesh::makeSphere(int width, int height)
{

	const int numPoints=100;
	Vec2d P;
	Vec2d center(0,0);

	//glBegin(GL_LINE_LOOP);

	for (int i=0;i<numpoints;i++){>
	P.x=cos((float)i/numPoints*PI)*width;
	P.y=sin((float)i/numPoints*PI)*height;

	P=P+center;

		for (int j =0; j<width;>
		{

			grid[i][j].x = P.x;
			grid[i][j].y = P.y*cos((float)j/width*2*PI);
			grid[i][j].z = P.y*sin((float)j/width*2*PI);

		}
	}
}</width;></numpoints;i++){>