python - 小狗。如何动态更改顶点的图片(动画)。 OpenGL

标签 python python-3.x opengl 3d pyglet

环境:

python :3.6.6
小狗版本:1.3.2

代码库:

抽象模型.py

import pyglet


def get_texture_group(file, order_group_index):
    texture = pyglet.image.load(file).texture
    order_group = pyglet.graphics.OrderedGroup(order_group_index)
    return pyglet.graphics.TextureGroup(texture, order_group)


class AbstractModel(object):

    def _create_as_vertex(self):
        v_x = self.cell_data.get("x") * 32
        v_y = self.cell_data.get("y") * -1 * 32

        texture_group = self.map_type_iamge.get(self.cell_data.get("t"))
        x_offset = self.x_offset * self.scale

        x, y, z = v_x + x_offset, v_y, self.z
        x_ = (texture_group.texture.width * self.scale + x_offset + v_x)
        y_ = (texture_group.texture.height * self.scale + v_y)

        tex_coords = ('t2f', (0, 0, 1, 0, 1, 1, 0, 1))

        self.vertices = self.batch.add(
            4, pyglet.gl.GL_QUADS,
            texture_group,
            ('v3f', (x, y, z,
                     x_, y, z,
                     x_, y_, z,
                     x, y_, z)),
            tex_coords)

    def _animate(self, dt):
        # lets assume that I have list of pyglet.graphics.TextureGroup
        # and they should somehow be drawn one after other
        print("I need change image. dt=", dt, self)
        pyglet.clock.schedule_once(self._animate, 1)

ground3d.py

import os
import pyglet

import settings
from models import abstract_model


GROUND_DIR = os.path.join(settings.STATIC_DIR, "ground")

order_group_index = 0

map_type_iamge = {
    1: abstract_model.get_texture_group(os.path.join(GROUND_DIR, "w1.png"), order_group_index),
    2: abstract_model.get_texture_group(os.path.join(GROUND_DIR, "t1.png"), order_group_index),
    1001: abstract_model.get_texture_group(os.path.join(GROUND_DIR, "t1_direction.png"), order_group_index),
}


class Ground3D(abstract_model.AbstractModel):

    def __init__(self, cell_data, batch):

        self.batch = batch
        self.cell_data = cell_data
        self.map_type_iamge = map_type_iamge
        self.scale = 1
        self.x_offset = 0
        self.z = 0
        self.entity = None

        self._create_as_vertex()
        pyglet.clock.schedule_once(self._animate, 1)

说明:

我有模型(例如平面矩形)应该放置在 3 个维度上。并且这些模型应该是动画的,比如 picture_1,在第二个 picture_2 之后,......等等。
据我了解 previous question在 3D 批处理中使用 pyglet.sprite.Sprite() 不是一个好主意。

问题:

如何在 self.vertices 上更改图片(使用 TextureGroup 或任何其他方法)?

或者我使用哪些 arroach/classes 来实现它。对于 3 维平面模型的动画,我找不到任何此类(就我的简单愿景而言)常见情况的示例。

有很多关于 vertices 旋转/移动/调整大小的示例,但是如何构建正确的问题(动画方面)以便在 google 中获得答案 - 我不知道。

附注:读者,如果您有关于此主题的任何有用链接(针对 pyglet 或仅针对 OpenGL),我将非常感谢您分享此链接(s ) 在评论中。

最佳答案

纹理坐标。

你应该有一个单一的纹理图集,用于所有动画的所有不同事物的所有帧。

最好,一切都应该始终具有相同的动画速度和相同的帧数。

假设有两个 Sprite ,整个动画有 2 帧,它们存储在 64x64 纹理图集中。 (编辑:抱歉含糊不清,64x64 PIXELS,只是因为它可能暗示我们有 64x64 tile atlas,在我提到这个的其他地方都一样)

现在,您需要一个具有全局值的全局计时器,它指示当前动画帧,而不是游戏帧。它应该独立于帧率。

上述值应该按照您希望的速度每隔一段时间更新一次,如下所示:

current_frame = (current_frame + 1) % animation_length

因为我们在这个例子中有 2 帧,所以它会变成这样:

# init
animation_length = 2
current_frame = 0

# updates:
current_frame = (0 + 1) % 2 # 1 % 2 -> 1
current_frame = (1 + 1) % 2 # 2 % 2 -> 0
...

现在,您只需要在帧发生变化时更新所有 Sprite 的 UV。

UV 从左到右开始,从 0 到 1(据我所知,为了这个例子,它们是这样做的,嘘)。

因为我们每个都有 2 帧,所以我们可以像这样计算 UV 坐标中的“图 block ”:

tile_width = 1.0 / frames # 2 frames each, width will be 0.5
tile_height = 1.0 / sprites # 2 sprites each, height will be 0.5 too, perfect

现在,在第一帧,你像平常一样生成你的 UV,你只需要垂直 ID 或其他东西,然后使用 tile_height * sprite_id 来获取当前的 V 坐标,然后您的 U 的计算方式类似于 tile_width * current_frame

这假设您已经有了 sprite 批处理,所以您所做的是在更新时遍历每个 sprite,基本上只是用新帧重新计算新 UV,这意味着所有 sprite 都将它们的帧更改为下一个,耶!

如果你想拥有彼此独立的系统,比如说,一些动画非常慢,而另一些则更快,你需要不同的 Sprite 批处理或适当的计算从哪里到哪里你需要更新顶点中的 UV缓冲区数组。其他一切都完全相同,除了现在 current_frame 不是全局的而是包含的,最好是在管理动画计时器的某个列表或单独的对象中。

您不需要更改着色器中的任何内容,它们只需要为帧设置正确的 UV,您就可以设置了。

顺便说一下,这是非常基础的,您可以自己应用一些逻辑,这样您就可以在纹理中使用 16x16 的 32x32 像素网格,每一行 Sprite 都有 4 种不同的动画,这些可以是 Sprite 的状态(攻击,运行等),但如何操作完全取决于您,最重要的是,让它发挥作用。祝你好运。

但是如果按照我说的那样去做,那么state会是另外一个整数,而UV是state,假设所有的state的宽度完全一样,那就是这样的:

state_width = 1 / states
tile_width = 1 / (states * frames_per_state)
U = state_width * current_state + tile_width * current_frame

现在出现了一个问题,玩家可以在最后一个攻击帧开始他的动画。

这是正常的,具有 Action 的实体都应该有单独的计时器,我上面描述的是用于大量只是背景的 Sprite ,比如草。现在,当您将其分开时,您可以通过适当的方式在分配新状态时将当前帧重置为 0。

如果您的实体是对象,您可以编写适当的方法,在每次使用这些 Sprite 重建 Sprite 批处理时重新计算 UV,然后计时器本身可以包含在对象中。

我们需要画点什么?检查动画状态,它改变了吗?发送之前计算好的UV,否则,稍等一下,我们需要重新计算,然后才将它们添加到VBO,好吧,渲染你的东西,最后,它看起来就像你有动画一样,即使真的,它是只是一个简单但很棒的 UV 操作。

祝你好运。

关于python - 小狗。如何动态更改顶点的图片(动画)。 OpenGL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52843381/

相关文章:

Python - pcolor 的 Pick_Event 获取 pandas 列和索引值

python - 加速 n-gram 处理

Python 在带有 numpy 数组数据的 for 循环中附加一个列表

c++ - Mac 上的自制 GLFW/GLEW

python - "register machine"到底是什么?

python - 如何使用键盘中断完全停止脚本

python - 如何找到孤立的顶点?

python倒计时器问题(不起作用)

opengl - 处理大量图像

javascript - WebGL 中高效的 VBO 分配