我目前正在将在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()
的{{1}}函数
glfwSetCursorPosCallback()
是我的慢Ubuntu问题还是代码问题?该代码甚至可以在Mac上运行,Mac被认为与Linux非常相似。我目前正在使用glfw3库。
这是与输入键盘处理有关的代码,效果非常好。 (以防万一)
在我的渲染循环中
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);
}
我的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
。它被调用,但是在整个程序中最多只能调用2次。仅在程序启动时才调用一次或两次。也许这是Ubuntu中glfw的错误