Question: complete and check my code This code needs 3 new functions : * * computeMatricesFromInputs ( ) * * reads the keyboard and mouse and
complete and check my code This code needs new functions :
computeMatricesFromInputs reads the keyboard and mouse and computes the Projection and View matrices. This is where all the magic happens.
getProjectionMatrix just returns the computed Projection matrix.
getViewMatrix just returns the computed View matrix.
# Global window
window None
null cvoidp
ViewMatrix glmmat
ProjectionMatrix glmmat
def getViewMatrix:
return ViewMatrix
def getProjectionMatrix:
return ProjectionMatrix
# Initial position : on Z
position glmvec
# Initial horizontal angle : toward Z
horizontalAngle
# Initial vertical angle : none
verticalAngle
# Initial Field of View
initialFoV
speed # units second
mouseSpeed
lastTime None
def computeMatricesFromInputswindow:
global lastTime
global position
global horizontalAngle
global verticalAngle
global ViewMatrix
global ProjectionMatrix
# glfwGetTime is called only once, the first time this function is called
if lastTime None:
lastTime glfwgettime
# Compute time difference between current and last frame
currentTime glfwgettime
deltaTime currentTime lastTime
# Get mouse position
xpos, ypos glfwgetcursorposwindow
# Reset mouse position for next frame
glfwsetcursorposwindow
# Compute new orientation
horizontalAngle mouseSpeed deltaTime float xpos
verticalAngle mouseSpeed deltaTime float ypos
# Direction : Spherical coordinates to Cartesian coordinates conversion
direction glmvec
mathf.cosverticalAngle mathf.sinhorizontalAngle
mathf.sinverticalAngle
mathf.cosverticalAngle mathf.coshorizontalAngle
# Right vector
right glmvec
mathf.sinhorizontalAngle
mathf.coshorizontalAngle
# Up vector
up glmcrossright direction
# Move forward
if glfwgetkeywindow glfwKEYUP glfwPRESS or glfwgetkeywindow glfwKEYW glfwPRESS:
position direction deltaTime speed
# Move backward
if glfwgetkeywindow glfwKEYDOWN glfwPRESS or glfwgetkeywindow glfwKEYS glfwPRESS:
position direction deltaTime speed
# Strafe right
if glfwgetkeywindow glfwKEYRIGHT glfwPRESS or glfwgetkeywindow glfwKEYD glfwPRESS:
position right deltaTime speed
# Strafe left
if glfwgetkeywindow glfwKEYLEFT glfwPRESS or glfwgetkeywindow glfwKEYA glfwPRESS:
position right deltaTime speed
# Projection matrix : Field of View, : ratio, display range : unit units
ProjectionMatrix glmperspectiveinitialFoV
# Camera matrix
ViewMatrix glmlookAt
position, # Camera is here
position direction, # and looks here : at the same position, plus "direction"
up # Head is up set to to look upsidedown
# For the next frame, the "last time" will be "now"
lastTime currentTime
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
