图形学第五章作业讲评

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

5.27试用OpenGL程序绘制实线、虚线和点划线。#includestdafx.h//stdafx.h中没有函数库,只是定义了一些环境参数,使得编译出来的//程序能在32位的操作系统环境下运行。#includegl/glut.hvoidInit(){glClearColor(0.0,0.0,0.0,1.0);}voiddisplay(){//glClearColor(0.0,0.0,0.0,1.0);glClear(GL_COLOR_BUFFER_BIT);//绘制实线glColor3f(1.0,0.0,0.0);/*注意:glColor系列函数,在参数类型不同时,表示“最大”颜色的值也不同。采用f和d做后缀的函数,以1.0表示最大的使用。采用b做后缀的函数,以127表示最大的使用。采用ub做后缀的函数,以255表示最大的使用。采用s做后缀的函数,以32767表示最大的使用。采用us做后缀的函数,以65535表示最大的使用。*/glLineWidth(5);glBegin(GL_LINES);glVertex2f(0,200);glVertex2f(300,200);glEnd();//绘制虚线glColor3f(1.0,0.0,1.0);//紫色glLineWidth(5);glEnable(GL_LINE_STIPPLE);//打开画线模式glLineStipple(4,0x00FF);glBegin(GL_LINES);glVertex2f(0,250);glVertex2f(300,250);glEnd();glDisable(GL_LINE_STIPPLE);//绘制点划线glColor3f(1.0,1.0,0.0);//黄色glLineWidth(5);glEnable(GL_LINE_STIPPLE);glLineStipple(2,0xFF0A);glBegin(GL_LINES);glVertex2f(0,300);glVertex2f(300,300);glEnd();glDisable(GL_LINE_STIPPLE);glFlush();}voidchangesize(GLsizeiw,GLsizeih){glViewport(100,100,w,h);//设置视区尺寸if(h==0)h=1;glMatrixMode(GL_PROJECTION);glLoadIdentity();if(w=h)glOrtho(0.0f,500.0f,0.0f,500.0f*h/w,1.0,-1.0);//gluOrtho2D(0.0f,500.0f,0.0f,500.0f*h/w);elseglOrtho(0.0f,500.0f*w/h,0.0f,500.0f,1.0,-1.0);}intmain(intargc,char*argv[]){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowPosition(100,100);glutInitWindowSize(600,600);glutCreateWindow(绘制不同的直线);glutDisplayFunc(display);glutReshapeFunc(changesize);Init();glutMainLoop();return0;}5.28试用OpenGL实现黑白相间的棋盘图案填充多边形。#includestdafx.h#includegl/glut.hvoiddisplay(){glClear(GL_COLOR_BUFFER_BIT);glColor3f(1.0,1.0,1.0);GLubytechess[]={//定义128字节大小的数组0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF};//未填充的多边形/*glBegin(GL_POLYGON);glVertex2f(100,100);glVertex2f(200,100);glVertex2f(200,400);glVertex2f(100,400);glEnd();*///填充的多边形glEnable(GL_POLYGON_STIPPLE);//启用多边形填充模式glPolygonStipple(chess);//指定chess数组定义的位图为填充模板glBegin(GL_POLYGON);glVertex2f(200,100);glVertex2f(500,100);glVertex2f(500,500);glVertex2f(200,500);glEnd();glDisable(GL_POLYGON_STIPPLE);glFlush();}voidchangesize(intw,inth){glViewport(0,0,w,h);if(h==0)h=1;glMatrixMode(GL_PROJECTION);glLoadIdentity();if(w=h)glOrtho(0,600,0,600*h/w,1,-1);elseglOrtho(0,600*w/h,0,600,1,-1);}intmain(intargc,char*argv[]){glutInit(&argc,argv);glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);glutInitWindowSize(600,600);glutInitWindowPosition(100,100);glutCreateWindow(填充多边形);glutDisplayFunc(display);glutReshapeFunc(changesize);glutMainLoop();return0;}5.29利用OpenGL,分别用点和折线模式实现正弦和余弦曲线的绘制。#includestdafx.h#includegl/glut.h#includemath.h#includeiostream.hfloatk=0.05;intmode=1;voiddisplay(){glClearColor(0.0,0.0,0.0,1.0);glClear(GL_COLOR_BUFFER_BIT);//画出坐标轴glColor3f(1.0,1.0,1.0);glBegin(GL_LINES);//OpenGL默认坐标值只能从-1到1glVertex2f(-1,0);glVertex2f(1,0);glVertex2f(0,1);glVertex2f(0,-1);glEnd();if(mode==1)//画线{cout现在是线模式,要想转换到点模式请按下p键!endl;//画正弦曲线glColor3f(1.0,0.0,0.0);glBegin(GL_LINE_STRIP);/*for(floata=-1;a=1;a=a+0.01){glVertex2f(a,sin(a));}*/for(floati=-1/k;i=1/k;i=i+0.01)//扩大20倍{glVertex2f(i*k,sin(i)*0.3);}glEnd();//画余弦曲线glColor3f(0.0,1.0,0.0);glBegin(GL_LINE_STRIP);for(floatj=-1/k;j=1/k;j=j+0.01){glVertex2f(j*k,cos(j)*0.3);}glEnd();}if(mode==2)//画点{cout现在是点模式,要想转换到线模式请按下l键!endl;glColor3f(1.0,1.0,0.0);glBegin(GL_POINTS);for(floatm=-1/k;m=1/k;m=m+0.05){glVertex2f(m*k,sin(m)*0.3);}glEnd();glColor3f(0.5,0.5,1.0);glBegin(GL_POINTS);for(floatn=-1/k;n=1/k;n=n+0.05){glVertex2f(n*k,cos(n)*0.3);}glEnd();}glFlush();}voidKey(unsignedcharkey,intx,inty)//实现键盘响应函数{switch(key){case'p':mode=2;break;case'l':mode=1;break;default:break;}glutPostRedisplay();}intmain(intargc,char*argv[]){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowSize(600,600);glutInitWindowPosition(100,100);glutCreateWindow(正弦和余弦);glutDisplayFunc(display);glutKeyboardFunc(Key);glutMainLoop();return0;}5.30利用OpenGL在屏幕上输出OpenGL字样.#includestdafx.h#includegl/glut.h#includestring.h/*voidoutput(intx,inty,char*string){intlen,i;glRasterPos2f(x,y);//字符显示的位置len=(int)strlen(string);for(i=0;ilen;i++){glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,string[i]);//字符的字体、大小和内容}}*/voiddisplay(){glClearColor(0.0,0.0,0.0,1.0);glClear(GL_COLOR_BUFFER_BIT);//output(100,150,OpenGL!);//显示位图字符(指定使用固定宽度的字体)glColor3f(1.0,0,0.7);glRasterPos2f(100,150);//字符显示的位置glutBitmapCharacter(GLUT_BITMAP_9_BY_15,'O');glutBitmapCharacter(GLUT_BITMAP_9_BY_15,112);glutBitmapCharacter(GLUT_BITMAP_9_BY_15,101);glutBitmapCharacter(GLUT_BITMA

1 / 8
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功