示例#1
0
def create4VertexColor(p1, p2, p3, p4, r, g, b):
    """
    Creates a 4-vertex poly with color.

    :param p1: Vertex (x,y,z)
    :param p2: Vertex (x,y,z)
    :param p3: Vertex (x,y,z)
    :param p4: Vertex (x,y,z)
    :param r: Red color
    :param g: Green color
    :param b: Blue color
    :return:
    """
    # Extend
    p1 = __vertexUnpack3(p1)
    p2 = __vertexUnpack3(p2)
    p3 = __vertexUnpack3(p3)
    p4 = __vertexUnpack3(p4)

    # Dissamble vertices
    x1, y1, z1 = p1
    x2, y2, z2 = p2
    x3, y3, z3 = p3
    x4, y4, z4 = p4

    # Defining locations and color
    vertices = [
        # X, Y,  Z, R, G, B,
        x1,
        y1,
        z1,
        r,
        g,
        b,
        x2,
        y2,
        z2,
        r,
        g,
        b,
        x3,
        y3,
        z3,
        r,
        g,
        b,
        x4,
        y4,
        z4,
        r,
        g,
        b
    ]

    # Defining connections among vertices
    # We have a triangle every 3 indices specified
    indices = [0, 1, 2, 2, 3, 0]

    return _Shape(vertices, indices)
示例#2
0
def readOBJ2(filename, texfilename):

    vertices = []
    normals = []
    texCoords = []
    faces = []

    with open(filename, 'r') as file:
        for line in file.readlines():
            aux = line.strip().split(' ')

            if aux[0] == 'v':
                vertices += [[float(coord) for coord in aux[1:]]]

            elif aux[0] == 'vn':
                normals += [[float(coord) for coord in aux[1:]]]

            elif aux[0] == 'vt':
                assert len(
                    aux[1:]
                ) == 2, "Texture coordinates with different than 2 dimensions are not supported"
                texCoords += [[float(coord) for coord in aux[1:]]]

            elif aux[0] == 'f':
                N = len(aux)
                faces += [[
                    readFaceVertex(faceVertex) for faceVertex in aux[1:4]
                ]]
                for i in range(3, N - 1):
                    faces += [[
                        readFaceVertex(faceVertex)
                        for faceVertex in [aux[i], aux[i + 1], aux[1]]
                    ]]

        vertexData = []
        indices = []
        index = 0

        # Per previous construction, each face is a triangle
        for face in faces:

            # Checking each of the triangle vertices
            for i in range(0, 3):
                vertex = vertices[face[i][0] - 1]
                texture = texCoords[face[i][1] - 1]
                normal = normals[face[i][2] - 1]

                vertexData += [
                    vertex[0], vertex[1], vertex[2], texture[0], texture[1],
                    normal[0], normal[1], normal[2]
                ]

            # Connecting the 3 vertices to create a triangle
            indices += [index, index + 1, index + 2]
            index += 3

        return _Shape(vertexData, indices, texfilename)
示例#3
0
def createTriangleTextureNormal(image_filename, p1, p2, p3, nx=1, ny=1):
    """
    Creates a triangle with textures.

    :param image_filename: Image
    :param p1: Vertex (x,y,z)
    :param p2: Vertex (x,y,z)
    :param p3: Vertex (x,y,z)
    :param nx: Texture coord pos
    :param ny: Texture coord pos
    :return:
    """
    # Dissamble vertices
    x1, y1, z1 = p1
    x2, y2, z2 = p2
    x3, y3, z3 = p3

    # Calculate the normal
    normal = _normal3(p3, p2, p1)

    # Defining locations and texture coordinates for each vertex of the shape
    vertices = [
        # X, Y,  Z,   U,   V
        x1,
        y1,
        z1,
        (nx + ny) / 2,
        nx,
        normal.get_x(),
        normal.get_y(),
        normal.get_z(),
        x2,
        y2,
        z2,
        0.0,
        0.0,
        normal.get_x(),
        normal.get_y(),
        normal.get_z(),
        x3,
        y3,
        z3,
        ny,
        0.0,
        normal.get_x(),
        normal.get_y(),
        normal.get_z()
    ]

    # Defining connections among vertices
    # We have a triangle every 3 indices specified
    indices = [0, 1, 2]

    return _Shape(vertices, indices, image_filename)
示例#4
0
def create4VertexTextureNormal(image_filename, p1, p2, p3, p4, nx=1, ny=1):
    """
    Creates a 4-vertex poly with texture.

    :param image_filename: Image
    :param p1: Vertex (x,y,z)
    :param p2: Vertex (x,y,z)
    :param p3: Vertex (x,y,z)
    :param p4: Vertex (x,y,z)
    :param nx: Texture coord pos
    :param ny: Texture coord pos
    :return:
    """
    # Extend
    p1 = __vertexUnpack3(p1)
    p2 = __vertexUnpack3(p2)
    p3 = __vertexUnpack3(p3)
    p4 = __vertexUnpack3(p4)

    # Dissamble vertices
    x1, y1, z1 = p1
    x2, y2, z2 = p2
    x3, y3, z3 = p3
    x4, y4, z4 = p4

    # Calculate the normal
    normal = _normal3(p3, p2, p1)

    # Defining locations and texture coordinates for each vertex of the shape
    vertices = [
        x1, y1, z1, 0, 0,
        normal.get_x(),
        normal.get_y(),
        normal.get_z(), x2, y2, z2, nx, 0,
        normal.get_x(),
        normal.get_y(),
        normal.get_z(), x3, y3, z3, nx, ny,
        normal.get_x(),
        normal.get_y(),
        normal.get_z(), x4, y4, z4, 0, ny,
        normal.get_x(),
        normal.get_y(),
        normal.get_z()
    ]

    # Defining connections among vertices
    # We have a triangle every 3 indices specified
    indices = [0, 1, 2, 2, 3, 0]

    return _Shape(vertices, indices, image_filename)
示例#5
0
def createTriangleColorNormal(p1, p2, p3, r, g, b):
    """
    Creates a triangle with color.

    :param p1: Vertex (x,y,z)
    :param p2: Vertex (x,y,z)
    :param p3: Vertex (x,y,z)
    :param r: Red color
    :param g: Green color
    :param b: Blue color
    :return:
    """
    # Extend
    p1 = __vertexUnpack3(p1)
    p2 = __vertexUnpack3(p2)
    p3 = __vertexUnpack3(p3)

    # Dissamble vertices
    x1, y1, z1 = p1
    x2, y2, z2 = p2
    x3, y3, z3 = p3

    # Calculate the normal
    normal = _normal3(p3, p2, p1)

    # Defining locations and color
    vertices = [
        # X, Y,  Z, R, G, B,
        x1,
        y1,
        z1,
        r,
        g,
        b,
        normal.get_x(),
        normal.get_y(),
        normal.get_z(),
        x2,
        y2,
        z2,
        r,
        g,
        b,
        normal.get_x(),
        normal.get_y(),
        normal.get_z(),
        x3,
        y3,
        z3,
        r,
        g,
        b,
        normal.get_x(),
        normal.get_y(),
        normal.get_z()
    ]

    # Defining connections among vertices
    # We have a triangle every 3 indices specified
    indices = [0, 1, 2]

    return _Shape(vertices, indices)