- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Python 创建一个“现代 OpenGL”应用程序。具体来说,我使用 pyopengl 来处理 OpenGL 接口(interface),并使用 pygame 来处理窗口生成、上下文、显示等。
我遇到的问题是我无法围绕超过 1 个轴旋转立方体对象。
如果我绕单个轴旋转,那么程序似乎可以按预期运行,但如果我尝试创建绕多个轴的旋转(通过将旋转矩阵相乘,按照 the tutorial - 有一个 github链接到代码 here ),那么我看到的只是一个拉伸(stretch)和翻转的四边形,而不是一个旋转的立方体。
注释:我之前遵循了完全相同的教程,在 Windows 上使用 GLFW 效果很好,但我现在在 ubuntu 上并尝试使用 pygame 而不是 GLFW 来处理上下文。从我在其他教程中看到的情况来看,这应该可以正常工作。也许我错过了什么?
我尝试将代码重新构造为平面的、顺序的脚本,但没有任何区别。我尝试重新排序代码以尽可能匹配教程,但这也不起作用。我还尝试删除顶点数组对象,但这也不起作用。看来我的代码结构不是问题(很高兴能对此进行纠正!),但我遗漏了 OpenGL 方面的一些内容。
如何创建围绕多个轴的所需旋转,以便我可以看到预期的旋转立方体?
预先感谢您提供的任何帮助。
我使用的代码如下:
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()
最佳答案
Pyrr 的 Matrix44
的行为类似于 numpy.array
,并且是使用 Numpy 实现的。
对于 array
数组,*
运算符表示逐元素乘法,而 @
运算符表示矩阵乘法。请参阅 ojit_a 。
表达式
rot = rot_x * rot_y
执行矩阵元素的逐分量乘法。
矩阵乘法的正确运算符是:
rot = rot_x @ rot_y
关于python - 如何使用着色器、pygame 和 pyopengl 在多于 1 个轴上执行 OpenGL 旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58075996/
...沮丧。我希望我的游戏仅在横向模式下运行。我已将适当的键/值添加到 Info.plist 文件中,以强制设备方向在启动时正确。 我现在正在尝试旋转 OpenGL 坐标空间以匹配设备的坐标空间。我正
我如何创建一个旋转矩阵,将 X 旋转 a,Y 旋转 b,Z 旋转 c? 我需要公式,除非您使用的是 ardor3d api 的函数/方法。 矩阵是这样设置的 xx, xy, xz, yx, yy, y
假设我有一个包含 3 个 vector 的类(一个用于位置,一个用于缩放,一个用于旋转)我可以使用它们生成一个变换矩阵,该矩阵表示对象在 3D 空间中的位置、旋转和大小。然后我添加对象之间的父/子关系
所以我只是在玩一个小的 javascript 游戏,构建一个 pacman 游戏。你可以在这里看到它:http://codepen.io/acha5066/pen/rOyaPW 不过我对旋转有疑问。你
在我的应用程序中,我有一个 MKMapView,其中显示了多个注释。 map 根据设备的航向旋转。要旋转 map ,请执行以下语句(由方法 locationManager 调用:didUpdateHe
使用此 jquery 插件时:http://code.google.com/p/jqueryrotate/wiki/Documentation我将图像旋转 90 度,无论哪个方向,它们最终都会变得模糊
我有以下代码:CSS: .wrapper { margin:80px auto; width:300px; border:none; } .square { widt
我只想通过小部件的轴移动图像并围绕小部件的中心旋转(就像任何数字绘画软件中的 Canvas ),但它围绕其左顶点旋转...... QPainter p(this); QTransform trans;
我需要先旋转图像,然后再将其加载到 Canvas 中。据我所知,我无法使用 canvas.rotate() 旋转它,因为它会旋转整个场景。 有没有好的JS方法来旋转图片? [不依赖于浏览器的方式] 最
我需要知道我的 Android 设备屏幕何时从一个横向旋转到另一个横向(rotation_90 到 rotation_270)。在我的 Android 服务中,我重新实现了 onConfigurati
**摘要:**本篇文章主要讲解Python调用OpenCV实现图像位移操作、旋转和翻转效果,包括四部分知识:图像缩放、图像旋转、图像翻转、图像平移。 本文分享自华为云社区《[Python图像处理] 六
我只是在玩MTKView中的模板设置;并且,我一直在尝试了解以下内容: 相机的默认位置。 使用MDLMesh和MTKMesh创建基元时的默认位置。 为什么轮换还涉及翻译。 相关代码: matrix_f
我正在尝试使用包 dendexend 创建一个树状图。它创建了非常好的 gg 树状图,但不幸的是,当你把它变成一个“圆圈”时,标签跟不上。我将在下面提供一个示例。 我的距离对象在这里:http://s
我想将一个完整的 ggplot 对象旋转 90°。 我不想使用 coord_flip因为这似乎会干扰 scale="free"和 space="free"使用刻面时。 例如: qplot(as.fac
我目前可以通过首先平移到轴心点然后执行旋转最后平移回原点来围绕轴心点旋转。在我的例子中,我很容易为肩膀做到这一点。但是,我不知道如何为前臂添加绕肘部的旋转。 我已经尝试了以下围绕肘部旋转的前臂: 平移
我想使用此功能旋转然后停止在特定点或角度。现在该元素只是旋转而不停止。代码如下: $(function() { var $elie = $("#bkgimg");
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 4 年前。 Improve this ques
我正在尝试创建一个非常简单的关键帧动画,其中图形通过给定的中点从一个角度旋转到另一个角度。 (目的是能够通过大于 180 度的 OBTUSE 弧角来制作旋转动画,而不是让动画“作弊”并走最短路线,即通
我需要旋转 NSView 实例的框架,使其宽度变为其高度,其高度变为其宽度。该 View 包含一个字符串,并且该字符串也被旋转,这一点很重要。 我查看了 NSView 的 setFrameRotati
我正在编写一个脚本,用于在 javascript 中旋转/循环浏览图像,同时遵守循环浏览图像的次数限制。我所拥有的如下: var delay = 3000; //6000 = change to
我是一名优秀的程序员,十分优秀!