示例#1
0
    def serialize(self):
        vertices = (c_float * (4 * len(self.verts)))()
        indices = (c_uint * (3 * len(self.faces)))()

        for i, vertex in enumerate(self.verts):
            vertices[i * 4 + 0:i * 4 + 4] = vertex.x, vertex.y, vertex.z, 1.0
            vertex.index = i

        for i, face in enumerate(self.faces):
            indices[i * 3 + 0:i * 3 +
                    3] = face.v1.index, face.v2.index, face.v3.index

        return VBO(
            indices=indices,
            count=len(indices),
            position_4=vertices,
        )
示例#2
0
from gletools import ShaderProgram, VertexObject, Matrix, VBO

window = pyglet.window.Window()
rotation = 0.0

vbo = VBO(
    count=6,
    indices=[0, 1, 2, 0, 2, 3],
    position_4=[
        +1,
        +1,
        0,
        1,
        +1,
        -1,
        0,
        1,
        -1,
        -1,
        0,
        1,
        -1,
        +1,
        0,
        1,
    ],
)

program = ShaderProgram.open(
    'triangles.shader',
    inner_level=8.0,
    outer_level=8.0,
示例#3
0
def make_triangles(width, height, terrain=None):
    position = (c_float * (width * height * 4))()

    s_factor = 1.0 / float(width - 1)
    t_factor = 1.0 / float(height - 1)
    for y in range(height):
        for x in range(width):
            offset = (y * width + x) * 4
            s = float(x) * s_factor
            t = float(y) * t_factor
            if terrain:
                x_tex = int(round(s * float(terrain.width - 1)))
                y_tex = int(round(t * float(terrain.height - 1)))
                h = terrain[x_tex, y_tex][3]
            else:
                h = 0

            position[offset:offset + 4] = s, t, h, 1

    normals = (c_float * (width * height * 3))()

    if terrain:
        for y in range(height):
            for x in range(width):
                offset = (y * width + x) * 3
                s = float(x) * s_factor
                t = float(y) * t_factor

                x_tex = int(round(s * float(terrain.width - 1)))
                y_tex = int(round(t * float(terrain.height - 1)))
                x_shift = int(round(s_factor * float(terrain.width - 1)))
                y_shift = int(round(t_factor * float(terrain.height - 1)))

                up = Point(0.0, 0.0, 1.0)
                h = terrain[x_tex, y_tex][3]

                l = Point(-s_factor, 0.0,
                          terrain[x_tex - x_shift, y_tex][3] - h)
                r = Point(+s_factor, 0.0,
                          terrain[x_tex + x_shift, y_tex][3] - h)
                t = Point(0.0, -t_factor,
                          terrain[x_tex, y_tex - y_shift][3] - h)
                b = Point(0.0, +t_factor,
                          terrain[x_tex, y_tex + y_shift][3] - h)

                normal = (l.cross(Point(0.0, -1.0, 0.0)).normalize() +
                          r.cross(Point(0.0, 1.0, 0.0)).normalize() +
                          t.cross(Point(1.0, 0.0, 0.0)).normalize() + b.cross(
                              Point(-1.0, 0.0, 0.0)).normalize()).normalize()

                normals[offset:offset + 3] = normal.x, normal.y, normal.z

    i_width, i_height = width - 1, height - 1
    indices = (c_uint * (i_width * i_height * 6))()
    for y in range(i_height):
        for x in range(i_width):
            offset = (x + y * i_width) * 6
            p1 = x + y * width
            p2 = p1 + width
            p4 = p1 + 1
            p3 = p2 + 1
            indices[offset:offset + 6] = p1, p2, p3, p1, p3, p4

    return VBO(
        count=len(indices),
        indices=indices,
        position_4=position,
        normal_3=normals,
    )