Example #1
0
            internal TerrainBlock(int x_start, int y_start, int x_count, int y_count,  BoundingBox box, int subdivision, Terrain owner)
            {
                bounds = box;
                if (subdivision < owner.Subdivisions)
                {
                    x_count /= 2;
                    y_count /= 2;
                    Vector3 new_size = box.Size / 2;
                    new_size.y = box.Size.y;
                    float offset = box.Size.x / 4;

                    children = new List<TerrainBlock>();

                    children.Add(new TerrainBlock(x_start, y_start, x_count, y_count, new BoundingBox() { Size = new_size, Position = box.Position + new Vector3(-offset, 0, offset) }, subdivision + 1, owner));
                    children.Add(new TerrainBlock(x_start + x_count, y_start, x_count, y_count, new BoundingBox() { Size = new_size, Position = box.Position + new Vector3(offset, 0, offset) }, subdivision + 1, owner));
                    children.Add(new TerrainBlock(x_start + x_count, y_start + y_count, x_count, y_count, new BoundingBox() { Size = new_size, Position = box.Position + new Vector3(offset, 0, -offset) }, subdivision + 1, owner));
                    children.Add(new TerrainBlock(x_start, y_start + y_count, x_count, y_count, new BoundingBox() { Size = new_size, Position = box.Position + new Vector3(-offset, 0, -offset) }, subdivision + 1, owner));
                }
                else
                {
                    ib = new IndexBuffer<int>();
                    ib.Allocate(6 * (x_count) * (y_count));
                    int index = 0;
                    for (int i = 0; i < y_count; i++)
                    {
                        for (int j = 0; j < x_count; j++)
                        {
                            int i1 = (y_start + i) * owner.heightmap.Width + x_start + j;
                            int i2 = (y_start + i) * owner.heightmap.Width + x_start + j + 1;
                            int i3 = (y_start + i + 1) * owner.heightmap.Width + x_start + j + 1;
                            int i4 = (y_start + i + 1) * owner.heightmap.Width + x_start + j;

                            ib[index++] = i1;
                            ib[index++] = i3;
                            ib[index++] = i2;

                            ib[index++] = i1;
                            ib[index++] = i4;
                            ib[index++] = i3;
                        }
                    }
                    ib.BufferData(VboUsage.GL_STATIC_DRAW);
                    ib.FreeClientData();
                }
            }
Example #2
0
            internal TerrainBlock(Terrain owner, List<int> indices, BoundingBox box, int subdivision)
            {
                bounds = box;

                if (subdivision < owner.Subdivisions)
                {
                    Vector3 new_size = box.Size / 2;
                    new_size.y = box.Size.y;
                    float offset = box.Size.x / 4;

                    children = new List<TerrainBlock>();

                    children.Add(new TerrainBlock(owner, indices, new BoundingBox() { Size = new_size, Position = box.Position + new Vector3(-offset, 0, offset) }, subdivision + 1));
                    children.Add(new TerrainBlock(owner, indices, new BoundingBox() { Size = new_size, Position = box.Position + new Vector3(offset, 0, offset) }, subdivision + 1));
                    children.Add(new TerrainBlock(owner, indices, new BoundingBox() { Size = new_size, Position = box.Position + new Vector3(offset, 0, -offset) }, subdivision + 1));
                    children.Add(new TerrainBlock(owner, indices, new BoundingBox() { Size = new_size, Position = box.Position + new Vector3(-offset, 0, -offset) }, subdivision + 1));

                }
                else
                {
                    List<int> mil = new List<int>();
                    for (int i = 0; i < indices.Count; i += 3)
                    {
                        var i1 = indices[i];
                        var i2 = indices[i + 1];
                        var i3 = indices[i + 2];

                        var v1 = owner.vb[i1];
                        var v2 = owner.vb[i2];
                        var v3 = owner.vb[i3];

                        var p1 = new Vector2(box.Position.x - box.Size.x / 2, box.Position.z - box.Size.z / 2);
                        var p2 = new Vector2(box.Position.x + box.Size.x / 2, box.Position.z + box.Size.z / 2);

                        if ((v1.Position.x >= p1.x && v1.Position.x <= p2.x && v1.Position.z >= p1.y && v1.Position.z <= p2.y) ||
                            (v2.Position.x >= p1.x && v2.Position.x <= p2.x && v2.Position.z >= p1.y && v2.Position.z <= p2.y) ||
                            (v3.Position.x >= p1.x && v3.Position.x <= p2.x && v3.Position.z >= p1.y && v3.Position.z <= p2.y))
                        {
                            mil.Add(i1);
                            mil.Add(i2);
                            mil.Add(i3);
                        }
                    }
                    ib = new IndexBuffer<int>();
                    ib.Allocate(mil);
                    ib.BufferData(VboUsage.GL_STATIC_DRAW);
                    //mil.Clear();
                }
            }