当前位置:实例文章 » 其他实例» [文章]C++ 左键实现 加线条,右键实现指定位置的点消失,现时删除线。

C++ 左键实现 加线条,右键实现指定位置的点消失,现时删除线。

发布人:shili8 发布时间:2025-02-13 21:50 阅读次数:0

**左键实现加线条,右键实现指定位置的点消失**

在 C++ 中,我们可以使用 OpenGL 来绘制图形。下面是一个简单的例子,展示了如何实现左键添加线条,右键删除指定位置的点。

###依赖库我们需要以下依赖库:

* `GLFW`:用于处理窗口事件和输入。
* `GLAD`:用于加载 OpenGL 函数。
* `glm`:用于数学运算。

###代码实现

cpp#include 
#include 
#include 
#include 

// 全局变量int windowWidth =800;
int windowHeight =600;

float points[100][2] = {{0.0f,0.0f}, {1.0f,0.0f}, {1.0f,1.0f}, {0.0f,1.0f}};
int pointCount =4;
bool isDrawingLine = false;

void initGLFW() {
 // 初始化 GLFW if (!glfwInit()) {
 std::cerr << "Failed to initialize GLFW" << std::endl;
 exit(EXIT_FAILURE);
 }

 // 创建窗口 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);
 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,3);
 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

 window = glfwCreateWindow(windowWidth, windowHeight, "C++ OpenGL", NULL, NULL);
 if (!window) {
 std::cerr << "Failed to create GLFW window" << std::endl;
 exit(EXIT_FAILURE);
 }

 // 设置窗口大小回调 glfwSetWindowSizeCallback(window, [](GLFWwindow* window, int width, int height) {
 windowWidth = width;
 windowHeight = height;
 });

 // 显示窗口 glfwMakeContextCurrent(window);

 // 初始化 GLEW if (glewInit() != GLEW_OK) {
 std::cerr << "Failed to initialize GLEW" << std::endl;
 exit(EXIT_FAILURE);
 }
}

void drawPoints() {
 glClear(GL_COLOR_BUFFER_BIT);

 for (int i =0; i < pointCount; ++i) {
 glBegin(GL_POINTS);
 glColor3f(1.0f,0.0f,0.0f); // 红色 glVertex2fv(glm::value_ptr(points[i]));
 glEnd();
 }

 glFlush();
}

void drawLine() {
 glClear(GL_COLOR_BUFFER_BIT);

 glBegin(GL_LINES);
 glColor3f(0.0f,1.0f,0.0f); // 绿色 glVertex2fv(glm::value_ptr(points[pointCount -2]));
 glVertex2fv(glm::value_ptr(points[pointCount -1]));
 glEnd();

 glFlush();
}

void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods) {
 if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
 isDrawingLine = true;
 } else if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS) {
 // 删除指定位置的点 pointCount--;
 for (int i =0; i < pointCount; ++i) {
 points[i][0] = points[i +1][0];
 points[i][1] = points[i +1][1];
 }
 } else if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE) {
 isDrawingLine = false;
 }
}

void mouseMoveCallback(GLFWwindow* window, double xpos, double ypos) {
 if (isDrawingLine) {
 // 添加线条 points[pointCount][0] = (float)xpos / (float)windowWidth *2.0f -1.0f;
 points[pointCount][1] = (float)ypos / (float)windowHeight *2.0f -1.0f;
 pointCount++;
 }
}

int main() {
 initGLFW();

 // 设置鼠标回调 glfwSetMouseButtonCallback(window, mouseButtonCallback);
 glfwSetCursorPosCallback(window, [](GLFWwindow* window, double xpos, double ypos) {
 mouseMoveCallback(window, xpos, ypos);
 });

 while (!glfwWindowShouldClose(window)) {
 if (pointCount >1 && isDrawingLine) {
 drawLine();
 } else {
 drawPoints();
 }

 glfwSwapBuffers(window);
 glfwPollEvents();
 }

 //释放资源 glfwTerminate();

 return0;
}


### 使用说明* 在 `main` 函数中,首先初始化 GLFW 和 GLEW。
* 然后设置鼠标回调函数 `mouseButtonCallback` 和鼠标移动回调函数 `mouseMoveCallback`。
* 在循环体内,如果正在绘制线条,则使用 `drawLine` 函数绘制线条;否则,使用 `drawPoints` 函数绘制点。
* 最后,在循环结束时释放 GLFW 资源。

### 注意事项* 这个例子假设窗口大小为800x600。
* 当鼠标左键点击时,会开始绘制线条;当鼠标右键点击时,会删除指定位置的点。
* 当鼠标移动时,如果正在绘制线条,则会添加线条。

### 总结这个例子展示了如何使用 C++ 和 OpenGL 实现左键添加线条,右键删除指定位置的点。它涉及到 GLFW 的窗口事件和输入处理,以及 GLEW 的 OpenGL 函数加载。

其他信息

其他资源

Top