public VertexEntity(Vector3 position, Vector3 rotation, PredefinedObject shape, Color color, Vector3 scale) : this(position, rotation, PrimitiveType.TriangleList) { if (shape == PredefinedObject.Plane) { _vertices = new VertexPositionColorNormal[6]; _vertices[0].Position = new Vector3(-1.0f, 1.0f, 0.0f) * scale; _vertices[1].Position = new Vector3(1.0f, 1.0f, 0.0f) * scale; _vertices[2].Position = new Vector3(1.0f, -1.0f, 0.0f) * scale; _vertices[3].Position = new Vector3(-1.0f, 1.0f, 0.0f) * scale; _vertices[4].Position = new Vector3(1.0f, -1.0f, 0.0f) * scale; _vertices[5].Position = new Vector3(-1.0f, -1.0f, 0.0f) * scale; _vertices[0].Color = color; _vertices[1].Color = color; _vertices[2].Color = color; _vertices[3].Color = color; _vertices[4].Color = color; _vertices[5].Color = color; _vertices[0].Normal = new Vector3(0, 1, 0); _vertices[1].Normal = new Vector3(0, 1, 0); _vertices[2].Normal = new Vector3(0, 1, 0); _vertices[3].Normal = new Vector3(0, 1, 0); _vertices[4].Normal = new Vector3(0, 1, 0); _vertices[5].Normal = new Vector3(0, 1, 0); _indices = new int[] {0, 1, 2, 3, 4, 5}; _primCount = 2; InitBuffers(); } }
public void GenerateAt(int x, int y, int z) { int corners = 0; for (int i = 0; i < 8; i++) { if (map[x + i / 4, y + i % 4 / 2, z + i % 2] < 0) corners |= 1 << i; } if (corners == 0 || corners == 255) return; VertexPositionColor[] vs = new VertexPositionColor[24]; Color c = Color.LightSteelBlue; vs[0] = new VertexPositionColor(new Vector3((x + 0), (y + 0), (z + 0)), c); vs[1] = new VertexPositionColor(new Vector3((x + 1), (y + 0), (z + 0)), c); vs[2] = new VertexPositionColor(new Vector3((x + 1), (y + 0), (z + 0)), c); vs[3] = new VertexPositionColor(new Vector3((x + 1), (y + 1), (z + 0)), c); vs[4] = new VertexPositionColor(new Vector3((x + 1), (y + 1), (z + 0)), c); vs[5] = new VertexPositionColor(new Vector3((x + 0), (y + 1), (z + 0)), c); vs[6] = new VertexPositionColor(new Vector3((x + 0), (y + 1), (z + 0)), c); vs[7] = new VertexPositionColor(new Vector3((x + 0), (y + 0), (z + 0)), c); vs[8] = new VertexPositionColor(new Vector3((x + 0), (y + 0), (z + 1)), c); vs[9] = new VertexPositionColor(new Vector3((x + 1), (y + 0), (z + 1)), c); vs[10] = new VertexPositionColor(new Vector3((x + 1), (y + 0), (z + 1)), c); vs[11] = new VertexPositionColor(new Vector3((x + 1), (y + 1), (z + 1)), c); vs[12] = new VertexPositionColor(new Vector3((x + 1), (y + 1), (z + 1)), c); vs[13] = new VertexPositionColor(new Vector3((x + 0), (y + 1), (z + 1)), c); vs[14] = new VertexPositionColor(new Vector3((x + 0), (y + 1), (z + 1)), c); vs[15] = new VertexPositionColor(new Vector3((x + 0), (y + 0), (z + 1)), c); vs[16] = new VertexPositionColor(new Vector3((x + 0), (y + 0), (z + 0)), c); vs[17] = new VertexPositionColor(new Vector3((x + 0), (y + 0), (z + 1)), c); vs[18] = new VertexPositionColor(new Vector3((x + 0), (y + 1), (z + 0)), c); vs[19] = new VertexPositionColor(new Vector3((x + 0), (y + 1), (z + 1)), c); vs[20] = new VertexPositionColor(new Vector3((x + 1), (y + 0), (z + 0)), c); vs[21] = new VertexPositionColor(new Vector3((x + 1), (y + 0), (z + 1)), c); vs[22] = new VertexPositionColor(new Vector3((x + 1), (y + 1), (z + 0)), c); vs[23] = new VertexPositionColor(new Vector3((x + 1), (y + 1), (z + 1)), c); OutlineBuffer.SetData<VertexPositionColor>(OutlineLocation * VertexPositionColor.VertexDeclaration.VertexStride, vs, 0, 24, VertexPositionColor.VertexDeclaration.VertexStride); OutlineLocation += 24; QEF3D qef = new QEF3D(); Vector3 average_normal = new Vector3(); for (int i = 0; i < 12; i++) { int c1 = edges[i, 0]; int c2 = edges[i, 1]; int m1 = (corners >> c1) & 1; int m2 = (corners >> c2) & 1; if (m1 == m2) continue; float d1 = map[x + c1 / 4, y + c1 % 4 / 2, z + c1 % 2]; float d2 = map[x + c2 / 4, y + c2 % 4 / 2, z + c2 % 2]; Vector3 p1 = new Vector3((float)((c1 / 4)), (float)((c1 % 4 / 2)), (float)((c1 % 2))); Vector3 p2 = new Vector3((float)((c2 / 4)), (float)((c2 % 4 / 2)), (float)((c2 % 2))); Vector3 intersection = Sampler.GetIntersection(p1, p2, d1, d2); Vector3 normal = Sampler.GetNormal(intersection + new Vector3(x, y, z));//GetNormal(x, y); average_normal += normal; qef.Add(intersection, normal); } Vector3 p = qef.Solve2(0, 16, 0); Vector3 n = average_normal / (float)qef.Intersections.Count; VertexPositionColorNormal[] v2 = new VertexPositionColorNormal[1]; Vector3 c_v = n * 0.5f + Vector3.One * 0.5f; c_v.Normalize(); Color clr = new Color(c_v); v2[0] = new VertexPositionColorNormal(new Vector3((p.X + x), (p.Y + y), (p.Z + z)), clr, n); VertexBuffer.SetData<VertexPositionColorNormal>(VertexCount * VertexPositionColorNormal.VertexDeclaration.VertexStride, v2, 0, 1, VertexPositionColorNormal.VertexDeclaration.VertexStride); vertex_indexes[x, y, z] = VertexCount; VertexCount++; /*vs[0] = new VertexPositionColor(new Vector3((x + 0) , (y + 0) , 0), Color.Black); vs[1] = new VertexPositionColor(new Vector3((x + 1) , (y + 0) , 0), Color.Black); vs[2] = new VertexPositionColor(new Vector3((x + 1) , (y + 0) , 0), Color.Black); vs[3] = new VertexPositionColor(new Vector3((x + 1) , (y + 1) , 0), Color.Black); vs[4] = new VertexPositionColor(new Vector3((x + 1) , (y + 1) , 0), Color.Black); vs[5] = new VertexPositionColor(new Vector3((x + 0) , (y + 1) , 0), Color.Black); vs[6] = new VertexPositionColor(new Vector3((x + 0) , (y + 1) , 0), Color.Black); vs[7] = new VertexPositionColor(new Vector3((x + 0) , (y + 0) , 0), Color.Black); vs[8] = new VertexPositionColor(new Vector3((p.X + x) , (p.Y + y) , 0), Color.Black); vs[9] = new VertexPositionColor(new Vector3((p.X + x + .1f) , (p.Y + y + .1f) , 0), Color.Black); index = 10; VertexBuffer.SetData<VertexPositionColor>(VertexCount * VertexPositionColor.VertexDeclaration.VertexStride, vs, 0, index, VertexPositionColor.VertexDeclaration.VertexStride); VertexCount += index;*/ }
public void Polygonize(List<VertexPositionColorNormal> vertices, int size) { int bitmask = 0; for (int i = 0; i < 4; i++) { if (Sampler.Sample(Positions[i]) < 0) bitmask |= Bitmasks[i]; } if (bitmask == 0 || bitmask == 15) return; List<Vector2> vs = new List<Vector2>(); switch (bitmask) { case 0x0: break; case 0x1: vs.Add(GetPosition(0, 1)); vs.Add(GetPosition(2, 0)); break; case 0x2: vs.Add(GetPosition(3, 2)); vs.Add(GetPosition(2, 0)); break; case 0x3: vs.Add(GetPosition(3, 2)); vs.Add(GetPosition(0, 1)); break; //Counterclockwise lines stop here because I grew lazy... case 0x4: vs.Add(GetPosition(3, 2)); vs.Add(GetPosition(1, 3)); break; case 0x5: vs.Add(GetPosition(0, 2)); vs.Add(GetPosition(3, 2)); vs.Add(GetPosition(0, 1)); vs.Add(GetPosition(1, 3)); break; case 0x6: vs.Add(GetPosition(0, 2)); vs.Add(GetPosition(1, 3)); break; case 0x7: vs.Add(GetPosition(0, 1)); vs.Add(GetPosition(1, 3)); break; case 0x8: vs.Add(GetPosition(0, 1)); vs.Add(GetPosition(1, 3)); break; case 0x9: vs.Add(GetPosition(0, 2)); vs.Add(GetPosition(1, 3)); break; case 0xA: vs.Add(GetPosition(0, 2)); vs.Add(GetPosition(0, 1)); vs.Add(GetPosition(3, 2)); vs.Add(GetPosition(1, 3)); break; case 0xB: vs.Add(GetPosition(3, 2)); vs.Add(GetPosition(1, 3)); break; case 0xC: vs.Add(GetPosition(0, 1)); vs.Add(GetPosition(3, 2)); break; case 0xD: vs.Add(GetPosition(0, 2)); vs.Add(GetPosition(3, 2)); break; case 0xE: vs.Add(GetPosition(0, 1)); vs.Add(GetPosition(2, 0)); break; case 0xF: break; } for (int i = 0; i < vs.Count; i += 2) { VertexPositionColorNormal v0 = new VertexPositionColorNormal(new Vector3(vs[i], 0) * size, Color.Blue, Vector3.One); VertexPositionColorNormal v1 = new VertexPositionColorNormal(new Vector3(vs[i + 1], 0) * size, Color.Blue, Vector3.One); vertices.Add(v0); vertices.Add(v1); } }
internal void ComputeVerticesIndices(PAL Palette) { if (ComputedVertices == null) { ComputedVertices = new List<VertexPositionColorNormal>(); ComputedIndices = new List<int>(); var Normals = GetNormals(); var shifts = new Vector3[] { new Vector3(-1, -1, -1), new Vector3(-1, +1, -1), new Vector3(-1, -1, +1), new Vector3(-1, +1, +1), new Vector3(+1, -1, -1), new Vector3(+1, +1, -1), new Vector3(+1, -1, +1), new Vector3(+1, +1, +1), }; foreach (var sp in Body.Spans) { foreach (var v in sp.Voxels) { var Clr = Palette.Colors[v.ColorIndex]; var N = Normals[v.NormalIndex]; var obs = Body.ObscuredFrom(v.X, v.Y, v.Z); if (obs == Edges.All) { continue; } var realPos = Tail.Displacement(v.X, v.Y, v.Z); var start = ComputedVertices.Count; foreach (var sh in shifts) { var vpcn = new VertexPositionColorNormal(); var scaledShift = sh * Tail.Scale; var pos = realPos + scaledShift; vpcn.Position = pos; vpcn.Color = Clr; vpcn.Normal = N; ComputedVertices.Add(vpcn); } if (!obs.HasFlag(Edges.XLess)) { // 0123 ComputedIndices.Add(start + 0); ComputedIndices.Add(start + 1); ComputedIndices.Add(start + 2); ComputedIndices.Add(start + 1); ComputedIndices.Add(start + 2); ComputedIndices.Add(start + 3); } if (!obs.HasFlag(Edges.YLess)) { // 0246 ComputedIndices.Add(start + 0); ComputedIndices.Add(start + 2); ComputedIndices.Add(start + 4); ComputedIndices.Add(start + 2); ComputedIndices.Add(start + 4); ComputedIndices.Add(start + 6); } if (!obs.HasFlag(Edges.ZLess)) { // 0145 ComputedIndices.Add(start + 0); ComputedIndices.Add(start + 1); ComputedIndices.Add(start + 4); ComputedIndices.Add(start + 1); ComputedIndices.Add(start + 4); ComputedIndices.Add(start + 5); } if (!obs.HasFlag(Edges.ZMore)) { // 2367 ComputedIndices.Add(start + 2); ComputedIndices.Add(start + 3); ComputedIndices.Add(start + 6); ComputedIndices.Add(start + 3); ComputedIndices.Add(start + 6); ComputedIndices.Add(start + 7); } if (!obs.HasFlag(Edges.YMore)) { // 1357 ComputedIndices.Add(start + 1); ComputedIndices.Add(start + 3); ComputedIndices.Add(start + 5); ComputedIndices.Add(start + 3); ComputedIndices.Add(start + 5); ComputedIndices.Add(start + 7); } if (!obs.HasFlag(Edges.XMore)) { // 4567 ComputedIndices.Add(start + 4); ComputedIndices.Add(start + 5); ComputedIndices.Add(start + 6); ComputedIndices.Add(start + 5); ComputedIndices.Add(start + 6); ComputedIndices.Add(start + 7); } } } } }
public static void Render(BoundingFrustum frustum, GraphicsDevice graphicsDevice, Matrix view, Matrix projection, Color color) { VertexPositionColorNormal[] boxVertices = new VertexPositionColorNormal[8]; Vector3[] corners = frustum.GetCorners(); for (int i = 0; i < 8; i++) { boxVertices[i].Position = corners[i]; boxVertices[i].Color = Color.White; boxVertices[i].Normal = Vector3.Up; } short[] boxLines = new short[] { 0, 1, 1, 2, 2, 3, 3, 0, 0, 4, 1, 5, 2, 6, 3, 7, 4, 5, 5, 6, 6, 7, 7, 4 }; VertexBuffer buffer = new VertexBuffer( graphicsDevice, VertexPositionColorNormal.VertexDeclaration, boxVertices.Length, BufferUsage.WriteOnly); buffer.SetData(boxVertices); IndexBuffer indexBuffer = new IndexBuffer(graphicsDevice, typeof(short), boxLines.Length, BufferUsage.WriteOnly); indexBuffer.SetData(boxLines); graphicsDevice.Indices = indexBuffer; graphicsDevice.SetVertexBuffer(buffer); effect.World = Matrix.Identity; effect.View = view; effect.Projection = projection; effect.DiffuseColor = color.ToVector3(); foreach (EffectPass pass in effect.CurrentTechnique.Passes) { pass.Apply(); graphicsDevice.DrawIndexedPrimitives(PrimitiveType.LineList, 0, 0, boxVertices.Length, 0, boxLines.Length / 2); } }
public void GenerateAt(int x, int y) { int corners = 0; for (int i = 0; i < 4; i++) { if (map[x + i / 2, y + i % 2] < 0) corners |= 1 << i; } if (corners == 0 || corners == 15) return; VertexPositionColor[] vs = new VertexPositionColor[10]; Color c = Color.LightSteelBlue; vs[0] = new VertexPositionColor(new Vector3((x + 0) * Size, (y + 0) * Size, 0), c); vs[1] = new VertexPositionColor(new Vector3((x + 1) * Size, (y + 0) * Size, 0), c); vs[2] = new VertexPositionColor(new Vector3((x + 1) * Size, (y + 0) * Size, 0), c); vs[3] = new VertexPositionColor(new Vector3((x + 1) * Size, (y + 1) * Size, 0), c); vs[4] = new VertexPositionColor(new Vector3((x + 1) * Size, (y + 1) * Size, 0), c); vs[5] = new VertexPositionColor(new Vector3((x + 0) * Size, (y + 1) * Size, 0), c); vs[6] = new VertexPositionColor(new Vector3((x + 0) * Size, (y + 1) * Size, 0), c); vs[7] = new VertexPositionColor(new Vector3((x + 0) * Size, (y + 0) * Size, 0), c); OutlineBuffer.SetData<VertexPositionColor>(OutlineLocation * VertexPositionColor.VertexDeclaration.VertexStride, vs, 0, 8, VertexPositionColor.VertexDeclaration.VertexStride); OutlineLocation += 8; QEF qef = new QEF(); Vector3 average_normal = new Vector3(); for (int i = 0; i < 4; i++) { int c1 = edges[i, 0]; int c2 = edges[i, 1]; int m1 = (corners >> c1) & 1; int m2 = (corners >> c2) & 1; if (m1 == m2) continue; float d1 = map[x + c1 / 2, y + c1 % 2]; float d2 = map[x + c2 / 2, y + c2 % 2]; Vector2 p1 = new Vector2((float)((c1 / 2)), (float)((c1 % 2))); Vector2 p2 = new Vector2((float)((c2 / 2)), (float)((c2 % 2))); Vector2 intersection = Sampler.GetIntersection(p1, p2, d1, d2); Vector2 normal = Sampler.GetNormal(intersection + new Vector2(x, y));//GetNormal(x, y); average_normal += new Vector3(normal, 0); qef.Add(intersection, normal); } average_normal /= (float)qef.Intersections.Count; average_normal.Normalize(); vertices[x, y] = qef.Solve2(0, 16, 0); Vector2 p = vertices[x, y]; Color n_c = new Color(average_normal * 0.5f + Vector3.One * 0.5f); VertexPositionColorNormal v = new VertexPositionColorNormal(new Vector3((p.X + x) * Size, (p.Y + y) * Size, 0), n_c, average_normal); Vertices.Add(v); vertex_indexes[x, y] = VertexCount; VertexCount++; }
private void Polygonize(int x, int y, int z) { if (Cells[x, y, z] == null) return; int cube_index = 0; for (int i = 0; i < 8; i++) { if (Sampler.Sample(new Vector3(x, y, z) + CornerDeltas[i]) < 0) cube_index |= 1 << i; } if (cube_index == 0 || cube_index == 255) return; Cells[x, y, z].Vertices = new Vertex[VerticesNumberTable[cube_index]]; /*for (int i = 0; i < 12; i++) { Cells[x, y, z].Edges[i].A = new Vector3(x, y, z) + CornerDeltas[EdgePairs[i, 0]]; Cells[x, y, z].Edges[i].B = new Vector3(x, y, z) + CornerDeltas[EdgePairs[i, 1]]; Cells[x, y, z].Edges[i].ValueA = Sampler.Sample(Cells[x, y, z].Edges[i].A); Cells[x, y, z].Edges[i].ValueB = Sampler.Sample(Cells[x, y, z].Edges[i].B); }*/ int v_index = 0; Cells[x, y, z].Vertices[0] = new Vertex(); for (int e = 0; e < EdgesTable.GetLength(1); e++) { if (EdgesTable[cube_index, e] == -2) break; if (EdgesTable[cube_index, e] == -1) { v_index++; if (v_index < Cells[x, y, z].Vertices.Length) Cells[x, y, z].Vertices[v_index] = new Vertex(); continue; } //Cells[x, y, z].Vertices[v_index].Index = v_index; Cells[x, y, z].Vertices[v_index].Edges.Add(EdgesTable[cube_index, e]); Cells[x, y, z].Edges[EdgesTable[cube_index, e]].Vertices.Add(Cells[x, y, z].Vertices[v_index]); //Cells[x, y, z].Edges[EdgesTable[cube_index, e]].Flipped = Cells[x, y, z].Edges[EdgesTable[cube_index, e]].ValueA < 0; } foreach (Vertex v in Cells[x, y, z].Vertices) { Vertex tx = v; if (v == null) //for edges 241/243, which were originally marked as having 2 vertices...? continue; Vector3 point = new Vector3(); if (VertexMode != VertexModes.Block) { //QEF3D qef = new QEF3D(); QEFProper.QEFSolver qef = new QEFProper.QEFSolver(); VertexPlacement qem = new VertexPlacement(); for (int e_i = 0; e_i < tx.Edges.Count; e_i++) { Edge e = Cells[x, y, z].Edges[tx.Edges[e_i]]; if (VertexMode == VertexModes.Edges) point += e.GetIntersection(); else if (VertexMode == VertexModes.QEF) qef.Add(e.GetIntersection() - new Vector3(x, y, z), Sampler.GetNormal(e.GetIntersection())); else qem.AddPlane(e.GetIntersection() - new Vector3(x, y, z), Sampler.GetNormal(e.GetIntersection())); } if (VertexMode == VertexModes.Edges) point /= (float)tx.Edges.Count; else if (VertexMode == VertexModes.QEF) point = qef.Solve(1e-6f, 4, 1e-6f) + new Vector3(x, y, z); else point = qem.Solve() + new Vector3(x, y, z); } else point = new Vector3(x, y, z) + Vector3.One * 0.5f; //point = Vector3.Clamp(point, new Vector3(x, y, z), new Vector3(x + 1, y + 1, z + 1)); tx.Position = point; Vector3 norm = Sampler.GetNormal(point); Vector3 c_v = norm * 0.5f + Vector3.One * 0.5f; c_v.Normalize(); Color clr = new Color(c_v); if (!UseFlatShading) { tx.Index = Vertices.Count; VertexPositionColorNormal pv = new VertexPositionColorNormal(tx.Position, clr, norm); Vertices.Add(pv); } else { tx.Index = CalculatedVertices.Count; VertexPositionColor pv = new VertexPositionColor(tx.Position, clr); CalculatedVertices.Add(pv); } VertexPositionColor[] vs = new VertexPositionColor[24]; Color c = Color.Red; float vx = tx.Position.X; float vy = tx.Position.Y; float vz = tx.Position.Z; float r = 0.25f; vs[0] = new VertexPositionColor(new Vector3((vx + 0), (vy + 0), (vz + 0)), c); vs[1] = new VertexPositionColor(new Vector3((vx + r), (vy + 0), (vz + 0)), c); vs[2] = new VertexPositionColor(new Vector3((vx + r), (vy + 0), (vz + 0)), c); vs[3] = new VertexPositionColor(new Vector3((vx + r), (vy + r), (vz + 0)), c); vs[4] = new VertexPositionColor(new Vector3((vx + r), (vy + r), (vz + 0)), c); vs[5] = new VertexPositionColor(new Vector3((vx + 0), (vy + r), (vz + 0)), c); vs[6] = new VertexPositionColor(new Vector3((vx + 0), (vy + r), (vz + 0)), c); vs[7] = new VertexPositionColor(new Vector3((vx + 0), (vy + 0), (vz + 0)), c); vs[8] = new VertexPositionColor(new Vector3((vx + 0), (vy + 0), (vz + r)), c); vs[9] = new VertexPositionColor(new Vector3((vx + r), (vy + 0), (vz + r)), c); vs[10] = new VertexPositionColor(new Vector3((vx + r), (vy + 0), (vz + r)), c); vs[11] = new VertexPositionColor(new Vector3((vx + r), (vy + r), (vz + r)), c); vs[12] = new VertexPositionColor(new Vector3((vx + r), (vy + r), (vz + r)), c); vs[13] = new VertexPositionColor(new Vector3((vx + 0), (vy + r), (vz + r)), c); vs[14] = new VertexPositionColor(new Vector3((vx + 0), (vy + r), (vz + r)), c); vs[15] = new VertexPositionColor(new Vector3((vx + 0), (vy + 0), (vz + r)), c); vs[16] = new VertexPositionColor(new Vector3((vx + 0), (vy + 0), (vz + 0)), c); vs[17] = new VertexPositionColor(new Vector3((vx + 0), (vy + 0), (vz + r)), c); vs[18] = new VertexPositionColor(new Vector3((vx + 0), (vy + r), (vz + 0)), c); vs[19] = new VertexPositionColor(new Vector3((vx + 0), (vy + r), (vz + r)), c); vs[20] = new VertexPositionColor(new Vector3((vx + r), (vy + 0), (vz + 0)), c); vs[21] = new VertexPositionColor(new Vector3((vx + r), (vy + 0), (vz + r)), c); vs[22] = new VertexPositionColor(new Vector3((vx + r), (vy + r), (vz + 0)), c); vs[23] = new VertexPositionColor(new Vector3((vx + r), (vy + r), (vz + r)), c); OutlineBuffer.SetData<VertexPositionColor>(OutlineLocation * VertexPositionColor.VertexDeclaration.VertexStride, vs, 0, 24, VertexPositionColor.VertexDeclaration.VertexStride); OutlineLocation += 24; } }
private void generateVertexBuffers() { VertexPositionColorNormal[] vertices = new VertexPositionColorNormal[24]; vertices[0].Position = new Vector3(-.0025f, .0025f, .5f); vertices[1].Position = new Vector3(-.0025f, -.0025f, .5f); vertices[2].Position = new Vector3(.0025f, .0025f, .5f); vertices[3].Position = new Vector3(.0025f, -.0025f, .5f); vertices[4].Position = new Vector3(.0025f, .0025f, .5f); vertices[5].Position = new Vector3(.0025f, -.0025f, .5f); vertices[6].Position = new Vector3(.0025f, .0025f, 0); vertices[7].Position = new Vector3(.0025f, -.0025f, 0); vertices[8].Position = new Vector3(.0025f, .0025f, 0); vertices[9].Position = new Vector3(.0025f, -.0025f, 0); vertices[10].Position = new Vector3(-.0025f, .0025f, 0); vertices[11].Position = new Vector3(-.0025f, -.0025f, 0); vertices[12].Position = new Vector3(-.0025f, .0025f, .5f); vertices[13].Position = new Vector3(-.0025f, -.0025f, .5f); vertices[14].Position = new Vector3(-.0025f, .0025f, 0); vertices[15].Position = new Vector3(-.0025f, -.0025f, 0); vertices[16].Position = new Vector3(-.0025f, .0025f, .5f); vertices[17].Position = new Vector3(.0025f, .0025f, .5f); vertices[18].Position = new Vector3(.0025f, .0025f, 0); vertices[19].Position = new Vector3(-.0025f, .0025f, 0); vertices[20].Position = new Vector3(-.0025f, -.0025f, .5f); vertices[21].Position = new Vector3(.0025f, -.0025f, .5f); vertices[22].Position = new Vector3(.0025f, -.0025f, 0); vertices[23].Position = new Vector3(-.0025f, -.0025f, 0); for (int i = 0; i < vertices.Length; i++) { vertices[i].Color = Color.White; switch (i / 4) { case 0: vertices[i].Normal = Vector3.UnitZ; break; case 1: vertices[i].Normal = Vector3.UnitX; break; case 2: vertices[i].Normal = -Vector3.UnitZ; break; case 3: vertices[i].Normal = -Vector3.UnitX; break; case 4: vertices[i].Normal = Vector3.UnitY; break; case 5: vertices[i].Normal = -Vector3.UnitY; break; } vertices[i].Normal.Normalize(); } geometryBuffer = new VertexBuffer(GraphicsDevice, VertexPositionColorNormal.VertexDeclaration, vertices.Length, BufferUsage.WriteOnly); geometryBuffer.SetData(vertices); tileGeometryBuffer = new VertexBuffer(GraphicsDevice, VertexPositionColorNormal.VertexDeclaration, 4, BufferUsage.WriteOnly); tileGeometryBuffer.SetData(vertices, 0, 4); short[] indices = new short[36] { 2, 1, 0, 1, 2, 3, 6, 5, 4, 7, 5, 6, 10, 9, 8, 11, 9, 10, 12, 15, 14, 13, 15, 12, 17, 16, 19, 17, 19, 18, 23, 20, 21, 22, 23, 21 }; indexBuffer = new IndexBuffer(GraphicsDevice, typeof(short), indices.Length, BufferUsage.WriteOnly); indexBuffer.SetData(indices); tileIndexBuffer = new IndexBuffer(GraphicsDevice, typeof(short), 6, BufferUsage.WriteOnly); tileIndexBuffer.SetData(indices, 0, 6); }
private Vector3 GetNormalNA(ref VertexPositionColorNormal[] verts, params int[] indexes) { Vector3 product = new Vector3(); Vector3 a = verts[indexes[0]].Position - verts[indexes[2]].Position; Vector3 b = verts[indexes[1]].Position - verts[indexes[2]].Position; Vector3 c = Vector3.Cross(a, b); //c.Normalize(); product += c; a = verts[indexes[2]].Position - verts[indexes[1]].Position; b = verts[indexes[3]].Position - verts[indexes[1]].Position; c = Vector3.Cross(a, b); //c.Normalize(); product += c; //product *= 0.5f; product.Normalize(); return product; }
private Vector3 GetNormalQ(ref VertexPositionColorNormal[] verts, params int[] indexes) { Vector3 a = verts[indexes[2]].Position - verts[indexes[1]].Position; Vector3 b = verts[indexes[2]].Position - verts[indexes[0]].Position; Vector3 c = Vector3.Cross(a, b); a = verts[indexes[5]].Position - verts[indexes[4]].Position; b = verts[indexes[5]].Position - verts[indexes[3]].Position; Vector3 d = Vector3.Cross(a, b); //c.Normalize(); if (float.IsNaN(c.X)) c = Vector3.Zero; if (float.IsNaN(d.X)) d = Vector3.Zero; c += d; c /= 2.0f; c.Normalize(); return -c; }
private Vector3 GetNormal(ref VertexPositionColorNormal[] verts, params int[] indexes) { /*Vector3 product = new Vector3(); Vector3 a = verts[0].Position - verts[2].Position; Vector3 b = verts[1].Position - verts[2].Position; Vector3 c = Vector3.Cross(a, b); //c = new Vector3(Math.Abs(c.X), Math.Abs(c.Y), Math.Abs(c.Z)); c.Normalize(); product += c; a = verts[2].Position - verts[1].Position; b = verts[3].Position - verts[1].Position; c = Vector3.Cross(a, b); //c = new Vector3(Math.Abs(c.X), Math.Abs(c.Y), Math.Abs(c.Z)); c.Normalize(); product += c; product /= 2.0f; product.Normalize();*/ Vector3 product = new Vector3(); if (!Quads) { Vector3 n0 = Sampler.GetNormal(verts[indexes[0]].Position); Vector3 n1 = Sampler.GetNormal(verts[indexes[1]].Position); Vector3 n2 = Sampler.GetNormal(verts[indexes[2]].Position); product = (n0 + n1 + n2) / 3.0f; product = Sampler.GetNormal((verts[indexes[0]].Position + verts[indexes[1]].Position + verts[indexes[2]].Position) / 3.0f); product.Normalize(); } else { Vector3 n0 = Sampler.GetNormal(verts[0].Position); Vector3 n1 = Sampler.GetNormal(verts[1].Position); Vector3 n2 = Sampler.GetNormal(verts[2].Position); Vector3 n3 = Sampler.GetNormal(verts[3].Position); product = (n0 + n1 + n2 + n3); //product = Sampler.GetNormal((verts[0].Position + verts[1].Position + verts[2].Position + verts[3].Position) * 0.25f); product.Normalize(); } Vector3 c_v = product * 0.5f + Vector3.One * 0.5f; c_v.Normalize(); Color clr = new Color(c_v); Vector3 d = new Vector3(-.1f, 1f, -.1f); d.Normalize(); float g = (Vector3.Dot(product, d) + 1.0f) * 0.5f; clr = new Color(0, g, 0); //clr = Color.Green; verts[indexes[0]].Normal = product; verts[indexes[1]].Normal = product; verts[indexes[2]].Normal = product; verts[indexes[0]].Color = clr; verts[indexes[1]].Color = clr; verts[indexes[2]].Color = clr; return product; }
private void AddFlatTriangle(bool flipped, params int[] indexes) { VertexPositionColorNormal[] verts = new VertexPositionColorNormal[indexes.Length]; for (int i = 0; i < verts.Length; i++) verts[i] = new VertexPositionColorNormal(CalculatedVertices[indexes[i]].Position, CalculatedVertices[indexes[i]].Color, Vector3.Zero); if (indexes.Length > 3) { if (!flipped) { Vector3 n = GetNormalQ(ref verts, 2, 0, 1, 1, 3, 2); Vector3 c_v = n * 0.5f + Vector3.One * 0.5f; c_v.Normalize(); Color clr = new Color(c_v); Vector3 d = new Vector3(-.1f, -1f, -.1f); d.Normalize(); float g = (Vector3.Dot(n, d) + 1.0f) * 0.5f; //clr = new Color(0, g, 0); for (int i = 0; i < 4; i++) { verts[i].Normal = n; verts[i].Color = clr; } //GetNormal(ref verts, 2, 0, 1); Vertices.Add(verts[2]); Vertices.Add(verts[0]); Vertices.Add(verts[1]); //GetNormal(ref verts, 1, 3, 2); Vertices.Add(verts[1]); Vertices.Add(verts[3]); Vertices.Add(verts[2]); } else { Vector3 n = GetNormalQ(ref verts, 2, 1, 0, 1, 2, 3); Vector3 c_v = n * 0.5f + Vector3.One * 0.5f; c_v.Normalize(); Color clr = new Color(c_v); Vector3 d = new Vector3(-.1f, -1f, -.1f); d.Normalize(); float g = (Vector3.Dot(n, d) + 1.0f) * 0.5f; //clr = new Color(0, g, 0); for (int i = 0; i < 4; i++) { verts[i].Normal = n; verts[i].Color = clr; } //GetNormal(ref verts, 2, 1, 0); Vertices.Add(verts[2]); Vertices.Add(verts[1]); Vertices.Add(verts[0]); //GetNormal(ref verts, 1, 2, 3); Vertices.Add(verts[1]); Vertices.Add(verts[2]); Vertices.Add(verts[3]); } } else if (indexes.Length == 3) { /*GetNormal(ref verts, 0, 1, 2); Vertices.Add(verts[0]); Vertices.Add(verts[1]); Vertices.Add(verts[2]);*/ } }
private VertexPositionColorNormal[] loadVertices(HeightMap heightMap, float heightScale) { VertexPositionColorNormal[] vertices = new VertexPositionColorNormal[this.width * this.height]; for (int x = 0; x < this.width; x++) for (int y = 0; y < this.height; y++) { int v = x + y * this.width; float h = heightMap[x, y] * heightScale; vertices[v].Position = new Vector3(x, h, -y); vertices[v].Color = Color.Green; } return vertices; }
private VertexPositionColorNormal[] CreateTriangle() { var vertices = new VertexPositionColorNormal[6]; vertices[0].Position = new Vector3(-1f, -1f, 0f); vertices[0].Color = Color.Blue; vertices[0].Normal = new Vector3(1, 0, 1); vertices[1].Position = new Vector3(0, 1f, 0f); vertices[1].Color = Color.Green; vertices[1].Normal = new Vector3(1, 0, 1); vertices[2].Position = new Vector3(1f, -1f, 0f); vertices[2].Color = Color.White; vertices[2].Normal = new Vector3(1, 0, 1); vertices[3].Position = new Vector3(1f, -1f, 0f); vertices[3].Color = Color.White; vertices[3].Normal = new Vector3(1, 0, 1); vertices[4].Position = new Vector3(0, 1f, 0f); vertices[4].Color = Color.Green; vertices[4].Normal = new Vector3(1, 0, 1); vertices[5].Position = new Vector3(-1f, -1f, 0f); vertices[5].Color = Color.Blue; vertices[5].Normal = new Vector3(1, 0, 1); return vertices; }
private VertexPositionColorNormal[] CreateBox() { var vertices = new VertexPositionColorNormal[] { // Front Surface new VertexPositionColorNormal(new Vector3(-1.0f, -1.0f, 1.0f), Color.Blue, new Vector3(1, 0, 1)), new VertexPositionColorNormal(new Vector3(-1.0f, 1.0f, 1.0f), Color.Red, new Vector3(1, 0, 1)), new VertexPositionColorNormal(new Vector3(1.0f, -1.0f, 1.0f), Color.Red, new Vector3(1, 0, 1)), new VertexPositionColorNormal(new Vector3(1.0f, -1.0f, 1.0f), Color.Red, new Vector3(1, 0, 1)), new VertexPositionColorNormal(new Vector3(-1.0f, 1.0f, 1.0f), Color.Red, new Vector3(1, 0, 1)), new VertexPositionColorNormal(new Vector3(1.0f, 1.0f, 1.0f), Color.Blue, new Vector3(1, 0, 1)), // Back Surface new VertexPositionColorNormal(new Vector3(1.0f, -1.0f, -1.0f), Color.Yellow, new Vector3(1, 0, 1)), new VertexPositionColorNormal(new Vector3(1.0f, 1.0f, -1.0f), Color.Yellow, new Vector3(1, 0, 1)), new VertexPositionColorNormal(new Vector3(-1.0f, -1.0f, -1.0f), Color.Yellow, new Vector3(1, 0, 1)), new VertexPositionColorNormal(new Vector3(-1.0f, -1.0f, -1.0f), Color.Yellow, new Vector3(1, 0, 1)), new VertexPositionColorNormal(new Vector3(1.0f, 1.0f, -1.0f), Color.Yellow, new Vector3(1, 0, 1)), new VertexPositionColorNormal(new Vector3(-1.0f, 1.0f, -1.0f), Color.Yellow, new Vector3(1, 0, 1)), // Left Surface new VertexPositionColorNormal(new Vector3(-1.0f, -1.0f, -1.0f), Color.Blue, new Vector3(1, 0, 1)), new VertexPositionColorNormal(new Vector3(-1.0f, 1.0f, -1.0f), Color.Blue, new Vector3(1, 0, 1)), new VertexPositionColorNormal(new Vector3(-1.0f, -1.0f, 1.0f), Color.Blue, new Vector3(1, 0, 1)), new VertexPositionColorNormal(new Vector3(-1.0f, -1.0f, 1.0f), Color.Blue, new Vector3(1, 0, 1)), new VertexPositionColorNormal(new Vector3(-1.0f, 1.0f, -1.0f), Color.Blue, new Vector3(1, 0, 1)), new VertexPositionColorNormal(new Vector3(-1.0f, 1.0f, 1.0f), Color.Blue, new Vector3(1, 0, 1)), // Right Surface new VertexPositionColorNormal(new Vector3(1.0f, -1.0f, 1.0f), Color.Violet, new Vector3(1, 0, 1)), new VertexPositionColorNormal(new Vector3(1.0f, 1.0f, 1.0f), Color.Violet, new Vector3(1, 0, 1)), new VertexPositionColorNormal(new Vector3(1.0f, -1.0f, -1.0f), Color.Violet, new Vector3(1, 0, 1)), new VertexPositionColorNormal(new Vector3(1.0f, -1.0f, -1.0f), Color.Violet, new Vector3(1, 0, 1)), new VertexPositionColorNormal(new Vector3(1.0f, 1.0f, 1.0f), Color.Violet, new Vector3(1, 0, 1)), new VertexPositionColorNormal(new Vector3(1.0f, 1.0f, -1.0f), Color.Violet, new Vector3(1, 0, 1)), // Top Surface new VertexPositionColorNormal(new Vector3(-1.0f, 1.0f, 1.0f), Color.Green, new Vector3(1, 0, 1)), new VertexPositionColorNormal(new Vector3(-1.0f, 1.0f, -1.0f), Color.Green, new Vector3(1, 0, 1)), new VertexPositionColorNormal(new Vector3(1.0f, 1.0f, 1.0f), Color.Green, new Vector3(1, 0, 1)), new VertexPositionColorNormal(new Vector3(1.0f, 1.0f, 1.0f), Color.Green, new Vector3(1, 0, 1)), new VertexPositionColorNormal(new Vector3(-1.0f, 1.0f, -1.0f), Color.Green, new Vector3(1, 0, 1)), new VertexPositionColorNormal(new Vector3(1.0f, 1.0f, -1.0f), Color.Green, new Vector3(1, 0, 1)), // Bottom Surface new VertexPositionColorNormal(new Vector3(-1.0f, -1.0f, -1.0f), Color.Orange, new Vector3(1, 0, 1)), new VertexPositionColorNormal(new Vector3(-1.0f, -1.0f, 1.0f), Color.Orange, new Vector3(1, 0, 1)), new VertexPositionColorNormal(new Vector3(1.0f, -1.0f, -1.0f), Color.Orange, new Vector3(1, 0, 1)), new VertexPositionColorNormal(new Vector3(1.0f, -1.0f, -1.0f), Color.Orange, new Vector3(1, 0, 1)), new VertexPositionColorNormal(new Vector3(-1.0f, -1.0f, 1.0f), Color.Orange, new Vector3(1, 0, 1)), new VertexPositionColorNormal(new Vector3(1.0f, -1.0f, 1.0f), Color.Orange, new Vector3(1, 0, 1)), }; return vertices; }