NeHe学习之基础篇一

本文介绍如何使用OpenGL绘制一个旋转的三菱锥和一个正方体。通过调用OpenGL函数如glTranslatef、glRotatef等实现图形的移动和旋转,并通过glBegin、glEnd设置绘制模式。此外,还展示了如何为不同面设置颜色。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

三菱锥,正方体的绘制,及着色,旋转:

没有单独列出三角形,正方形的代码

在原有的框架基础上,开始添加代码。代码里会使用到以下函数:

这些函数我只是给出了函数名及其作用,但是没有详细说明,因为这些函数网络上都可以搜到。

glTranslatef(x,y,z); //沿着x,y,z移动,其参数的值是相对值,就是说是参照上一个点的位置来移动的。
glBegin(GLenummode); //开始绘制
glEnd(); //结束绘制
glRotatef(Angle,Xvector,Yvector,Zvector); //绕着x,y,z所代表的矢量旋转Angle度
glColor3f(r,g,b); //设置颜色
glVertex3f(x,y,z); //设置点的位置

在代码开始处添加两个全局变量

GLfloat rtri;
GLfloat rquad;

在DrawGLScene()中添加以下代码:

/******************** triangles ***********************/
	glTranslatef(-1.5f,	0.0f,-6.0f);
	glRotatef(rtri,0.0f,1.0f,0.0f);
	glBegin(GL_TRIANGLES);
	/* fornt-side */
		glColor3f(1.0f,0.0f,0.0f);
		glVertex3f(0.0f,1.0f,0.0f); // triangles top point	

		glColor3f(0.0f,1.0f,0.0f);  // triangles left point
		glVertex3f(-1.0f,-1.0f,1.0f);

		glColor3f(0.0f,0.0f,1.0f);  // triangles right point
		glVertex3f(1.0f,-1.0f,1.0f);
	/* right-side */
		glColor3f(1.0f,0.0f,0.0f);
		glVertex3f(0.0f,1.0f,0.0f); // triangles top point	

		glColor3f(0.0f,0.0f,1.0f);  // triangles left point
		glVertex3f(1.0f,-1.0f,1.0f);

		glColor3f(0.0f,1.0f,0.0f);  // triangles right point
		glVertex3f(1.0f,-1.0f,-1.0f);
	/* back-side */
		glColor3f(1.0f,0.0f,0.0f);
		glVertex3f(0.0f,1.0f,0.0f); // triangles top point	

		glColor3f(0.0f,1.0f,0.0f);  // triangles left point
		glVertex3f(1.0f,-1.0f,-1.0f);

		glColor3f(0.0f,0.0f,1.0f);  // triangles right point
		glVertex3f(-1.0f,-1.0f,-1.0f);
	/* left-side */
		glColor3f(1.0f,0.0f,0.0f);
		glVertex3f(0.0f,1.0f,0.0f); // triangles top point	

		glColor3f(0.0f,0.0f,1.0f);  // triangles left point
		glVertex3f(-1.0f,-1.0f,-1.0f);

		glColor3f(0.0f,1.0f,0.0f);// triangles right point
		glVertex3f(-1.0f,-1.0f,1.0f);
	glEnd();

	/******************** quad ***********************/
	glLoadIdentity();
	glTranslatef(1.5f,0.0f,-7.0f);
	glRotatef(rquad,1.0f,1.0f,1.0f);
	glBegin(GL_QUADS);
		glColor3f(0.0f,1.0f,0.0f);	// quad top
		glVertex3f( 1.0f, 1.0f,-1.0f);
		glVertex3f(-1.0f, 1.0f,-1.0f);	
		glVertex3f(-1.0f, 1.0f, 1.0f);
		glVertex3f( 1.0f, 1.0f, 1.0f);

		glColor3f(1.0f,0.5f,0.0f);	// quad bottom
		glVertex3f(-1.0f, -1.0f,-1.0f);	
		glVertex3f( 1.0f, -1.0f,-1.0f);
		glVertex3f( 1.0f, -1.0f, 1.0f);
		glVertex3f(-1.0f, -1.0f, 1.0f);

		glColor3f(1.0f,.0f,0.0f);	// quad front
		glVertex3f( 1.0f, 1.0f, 1.0f);	
		glVertex3f(-1.0f, 1.0f, 1.0f);
		glVertex3f(-1.0f,-1.0f, 1.0f);
		glVertex3f( 1.0f,-1.0f, 1.0f);

		glColor3f(1.0f,1.0f,0.0f);	// quad back
		glVertex3f(-1.0f, 1.0f,-1.0f);	
		glVertex3f( 1.0f, 1.0f,-1.0f);
		glVertex3f( 1.0f,-1.0f,-1.0f);
		glVertex3f(-1.0f,-1.0f,-1.0f);

		glColor3f(0.0f,0.0f,1.0f);	// quad left
		glVertex3f( 1.0f, 1.0f, 1.0f);	
		glVertex3f( 1.0f, 1.0f,-1.0f);
		glVertex3f( 1.0f,-1.0f,-1.0f);
		glVertex3f( 1.0f,-1.0f, 1.0f);

		glColor3f(1.0f,0.0f,1.0f);	// quad right
		glVertex3f( 1.0f, 1.0f,-1.0f);	
		glVertex3f( 1.0f, 1.0f, 1.0f);
		glVertex3f( 1.0f,-1.0f, 1.0f);
		glVertex3f( 1.0f,-1.0f,-1.0f);
	glEnd();

        rtri += 0.2f;
	rquad -= 0.15f;
你将会看到一个三菱锥和一个正方体在旋转。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值