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 3 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 = c_void_p(0)
ViewMatrix = glm.mat4(1.0)
ProjectionMatrix = glm.mat4(1.0)
def getViewMatrix():
return ViewMatrix
def getProjectionMatrix():
return ProjectionMatrix
# Initial position : on +Z
position = glm.vec3(0.2,0,2)
# Initial horizontal angle : toward -Z
horizontalAngle =3.14
# Initial vertical angle : none
verticalAngle =0.0
# Initial Field of View
initialFoV =45.0
speed =3.0 # 3 units / second
mouseSpeed =0.002
lastTime = None
def computeMatricesFromInputs(window):
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 = glfw.get_time()
# Compute time difference between current and last frame
currentTime = glfw.get_time()
deltaTime = currentTime - lastTime
# Get mouse position
xpos, ypos = glfw.get_cursor_pos(window)
# Reset mouse position for next frame
glfw.set_cursor_pos(window,1024/2,768/2)
# Compute new orientation
horizontalAngle += mouseSpeed * deltaTime * float(1024/2- xpos)
verticalAngle += mouseSpeed * deltaTime * float(768/2- ypos)
# Direction : Spherical coordinates to Cartesian coordinates conversion
direction = glm.vec3(
mathf.cos(verticalAngle)* mathf.sin(horizontalAngle),
mathf.sin(verticalAngle),
mathf.cos(verticalAngle)* mathf.cos(horizontalAngle)
)
# Right vector
right = glm.vec3(
mathf.sin(horizontalAngle -3.14/2.0),
0,
mathf.cos(horizontalAngle -3.14/2.0)
)
# Up vector
up = glm.cross(right, direction)
# Move forward
if glfw.get_key(window, glfw.KEY_UP)== glfw.PRESS or glfw.get_key(window, glfw.KEY_W)== glfw.PRESS:
position += direction * deltaTime * speed
# Move backward
if glfw.get_key(window, glfw.KEY_DOWN)== glfw.PRESS or glfw.get_key(window, glfw.KEY_S)== glfw.PRESS:
position -= direction * deltaTime * speed
# Strafe right
if glfw.get_key(window, glfw.KEY_RIGHT)== glfw.PRESS or glfw.get_key(window, glfw.KEY_D)== glfw.PRESS:
position += right * deltaTime * speed
# Strafe left
if glfw.get_key(window, glfw.KEY_LEFT)== glfw.PRESS or glfw.get_key(window, glfw.KEY_A)== glfw.PRESS:
position -= right * deltaTime * speed
# Projection matrix : 45 Field of View, 4:3 ratio, display range : 0.1 unit <->100 units
ProjectionMatrix = glm.perspective(initialFoV,4.0/3.0,0.1,100.0)
# Camera matrix
ViewMatrix = glm.lookAt(
position, # Camera is here
position + direction, # and looks here : at the same position, plus "direction"
up # Head is up (set to 0,-1,0 to look upside-down)
)
# 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!