前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >实验8 OpenGL交互

实验8 OpenGL交互

作者头像
步行者08
发布2018-10-09 10:49:20
1.1K0
发布2018-10-09 10:49:20
举报
文章被收录于专栏:图形学与OpenGL

1.实验目的:

理解掌握一个OpenGL程序的常见交互方法。

2.实验内容:

(1) 运行示范实验代码1,掌握程序鼠标交互方法,尝试为其添加键盘与菜单控制,实现同样功能;

(2)运行示范实验代码2,掌握程序鼠标坐标获取与绘图方法,尝试为其添加绘制直线功能;

(3)结合上述两步,能否实现通过鼠标右键菜单切换实现一个简单的绘图程序,可以绘制直线、三角形、正方形等常见图形?

3.实验原理:

要想在OpenGL中处理鼠标事件非常的方便,GLUT已经为我们的注册好了函数,只要我们提供一个方法。使用函数glutMouseFunc,就可以帮我们注册我们的函数,这样当发生鼠标事件时就会自动调用我们的方法。

函数的原型是:

void glutMouseFunc(void(*func)(int button,int state,int x,int y));

参数:func:处理鼠标click事件的函数的函数名。

从上面可以看到,处理鼠标单击事件的函数,一定有4个参数。第一个参数表明哪个鼠标键被按下或松开,这个变量可以是下面的三个值中的一个: GLUT_LEFT_BUTTON GLUT_MIDDLE_BUTTON GLUT_RIGHT_BUTTON

第二个参数表明,函数被调用发生时,鼠标的状态,也就是是被按下,或松开,可能取值如下: GLUT_DOWN GLUT_UP

当函数被调用时,state的值是GLUT_DOWN,那么程序可能会假定将会有个GLUT_UP事件,甚至鼠标移动到窗口外面,也如此。然而,如果程序调用glutMouseFunc传递NULL作为参数,那么GLUT将不会改变鼠标的状态。剩下的两个参数(x,y)提供了鼠标当前的窗口坐标(以左上角为原点)。

键盘相关知识可参考:http://blog.csdn.net/xie_zi/article/details/1911891

菜单相关知识可参考:http://blog.csdn.net/xie_zi/article/details/1963383

4.示范代码:

(1)鼠标控制旋转的正方形

#include <GL/glut.h>

#include <math.h>

#include <stdlib.h>

#define DEGREES_TO_RADIANS 3.14159/180.0

static GLfloat spin = 0.0;

GLfloat x = 0.0;

GLfloat y = 0.0;

GLfloat size = 50.0;

GLfloat angle = 2.0;

GLsizei wh = 500, ww = 500;

void square()

{

glBegin(GL_QUADS);

glVertex2f(x,y);

glVertex2f(-y,x);

glVertex2f(-x,-y);

glVertex2f(y,-x);

glEnd();

}

void spinDisplay(void)

{

spin = spin + 2.0;

if (spin > 360.0) spin = spin - 360.0;

x=125.0 * cos(DEGREES_TO_RADIANS*spin);

y=125.0 * sin(DEGREES_TO_RADIANS*spin);

glutPostRedisplay();

}

void mydisplay()

{

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0, 1.0, 1.0);

square();

glutSwapBuffers();

}

void init()

{

glClearColor (0.0, 0.0, 0.0, 1.0);

}

void myreshape(GLint w, GLint h) {

glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glOrtho(-w/2, w/2, -h/2, h/2, -1.0, 1.0);

glMatrixMode(GL_MODELVIEW);

ww = w;

wh = h;

}

void mymouse(GLint button, GLint state, GLint wx, GLint wy)

{

if(button ==GLUT_RIGHT_BUTTON && state == GLUT_DOWN)

exit(0);

if(button ==GLUT_LEFT_BUTTON && state == GLUT_DOWN)

glutIdleFunc(spinDisplay);

if(button== GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)

glutIdleFunc(NULL);

}

void main(int argc, char** argv)

{

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

glutInitWindowSize(500,500);

glutInitWindowPosition(0,0);

glutCreateWindow("double");

init();

glutDisplayFunc(mydisplay);

glutReshapeFunc(myreshape);

glutMouseFunc(mymouse);

glutIdleFunc(spinDisplay);

glutMainLoop();

}

(2)鼠标当前位置绘制方框

#include <GL/glut.h>

#include <math.h>

#include <stdlib.h>

GLfloat x = 0.0;

GLfloat y = 0.0;

GLfloat size = 50.0;

GLsizei wh = 500, ww = 500;

void drawSquare(GLint x, GLint y) {

y = wh-y;

glBegin(GL_POLYGON);

glVertex3f(x + size, y + size, 0);

glVertex3f(x - size, y + size, 0);

glVertex3f(x - size, y - size, 0);

glVertex3f(x + size, y - size, 0);

glEnd();

}

void mydisplay()

{

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0, 1.0, 1.0);

drawSquare(x, y);

glutSwapBuffers();

glutPostRedisplay();

}

void init()

{

glClearColor (0.0, 0.0, 0.0, 1.0);

}

void myreshape(GLint w, GLint h) {

glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glOrtho(0, w, 0, h, -1.0, 1.0);

glMatrixMode(GL_MODELVIEW);

ww = w;

wh = h;

}

void mymouse(GLint button, GLint state, GLint wx, GLint wy)

{

if(button ==GLUT_RIGHT_BUTTON && state == GLUT_DOWN)

exit(0);

if(button ==GLUT_LEFT_BUTTON && state == GLUT_DOWN)

{

x = wx;

y = wy;

}

}

void main(int argc, char** argv)

{

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

glutInitWindowSize(500,500);

glutInitWindowPosition(0,0);

glutCreateWindow("double");

init();

glutDisplayFunc(mydisplay);

glutReshapeFunc(myreshape);

glutMouseFunc(mymouse);

glutMainLoop();

}

5. 实验作业:

试比较所给两个示范代码的窗口坐标系有何不同。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017年04月17日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档