C++程序 使用鼠标移动在OpenGL中绘制圆的程序
在OpenGL中,绘制圆形是经常用到的操作之一,本文将通过一个示例程序来演示如何使用鼠标移动在OpenGL中绘制圆形。
准备工作
首先需要安装OpenGL的库文件,在Windows系统下,可以使用MinGW进行安装。同时,需要安装OpenGL的窗口管理库GLFW。
安装完成之后,将其加入到编译器的链接库中,以便正常使用。在本示例中,将使用C++进行编写。
#include <GLFW/glfw3.h>
#include <iostream>
绘制圆形
在OpenGL中,绘制圆形需要用到数学计算中的正弦函数和余弦函数。首先定义圆心的坐标为(x_{0}, y_{0}),圆的半径为r,则圆上任意一点的坐标为(x, y)。
根据正弦函数和余弦函数的定义,有:
x = x_{0} + r\cos\theta \
y = y_{0} + r\sin\theta
其中,\theta为弧度制的角度,其范围在[0, 2\pi]之间。可以使用一个循环来遍历角度值,然后根据上式计算出圆上的点的坐标。
void drawCircle(float x, float y, float r) {
    glBegin(GL_POLYGON);
    for (int i = 0; i < 360; i++) {
        float theta = 2.0f * 3.1415926f * float(i) / float(360);
        float x1 = r * cosf(theta);
        float y1 = r * sinf(theta);
        glVertex2f(x + x1, y + y1);
    }
    glEnd();
}
在上述函数中,使用了OpenGL中的glBegin()和glEnd()函数来绘制多边形,同时使用glVertex2f()来绘制点的位置。
监听鼠标事件和绘制圆形
接下来,需要监听鼠标事件,并根据鼠标的移动来绘制圆形。
void mouse_callback(GLFWwindow* window, double xpos, double ypos) {
    static float x, y, r = 0.1f;
    int win_width, win_height;
    glfwGetWindowSize(window, &win_width, &win_height);
    x = (xpos / win_width * 2 - 1) / 1.05f;
    y = -(ypos / win_height * 2 - 1) / 1.05f;
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0f, 1.0f, 1.0f);
    drawCircle(x, y, r);
    glfwSwapBuffers(window);
}
int main() {
    GLFWwindow* window;
    if (!glfwInit()) {
        std::cout << "Failed to initialize GLFW" << std::endl;
        return -1;
    }
    window = glfwCreateWindow(800, 600, "My OpenGL Window", NULL, NULL);
    if (!window) {
        glfwTerminate();
        std::cout << "Failed to create GLFW window" << std::endl;
        return -1;
    }
    glfwMakeContextCurrent(window);
    glfwSetCursorPosCallback(window, mouse_callback);
    while (!glfwWindowShouldClose(window)) {
        glfwPollEvents();
    }
    glfwTerminate();
    return 0;
}
在上述代码中,主要有以下几个步骤:
- 在
mouse_callback()函数中,首先获取到窗口大小,然后根据鼠标的位置计算得到圆心的坐标。通过在GLFWwindow中设置glfwSetCursorPosCallback回调函数,进行事件监听。 - 在
main()函数中,创建窗口,并调用glfwPollEvents()等待事件触发。 
完整代码
完整代码如下:
#include <GLFW/glfw3.h>
#include<iostream>
void drawCircle(float x, float y, float r) {
    glBegin(GL_POLYGON);
    for (int i = 0; i < 360; i++) {
        float theta = 2.0f * 3.1415926f * float(i) / float(360);
        float x1 = r * cosf(theta);
        float y1 = r * sinf(theta);
        glVertex2f(x + x1, y + y1);
    }
    glEnd();
}
void mouse_callback(GLFWwindow* window, double xpos, double ypos) {
    static float x, y, r = 0.1f;
    int win_width, win_height;
    glfwGetWindowSize(window, &win_width, &win_height);
    x = (xpos / win_width * 2 - 1) / 1.05f;
    y = -(ypos / win_height * 2 - 1) / 1.05f;
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0f, 1.0f, 1.0f);
    drawCircle(x, y, r);
    glfwSwapBuffers(window);
}
int main() {
    GLFWwindow* window;
    if (!glfwInit()) {
        std::cout << "Failed to initialize GLFW" << std::endl;
        return -1;
    }
    window = glfwCreateWindow(800, 600, "My OpenGL Window", NULL, NULL);
    if (!window) {
        glfwTerminate();
        std::cout << "Failed to create GLFW window" << std::endl;
        return -1;
    }
    glfwMakeContextCurrent(window);
    glfwSetCursorPosCallback(window, mouse_callback);
    while (!glfwWindowShouldClose(window)) {
        glfwPollEvents();
    }
    glfwTerminate();
    return 0;
}
结论
通过本文的介绍,可以看到在OpenGL中绘制圆形的方法,并利用鼠标事件来移动圆心绘制圆形,希望对大家有所帮助。
极客笔记