0
\$\begingroup\$

I'm trying my hardest to understand what this code is doing so far I think the sin(theta) and cos(theta) of the code is representing a point with an angle.

This point with an angle sin(theta) and cos(theta) will allow the xmouse/16 value and ymouse/16 value to rotate or translate on this point with an angle because of their layout representation in the GLM::LookAt() function (view/camera matrix).

The result of doing this is that the view matrix rotates on the x axis left and right when I move the mouse left and right and the y axis up and down when I move the mouse up and down.

Is this correct? It took a lot work to understand this and if I am correct does anyone know how to move the mouse left, right, up and down from the centre of the screen (as in the mouse cursor at centre of the screen when moved to the left, right, up and down will move the camera respectively) rather then from a small region of the top left hand corner thanks!.

N.B.: I'm using Model Matrix and Perspective Projection Matrix with this in the middle so Model * View * Perspective

  • C++ OpenGL 3.3x GLSL 3.30 CORE Windows 10

Code:

GLuint OpenGL_Engine::SetupViewCamera(GLFWwindow *RenderWindow, glm::vec3 eyepos, glm::vec3 originpos, glm::vec3 viewrotationpos, GLuint program, GLfloat RotAng)
{
    double xmouse = 0.0f, ymouse = 0.0f;
    glfwGetCursorPos(RenderWindow, &xmouse, &ymouse);

    double theta = glm::radians(RotAng);

    glm::vec3 camera(cos(theta), 0.0f, sin(theta));
    camera *= 5;
    glm::mat4 ViewMatrix = glm::lookAt(
    camera, 
    glm::vec3(xmouse/16, ymouse/16, 0.0), 
    glm::vec3(0.0, 1.0, 0));

    glm::mat4 IViewMatrix = glm::inverse(ViewMatrix);

    GLint Viewmatrixloc = glGetUniformLocation(program, "VM");
    glUniformMatrix4fv(Viewmatrixloc, 1, GL_FALSE, &IViewMatrix[0][0]);

    return 80;
}
\$\endgroup\$
-1
\$\begingroup\$

all this code helped me it's some of it's from learn opengl that was the most frustrating thing to fix almost ever apart from my first attempts at a 3d model parser which is now piss to do it's literally just sscanf vertexs , sscanf faces and then use the faces as the index for the vertexes index in a loop of the entire vertexes those vertex[face_index] hand that to your vertexbufferobject do a vao and set the attribpointer and uniforms appropriately and wala "something definitly along those lines" but here is most of the code for a 3d camera and if I missed anything their's the link directly enjoy finally -> http://www.learnopengl.com

void mouse_callback(GLFWwindow* window, double xpos, double ypos);

// Camera
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 0.5f);
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);

GLfloat yaw = -20.0f;   // Yaw is initialized to -90.0 degrees since a yaw of 0.0 results in a direction vector pointing to the right (due to how Euler angles work) so we initially rotate a bit to the left.
GLfloat pitch = 10.0f;
GLfloat lastX = 1280 / 2.0;
GLfloat lastY =  720 / 2.0;

        GLfloat currentFrame = glfwGetTime();
        deltaTime = currentFrame - lastFrame;
        lastFrame = currentFrame;
glfwSetCursorPosCallback(RenderWindow, mouse_callback);
    glfwSetInputMode(RenderWindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED);

glm::mat4 view;
        view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);

if (glfwGetKey(RenderWindow, GLFW_KEY_W) == GLFW_PRESS)
        {
            cameraPos += cameraSpeed * cameraFront;
        }

        if (glfwGetKey(RenderWindow, GLFW_KEY_S) == GLFW_PRESS)
        {
            cameraPos -= cameraSpeed * cameraFront;
        }

        if (glfwGetKey(RenderWindow, GLFW_KEY_A) == GLFW_PRESS)
        {
            cameraPos -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
        }

        if (glfwGetKey(RenderWindow, GLFW_KEY_D) == GLFW_PRESS)
        {
            cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
        }


bool firstMouse = true;
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
    if (firstMouse)
    {
        lastX = xpos;
        lastY = ypos;
        firstMouse = false;
    }

    GLfloat xoffset = xpos - lastX;
    GLfloat yoffset = lastY - ypos; // Reversed since y-coordinates go from bottom to left
    lastX = xpos;
    lastY = ypos;

    GLfloat sensitivity = 0.05; // Change this value to your liking
    xoffset *= sensitivity;
    yoffset *= sensitivity;

    yaw += xoffset;
    pitch += yoffset;

    // Make sure that when pitch is out of bounds, screen doesn't get flipped
    if (pitch > 89.0f)
        pitch = 89.0f;
    if (pitch < -89.0f)
        pitch = -89.0f;

    glm::vec3 front;
    front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
    front.y = sin(glm::radians(pitch));
    front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
    cameraFront = glm::normalize(front);
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.