示例#1
0
        public void Cull(bool combine)
        {
            for (int x = 0; x < MineGame.chunk_size; x++)
            {
                for (int y = 0; y < MineGame.chunk_size; y++)
                {
                    for (int z = 0; z < MineGame.chunk_height; z++)
                    {
                        Block cur = blocks[x, y, z];

                        if (!cur.active)
                        {
                            continue;
                        }

                        cur.render_faces[Block.top_face]    = Convert.ToInt16(z == MineGame.chunk_height - 1 || !blocks[x, y, z + 1].active);
                        cur.render_faces[Block.bottom_face] = Convert.ToInt16((z == 0 || !blocks[x, y, z - 1].active) && this.radial_distance != planet.radial_distance);
                        cur.render_faces[Block.north_face]  = Convert.ToInt16(x == MineGame.chunk_size - 1 || !blocks[x + 1, y, z].active);
                        cur.render_faces[Block.south_face]  = Convert.ToInt16(x == 0 || !blocks[x - 1, y, z].active);
                        cur.render_faces[Block.west_face]   = Convert.ToInt16(y == 0 || !blocks[x, y - 1, z].active);
                        cur.render_faces[Block.east_face]   = Convert.ToInt16(y == MineGame.chunk_size - 1 || !blocks[x, y + 1, z].active);
                    }
                }
            }
            vertex_count = 0;

            for (int x = 0; x < MineGame.chunk_size; x++)
            {
                for (int y = 0; y < MineGame.chunk_size; y++)
                {
                    int starting_index = 0;
                    for (int z = 0; z < MineGame.chunk_height; z++)
                    {
                        Block cur = blocks[x, y, z];
                        cur.combine_primary = false;

                        bool isLastBlockInColumn  = true;// !combine || (y == MineGame.chunk_height - 1) || !cur.are_combinable(blocks[x, y + 1, z]);
                        var  bottom_height_offset = starting_index - y;

                        if (!isLastBlockInColumn)
                        {
                            continue;
                        }
                        else
                        {
                            starting_index = y + 1;
                        }
                        if (cur.rendered_vertice_count() == 0)
                        {
                            continue;
                        }
                        cur.combine_height  = bottom_height_offset;
                        cur.combine_primary = true;

                        vertex_count += 6 * cur.rendered_vertice_count();
                    }
                }
            }
        }
示例#2
0
        public void UpdateBuffer()
        {
            if (vertex_count != 0)
            {
                block_vertices     = new VertexPositionColorTexture[vertex_count];
                block_vertex_index = 0;
            }
            for (int x = 0; x < MineGame.chunk_size; x++)
            {
                for (int y = 0; y < MineGame.chunk_size; y++)
                {
                    for (int z = 0; z < MineGame.chunk_height; z++)
                    {
                        Block cur = blocks[x, y, z];
                        if (cur.rendered_vertice_count() == 0)
                        {
                            continue;
                        }
                        float bottom = 0.0f; // cur.radial_distance;
                        float top    = 1.0f; // cur.radial_distance + planet.height_step;

                        Vector3 north_east_top    = new Vector3(0.0f, top, -planet.step * 0.5);
                        Vector3 north_west_top    = new Vector3(0.0f, top, 0.0f);
                        Vector3 south_east_top    = new Vector3(0.0f, top, 0.0f);
                        Vector3 south_west_top    = new Vector3(0.0f, top, 0.0f);
                        Vector3 north_east_bottom = new Vector3(0.0f, bottom, 0.0f);
                        Vector3 north_west_bottom = new Vector3(0.0f, bottom, 0.0f);
                        Vector3 south_east_bottom = new Vector3(0.0f, bottom, 0.0f);
                        Vector3 south_west_bottom = new Vector3(0.0f, bottom, 0.0f);

                        /*
                         * Vector3 north_east_top =      ToCartesian(top, cur.latitude + planet.step, cur.longitude + planet.step);
                         * Vector3 north_west_top =      ToCartesian(top, cur.latitude + planet.step, cur.longitude);
                         * Vector3 south_east_top =      ToCartesian(top, cur.latitude, cur.longitude + planet.step);
                         * Vector3 south_west_top =      ToCartesian(top, cur.latitude, cur.longitude);
                         * Vector3 north_east_bottom =       ToCartesian(bottom, cur.latitude + planet.step, cur.longitude + planet.step);
                         * Vector3 north_west_bottom =       ToCartesian(bottom, cur.latitude + planet.step, cur.longitude);
                         * Vector3 south_east_bottom =       ToCartesian(bottom, cur.latitude, cur.longitude + planet.step);
                         * Vector3 south_west_bottom =       ToCartesian(bottom, cur.latitude, cur.longitude);
                         */

                        for (int i = 0; i < 6; i++)
                        {
                            if (cur.render_faces[i] != 1) // dont render it
                            {
                                continue;
                            }
                            var a = Color.White;
                            if (cur.type == BlockType.Grass && i == Block.top_face)
                            {
                                a = Color.FromNonPremultiplied(107, 168, 64, 255);
                            }


                            int offset = block_vertex_index;

                            Vector2 textureTopLeft     = planet.game.texture_coordinates[cur.type][i, Block.textureTopLeft];
                            Vector2 textureBottomLeft  = planet.game.texture_coordinates[cur.type][i, Block.textureBottomLeft];
                            Vector2 textureTopRight    = planet.game.texture_coordinates[cur.type][i, Block.textureTopRight];
                            Vector2 textureBottomRight = planet.game.texture_coordinates[cur.type][i, Block.textureBottomRight];

                            switch (i + 1)
                            {
                            case 1: //front   //north
                                block_vertices[offset + 0] = new VertexPositionColorTexture(north_west_bottom, a, textureBottomRight);
                                block_vertices[offset + 1] = new VertexPositionColorTexture(north_west_top, a, textureTopRight);
                                block_vertices[offset + 2] = new VertexPositionColorTexture(north_east_top, a, textureTopLeft);
                                block_vertices[offset + 3] = new VertexPositionColorTexture(north_east_top, a, textureTopLeft);
                                block_vertices[offset + 4] = new VertexPositionColorTexture(north_east_bottom, a, textureBottomLeft);
                                block_vertices[offset + 5] = new VertexPositionColorTexture(north_west_bottom, a, textureBottomRight);
                                break;

                            case 2: //back  //south
                                block_vertices[offset + 0] = new VertexPositionColorTexture(south_west_bottom, a, textureBottomLeft);
                                block_vertices[offset + 1] = new VertexPositionColorTexture(south_east_bottom, a, textureBottomRight);
                                block_vertices[offset + 2] = new VertexPositionColorTexture(south_east_top, a, textureTopRight);
                                block_vertices[offset + 3] = new VertexPositionColorTexture(south_east_top, a, textureTopRight);
                                block_vertices[offset + 4] = new VertexPositionColorTexture(south_west_top, a, textureTopLeft);
                                block_vertices[offset + 5] = new VertexPositionColorTexture(south_west_bottom, a, textureBottomLeft);
                                break;

                            case 3: //top
                                block_vertices[offset + 0] = new VertexPositionColorTexture(north_west_bottom, a, textureTopLeft);
                                block_vertices[offset + 1] = new VertexPositionColorTexture(south_east_bottom, a, textureBottomRight);
                                block_vertices[offset + 2] = new VertexPositionColorTexture(south_west_bottom, a, textureBottomLeft);
                                block_vertices[offset + 5] = new VertexPositionColorTexture(south_east_bottom, a, textureBottomRight);
                                block_vertices[offset + 4] = new VertexPositionColorTexture(north_east_bottom, a, textureTopRight);
                                block_vertices[offset + 3] = new VertexPositionColorTexture(north_west_bottom, a, textureTopLeft);
                                break;

                            case 4: //Bottom
                                block_vertices[offset + 0] = new VertexPositionColorTexture(south_west_top, a, textureBottomLeft);
                                block_vertices[offset + 1] = new VertexPositionColorTexture(south_east_top, a, textureBottomRight);
                                block_vertices[offset + 2] = new VertexPositionColorTexture(north_west_top, a, textureTopLeft);
                                block_vertices[offset + 3] = new VertexPositionColorTexture(south_east_top, a, textureBottomRight);
                                block_vertices[offset + 4] = new VertexPositionColorTexture(north_east_top, a, textureTopRight);
                                block_vertices[offset + 5] = new VertexPositionColorTexture(north_west_top, a, textureTopLeft);
                                break;

                            case 5: //east
                                block_vertices[offset + 0] = new VertexPositionColorTexture(north_east_top, a, textureTopRight);
                                block_vertices[offset + 1] = new VertexPositionColorTexture(south_east_top, a, textureTopLeft);
                                block_vertices[offset + 2] = new VertexPositionColorTexture(south_east_bottom, a, textureBottomLeft);
                                block_vertices[offset + 3] = new VertexPositionColorTexture(south_east_bottom, a, textureBottomLeft);
                                block_vertices[offset + 4] = new VertexPositionColorTexture(north_east_bottom, a, textureBottomRight);
                                block_vertices[offset + 5] = new VertexPositionColorTexture(north_east_top, a, textureTopRight);
                                break;

                            case 6: //west
                                block_vertices[offset + 0] = new VertexPositionColorTexture(south_west_bottom, a, textureBottomRight);
                                block_vertices[offset + 1] = new VertexPositionColorTexture(south_west_top, a, textureTopRight);
                                block_vertices[offset + 2] = new VertexPositionColorTexture(north_west_top, a, textureTopLeft);
                                block_vertices[offset + 5] = new VertexPositionColorTexture(south_west_bottom, a, textureBottomRight);
                                block_vertices[offset + 4] = new VertexPositionColorTexture(north_west_bottom, a, textureBottomLeft);
                                block_vertices[offset + 3] = new VertexPositionColorTexture(north_west_top, a, textureTopLeft);
                                break;
                            }
                            block_vertex_index += 6;
                        }
                    }
                }
            }
        }