实验二-交互与动画_第1页
实验二-交互与动画_第2页
实验二-交互与动画_第3页
实验二-交互与动画_第4页
实验二-交互与动画_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实验二 交互与动画姓名 叶传军 学号 E11414103 得分 【实验目的】1.掌握基本交互式程序的编程方法。2.掌握基本动画程序的编程方法。一.实验题目1.阅读squareMouse.c, 回答下面的问题:(1)glFlush()函数和glClear(GL_COLOR_BUFFER_BIT)函数的作用分别是什么?(可将这两个函数注释掉,和注释前的结果对比)(2)修改squareMouse.c,分别实现如下功能:a. 通过利用移动回调函数可以在不释放鼠标按钮的情况下,连续画一系列正方形;b. 应用被动移动回调函数,可以不用按鼠标按钮就可以连续画正方形;c. 按下Alt+c或Alt+C时,终止程

2、序。2. 编写一个程序,实现如下的功能:连续两次单击鼠标左键,以两次单击的位置作为矩形的对角线来绘制一个矩形,且该矩形各边与屏幕对齐。鼠标右键用于程序的退出。(1)将绘制矩形的函数放在鼠标回调函数中完成。(2)修改(1)中的程序,将绘制矩形的函数放在显示回调函数中完成。鼠标回调函数用于状态的修改,并调用显示回调函数(利用glutPostRedisplay())。3.将正方形旋转的程序squareRotate.c改成正六边形旋转的程序。4.创建一个绘图程序,使得可用鼠标来创建一些简单的形状,如线段,三角形,矩形,并可通过菜单来实现下列功能。要求:(1)可改变形状的颜色。(2)可改变形状的大小。(

3、3)可移动形状。(4)可旋转形状。(5)你能想到的任何功能。二.设计思想本次实验主要通过改变几个主要回调函数来实现一些图形的绘制,主要使用的回调函数有glutMouseFunc();鼠标回调函数glutMotionFunc();鼠标移动回调函数glutPassiveMotionFunc();被动移动回调函数glutKeyboardFunc();键盘回调函数void display();显示回调函数其中应注意每个函数参数的含义以及glutPostRedisplay()函数与display()函数的使用。三.程序清单1.阅读squareMouse.c, 回答下面的问题:(1)glFlush()函数

4、和glClear(GL_COLOR_BUFFER_BIT)函数的作用分别是什么?(可将这两个函数注释掉,和注释前的结果对比)glFlush()函数的作用是清空OPENGL命令缓冲区,执行OPENGL程序。GlClear(GL_COLOR_BUFFER_BIT)函数的作用是用当前背景色填充窗口。(2)修改squareMouse.c,分别实现如下功能:a.通过利用移动回调函数可以在不释放鼠标按钮的情况下,连续画一系列正方形;#include"stdafx.h"#include <stdlib.h>#include <GL/glut.h>#include&

5、lt;math.h>/* globals */GLsizei wh = 300, ww = 300; /* initial window size */GLfloat size = 5.0; /* half side length of square */void drawSquare(int x, int y) y=wh-y; glColor3ub( (char) rand()%256, (char) rand()%256, (char) rand()%256); /glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POLYGON); glVertex2

6、f(x+size, y+size); glVertex2f(x-size, y+size); glVertex2f(x-size, y-size); glVertex2f(x+size, y-size); glEnd(); glFlush();/* rehaping routine called whenever window is resizedor moved */void myReshape(GLsizei w, GLsizei h) /* adjust clipping box */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOr

7、tho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); /* adjust viewport and clear */ glViewport(0,0,w,h); glClearColor (0.0, 0.0, 0.0, 1.0);glClear(GL_COLOR_BUFFER_BIT); glFlush(); /* set global size for use by drawing routine */ ww = w; wh = h; void myin

8、it(void)/* set clear color to black */glClearColor (0.0, 0.0, 0.0, 1.0); void mouse(int btn, int state, int x, int y) if(btn=GLUT_RIGHT_BUTTON && state=GLUT_DOWN) exit(0);if(btn = GLUT_LEFT_BUTTON && state = GLUT_DOWN) drawSquare(x, y);/* display callback required by GLUT 3.0 */void

9、display(void)int main(int argc, char* argv) glutInit(&argc,argv);glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);glutInitWindowSize(300,300);glutInitWindowPosition(0,0); glutCreateWindow("E11414103叶传军");glutDisplayFunc(display); glutReshapeFunc (myReshape); glutMotionFunc (drawSquare); my

10、init (); glutMainLoop();b.应用被动移动回调函数,可以不用按鼠标按钮就可以连续画正方形;#include"stdafx.h"#include <stdlib.h>#include <GL/glut.h>#include<math.h>/* globals */GLsizei wh = 300, ww = 300; /* initial window size */GLfloat size = 5.0; /* half side length of square */void drawSquare(int x, in

11、t y) y=wh-y; glColor3ub( (char) rand()%256, (char) rand()%256, (char) rand()%256); /glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POLYGON); glVertex2f(x+size, y+size); glVertex2f(x-size, y+size); glVertex2f(x-size, y-size); glVertex2f(x+size, y-size); glEnd(); glFlush();/* rehaping routine called wheneve

12、r window is resizedor moved */void myReshape(GLsizei w, GLsizei h) /* adjust clipping box */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); /* adjust viewport and clear */ glViewport(0,0,w,h); glCle

13、arColor (0.0, 0.0, 0.0, 1.0);glClear(GL_COLOR_BUFFER_BIT); glFlush(); /* set global size for use by drawing routine */ ww = w; wh = h; void myinit(void)/* set clear color to black */glClearColor (0.0, 0.0, 0.0, 1.0); void mouse(int btn, int state, int x, int y) if(btn=GLUT_RIGHT_BUTTON && st

14、ate=GLUT_DOWN) exit(0);if(btn = GLUT_LEFT_BUTTON && state = GLUT_DOWN) drawSquare(x, y);/* display callback required by GLUT 3.0 */void display(void)int main(int argc, char* argv) glutInit(&argc,argv);glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);glutInitWindowSize(300,300);glutInitWindow

15、Position(0,0); glutCreateWindow("E11414103叶传军");glutDisplayFunc(display); glutReshapeFunc (myReshape); glutPassiveMotionFunc (drawSquare); myinit (); glutMainLoop();c.按下Alt+c或Alt+C时,终止程序。void keyboard(unsigned char key,int x,int y)if(glutGetModifiers()=GLUT_ACTIVE_ALT&&(key='c&

16、#39;|key='C') exit(0);int main(int argc, char* argv) glutInit(&argc,argv);glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);glutInitWindowSize(300,300);glutInitWindowPosition(0,0); glutCreateWindow("square");glutDisplayFunc(display); glutReshapeFunc (myReshape); glutPassiveMotionFun

17、c (drawSquare);glutKeyboardFunc(keyboard); myinit (); glutMainLoop();2.)编写一个程序,实现如下的功能:连续两次单击鼠标左键,以两次单击的位置作为矩形的对角线来绘制一个矩形,且该矩形各边与屏幕对齐。鼠标右键用于程序的退出。(1)将绘制矩形的函数放在鼠标回调函数中完成。#include"stdafx.h"#include <stdlib.h>#include <GL/glut.h>/* globals */int ipointnum=0;int x1=0,x2=0,y1=0,y2=0

18、;GLsizei wh = 300, ww = 300; /* initial window size */GLfloat size = 5.0; /* half side length of square */void drawSquare(int x, int y) y=wh-y; glColor3ub( (char) rand()%256, (char) rand()%256, (char) rand()%256); /glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POLYGON); glVertex2f(x+size, y+size); glVert

19、ex2f(x-size, y+size); glVertex2f(x-size, y-size); glVertex2f(x+size, y-size); glEnd(); glFlush();/* rehaping routine called whenever window is resizedor moved */void myReshape(GLsizei w, GLsizei h) /* adjust clipping box */glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, (GLdouble)w, 0.0,

20、 (GLdouble)h, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); /* adjust viewport and clear */glViewport(0,0,w,h); glClearColor (0.0, 0.0, 0.0, 1.0);glClear(GL_COLOR_BUFFER_BIT); glFlush(); /* set global size for use by drawing routine */ ww = w; wh = h; void myinit(void) /* set clear color

21、 to black */glClearColor (0.0, 0.0, 0.0, 1.0); void mouse(int btn, int state, int x, int y)if(btn=GLUT_RIGHT_BUTTON && state=GLUT_DOWN) exit(0);if(btn = GLUT_LEFT_BUTTON && state = GLUT_DOWN) if(ipointnum=0|ipointnum=2) ipointnum=1; x1=x; y1=wh-y; else ipointnum=2; x2=x; y2=wh-y; if(

22、ipointnum=2) glColor3ub( (char) rand()%256, (char) rand()%256, (char) rand()%256); glBegin(GL_POLYGON); glVertex2f(x1, y1); glVertex2f(x1, y2); glVertex2f(x2, y2); glVertex2f(x2, y1); glEnd(); glFlush(); /* display callback required by GLUT 3.0 */void display(void)int main(int argc, char* argv)glutI

23、nit(&argc,argv);glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);glutInitWindowSize(300,300);glutInitWindowPosition(0,0); glutCreateWindow("E11414103叶传军");glutDisplayFunc(display); glutReshapeFunc (myReshape); glutMouseFunc (mouse);myinit (); glutMainLoop();(2)修改(1)中的程序,将绘制矩形的函数放在显示回调函数中完成

24、。鼠标回调函数用于状态的修改,并调用显示回调函数(利用glutPostRedisplay())。void mouse(int btn, int state, int x, int y) if(btn=GLUT_RIGHT_BUTTON && state=GLUT_DOWN) exit(0);if(btn = GLUT_LEFT_BUTTON && state = GLUT_DOWN) if(ipointnum=0|ipointnum=2) ipointnum=1; x1=x; y1=wh-y; else ipointnum=2; x2=x; y2=wh-y; i

25、f(ipointnum=2) glutPostRedisplay(); /* display callback required by GLUT 3.0 */void display(void) glColor3ub( (char) rand()%256, (char) rand()%256, (char) rand()%256); glBegin(GL_POLYGON); glVertex2f(x1, y1); glVertex2f(x1, y2); glVertex2f(x2, y2); glVertex2f(x2, y1); glEnd(); glFlush();3.将正方形旋转的程序s

26、quareRotate.c改成正六边形旋转的程序。/* * double.c * This program demonstrates double buffering for * flicker-free animation. The left and middle mouse * buttons start and stop the spinning motion of the square. */#include"stdafx.h"#include <stdlib.h>#include <GL/glut.h>#include <math.h

27、>#define DEGREES_TO_RADIANS 3.14159/180.0GLfloat theta = 0.0; / 全局变量void display() glClear(GL_COLOR_BUFFER_BIT); glPushMatrix(); glBegin(GL_POLYGON); glVertex2f(sin(theta*60/180*3.14159),cos(theta*60/180*3.14159); glVertex2f(sin(theta*60/180*3.14159+1),cos(theta*60/180*3.14159+1); glVertex2f(sin(

28、theta*60/180*3.14159+2),cos(theta*60/180*3.14159+2); glVertex2f(sin(theta*60/180*3.14159+3),cos(theta*60/180*3.14159+3); glVertex2f(sin(theta*60/180*3.14159+4),cos(theta*60/180*3.14159+4); glVertex2f(sin(theta*60/180*3.14159+5),cos(theta*60/180*3.14159+5); glEnd(); glPopMatrix(); glutSwapBuffers ();

29、void idle() theta += 2.0; if (theta > 360.0) theta -= 360.0; glutPostRedisplay(); / 请求重绘void myinit () glClearColor (0.0, 0.0, 0.0, 1.0); glColor3f (1.0, 1.0, 1.0); glShadeModel (GL_FLAT);void mouse(int btn, int state, int x, int y) if(btn=GLUT_LEFT_BUTTON && state=GLUT_DOWN) glutIdleFunc(idle); if(glutGetModifiers() = GLUT_ACTIVE_CTRL && btn=GLUT_LEFT_BUTTON && state=GLUT_DOWN) glutIdleFunc(NULL);void mykey(unsigned char key, int x, int y) / 按下Q、q,终止程序if(key =

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论