CompuForm Assigment 7: 3D · posted by vaibhav bhawsar Nov 2, 19:00

cylinder wireframe extrude a 2d set of points (a wave in this case)

Problem 1: Create a function called drawWireframeCylinder that takes as input a radius (float), a height (float) and a number of steps (int). The function should draw a series of circles stacked vertically as specified. Draw vertical lines between the circles to complete wireframe effect.

void cylinder(float radius, float height, int nSteps)
{
	int nPoints = 30;
	Vec3d aShape[nSteps][nPoints];
	
	glPushMatrix();
	glRotatef(mouseY,1,0,0);
	
	color(255,0,255,255);
	for (int D =0 ; D < nSteps ; D++) //number of rings
	{
		glBegin(GL_LINE_LOOP);//draw each ring
		for ( int A = 0 ; A < nPoints ; A++ )
		{
			float ang = A / (float) nPoints * 2 * PI;
			float x = radius * cos (ang);
			float y = D * (height/nSteps) - height/2;
			float z = radius * sin (ang);
			glVertex3f(x,y,z);
			aShape[D][A] = Vec3d(x,y,z);
		}
		glEnd();
	}
	
	color(0,0,255,255);
	for (int A =0 ; A < nPoints ; A++) //iterate lat, X
	{
		glBegin(GL_LINE_LOOP); //draw each ring iterate long, Y
		for ( int D = 0 ; D < nSteps ; D++ )
		{
			Vec3d a = aShape[D][A];
			glVertex3f(a.x,a.y,a.z);
		}
		glEnd();
	}
	glPopMatrix();	
}

Problem 2: Create a function called drawWireframeExtrusion that takes as input an array of points (Vec2d*), the number of points (int), a height (float) and a number of steps (int). The function should draw a wireframe extrusion of the two-dimensional form that is passed in.

//globals 
//pixels
int wavePLength = 720; 
Vec2d waveP [720];
void drawCrest(Vec2d v, float wavelength, float amplitude)
{	
	
	glBegin(GL_LINE_STRIP);
	glColor3f( .8,.2,.4 );
	for (int i =0; i<wavePLength; i++)
	{
		float X = i;
		float Y = ( amplitude * d_sin( X *  (360/wavelength)) );
		Y = fabs(Y);
		glVertex2f(X+v.x,Y+v.y);
		Vec2d p (X+v.x,Y+v.y);
		waveP[i] = p;
	}
	glEnd();
}
void extrude (int nSteps,int height)
{
	drawCrest(Vec2d(-200,0),200,50);
	glPushMatrix();
	glRotatef(mouseY,1,0,0);
	color(255,255,0,255);
	for (int D =0 ; D < nSteps ; D++) //number of rings
	{
		glBegin(GL_LINE_STRIP); //draw each ring
		for ( int A = 0 ; A < wavePLength ; A++ )
		{
			float x = waveP[A].x;
			float y = D * (height/nSteps) - height/2;
			float z = waveP[A].y;
			glVertex3f(x,y,z);
			//aShape[D][A] = Vec3d(x,y,z);
		}
		glEnd();
	}
	glPopMatrix();
}

Problem 3. Create a function called drawWireframeRevolution that takes as input an array of points (Vec2d) and the number of points (int). The function should uses the array of points as a revolution profile. To do this, draw a circle for each point in the array where the circle’s radius is equal to the point’s X component, and the the circle’s vertical height is equal to the circle’s Y value. Complete the wireframe by drawing the vertical lines.

void drawWireframeRevolution()
{
	//generate a vertical bell curve
	int length = meshH;
	Vec2d p[length];
	glLineWidth(4);
	glBegin(GL_LINE_STRIP);
	glColor4f( .5,.5,.5,.5 );
	for (int i = 0 ; i < length ; i++)
	{
		float Y = i;
		float amp = 20;
		float n = Y/length;
		float falloff = pow ( 5.0, -1.0 * n * n );
		float X = amp * d_sin( n * 360) * falloff;
		glVertex2f(X,Y);
		Vec2d pt(X,Y);
		p[i] = pt;
	}
	glEnd();
	
	int nPoints = meshW;
	int height = length;
	int radius = 0;
	
	glPushMatrix();
	//glRotatef(mouseY,1,0,0);
	
	color(255,0,255,255);
	glLineWidth(1);
	for (int D =0 ; D < height ; D++) //number of rings
	{
		radius = p[D].x;
		glBegin(GL_LINE_LOOP);//draw each ring
		for ( int A = 0 ; A < nPoints ; A++ )
		{
			float ang = A / (float) nPoints * 2 * PI;
			float x = radius * cos (ang);
			float y = p[D].y;
			float z = radius * sin (ang);
			//glVertex3f(x,y,z);
			mesh[D][A] = Vec3d(x,y,z);
		}
		glEnd();
		
	}
	color(0,0,255,255);
	for (int A =0 ; A < nPoints ; A++) //iterate lat, X
	{
		glBegin(GL_LINE_STRIP);//draw each ring iterate long, Y
		for ( int D = 0 ; D < height; D++ )
		{
			Vec3d a = mesh[D][A];
			//glVertex3f(a.x,a.y,a.z);
		}
		glEnd();
	}
	glPopMatrix();
	
	drawWarpedFillMesh();
}
void drawWarpedFillMesh()
{
	for (int z=0; z < meshH-1; z++)
	{
		for (int x=0; x < meshW-1; x++)
		{
			Vec3d A = mesh[x][z];
			Vec3d B = mesh[x+1][z];
			Vec3d C = mesh[x+1][z+1];
			color(200,200,200,200);
			//fillTriangle(A,B,C);
			fillShadedTriangle(A,B,C,.1,.01,1,.8);
			
			A = mesh[x][z];
			B = mesh[x+1][z+1];
			C = mesh[x][z+1];
			color(255,255,255,235);			
			//fillTriangle(A,B,C);
			fillShadedTriangle(A,B,C,.1,.01,1,.8);
		}
	}
	for (int y=0; y < meshW-1; y++)
	{
		Vec3d A = mesh[y+1][0];
		Vec3d B = mesh[y][0];
		Vec3d C = mesh[y+1][meshH-1];
		//color(255,0,0,200);
		//fillTriangle(A,B,C);
		fillShadedTriangle(A,B,C,.1,.01,1,.8);
		
		A = mesh[y][0];
		B = mesh[y][meshH-1];
		C = mesh[y+1][meshH-1];
		//color(0,255,0,235);			
		//fillTriangle(A,B,C);
		fillShadedTriangle(A,B,C,.1,.01,1,.8);
	}
}

Comment

Commenting is closed for this article.