Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions common/controls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ glm::mat4 getProjectionMatrix(){


// Initial position : on +Z
glm::vec3 position = glm::vec3( 0, 0, 5 );
glm::vec3 position = glm::vec3( 0, 0, 5 );
// Initial horizontal angle : toward -Z
float horizontalAngle = 3.14f;
// Initial vertical angle : none
Expand All @@ -45,29 +45,45 @@ void computeMatricesFromInputs(){

// Get mouse position
double xpos, ypos;
glfwGetCursorPos(window, &xpos, &ypos);

// for some reason, on linux, the camera ends up in a random location
// because of glfwGetCursorPos glfwSetCursorPos, whereas on Windows
// it works just fine.
// To make the camera work consistently across platforms, set
// the cursor location on first rendering, and only compute the
// offset on subsequent renderings.
static bool firstPass = true;
if(!firstPass){
glfwGetCursorPos(window, &xpos, &ypos);
}

// Reset mouse position for next frame
glfwSetCursorPos(window, 1024/2, 768/2);

// Compute new orientation
horizontalAngle += mouseSpeed * float(1024/2 - xpos );
verticalAngle += mouseSpeed * float( 768/2 - ypos );
// Compute new orientation after the first frame
if(!firstPass){
// Compute new orientation
horizontalAngle += mouseSpeed * float(1024/2 - xpos );
verticalAngle += mouseSpeed * float( 768/2 - ypos );
}
else {
firstPass = false;
}

// Direction : Spherical coordinates to Cartesian coordinates conversion
glm::vec3 direction(
cos(verticalAngle) * sin(horizontalAngle),
cos(verticalAngle) * sin(horizontalAngle),
sin(verticalAngle),
cos(verticalAngle) * cos(horizontalAngle)
);

// Right vector
glm::vec3 right = glm::vec3(
sin(horizontalAngle - 3.14f/2.0f),
sin(horizontalAngle - 3.14f/2.0f),
0,
cos(horizontalAngle - 3.14f/2.0f)
);

// Up vector
glm::vec3 up = glm::cross( right, direction );

Expand Down Expand Up @@ -101,4 +117,4 @@ void computeMatricesFromInputs(){

// For the next frame, the "last time" will be "now"
lastTime = currentTime;
}
}