I am currently moving my opengl codes I developed in Windows/Mac to Ubuntu. I don't know if my Ubuntu has a really slow performance, or if my code is wrong. When the render window is open, the camera system should read the mouse movement and change the yaw/pitch/fork of the camera, but it just reads the keyboard input, and not the mouse movement. I can only move around the camera with the keyboard.
Here are the basic codes regarding the mouse callback.
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);
....
}
Here is the mouse_callback()
function I send to 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);
}
Is it my slow Ubuntu problem or the code problem? This code even works in Mac, which is considered a really similar OS with Linux. I'm currently using the glfw3 library.
Here is the code related to the input keyboard processing, which works really well. (Just in case)
Inside my render loop
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();
}
My processInput()
function
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);
}
EDIT
I tried std::cout << "callback called" <<std::endl
in the mouse_callback()
function. It is called, but only maximum 2 times in the whole program. It's either called once or twice only when the program starts. Maybe it is a bug of glfw in Ubuntu