1
votes

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

Write a minimal reproducible example and see whether the problem still exists.tkausl
@tkausl Hmm that's weird. I took your advice and I just drew a basic cube to see if I can move around the camera. But even now only the keyboard input works, but not the mouse movement input.Peter
Then this might be a bug in GLFW. You could try to print something in your callback to see whether GLFW even calls it, but I assume since you're not seeing any results the answer is no.tkausl
@tkausl I wish that was the case. The callback code doesn't have any problems though right?Peter
Just out of curiosity: What kind of graphics environment are you using. Is it a "pure" true Xorg server? Is is "pure" Wayland? Is it legacy Ubuntu Mir? Is it Xwayland on top of a Wayland compositor? Both Wayland and Mir set out to replace X11 because input handling was too complicated and fragile… Guess what: Input handling in Wayland and Mir is even more broken than in X11… Display surface management is nice though. I suggest you try your program in a pure, true Xorg environment.datenwolf