如何使用着色器、pygame 和 pyopengl 在多于 1 个轴上执行 OpenGL 旋转
How to perform OpenGL rotation in more than 1 axis using shaders, pygame, and pyopengl
我正在尝试使用 Python 创建一个 "modern OpenGL" 应用程序。具体来说,我正在使用 pyopengl 来处理 OpenGL 接口,我正在使用 pygame 来处理 window 生成、上下文、显示等
我遇到的问题是我无法围绕超过 1 个轴旋转立方体对象。
如果我围绕一个轴旋转,那么程序似乎按预期运行,但如果我尝试创建一个围绕多个轴的旋转(通过将旋转矩阵相乘,根据 the tutorial - there's a github link to the code here ), 然后我看到的只是一个可以拉伸和翻转的四边形,而不是一个旋转的立方体。
注意:在 Windows 上使用 GLFW 之前我已经遵循了完全相同的教程,但我现在在 ubuntu 上并尝试使用 pygame 而不是 GLFW 来处理上下文。从我在其他教程中看到的情况来看,这应该可以正常工作。也许我遗漏了什么?
我曾尝试将代码重新构建为扁平的顺序脚本,但没有任何区别。我尝试重新排序代码以尽可能匹配教程,但这也没有用。我也试过删除顶点数组对象,但这也没有用。看来我的代码结构不是问题(很高兴得到纠正!),但我在 OpenGL 方面遗漏了一些东西。
如何围绕 1 个以上的轴创建所需的旋转,以便我可以看到预期的旋转立方体?
在此先感谢您提供的任何帮助。
我使用的代码如下:
from OpenGL.GL import *
import OpenGL.GL.shaders
import ctypes
import pygame
import numpy
import pyrr
vertex_shader = """
#version 130
in vec4 position;
in vec4 colour;
uniform mat4 transformation;
out vec4 newColour;
void main()
{
gl_Position = transformation * position;
newColour = colour;
}
"""
fragment_shader = """
#version 130
in vec4 newColour;
out vec4 outColour;
void main()
{
outColour = newColour;
}
"""
vertices = [
-0.5, -0.5, 0.5, 1.0, 1.0, 0.0, 0.0, 1.0,
0.5, -0.5, 0.5, 1.0, 1.0, 1.0, 0.0, 1.0,
0.5, 0.5, 0.5, 1.0, 1.0, 1.0, 1.0, 1.0,
-0.5, 0.5, 0.5, 1.0, 1.0, 0.0, 1.0, 1.0,
-0.5, -0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 1.0,
0.5, -0.5, -0.5, 1.0, 0.0, 1.0, 0.0, 1.0,
0.5, 0.5, -0.5, 1.0, 0.0, 1.0, 1.0, 1.0,
-0.5, 0.5, -0.5, 1.0, 0.0, 0.0, 1.0, 1.0
]
vertices = numpy.array(vertices, dtype=numpy.float32)
indices = [0, 1, 2, 0, 2, 3, # front
5, 4, 7, 5, 7, 6, # back 7b 6c
3, 2, 7, 7, 2, 6, # top 3m 2w
2, 1, 5, 2, 5, 6, # right
1, 0, 5, 5, 0, 4, # bottom 4k 5g
3, 7, 4, 3, 4, 0 # left 0r 1y
]
indices = numpy.array(indices, dtype=numpy.uint32)
def create_object(shader):
# Create a new VAO (Vertex Array Object) and bind it
vertex_array_object = glGenVertexArrays(1)
glBindVertexArray(vertex_array_object)
# Generate buffers to hold our vertices
vertex_buffer = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer)
# Generate buffers to hold buffer indices
element_buffer = glGenBuffers(1)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, element_buffer)
# Get the position of the 'position' in parameter of our shader and bind it.
position = glGetAttribLocation(shader, 'position')
glEnableVertexAttribArray(position)
# Describe the position data layout in the buffer
glVertexAttribPointer(position, 4, GL_FLOAT, False, 32, ctypes.c_void_p(0))
# Get the position of the 'colour' in parameter of our shader and bind it.
colour = glGetAttribLocation(shader, 'colour')
glEnableVertexAttribArray(colour)
# Describe the colour data layout in the buffer
glVertexAttribPointer(colour, 4, GL_FLOAT, False, 32, ctypes.c_void_p(16))
# Send the data over to the buffers
glBufferData(GL_ARRAY_BUFFER, 256, vertices, GL_STATIC_DRAW) # Vertices array
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 144, indices, GL_STATIC_DRAW) # Indices array
# Unbind the VAO first (Important)
glBindVertexArray(0)
# Unbind other stuff
glDisableVertexAttribArray(position)
glBindBuffer(GL_ARRAY_BUFFER, 0)
return vertex_array_object
def display(shader, vertex_array_object):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glUseProgram(shader)
rot_x = pyrr.matrix44.create_from_x_rotation(0.5 * pygame.time.get_ticks() / 1000, dtype=numpy.float32)
rot_y = pyrr.matrix44.create_from_y_rotation(0.8 * pygame.time.get_ticks() / 1000, dtype=numpy.float32)
rot = rot_x * rot_y
transform_location = glGetUniformLocation(shader, 'transformation')
glUniformMatrix4fv(transform_location, 1, GL_FALSE, rot) # change final argument to rot_x for single axis rotation
glBindVertexArray(vertex_array_object)
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, None)
glBindVertexArray(0)
glUseProgram(0)
def main():
display_width = 512
display_height = 512
pygame.init()
pygame.display.set_mode((display_width, display_height), pygame.OPENGL | pygame.DOUBLEBUF)
glClearColor(0.0, 0.0, 0.1, 1.0)
glEnable(GL_DEPTH_TEST)
# glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
shader = OpenGL.GL.shaders.compileProgram(
OpenGL.GL.shaders.compileShader(vertex_shader, GL_VERTEX_SHADER),
OpenGL.GL.shaders.compileShader(fragment_shader, GL_FRAGMENT_SHADER)
)
vertex_array_object = create_object(shader)
clock = pygame.time.Clock()
while True:
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
if event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE:
return
display(shader, vertex_array_object)
pygame.display.set_caption("FPS: %.2f" % clock.get_fps())
pygame.display.flip()
if __name__ == '__main__':
try:
main()
finally:
pygame.quit()
皮尔的 Matrix44
behaves like and is implemented using numpy.array
.
对于Numpy arrays the *
operator means element-wise multiplication, while the @
operator means matrix multiplication. See array
。
表达式
rot = rot_x * rot_y
执行矩阵元素的分量乘法。
矩阵乘法的正确运算符是:
rot = rot_x @ rot_y
除了上一个答案,您还可以使用pyrr的矩阵乘法函数。
rot = pyrr.matrix44.multiply(rot_x, rot_y)
我正在尝试使用 Python 创建一个 "modern OpenGL" 应用程序。具体来说,我正在使用 pyopengl 来处理 OpenGL 接口,我正在使用 pygame 来处理 window 生成、上下文、显示等
我遇到的问题是我无法围绕超过 1 个轴旋转立方体对象。
如果我围绕一个轴旋转,那么程序似乎按预期运行,但如果我尝试创建一个围绕多个轴的旋转(通过将旋转矩阵相乘,根据 the tutorial - there's a github link to the code here ), 然后我看到的只是一个可以拉伸和翻转的四边形,而不是一个旋转的立方体。
注意:在 Windows 上使用 GLFW 之前我已经遵循了完全相同的教程,但我现在在 ubuntu 上并尝试使用 pygame 而不是 GLFW 来处理上下文。从我在其他教程中看到的情况来看,这应该可以正常工作。也许我遗漏了什么?
我曾尝试将代码重新构建为扁平的顺序脚本,但没有任何区别。我尝试重新排序代码以尽可能匹配教程,但这也没有用。我也试过删除顶点数组对象,但这也没有用。看来我的代码结构不是问题(很高兴得到纠正!),但我在 OpenGL 方面遗漏了一些东西。
如何围绕 1 个以上的轴创建所需的旋转,以便我可以看到预期的旋转立方体?
在此先感谢您提供的任何帮助。
我使用的代码如下:
from OpenGL.GL import *
import OpenGL.GL.shaders
import ctypes
import pygame
import numpy
import pyrr
vertex_shader = """
#version 130
in vec4 position;
in vec4 colour;
uniform mat4 transformation;
out vec4 newColour;
void main()
{
gl_Position = transformation * position;
newColour = colour;
}
"""
fragment_shader = """
#version 130
in vec4 newColour;
out vec4 outColour;
void main()
{
outColour = newColour;
}
"""
vertices = [
-0.5, -0.5, 0.5, 1.0, 1.0, 0.0, 0.0, 1.0,
0.5, -0.5, 0.5, 1.0, 1.0, 1.0, 0.0, 1.0,
0.5, 0.5, 0.5, 1.0, 1.0, 1.0, 1.0, 1.0,
-0.5, 0.5, 0.5, 1.0, 1.0, 0.0, 1.0, 1.0,
-0.5, -0.5, -0.5, 1.0, 0.0, 0.0, 0.0, 1.0,
0.5, -0.5, -0.5, 1.0, 0.0, 1.0, 0.0, 1.0,
0.5, 0.5, -0.5, 1.0, 0.0, 1.0, 1.0, 1.0,
-0.5, 0.5, -0.5, 1.0, 0.0, 0.0, 1.0, 1.0
]
vertices = numpy.array(vertices, dtype=numpy.float32)
indices = [0, 1, 2, 0, 2, 3, # front
5, 4, 7, 5, 7, 6, # back 7b 6c
3, 2, 7, 7, 2, 6, # top 3m 2w
2, 1, 5, 2, 5, 6, # right
1, 0, 5, 5, 0, 4, # bottom 4k 5g
3, 7, 4, 3, 4, 0 # left 0r 1y
]
indices = numpy.array(indices, dtype=numpy.uint32)
def create_object(shader):
# Create a new VAO (Vertex Array Object) and bind it
vertex_array_object = glGenVertexArrays(1)
glBindVertexArray(vertex_array_object)
# Generate buffers to hold our vertices
vertex_buffer = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer)
# Generate buffers to hold buffer indices
element_buffer = glGenBuffers(1)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, element_buffer)
# Get the position of the 'position' in parameter of our shader and bind it.
position = glGetAttribLocation(shader, 'position')
glEnableVertexAttribArray(position)
# Describe the position data layout in the buffer
glVertexAttribPointer(position, 4, GL_FLOAT, False, 32, ctypes.c_void_p(0))
# Get the position of the 'colour' in parameter of our shader and bind it.
colour = glGetAttribLocation(shader, 'colour')
glEnableVertexAttribArray(colour)
# Describe the colour data layout in the buffer
glVertexAttribPointer(colour, 4, GL_FLOAT, False, 32, ctypes.c_void_p(16))
# Send the data over to the buffers
glBufferData(GL_ARRAY_BUFFER, 256, vertices, GL_STATIC_DRAW) # Vertices array
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 144, indices, GL_STATIC_DRAW) # Indices array
# Unbind the VAO first (Important)
glBindVertexArray(0)
# Unbind other stuff
glDisableVertexAttribArray(position)
glBindBuffer(GL_ARRAY_BUFFER, 0)
return vertex_array_object
def display(shader, vertex_array_object):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glUseProgram(shader)
rot_x = pyrr.matrix44.create_from_x_rotation(0.5 * pygame.time.get_ticks() / 1000, dtype=numpy.float32)
rot_y = pyrr.matrix44.create_from_y_rotation(0.8 * pygame.time.get_ticks() / 1000, dtype=numpy.float32)
rot = rot_x * rot_y
transform_location = glGetUniformLocation(shader, 'transformation')
glUniformMatrix4fv(transform_location, 1, GL_FALSE, rot) # change final argument to rot_x for single axis rotation
glBindVertexArray(vertex_array_object)
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, None)
glBindVertexArray(0)
glUseProgram(0)
def main():
display_width = 512
display_height = 512
pygame.init()
pygame.display.set_mode((display_width, display_height), pygame.OPENGL | pygame.DOUBLEBUF)
glClearColor(0.0, 0.0, 0.1, 1.0)
glEnable(GL_DEPTH_TEST)
# glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
shader = OpenGL.GL.shaders.compileProgram(
OpenGL.GL.shaders.compileShader(vertex_shader, GL_VERTEX_SHADER),
OpenGL.GL.shaders.compileShader(fragment_shader, GL_FRAGMENT_SHADER)
)
vertex_array_object = create_object(shader)
clock = pygame.time.Clock()
while True:
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
if event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE:
return
display(shader, vertex_array_object)
pygame.display.set_caption("FPS: %.2f" % clock.get_fps())
pygame.display.flip()
if __name__ == '__main__':
try:
main()
finally:
pygame.quit()
皮尔的 Matrix44
behaves like and is implemented using numpy.array
.
对于Numpy arrays the *
operator means element-wise multiplication, while the @
operator means matrix multiplication. See array
。
表达式
rot = rot_x * rot_y
执行矩阵元素的分量乘法。
矩阵乘法的正确运算符是:
rot = rot_x @ rot_y
除了上一个答案,您还可以使用pyrr的矩阵乘法函数。
rot = pyrr.matrix44.multiply(rot_x, rot_y)