linux - 为什么在Linux开发的openGL程序中没有捕捉到鼠标移动?
问题描述
我目前正在将我在 Windows/Mac 中开发的 opengl 代码移动到 Ubuntu。我不知道我的 Ubuntu 性能是否真的很慢,或者我的代码是否错误。当渲染窗口打开时,相机系统应该读取鼠标移动并改变相机的偏航/俯仰/前叉,但它只读取键盘输入,而不是鼠标移动。我只能用键盘在相机周围移动。
以下是有关鼠标回调的基本代码。
int main()
{
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this statement to fix compilation on OS X
#endif
// glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", glfwGetPrimaryMonitor(), NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
// tell GLFW to capture our mouse
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
....
}
这是mouse_callback()
我发送到的功能glfwSetCursorPosCallback()
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
if (firstMouse)
{
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
float xoffset = xpos - lastX;
float yoffset = lastY - ypos; // reversed since y-coordinates go from bottom to top
lastX = xpos;
lastY = ypos;
camera.ProcessMouseMovement(xoffset, yoffset);
}
是我的 Ubuntu 慢问题还是代码问题?这段代码甚至可以在 Mac 上运行,Mac 被认为是与 Linux 非常相似的操作系统。我目前正在使用 glfw3 库。
这是与输入键盘处理相关的代码,效果非常好。(以防万一)
在我的渲染循环中
while (!glfwWindowShouldClose(window))
{
// per-frame time logic
// --------------------
VISITED = new unsigned int[11]{ 0,0,0,0,0,0,0,0,0,0,0 };
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
// input
// -----
processInput(window);
//render
.......
glfwSwapBuffers(window);
glfwPollEvents();
}
我的processInput()
功能
void processInput(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
camera.ProcessKeyboard(FORWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
camera.ProcessKeyboard(BACKWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
camera.ProcessKeyboard(LEFT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
camera.ProcessKeyboard(RIGHT, deltaTime);
}
编辑
std::cout << "callback called" <<std::endl
我在功能中尝试过mouse_callback()
。它被调用,但在整个程序中最多只能调用 2 次。仅在程序启动时才调用一次或两次。也许这是 Ubuntu 中 glfw 的错误
解决方案
推荐阅读
- wordpress - 添加产品时,Woocommerce 产品类别丢失(不显示)
- javascript - 在 Laravel 中验证前后使用 javascript 添加表单元素
- javascript - 世博会缺少相机胶卷权限
- android - 延迟注册的 Java c++ 本机函数
- laravel - InvalidArgumentException Route [login] 未定义。在 Laravel 5.6 中
- javascript - 三个 JS OBJloader - obj 没有正确导入
- go - 如何在 Go 语言中获取指定时区的 Hour()?
- mysql - 创建一个视图,其中来自同一列的 2 条记录位于同一行
- git - Git - rm:无法识别的选项`--cached'
- sql - 使用计算值更新 psql 表