Beispiel #1
0
    def generate(self):

        # Number of divisions is equal to 2^n + 1
        # n0 - 2
        # n1 - 3
        # n2 - 5
        # n3 - 9

        max_points = 2 ** self.divs + 1



        edges = [self.final_edges[(self.face[0], self.face[1])], \
                 # Note, this edge is backwards
                self.final_edges[(self.face[2], self.face[1])],\
                self.final_edges[(self.face[2], self.face[0])]]

        surface = generator.generate_surface(self.divs, edges, self.roughness, self.main_displacement)

        verts = []
        colors = []
        for i in range(0, max_points - 1):
            # Create a counter k, this is used in place of j when we draw the triangle at the end of the row
            k = 0
            for j in range(max_points - i - 2):
                # Add both triangle in the square to the render batch
                #  ______
                # |\     |          |\
                # | \    |          | \
                # |  \ 2 |          |  \
                # | 1 \  |          | 3 \
                # |    \ |          |    \
                # |_____\| ........ |_____\

                # Triangle 1
                point1 = surface[i][j]
                point2 = surface[i][j + 1]
                point3 = surface[i + 1][j]
                verts += point1 + point2 + point3
                colors += get_color(point1) + get_color(point2) + get_color(point3)
                # Triangle 2
                point1 = surface[i][j + 1]
                point2 = surface[i + 1][j + 1]
                point3 = surface[i + 1][j]
                verts += point1 + point2 + point3
                colors += get_color(point1) + get_color(point2) + get_color(point3)
                k += 1

            # Add remaining triangle at the end of the row, Triangle 3
            point1 = surface[i][k]
            point2 = surface[i][k + 1]
            point3 = surface[i + 1][k]

            verts += point1 + point2 + point3
            colors += get_color(point1) + get_color(point2) + get_color(point3)

        self.verts = verts
        self.colors = colors
Beispiel #2
0
    def __init__(self):
        self.throttle = 500
        self.icosahedron = generator.generate_icosahedron()

        self.ocean_batch = pyglet.graphics.Batch()
        self.cloud_batch = pyglet.graphics.Batch()


        self.sun_batch = pyglet.graphics.Batch()

        self.chunks = {}

        self.queue = Queue.Queue()

        self.background_stars = stars.Stars()

        divs = 6
        main_displacement = 0.3

        roughness = 1.0

        # First apply random steps to each vertex
        new_icosahedron = self.icosahedron
        final_edges = generator.generate_edges(new_icosahedron, divs, main_displacement, roughness)

        faces = generator.get_icosahedron_faces()

        ocean_icosahedron = generator.generate_icosahedron()


        new_ocean = []
        for face in faces:
            # Not the most elegant solution
            a = ocean_icosahedron[face[0]]
            b = ocean_icosahedron[face[1]]
            c = ocean_icosahedron[face[2]]

            surface = generator.generate_surface(divs, [[a], [b], [c]])
            new_ocean.append(surface)

        ocean_color = [0, 0, 255, 170]
        verts = []
        max_points = 2 ** divs + 1
        for face in new_ocean:
            for i in range(0, max_points - 1):
                # Create a counter k, this is used in place of j when we draw the triangle at the end of the row
                k = 0
                for j in range(max_points - i - 2):
                    # Add both triangle in the square to the render batch
                    #
                    #  b______d           .
                    #  |\     |           |\
                    #  | \    |           | \
                    #  |  \ 2 |           |  \
                    #  | 1 \  |           | 3 \
                    #  |    \ |           |    \
                    # a|_____\|c ........ |_____\

                    a = face[i][j]
                    b = face[i][j + 1]
                    c = face[i + 1][j]
                    d = face[i + 1][j + 1]

                    # Triangle 1
                    verts += a + b + c
                    # Triangle 2,  bdc to maintain clockwise winding order
                    verts += b + d + c

                    k += 1

                # Add remaining triangle at the end of the row, Triangle 3
                a = face[i][k]
                b = face[i][k + 1]
                c = face[i + 1][k]
                verts += a + b + c

        num_triangles = len(verts) / 3
        # As water color is uniform, add all colors at the end
        colors = ocean_color * num_triangles

        # Example of group, class overrides functions, functions within pyglet are all pass
        # https: // github.com / reidrac / pyglet - obj - batch / blob / master / obj_batch.py
        group = pyglet.graphics.Group
        # print group.


        # water_group = material.Material()
        water_group = None

        self.ocean_batch.add(num_triangles, pyglet.gl.GL_TRIANGLES, water_group, ('v3f', verts), ('n3f', verts),
                             ('c4B', colors))

        cloud_icosahedron = generator.generate_icosahedron(2)


        new_clouds = []
        new_cloud_density = []

        cloud_displacement = 0.9
        cloud_roughness = 1
        scale_div = 2
        cloud_cutoff = 0
        cloud_edges = generator.generate_edges(new_icosahedron, divs, cloud_displacement, cloud_roughness)
        for face in faces:
            # Not the most elegant solution
            a = cloud_icosahedron[face[0]]
            b = cloud_icosahedron[face[1]]
            c = cloud_icosahedron[face[2]]

            edges = [cloud_edges[(face[0], face[1])], \
                     # Note, this edge is backwards
                     cloud_edges[(face[2], face[1])], \
                     cloud_edges[(face[2], face[0])]]
            #surface = generator.generate_surface(divs, [[a], [b], [c]])
            # Need to pre generate the edges for this one
            surface = generator.generate_surface(divs, [[a], [b], [c]])
            new_clouds.append(surface)
            surface_density = generator.generate_surface(divs, edges, cloud_displacement, cloud_roughness)
            new_cloud_density.append(surface_density)


        # Need a function to generate density
        # Add the water to the render batch
        cloud_color = [0, 250, 255, 170]
        verts = []

        max_points = 2 ** divs + 1
        for f, face in enumerate(new_clouds):


            for i in range(0, max_points - 1):
                # Create a counter k, this is used in place of j when we draw the triangle at the end of the row
                k = 0
                for j in range(max_points - i - 2):
                    # Add both triangle in the square to the render batch
                    #
                    #  b______d           .
                    #  |\     |           |\
                    #  | \    |           | \
                    #  |  \ 2 |           |  \
                    #  | 1 \  |           | 3 \
                    #  |    \ |           |    \
                    # a|_____\|c ........ |_____\

                    a = face[i][j]
                    b = face[i][j+1]
                    c = face[i+1][j]
                    d = face[i+1][j+1]

                    verts = []
                    verts += a + b + c
                    color = []
                    # get mag new_cloud_density[f][i][j] + new_cloud_density[f][i][j + 1] + new_cloud_density[f][i + 1][j]

                    mag1 = int((255 / scale_div) * math_helper.get_mag(new_cloud_density[f][i][j]))
                    mag2 = int((255 / scale_div) * math_helper.get_mag(new_cloud_density[f][i][j + 1]))
                    mag3 = int((255 / scale_div) * math_helper.get_mag(new_cloud_density[f][i + 1][j]))
                    if mag1 < cloud_cutoff:
                        mag1 = 0
                    if mag2 < cloud_cutoff:
                        mag2 = 0
                    if mag3 < cloud_cutoff:
                        mag3 = 0
                    color = [255, 255, 255, mag1, 255, 255, 255, mag2, 255, 255, 255, mag3]

                    self.cloud_batch.add(3, pyglet.gl.GL_TRIANGLES, None, ('v3f', verts), ('n3f', verts),('c4B', color))
                                         #('c4B', [0, 255, 255, 180]))                    # Triangle 1
                    #verts += a + b + c
                    # Triangle 2,  bdc to maintain clockwise winding order

                    verts = []
                    verts += b + d + c

                    color = []
                    mag1 = int((255/scale_div) * math_helper.get_mag(new_cloud_density[f][i][j + 1]))
                    mag2 = int((255/scale_div) * math_helper.get_mag(new_cloud_density[f][i+1][j + 1]))
                    mag3 = int((255/scale_div) * math_helper.get_mag(new_cloud_density[f][i+1][j]))


                    if mag1 < cloud_cutoff:
                        mag1 = 0
                    if mag2 < cloud_cutoff:
                        mag2 = 0
                    if mag3 < cloud_cutoff:
                        mag3 = 0

                    color = [255, 255, 255, mag1, 255, 255, 255, mag2, 255, 255, 255, mag3]

                    #Get mag - new_cloud_density[f][i][j + 1] + new_cloud_density[f][i + 1][j + 1] + new_cloud_density[f][i + 1][j]
                    #color = [0, 255, 255, 180]*3
                    self.cloud_batch.add(3, pyglet.gl.GL_TRIANGLES, None, ('v3f', verts), ('n3f', verts),('c4B', color))
                                         #('c4B', [0, 255, 255, 180]))  # Triangle 1

                    k += 1

                # Add remaining triangle at the end of the row, Triangle 3
                a = face[i][k]
                b = face[i][k + 1]
                c = face[i + 1][k]
                # verts += a + b + c
                verts = []
                verts += a + b + c
                color = []

                # TODO - Remember to use k here, not j
                mag1 = int((255 / scale_div) * math_helper.get_mag(new_cloud_density[f][i][k]))
                mag2 = int((255 / scale_div) * math_helper.get_mag(new_cloud_density[f][i][k + 1]))
                mag3 = int((255 / scale_div) * math_helper.get_mag(new_cloud_density[f][i + 1][k]))
                if mag1 < cloud_cutoff:
                    mag1 = 0
                if mag2 < cloud_cutoff:
                    mag2 = 0
                if mag3 < cloud_cutoff:
                    mag3 = 0

                color = [255, 255, 255, mag1, 255, 255, 255, mag2, 255, 255, 255, mag3]
                self.cloud_batch.add(3, pyglet.gl.GL_TRIANGLES, None, ('v3f', verts),('n3f', verts), ('c4B', color))

        #num_triangles = len(verts)/3




        # TODO - Maybe create solar system class
        sun_icosahedron = generator.generate_icosahedron(10)
        print sun_icosahedron



        new_sun = []
        for face in faces:
            a = sun_icosahedron[face[0]]
            b = sun_icosahedron[face[1]]
            c = sun_icosahedron[face[2]]

            surface = generator.generate_surface(divs, [[a], [b], [c]])
            new_sun.append(surface)

        sun_color = [255, 255, 50]
        verts = []
        max_points = 2 ** divs + 1
        for face in new_sun:
            for i in range(0, max_points - 1):
                # Create a counter k, this is used in place of j when we draw the triangle at the end of the row
                k = 0
                for j in range(max_points - i - 2):
                    # Add both triangle in the square to the render batch
                    #
                    #  b______d           .
                    #  |\     |           |\
                    #  | \    |           | \
                    #  |  \ 2 |           |  \
                    #  | 1 \  |           | 3 \
                    #  |    \ |           |    \
                    # a|_____\|c ........ |_____\

                    a = face[i][j]
                    b = face[i][j + 1]
                    c = face[i + 1][j]
                    d = face[i + 1][j + 1]

                    # Triangle 1
                    verts += a + b + c
                    # Triangle 2,  bdc to maintain clockwise winding order
                    verts += b + d + c

                    k += 1

                # Add remaining triangle at the end of the row, Triangle 3
                a = face[i][k]
                b = face[i][k + 1]
                c = face[i + 1][k]
                verts += a + b + c

        num_triangles = len(verts) / 3
        # As water color is uniform, add all colors at the end
        colors = sun_color * num_triangles

        # Update x position
        for i in range(0, len(verts), 3):
            verts[i] += 20

        self.sun_batch.add(num_triangles, pyglet.gl.GL_TRIANGLES, None, ('v3f', verts), ('n3f', verts),
                             ('c3B', colors))

        # Seems to work if I up the number of processes above the cpu count
        # Should either remove multiprocessing, or find a way to make it work correctly
        pool = Pool(processes=7)  # process per core

        # Output isn't in same order
        self.chunks = pool.map(chunk.Chunk(divs, final_edges, main_displacement, roughness), faces)

        # Needs to be done outside of multiprocess, as some parts of pyglet arnt thread safe
        for i in self.chunks:
            i.generate_batch()