电子科大-计算机学院-三维图形程序设计实验报告

时间:2020-10-16 08:58:33 手机站 来源:网友投稿

课程名称:三维图形程序设计

学 院:计算机科学与工程学院

专 业:计算机科学与技术

指导教师:周川

学生姓名:郫县卡戴珊

学 号:20

实验成绩:

期:2016年3月31日

电子科技大学计算机学院实验中心

电子科技大学

实验报告

一、实验一: 三维图形程序上机操作训练与图元编程

二、实验室名称: 主楼 A2412 实验时间: 2016年 3月 31号

三、实验目的:

掌握VC中OpenG三维编程环境的建立过程;

掌握GLUT编写OpenGl应用程序的基本步骤,熟悉 GLUT基于事件驱动的编程模型;

掌握不同类型图元的顶点装配方式,并能够利用图元建立简单的几何对象。

四、实验原理:

Ope nGl编程语言

面向对象编程技术

五、实验内容:

(一)安装、配置和测试 Ope nGL三维编程环境

首先检查 Windows操作系统中是否正确安装了 Visual Studio集成开发环境:实验 要求在 Windows XP、 Visual C++ 2005 平台以上;

解压到任意目录下;

检查OpenG核心库和实用库及其头文件是否安装正确和完整:检

查%VSINSTALLDIR%\VC\PlatformSDK\l nclude'GL目录下是否存在、两个头文件,并从解压 得到的GL子目录下拷贝缺少的头文件到该目录下;检

查%VSINSTALLDIR%\VC\PlatformSDK\Lib目录下是否存在、两个库文件,并从解压得到的Lib 子目录下拷贝缺少的 Lib 文件到该目录下;

安装GLUT库及其头文件。从解压得到的 GL子目录下拷贝

到%VSINSTALLDIR%\VC\PlatformSDK\Include'GL目录中,从解压得到的 Lib子目录下拷贝

到%VSINSTALLDIR%\VC\PlatformSDK\LibT,拷贝到 %windir%\system32 目录下。

检测OpenG环境是否配置正确。

打开Visual Studio 集成开发环境,新建一个类型为“ Win32控制台应用程序” 的空项目Test;

在项目中新建一个C源文件;

拷贝如下代码到中;

生成Test项目,观察“输出”窗口中的编译和链接输出信息,若显示类似“生 成:1已成功,0已失败,0最新,0已跳过”表明项目生成成功,否则根据输出信息检查 程序或者OpenG编程环境是否设置正确,并在改正后重新生成 Test项目;

运行查看程序是否能够正常启动。

/** **/

#i nclude <GL/>

void display(); void ini t();

int main (i nt argc, char ** argv) {

glutl nit(&argc, argv);

glutCreateWi ndow( “ Simple ”);

glutDisplay Fun c(display);

ini t();

glutMa in Loop(); } void display()

{

glClear(GL_COLOR_BUFFER_BIT);

glBegi n(GL_POLYGON); glVertex2f,; glVertex2f,; glVertex2f,; glVertex2f,;

编程任务1:

1.分别利用点、线图元生成锥形螺旋曲线和环形螺旋曲线,要求可以设置点的大小、

线可以设置线形和宽度。

锥形螺旋曲线的参数方程为:

x a * t * cos(c * t ) b

y a * t * sin( c *t ) b

z c * t

环形螺旋曲线的参数方程为:

x (a*sin(c*t) b)* cos(t )

y (a * sin(c * t) b)* sin(t )

z a* cos(c* t)

其中,t为参数且t [0,2 ]。

 a, b和c是常数,比如可取a=, b=, c=。

编程任务 2:

1. 在窗口中绘制三角形和四边形两个简单填充图形, 要求它们能够被鼠标选中并随鼠标 一起拖动,此外能够使用右键菜单分别设置两个简单图形的颜色。

利用三角形图元生成黑白相间的圆锥台,要求通过键盘或鼠标能够改变圆锥台地大端和 小端直径。

六、实验器材(设备、元器件) :

机房电脑、Microsoft Visual Studio 、OpenGLAPI函数库

七、实验步骤:

1:配置环境

首先需要配置 OpenGL编程环境,在 Microsoft Visual Studio 2010 中添加相应

的头文件以及 lib 文件》

头文件添加地址为 C:\Program Files\Microsoft Visual Studio \VC\include\GL lib 文件添加地址 C:\Program Files\Microsoft Visual Studio \VC\lib 并且将 添加到 的文件夹里

2:建立工程

建立OpenGLX程的过程

选择空项目;

输入名称;

选择添加文件; 键入代码;

3:根据具体的题目要求进行实验

八、实验数据及结果分析:

编程任务 1:

分别利用点、线图元生成锥形螺旋曲线和环形螺旋曲线,要求可以设置点的大小、线

可以设置线形和宽度。

锥形螺旋曲线的参数方程为:

x a * t * cos( c * t ) b y a * t * sin( c * t ) b z c * t

环形螺旋曲线的参数方程为:

x (a* sin(c*t) b) * cos(t) y (a* sin(c*t) b)*sin(t) z a* cos(c* t)

其中,t为参数且t [0,2 ]。a,b和c是常数,比如可取a=,b=, c=

/** **/

#i nclude <GL/>

#in clude <>

#define PI void drawthelines()

{

float t, x, y, z;

float a=2, b=3, c=18;

glColor3f,,;

glBegi n(GL_LINE_STRIP);

for (t=0; t<=2*PI; t+= {

x = a*t*cos(c*t)+b; y = a*t*s in (c*t)+b;

z = c*t;

glVertex3f(x, y, z);

}

glEnd();

glColor3f, , ;

glBegin(GL_LINES); glVertex3f(0, 0, 0); glVertex3f(12, 0, 0);

glEnd();

glColor3f, , ;

glBegin(GL_LINES);

glVertex3f(0, 0, 0); glVertex3f(0, 12, 0);

glEnd();

glColor3f, , ;

glBegin(GL_LINES);

glVertex3f(0, 0, 0); glVertex3f(0, 0, 12);

glEnd();

}

void display()

{

glClear(GL_COLOR_BUFFER_BIT); drawthelines();

glFlush();

}

void init()

{

glClearColor, , ,;

glColor3f, , ;

gluLookAt(1, 1, 1,

0, 0, 0,

0, 1, 0);

glMatrixMode(GL_PROJECTION); glLoadIdentity();

glOrtho, , , , -12, 12);

}

int main( int argc, char ** argv)

{

glutl nit (&argc, argv);

glut In itDisplayMode(GLUT_SINGLE|GLUT_RGB);

glutl nitWin dowPositio n(O,O);

glutCreateWindow( "Draw the lines" ); glutDisplayFu nc(display);

in it();

glutMai nLoop();

return 0;

}

实验一(2)

/** **/

#i nclude <GL/>

#in clude <>

#define PI void drawthelines()

{

float t, x, y, z;

float a=2, b=3, c=18;

glColor3f,,;

glBegi n(GL_LINE_STRIP); for (t=0; t<=2*PI; t+= { x = (a*sin(c*t)+b)*cos(t); y = (a*sin(c*t)+b)*sin(t); z = a*cos(c*t); glVertex3f(x, y, z);

}

glEnd();

glColor3f, , ; glBegin(GL_LINES);

glVertex3f(0, 0, 0); glVertex3f(12, 0, 0);

glEnd();

glColor3f, , ; glBegin(GL_LINES);

glVertex3f(0, 0, 0); glVertex3f(0, 12, 0);

glEnd();

glColor3f, , ; glBegin(GL_LINES);

glVertex3f(0, 0, 0); glVertex3f(0, 0, 12);

glEnd();

}

void display()

{ glClear(GL_COLOR_BUFFER_BIT); drawthelines();

glFlush();

}

void init()

{

glClearColor, , ,; glColor3f, , ;

gluLookAt(1, 1, 1,

0, 0, 0,

0, 1, 0);

glMatrixMode(GL_PROJECTION); glLoadIdentity();

glOrtho, , , , -12, 12);

}

int main( int argc, char ** argv)

{ glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowPosition(0,0); glutCreateWindow( "Draw the lines" );

glutDisplayFunc(display); init();

glutMainLoop();

return 0;

}

编程任务 2:

1. 在窗口中绘制三角形和四边形两个简单填充图形, 要求它们能够被鼠标选中并随鼠标 一起拖动,此外能够使用右键菜单分别设置两个简单图形的颜色。

#include <>

#include <GL/>

#define SIZE 512 int wide=600,height=600;

GLint HITS;

void init()

{

glClearColor , , , ;

}

int Rect1_x1=-250,Rect1_y1=-250,Rect1_x2=450,Rect1_y2=450;

int Rect2_x1=-500,Rect2_y1=-500,Rect2_x2=250,Rect2_y2=250;

int movex1=0,movey1=0;

int movex2=0,movey2=0;

int mousex=0,mousey=0;

int RGB1[3]={,,};

int RGB2[3]={,,};

void drawObjects(GLenum mode, int flag)

}

}

if (mode == GL_SELECT) glLoadName(1); glColor3f(RGB1[0],RGB1[1],RGB1[2]); glTranslated(4*movex1,4*movey1,0); glRectf(Rect1_x1,Rect1_y1 ,Rect1_x2 ,Rect1_y2 ); glTranslated(-4*movex1,-4*movey1,0);

if (mode == GL_SELECT) glLoadName(2); glColor3f(RGB2[0],RGB2[1],RGB2[2]); glTranslated(4*movex2,4*movey2,0); glBegin(GL_TRIANGLES);

glVertex2i(0,0);

glVertex2i(145,245);

glVertex2i(-45,280);

glEnd();

glTranslated(-4*movex2,-4*movey2,0);

}

void display( int flag)

{

glClear(GL_COLOR_BUFFER_BIT); drawObjects(GL_RENDER,flag);

glFlush();

}

void processHits (GLint hits, GLuint buffer[])

{

unsigned int i, j;

GLint ii, jj, names, *ptr;

printf ( "hits = %d\n" , hits);

ptr = (GLint *) buffer;

for (i = 0; i < hits; i++) {

names = *ptr;

ptr+=3;

for (j = 0; j < names; j++) {

if (*ptr==1)

printf ( "red rectangle\n" );

else

printf ( "blue rectangle\n" );

ptr++;

}

printf ( "\n" );

}

GLuint selectBuf[SIZE];

void mouse( int button, int state, int x, int y)

推荐访问:实验报告 程序设计 图形 实验 电子科大-计算机学院-三维图形程序设计实验报告

版权声明 :以上文章中选用的图片文字均来源于网络或用户投稿 ,如果有侵权请立即联系我们 , 我们立即删除 。