1、opengl实验报告贵州大学实验报告学院:计算计科学与信息学院 专业: 班级: 姓名 学号 实验组实验时间指导教师 成绩实验项目名称基本图元生成实验目的1、掌握opengl头文件的使用和语法规则2、opengl程序的基本结构和程序运行环境配置3、掌握使用opengl函数绘制基本图元,如点、线、封闭的多边形等。实验要求 学会如何在vc+6.0中配置opengl的运行环境实验原理在visual c+编程环境中编写程序源代码,并编译、运行程序结果实验仪器奔腾2计算机或以上机型、visual c+编程环境实验 内容1OpenGL环境设置v将OpenGL所需函数库和头文件复制到指定目录. v将压缩包内的
2、glut.h放到.Microsoft Visual StudioVC98IncludeGL目录下v 将glut32.lib放到.Microsoft Visual StudioVC98Lib目录下v 将glut32.dll放到X:windowssystom32目录下(win98用户放到X:windowssystom目录下2.通过vc+编译运行画出点、直线、几何图形等。 实验步骤v1.找到vc+的安装路径.Microsoft Visual StudioVC98IncludeGL目录,然后将压缩包里的glut.h文件复制一份到该目录下。v2.找到vc+的安装路径.Microsoft Visual S
3、tudioVC98Lib目录下,然后将glut32.lib放到该目录下。v3.找到路径windowssystom32目录,然后将glut32.dll文件复制一份到该目录下。v4. OpenGL环境设置好后就可以开始编写程序了,接下来就是打开vc+编辑器,在编辑器里输入如下代码: #include#include#include#includevoid myinit(void);void display(void);void init(void) glClearColor(0.0,0.0,0.0,1.0);void display(void) glClear(GL_COLOR_BUFFER_BI
4、T); glColor4f(0.5,0.2,1.0,0.8); glPointSize(5.0); glBegin(GL_POINTS); glVertex3f(0.1,0.5,0.0); glVertex3f(0.4,0.7,0.0); glVertex3f(0.6,0.2,0.0); glEnd(); glFlush();void main(int argc,char* argv) glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowPosition(50,100); glutInit
5、WindowSize(800,600); glutCreateWindow(the first openglsample); init(); glutDisplayFunc(display); glutMainLoop();3.单击“启动调试”按钮后可在屏幕上看到生成的点的图案 4.接着在void display(void)函数里面依次输入下面代码编译后可看到相应的图案5. glBegin(GL_LINES); glVertex3f(0.1,0.2,0.0); glVertex3f(0.5,0.9,0.0); glEnd();6glBegin(GL_POLYGON); glVertex3f(0
6、.1,0.2,0.0); glVertex3f(0.2,0.7,0.0); glVertex3f(0.5,0.8,0.0); glVertex3f(0.3,0.6,0.5); glEnd();7. glBegin(GL_LINE_STRIP); glVertex3f(0.1,0.2,0.0); glVertex3f(0.2,0.7,0.0); glVertex3f(0.5,0.8,0.0); glVertex3f(0.3,0.6,0.5);glEnd();8. glBegin(GL_LINE_LOOP); glVertex3f(0.1,0.2,0.0); glVertex3f(0.2,0.7,
7、0.0); glVertex3f(0.5,0.8,0.0); glVertex3f(0.3,0.6,0.5);glEnd();9. glBegin(GL_QUADS); glVertex3f(0.1,0.1,0.0); glVertex3f(0.2,0.4,0.0); glVertex3f(0.5,0.6,0.0); glVertex3f(0.7,0.2,0.0);glEnd(); glBegin(GL_TRIANGLE_FAN); glVertex2f(0.5,0.5); glVertex2f(0.5,1.0); glVertex2f(0.8,0.9); glVertex2f(0.9,0.8
8、); glVertex2f(1.0,0.5); glEnd(); 10.运行后,在屏幕上就可以出现相应的各种图案,如实验结果所示。实验结果实验总结 这次试验的主要任务是学会配置opengl的运行环境,并通过编写程序来测试能否正确的生成相应的图案,所以还算比较简单的,做实验时只要看一下老师发给我们的ppt,然后按照上面的步骤一步一步的操作就行,先是把下载好的压缩包解压然后找到安装目录,然后将相应的文件放到相应的目录下。配置好后就按照相应的格式在编辑器里输入代码,当要显示不同的图形时就写入相应的函数,设置相应的点。最后运行就可以了。当然这些都还是简单的,规则的图形,所以只要代码没有问题就好了。在设
9、置点的时候会出现一个问题,那就是设置的点的值不当时就有可能无法显示在窗口中,也就是说在窗口外了,这是就学要重新设置点的坐标。指导教师意见签名: 年 月 日贵州大学实验报告学院:计算计科学与信息学院 专业:数字媒体技术 班级:数媒091姓名杨涵松学号0908060209实验组实验时间指导教师陈静 成绩实验项目名称 opengl线属性函数和位图与图像实验目的 1、掌握并会使用opengl线属性函数glLineWidth(width)、glLineStripple(repeatFactor,pattern)、glShadeModel()。2、理解位图与图像的相同与不同之处,并会使用opengl函数生
10、成位图与图象。3、掌握使用glBitmap()、glRasterPos*()、glReadPixels()、glDrawPixels()、glCopyPixels()、glPixelZoom()等函数的使用。实验要求 学会设置各种属性实验原理在visual c+编程环境中编写程序源代码,并编译、运行程序结果实验仪器奔腾2计算机或以上机型、visual c+编程环境实验 内容 1、在第一次实验的基础上使用opengl线属性函数设置绘制的线的各种属性,如线宽、线效果、线颜色等属性。请将程序缺少的部分填写完整。#include #include #include #include void init
11、(void) /初始化 glClearColor(0.0,0.0,0.0,0.0); /将窗口清为黑色void display(void) glClear(GL_COLOR_BUFFER_BIT); /将颜色缓存清为glClearColor命令所设置的颜色,即背景色 glFlush(); /强制绘图,不驻留缓存void main(int argc,char* argv) glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowPosition(50,100); glutInitWindowSi
12、ze(1000,1000); glutCreateWindow(the first openglsample); init(); glutDisplayFunc(display); glutMainLoop();2、使用合适的函数,绘制位图字符,结合所绘制的位图图形理解其中每个函数的含义及参数的含义。请将程序缺少的部分填写完整。#include #include #include #include void myinit(void);void display(void);GLubyte rasters12 = ;void init(void) /初始化 glPixelStorei (GL_UN
13、PACK_ALIGNMENT, 1); glClearColor(0.0,0.0,0.0,0.0); /将窗口清为黑色void display(void) glClear(GL_COLOR_BUFFER_BIT); /将颜色缓存清为glClearColor命令所设置的颜色,即背景色 glFlush(); /强制绘图,不驻留缓存void main(int argc,char* argv) glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowPosition(.); glutInitWindo
14、wSize(.); glutCreateWindow(.); init(); glutDisplayFunc(display); glutMainLoop();3、使用合适的函数,绘制图像,结合所绘制的图象理解其中每个函数的含义及参数的含义。请将程序缺少的部分填写完整。#include #include #include #include void myinit(void);void display(void);void init(void) /初始化 glClearColor(0.0,0.0,0.0,0.0); /将窗口清为黑色void triangle() void SourceImage
15、() void display(void) glClear(GL_COLOR_BUFFER_BIT); /将颜色缓存清为glClearColor命令所设置的颜色,即背景色 glFlush(); /强制绘图,不驻留缓存void main(int argc,char* argv) glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowPosition(); glutInitWindowSize(); glutCreateWindow(); init(); glutDisplayFunc(disp
16、lay); glutMainLoop(); 实验步骤打开vc+编辑器,在编辑器里输入如下代码:/线宽线效果 #include#include#include#includevoid myinit(void);void display(void);void init(void) glClearColor(0.0,0.0,0.0,1.0);void display(void) glClear(GL_COLOR_BUFFER_BIT); glColor4f(0.5,0.2,1.0,0.8); glPointSize(5.0); glBegin(GL_LINES);glVertex3f(0.1,0.2
17、,0.0); glVertex3f(0.5,0.9,0.0); glEnd(); glFlush(); GLfloat y; / Storeage for varying Y coordinate GLint factor = 1; / Stippling factor GLushort pattern = 0x5555; / Stipple pattern / Clear the window with current clearing color glClear(GL_COLOR_BUFFER_BIT); / Save matrix state and do the rotation gl
18、PushMatrix(); glRotatef(xRot, 1.0f, 0.0f, 0.0f); glRotatef(yRot, 0.0f, 1.0f, 0.0f); / Enable Stippling glEnable(GL_LINE_STIPPLE); / Step up Y axis 20 units at a time for(y = -90.0f; y 90.0f; y += 20.0f) / Reset the repeat factor and pattern glLineStipple(1, 0xFF00);/线型 / Draw the line glBegin(GL_LIN
19、ES); glVertex2f(-80.0f, y); glVertex2f(80.0f, y); glEnd(); factor+; / Restore transformations glPopMatrix(); / Flush drawing commands glutSwapBuffers(); / This function does any needed initialization onGLfloat nRange = 100.0f; / Prevent a divide by zero if(h = 0) h = 1; / Set Viewport to window dime
20、nsions glViewport(0, 0, w, h); / Reset coordinate system glMatrixMode(GL_PROJECTION); glLoadIdentity(); / Establish clipping volume (left, right, bottom, top, near, far) if (w = h) glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange); else glOrtho (-nRange*w/h, nRange*w/h, -nRange, nR
21、ange, -nRange, nRange); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); int main(int argc, char* argv) glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutCreateWindow(Stippled Line Example); glutReshapeFunc(ChangeSize); glutSpecialFunc(SpecialKeys); glutDisplayFunc(Re
22、nderScene); SetupRC(); glutMainLoop(); return 0; /位图字符#include /位图图形#include #include #include #include void myinit(void);void display(void);void init(void) /初始化 glClearColor(0.0,0.0,0.0,0.0); /将窗口清为黑色void triangle() glBegin(GL_TRIANGLES); glColor3f(0.0,1.0,0.0); glVertex2f(0.1,0.2); glColor3f(0.0,0
23、.0,1.0); glVertex2f(0.2,0.7); glColor3f(1.0,0.0,0.0); glVertex2f(0.5,0.8); glEnd();void SourceImage() glPushMatrix(); glLoadIdentity(); glTranslatef(0.5,0.5,0.0); glScalef(0.5,0.5,0.5); triangle (); glPopMatrix(); void main(int argc,char* argv) glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|G
24、LUT_RGB); glutInitWindowPosition(50,100); glutInitWindowSize(800,600); glutCreateWindow(the first openglsample); glShadeModel(GL_SMOOTH);/线效果 glLineWidth(10);/线宽 init(); glutDisplayFunc(display); glutMainLoop();/绘制虚线#include /#include /#include #include #include / Define a constant for the value of
25、PI#define GL_PI 3.1415f/ Rotation amountsstatic GLfloat xRot = 0.0f;static GLfloat yRot = 0.0f;/ Called to draw scenevoid RenderScene(void) the rendering/ context. void SetupRC() / Black background glClearColor(0.0f, 0.0f, 0.0f, 1.0f ); / Set drawing color to green glColor3f(0.0f, 1.0f, 0.0f); void
26、SpecialKeys(int key, int x, int y) if(key = GLUT_KEY_UP) xRot-= 5.0f; if(key = GLUT_KEY_DOWN) xRot += 5.0f; if(key = GLUT_KEY_LEFT) yRot -= 5.0f; if(key = GLUT_KEY_RIGHT) yRot += 5.0f; if(key 356.0f) xRot = 0.0f; if(key 356.0f) yRot = 0.0f; if(key -1.0f) yRot = 355.0f; / Refresh the Window glutPostR
27、edisplay(); void ChangeSize(int w, int h) #include #include #include void myinit(void);void display(void);GLubyte rasters12 = 0xff,0xff,0xc0,0xc0,0xc0,0xfc,0xfc,0xc0,0xc0,0xc0,0xff,0xff;void init(void) /初始化 glPixelStorei (GL_UNPACK_ALIGNMENT, 1); glClearColor(0.0,0.0,0.0,0.0); /将窗口清为黑色void display(v
28、oid) glClear(GL_COLOR_BUFFER_BIT); /将颜色缓存清为glClearColor命令所设置的颜色,即背景色 glColor3f (1.0, 0.0, 1.0); glRasterPos2i (0.5,0.8); glBitmap (8,12,0.0,0.0,20.0,20.0,rasters); glBitmap (8,12,0.0,40.0,10.0,0.0,rasters); glFlush(); /强制绘图,不驻留缓存void main(int argc,char* argv) glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowPosition
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1