Exemplo n.º 1
0
        protected QuadTree(QuadTree parent,int level,int n)
        {
            Level=level;
            Units=new Point(parent.Units.X/2,parent.Units.Y/2);
            switch(n)
            {
                case 0:
                    Center=parent.Center-parent.Size/4;
                    Position.X=parent.Position.X;
                    Position.Y=parent.Position.Y;
                    break;
                case 1:
                    Center.X=parent.Center.X+parent.Size.X/4;
                    Center.Z=parent.Center.Z-parent.Size.Z/4;
                    Position.X=parent.Position.X+Units.X;
                    Position.Y=parent.Position.Y;
                    break;
                case 2:
                    Center.X=parent.Center.X-parent.Size.X/4;
                    Center.Z=parent.Center.Z+parent.Size.Z/4;
                    Position.X=parent.Position.X;
                    Position.Y=parent.Position.Y+Units.Y;
                    break;
                case 3:
                    Center=parent.Center+parent.Size/4;
                    Position.X=parent.Position.X+Units.X;
                    Position.Y=parent.Position.Y+Units.Y;
                    break;
            }
            Center.Y=parent.Center.Y;
            Size=parent.Size/2;
            Size.Y=parent.Size.Y;
            Radius = (Size / 2).Length;

            if(level>0)
            {
                Children=new QuadTree[4];
                for(int i=0;i<4;++i)
                {
                    Children[i]=new QuadTree(this,level-1,i);
                }
            }
        }
Exemplo n.º 2
0
        protected void Init(IHeightMap heightmap,Texture color,Texture detail)
        {
            Height=heightmap;
            if(Height.Size.X!=Height.Size.Y)
                throw new Exception("map.Size.X!=map.Size.Y");
            if(IsPowerOf2(Height.Size.X-1))
            {
                PatchCount=(Height.Size.X-1)/(PatchSize-1);
            }
            else if(IsPowerOf2(Height.Size.X))
            {
                PatchCount=(Height.Size.X)/(PatchSize-1);
            }
            else
                throw new Exception("!IsPowerOf2(map.Size.X-1)");
            if(Height.Size.X<PatchSize)
                throw new Exception("map.Size.X<patchvertexsize");
            Levels=(int)Math.Log(PatchSize-1,2)+1;

            Patches=new Patch[PatchCount][];
            for(int x=0;x<PatchCount;++x)
            {
                Patches[x]=new Patch[PatchCount];
                for(int y=0;y<PatchCount;++y)
                {
                    Patch p=new Patch(this,x,y);
                    Patches[x][y]=p;
                }
            }
            for(int x=0;x<PatchCount;++x)
            {
                for(int y=0;y<PatchCount;++y)
                {
                    Patches[y][x].SetNeighbors(x>0 ? Patches[y][x-1] : null,
                        x<PatchCount-1 ? Patches[y][x+1] : null,
                        y>0 ? Patches[y-1][x] : null,
                        y<PatchCount-1 ? Patches[y+1][x] : null);

                    Patches[y][x].Generate();
                }
            }
            /*for(int x=0;x<PatchCount;++x)
            {
                for(int y=0;y<PatchCount;++y)
                {
                    Patches[y][x].Generate();
                }
            }*/
            Material=new Material();
            Material.diffusemap=color;
            Material.DetailMap=detail;

            HighDetail = 1 * PatchScale;
            LowDetail = HighDetail + Levels * PatchScale * 1.2f;
            //Color=color;
            //Detail=detail;
            Tree=new QuadTree(new Vector3(0,HeightScale/2,0),new Vector3(PatchScale*(float)PatchCount,HeightScale,PatchScale*(float)PatchCount),(int)Math.Log(PatchCount,2)+1,new Point(PatchCount,PatchCount));

            //collision
            /*Ode.dVector3[] verts=new Ode.dVector3[heightmap.Size.X*heightmap.Size.Y];
            int[] inds=new int[(heightmap.Size.X-1)*(heightmap.Size.Y-1)*2*3];
            int i=0;
            int j=0;
            for(int y=0;y<heightmap.Size.Y;++y)
            {
                for(int x=0;x<heightmap.Size.X;++x)
                {
                    float h=((float)Height.GetHeight(x,y)/255)*HeightScale;
                    verts[i].X = (float)(x - heightmap.Size.X / 2) * PatchScale;
                    verts[i].Y = h;
                    verts[i].Z = (float)(y - heightmap.Size.Y / 2) * PatchScale;
                    if (x < heightmap.Size.X - 1 && y < heightmap.Size.Y - 1)
                    {
                        inds[j++] = y * heightmap.Size.X + x;
                        inds[j++] = y * heightmap.Size.X + x +1;
                        inds[j++] = (y+1) * heightmap.Size.X + x;

                        inds[j++] = (y + 1) * heightmap.Size.X + x;
                        inds[j++] = y * heightmap.Size.X + x + 1;
                        inds[j++] = (y + 1) * heightmap.Size.X + x +1;
                    }
                    i++;
                }
            }

            CollisionMesh = new OdeTriMeshData(verts,inds);*/
            //CollisionMesh.
        }
Exemplo n.º 3
0
 public QuadTree(Vector3 pos,Vector3 size,int levels,Point units)
 {
     Level=levels-1;
     Center=pos;
     Size=size;
     Radius=(size/2).Length;
     Children=new QuadTree[4];
     Units=units;
     for(int i=0;i<4;++i)
     {
         Children[i]=new QuadTree(this,levels-2,i);
     }
 }