当前位置: 首页 > >

OpenGL 中真实感图形的显示实现

发布时间:

实验名称

OpenGL 中真实感图形的显示实现

一、实验目的 学*基于 OpenGL 真实感图形显示的原理与实现方法。

二、实验内容
利用向导生成应用程序框架,参考示例编写实现三维图形的应用程序,增加光照 与材质效果实现真实感图形的显示。

三、实验步骤
1.利用向导生成应用程序框架。 2.添加相应的函数,绘制立方体(参照上课给的实例) 。 3.添加相应的函数,实现三维球体的绘制。 4.增加光源,并设置光源参数与材质参数,观察显示效果。 实验代码及程序运行结果截图如下:
// 2008cube.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "gl/glut.h" #include "math.h" void myinit() { glClearColor( 1.0, 1.0, 1.0, 0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-1.5, 1.5, -1.5, 1.5,-1.5,1.5); } void draw_cube() { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0,0.0,0.0); glBegin(GL_POLYGON); glVertex3f(0.0,0.0,0.0); glVertex3f(0.0,1.0,0.0); glVertex3f(1.0,1.0,0.0); glVertex3f(1.0,0.0,0.0); glEnd(); glColor3f(0.0,1.0,0.0);

glBegin(GL_POLYGON); glVertex3f(0.0,0.0,0.0); glVertex3f(0.0,1.0,0.0); glVertex3f(0.0,1.0,1.0); glVertex3f(0.0,0.0,1.0); glEnd(); glColor3f(0.0,0.0,1.0); glBegin(GL_POLYGON); glVertex3f(0.0,0.0,1.0); glVertex3f(0.0,1.0,1.0); glVertex3f(1.0,1.0,1.0); glVertex3f(1.0,0.0,1.0); glEnd(); glColor3f(1.0,1.0,0.0); glBegin(GL_POLYGON); glVertex3f(1.0,0.0,0.0); glVertex3f(1.0,1.0,0.0); glVertex3f(1.0,1.0,1.0); glVertex3f(1.0,0.0,1.0); glEnd(); glColor3f(1.0,0.0,1.0); glBegin(GL_POLYGON); glVertex3f(0.0,1.0,0.0); glVertex3f(1.0,1.0,0.0); glVertex3f(1.0,1.0,1.0); glVertex3f(0.0,1.0,1.0); glEnd(); } void display() { glClear( GL_COLOR_BUFFER_BIT); glColor3f(1.0,0.0,0.0); gluLookAt(0.5,0.5,0.0,1.0,1.0,1.0,1.0,0.0,1.0); glutWireCube(0.5); draw_cube(); glFlush(); } void main(int argc, char* argv[]) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(300,300); glutCreateWindow("立方体的绘制");

glutDisplayFunc(display); myinit(); glutMainLoop(); }

// 2008sphere.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "gl/glut.h" #include "math.h" #define M_PI 3.1415926 void draw_sphere() { double c=M_PI/180.0; float phi,phir,theta,thetar,x,y,z,phir20; glColor3f(1.0,1.0,1.0); for(phi=-10.0;phi<100.0;phi+=4.0) { phir=c*phi; phir20=c*(phi+20); glBegin(GL_QUAD_STRIP); for(theta=-200.0;theta<=200.0;theta+=1.0) { thetar=c*theta; x=sin(thetar)*cos(phir); y=cos(thetar)*cos(phir); z=sin(phir); glVertex3d(x,y,z); x=sin(thetar)*cos(phir20);

y=cos(thetar)*cos(phir20); z=sin(phir20); glVertex3d(x,y,z); } glEnd(); } glBegin(GL_TRIANGLE_FAN); glVertex3d(0.0,0.0,0.0); c=M_PI/180; double c80=c*80;z=sin(c80); for(theta=-200;theta<=200;theta+=1.0) { thetar=c*theta; x=sin(thetar)*cos(c80); y=cos(thetar)*cos(c80); glVertex3d(x,y,z); } glEnd(); glBegin(GL_TRIANGLE_FAN); glVertex3d(0.0,0.0,-1.0); z=-sin(c80); for(theta=-200;theta<=200;theta+=1.0) { thetar=c*theta; x=sin(thetar)*cos(c80); y=cos(thetar)*cos(c80); glVertex3d(x,y,z); } glEnd(); } void display() { glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); GLfloat ball_specular[]={1.0, 1.0, 1.0, 1.0}; GLfloat ball_diffuse[]={1.0, 1.0, 1.0, 8.0}; GLfloat ball_ambient[]={1.0, 1.0, 1.0, 2.0}; GLfloat ball_shininess={100.0}; GLfloat light_ambient[]={0.0, 0.0, 0.0, 1.0}; GLfloat light_diffuse[]={1.0, 1.0, 1.0, 1.0}; GLfloat light_specular[]={2.0, 1.0, 1.0, 0.0};

GLfloat light_position[]={2.0, 2.0, 2.5, 2.5}; glLightfv(GL_LIGHT0, GL_POSITION, light_position); glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); glMaterialfv(GL_FRONT, GL_SPECULAR, ball_specular); glMaterialfv(GL_FRONT, GL_AMBIENT, ball_ambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, ball_diffuse); glMaterialf(GL_FRONT, GL_SHININESS,ball_shininess); glShadeModel(GL_SMOOTH); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST); glClearColor (0.0, 0.0, 0.0, 1.0); glColor3f (1.0, 1.0, 1.0); glEnable(GL_NORMALIZE); glEnable(GL_CULL_FACE); glCullFace(GL_BACK); draw_sphere(); glEnd(); glFlush(); } void init() { glClearColor( 0.0, 0.0,0.0, 0.0); glColor3f(0.0,0.0,0.0); glOrtho(-5.0, 5.0, -5.0, 5.0,-5.0,5.0); } void reshape(int w,int h) { glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-1.5,1.5,-1.5,1.5,-1.5,1.5); } int main(int argc, char* argv[]) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH); glEnable(GL_DEPTH_TEST); glutInitWindowSize(400,400); glutInitWindowPosition(0,0); glutCreateWindow("三维球体");

glutReshapeFunc(reshape); glutDisplayFunc(display); init(); glutMainLoop(); return 0; }




友情链接: 时尚网 总结汇报 幼儿教育 小学教育 初中学习资料网