opengl绘制立方体

有键盘控制函数和颜色设置

#include<GL/glut.h>
#include<math.h>
int faces[6][4]={{0,3,2,1},{3,7,6,2},{7,4,5,6},{4,0,1,5},{1,2,6,5},{4,7,3,0}};//六个面,每个面有四个顶点
 GLfloat points[8][3]={{-0.5,-0.5,0.5},{-0.5,0.5,0.5},{0.5,0.5,0.5},{0.5,-0.5,0.5},{-0.5,-0.5,-0.5},{-0.5,0.5,-0.5},{0.5,0.5,-0.5},{0.5,-0.5,-0.5}};//每个顶点的位置
GLfloat faceColor[6][3]={{1.0,0.0,0.0},{0.0,1.0,0.0},{0.0,0.0,1.0},{1.0,1.0,0.0},{0.0,1.0,1.0},{1.0,0.0,1.0}};
GLfloat pointColor[8][3]={{1.0,0.0,0.0},{0.0,1.0,0.0},{0.0,0.0,1.0},{1.0,1.0,0.0},{0.0,1.0,1.0},{1.0,0.0,1.0},{0.5,0.5,1.0},{1.0,0.3,0.5}};
GLfloat r=1,theta=30,phi=30;
void drawFace(int*face){
	glBegin(GL_POLYGON);
		glColor3fv(pointColor[*face]);
	glVertex3fv(points[*face]);
		glColor3fv(pointColor[*(face+1)]);
	glVertex3fv(points[*(face+1)]);
		glColor3fv(pointColor[*(face+2)]);
	glVertex3fv(points[*(face+2)]);
		glColor3fv(pointColor[*(face+3)]);
	glVertex3fv(points[*(face+3)]);
	glEnd();	
	
}

void Cube(){

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1,1,-1,1,-1,5);
gluLookAt(r*sin(theta*3.14159/180)*sin(phi*3.14159/180),r*cos(theta*3.14159/180),r*sin(theta*3.14159/180)*cos(phi*3.14159/180),0,0,0,0,1,0);
int k;
for(k=0;k<6;k++){
glColor3fv( faceColor[k]);
drawFace(faces[k]);
}
glFlush();
}
void myKeyboard(unsigned char key,int x,int y){//键盘控制函数,控制视角变化
	
switch(key)
{
case'A':
case'a':
	if(phi-1>-180)
		phi=phi-1;
	break;
case'D':
case'd':
	if(phi+1<180)
		phi=phi+1;
	break;
case'S':
case's':
	if(theta+1<180)
		theta=theta+1;
	break;
case'W':
case'w':
	if(theta-1>0)
		theta=theta-1;
	break;
case'G':
case'g':
	for(int i=0;i<8;i++){
	points[i][0]=points[i][0]+0.1;
	}
	break;
case'L':
case'l':
	for(int i=0;i<8;i++){
	points[i][0]=points[i][0]-0.1;
	}
	break;
}
	glutPostRedisplay();
}
void main(int argc, char** argv){
    glutInit(&argc, argv); //初始化GLUT库;
	glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
	glutCreateWindow("hello"); // 创建窗口,标题为“hello" ;
	glEnable(GL_DEPTH_TEST);
	glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-1,1,-1,1,-1,5);
	
    gluLookAt(r*sin(theta*3.14159/180)*sin(phi*3.14159/180),r*cos(theta*3.14159/180),r*sin(theta*3.14159/180)*cos(phi*3.14159/180),0,0,0,0,1,0);
	glutDisplayFunc(Cube); //用于绘制当前窗口;
	glutKeyboardFunc(myKeyboard); //指定键盘回调函数
	glutMainLoop();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这辈子秃头是不可能的

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值