Reputation: 44
So I was just testing to see how things working in my engine, and tried to rotate an object simply using the following.
rot_y = Matrix44.from_y_rotation(glfw.get_time() * 0.5)
But... It rotates around the origin instead of it's place.
Main code: http://hatebin.com/rtuqgeqptw
projection = pyrr.matrix44.create_perspective_projection_matrix(60.0, w_width/w_height, 0.1, 100.0)
sphere_model = matrix44.create_from_translation(Vector3([-4.0,0.0,-3.0]))
monkey_model = matrix44.create_from_translation(Vector3([0.0,0.0,-3.0]))
glUseProgram(shader)
model_loc = glGetUniformLocation(shader, "model")
view_loc = glGetUniformLocation(shader, "view")
proj_loc = glGetUniformLocation(shader, "proj")
glUniformMatrix4fv(proj_loc, 1, GL_FALSE, projection)
while not glfw.window_should_close(window):
glfw.poll_events()
do_movement()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
view = cam.get_view_matrix()
glUniformMatrix4fv(view_loc, 1, GL_FALSE, view)
rot_y = Matrix44.from_y_rotation(glfw.get_time() * 0.5)
glBindVertexArray(sphere_vao)
glBindTexture(GL_TEXTURE_2D, sphere_tex)
glUniformMatrix4fv(model_loc, 1, GL_FALSE, rot_y *sphere_model)
glDrawArrays(GL_TRIANGLES, 0, len(sphere_obj.vertex_index))
glBindVertexArray(0)
glBindVertexArray(monkey_vao)
glBindTexture(GL_TEXTURE_2D, monkey_tex)
glUniformMatrix4fv(model_loc, 1, GL_FALSE, monkey_model)
glDrawArrays(GL_TRIANGLES, 0, len(monkey_obj.vertex_index))
glBindVertexArray(0)
glfw.swap_buffers(window)
glfw.terminate()
I'm not sure what the problem is and why it's not rotating around it's own point.
Upvotes: 1
Views: 345
Reputation: 211166
If you want that the model rotates around the origin of the mesh, then you have to apply the rotation matrix before the model matrix. First rotate the model then translate it.
Further note, that the return type of matrix44.create_from_translation
is numpy.array
. This means you have to make a Matrix44
first, before you can multiply the matrices.
Swap the translation and the rotation matrix to solve your issue:
sphere_model = matrix44.create_from_translation(Vector3([-4.0,0.0,-3.0]))
rot_y = Matrix44.from_y_rotation(glfw.get_time() * 0.5)
# roation before tranasltion
model_mat = Matrix44(sphere_model) * rot_y
glUniformMatrix4fv(model_loc, 1, GL_FALSE, model_mat)
Note the matrix multiplication has to be read form the right to the left. See also GLSL Programming/Vector and Matrix Operations
translate * rotate
:
rotate * translate
:
Upvotes: 1