Пример #1
0
    def __init__(self):
        self.texture_manager = texture_manager.Texture_manager(16, 16, 256)
        self.block_types = [None]

        # parse block type data file

        blocks_data_file = open("data/blocks.mcpy")
        blocks_data = blocks_data_file.readlines()
        blocks_data_file.close()

        for block in blocks_data:
            if block[0] in ['\n', '#']:  # skip if empty line or comment
                continue

            number, props = block.split(':', 1)
            number = int(number)

            # default block

            name = "Unknown"
            model = models.cube
            texture = {"all": "unknown"}

            # read properties

            for prop in props.split(','):
                prop = prop.strip()
                prop = list(filter(None, prop.split(' ', 1)))

                if prop[0] == "sameas":
                    sameas_number = int(prop[1])

                    name = self.block_types[sameas_number].name
                    texture = self.block_types[
                        sameas_number].block_face_textures
                    model = self.block_types[sameas_number].model

                elif prop[0] == "name":
                    name = eval(prop[1])

                elif prop[0][:7] == "texture":
                    _, side = prop[0].split('.')
                    texture[side] = prop[1].strip()

                elif prop[0] == "model":
                    model = eval(prop[1])

            # add block type

            _block_type = block_type.Block_type(self.texture_manager, name,
                                                texture, model)

            if number < len(self.block_types):
                self.block_types[number] = _block_type

            else:
                self.block_types.append(_block_type)

        self.texture_manager.generate_mipmaps()

        # load the world

        self.save = save.Save(self)

        self.chunks = {}
        self.save.load()

        for chunk_position in self.chunks:
            self.chunks[chunk_position].update_subchunk_meshes()
            self.chunks[chunk_position].update_mesh()
Пример #2
0
    def __init__(self, **args):
        super().__init__(**args)

        # create blocks

        self.texture_manager = texture_manager.Texture_manager(16, 16, 256)

        self.cobblestone = block_type.Block_type(self.texture_manager,
                                                 "cobblestone",
                                                 {"all": "cobblestone"})
        self.grass = block_type.Block_type(self.texture_manager, "grass", {
            "top": "grass",
            "bottom": "dirt",
            "sides": "grass_side"
        })
        self.dirt = block_type.Block_type(self.texture_manager, "dirt",
                                          {"all": "dirt"})
        self.stone = block_type.Block_type(self.texture_manager, "stone",
                                           {"all": "stone"})
        self.sand = block_type.Block_type(self.texture_manager, "sand",
                                          {"all": "sand"})
        self.planks = block_type.Block_type(self.texture_manager, "planks",
                                            {"all": "planks"})
        self.log = block_type.Block_type(self.texture_manager, "log", {
            "top": "log_top",
            "bottom": "log_top",
            "sides": "log_side"
        })

        self.texture_manager.generate_mipmaps()

        # create vertex array object

        self.vao = gl.GLuint(0)
        gl.glGenVertexArrays(1, ctypes.byref(self.vao))
        gl.glBindVertexArray(self.vao)

        # create vertex position vbo

        self.vertex_position_vbo = gl.GLuint(0)
        gl.glGenBuffers(1, ctypes.byref(self.vertex_position_vbo))
        gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self.vertex_position_vbo)

        gl.glBufferData(
            gl.GL_ARRAY_BUFFER,
            ctypes.sizeof(gl.GLfloat * len(self.grass.vertex_positions)),
            (gl.GLfloat *
             len(self.grass.vertex_positions))(*self.grass.vertex_positions),
            gl.GL_STATIC_DRAW)

        gl.glVertexAttribPointer(0, 3, gl.GL_FLOAT, gl.GL_FALSE, 0, 0)
        gl.glEnableVertexAttribArray(0)

        # create tex coord vbo

        self.tex_coord_vbo = gl.GLuint(0)
        gl.glGenBuffers(1, ctypes.byref(self.tex_coord_vbo))
        gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self.tex_coord_vbo)

        gl.glBufferData(gl.GL_ARRAY_BUFFER,
                        ctypes.sizeof(gl.GLfloat * len(self.grass.tex_coords)),
                        (gl.GLfloat *
                         len(self.grass.tex_coords))(*self.grass.tex_coords),
                        gl.GL_STATIC_DRAW)

        gl.glVertexAttribPointer(1, 3, gl.GL_FLOAT, gl.GL_FALSE, 0, 0)
        gl.glEnableVertexAttribArray(1)

        # create index buffer object

        self.ibo = gl.GLuint(0)
        gl.glGenBuffers(1, self.ibo)
        gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, self.ibo)

        gl.glBufferData(gl.GL_ELEMENT_ARRAY_BUFFER,
                        ctypes.sizeof(gl.GLuint * len(self.grass.indices)),
                        (gl.GLuint *
                         len(self.grass.indices))(*self.grass.indices),
                        gl.GL_STATIC_DRAW)

        # create shader

        self.shader = shader.Shader("vert.glsl", "frag.glsl")
        self.shader_sampler_location = self.shader.find_uniform(
            b"texture_array_sampler")
        self.shader.use()

        # pyglet stuff

        pyglet.clock.schedule_interval(self.update, 1.0 / 60)
        self.mouse_captured = False

        # camera stuff

        self.camera = camera.Camera(self.shader, self.width, self.height)
Пример #3
0
    def __init__(self, **args):
        super().__init__(**args)

        # create blocks

        self.texture_manager = texture_manager.Texture_manager(
            16, 16, 256
        )  # create our texture manager (256 textures that are 16 x 16 pixels each)

        self.cobblestone = block_type.Block_type(
            self.texture_manager, "cobblestone", {"all": "cobblestone"}
        )  # create each one of our blocks with the texture manager and a list of textures per face
        self.grass = block_type.Block_type(self.texture_manager, "grass", {
            "top": "grass",
            "bottom": "dirt",
            "sides": "grass_side"
        })
        self.dirt = block_type.Block_type(self.texture_manager, "dirt",
                                          {"all": "dirt"})
        self.stone = block_type.Block_type(self.texture_manager, "stone",
                                           {"all": "stone"})
        self.sand = block_type.Block_type(self.texture_manager, "sand",
                                          {"all": "sand"})
        self.planks = block_type.Block_type(self.texture_manager, "planks",
                                            {"all": "planks"})
        self.log = block_type.Block_type(self.texture_manager, "log", {
            "top": "log_top",
            "bottom": "log_top",
            "sides": "log_side"
        })

        self.texture_manager.generate_mipmaps(
        )  # generate mipmaps for our texture manager's texture

        # create vertex array object

        self.vao = gl.GLuint(0)
        gl.glGenVertexArrays(1, ctypes.byref(self.vao))
        gl.glBindVertexArray(self.vao)

        # create vertex position vbo

        self.vertex_position_vbo = gl.GLuint(0)
        gl.glGenBuffers(1, ctypes.byref(self.vertex_position_vbo))
        gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self.vertex_position_vbo)

        gl.glBufferData(
            gl.GL_ARRAY_BUFFER,
            ctypes.sizeof(gl.GLfloat * len(self.grass.vertex_positions)),
            (gl.GLfloat * len(self.grass.vertex_positions))(
                *self.grass.vertex_positions
            ),  # use grass block's vertex positions
            gl.GL_STATIC_DRAW)

        gl.glVertexAttribPointer(0, 3, gl.GL_FLOAT, gl.GL_FALSE, 0, 0)
        gl.glEnableVertexAttribArray(0)

        # create tex coord vbo

        self.tex_coord_vbo = gl.GLuint(0)
        gl.glGenBuffers(1, ctypes.byref(self.tex_coord_vbo))
        gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self.tex_coord_vbo)

        gl.glBufferData(
            gl.GL_ARRAY_BUFFER,
            ctypes.sizeof(gl.GLfloat * len(self.grass.tex_coords)),
            (gl.GLfloat * len(self.grass.tex_coords))(
                *self.grass.tex_coords
            ),  # use grass block's texture coordinates positions
            gl.GL_STATIC_DRAW)

        gl.glVertexAttribPointer(1, 3, gl.GL_FLOAT, gl.GL_FALSE, 0, 0)
        gl.glEnableVertexAttribArray(1)

        # create index buffer object

        self.ibo = gl.GLuint(0)
        gl.glGenBuffers(1, self.ibo)
        gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, self.ibo)

        gl.glBufferData(
            gl.GL_ELEMENT_ARRAY_BUFFER,
            ctypes.sizeof(gl.GLuint * len(self.grass.indices)),
            (gl.GLuint * len(self.grass.indices))(
                *self.grass.indices),  # use grass block's indices
            gl.GL_STATIC_DRAW)

        # create shader

        self.shader = shader.Shader("vert.glsl", "frag.glsl")
        self.shader_matrix_location = self.shader.find_uniform(b"matrix")
        self.shader_sampler_location = self.shader.find_uniform(
            b"texture_array_sampler"
        )  # find our texture array sampler's uniform
        self.shader.use()

        # create matrices

        self.mv_matrix = matrix.Matrix()
        self.p_matrix = matrix.Matrix()

        self.x = 0
        pyglet.clock.schedule_interval(self.update, 1.0 / 60)
Пример #4
0
	def __init__(self, shader, player, texture_manager, options):
		self.options = options
		self.shader = shader
		self.player = player
		self.texture_manager = texture_manager
		self.block_types = [None]

		self.shader_daylight_location = shader.find_uniform(b"u_Daylight")
		self.daylight = 1800
		self.incrementer = 0
		self.time = 0
		self.c = 0

		# Compat
		self.get_chunk_position = get_chunk_position
		self.get_local_position = get_local_position

		# parse block type data file

		blocks_data_file = open("data/blocks.mcpy")
		blocks_data = blocks_data_file.readlines()
		blocks_data_file.close()

		logging.info("Loading block models")
		for block in blocks_data:
			if block[0] in ['\n', '#']: # skip if empty line or comment
				continue
			
			number, props = block.split(':', 1)
			number = int(number)

			# default block

			name = "Unknown"
			model = models.cube
			texture = {"all": "unknown"}

			# read properties

			for prop in props.split(','):
				prop = prop.strip()
				prop = list(filter(None, prop.split(' ', 1)))

				if prop[0] == "sameas":
					sameas_number = int(prop[1])

					name = self.block_types[sameas_number].name
					texture = self.block_types[sameas_number].block_face_textures
					model = self.block_types[sameas_number].model
				
				elif prop[0] == "name":
					name = eval(prop[1])
				
				elif prop[0][:7] == "texture":
					_, side = prop[0].split('.')
					texture[side] = prop[1].strip()

				elif prop[0] == "model":
					model = eval(prop[1])
			
			# add block type

			_block_type = block_type.Block_type(self.texture_manager, name, texture, model)

			if number < len(self.block_types):
				self.block_types[number] = _block_type
			
			else:
				self.block_types.append(_block_type)

		self.light_blocks = [10, 11, 50, 51, 62, 75]

		self.texture_manager.generate_mipmaps()

		indices = []

		for nquad in range(chunk.CHUNK_WIDTH * chunk.CHUNK_HEIGHT * chunk.CHUNK_LENGTH * 8):
			indices.append(4 * nquad + 0)
			indices.append(4 * nquad + 1)
			indices.append(4 * nquad + 2)
			indices.append(4 * nquad + 2)
			indices.append(4 * nquad + 3)
			indices.append(4 * nquad + 0)


		self.ibo = gl.GLuint(0)
		gl.glGenBuffers(1, self.ibo)
		gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, self.ibo)
		gl.glBufferData(
			gl.GL_ELEMENT_ARRAY_BUFFER,
			ctypes.sizeof(gl.GLuint * len(indices)),
			(gl.GLuint * len(indices))(*indices),
			gl.GL_STATIC_DRAW)
		gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, 0)

		logging.debug("Created Shared Index Buffer")

		# load the world

		self.save = save.Save(self)

		self.chunks = {}
		self.sorted_chunks = []

		# light update queue

		self.light_increase_queue = deque() # Node: World Position, light
		self.light_decrease_queue = deque() # Node: World position, light
		self.skylight_increase_queue = deque()
		self.skylight_decrease_queue = deque()
		self.chunk_building_queue = deque()

		self.save.load()
		
		logging.info("Lighting chunks")
		for world_chunk in self.chunks.values():
			self.init_skylight(world_chunk)

		logging.info("Generating chunks")
		for world_chunk in self.chunks.values():
			world_chunk.update_subchunk_meshes()

		del indices
		self.visible_chunks = []

		# Debug variables

		self.pending_chunk_update_count = 0
		self.chunk_update_counter = 0
Пример #5
0
    def __init__(self):
        self.texture_manager = texture_manager.Texture_manager(16, 16, 256)
        self.block_types = [None]

        self.block_types.append(
            block_type.Block_type(self.texture_manager, "cobblestone",
                                  {"all": "cobblestone"}))
        self.block_types.append(
            block_type.Block_type(self.texture_manager, "grass", {
                "top": "grass",
                "bottom": "dirt",
                "sides": "grass_side"
            }))
        self.block_types.append(
            block_type.Block_type(self.texture_manager, "grass_block",
                                  {"all": "grass"}))
        self.block_types.append(
            block_type.Block_type(self.texture_manager, "dirt",
                                  {"all": "dirt"}))
        self.block_types.append(
            block_type.Block_type(self.texture_manager, "stone",
                                  {"all": "stone"}))
        self.block_types.append(
            block_type.Block_type(self.texture_manager, "sand",
                                  {"all": "sand"}))
        self.block_types.append(
            block_type.Block_type(self.texture_manager, "planks",
                                  {"all": "planks"}))
        self.block_types.append(
            block_type.Block_type(self.texture_manager, "log", {
                "top": "log_top",
                "bottom": "log_top",
                "sides": "log_side"
            }))
        self.block_types.append(
            block_type.Block_type(self.texture_manager, "daisy",
                                  {"all": "daisy"}, models.plant))
        self.block_types.append(
            block_type.Block_type(self.texture_manager, "rose",
                                  {"all": "rose"}, models.plant))
        self.block_types.append(
            block_type.Block_type(
                self.texture_manager, "cactus", {
                    "top": "cactus_top",
                    "bottom": "cactus_bottom",
                    "sides": "cactus_side"
                }, models.cactus))
        self.block_types.append(
            block_type.Block_type(self.texture_manager, "dead_bush",
                                  {"all": "dead_bush"}, models.plant))

        self.texture_manager.generate_mipmaps()

        self.chunks = {}

        for x in range(2):
            for z in range(2):
                chunk_position = (x - 1, -1, z - 1)
                current_chunk = chunk.Chunk(self, chunk_position)

                for i in range(chunk.CHUNK_WIDTH):
                    for j in range(chunk.CHUNK_HEIGHT):
                        for k in range(chunk.CHUNK_LENGTH):
                            if j == 15:
                                current_chunk.blocks[i][j][k] = random.choices(
                                    [0, 9, 10], [20, 2, 1])[0]
                            elif j == 14:
                                current_chunk.blocks[i][j][k] = 2
                            elif j > 10:
                                current_chunk.blocks[i][j][k] = 4
                            else:
                                current_chunk.blocks[i][j][k] = 5

                self.chunks[chunk_position] = current_chunk

        for chunk_position in self.chunks:
            self.chunks[chunk_position].update_subchunk_meshes()
            self.chunks[chunk_position].update_mesh()
Пример #6
0
    def __init__(self):
        # create list of block types

        self.texture_manager = texture_manager.Texture_manager(16, 16, 256)
        self.block_types = [None]  # "None" is the block type for air

        self.block_types.append(
            block_type.Block_type(self.texture_manager, "cobblestone",
                                  {"all": "cobblestone"}))
        self.block_types.append(
            block_type.Block_type(self.texture_manager, "grass", {
                "top": "grass",
                "bottom": "dirt",
                "sides": "grass_side"
            }))
        self.block_types.append(
            block_type.Block_type(self.texture_manager, "grass_block",
                                  {"all": "grass"}))
        self.block_types.append(
            block_type.Block_type(self.texture_manager, "dirt",
                                  {"all": "dirt"}))
        self.block_types.append(
            block_type.Block_type(self.texture_manager, "stone",
                                  {"all": "stone"}))
        self.block_types.append(
            block_type.Block_type(self.texture_manager, "sand",
                                  {"all": "sand"}))
        self.block_types.append(
            block_type.Block_type(self.texture_manager, "planks",
                                  {"all": "planks"}))
        self.block_types.append(
            block_type.Block_type(self.texture_manager, "log", {
                "top": "log_top",
                "bottom": "log_top",
                "sides": "log_side"
            }))

        self.texture_manager.generate_mipmaps()

        # create chunks with very crude terrain generation

        self.chunks = {}

        for x in range(8):
            for z in range(8):
                chunk_position = (x - 4, -1, z - 4)
                current_chunk = chunk.Chunk(self, chunk_position)

                for i in range(chunk.CHUNK_WIDTH):
                    for j in range(chunk.CHUNK_HEIGHT):
                        for k in range(chunk.CHUNK_LENGTH):
                            if j > 13:
                                current_chunk.blocks[i][j][k] = random.choice(
                                    [0, 3])
                            else:
                                current_chunk.blocks[i][j][k] = random.choice(
                                    [0, 0, 1])

                self.chunks[chunk_position] = current_chunk

        # update each chunk's mesh

        for chunk_position in self.chunks:
            self.chunks[chunk_position].update_mesh()