⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cylinder.cpp

📁 C arm 3D traslate 模擬C手臂在三度空間移動
💻 CPP
字号:
// Cylinder.cpp: implementation of the CCylinder class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "math.h"
#include "OpenGLtest.h"
#include "Cylinder.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CCylinder::CCylinder()
{
	m_Height=0.0;	
	m_Diameter=0.0;
	
	for(int i=0;i<3;i++)
    m_BottonCenterPt[i]=0;
}

CCylinder::~CCylinder()
{

}
void CCylinder::Draw(CDC *pCDC,HGLRC hRC)
{
/*
	float x,z,angle;			
	float corners[2][3];	// Storeage for vertex calculations
	float step = (3.1415f/50.0f);	// Approximate the cylinder wall with
									// 100 flat segments.
	

	glColor3f(0.0f, 0.0f, 0.7f);

	// ┏籠
	glBegin(GL_TRIANGLE_FAN);
	
		
		// Center of fan is at the origin
		glVertex3f(0.0f, -m_Height/2.0f, 0.0f);
		
		// Spin around matching step size of cylinder wall
		for(angle = (2.0f*3.1415f); angle > 0.0f; angle -= step)
			{
			// Calculate x and y position of the next vertex
			x = m_Diameter*(float)sin(angle);
			z = m_Diameter*(float)cos(angle);

			// Specify the next vertex for the triangle fan
			glVertex3f(x, -m_Height/2.0f, z);
			}
		
		// Be sure loop is closed by specifiying initial vertex
		// on arc as the last too
		glVertex3f(m_Diameter*(float)sin(2.0f*3.1415f),
					-m_Height/2.0f,
					m_Diameter*(float)cos(2.0f*3.1415f));
	glEnd();
	//郴籠
	glBegin(GL_TRIANGLE_FAN);
	
		
		// Center of fan is at the origin
		glVertex3f(0.0f, m_Height/2.0f, 0.0f);
		
		// Spin around matching step size of cylinder wall
		for(angle = (2.0f*3.1415f); angle > 0.0f; angle -= step)
			{
			// Calculate x and y position of the next vertex
			x = m_Diameter*(float)sin(angle);
			z = m_Diameter*(float)cos(angle);

			// Specify the next vertex for the triangle fan
			glVertex3f(x, m_Height/2.0f, z);
			}
		
		// Be sure loop is closed by specifiying initial vertex
		// on arc as the last too
		glVertex3f(m_Diameter*(float)sin(2.0f*3.1415f),
					m_Height/2.0f,
					m_Diameter*(float)cos(2.0f*3.1415f));
	glEnd();

	//蛾琖
	glBegin(GL_QUAD_STRIP);

	for(angle = (2.0f*3.1415f); angle > 0.0f; angle -= step)
		{
		// Calculate x and y position of the first vertex
		x = m_Diameter*(float)sin(angle);
		z = m_Diameter*(float)cos(angle);
	
		// Get the coordinate for this point and extrude the 
		// length of the cylinder.
		corners[0][0] = x;
		corners[0][1] = -m_Height/2.0f;
		corners[0][2] = z;

		corners[1][0] = x;
		corners[1][1] = m_Height/2.0f;
		corners[1][2] = z;

		glVertex3fv(corners[0]);
		glVertex3fv(corners[1]);
		}

		// Make sure there are no gaps by extending last quad to
		// the original location
		glVertex3f(m_Diameter*(float)sin(2.0f*3.1415f),
					-m_Height/2.0f,
					m_Diameter*(float)cos(2.0f*3.1415f));

		glVertex3f(m_Diameter*(float)sin(2.0f*3.1415f),
					m_Height/2.0f,
					m_Diameter*(float)cos(2.0f*3.1415f));

	glEnd();	// Done with cylinder sides

*/	

	//**************************  
//Draw cylinder
	     int d=500;
		int r=1;
		float X1,Y1,X2,Y2;
		double GL_PI=3.1415;
	    double i;
     //down
		glBegin(GL_TRIANGLE_FAN);
	    glColor3f(0.f,1.f,0.f);
		glVertex3d(0.0,0.0,0.0);
	
        for(i=0;i<(2*GL_PI);i+=(GL_PI/d))
		{
		  X1=(float)r*(float)sin(i);
		  Y1=(float)r*(float)cos(i);
          glVertex3d(X1,Y1,0);
	
		}
		  glEnd();
     //up 
		glBegin(GL_TRIANGLE_FAN);
	    glColor3f(0.f,1.f,0.f);
		glVertex3d(0.0f,0.0f,1.0f);
        for(i=0;i<(2*GL_PI);i+=(GL_PI/d))
		{
		  X1=(float)r*(float)sin(i);
		  Y1=(float)r*(float)cos(i);
          glVertex3d(X1,Y1,1.0f);
	
		}
		  glEnd();

//Draw surface

		glBegin(GL_LINES);
	    glColor3f(0.f,1.f,0.f);
		for(i=0;i<(2*GL_PI);i+=(GL_PI/d))
		{
		  X1=(float)r*(float)sin(i);
		  Y1=(float)r*(float)cos(i);
		  glVertex3f(X1,Y1,0.0f);
          X2=(float)r*(float)sin(i);
		  Y2=(float)r*(float)cos(i);
          glVertex3f(X2,Y2,1.0f);
		
		}
		  glEnd();
    
//***********************************	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -