【问题标题】:PyOpenGL Objects keep rotating around the originPyOpenGL 对象不断围绕原点旋转
【发布时间】:2018-10-24 00:33:30
【问题描述】:

所以我只是在测试我的引擎是如何工作的,并尝试使用以下方法简单地旋转对象。

rot_y = Matrix44.from_y_rotation(glfw.get_time() * 0.5)

但是...它围绕原点而不是原点旋转。

主要代码: 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()

我不确定问题是什么以及为什么它没有围绕它自己的点旋转。

【问题讨论】:

    标签: python opengl pyopengl


    【解决方案1】:

    如果您希望模型围绕网格的原点旋转,那么您必须在模型矩阵之前应用旋转矩阵。首先旋转模型然后平移它。 进一步注意,matrix44.create_from_translation 的返回类型是numpy.array。这意味着您必须先创建一个Matrix44,然后才能将矩阵相乘。

    交换平移和旋转矩阵来解决您的问题:

    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) 
    

    注意矩阵乘法必须从右到左读取。另见GLSL Programming/Vector and Matrix Operations

    translate * rotate:

    rotate * translate:

    【讨论】:

    • 我的东西在原点产生并且变得很奇怪。它变成一个二维对象,然后扩展为一个球体,然后重复
    • @AhmedMerabet 你的顶点着色器是做什么的?
    • layout(location = 0) in vec3 position; layout(location = 1) in vec2 textureCoords; uniform mat4 model; uniform mat4 view; uniform mat4 proj; out vec2 newTexture; void main() { gl_Position = proj * view * model * vec4(position, 1.0f); newTexture = vec2(textureCoords.x, 1 - textureCoords.y); }
    • @AhmedMerabet 它必须是Matrix44(sphere_model) * rot_y - 查看答案的变化
    • @AhmedMerabet * 是为 Matrix44() * Matrix44() 定义的,请参阅 Matricesnumpy.array * Matrix44() 会对单个组件进行乘法运算。
    猜你喜欢
    • 2014-12-28
    • 2015-04-11
    • 1970-01-01
    • 2015-08-15
    • 1970-01-01
    • 2020-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多