You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GLFW与OpenGL开发:蓝色方块边界检测触发延迟问题求助

GLFW与OpenGL开发:蓝色方块边界检测触发延迟问题求助

我最近在用GLFW做个小实验,编译命令是g++ -o nxtBluePixel nxtBluePixel.cpp -lglfw -lGLEW -lGL,功能很简单:画一个蓝色的点(当作方块),用WASD控制它上下左右移动。我想实现的是,当这个点碰到可视区域边缘时,输出"out of bounds"提示。

我查资料了解到OpenGL的归一化坐标范围是-1到1,所以就把边界检测条件设成了判断坐标是否超出这个范围。但实际运行时出问题了:点明明已经移出可视区域变透明了,却要按很多次方向键(至少10次左右)才会触发提示。

我找ChatGPT 4问了这个问题,它的回复是:

"Correct Boundary Check: If you want the "out of bounds" message to appear as soon as the point is about to leave the visible area, your original check without the large epsilon is logically correct. However, if the message appears too late or too early, it might be due to how the point's position is updated or rendered, not the boundary check itself."

我完全是OpenGL新手,本来想试试手,结果遇到这种烦人的问题就头大!有没有大佬能帮我分析分析?

附上我的代码:

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <string>
#include <cctype>

GLint worldWidth = 400;
GLint worldHeight = 300;

// Starting position of the pixel
float currX = 0.0f;
float currY = 0.0f;

float stepSize = 1.0f / worldWidth;
float speed = 2.0f;

void updateWorld(char keyPressed, float speed){
  switch (keyPressed) {
  case 'W':
    //up
    currY += stepSize*speed;
    break;
  case 'A':
    //left
    currX -= stepSize*speed;
    break;
  case 'S':
    //down
    currY -= stepSize*speed;
    break;
  case 'D':
    //right
    currX += stepSize*speed;
    break;
  }

  //using openGL 'normalized' coords i.e. between -1 and 1
  if (currX >= 1.0 || currX <= -1.0 || currY >= 1.0 || currY <= -1.0){
    printf("OUT OF BOUNDS !!!!");
  }
}

void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
  char key_str = '0';
  if (action == GLFW_PRESS || action == GLFW_REPEAT) {
    switch (key) {
    case GLFW_KEY_W:
      key_str = 'W';
      break;
    case GLFW_KEY_A:
      key_str = 'A';
      break;
    case GLFW_KEY_S:
      key_str = 'S';
      break;
    case GLFW_KEY_D:
      key_str = 'D';
      break;
    case GLFW_KEY_Q:
      printf("Bye ...");
      glfwSetWindowShouldClose(window, GL_TRUE);
      break;
    default:
      printf("unknown key pressed \n");
      break;
    }
    updateWorld(key_str, speed);
  }
}

int main(void) {
  GLFWwindow* window;

  // Initialize the library
  if (!glfwInit())
    return -1;

  //remove win frame
  //glfwWindowHint(GLFW_DECORATED, GLFW_FALSE);

  // Create a windowed mode window and its OpenGL context
  window = glfwCreateWindow(worldWidth, worldHeight, "Move Pixel with Keyboard", NULL, NULL);
  if (!window) {
    glfwTerminate();
    return -1;
  }

  // Make the window's context current
  glfwMakeContextCurrent(window);

  // Set the keyboard input callback
  glfwSetKeyCallback(window, key_callback);

  // Initialize GLEW
  glewExperimental = GL_TRUE;
  if (glewInit() != GLEW_OK) {
    std::cerr << "Failed to initialize GLEW" << std::endl;
    return -1;
  }

  //glfwGetFramebufferSize(window, &worldWidth, &worldHeight);
  // Define the viewport dimensions
  glViewport(0, 0, worldWidth, worldHeight);

  // Set point size
  glPointSize(10.0f); // Increase if you want the "pixel" to be bigger

  // Loop until the user closes the window
  while (!glfwWindowShouldClose(window)) {
    // Render here
    glClear(GL_COLOR_BUFFER_BIT);

    // Set the drawing color to blue
    glColor3f(0.0f, 0.0f, 1.0f); // RGB: Blue

    // Draw a point at the current position
    glBegin(GL_POINTS);
    glVertex2f(currX, currY); // Use the updated position
    glEnd();

    // Swap front and back buffers
    glfwSwapBuffers(window);

    // Poll for and process events
    glfwPollEvents();
  }

  glfwTerminate();
  return 0;
}

我知道最坏情况可以用printf一步步调试,但那样太费时间了,而且不一定能找到根源——我感觉这里有个和分辨率相关的“误差值”,但不知道怎么计算它,总不能一直靠试错来调这个值吧?

备注:内容来源于stack exchange,提问作者AlanTuring

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.17 12:43:03