public Patch(IHeightMap map, int patchsize, int gridx, int gridy, int patchcount, float scale, float heightscale) { int c = patchsize; float s = scale; VertexP3T2[] vtx = new VertexP3T2[c * c]; int i = 0; for (int y = 0; y < c; ++y) { for (int x = 0; x < c; ++x) { //x and y position float px = (float)gridx * s + ((float)x / (float)(c - 1)) * s; float py = (float)gridy * s + ((float)y / (float)(c - 1)) * s; px -= s * patchcount / 2; py -= s * patchcount / 2; //z position (height) float h = ((float)map.GetHeight( gridx * (patchsize - 1) + x, gridy * (patchsize - 1) + y)) * heightscale; //add the vertex vtx[i].position = new Vector3(px, h, py); vtx[i++].texture0 = new Vector2( (float)(gridx * (c - 1) + x) / (float)(patchcount * (c - 1)), (float)(gridy * (c - 1) + y) / (float)(patchcount * (c - 1)) ); } } Vertices = Root.Instance.UserInterface.Renderer.CreateStaticVertexBuffer(vtx, Format.Size * vtx.Length); Vertices.Format = Format; }
public Patch(IHeightMap map, int patchsize, int gridx, int gridy, int patchcount,float scale,float heightscale) { int c = patchsize; float s = scale; VertexP3T2[] vtx = new VertexP3T2[c * c]; int i = 0; for (int y = 0; y < c; ++y) { for (int x = 0; x < c; ++x) { //x and y position float px = (float)gridx * s + ((float)x / (float)(c - 1)) * s; float py = (float)gridy * s + ((float)y / (float)(c - 1)) * s; px -= s * patchcount / 2; py -= s * patchcount / 2; //z position (height) float h = ((float)map.GetHeight( gridx * (patchsize - 1) + x, gridy * (patchsize - 1) + y)) * heightscale; //add the vertex vtx[i].position=new Vector3(px, h, py); vtx[i++].texture0=new Vector2( (float)(gridx * (c - 1) + x) / (float)(patchcount * (c - 1)), (float)(gridy * (c - 1) + y) / (float)(patchcount * (c - 1)) ); } } Vertices = Root.Instance.UserInterface.Renderer.CreateStaticVertexBuffer(vtx, Format.Size * vtx.Length); Vertices.Format = Format; }
private void BuildBuffers() { if (Vertices.Length/3 != TextureCoords.Length/2) throw new Exception("Vertices.Length!=TextureCoords.Length"); int c = Vertices.Length / 3; VertexP3T2[] data = new VertexP3T2[Vertices.Length]; for (int i = 0; i < c; ++i) { data[i].position = new Vector3(Vertices[i*3],Vertices[i*3+1],Vertices[i*3+2]); data[i].texture0 = new Vector2(TextureCoords[i*2],TextureCoords[i*2+1]); } VertexBuffer = Root.Instance.UserInterface.Renderer.CreateStaticVertexBuffer(data, Vertices.Length * VertexFormat.VF_P3T2.Size); VertexBuffer.Format = VertexFormat.VF_P3T2; IndexBuffer = new IndexBuffer(); IndexBuffer.buffer = new int[MeshIndices.Length]; for (int i = 0; i < MeshIndices.Length; ++i) IndexBuffer.buffer[i] = (int)MeshIndices[i]; }