#include<iostream.h>
#include<stdlib.h>
#include<math.h>
#include<GL/glut.h>
int winWidth=800,winHeight=650,key;
float angle=0.0,c_Angle,p1[3],p2[3],vx,vy,vz;
void init()
{
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glColorMaterial(GL_FRONT_AND_BACK,GL_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
glutSetCursor(GLUT_CURSOR_UP_DOWN); //设置鼠标
}
void DrawLine() //画旋转轴
{
vx=p2[0]-p1[0];
vy=p2[1]-p1[1];
vz=p2[2]-p1[2];
glColor3f(0.0f,1.0f,1.0f);
glTranslatef(-p1[0],-p1[1],-p1[2]);
glRotatef(-angle,vx,vy,vz); //反向旋转
glBegin(GL_LINES);
glVertex3i(p1[0],p1[1],p1[2]);
glVertex3i(p2[0],p2[1],p2[2]);
glEnd();
glTranslatef(p1[0],p1[1],p1[2]);
}
void DrawTetrahedron() //画四面体
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLES);
//侧面A
glColor3f(1.0f,0.0f,0.0f);
glVertex3i(1,2,1);
glColor3f(0.0f,1.0f,0.0f);
glVertex3i(1,1,2);
glColor3f(0.0f,0.0f,1.0f);
glVertex3i(2,1,1);
//侧面B
glColor3f(1.0f,0.0f,0.0f);
glVertex3i(1,2,1);
glColor3f(1.0f,1.0f,0.0f);
glVertex3i(1,1,1);
glColor3f(0.0f,1.0f,0.0f);
glVertex3i(1,1,2);
//侧面C
glColor3f(0.0f,0.0f,1.0f);
glVertex3i(2,1,1);
glColor3f(1.0f,1.0f,0.0f);
glVertex3i(1,1,1);
glColor3f(0.0f,1.0f,0.0f);
glVertex3i(1,1,2);
//侧面D
glColor3f(1.0f,0.0f,0.0f);
glVertex3i(1,2,1);
glColor3f(1.0f,1.0f,0.0f);
glVertex3i(1,1,1);
glColor3f(0.0f,0.0f,1.0f);
glVertex3i(2,1,1);
glEnd();
}
void Draw()
{
DrawTetrahedron();
DrawLine();
}
void Rotate3D(float theta,float*point1,float*point2)
{
vx=point2[0]-point1[0];
vy=point2[1]-point1[1];
vz=point2[2]-point1[2];
glTranslatef(-point1[0],-point1[1],-point1[2]); //平移到原点
glRotatef(theta,vx,vy,vz); //旋转
glTranslatef(point1[0],point1[1],point1[2]); //回到原来位置
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //清除颜色和深度缓冲
glPushMatrix(); //变换矩阵入棧
glScalef(0.3,0.3,0.3); //缩放
glTranslatef(-0.5,-1.5,0.0); //平移
Rotate3D(angle,p1,p2); //旋转
Draw(); //画图
glPopMatrix(); //变换矩阵出棧
glFlush();
glutSwapBuffers(); //交换缓存
}
void winReshapeFcn(int newWidth,int newHeight)
{
glViewport(0, 0, newWidth, newHeight); //指定视口参数
winWidth=newWidth;
winHeight=newHeight;
glMatrixMode(GL_PROJECTION);
if(newWidth <=newHeight) //选择裁剪窗口坐标和观察系原点到近和远裁剪的距离
glOrtho (-1.5, 1.5, -1.5*(GLfloat)newHeight/(GLfloat)newWidth, 1.50*(GLfloat)newHeight/(GLfloat)newWidth, -10.0, 10.0);
else
glOrtho (-1.5*(GLfloat)newWidth/(GLfloat)newHeight, 1.5*(GLfloat)newWidth/(GLfloat)newHeight, -1.5, 1.5, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); // 重置模型观察矩阵为单元矩阵
}
void myIdle(void)
{
switch(key){
case 1:{
angle++;
if( angle >= 360.0f )
angle = 0.0f;
display();
};break;
case 2:{
if(angle<=c_Angle){
angle++;
display();
}
};break;
}
}
void main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowPosition(100,40);
glutInitWindowSize(winWidth,winHeight);
glutCreateWindow("An Example");
init();
cout<<"请选择旋转的模式:\n1.自动旋转;\n2.输入某个角度旋转."<<endl;
cin>>key;
if(key==2){
cout<<"输入要旋转的角度(单位为:度):"<<endl;
cin>>c_Angle;
}
cout<<"输入旋转轴的方向向量点P1:"<<endl;
cin>>p1[0]>>p1[1]>>p1[2];
cout<<"输入旋转轴的方向向量点P2:"<<endl;
cin>>p2[0]>>p2[1]>>p2[2];
glutDisplayFunc(display);
glutReshapeFunc(winReshapeFcn); //调整显示窗口的变化
glutIdleFunc(myIdle); //空闲回调函数
glutMainLoop();
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
03_481228_04.rar (2个子文件)
03_481228_04
3D.cpp 4KB
3D.exe 208KB
共 2 条
- 1
laijoe
- 粉丝: 1
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页