GLUT鼠标回调函数详解:计算机图形学与交互输入

需积分: 20 0 下载量 69 浏览量 更新于2024-08-24 收藏 16.24MB PPT 举报
GLUT鼠标函数/子程序是计算机图形学中的一个重要部分,它在图形用户界面设计中扮演着关键角色。这些函数允许程序对用户的鼠标操作做出响应,比如点击、拖动等,这对于实现交互式图形应用程序至关重要。在GLUT库中,`mouseFunc` 函数是核心的回调函数,其原型定义为`void mouseFunc (GLint button, GLint action, GLint xMouse, GLint yMouse)`。其中,`button` 参数指示被按下的鼠标键,可以是`GLUT_LEFT_BUTTON`、`GLUT_MIDDLE_BUTTON` 或 `GLUT_RIGHT_BUTTON`;`action` 参数表示事件类型,即`GLUT_DOWN` 表示按钮按下,`GLUT_UP` 表示按钮释放;`(xMouse, yMouse)` 是鼠标光标在窗口内的相对坐标,这对于跟踪用户在屏幕上的操作位置非常重要。 通过`glutMouseFunc(mouseFunc)`,开发者可以将这个自定义函数绑定到窗口的鼠标事件上,以便在适当的时候执行相应的处理逻辑。这涉及到计算机图形学的交互输入部分,包括图形用户界面的设计,以及如何利用鼠标的输入来改变视图、选择对象或者触发其他图形操作。 在更广泛的计算机图形学领域,`mouseFunc` 是一个典型的应用实例,它与图形用户界面技术紧密相连。计算机图形学是一门跨学科的研究领域,涵盖了诸如光栅图形学(Raster Graphics)、计算几何学(Computational Geometry)、真实感图形学(Photorealistic Rendering)和虚拟现实(Virtual Reality)等多个分支。这些分支研究内容包括图形硬件和软件、输入输出设备、图形标准、图形交互技术、图形生成算法、实体造型、真实感渲染技术、动画制作以及虚拟环境的创建。 在真实感图形学中,投影变换是一个关键概念,它涉及到从三维空间到二维屏幕空间的转换,确保物体在屏幕上正确呈现。投影变换涉及到数学投影理论,如透视投影、正射投影等,这些都是实现逼真渲染效果的基础。 总而言之,GLUT鼠标函数是计算机图形学中实现用户交互和图形反馈的关键组成部分,而整个计算机图形学则是一门广泛且深入的科学,它融合了数学、硬件技术、软件开发和感知科学,旨在创造和呈现逼真的视觉体验。

#define _USE_MATH_DEFINES #include <cstdlib> #include <cmath> #include <iostream> #include <GL/glew.h> #include <GL/freeglut.h> // Globals. static float R = 40.0; // Radius of circle. static float X = 50.0; // X-coordinate of center of circle. static float Y = 50.0; // Y-coordinate of center of circle. static const int numVertices = 50; // Number of vertices on circle. static int verticesColors[6 * numVertices]; void generateVertices() { float t = 0; // Angle parameter. for (int i = 0; i < 6 * numVertices; i += 6) { verticesColors[i] = X + R * cos(t); //x verticesColors[i] = Y + R * sin(t); //y verticesColors[i] = 0.0; //z verticesColors[i] = 1.0; //r verticesColors[i] = 0.0; //g verticesColors[i] = 0.0; //b t += 2 * M_PI / numVertices; //angle } } // Drawing routine. void drawScene(void) { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1, 0, 0); glLineWidth(5); glDrawArrays(GL_POLYGON, 0, numVertices); glFlush(); } // Initialization routine. void setup(void) { glClearColor(1.0, 1.0, 1.0, 0.0); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); glColorPointer(3, GL_FLOAT, 6 * sizeof(float), &verticesColors[0]); glVertexPointer(3, GL_FLOAT, 6 * sizeof(float), &verticesColors[3]); } // OpenGL window reshape routine. void resize(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, 100.0, 0.0, 100.0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } // Keyboard input processing routine. void keyInput(unsigned char key, int x, int y) { switch (key) { case 27: exit(0); break; default: break; } } // Main routine. int main(int argc, char** argv) { generateVertices(); glutInit(&argc, argv); glutInitContextVersion(4, 3); glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA); glutInitWindowSize(500, 500); glutInitWindowPosition(100, 100); glutCreateWindow("circle.cpp"); glutDisplayFunc(drawScene); glutReshapeFunc(resize); glutKeyboardFunc(keyInput); glewExperimental = GL_TRUE; glewInit(); setup(); glutMainLoop(); }

2023-05-25 上传