计算机图形学OpenGL中绘制太阳_地球_月亮的运动模型源代码

合集下载

基于opengl的3D天体运动模型设计方案

基于opengl的3D天体运动模型设计方案

基于opengl的3D天体运动模型设计方案学生: ____???___学号: ************指导老师: ___???___一、背景简介1.1设计概述本3D建模设计运用Win32程序设计的基本原理,基于OpenGL接口函数,以Microsoft Visual Studio 2008为开发工具,以C++语言为开发语言,设计了一个星空背景下的太阳—地球—月球公转自转运动模型,模拟了太阳的光照效果,并实现了可以加速和减速地球和月球的自转、公转速度,而且还可以调整视图的远近和方位,方便各方面的观察。

1.2 OpenGL的基本特点OpenGL即开放图形库(Open Graphics Library),是一个三维的计算机图形和模型库。

OpenGL作为一个性能优越的图形应用程序设计接口,适用于广泛的计算机环境。

从个人计算机到工作站和超级计算机,OpenGL都能实现高性能的三维图形功能。

OpenGL是一个与硬件图形发生器的软件接口,它包括了100多个图形操作函数,开发者可以利用这些函数来构造景物模型、进行三维图形交互软件的开发。

正如上一章所述,OpenGL是一个高性能的图形开发软件包。

OpenGL支持网络,在网络系统中用户可以在不同的图形终端上运行程序显示图形。

OpenGL作为一个与硬件独立的图形接口,它不提供与硬件密切相关的设备操作函数,同时,它也不提供描述类似于飞机、汽车、分子形状等复杂形体的图形操作函数。

用户必须从点、线、面等最基本的图形单元开始构造自己的三维模型。

当然,像OpenInventor那样更高一级的基于OpenGL的三维图形建模开发软件包将提供方便的工具。

因此OpenGL的图形操作函数十分基本、灵活。

它具有如下特点。

(1)图形质量好、性能高。

无论是三维动画、CAD,还是视觉模拟、可视化计算等,都利用了OpenGL高图形质量、高性能的特点。

这个特点使得程序开发者在广播、CAD/CAM/CAE、娱乐、医学图像和虚拟现实等领域中创造和显示出难以想象的2D和3D图形。

OpenGL第七章源代码

OpenGL第七章源代码

// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Psuedo window coordinates
gluOrtho2D(0.0, (GLfloat) w, 0.0f, (GLfloat) h);
0x03, 0xf0, 0xff, 0x80,
0x03, 0xf5, 0xff, 0xe0,
0x07, 0xfd, 0xff, 0xf8,
0x1f, 0xfc, 0xff, 0xe8,
0xff, 0xe3, 0xbf, 0x70,
for(x = 0; x < 16; x++)
// Draw the "fire" bitmap, advance raster position
glBitmap(32, 32, 0.0, 0.0, 32.0, 0.0, fire);
}
// Do the buffer Swap
}
-----------------------------------------------------------
bitmap2.c 绘制模式
#include <gl/glut.h>
GLfloat hh;
GLubyte wb[2]={0x00,0xff};
GLint i,j;
// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).

计算机图形学模拟太阳系源代码

计算机图形学模拟太阳系源代码

#include <windows.h>#include <stdio.h>#include <stdlib.h>#include <glut.h>//行星GLfloat rot0 = 30.0;GLfloat rot1 = 0.0;GLfloat rot2 = 0.0;GLfloat rot3 = 0.0;GLfloat rot4 = 0.0;GLfloat rot5 = 0.0;GLfloat rot6 = 0.0;GLfloat rot7 = 0.0;GLfloat rot8 = 0.0;//卫星GLfloat rot9 = 0.0;GLfloat rot10 = 0.0;GLfloat rot11 = 0.0;void init(){glClearColor(0.0,0.0,0.0,0.0);glClearDepth(1.0);glShadeModel(GL_FLAT);}void display(){glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glColor3f(1.0,1.0,1.0);glLoadIdentity();//gluLookAt(0, 10, 10, 0,0,0,0, 1,0);//glRotatef(45.0,0.0,0.0,1.0);glTranslatef(0.0,0.0,-20.0);glRotatef(90.0,1.0,0.0,0);glPushMatrix();//绘制太阳glColor3f(1.0,0.0,0.0);glutSolidSphere(2.0,32,32);//绘制地球glPushMatrix();glColor3f(0.0,0.0,1.0);// 绘制辅助轨道glRotatef(90,1.0,0,0.0);glutSolidTorus(0.02, 5.0, 10, 64); glRotatef(-90,1.0,0,0.0);// 设置地球公转速度glRotatef(rot0,0.0,1.0,0.0);// 设置地球半径glTranslatef(5.0,0.0,0.0);// 设置地球自转速度//glRotatef(rot1,0.0,1.0,0.0);// 绘制地球glutSolidSphere(0.4,32,32);// 绘制地球的卫星-月亮glColor3f(0.5,0.6,0.5);// 抵消地球自转影响//glRotatef(-rot1,0.0,1.0,0.0);// 绘制辅助轨道glRotatef(90,1.0,0,0.0);glRotatef(-90,1.0,0,0.0);// 设置月亮公转速度glRotatef(rot9,0.0,1.0,0.0);// 设置月亮公转半径glTranslatef(0.6,0.0,0.0);// 绘制月亮glutSolidSphere(0.1,10,8); glPopMatrix();// 绘制水星glPushMatrix();glColor3f(0.0,1.0,1.0);// 绘制辅助轨道glRotatef(90,1.0,0,0.0); glutSolidTorus(0.02, 2.5, 10, 64); glRotatef(-90,1.0,0,0.0);// 设置水星公转速度glRotatef(rot1,0.0,1.0,0.0);// 设置水星公转半径glTranslatef(2.5,0.0,0.0);// 设置水星自传glRotatef(rot3,0.0,1.0,0.0);// 绘制水星glutSolidSphere(0.2,32,32); glPopMatrix();//绘制金星glPushMatrix();glColor3f(0.0,1.0,0.0);//绘制辅助轨道glRotatef(90,1.0,0,0.0); glutSolidTorus(0.02, 3.4, 10, 64); glRotatef(-90,1.0,0,0.0);// 设置金星公转速度glRotatef(rot2,0.0,3.0,0.0);// 设置金星公转半径glTranslatef(3.4,0.0,0.0);// 设置金星自传glRotatef(rot0,0.0,1.0,0.0);// 绘制金星glutSolidSphere(0.3,32,32); glPopMatrix();//绘制火星glPushMatrix();glColor3f(1.0,0.0,0.0);//绘制辅助轨道glRotatef(90,1.0,0,0.0); glutSolidTorus(0.02, 6.6, 10, 64); glRotatef(-90,1.0,0,0.0);// 设置火星公转速度glRotatef(rot3,0.0,4.0,0.0);// 设置火星公转半径glTranslatef(6.6,0.0,0.0);// 设置火星自传glRotatef(rot7,0.0,2.0,0.0);// 绘制火星glutSolidSphere(0.5,32,32); glPopMatrix();//绘制木星glPushMatrix();glColor3f(2.0,0.1,1.0);//绘制辅助轨道glRotatef(90,1.0,0,0.0); glutSolidTorus(0.02, 8.5, 10, 64); glRotatef(-90,1.0,0,0.0);// 设置木星公转速度glRotatef(rot4,0.0,0.4,0.0);// 设置木星公转半径glTranslatef(8.5,0.0,0.0);// 设置木星自传glRotatef(rot3,0.0,0.3,0.0);// 绘制木星glutSolidSphere(1.0,32,32);// 绘制木星卫星-木卫1glColor3f(0.4,0.3,0.5);// 抵消地球自转影响//glRotatef(-rot1,0.0,1.0,0.0);// 绘制辅助轨道glRotatef(90,1.0,0,0.0);glRotatef(-90,1.0,0,0.0);// 设置木卫1公转速度glRotatef(rot10,0.0,1.0,0.0);// 设置木卫1公转半径glTranslatef(1.3,0.0,0.0);// 绘制木卫1glutSolidSphere(0.1,10,8);// 绘制木星卫星-木卫2glColor3f(0.5f,0.5f,0.5f);// 抵消地球自转影响//glRotatef(-rot1,0.0,1.0,0.0);// 绘制辅助轨道glRotatef(90,1.0,0,0.0);glRotatef(-90,1.0,0,0.0);// 设置木卫2公转速度glRotatef(rot11,0.0,1.0,0.0);// 设置木卫2公转半径glTranslatef(1.2,0.0,0.0);// 绘制木卫2glutSolidSphere(0.08,10,8);glPopMatrix();//绘制土星glColor3f(1.0f, 1.0f, 0.0f);//绘制辅助轨道glRotatef(90,1.0,0,0.0); glutSolidTorus(0.02, 12.5, 10, 64); glRotatef(-90,1.0,0,0.0);// 设置土星公转速度glRotatef(rot5,0.0,0.4,0.0);// 设置土星公转半径glTranslatef(12.5,0.0,0.0);// 设置土星自传glRotatef(rot3,0.0,0.3,0.0);// 绘制土星glutSolidSphere(0.85,32,32);//绘制土星光环glRotatef(90,1.0,0,0.0); glutSolidTorus(0.1, 1.25, 10, 64); glRotatef(-90,1.0,0,0.0);glRotatef(90,1.0,0,0.0);glutSolidTorus(0.07, 1.65, 10, 64); glRotatef(-90,1.0,0,0.0); glPopMatrix();glPopMatrix();//绘制天王星glPushMatrix();glColor3f(0.0f, 1.0f, 1.0f);//绘制辅助轨道glRotatef(90,1.0,0,0.0); glutSolidTorus(0.02, 15.5, 10, 64); glRotatef(-90,1.0,0,0.0);// 设置天王星公转速度glRotatef(rot6,0.0,0.4,0.0);// 设置天王星公转半径glTranslatef(15.5,0.0,0.0);// 设置天王星自传glRotatef(rot1,0.0,0.3,0.0);// 绘制天王星glutSolidSphere(0.15,32,32); glPopMatrix();//绘制海王星glColor3f(0.0f, 0.0, 8.0f);//绘制辅助轨道glRotatef(90,1.0,0,0.0);glutSolidTorus(0.02, 17.5, 10, 64);glRotatef(-90,1.0,0,0.0);// 设置海王星公转速度glRotatef(rot7,0.0,0.4,0.0);// 设置海王星公转半径glTranslatef(17.5,0.0,0.0);// 设置海王星自传glRotatef(rot3,0.0,0.3,0.0);// 绘制海王星glutSolidSphere(0.145,32,32);glPopMatrix();//绘制冥王星glPushMatrix();glColor3f(0.5f, 0.5f, 0.5f);//绘制辅助轨道glRotatef(90,1.0,0,0.0);glutSolidTorus(0.02, 19.5, 10, 64);glRotatef(-90,1.0,0,0.0);// 设置冥王星公转速度glRotatef(rot8,0.0,0.4,0.0);// 设置冥王星公转半径glTranslatef(19.5,0.0,0.0);// 设置冥王星自传glRotatef(rot2,0.0,0.3,0.0);// 绘制冥王星glutSolidSphere(0.145,32,32);glPopMatrix();glutSwapBuffers();glFlush();}void idle(){if(rot0>=360.0)rot0-=360.0;rot1+=0.416;if(rot1>=360.0)rot1-=360.0;rot2+=0.1631;if(rot2>=360.0)rot2-=360.0;rot3+=0.053;if(rot3>=360.0)rot3-=360.0;rot4+=0.0083;if(rot4>=360.0)rot4-=360.0;rot5+=0.0034;if(rot5>=360.0)rot5-=360.0;rot6+=0.00119;if(rot6>=360.0)rot6-=360.0;rot7+=0.00069;if(rot7>=360.0)rot7-=360.0;rot8+=0.0008;if(rot8>=360.0)rot8-=360.0;rot9+=1.0;if(rot9>=360.0)rot9-=360.0;rot10+=0.005;if(rot10>=360.0) rot10-=360.0;if(rot11>=360.0)rot11-=360.0;glutPostRedisplay();}void reshape(int w,int h){glViewport(0,0,(GLsizei)w,(GLsizei)h);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(60.0,(GLdouble)w/(GLdouble)h,1.0,100.0);glMatrixMode(GL_MODELVIEW);}void main(int argc,char **argv){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);glutInitWindowSize(600,400);glutInitWindowPosition(500,500);glutCreateWindow("planet sim");init();glutDisplayFunc(display);glutReshapeFunc(reshape);glutIdleFunc(idle);glutMainLoop();return;}。

计算机图形学复习题目

计算机图形学复习题目

第一章1.1 名词解释:图形、图像、点阵法、参数法。

1.2 图形包括哪两方面的要素?在计算机中如何表示它们?1.3 什么叫计算机图形学?分析计算机图形学、数字图像处理和计算机视觉学科间的关系。

1.4 有关计算机图形学的软件标准有哪些?1.5 试从科学发展历史的角度分析计算机图形学以及硬设备的发展过程。

1.6 试发挥你的想象力,举例说明计算机图形学有哪些应用范围,解决的问题是什么?1.7 一个交互性计算机图形系统必须具有哪几种功能?第二章2.1 名词解释:随机扫描、光栅扫描、图形显示子系统、像素点、光点、屏幕分辨率、显示分辨率、存储分辨率、组合像素法、颜色位面法、位平面、颜色查找表。

2.2 试列举出你所知道的图形输入与输出设备。

2.3 阴极射线管由哪几部分组成?它们的功能分别是什么?2.4 简述什么叫桶形失真?如何校正?2.5 简述荫罩式彩色阴极射线管的结构和工作原理。

2.6 比较荫罩式彩色阴极射线管和穿透式彩色阴极射线管的异同。

2.7 简述黑底荫罩式彩色阴极射线管的结构和特点。

2.8 简述光栅扫描图形显示器的工作逻辑。

2.9 基于光栅扫描的图形显示子系统由哪几个逻辑部件组成?它们的功能分别是什么?2.10 什么是像素点?什么是显示器的分辨率?2.11 某些显示卡为什么要采用颜色查找表?采用颜色查找表的系统的工作原理是什么?2.12 确定用你的系统中的视频显示器x和y方向的分辨率,确定其纵横比,并说明你的系统怎样保持图形对象的相对比例。

2.13 如何根据显示器的指标计算显示存储器的容量。

2.14 图形的硬拷贝设备有哪些,简述其各自的特点。

第三章3.1 名词解释(可用图示):回显、约束、网格、引力域、橡皮筋技术、草拟技术、拖动、旋转、形变。

3.2 什么是用户模型,设计一个好的用户接口要涉及到哪些因素?3.3 gks的有哪六种逻辑输入设备,试评价这六种逻辑分类方法。

3.4 举例说明什么是请求方式、取样方式、事件方式及其组合形式。

opengl鼠标绘制直线及圆图元算法源代码

opengl鼠标绘制直线及圆图元算法源代码

#include <assert.h>#include <cmath>#include <iostream>#include <vector>#include <glut.h>using namespace std;typedef vector<int> list;vector<list> ls, cs;int old_x, old_y, new_x, new_y, cx, cy, cr; int mouse_type;/* breseman画线算法*/void line(int x1, int y1, int x2, int y2) { glBegin(GL_POINTS);if (x1==x2 && y1==y2) {glVertex2i(x1, y1);return;}if (x1>x2) {swap(x1, x2);swap(y1, y2);}glVertex2d(x1, y1);int dy=abs(y2-y1);int dx=abs(x2-x1);if (dx>=dy) {int incy=(dy!=0?dy/(y2-y1):0);int p=2*dy-dx;int dy2=2*dy;int dd2=2*(dy-dx);int x, y=y1;for (x=x1+1; x<=x2; ++x) {if (p<0) {glVertex2i(x, y);p+=dy2;}else {y+=incy;glVertex2i(x, y);p+=dd2;}}}else {if (y1>y2) {swap(x1, x2);swap(y1, y2);}int incx=(dx!=0?dx/(x2-x1):0);int p=2*dx-dy;int dx2=2*dx;int dd2=2*(dx-dy);int x=x1, y;for (y=y1+1; y<=y2; ++y) {if (p<0) {glVertex2i(x, y);p+=dx2;}else {x+=incx;glVertex2i(x, y);p+=dd2;}}}glEnd();}/* 中点圆算法*/void circle(int cx, int cy, int r) { int x=0, y=r;float p=1.25-r;while (x<=y) {glBegin(GL_POINTS);glVertex2i(cx+x, cy+y);glVertex2i(cx-x, cy+y);glVertex2i(cx-x, cy-y);glVertex2i(cx+x, cy-y);glVertex2i(cx+y, cy+x);glVertex2i(cx-y, cy+x);glVertex2i(cx-y, cy-x);glVertex2i(cx+y, cy-x);glEnd();if (p<0) {++x;p+=2*x+1;}else {p+=2*x-2*y+5;++x;--y;}}}void draw(){int i;for (i=0; i<ls.size(); ++i)line(ls[i][0], ls[i][1], ls[i][2], ls[i][3]);for (i=0; i<cs.size(); ++i)circle(cs[i][0], cs[i][1], cs[i][2]);if(mouse_type == 1)line(old_x, old_y, new_x, new_y);if(mouse_type == 3)circle(cx, cy, cr); //圆上的点和半径}void display() {glClear(GL_COLOR_BUFFER_BIT);glColor3i(0, 0, 0);draw();glutSwapBuffers();}void keyboard( unsigned char key, int x, int y) {switch (key) {case 27:exit (0);break;case 'a':case 'A':break;default:if (key<='z' && key>='a' || key<='Z' && key>='A' || key<='9' && key>='0'){ cout<<char(key);}else{cout<<"\n字符码为"<<static_cast<int>(key);display();}}}void mouseclick( int button, int state, int x, int y ){cout<<"("<<x<<","<<y<<")";switch ( button ) {case GLUT_LEFT_BUTTON:if ( state == GLUT_DOWN ) {cout<<"左键被按下";old_x = x;old_y = 600-y;mouse_type = 1;}else if ( state == GLUT_UP ) {cout<<"左键被松开";list l;l.push_back(old_x);l.push_back(old_y);l.push_back(x);l.push_back(600-y);ls.push_back(l);mouse_type = 0;glutPostRedisplay();for(int i=0; i<ls.size(); ++i)cout<<endl<<"line_"<<i<<": "<<ls[i][0]<<","<<ls[i][1]<<" "<<ls[i][2]<<","<<ls[i][3];}break;case GLUT_MIDDLE_BUTTON:break;case GLUT_RIGHT_BUTTON:if ( state == GLUT_DOWN ) {cout<<"右键被按下";cx = x;cy = 600-y;mouse_type = 3;}else if ( state == GLUT_UP ) {cout<<"右键被松开";list l;l.push_back(cx);l.push_back(cy);cr=int(sqrt(double(x-cx)*(x-cx)+(600-y-cy)*(600-y-cy))+0.5);l.push_back(cr);cs.push_back(l);mouse_type = 2;glutPostRedisplay();for(int i=0; i<cs.size(); ++i)cout<<endl<<"circle_"<<i<<": "<<cs[i][0]<<","<<cs[i][1]<<" "<<cs[i][2];}break;}}void screenmotion(int x, int y){if(mouse_type == 1){new_x = x;new_y = 600-y;cout<<endl<<"new-line:"<<old_x<<","<<old_y<<" "<<new_x<<","<<new_y<<endl; }else if(mouse_type == 3){cr=int(sqrt(double(x-cx)*(x-cx)+(600-y-cy)*(600-y-cy))+0.5);cout<<endl<<"new-circle: "<<cx<<","<<cy<<" "<<cr<<endl;}glutPostRedisplay();}void init() {glClearColor(0.2, 0.5, 0.8, 0.0); //背景颜色glMatrixMode(GL_PROJECTION);gluOrtho2D(-1.0, 600, 0.0, 601);}int main(int argc, char** argv) {glutInit(&argc, argv);glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);glutInitWindowPosition(100, 50); //图框在屏幕的位置glutInitWindowSize(600, 600); //边框大小glutCreateWindow("图元算法"); //图框边显示的字init();glutDisplayFunc(display);glutMotionFunc(screenmotion);glutKeyboardFunc( keyboard );glutMouseFunc( mouseclick );glutMainLoop();return 0;}。

计算机图形学opengl动画的实现源代码

计算机图形学opengl动画的实现源代码

实验目的:
在VC6.0的opengl的环境下,实现固定物体的连续变化,从而使简单动画得以实现。

实验思想:
由于是简单图形的变化过程,所以问题关键在于:
1、物体的位置变化如何实现,而位置变化在opengl中体现在坐标的变化中,因而设置坐标的每一步物体的位置变化情况是该问题的关键,此关键代码如下:
wAngleX += 1.0f;
wAngleY += 10.0f;
wAngleZ += 5.0f;
2、再有就是如何使得连续的变化得以实现,这就实现了手动变化相自动变化的转变,为了实现这一问题,我们想到设置一个变化时间,使得物体在每个时间段变换一次位置,当这个时间适当小时,就实现了物体的连续变化;这就需要引进一个计时器,通过设置间隔时间实现物体的连续变化,此关键代码如下:
SetTimer(1, 150, NULL);
实验步骤:
1、初始化OpenGL。

2、设置菜单与关联函数初始化,设置视口与窗口匹配,重新设置坐标系统。

3、建立正交变换下的剪切体(即变换物体)。

4、编写变换幅度函数,设置没不变换情况。

5、编写连续变换计时器,使得在一定间隔下连续变换。

实验关键代码:
实验结果:。

计算机图形学 课程设计

计算机图形学 课程设计

2、gluSphere(quadric, 0.45f, 40, 40)函数是在原点绘制图形,经过 glTranslatef* 和 glRotatef*平移和旋转可以随意改变球体位置:
glRotatef(-90.0f, 1.0f, 0.0f, 0.0f); // 将坐标系绕 X 轴旋转-90 度 glTranslatef(1.7f, 0.0f, 0.0f); // 将坐标系右移 1.7f
8、建立一个地球公转轨道,调用函数 glTranslatef*把球体平移一定距离,使其在自
4
己的轨道上运行。同时为了使月球跟随地球,glRotatef(fEarthOrbit , 0.0f, 1.0f, 0.0f); 将坐标系绕 Y 轴旋转地球公转的角度,控制月亮跟随地球,用函数 glTranslatef*平移使月 球轨道在地球周围而不与地球重叠。
5
参数代表要操作的目标,GL_PROJECTION 是对投影矩阵操作,GL_MODELVIEW 是对模型视景矩 阵操作,GL_TEXTURE 是对纹理矩阵进行随后的操作。 13、glutIdleFunc(void (*func)(void))在 glutIdleFunc(void (*func)(void))回调函数 中指定一个函数,如果不存在其他尚未完成的事件(例如,当事件循环处于空闲的时候), 就执行这个函数。这个回调函数接受一个函数指针作为它的唯一参数。如果向它传递 NULL(0),就相当于禁用这个函数。glutMainLoop 进入 GLUT 事件处理循环。在一个 GLUT 程 序中,这个例程被调用一次 。一旦被调用,这个程序将永远不会返回 无限循环。它将调用 必要的任何已注册的回调。程序最后调用这个函数,让程序循环执行下去,使运动继续下去。
9、OpenGl 把三维坐标中的球体绘制到而为坐标屏幕,绘制的顺序是按代码的顺序来进 行的。因此后绘制的物体会遮住先绘制的物体,既使后绘制的物体在先绘制的物体后面也是 一样,使用深度测试可以解决这个问题。使用方法是:以 GL_DEPTH_TEST 为参数调用 glEnable 函数,启动深度测试。在必要时(通常是每次绘制画面开始时)清空深度缓冲,即: glClear(GL_DEPTH_BUFFER_BIT) , 其 中 glClear(GL_COLOR_BUFFER_BIT) 与 glClear(GL_DEPTH_BUFFER_BIT) 可 以 合 并 写 为 glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT),且后者运行速度比前者快。

opengl实现__太阳、地球和月亮的运动模型及小球的自由落体运动

opengl实现__太阳、地球和月亮的运动模型及小球的自由落体运动

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void initDisplay(void)
{ printf(" initDisplay()");
down=1;
//向下运动
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
int count=1;
double timeSpan=0;
//下降到底所需时间
double movey=0.0;
double duration=0.0;
//持续时间
double length=0.0;
专业资料学习参考
WORD 整理版
clock_t start,end; void init(void) { printf(" init");
专业资料学习参考
glutTimerFunc(50,timer,0); glutPostRedisplay(); }
WORD 整理版
int main(int argc,char **argv) { glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); glutCreateWindow("earth,moon,sun"); glutInitWindowSize(400,400); glutDisplayFunc(display); glutTimerFunc(50,timer,0); glutMainLoop(); return 0; }
z); 缩放矩阵函数 void glScale{df}(TYPE x,TYPE y,TYPE z); 如不需要效果积累可调用重置矩阵函数 void glLoadIdentity(void); 该函数将单位矩阵 置为当前变换矩阵。

地球动效代码

地球动效代码

地球动效代码地球动效是一种通过计算机程序实现的模拟地球自转和公转运动的效果。

通过编写相应的代码,可以让地球呈现出逼真的运动轨迹和变化。

在编写地球动效代码时,我们需要使用一些数学知识和计算机图形学的基础知识。

首先,我们需要确定地球的中心点和半径,以及地球自转和公转的速度。

然后,我们可以使用三维坐标系来表示地球的位置和运动轨迹。

在代码中,我们可以使用一些常见的编程语言,如JavaScript、Python或C++来实现地球动效。

下面是一个使用JavaScript语言实现地球动效的示例代码:```javascript// 设置地球的中心点和半径var earthCenter = { x: 0, y: 0, z: 0 };var earthRadius = 100;// 设置地球的自转和公转速度var earthRotationSpeed = 0.01;var earthRevolutionSpeed = 0.005;// 创建一个函数来更新地球的位置function updateEarthPosition() {// 计算地球的自转角度var earthRotationAngle = earthRotationSpeed * Date.now(); // 计算地球的公转角度var earthRevolutionAngle = earthRevolutionSpeed * Date.now();// 计算地球在三维坐标系中的位置var earthX = earthCenter.x + earthRadius * Math.sin(earthRevolutionAngle);var earthY = earthCenter.y + earthRadius * Math.cos(earthRevolutionAngle);var earthZ = earthCenter.z;// 将地球的位置应用到对应的HTML元素上var earthElement = document.getElementById('earth');earthElement.style.transform = 'translate3d(' + earthX + 'px, ' + earthY + 'px, ' + earthZ + 'px) rotateY(' + earthRotationAngle + 'rad)';}// 创建一个动画循环来更新地球的位置function animate() {requestAnimationFrame(animate);updateEarthPosition();}// 启动动画循环animate();```在这个示例代码中,我们首先设置了地球的中心点和半径,以及自转和公转的速度。

计算机图形学实验代码及截图

计算机图形学实验代码及截图

目录实验一:OpenGL基础知识 0实验二 OpenGL中的建模与变换 (1)实验三 OpenGL中的光照 (4)实验四 OpenGL中的拾取 (7)实验五 OpenGL中的纹理映射 (10)实验一:OpenGL基础知识一、实验目的1、建立Windows系统下的OpenGL实验框架。

2、学习理解OpenGL工作流程。

二、实验环境⒈硬件:每个学生需配备计算机一台。

⒉软件:Visual C++;三、实验内容1、建立非控制台的Windows程序框架。

2、建立OpenGL框架。

3、建立OpenGL框架的类文件。

4、完善Windows框架。

5、理解程序间的相互关系。

四、实验要求1、学习教材第2章的内容。

2、理解教材中相关实例的代码,按要求添加到适当的地方,调试并通过运行。

3、运行结果应该与教材中的相关实例相符。

4、编译第2章的框架代码,修改背景色、窗口标题。

五、程序设计提示(略)六、报告要求1.认真书写实验报告,字迹清晰,格式规范。

报告中应写清姓名、学号、实验日期、实验题目、实验目的、实验原理。

2.报告中应书写主要源程序,且源程序中要有注释。

3.报告中应包含运行结果及结果分析。

如调试通过并得到预期的效果,请注明‘通过’并粘贴运行效果图;如未调试通过或结果不正确,试分析原因。

4.报告最后包含实验总结和体会。

实验二 OpenGL中的建模与变换一、实验目的1.学习配置OpenGL环境。

2.掌握在OpenGL中指定几何模型的方法。

3. 掌握OpenGL中的透视投影和正投影的设置方法,学会诊断投影问题导致的显示异常。

二、实验环境⒈ 硬件:每个学生需配备计算机一台。

⒉ 软件:Visual C++;三、实验内容1.建立OpenGL编程环境(注:Windows自带gl和glu,还需要安装glut库文件。

)(a)查看Windows自带的OpenGL文件。

在文件夹c:\windows\system32下查看是否存在文件opengl32.dll和glu32.dll;在Visual Studio的安装目录Vc7\PlatformSDK\Include\gl下查看是否存在gl.h和glu.h;在Vc7\PlatformSDK\Lib 下是否存在opengl32.lib和glu32.lib。

OpenGl大作业

OpenGl大作业

八大行星与太阳代码:#include<glut.h>#include<math.h>// Lighting valuesGLfloat whiteLight[] = { 0.2f, 0.2f, 0.2f, 1.0f }; GLfloat sourceLight[] = { 0.8f, 0.8f, 0.8f, 1.0f }; GLfloat lightPos[] = { 0.0f, 0.0f, 0.0f, 1.0f };// Earth and Moon angle of revolutionGLfloat fMoonRot = 10.0f;GLfloat fEarthRot = 29.786f;GLfloat fMercuryRot = 47.874f;GLfloat fVenusRot = 35.024f;GLfloat fMarsRot = 24.131f;GLfloat fJupiterRot = 13.059f;GLfloat fSaturnRot = 9.644f;GLfloat fUranusRot = 6.799f;GLfloat fNeptuneRot = 5.433f;void Neptune(){glColor3ub(162,56,90);glutSolidSphere(22.0f,30,30);}void Uranus(){glColor3ub(146,48,47);glutSolidSphere(22.5f,30,30);}void Saturn(){glColor3ub(100,190,59);glutSolidSphere(29.0f,30,30);}void Jupiter(){glColor3ub(49,100,112);glutSolidSphere(30.0f,30,30);}void Mars(){glColor3ub(40,90,10);glutSolidSphere(7.50f,30,30);}void Venus(){glColor3ub(46,112,12);glutSolidSphere(7.0f,30,30);}void Mercury(){glColor3ub(18,46,19);glutSolidSphere(5.0f,30,30);}void Moon(){glColor3ub(0,200,0);glutSolidSphere(5.0f,30,30);}void Earth(){glColor3ub(46,10,0);glutSolidSphere(15.0f,30,30);glPushMatrix();glRotatef(fMoonRot,0.0f,1.0f,0.0f);glTranslatef(25.0f,0.0f,0.0f);Moon();glPopMatrix();}void Solar(){glColor3ub(255,63,0);glutSolidSphere(40.0f,30,30);glPushMatrix();glRotatef(fMercuryRot,0.0f,1.0f,0.09f); glTranslatef(48.50f,0.0f,0.0f); Mercury();glPopMatrix();glPushMatrix();glRotatef(fVenusRot,0.0f,1.0f,0.07f); glTranslatef(54.0f,0.0f,0.0f);Venus();glPopMatrix();glPushMatrix();glRotatef(fEarthRot,0.0f,1.0f,0.05f); glTranslatef(69.50f,0.0f,0.0f);Earth();glPopMatrix();glPushMatrix();glRotatef(fMarsRot,0.0f,1.0f,0.09f); glTranslatef(118.50f,0.0f,0.0f);Mars();glPopMatrix();glPushMatrix();glRotatef(fJupiterRot,0.0f,1.0f,.06f); glTranslatef(778.0f/2,0.0f,0.0f); Jupiter();glPopMatrix();glPushMatrix();glRotatef(fSaturnRot,0.0f,1.0f,0.12f); glTranslatef(1426.0f/6,0.0f,0.0f); Saturn();glPopMatrix();glPushMatrix();glRotatef(fUranusRot,0.0f,1.0f,0.08f); glTranslatef(2870.0f/6,0.0f,0.0f); Uranus();glPopMatrix();glPushMatrix();glRotatef(fNeptuneRot,0.0f,1.0f,0.14f);glTranslatef(4497.0f/6,0.0f,0.0f);Neptune();glPopMatrix();}// Called to draw scenevoid display(void){glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glMatrixMode(GL_MODELVIEW);glPushMatrix();glTranslatef(0.0f,0.0f,-400.0f);Solar();glPopMatrix();glutSwapBuffers();}// This function does any needed initialization on the rendering// context.void init(){// Light values and coordinatesglEnable(GL_DEPTH_TEST); // Hidden surface removalglFrontFace(GL_CCW); // Counter clock-wise polygons face out glEnable(GL_CULL_FACE); // Do not calculate inside of jet// Enable lightingglEnable(GL_LIGHTING);// Setup and enable light 0glLightModelfv(GL_LIGHT_MODEL_AMBIENT,whiteLight);glLightfv(GL_LIGHT0,GL_DIFFUSE,sourceLight);glLightfv(GL_LIGHT0,GL_POSITION,lightPos);glEnable(GL_LIGHT0);// Enable color trackingglEnable(GL_COLOR_MATERIAL);// Set Material properties to follow glColor values glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);// Black blue backgroundglClearColor(0.0f, 0.0f, 0.0f, 1.0f );}void move(void){fMoonRot+= 0.50f;if(fMoonRot > 360.0f)fMoonRot = 0.0f;// Step earth orbit .25 degreesfEarthRot += .29786f;if(fEarthRot > 360.0f)fEarthRot = 0.0f;fMercuryRot += .47874f;if(fMercuryRot > 360.0f)fMercuryRot = 0.0f;fVenusRot += .35024f;if(fVenusRot > 360.0f)fVenusRot = 0.0f;fMarsRot += .24131f;if(fMarsRot > 360.0f)fMarsRot = 0.0f;fJupiterRot += .13059f;if(fJupiterRot > 360.0f)fJupiterRot = 0.0f;fSaturnRot += .09644f;if(fSaturnRot > 360.0f)fSaturnRot = 0.0f;fUranusRot += .06799f;if(fUranusRot > 360.0f)fUranusRot = 0.0f;fNeptuneRot += .05433f;if(fNeptuneRot > 360.0f)fNeptuneRot = 0.0f;glutPostRedisplay();}void reshape(int w, int h){GLfloat fAspect;// Prevent a divide by zeroif(h == 0)h = 1;// Set Viewport to window dimensionsglViewport(0, 0, w, h);// Calculate aspect ratio of the windowfAspect = (GLfloat)w/(GLfloat)h;// Set the perspective coordinate systemglMatrixMode(GL_PROJECTION);glLoadIdentity();// field of view of 90 degrees, near and far planes 1.0 and 425 gluPerspective(90.0f, fAspect, 1.0, 1000.0);// Modelview matrix resetglMatrixMode(GL_MODELVIEW);glLoadIdentity();}void InitAWindow(int argc, char **argv){glutInit(&argc, argv);glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);glutInitWindowSize(800,800);glutCreateWindow("Sun System:the eight and the one");return;}int main(int argc, char* argv[]){InitAWindow(argc, argv);glutReshapeFunc(reshape);glutDisplayFunc(display); glutIdleFunc(move);init();glutMainLoop();return 0;}。

计算机图形学实验2------OPengl绘制3D图形

计算机图形学实验2------OPengl绘制3D图形

实验二 OPengl绘制3D图形一、实验目的➢了解和学习OpengL的编程➢通过学习掌握OpenGL 基本图元的绘制,进行3D图形的绘制➢掌握基于 Win32、Visual C++环境绘制3D 图形配置过程、绘制原理二、环境配置1)创建一个工程。

2)链接OpenGL libraries。

在Visual C++中先单击Project,再单击Settings,再找到Link单击,最后在Object/library modules 的最前面加上opengl32.lib glu32.lib glut.lib g laux.lib gdi32.lib user32.lib advapi32.lib或者在project -> add to project ->files引入需要的头文件3)单击Project Settings中的C/C++标签,将Preprocessor definitions 中的_CONSOLE 改为__WINDOWS。

最后单击OK。

三、实验关键代码void CCMyOpenGLView::RenderScene(){glLoadIdentity();glTranslatef(m_xPos,m_yPos,-5.0f);glRotatef(m_xAngle,1.0f,0.0f,0.0f);glRotatef(m_yAngle,0.0f,1.0f,0.0f);glutWireCone(1.0f,2.0,20,20);}bool CCMyOpenGLView::InitializeOpenGL(){m_pDC = new CClientDC(this);if(m_pDC == NULL){MessageBox("Error Obtaining DC");return FALSE;}if(!SetupPixelFormat()){return FALSE;}m_hRC = ::wglCreateContext (m_pDC->GetSafeHdc ());if(m_hRC == 0){MessageBox("RC创建失败!");return FALSE;}if(::wglMakeCurrent (m_pDC->GetSafeHdc (), m_hRC)==FALSE) {MessageBox("设置当前RC失败!");return FALSE;}::glClearColor(0.0f,0.0f,0.0f,0.0f);::glClearDepth(1.0f);::glEnable(GL_DEPTH_TEST);return TRUE;}bool CCMyOpenGLView::SetupPixelFormat(){static PIXELFORMATDESCRIPTOR pfd ={sizeof(PIXELFORMATDESCRIPTOR),1, PFD_DRAW_TO_WINDOW |PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,PFD_TYPE_RGBA, 24, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 16, 0, 0, PFD_MAIN_PLANE, 0, 0, 0, 0};int m_nPixelFormat = ::ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd);if ( m_nPixelFormat == 0 ){return FALSE;}if(::SetPixelFormat(m_pDC->GetSafeHdc(),m_nPixelFormat,&pfd)==FALSE){return FALSE;}return TRUE;}int CCMyOpenGLView::OnCreate(LPCREATESTRUCT lpCreateStruct) {if (CView::OnCreate(lpCreateStruct) == -1)return -1;InitializeOpenGL();return 0;}void CCMyOpenGLView::OnDestroy(){CView::OnDestroy();if(::wglMakeCurrent (0,0) == FALSE)MessageBox("Could not make RC non-current");if(::wglDeleteContext (m_hRC)==FALSE)MessageBox("Could not delete RC");if(m_pDC)delete m_pDC;m_pDC = NULL;}BOOL CCMyOpenGLView::OnEraseBkgnd(CDC* pDC){return TRUE;}void CCMyOpenGLView::OnSize(UINT nType, int cx, int cy){CView::OnSize(nType, cx, cy);GLdouble aspect_ratio;if ( 0 >= cx || 0 >= cy )return;::glViewport(0, 0, cx, cy);aspect_ratio = (GLdouble)cx/(GLdouble)cy;::glMatrixMode(GL_PROJECTION);::glLoadIdentity();::gluPerspective(45.0f, aspect_ratio, .01f, 200.0f);::glMatrixMode(GL_MODELVIEW);::glLoadIdentity();}void CCMyOpenGLView::OnLButtonDown(UINT nFlags, CPoint point) {m_MouseDownPoint=point;SetCapture();CView::OnLButtonDown(nFlags, point);}void CCMyOpenGLView::OnLButtonUp(UINT nFlags, CPoint point) {m_MouseDownPoint=CPoint(0,0);ReleaseCapture();CView::OnLButtonUp(nFlags, point);}void CCMyOpenGLView::OnMouseMove(UINT nFlags, CPoint point) {if (GetCapture()==this){m_xAngle+=(point.y-m_MouseDownPoint.y)/3.6;m_yAngle+=(point.x-m_MouseDownPoint.x)/3.6;InvalidateRect(NULL,FALSE);m_MouseDownPoint=point;};CView::OnMouseMove(nFlags, point);}void CCMyOpenGLView::OnObjectsCone(){m_bTeapot=FALSE;m_bSphere=FALSE;m_bCone=TRUE;InvalidateRect(NULL,FALSE);}四、实验结果五、心得体会通过学习Opengl编程,实现三维立体图形的绘制,了解了设置像素格式,即OpenGL怎样操作像素、创建着色描述表并当前化着色表、删除着色表,响应WM_DESTROY消息响应WM_CREATE消息等等,函数实现了解了Opengl实用工具集,它体现了几乎所有现代窗口系统所共有的功能的函数库。

华中科技大学计算机图形学实验报告——画直线、日地月模型

华中科技大学计算机图形学实验报告——画直线、日地月模型

华中科技大学计算机图形学实验报告——画直线、日地月模型计算机图形学基础实验报告专业:班级:姓名:学号:日期:OpenGL简介(1)OpenGL作为一个性能优越的图形应用程序设计界面(API),它独立于硬件和窗口系统,在运行各种操作系统的各种计算机上都可用,并能在网络环境下以客户/服务器模式工作,是专业图形处理、科学计算等高端应用领域的标准图形库。

它具有以下功能。

1. 模型绘制2. 模型观察在建立了三维景物模型后,就需要用OpenGL描述如何观察所建立的三维模型。

3. 颜色模式的指定OpenGL应用了一些专门的函数来指定三维模型的颜色。

4. 光照应用用OpenGL绘制的三维模型必须加上光照才能更加与客观物体相似。

5. 图象效果增强OpenGL提供了一系列的增强三维景观的图象效果的函数,这些函数通过反走样、混合和雾化来增强图象的效果。

6. 位图和图象处理OpenGL还提供了专门对位图和图象进行操作的函数。

7. 纹理映射8. 实时动画9. 交互技术应用软件OpenGL窗口系统操作系统图形硬件图1.1 OpenGL图形处理系统的层次结构(2)OpenGL的操作步骤在OpenGL中进行的图形操作直至在计算机屏幕上渲染绘制出三维图形景观的基本步骤如下:1. 根据基本图形单元建立景物模型,得到景物模型的数学描述(OpenGL 中把点、线、多边形、图像和位图都作为基本图形单元);2. 把景物模型放在三维空间中的合适的位置,并且设置视点(Viewpoint)以观察所感兴趣的景观;3. 计算模型中所有物体的色彩,同时确定光照条件、纹理粘贴方式等;4. 把景物模型的数学描述及其色彩信息转换至计算机屏幕上的像素,这个过程也就是光栅化(rasterization)。

在这些步骤的执行过程中,OpenGL可能执行其他的一些操作,例如自动消隐处理等。

另外,景物光栅化之后被送入帧缓冲器之前还可以根据需要对象素数据进行操作。

(3)OpenGL的组成OpenGL不是一种编程语言,而是一种API(应用程序编程接口),它实际上是一种图形与硬件的接口,包括了多个图形函数。

太阳月亮提示源代码

太阳月亮提示源代码

太阳月亮提示源代码
(最新版)
目录
1.太阳和月亮的简介
2.太阳和月亮的源代码
3.太阳和月亮源代码的启示
正文
1.太阳和月亮的简介
太阳和月亮是我们宇宙中最重要的天体之一。

太阳是太阳系中的中心,它是地球上生命的源泉,为地球提供了光和热。

月亮是地球的卫星,它对地球的潮汐和气候有着重要的影响。

2.太阳和月亮的源代码
太阳的源代码是太阳的核心,它由氢原子核聚变成氦原子核,释放出巨大的能量。

这个过程是通过核聚变反应实现的,它的数学表达式是:4 个氢原子核(质子)聚变成一个氦原子核,同时释放出 2 个正电子和中微子。

月亮的源代码是它的岩石层,它是由撞击地球的小行星和彗星形成的。

月亮的岩石层由各种不同的矿物质组成,包括橄榄石、辉石和斜长石等。

3.太阳和月亮源代码的启示
太阳和月亮的源代码告诉我们,宇宙中的万物都是有其内在规律的。

太阳的源代码揭示了核聚变的原理,这个原理被广泛应用于核能发电和核武器等领域。

月亮的源代码揭示了地球形成的过程,帮助我们更好地理解了地球的演化历程。

同时,太阳和月亮的源代码也启示我们,保护环境和资源是至关重要的。

太阳和月亮是我们宇宙中最重要的天体之一,它们的存在和演化对地
球的生命和生态系统产生了深远的影响。

opengl实现太阳系天体运动

opengl实现太阳系天体运动

Opengl实现太阳系天体运动本文主要讲OpenGL中的几何变换。

我们生活在一个三维的世界——如果要观察一个物体,我们可以:1、从不同的位置去观察它。

(视点变换/视图变换,gluLookAt)2、移动或者旋转它,当然了,如果它只是计算机里面的物体,我们还可以放大或缩小它。

(模型变换)3、如果把物体画下来,我们可以选择:是否需要一种“近大远小”的透视效果。

另外,我们可能只希望看到物体的一部分,而不是全部(剪裁)。

(投影变换)4、我们可能希望把整个看到的图形画下来,但它只占据纸张的一部分,而不是全部。

(视口变换)这些,都可以在OpenGL中实现。

1、模型变换和视图变换从“相对移动”的观点来看,改变观察点的位置与方向和改变物体本身的位置与方向具有等效性。

在OpenGL中,实现这两种功能甚至使用的是同样的函数。

由于模型和视图的变换都通过矩阵运算来实现,在进行变换前,应先设置当前操作的矩阵为“模型视图矩阵”。

设置的方法是以GL_MODELVIEW为参数调用glMatrixMode函数,像这样:glMatrixMode(GL_MODELVIEW);通常,我们需要在进行变换前把当前矩阵设置为单位矩阵。

这也只需要一行代码:glLoadIdentity();然后,就可以进行模型变换和视图变换了。

进行模型和视图变换,主要涉及到三个函数:glTranslate*,把当前矩阵和一个表示移动物体的矩阵相乘。

三个参数分别表示了在三个坐标上的位移值。

glRotate*,把当前矩阵和一个表示旋转物体的矩阵相乘。

物体将绕着(0,0,0)到(x,y,z)的直线以逆时针旋转,参数angle表示旋转的角度。

glScale*,把当前矩阵和一个表示缩放物体的矩阵相乘。

x,y,z分别表示在该方向上的缩放比例。

注意我都是说“与XX相乘”,而不是直接说“这个函数就是旋转”或者“这个函数就是移动”,这是有原因的,马上就会讲到。

假设当前矩阵为单位矩阵,然后先乘以一个表示旋转的矩阵R,再乘以一个表示移动的矩阵T,最后得到的矩阵再乘上每一个顶点的坐标矩阵v。

图形学opengl绘图入门代码

图形学opengl绘图入门代码

图形学opengl绘图入门代码// opg1.cpp : Defines the entry point for the console application.//#include "stdafx.h"/*#includevoid display(void){glClear (GL_COLOR_BUFFER_BIT);// clear all pixelsglColor3f (1.0, 1.0, 1.0);glBegin(GL_POL YGON);//draw white polygonglVertex3f (0.25, 0.25, 0.0);glVertex3f (0.75, 0.25, 0.0);glVertex3f (0.75, 0.75, 0.0);glVertex3f (0.25, 0.75, 0.0);glEnd();glFlush ();// start processing buffered OpenGL routines}void init (void){glClearColor (0.0, 0.0, 0.0, 0.0);// select clearing colorglMatrixMode(GL_PROJECTION);glLoadIdentity();glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);// initialize viewing values }int main(int argc, char** argv){glutInit(&argc, argv);glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);glutInitWindowSize (250, 250); //Declare initial window size.glutInitWindowPosition (100, 100);//Declare initial window position.glutCreateWindow ("hello");//Open window with "hello"in its title bar.init ();//Call initialization routines.glutDisplayFunc(display); /*Register callback function to display graphics.glutMainLoop();//Enter main loop and process events.return 0; // ANSI C requires main to return int.}*/// GL_2_17.cpp : Defines the entry point for the console application.///*#include#include#include#includeconst int screenWidth=640;const int screenHeight=480;GLdouble A,B,C,D;void myInit(void){glClearColor(1.0,1.0,1.0,0.0);glColor3f(0.0f,0.0f,0.0f);glPointSize(2.0);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0,screenWidth,0.0,screenHeight);A=screenWidth/4.0;B=0.0;C=D=screenHeight/2.0;}void myDisplay(void){glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBegin(GL_POINTS);for(GLdouble x=0;x<4.0;x+=0.001){GLdouble func=exp(-x)*cos(2*3.14159265*x);glVertex2d(A*x+B,C*func+D);}glEnd();glFlush();}void main(int argc, char** argv){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(640,480); glutInitWindowPosition(100,150); glutCreateWindow("Dot plot of a function"); glutDisplayFunc(myDisplay);myInit();glutMainLoop();*/图形学第三课--opengl绘图入门代码#include#include#include#include#includevoid myInit(void){glClearColor(0.0,0.0,0.0,0.0);glClear(GL_COLOR_BUFFER_BIT);}void myReshape(GLsizei w,GLsizei h){glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION);glLoadIdentity();if(w<=h)glOrtho(-1.5,1.5,-1.5*(GLfloat)h/(GLfloat)w, 1.5*(GLfloat)h/(GLfloat)w,-10.0,10.0);elseglOrtho(-1.5*(GLfloat)h/(GLfloat)w,1.5*(GLfloat)h/(GLfloat)w,-1.5,1.5,-10.0,10.0);glMatrixMode(GL_MODELVIEW);glLoadIdentity();}void myDisplay(void){glColor3f(1.0,1.0,0.0);//auxWireSphere(1.0);auxSolidTeapot(1.0);glFlush();}void main(int argc, char** argv){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(640,480); glutInitWindowPosition(100,150); glutCreateWindow("AUX_SAMPLE");myInit();glutReshapeFunc(myReshape); glutDisplayFunc(myDisplay);glutMainLoop();}图形学opengl入门--建模代码#include#include#include#include#includevoid DrawMyObjects(void){/* draw some points */glBegin(GL_POINTS);glColor3f(1.0,0.0,0.0);glVertex2f(-10.0,11.0);glColor3f(1.0,1.0,0.0);glVertex2f(-9.0,10.0);glColor3f(0.0,1.0,1.0);glVertex2f(-8.0,12.0);glEnd();/* draw some line_segments */glBegin(GL_LINES);glVertex2f(-11.0,8.0); glVertex2f(-7.0,7.0); glColor3f(1.0,0.0,1.0); glVertex2f(-11.0,9.0); glVertex2f(-8.0,6.0);glEnd();/* draw one opened_line */ glBegin(GL_LINE_STRIP); glColor3f(0.0,1.0,0.0); glVertex2f(-3.0,9.0); glVertex2f(2.0,6.0); glVertex2f(3.0,8.0); glVertex2f(-2.5,6.5);glEnd();/* draw one closed_line */ glBegin(GL_LINE_LOOP); glColor3f(0.0,1.0,1.0); glVertex2f(7.0,7.0); glVertex2f(8.0,8.0); glVertex2f(10.3,7.5); glVertex2f(11.5,6.0); glVertex2f(7.5,6.0);glEnd();/* draw one filled_polygon */ glBegin(GL_POL YGON); glColor3f(0.5,0.3,0.7); glVertex2f(-7.0,2.0); glVertex2f(-8.0,3.0); glVertex2f(-10.3,0.5);glVertex2f(-6.0,-1.0);glEnd();/* draw some filled_quandrangles */ glBegin(GL_QUADS);glColor3f(0.7,0.5,0.2);glVertex2f(0.0,2.0);glVertex2f(-1.0,3.0);glVertex2f(-3.3,0.5);glVertex2f(-0.5,-1.0);glColor3f(0.5,0.7,0.2);glVertex2f(3.0,2.0);glVertex2f(2.0,3.0);glVertex2f(0.0,0.5);glVertex2f(2.5,-1.0);glEnd();/* draw some filled_strip_quandrangles */ glBegin(GL_QUAD_STRIP);glVertex2f(6.0,-2.0);glVertex2f(5.5,1.0);glVertex2f(8.0,-1.0);glColor3f(0.8,0.0,0.0);glVertex2f(9.0,2.0);glVertex2f(11.0,-2.0);glColor3f(0.0,0.0,0.8);glVertex2f(11.0,2.0);glVertex2f(13.0,-1.0);glColor3f(0.0,0.8,0.0);glVertex2f(14.0,1.0);glEnd();/* draw some filled_triangles */glBegin(GL_TRIANGLES);glColor3f(0.2,0.5,0.7);glVertex2f(-12.3,-7.5);glVertex2f(-8.5,-6.0);glColor3f(0.2,0.7,0.5);glVertex2f(-8.0,-7.0);glVertex2f(-7.0,-4.5);glVertex2f(-5.5,-9.0);glEnd();/* draw some filled_strip_triangles */ glBegin(GL_TRIANGLE_STRIP); glVertex2f(-1.0,-8.0);glVertex2f(-2.5,-5.0);glColor3f(0.8,0.8,0.0);glVertex2f(1.0,-7.0);glColor3f(0.0,0.8,0.8);glVertex2f(2.0,-4.0);glColor3f(0.8,0.0,0.8);glVertex2f(4.0,-6.0);glEnd();/* draw some filled_fan_triangles */ glBegin(GL_TRIANGLE_FAN); glVertex2f(8.0,-6.0);glVertex2f(10.0,-3.0);glColor3f(0.8,0.2,0.5);glVertex2f(12.5,-4.5);glColor3f(0.2,0.5,0.8);glVertex2f(13.0,-7.5);glColor3f(0.8,0.5,0.2);glVertex2f(10.5,-9.0);glEnd();}void myInit(void){glClearColor(0.0,0.0,0.0,0.0);glClear(GL_COLOR_BUFFER_BIT);glShadeModel(GL_FLAT);}void myReshape(GLsizei w,GLsizei h) {glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION);glLoadIdentity();if(w<=h)glOrtho(-20.0,20.0,-20.0*(GLfloat)h/(GLfloat)w, 20.0*(GLfloat)h/(GLfloat)w,-50.0,50.0);elseglOrtho(-20.0*(GLfloat)h/(GLfloat)w,20.0*(GLfloat)h/(GLfloat)w,-20.0,20.0,-50.0,50.0);glMatrixMode(GL_MODELVIEW);glLoadIdentity();}void myDisplay(void){glColor3f(1.0,1.0,0.0);DrawMyObjects();glFlush();}void main(int argc, char** argv){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(600,600); glutInitWindowPosition(100,150); glutCreateWindow("Geometric Primitive Types"); myInit();glutReshapeFunc(myReshape); glutDisplayFunc(myDisplay);glutMainLoop();}。

opengl绘制教室大作业源代码wp

opengl绘制教室大作业源代码wp

wp.h#include <windows.h>// Windows的头文件#include <glut.h>// 包含OpenGL实用库#include<stdio.h>// 标准输入/输出库的头文件#include<glaux.h>// GLaux库的头文件#include<math.h>#include <stdlib.h>#include <time.h>#include <iostream.h>//..................................定义变量区............................................................////.............................用于场景漫游有关变量........................................//GLfloat xrot=0.0f;// X 旋转量GLfloat yrot=0.0f;// Y 旋转量GLfloat x=0.0f;// X 平移量GLfloat y=0.0f;// Y 平移量GLfloat z=-40.0f;// 深入屏幕的距离//.............................用于键盘操作有关变量.......................................//BOOL light=TRUE;// 光源的开/关BOOL lp;// L键按下了么?BOOL fp;// F键按下了么?BOOL TurnOn=FALSE; // 开灯GLUquadricObj *qobj;int fantheta=0; // 风扇转过角度HGLRC hRC=NULL;// 窗口着色描述表句柄HDC hDC=NULL;// OpenGL渲染描述表句柄HWND hWnd=NULL;// 保存我们的窗口句柄HINSTANCE hInstance;// 保存程序的实例bool keys[256];// 保存键盘按键的数组bool active=TRUE;// 窗口的活动标志,缺省为TRUEbool fullscreen=TRUE;// 全屏标志缺省,缺省设定成全屏模式//.................................光照材质有关变量......................................//GLfloat light_Ambient[]= { 0.1f, 0.1f, 0.1f, 1.0f }; // 环境光参数GLfloat light_Diffuse[]= { 1.0f, 1.0f, 0.45f, 1.0f };// 漫射光参数GLfloat light_position[]={0.0,29.9,0.0,1.0}; // 光源位置GLfloat light_position1[]={20.0,29.9,0.0,1.0};GLfloat mat_ambient[]={0.05f,0.05f,0.05f,1.0f}; // 材质环境光参数GLfloat mat_specular[]={0.8,1.0,1.0,1.0}; // 材质镜面光参数GLfloat mat_shininess[]={5.0};GLfloat white_light[]={1.0,1.0,1.0,1.0}; // 光GLfloat no_mat[]= {0.0f, 0.0f, 0.0f, 1.0f};// 无材质色GLfloat no_shininess[]= {0.0f};// 无反光GLfloat mat_diffuse[]= {1.0f, 1.0f, 1.0f, 1.0f};// 漫反射GLfloat diffusion[]={0.521f,0.121f,0.0547f,1.0};//......................................纹理...........................................................//GLuint texture[6];// 7种纹理的储存空间//.....................................时钟有关变量...................................................//GLfloat cx=35.0,cy=15.0,cz=-79.9,radius=5.0;GLfloat hradius=2.0,mradius=3.0,sradius=4.0;GLfloat sangle,mangle,hangle;GLuint second,miniter,hour;char timebuffer[9];LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);// WndProc的定义wp.cpp#include"wp.h"//......................................创建纹理..........................................................//AUX_RGBImageRec *LoadBMP(char *Filename)// 载入位图图象{FILE *File=NULL;// 文件句柄if (!Filename)// 确保文件名已提供{return NULL;// 如果没提供,返回NULL}File=fopen(Filename,"r");// 尝试打开文件if (File)// 文件存在{fclose(File);// 关闭句柄return auxDIBImageLoad(Filename);// 载入位图并返回指针}return NULL;}// 如果载入失败,返回NULLint LoadGLTextures()// 载入位图并转换成纹理{int Status=FALSE;// 状态指示器AUX_RGBImageRec *TextureImage[6];// 创建纹理的存储空间memset(TextureImage,0,sizeof(void *)*1);// 将指针设为NULL// 载入位图,检查有无错误,如果位图没找到则退出Status=TRUE;// 将Status 设为TRUE//...............................创建第一个纹理,地板.......................................//if (TextureImage[0]=LoadBMP("floor.bmp")){glGenTextures(1, &texture[0]);// 创建纹理glBindTexture(GL_TEXTURE_2D, texture[0]);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);//远处采用线性滤波glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );//近处采用MipMapped滤波gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);//绑定纹理}if (TextureImage[0])//纹理是否存在{if (TextureImage[0]->data)//纹理图像是否存在{free(TextureImage[0]->data);//释放纹理图像占用的内存}free(TextureImage[0]);//释放图像结构//...............................创建第二个纹理,墙.......................................//if (TextureImage[1]=LoadBMP("wall.bmp")){glGenTextures(1, &texture[1]); //创建纹理glBindTexture(GL_TEXTURE_2D, texture[1]);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);//远处采用线性滤波glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );//近处采用MipMapped滤波gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[1]->sizeX, TextureImage[1]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[1]->data);//绑定纹理}if (TextureImage[1])//纹理是否存在{if (TextureImage[1]->data)//纹理图像是否存在{free(TextureImage[1]->data);//释放纹理图像占用的内存}free(TextureImage[1]);//释放图像结构}//...............................创建第三个纹理,窗户.......................................//if (TextureImage[2]=LoadBMP("glass.bmp")){glGenTextures(1, &texture[2]);//创建纹理glBindTexture(GL_TEXTURE_2D, texture[2]);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);//远处采用线性滤波glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );//近处采用MipMapped滤波gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[2]->sizeX, TextureImage[2]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[2]->data);//绑定纹理}if (TextureImage[2])//纹理是否存在{if (TextureImage[2]->data)//纹理图像是否存在{free(TextureImage[2]->data);//释放纹理图像占用的内存}free(TextureImage[2]);//释放图像结构//...............................创建第四个纹理,门.......................................//if (TextureImage[3]=LoadBMP("door.bmp")){glGenTextures(1, &texture[3]);//创建纹理glBindTexture(GL_TEXTURE_2D, texture[3]);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);//远处采用线性滤波glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );//近处采用MipMapped滤波gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[3]->sizeX, TextureImage[3]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[3]->data);//绑定纹理}if (TextureImage[3])//纹理是否存在{if (TextureImage[3]->data)//纹理图像是否存在{free(TextureImage[3]->data);//释放纹理图像占用的内存}free(TextureImage[3]);//释放图像结构}//...............................创建第五个纹理,灯.......................................//if (TextureImage[4]=LoadBMP("lamp.bmp")){Status=TRUE;glGenTextures(1, &texture[4]);//创建纹理glBindTexture(GL_TEXTURE_2D, texture[4]);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);//远处采用线性滤波glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );//近处采用MipMapped滤波gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[4]->sizeX, TextureImage[4]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[4]->data);//绑定纹理}if (TextureImage[4])//纹理是否存在{if (TextureImage[4]->data)//纹理图像是否存在{free(TextureImage[4]->data);//释放纹理图像占用的内存}free(TextureImage[4]);//释放图像结构}//...............................创建第六个纹理,黑板.......................................//if (TextureImage[5]=LoadBMP("blackwall.bmp")){glGenTextures(1, &texture[5]);//创建纹理glBindTexture(GL_TEXTURE_2D, texture[5]);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);//远处采用线性滤波glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );//近处采用MipMapped滤波gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[5]->sizeX, TextureImage[5]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[5]->data);//绑定纹理}if (TextureImage[5])//纹理是否存在{if (TextureImage[5]->data)//纹理图像是否存在{free(TextureImage[5]->data);//释放纹理图像占用的内存}free(TextureImage[5]);//释放图像结构}return Status;}//...................................重置OpenGL窗口大小..............................................//GLvoid ReSizeGLScene(GLsizei width, GLsizei height)// 重置OpenGL窗口大小{if (height==0)// 防止被零除{height=1;// 将Height设为1 }glViewport(0, 0, width, height);// 重置当前的视口glMatrixMode(GL_PROJECTION);// 选择投影矩阵glLoadIdentity();// 重置投影矩阵// 设置视口的大小gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,1000.0f);glMatrixMode(GL_MODELVIEW);// 选择模型观察矩阵glLoadIdentity();// 重置模型观察矩阵}//...................................初始化操作................................................//int InitGL(GLvoid)// 此处开始对OpenGL进行所有设置{if (!LoadGLTextures())// 调用纹理载入子例程{return FALSE;// 如果未能载入,返回FALSE}glEnable(GL_TEXTURE_2D);// 启用纹理映射glShadeModel(GL_SMOOTH);// 启用阴影平滑glClearColor(0.0f, 0.0f, 0.0f, 0.0f);// 黑色背景glClearDepth(1.0f);// 设置深度缓存glEnable(GL_DEPTH_TEST);// 启用深度测试glDepthFunc(GL_LEQUAL);// 所作深度测试的类型glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);// 告诉系统对透视进行修正glLightModelfv(GL_LIGHT_MODEL_AMBIENT,mat_ambient);glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,GL_TRUE);glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);glLightfv(GL_LIGHT1,GL_POSITION,light_position); // 设置光源位置glLightfv(GL_LIGHT1,GL_AMBIENT,light_Ambient); // 设置环境光glLightfv(GL_LIGHT1,GL_DIFFUSE,white_light); // 设置漫射光glLightfv(GL_LIGHT1,GL_SPECULAR,white_light);glEnable(GL_LIGHTING);glEnable(GL_LIGHT1);// 启用一号光源glEnable(GL_COLOR_MATERIAL);glPixelStorei(GL_UNPACK_ALIGNMENT,1); //对齐像素字节函数return TRUE;// 初始化OK}//..............................................开始绘制..........................................//int DrawGLScene(GLvoid)// 从这里开始进行所有的绘制{glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);// 清除屏幕和深度缓存glLoadIdentity();// 重置当前的模型观察矩阵glTranslatef(x,y,z); //沿X,Y,Z轴移动glRotatef(xrot,1.0f,0.0f,0.0f);// 绕X轴旋转glRotatef(yrot,0.0f,1.0f,0.0f);// 绕Y轴旋转glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat); //选择材质光照glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);//.............................................房间........................................................////.............................................地板........................................................//glBindTexture(GL_TEXTURE_2D, texture[0]);// 选择纹理glBegin(GL_QUADS); //开始绘制四边形glColor3f(1.0f, 1.0f, 1.0f);glNormal3f(0.0,1.0,0.0); //glNormal3f用于定义点的法向量glTexCoord2f(1.0f, 1.0f);glVertex3f(-50.0f, -21.0f, -80.0f);//右上glTexCoord2f(1.0f, 0.0f);glVertex3f(-50.0f, -21.0f, 40.0f);//右下glTexCoord2f(0.0f, 0.0f);glVertex3f( 50.0f, -21.0f, 40.0f);//左下glTexCoord2f(0.0f, 1.0f);glVertex3f( 50.0f, -21.0f, -80.0f);//左上glEnd();//.............................................天花板......................................................//glBindTexture(GL_TEXTURE_2D, texture[1]);// 选择纹理glBegin(GL_QUADS);//开始绘制四边形glColor3f(1.0f, 1.0f, 1.0f);glNormal3f(0.0f, -1.0f, 0.0f);//glNormal3f用于定义点的法向量glTexCoord2f(0.0f, 0.0f);glVertex3f(-50.0f, 30.0f, 40.0f);//左下glTexCoord2f(0.0f, 1.0f);glVertex3f(-50.0f, 30.0f, -80.0f);//左上glTexCoord2f(1.0f, 1.0f);glVertex3f( 50.0f, 30.0f, -80.0f);//右上glTexCoord2f(1.0f, 0.0f);glVertex3f( 50.0f, 30.0f, 40.0f);//右下glEnd();//............................................后墙面.......................................................//glBegin(GL_QUADS);//开始绘制四边形glColor3f(1.0f, 1.0f, 1.0f);glNormal3f(0.0f, 0.0f, 1.0f);//glNormal3f用于定义点的法向量glTexCoord2f(1.0f, 1.0f);glVertex3f(-50.0f, 30.0f, -80.0f);//右上glTexCoord2f(1.0f, 0.0f);glVertex3f(-50.0f, -21.0f, -80.0f);//右下glTexCoord2f(0.0f, 0.0f);glVertex3f( 50.0f, -21.0f, -80.0f);//左下glTexCoord2f(0.0f, 1.0f);glVertex3f( 50.0f, 30.0f, -80.0f);//左上//............................................前墙面.......................................................//glNormal3f(0.0f, 0.0f, -1.0f);//glNormal3f用于定义点的法向量glTexCoord2f(1.0f, 1.0f);glVertex3f( 50.0f, 30.0f, 40.0f);//右上glTexCoord2f(1.0f, 0.0f);glVertex3f( 50.0f, -21.0f, 40.0f);//右下glTexCoord2f(0.0f, 0.0f);glVertex3f(-50.0f, -21.0f, 40.0f);//左下glTexCoord2f(0.0f, 1.0f);glVertex3f(-50.0f, 30.0f, 40.0f);//左上//............................................左墙面.......................................................//glNormal3f( 1.0f, 0.0f, 0.0f);//glNormal3f用于定义点的法向量glTexCoord2f(1.0f, 1.0f);glVertex3f(-50.0f, 30.0f, 40.0f);//右上glTexCoord2f(1.0f, 0.0f);glVertex3f(-50.0f, -21.0f, 40.0f);//右下glTexCoord2f(0.0f, 0.0f);glVertex3f(-50.0f, -21.0f, -80.0f);//左下glTexCoord2f(0.0f, 1.0f);glVertex3f(-50.0f, 30.0f, -80.0f);//左上//............................................右墙面.......................................................//glNormal3f(-1.0f, 0.0f, 0.0f);//glNormal3f用于定义点的法向量glTexCoord2f(1.0f, 1.0f);glVertex3f( 50.0f, 30.0f, -80.0f);//右上glTexCoord2f(1.0f, 0.0f);glVertex3f( 50.0f, -21.0f, -80.0f);//右下glTexCoord2f(0.0f, 0.0f);glVertex3f( 50.0f, -21.0f, 40.0f);//左下glTexCoord2f(0.0f, 1.0f);glVertex3f( 50.0f, 30.0f, 40.0f);//左上glEnd();//.............................................桌椅..........................................................//GLuint i=0;for(;i<5;i++)//一共五排桌椅{//............................................第一张桌椅......................................................////..........................桌子的上面.......................//glPushMatrix();//因为下面要对坐标进行变换,所以先将信息入栈保护,使整个工程的坐标不发生改变glColor3f(1.0,1.0,0.45);glTranslatef(30.0f,-10.0f,-30.0+i*9);//平移变换glScalef(13.0f,0.3f,5.0f);//缩放变换glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glutSolidCube(1.0);//绘制立方体glPopMatrix();//弹出堆栈//..........................桌子的下面......................//glPushMatrix();//压入堆栈glColor3f(1.0,1.0,0.45);glTranslatef(30.0f,-13.0f,-30.0+i*9);//平移变换glScalef(11.0f,0.3f,5.0f);//缩放变换glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glutSolidCube(1.0);//绘制立方体glPopMatrix();//弹出堆栈//...........................桌子的后面......................//glPushMatrix();//压入堆栈glColor3f(0.2,0.8,0.8);glTranslatef(30.0f,-12.0f,-32.5f+i*9);//平移变换glScalef(11.0f,3.6f,0.3f);//缩放变换glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glutSolidCube(1.0);//绘制立方体glPopMatrix();//弹出堆栈//...........................桌子的左边桌腿...................//glPushMatrix();//压入堆栈glColor3f(0.0,0.0,0.0);glTranslatef(25.0f,-17.0f,-32.5f+i*9);//平移变换glScalef(0.5f,8.0f,0.3f);//缩放变换glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glutSolidCube(1.0);//绘制立方体glPopMatrix();//弹出堆栈//...........................桌子的右边桌腿...................//glPushMatrix();//压入堆栈glColor3f(0.0,0.0,0.0);glTranslatef(35.0f,-17.0f,-32.5f+i*9);//平移变换glScalef(0.5f,8.0f,0.3f);//缩放变换glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glutSolidCube(1.0);//绘制立方体glPopMatrix();//弹出堆栈//............................椅子............................//glPushMatrix();//压入堆栈glColor3f(0.2,0.8,0.8);glTranslatef(30.0f,-15.0f,-25.0+i*9);//平移变换glScalef(11.3f,0.3f,3.5f);//缩放变换glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glutSolidCube(1.0);//绘制立方体glPopMatrix();//弹出堆栈//.....................................................第二张桌椅....................................................// //..........................桌子的上面.......................//glPushMatrix();glColor3f(1.0,1.0,0.45);glTranslatef(0.0f,-10.0f,-30.0+i*9);glScalef(26.0f,0.3f,5.0f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//..........................桌子的下面......................//glPushMatrix();glColor3f(1.0,1.0,0.45);glTranslatef(0.0f,-13.0f,-30.0+i*9);glScalef(24.0f,0.3f,5.0f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//...........................桌子的后面......................//glPushMatrix();glColor3f(0.2,0.8,0.8);glTranslatef(0.0f,-12.0f,-32.5f+i*9);glScalef(24.0f,3.6f,0.3f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//...........................桌子的左边桌腿...................//glPushMatrix();glColor3f(0.0,0.0,0.0);glTranslatef(-11.4f,-17.0f,-32.5f+i*9);glScalef(0.5f,8.0f,0.3f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//...........................桌子的右边桌腿...................//glPushMatrix();glColor3f(0.0,0.0,0.0);glTranslatef(11.4f,-17.0f,-32.5f+i*9);glScalef(0.5f,8.0f,0.3f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//............................椅子............................//glPushMatrix();glColor3f(0.2,0.8,0.8);glTranslatef(0.0f,-15.0f,-25.0+i*9);glScalef(24.3f,0.3f,3.5f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//........................................第三张桌椅..................................................////..........................桌子的上面.......................//glPushMatrix();glColor3f(1.0,1.0,0.45);glTranslatef(-30.0f,-10.0f,-30.0+i*9);glScalef(13.0f,0.3f,5.0f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//..........................桌子的下面......................//glPushMatrix();glColor3f(1.0,1.0,0.45);glTranslatef(-30.0f,-13.0f,-30.0+i*9);glScalef(11.0f,0.3f,5.0f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//...........................桌子的后面......................//glPushMatrix();glColor3f(0.2,0.8,0.8);glTranslatef(-30.0f,-12.0f,-32.5f+i*9);glScalef(11.0f,3.6f,0.3f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//...........................桌子的左边桌腿...................//glPushMatrix();glColor3f(0.0,0.0,0.0);glTranslatef(-25.0f,-17.0f,-32.5f+i*9);glScalef(0.5f,8.0f,0.3f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//...........................桌子的右边桌腿...................//glPushMatrix();glColor3f(0.0,0.0,0.0);glTranslatef(-35.0f,-17.0f,-32.5f+i*9);glScalef(0.5f,8.0f,0.3f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//............................椅子............................//glPushMatrix();glColor3f(0.2,0.8,0.8);glTranslatef(-30.0f,-15.0f,-25.0+i*9);glScalef(11.3f,0.3f,3.5f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();}//..........................................最后一排的椅背..........................................////....................最右边的椅背..................//glPushMatrix();glColor3f(0.2,0.8,0.8);glTranslatef(30.0f,-15.0f,12.6f);glScalef(11.3f,12.0f,0.3f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//.....................中间的椅背...................//glPushMatrix();glColor3f(0.2,0.8,0.8);glTranslatef(0.0f,-15.0f,12.6f);glScalef(24.3f,12.0f,0.3f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//.....................最左边的椅背.................//glPushMatrix();glColor3f(0.2,0.8,0.8);glTranslatef(-30.0f,-15.0f,12.6f);glScalef(11.3f,12.0f,0.3f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glutSolidCube(1.0);glPopMatrix();//................................风扇................................................//GLuint m=0;for(;m<2;m++)//一共两排风扇{qobj = gluNewQuadric();//初始化二次曲面并创建一个指向二次曲面的指针//...............................右边的风扇.............................//glPushMatrix();//压入堆栈,保证此处坐标变换不影响外部大环境glTranslatef(20.0,-3.0,-10.0-40*m); //风扇的位置if(TurnOn==TRUE) //如果TurnOn为1{fantheta+=10; //风扇转过的角度fantheta不断增加if(fantheta==360) //如果旋转角度超过360度fantheta=0; //则fantheta重新回0}glRotatef(fantheta,0,1,0); //风扇旋转//..........风扇中间的扁圆形....................//glPushMatrix();//压入堆栈glColor3f(0.7,0.7,0.7);glTranslatef(0.0,20.0,0.0);//平移变换glScalef(1,0.3,1);//缩放变换gluSphere(qobj,2,20,20);//绘制一个半径为2的球体glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glPopMatrix();//弹出堆栈//............风扇旋转120度的扇叶...............//glPushMatrix();//压入堆栈glColor3f(0.7,0.7,0.7);glTranslatef(0,20,0.0);//平移变换glScalef(1,0.05,1);//缩放变换glRotatef(120,0,1,0);//绕y周旋转120度gluCylinder(qobj,1,3,10,20,20);//绘制一个底面圆半径为1、顶面圆半径为2、高为10的圆锥体glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glPopMatrix();//弹出堆栈//............风扇不做旋转的扇叶.................//glPushMatrix();//压入堆栈glColor3f(0.7,0.7,0.7);glTranslatef(0,20,0.0);//平移变换glScalef(1,0.05,1);//缩放变换gluCylinder(qobj,1,3,10,20,20);//绘制一个底面圆半径为1、顶面圆半径为2、高为10的圆锥体glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glPopMatrix();//弹出堆栈//.............风扇旋转240度的扇叶...............//glPushMatrix();//压入堆栈glColor3f(0.7,0.7,0.7);glTranslatef(0,20,0.0);//平移变换glScalef(1,0.05,1);//缩放变换glRotatef(240,0,1,0);//绕y周旋转240度gluCylinder(qobj,1,3,10,20,20);//绘制一个底面圆半径为1、顶面圆半径为2、高为10的圆锥体glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glPopMatrix();//弹出堆栈//.............风扇的杆...........................//glPushMatrix();//压入堆栈glTranslatef(0,33,0.0);//平移变换glRotatef(90,1,0,0);//缩放变换gluCylinder(qobj,0.5,0.5,13,10,10);//绘制一个半径为0.5、高为13的圆锥体glPopMatrix();//弹出堆栈glPopMatrix();//弹出堆栈//................左边的风扇...........................................//glPushMatrix();glTranslatef(-20.0,-3.0,-10.0-40*m);if(TurnOn==TRUE){fantheta+=10;if(fantheta==360)fantheta=0;}glRotatef(fantheta,0,1,0);//..........风扇中间的扁圆形....................//glPushMatrix();//风扇中间的扁圆形glColor3f(0.7,0.7,0.7);glTranslatef(0.0,20.0,0.0);glScalef(1,0.3,1);gluSphere(qobj,2,20,20);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glPopMatrix();//............风扇旋转120度的扇叶...............//glPushMatrix();//风扇旋转120度的扇叶glColor3f(0.7,0.7,0.7);glTranslatef(0,20,0.0);glScalef(1,0.05,1);glRotatef(120,0,1,0);gluCylinder(qobj,1,3,10,20,20);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glPopMatrix();//............风扇不做旋转的扇叶.................//glPushMatrix();//风扇不做旋转的扇叶glColor3f(0.7,0.7,0.7);glTranslatef(0,20,0.0);glScalef(1,0.05,1);gluCylinder(qobj,1,3,10,20,20);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glPopMatrix();//.............风扇旋转240度的扇叶...............//glPushMatrix();//风扇旋转240度的扇叶glColor3f(0.7,0.7,0.7);glTranslatef(0,20,0.0);glScalef(1,0.05,1);glRotatef(240,0,1,0);gluCylinder(qobj,1,3,10,20,20);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glPopMatrix();//.............风扇的杆...........................//glPushMatrix();glTranslatef(0,33,0.0);glRotatef(90,1,0,0);gluCylinder(qobj,0.5,0.5,13,10,10);glPopMatrix();glPopMatrix();}//...........................................门.....................................................................//glColor3f(0.521f,0.121f,0.0547f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,diffusion);//定义材质光照glBindTexture(GL_TEXTURE_2D,texture[3]);//绑定纹理glBegin(GL_QUADS);//开始绘制四边形glColor3f(1.0f, 1.0f, 1.0f);glNormal3f(-1.0f, 0.0f, 0.0f);//glNormal3f用于定义点的法向量//............................前门.........................................//glTexCoord2f(0.0,1.0);glVertex3f( 49.9f, 20.0f, -70.0f);//左上glTexCoord2f(1.0,1.0);glVertex3f( 49.9f, 20.0f, -45.0f);//右上glTexCoord2f(1.0,0.0);glVertex3f( 49.9f, -21.0f, -45.0f);//右下glTexCoord2f(0.0,0.0);glVertex3f( 49.9f, -21.0f, -69.9f);//左下//............................后门.....................................//glTexCoord2f(0.0,1.0);glVertex3f( 49.9f, 20.0f, 5.0f);//左上glTexCoord2f(1.0,1.0);glVertex3f( 49.9f, 20.0f, 30.0f);//右上glTexCoord2f(1.0,0.0);glVertex3f( 49.9f, -21.0f, 30.0f);//右下glTexCoord2f(0.0,0.0);glVertex3f( 49.9f, -21.0f, 5.0f);//左下glEnd();//..............................................窗户.............................................................////.....................右面墙上的窗户......................................//glBindTexture(GL_TEXTURE_2D,texture[2]);//绑定纹理glBegin(GL_QUADS);//开始绘制四边形glColor4f(1.0f,1.0f,1.0f,0.5f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glNormal3f(-1.0f, 0.0f, 0.0f);//glNormal3f用于定义点的法向量glBlendFunc(GL_SRC_ALPHA,GL_ONE);// 基于源象素alpha通道值的半透明混合函数glTexCoord2f(0.0,1.0);glVertex3f( 49.9f,29.0f,-35.0f);//左上glTexCoord2f(1.0,1.0);glVertex3f( 49.9f,29.0f,0.0f);//右上glTexCoord2f(1.0,0.0);glVertex3f( 49.9f,10.0f,0.0f);//右下glTexCoord2f(0.0,0.0);glVertex3f( 49.9f,10.0f,-35.0f);//左下glEnd();//......................左面墙上的窗户(两扇)..............................//glBegin(GL_QUADS);//开始绘制四边形glNormal3f(1.0f, 0.0f, 0.0f);//glNormal3f用于定义点的法向量glColor4f(1.0f,1.0f,1.0f,0.5f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);glTexCoord2f(0.0,1.0);glVertex3f( -49.9f, 15.0f,-60.0f);//左上glTexCoord2f(1.0,1.0);glVertex3f( -49.9f, 15.0f,-20.0f);//右上glTexCoord2f(1.0,0.0);glVertex3f( -49.9f, -10.0f,-20.0f);//右下glTexCoord2f(0.0,0.0);glVertex3f( -49.9f, -10.0f,-60.0f);//左下glTexCoord2f(0.0,1.0);glVertex3f( -49.9f, 15.0f,-10.0f);//左上glTexCoord2f(1.0,1.0);glVertex3f( -49.9f, 15.0f,30.0f);//右上glTexCoord2f(1.0,0.0);glVertex3f( -49.9f, -10.0f,30.0f);//右下glTexCoord2f(0.0,0.0);glVertex3f( -49.9f, -10.0f,-10.0f);//左下glEnable(GL_BLEND);// 打开混合glDisable(GL_DEPTH_TEST);// 关闭深度测试glEnd();//............................................灯......................................................//GLuint j=0;for(;j<2;j++) //一共两排灯{glBindTexture(GL_TEXTURE_2D,texture[4]);//绑定纹理glColor3f(1.0f,1.0f,1.0f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义材质光照glBegin(GL_QUADS);//开始绘制四边形glNormal3f(0.0f, -1.0f, 0.0f);//glNormal3f用于定义点的法向量glTexCoord2f(0.0f, 1.0f);glVertex3f( 25.0f, 29.9f, -70.0f+j*40);//左上glTexCoord2f(1.0f, 1.0f);glVertex3f( 45.0f, 29.9f, -70.0f+j*40);//右上glTexCoord2f(1.0f, 0.0f);glVertex3f( 45.0f, 29.9f, -50.0f+j*40);//右下glTexCoord2f(0.0f, 0.0f);glVertex3f( 25.0f, 29.9f, -50.0f+j*40);//左下glColor3f(1.0f,1.0f,1.0f);glNormal3f(0.0f, -1.0f, 0.0f);glTexCoord2f(0.0f, 1.0f);glVertex3f( -10.0f, 29.9f, -70.0f+j*40);//左上glTexCoord2f(1.0f, 1.0f);glVertex3f( 10.0f, 29.9f, -70.0f+j*40);//右上glTexCoord2f(1.0f, 0.0f);glVertex3f( 10.0f, 29.9f, -50.0f+j*40);//右下glTexCoord2f(0.0f, 0.0f);glVertex3f( -10.0f, 29.9f, -50.0f+j*40);//左下glColor3f(1.0f,1.0f,1.0f);glNormal3f(0.0f, -1.0f, 0.0f);glTexCoord2f(0.0f, 1.0f);glVertex3f( -45.0f, 29.9f, -70.0f+j*40);//左上glTexCoord2f(1.0f, 1.0f);glVertex3f( -25.0f, 29.9f, -70.0f+j*40);//右上glTexCoord2f(1.0f, 0.0f);glVertex3f( -25.0f, 29.9f, -50.0f+j*40);//右下glTexCoord2f(0.0f, 0.0f);glVertex3f( -45.0f, 29.9f, -50.0f+j*40);//左下glEnd();}//....................................讲台.............................................................////.....................讲台的桌子........................................//glPushMatrix();//压入堆栈glColor3f(0.5f,0.9f,0.5f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义光照材质glTranslatef(-0.0f,-10.0f,-60.5f);//平移变换glScalef(20.0f,20.0f,10.0f);//缩放变换glutSolidCube(1.0);//绘制立方体glPopMatrix();//弹出堆栈//......................地面上高起的平台.................................//glPushMatrix();//压入堆栈glColor3f(0.8f,0.8f,0.8f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义光照材质glTranslatef(0.0f,-20.0f,-70.0f);//平移变换glScalef(65.0f,2.9f,30.0f);//缩放变换glutSolidCube(1.0);//绘制立方体glPopMatrix();//弹出堆栈//........................................黑板............................................................//glBindTexture(GL_TEXTURE_2D, texture[5]); //绑定纹理glBegin(GL_QUADS); //绘制四边形glColor3f(1.0f, 1.0f, 1.0f);glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,light_Diffuse);//定义光照材质glNormal3f(0.0f, 0.0f, 1.0f);//glNormal3f用于定义点的法向量glTexCoord2f(1.0f, 1.0f);glVertex3f(-25.0f, 20.0f, -79.9f);//右上glTexCoord2f(1.0f, 0.0f);glVertex3f(-25.0f, -5.0f, -79.9f);//右下glTexCoord2f(0.0f, 0.0f);glVertex3f( 25.0f, -5.0f, -79.9f);//左下glTexCoord2f(0.0f, 1.0f);glVertex3f( 25.0f, 20.0f, -79.9f);//左上glEnd();//........................................时钟..........................................................//_strtime(timebuffer);//取系统时间到timebuffer数组中。

编程月亮绕地球的动画源码程序【matlab源码】

编程月亮绕地球的动画源码程序【matlab源码】

毕业论文(设计)题目学院学院专业学生姓名学号年级级指导教师教务处制表»ùÓÚmatlab±à³ÌÔÂÁÁÈƵØÇòµÄ¶¯»­Ô´Âë³ÌÐò一、程序说明本团队长期从事matlab编程与仿真工作,擅长各类毕业设计、数据处理、图表绘制、理论分析等,程序代做、数据分析具体信息联系二、程序示例function nightonearth% NIGHTONEARTH shows rotating earth by night% Requires images 'earth_lights_small.jpg' and% 'moon.jpg' on the pathstars=rand(100,2);[x,y,z] = sphere(48);x2=(x*0.2734)+2;y2=y*0.2734;z2=z*0.2734;f=figure('Color','k');staraxis=axes('units','normalized','position',[0,0,1,1]);scatter(stars(:,1),stars(:,2),'w.');axis offearthaxis=axes('units','normalized','position',[0,0,1,1]);hold on;s=surf(x,y,z);s2=surf(x2,y2,z2);% [X,Y]=meshgrid(0:2*pi/60:2*pi,(logspace(0,1,20)-1)./10);% [X,Y]=pol2cart(X,Y);% Z=X.^2+Y.^2;% s3=surf(X,Y,Z);shading interpaxis([-2.5,2.5,-2.5,2.5,-1.5,1.5]);daspect([1,1,1])axis off vis3dset(gca,'Position',[0,0,1,1]);topo=imread('earth_lights_small.jpg');topoud=flipdim(topo,1);set(s,'facecolor','texture');set(s,'cdata', im2double(topoud));[topomoon,map]=imread('moon.jpg');set(s2,'facecolor','texture');set(s2,'cdata', im2double(topomoon));camtarget([0,0,0]);campos([25,0,0]);u=uicontrol('style', 'togglebutton', 'value', false, 'string', 'Exit'); while ~get(u, 'value')rotate(s,[0,0,1],1,[0,0,0]);rotate(s2,[0,0,1],0.0357,[0,0,0]);% camorbit(15,0)pause(0.1)endclose(f)。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
相关文档
最新文档