opengl生成空心圆柱,测试可用生成空心圆柱,每一步都有注释可用。

opengl生成空心圆柱,测试可用生成空心圆柱,每一步都有注释,可用。

#include <gl/glut.h>
#include <gl/GLU.h>
#include <gl/GL.h>
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glut32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "glut.lib")


#include <math.h>
void Cube()
{
	glBegin(GL_QUAD_STRIP);//填充凸多边形
	glVertex3f(0.0f, 0.0f, 0.0f);
	glVertex3f(0.0f, 1.0f, 0.0f);
	glVertex3f(1.0f, 0.0f, 0.0f);
	glVertex3f(1.0f, 1.0f, 0.0f);
	glVertex3f(1.0f, 0.0f, -1.0f);
	glVertex3f(1.0f, 1.0f, -1.0f);
	glVertex3f(0.0f, 0.0f, -1.0f);
	glVertex3f(0.0f, 1.0f, -1.0f);
	glVertex3f(0.0f, 0.0f, 0.0f);
	glVertex3f(0.0f, 1.0f, 0.0f);
	glEnd();
	glBegin(GL_QUAD_STRIP);
	glVertex3f(0.0f, 0.0f, 0.0f);
	glVertex3f(1.0f, 0.0f, 0.0f);
	glVertex3f(0.0f, 0.0f, -1.0f);
	glVertex3f(1.0f, 0.0f, -1.0f);
	glVertex3f(0.0f, 1.0f, 0.0f);
	glVertex3f(1.0f, 1.0f, 0.0f);
	glVertex3f(0.0f, 1.0f, -1.0f);
	glVertex3f(1.0f, 1.0f, -1.0f);
	glEnd();
}
void Circle()
{
	glBegin(GL_TRIANGLE_FAN);//扇形连续填充三角形串
	glVertex3f(0.0f, 0.0f, 0.0f);//坐标顶点
	int i = 0;
	for (i = 0; i <= 100; i += 15)
	{
		float p = i * 3.14 / 180;
		glVertex3f(sin(p), cos(p), 0.0f);//坐标顶点 每个点
	}
	glEnd();
}

void Cylinder(double j)//j 代表倍率 //外面的圆柱
{
	glBegin(GL_QUAD_STRIP);//连续填充四边形串 GL_QUAD_STRIP 连续填充四边形串   mode:创建图元的类型
	int i = 0;
	for (i = 0; i <= 360; i += 15)
	{
		float p = i * 3.14 / 180;
		glVertex3f(j*sin(p), j*cos(p), 1.0f);//指定每个点的坐标顶点  坐标 角度360应该就好
		glVertex3f(j*sin(p), j*cos(p), 0.0f);
	}
	glEnd(); //结束 与glBegin对应
	//Circle();//调用方法圆
	//glTranslatef(0, 0, 1);//平移变换
	//Circle();
}



void Cylinder_Bottom(double j)//j 代表倍率 //外面的圆柱
{
	glBegin(GL_TRIANGLE_STRIP);
	int i = 0;
	for (i = 0; i <= 360; i += 15)
	{
		float p = i * 3.14 / 180;
		glVertex3f(j*sin(p), j*cos(p), 0.0f);
		glVertex3f(sin(p), cos(p), 0.0f);
	}
	glEnd(); 
}



void Cylinder_Hollow()//j 代表倍率 //外面的圆柱
{
	Cylinder(1);
	Cylinder(0.618);
	Cylinder_Bottom(0.618);
	glTranslatef(0, 0, 1);//平移变换
	Cylinder_Bottom(0.618);
}


void Cone()
{
	glBegin(GL_QUAD_STRIP);//连续填充四边形串
	int i = 0;
	for (i = 0; i <= 390; i += 15)
	{
		float p = i * 3.14 / 180;
		glVertex3f(0, 0, 1.0f);
		glVertex3f(sin(p), cos(p), 0.0f);
	}
	glEnd();
	Circle();
}
void renderScene(void)//渲染
{
	//static float i = 0;
	//i += 0.01;
	//if (i > 360)
	//	i = 0;
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//清除初始化
	glLoadIdentity();//功能是重置当前指定的矩阵为单位矩阵

	glPushMatrix();//当前矩阵堆栈推送,通过一个,复制当前矩阵
	glColor3f(0, 1, 0);//颜色设置

	//glTranslatef(-2, 2.0, -12); //沿X轴正方向平移x个单位(x是有符号数) Y Z 平移矩阵


	glTranslatef(0, 0, -12); //沿X轴正方向平移x个单位(x是有符号数) Y Z 平移矩阵

    glRotatef(45, 1, 1, 1);//旋转矩阵 以点(0,0,0)到点(x,y,z)为轴,旋转i角度;

	//glRotatef(i, 1, 1, 1);//旋转矩阵 以点(0,0,0)到点(x,y,z)为轴,旋转i角度;
	//Cylinder();//函数
	Cylinder_Hollow();
	glPopMatrix();//出 对应上方进  消除上一次的变换对本次变换的影响。使本次变换是以世界坐标系的原点为参考点进行

	//glPushMatrix();
	//glColor3f(1, 0, 0);
	//glTranslatef(2, 2, -12);
	//glRotatef(i, 1, 1, 1);
	//Circle();
	//glPopMatrix();

	//glPushMatrix();
	//glColor3f(0, 1, 1);
	//glTranslatef(-2, -2, -12);
	//glRotatef(i, 1, 1, 1);
	//Cube();
	//glPopMatrix();

	//glPushMatrix();
	//glColor3f(1, 1, 0);
	//glTranslatef(2, -2, -12);
	//glRotatef(i, 1, 1, 1);
	//Cone();
	//glPopMatrix();
	glutSwapBuffers();//交互缓冲指针 双缓冲
}
void changeSize(int w, int h) {

	// 防止除数即高度为0
	// (你可以设置窗口宽度为0).
	if (h == 0)
		h = 1;
	float ratio = 1.0* w / h;

	// 单位化投影矩阵。
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	// 设置视口大小为整个窗口大小
	glViewport(0, 0, w, h);

	// 设置正确的投影矩阵
	gluPerspective(45, ratio, 1, 1000);
	//下面是设置模型视图矩阵
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, -1.0, 0.0f, 1.0f, 0.0f);//设置观测点
}
int main(int argc, char * argv[])
{
	glutInit(&argc, argv);//窗口初始化
	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);//指定其显示模式的类型。GLUT_DEPTH//深度缓存区
	glutInitWindowPosition(100, 100);//确定窗口位置
	glutInitWindowSize(320, 320);//大小
	glutCreateWindow("Hello OpenGL");//名字
	glutDisplayFunc(renderScene);//注册一个绘图函数
	glutIdleFunc(renderScene); //指定程序空闲时调用函数
	glutReshapeFunc(changeSize); //指定窗口形状变化时的回调函数
	glEnable(GL_DEPTH_TEST);//启用什么... 启用深度测试。根据坐标的远近自动隐藏被遮住的图形(材料)
	glutMainLoop();//glutMainLoop进入GLUT事件处理循环,让所有的与“事件”有关的函数调用无限循环。
	return 0;
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值