private void ReadVertices() { //Length of the Lump int VerticesLength = Header.DirEntries[10].Length; //Number of textures to load int nbVertices = VerticesLength / (sizeof(float) * 10 + sizeof(byte) * 4); Vertices = new vertex[nbVertices]; for (int i = 0; i < nbVertices; i++) { //position float[] position = new float[3] { ReadFloat(), ReadFloat(), ReadFloat() }; //texcoord float[,] texcoord = new float[2, 2] { { ReadFloat(), ReadFloat() }, { ReadFloat(), ReadFloat() } }; //position float[] normal = new float[3] { ReadFloat(), ReadFloat(), ReadFloat() }; //position byte[] color = new byte[4] { ReadByte(), ReadByte(), ReadByte(), ReadByte() }; Vertices[i] = new vertex(position, texcoord, normal, color); } }
public static vertex operator +(vertex original, vertex other) { vertex temp = new vertex((float[])original.Position.Clone(), (float[, ])original.TexCoord.Clone(), (float[])original.Normal.Clone(), original.Color); for (int i = 0; i < original.Position.Length; i++) { temp.Position[i] += other.Position[i]; temp.Normal[i] += other.Normal[i]; } return(temp); }
public static vertex operator *(vertex original, double pMultiplier) { vertex temp = new vertex((float[])original.Position.Clone(), (float[, ])original.TexCoord.Clone(), (float[])original.Normal.Clone(), original.Color); for (int i = 0; i < original.Position.Length; i++) { temp.Position[i] *= (float)pMultiplier; temp.Normal[i] *= (float)pMultiplier; } return(temp); }
private List <int> TesselateOnePatch(List <vertex> pVerticesList, vertex[] pControls) { int Length = Tesselation + 1; vertex[,] newControls = new vertex[3, Length]; for (int i = 0; i < 3; i++) { vertex p0 = pControls[i * 3]; vertex p1 = pControls[(i * 3) + 1]; vertex p2 = pControls[(i * 3) + 2]; for (int l = 0; l < Length; l++) { double a = l / (double)Tesselation; double b = 1 - a; newControls[i, l] = p0 * b * b + p1 * 2 * b * a + p2 * a * a; } } vertex[] vertices = new vertex[Length * Length]; for (int x = 0; x < Length; x++) { vertex p0 = newControls[0, x]; vertex p1 = newControls[1, x]; vertex p2 = newControls[2, x]; double c = x / (double)Tesselation; // texcoord for (int y = 0; y < Length; y++) { double a = y / (double)Tesselation; double b = 1 - a; //2nd pass vertices[y + x * Length] = p0 * b * b + p1 * 2 * b * a + p2 * a * a; vertices[y + x * Length].TexCoord = new float[, ] { { p0.TexCoord[0, 0] + (float)a, p0.TexCoord[0, 0] + (float)c }, { p0.TexCoord[0, 1] + (float)a, p0.TexCoord[0, 1] + (float)c } }; } } List <int> indices = new List <int>(); int offset = Length * Length; for (int row = 0; row < Length - 1; row++) { for (int col = 0; col < Length - 1; col++) { // 0, 0 indices.Add(col + (Length * row) + (TesselationOffset * offset)); // 1, 1 indices.Add((col + 1) + (Length * (row + 1)) + (TesselationOffset * offset)); // 1, 0 indices.Add((col + 1) + (Length * row) + (TesselationOffset * offset)); // 0, 0 indices.Add(col + (Length * row) + (TesselationOffset * offset)); // 0, 1 indices.Add(col + (Length * (row + 1)) + (TesselationOffset * offset)); // 1, 1 indices.Add((col + 1) + (Length * (row + 1)) + (TesselationOffset * offset)); } } TesselationOffset++; pVerticesList.AddRange(vertices); return(indices); }
private void CreateBeziersWithTesselation() { List <int> bezierTextureIds = new List <int>(); List <vertex[]> controlList = new List <vertex[]>(); List <int> textureIds = new List <int>(); foreach (face f in file.Faces) { if (f.Type == 2) { int width = (f.Size[0] - 1) / 2; int height = (f.Size[1] - 1) / 2; bezierTextureIds.Add(f.Texture); vertex[,] patch = new vertex[f.Size[0], f.Size[1]]; int cpt = f.Vertex; for (int y = 0; y < f.Size[1]; y++) { for (int x = 0; x < f.Size[0]; x++) { patch[x, y] = file.Vertices[cpt]; cpt++; } } for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { int i = 2 * x; int j = 2 * y; vertex[] controls = new vertex[3 * 3]; for (int u = 0; u < 3; u++) { for (int v = 0; v < 3; v++) { vertex vert = patch[u + i, v + j]; controls[u * 3 + v] = vert; } } controlList.Add(controls); textureIds.Add(f.Texture); } } } } List <vertex> bspVertices; bspVertices = new List <vertex>(); List <int>[] Texturearrays = new List <int> [file.Textures.Count()]; for (int i = 0; i < file.Textures.Count(); i++) { Texturearrays[i] = new List <int>(); } TesselationOffset = 0; for (int i = 0; i < controlList.Count; i++) { Texturearrays[textureIds[i]].AddRange(TesselateOnePatch(bspVertices, controlList[i])); } List <int> realList = new List <int>(); BezierstextureLengths = new int[file.Textures.Count()]; int cpt3 = 0; for (int i = 0; i < Texturearrays.Length; i++) { realList.AddRange(Texturearrays[i]); cpt3 += Texturearrays[i].Count; BezierstextureLengths[i] = cpt3; } List <VertexPositionNormalTexture> verticesList = new List <VertexPositionNormalTexture>(); int cpt2 = 0; foreach (vertex vert in bspVertices) { Vector3 normal = V3FromFloatArray(vert.Normal); normal.Normalize(); verticesList.Add(new VertexPositionNormalTexture(V3FromFloatArray(vert.Position), normal, new Vector2(vert.TexCoord[0, 0], vert.TexCoord[0, 1]))); cpt2++; } if (bspVertices.Count > 0) { BeziersVertices = new VertexBuffer(GraphicsDevice, typeof(VertexPositionNormalTexture), bspVertices.Count, BufferUsage.WriteOnly); BeziersVertices.SetData(verticesList.ToArray()); BeziersIndex = new IndexBuffer(GraphicsDevice, IndexElementSize.ThirtyTwoBits, realList.Count, BufferUsage.WriteOnly); BeziersIndex.SetData(realList.ToArray(), 0, realList.Count); } else { BeziersIndex = null; BeziersVertices = null; } }