protected override void Draw(GameTime gameTime) { base.Draw(gameTime); _engine.Draw(); int n = _flag.MeshData.NumberOfVertices.Value; var positions = _flag.MeshData.PositionsStream.GetData <Vector3>(); var normals = _flag.MeshData.NormalsStream.GetData <Vector3>(); var indicies = _flag.MeshData.IndicesStream.GetData <int>(); ClothVertex[] vertices = new ClothVertex[n]; for (int x = 0; x < n; x++) { vertices[x].Position = positions[x]; vertices[x].Normal = normals[x]; } _lighting.TextureEnabled = false; _lighting.World = Matrix.Identity; _lighting.View = _engine.Camera.View; _lighting.Projection = _engine.Camera.Projection; _lighting.LightingEnabled = false; _lighting.Begin(); foreach (var pass in _lighting.CurrentTechnique.Passes) { pass.Begin(); _engine.Device.DrawUserIndexedPrimitives <ClothVertex>(PrimitiveType.TriangleList, vertices, 0, n, indicies, 0, indicies.Length / 3); pass.End(); } _lighting.End(); }
protected override void LoadContent() { base.LoadContent(); _basicEffect = new BasicEffect(GraphicsDevice); _basicEffect.EnableDefaultLighting(); _numVertices = SIZE * SIZE; int numInternalRows = SIZE - 2; _numIndices = (2 * SIZE * (1 + numInternalRows)) + (2 * numInternalRows); VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[_numVertices]; for (int z = 0; z < SIZE; z++) { for (int x = 0; x < SIZE; x++) { vertices[GetIndex(x, z)] = new VertexPositionNormalTexture( new Vector3(x, 0, -z), new Vector3(0, 1, 0), new Vector2(x / (float)(SIZE - 1) * 8, z / (float)(SIZE - 1) * 8)); } } _vertexBuffer = new VertexBuffer( this.GraphicsDevice, VertexPositionNormalTexture.VertexDeclaration, vertices.Length, BufferUsage.WriteOnly); _vertexBuffer.SetData<VertexPositionNormalTexture>(vertices); short[] indices = new short[_numIndices]; int indexCounter = 0; for (int z = 0; z < SIZE - 1; z++) { // insert index for degenerate triangle if (z > 0) indices[indexCounter++] = GetIndex(0, z); for (int x = 0; x < SIZE; x++) { indices[indexCounter++] = GetIndex(x, z); indices[indexCounter++] = GetIndex(x, z + 1); } // insert index for degenerate triangle if (z < SIZE - 2) indices[indexCounter++] = GetIndex(SIZE - 1, z); } _indexBuffer = new IndexBuffer( this.GraphicsDevice, typeof(short), indices.Length, BufferUsage.WriteOnly); _indexBuffer.SetData<short>(indices); Texture2D texture = Game.Content.Load<Texture2D>(@"Textures\dirt"); _basicEffect.Texture = texture; _basicEffect.TextureEnabled = true; }
void CreateVertexBuffer() { VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[number_of_vertices]; float halfWidth = (number_of_cols - 1) * 0.5f; float halfDepth = (number_of_rows - 1) * 0.5f; float du = 1.0f / (number_of_cols - 1); float dv = 1.0f / (number_of_rows - 1); for (int i = 0; i < number_of_rows; ++i) { float z = halfDepth - i; for (int j = 0; j < number_of_cols;++j) { float x = -halfWidth + j; float y = getHeight(x, z); vertices[i * number_of_cols + j].Position = new Vector3(x, y, z); vertices[i * number_of_cols + j].TextureCoordinate = new Vector2(j * du, i * dv); Vector3 normal = new Vector3(); normal.X = -0.03f * z * (float)Math.Cos(0.1f * x) - 0.3f * (float)Math.Cos(0.1f * z); normal.Y = 1; normal.Z = -0.3f * (float)Math.Sin(0.1f * x) + 0.03f * x *(float)Math.Sin(0.1f * z); normal.Normalize(); vertices[i * number_of_cols + j].Normal = normal; } } vertexBuffer = new VertexBuffer(GraphicsDevice, VertexPositionNormalTexture.VertexDeclaration, number_of_vertices, BufferUsage.WriteOnly); vertexBuffer.SetData<VertexPositionNormalTexture>(vertices); }
public void GenerateStructures() { vertices = new VertexPositionNormalTexture[(dimension + 1) * (dimension + 1)]; indices = new int[dimension * dimension * 6]; for (int i = 0; i < dimension + 1; i++) { for (int j = 0; j < dimension + 1; j++) { VertexPositionNormalTexture vert = new VertexPositionNormalTexture(); vert.Position = new Vector3((i - dimension / 2.0f) * cellSize, 0, (j - dimension / 2.0f) * cellSize); vert.Normal = Vector3.Up; vert.TextureCoordinate = new Vector2((float)i / dimension, (float)j / dimension); vertices[i * (dimension + 1) + j] = vert; } } for (int i = 0; i < dimension; i++) { for (int j = 0; j < dimension; j++) { indices[6 * (i * dimension + j)] = (i * (dimension + 1) + j); indices[6 * (i * dimension + j) + 1] = (i * (dimension + 1) + j + 1); indices[6 * (i * dimension + j) + 2] = ((i + 1) * (dimension + 1) + j + 1); indices[6 * (i * dimension + j) + 3] = (i * (dimension + 1) + j); indices[6 * (i * dimension + j) + 4] = ((i + 1) * (dimension + 1) + j + 1); indices[6 * (i * dimension + j) + 5] = ((i + 1) * (dimension + 1) + j); } } }
public Clouds(Vector2 orbit) { var size = new Vector2(9192, 4096); billboardVertices = new VertexPositionNormalTexture[6]; Vector3 e = new Vector3(-size.X / 2, size.Y, 0); Vector3 f = new Vector3(size.X / 2, size.Y, 0); Vector3 g = new Vector3(size.X / 2, 0, 0); Vector3 h = new Vector3(-size.X / 2, 0, 0); Vector3 frontNormal = new Vector3(0.0f, 0.0f, 1.0f); billboardVertices[0] = new VertexPositionNormalTexture(h, frontNormal, new Vector2(0, 1)); billboardVertices[1] = new VertexPositionNormalTexture(e, frontNormal, new Vector2(0, 0)); billboardVertices[2] = new VertexPositionNormalTexture(f, frontNormal, new Vector2(1, 0)); billboardVertices[3] = new VertexPositionNormalTexture(h, frontNormal, new Vector2(0, 1)); billboardVertices[4] = new VertexPositionNormalTexture(f, frontNormal, new Vector2(1, 0)); billboardVertices[5] = new VertexPositionNormalTexture(g, frontNormal, new Vector2(1, 1)); offsetA = new Vector3(-orbit.X * 4, 0, -orbit.Y * 4); offsetB = new Vector3(-orbit.X * 3, 0, -orbit.Y * 3); offsetC = new Vector3(-orbit.X * 5f, 0, -orbit.Y * 5f); orbitA = new Vector3(orbit.X, 256, orbit.Y); orbitB = new Vector3(orbit.X, 5120, orbit.Y); orbitC = new Vector3(orbit.X, 2048, orbit.Y); degreesA = G.r.Next(360); degreesB = G.r.Next(360); degreesC = G.r.Next(360); CloudA = RM.GetTexture("cloud1"); CloudB = RM.GetTexture("cloud2"); CloudC = RM.GetTexture("cloud3"); }
protected override void BuildPrimitive() { const float MAGIC_NUMBER = 666f; vertices = new VertexPositionNormalTexture[6]; int n = 0; vertices[n++] = new VertexPositionNormalTexture(Vector3.Zero, Vector3.Zero, new Vector2(1, 1)); vertices[n++] = new VertexPositionNormalTexture(Vector3.Zero, Vector3.Zero, new Vector2(1, 0)); vertices[n++] = new VertexPositionNormalTexture(Vector3.Zero, Vector3.Zero, new Vector2(0, 1)); vertices[n++] = new VertexPositionNormalTexture(Vector3.Zero, Vector3.Zero, new Vector2(0, 0)); vertices[n++] = new VertexPositionNormalTexture(Vector3.Zero, Vector3.Zero, new Vector2(0, 1)); vertices[n++] = new VertexPositionNormalTexture(Vector3.Zero, Vector3.Zero, new Vector2(1, 0)); for (int i = 0; i < vertices.Length; ++i) { float u = (float)core.art.GetTileSize() / (float)core.art.GetTiles().Width; float v = (float)core.art.GetTileSize() / (float)core.art.GetTiles().Height; float integral = (float)Math.Truncate(u * (int)tileType); float x = u * (int)tileType - integral; float y = v * integral; vertices[i].TextureCoordinate = vertices[i].TextureCoordinate * new Vector2(u, v) + new Vector2(x, y) + vertices[i].TextureCoordinate * MAGIC_NUMBER; } }
public Dot(Vector3 position, int scale) { if (scale <= 0) throw new ArgumentOutOfRangeException("scale", "Parameter must be grater than zero"); Center = startCenter = position; startVertices = new VertexPositionNormalTexture[3]; currentVertices = new VertexPositionNormalTexture[3]; lineIndices = new short[6]; triangleIndices = new short[6]; currentVertices[0] = new VertexPositionNormalTexture (position - 0.01f * scale * Vector3.UnitX + 0.01f * scale * Vector3.UnitZ, Vector3.Up, Vector2.Zero); currentVertices[1] = new VertexPositionNormalTexture (position + 0.01f * scale * Vector3.UnitX + 0.01f * scale * Vector3.UnitZ, Vector3.Up, Vector2.UnitX); currentVertices[2] = new VertexPositionNormalTexture (position - 0.01f * scale * Vector3.UnitZ, Vector3.Up, Vector2.UnitY); lineIndices[0] = 0; lineIndices[1] = 1; lineIndices[2] = 1; lineIndices[3] = 2; lineIndices[4] = 2; lineIndices[5] = 0; triangleIndices[0] = 0; triangleIndices[1] = 1; triangleIndices[2] = 2; triangleIndices[3] = 2; triangleIndices[4] = 1; triangleIndices[5] = 0; Array.Copy(currentVertices, startVertices, startVertices.Length); }
public Entity(Island i, Vector2 size, Vector3 pos) { this.island = i; billboardVertices = new VertexPositionNormalTexture[6]; reverseBillboardVertices = new VertexPositionNormalTexture[6]; Vector3 e = new Vector3(-size.X / 2, size.Y, 0); Vector3 f = new Vector3(size.X / 2, size.Y, 0); Vector3 g = new Vector3(size.X / 2, 0, 0); Vector3 h = new Vector3(-size.X / 2, 0, 0); this.size = new Vector3(size.X, size.Y, size.X); Vector3 frontNormal = new Vector3(0.0f, 0.0f, 1.0f); billboardVertices[0] = new VertexPositionNormalTexture(h, frontNormal, new Vector2(0, 1)); billboardVertices[1] = new VertexPositionNormalTexture(e, frontNormal, new Vector2(0, 0)); billboardVertices[2] = new VertexPositionNormalTexture(f, frontNormal, new Vector2(1, 0)); billboardVertices[3] = new VertexPositionNormalTexture(h, frontNormal, new Vector2(0, 1)); billboardVertices[4] = new VertexPositionNormalTexture(f, frontNormal, new Vector2(1, 0)); billboardVertices[5] = new VertexPositionNormalTexture(g, frontNormal, new Vector2(1, 1)); this.Position = pos; Vector3 backNormal = new Vector3(0.0f, 0.0f, -1.0f); reverseBillboardVertices[0] = new VertexPositionNormalTexture(h, backNormal, new Vector2(0, 1)); reverseBillboardVertices[1] = new VertexPositionNormalTexture(e, backNormal, new Vector2(0, 0)); reverseBillboardVertices[2] = new VertexPositionNormalTexture(f, backNormal, new Vector2(1, 0)); reverseBillboardVertices[3] = new VertexPositionNormalTexture(h, backNormal, new Vector2(0, 1)); reverseBillboardVertices[4] = new VertexPositionNormalTexture(f, backNormal, new Vector2(1, 0)); reverseBillboardVertices[5] = new VertexPositionNormalTexture(g, backNormal, new Vector2(1, 1)); }
/// <summary> /// Constructor to generate the plane geometry /// </summary> /// <param name="segments">The number of rows/columns</param> public Plane(int segments = 1) { int rowCount = segments + 1; float fSegments = segments; // Initialize the arrays Indices = new int[6 * segments * segments]; Vertices = new VertexPositionNormalTexture[(rowCount) * (rowCount)]; // Populate the vertices for(int i = 0; i <= segments; i++) for (int j = 0; j <= segments; j++) { Vertices[i * rowCount + j] = new VertexPositionNormalTexture( new Vector3(-1 + 2 * j / fSegments, 0, -1 + 2 * i / fSegments), // Position Vector3.Up, // Normal new Vector2(j / fSegments, i / fSegments)); // Texture } // Populate the indices int index = 0; for(int i = 0; i < segments; i++) for (int j = 0; j < segments; j++) { Indices[index++] = i * rowCount + j; Indices[index++] = i * rowCount + j + 1; Indices[index++] = (i + 1) * rowCount + j + 1; Indices[index++] = i * rowCount + j; Indices[index++] = (i + 1) * rowCount + j + 1; Indices[index++] = (i + 1) * rowCount + j; } }
public Create3DPlane(Vector3 position, float size, Vector3 normal) { vertexes = new VertexPositionNormalTexture[6]; //Vértices //1º triângulo Vector3 topLeft = new Vector3(position.X - size / 2, position.Y, -position.Z - size / 2); Vector3 bottomLeft = new Vector3(position.X - size / 2, position.Y, position.Z + size / 2); Vector3 topRight = new Vector3(position.X + size / 2, position.Y, position.Z - size / 2); //2º triângulo //bottomLeft já está definido Vector3 bottomRight = new Vector3(position.X + size / 2, position.Y, position.Z + size / 2); //topRight já está definido // Coordenadas da textura Vector2 textureTopLeft = new Vector2(0.0f, 0.0f); Vector2 textureTopRight = new Vector2(1.0f, 0.0f); Vector2 textureBottomLeft = new Vector2(0.0f, 1.0f); Vector2 textureBottomRight = new Vector2(1.0f, 1.0f); vertexes[0] = new VertexPositionNormalTexture(topLeft, normal, textureTopLeft); vertexes[1] = new VertexPositionNormalTexture(bottomLeft, normal, textureBottomLeft); vertexes[2] = new VertexPositionNormalTexture(topRight, normal, textureTopRight); vertexes[3] = new VertexPositionNormalTexture(bottomLeft, normal, textureBottomLeft); vertexes[4] = new VertexPositionNormalTexture(bottomRight, normal, textureBottomRight); vertexes[5] = new VertexPositionNormalTexture(topRight, normal, textureTopRight); }
public static void GenerateNormalsForTriangleStrip( VertexPositionNormalTexture[] verts, int[] indices ) { // first reset all normals VertexUtils.ResetAllNormals( verts ); // iterate through and add normals to all vertices for each indexed primitive // because indexed as triange strip, need to keep track of swap vert winding to get normals pointing in correct direction bool swapWinding = false; for ( int i = 2; i < indices.Length; i++ ) { Vector3 firstVec = verts[indices[i - 1]].Position - verts[indices[i]].Position; Vector3 secondVec = verts[indices[i - 2]].Position - verts[indices[i]].Position; Vector3 normal = Vector3.Cross( firstVec, secondVec ); normal.Normalize(); if ( swapWinding ) { normal *= -1; } verts[indices[i]].Normal += normal; verts[indices[i - 1]].Normal += normal; verts[indices[i - 2]].Normal += normal; swapWinding = !swapWinding; } VertexUtils.NormalizeAllNormals( verts ); }
public Bilboard(Texture2D tex, Matrix scale) { vertices = new VertexPositionNormalTexture[4]; vertices[0] = new VertexPositionNormalTexture (new Vector3(-0.5f, -0.5f, 0.0f), new Vector3(0.0f,0.0f,1.0f), new Vector2(0.0f,0.0f)); vertices[1] = new VertexPositionNormalTexture (new Vector3(0.5f, -0.5f, 0.0f), new Vector3(0.0f,0.0f,1.0f), new Vector2(1.0f,0.0f)); vertices[2] = new VertexPositionNormalTexture (new Vector3(-0.5f, 0.5f, 0.0f), new Vector3(0.0f,0.0f,1.0f), new Vector2(0.0f,1.0f)); vertices[3] = new VertexPositionNormalTexture (new Vector3(0.5f, 0.5f, 0.0f), new Vector3(0.0f,0.0f,1.0f), new Vector2(1.0f,1.0f)); offsets = new Vector3[4]; offsets [0] = new Vector3 (-0.5f, -0.5f, 0.0f); offsets [1] = new Vector3 (0.5f, -0.5f, 0.0f); offsets [2] = new Vector3 (-0.5f, 0.5f, 0.0f); offsets [3] = new Vector3 (0.5f, 0.5f, 0.0f); indices = new short[6]; indices [0] = 0; indices [1] = 1; indices [2] = 2; indices [3] = 1; indices [4] = 3; indices [5] = 2; this.texture = tex; this.scale = scale; inUse = false; }
public Bullet(Game game) : base(game) { vertex = new VertexPositionNormalTexture[12]; // Base vertex[0] = new VertexPositionNormalTexture(new Vector3(-2.0f, 0.0f, -2.0f), BottomNormal, new Vector2(0.0f, 1.0f)); vertex[1] = new VertexPositionNormalTexture(new Vector3(2.0f, 0.0f, -2.0f), BottomNormal, new Vector2(1.0f, 1.0f)); vertex[2] = new VertexPositionNormalTexture(new Vector3(2.0f, 0.0f, 2.0f), BottomNormal, new Vector2(1.0f, 0.0f)); vertex[3] = new VertexPositionNormalTexture(new Vector3(-2.0f, 0.0f, 2.0f), BottomNormal, new Vector2(0.0f, 0.0f)); // Face 1 and 2 vertex[4] = new VertexPositionNormalTexture(new Vector3(0.0f, 3.0f, 0.0f), TopNormal, new Vector2(0.0f, 0.0f)); vertex[5] = new VertexPositionNormalTexture(new Vector3(2.0f, 0.0f, -2.0f), new Vector3(1, 0, -1), new Vector2(1.0f, 1.0f)); vertex[6] = new VertexPositionNormalTexture(new Vector3(2.0f, 0.0f, 2.0f), new Vector3(1, 0, 1), new Vector2(0.5f, 1.0f)); vertex[7] = new VertexPositionNormalTexture(new Vector3(-2.0f, 0.0f, 2.0f), new Vector3(-1, 0, 1), new Vector2(0.0f, 1.0f)); // Face 3 and 4 vertex[8] = new VertexPositionNormalTexture(new Vector3(0.0f, 3.0f, 0.0f), TopNormal, new Vector2(0.0f, 0.0f)); vertex[9] = new VertexPositionNormalTexture(new Vector3(-2.0f, 0.0f, 2.0f), new Vector3(-1, 0, 1), new Vector2(1.0f, 1.0f)); vertex[10] = new VertexPositionNormalTexture(new Vector3(-2.0f, 0.0f, -2.0f), new Vector3(-1, 0, -1), new Vector2(0.5f, 1.0f)); vertex[11] = new VertexPositionNormalTexture(new Vector3(2.0f, 0.0f, -2.0f), new Vector3(1, 0, -1), new Vector2(0.0f, 1.0f)); triangleListIndices = new short[18] { 0, 2, 1, 0, 3, 2, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11 }; texture_metal = game.Content.Load<Texture2D>("metal3"); }
public void CreateShape() { double angle = MathHelper.TwoPi / CIRCLE_NUM_POINTS; _vertices = new VertexPositionNormalTexture[CIRCLE_NUM_POINTS + 1]; _vertices[0] = new VertexPositionNormalTexture( Vector3.Zero, Vector3.Forward, Vector2.One); for (int i = 1; i <= CIRCLE_NUM_POINTS; i++) { float x = (float)Math.Round(Math.Sin(angle * i), 4); float y = (float)Math.Round(Math.Cos(angle * i), 4); Vector3 point = new Vector3( x, y, 0.0f); _vertices[i] = new VertexPositionNormalTexture( point, Vector3.Forward, new Vector2()); } buffer = new VertexBuffer(graphicsDevice, typeof(VertexPositionNormalTexture), _vertices.Length, BufferUsage.None); // Set the vertex buffer data to the array of vertices buffer.SetData<VertexPositionNormalTexture>(_vertices); InitializeLineStrip(); }
public static void GetShapeMeshData(EntityCollidable collidable, List<VertexPositionNormalTexture> vertices, List<ushort> indices) { var convexHullShape = collidable.Shape as ConvexHullShape; if (convexHullShape == null) throw new ArgumentException("Wrong shape type."); var hullTriangleVertices = new List<Vector3>(); var hullTriangleIndices = new List<int>(); Toolbox.GetConvexHull(convexHullShape.Vertices, hullTriangleIndices, hullTriangleVertices); //The hull triangle vertices are used as a dummy to get the unnecessary hull vertices, which are cleared afterwards. hullTriangleVertices.Clear(); foreach (int i in hullTriangleIndices) { hullTriangleVertices.Add(convexHullShape.Vertices[i]); } var toReturn = new VertexPositionNormalTexture[hullTriangleVertices.Count]; Vector3 normal; for (ushort i = 0; i < hullTriangleVertices.Count; i += 3) { normal = Vector3.Normalize(Vector3.Cross(hullTriangleVertices[i + 2] - hullTriangleVertices[i], hullTriangleVertices[i + 1] - hullTriangleVertices[i])); vertices.Add(new VertexPositionNormalTexture(hullTriangleVertices[i], normal, new Vector2(0, 0))); vertices.Add(new VertexPositionNormalTexture(hullTriangleVertices[i + 1], normal, new Vector2(1, 0))); vertices.Add(new VertexPositionNormalTexture(hullTriangleVertices[i + 2], normal, new Vector2(0, 1))); indices.Add(i); indices.Add((ushort)(i + 1)); indices.Add((ushort)(i + 2)); } }
//See https://electronicmeteor.wordpress.com/2011/10/25/bounding-boxes-for-your-model-meshes/ public static BoundingBox BuildBoundingBox(ModelMesh mesh, Matrix meshTransform) { // Create initial variables to hold min and max xyz values for the mesh Vector3 meshMax = new Vector3(float.MinValue); Vector3 meshMin = new Vector3(float.MaxValue); foreach (ModelMeshPart part in mesh.MeshParts) { // The stride is how big, in bytes, one vertex is in the vertex buffer // We have to use this as we do not know the make up of the vertex int stride = part.VertexBuffer.VertexDeclaration.VertexStride; VertexPositionNormalTexture[] vertexData = new VertexPositionNormalTexture[part.NumVertices]; part.VertexBuffer.GetData(part.VertexOffset * stride, vertexData, 0, part.NumVertices, stride); // Find minimum and maximum xyz values for this mesh part Vector3 vertPosition = new Vector3(); for (int i = 0; i < vertexData.Length; i++) { vertPosition = vertexData[i].Position; // update our values from this vertex meshMin = Vector3.Min(meshMin, vertPosition); meshMax = Vector3.Max(meshMax, vertPosition); } } // transform by mesh bone matrix meshMin = Vector3.Transform(meshMin, meshTransform); meshMax = Vector3.Transform(meshMax, meshTransform); // Create the bounding box return new BoundingBox(meshMin, meshMax); }
private unsafe void StartDrawing(VertexFragment vertexFragment, out VertexPositionColor* vertices, out VertexPositionNormalTexture* textures, out short* indices, out short baseIndex) { textures = null; if (vertexFragment.PrimitiveType == PrimitiveType.LineList) { if ((this.SortMode == DrawingSortMode.Order) && (this._triangleVertexCount > 0)) { Flush(); StartLineDrawing(vertexFragment, out vertices, out indices, out baseIndex); } else { StartLineDrawing(vertexFragment, out vertices, out indices, out baseIndex); } } else if (vertexFragment.PrimitiveType == PrimitiveType.TriangleList) { if ((this.SortMode == DrawingSortMode.Order) && (this._lineVertexCount > 0)) { Flush(); StartTriangleDrawing(vertexFragment, out vertices, out textures, out indices, out baseIndex); } else { StartTriangleDrawing(vertexFragment, out vertices, out textures, out indices, out baseIndex); } } else { throw new NotSupportedException(string.Format("PrimitiveType: {0} is not supported.", vertexFragment.PrimitiveType)); } }
public void drawSquarre(VertexPositionNormalTexture[] vertexData, int[] indexData, Camera came, BasicEffect effect, GraphicsDevice graphicsDevice) { Texture2D texture = Tools.Quick.groundTexture[BiomeType.SubtropicalDesert]; effect.Projection = projectionMatrix; effect.Texture = texture; effect.TextureEnabled = true; ; graphicsDevice.RasterizerState = WIREFRAME_RASTERIZER_STATE; // draw in wireframe graphicsDevice.BlendState = BlendState.Opaque; // no alpha this time // effect.DiffuseColor = Color.Red.ToVector3(); effect.CurrentTechnique.Passes[0].Apply(); effect.View = came.getview(); effect.CurrentTechnique.Passes[0].Apply(); graphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, vertexData, 0, 4, indexData, 0, 2); /* // Draw wireframe box graphicsDevice.RasterizerState = WIREFRAME_RASTERIZER_STATE; // draw in wireframe graphicsDevice.BlendState = BlendState.Opaque; // no alpha this time effect.TextureEnabled = false; // effect.DiffuseColor = Color.Black.ToVector3(); effect.CurrentTechnique.Passes[0].Apply(); graphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, vertexData, 0, 4, indexData, 0, 2); */ }
public IndexedVertexModel(Vector3[] positions, Vector3[] normals, short[] indices) { Indices = indices; var texdata = new[] { new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1) }; Vertices = new VertexPositionNormalTexture[positions.Length]; for (int i = 0; i < positions.Length; i++) Vertices[i] = new VertexPositionNormalTexture(positions[i], normals[i], texdata[i % 3]); }
public MeshPart(VertexPositionNormalTexture[] vertices, ushort[] indices) { if (vertices == null) throw new ArgumentNullException("vertices"); if (indices == null) throw new ArgumentNullException("indices"); Vertices = vertices; Indices = indices; }
// Creates the graphics resources (e.g. the vertex buffer). public void LoadContent(GraphicsDevice graphicsDevice, IGeometricObject geometricObject) { // Create a mesh for the given shape. (The arguments define the desired resolution if the // mesh is only approximated - for example for a sphere.) TriangleMesh mesh = geometricObject.Shape.GetMesh(0.01f, 3); // Abort if we have nothing to draw. (This happens for "EmptyShapes".) if (mesh.Vertices.Count == 0) return; // Apply the scaling that is defined in the geometric object. if (geometricObject.Scale != Vector3F.One) mesh.Transform(Matrix44F.CreateScale(geometricObject.Scale)); _numberOfTriangles = mesh.NumberOfTriangles; // Create vertex data for a triangle list. VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[mesh.NumberOfTriangles * 3]; // Create vertex normals. Do not merge normals if the angle between the triangle normals // is > 70°. Vector3F[] normals = mesh.ComputeNormals(false, MathHelper.ToRadians(70)); // Loop over all triangles and copy vertices to the vertices array. for (int i = 0; i < _numberOfTriangles; i++) { // Get next triangle of the mesh. Triangle triangle = mesh.GetTriangle(i); // Add new vertex data. // DigitalRune.Geometry uses counter-clockwise front faces. XNA uses // clockwise front faces (CullMode.CullCounterClockwiseFace) per default. // Therefore we change the vertex orientation of the triangles. // We could also keep the vertex order and change the CullMode to CullClockwiseFace. vertices[i * 3 + 0] = new VertexPositionNormalTexture( (Vector3)triangle.Vertex0, (Vector3)normals[i * 3 + 0], Vector2.Zero); vertices[i * 3 + 1] = new VertexPositionNormalTexture( (Vector3)triangle.Vertex2, // triangle.Vertex2 instead of triangle.Vertex1 to change vertex order! (Vector3)normals[i * 3 + 2], Vector2.Zero); vertices[i * 3 + 2] = new VertexPositionNormalTexture( (Vector3)triangle.Vertex1, // triangle.Vertex1 instead of triangle.Vertex2 to change vertex order! (Vector3)normals[i * 3 + 1], Vector2.Zero); } // Create a vertex buffer. _vertexBuffer = new VertexBuffer( graphicsDevice, vertices[0].GetType(), vertices.Length, BufferUsage.WriteOnly); // Fill the vertex buffer. _vertexBuffer.SetData(vertices); }
public VertexPositionNormalTexture[] GetVertexColumn(int c) { VertexPositionNormalTexture[] col = new VertexPositionNormalTexture[BLOCK_VERT_WIDTH]; for (int i = 0; i < BLOCK_VERT_WIDTH; i++) { col[i] = vertices[i * BLOCK_VERT_WIDTH + c]; } return col; }
public void CreateFromVertex(VertexPositionNormalTexture[] vertex) { Vector3[] array = new Vector3[vertex.Length]; for (int i = 0; i < array.Length; i++) { array[i] = vertex[i].Position; } this.box = BoundingBox.CreateFromPoints(array); }
public Square(Vector3 origin, Vector3 normal, Vector3 up, float height, float width,string type) { Vertices = new VertexPositionNormalTexture[4]; Indexes = new short[6]; Origin = origin; Tex = Game1.contentManager.Load<Texture2D>(type); SetVertices(up,normal,origin,height,width); }
public void Initialize( GraphicsDevice gd ) { this.graphicsDevice = gd; InitializeView(); InitializeEffect(); vertexDeclaration = new VertexDeclaration( VertexPositionNormalTexture.VertexDeclaration.GetVertexElements() ); double angle = MathHelper.TwoPi / 8; pointList = new VertexPositionNormalTexture[ points ]; //for( int i = 1; i <= points; ++i ) //{ // pointList[ i ] = new VertexPositionNormalTexture( // new Vector3( ( float )Math.Round( Math.Sin( angle * i ), 4 ), // ( float )Math.Round( Math.Cos( angle * i ), 4 ), // 0.0f ), Vector3.Backward, new Vector2() ); //} pointList[ 0 ] = new VertexPositionNormalTexture( new Vector3( position.X + scale * ( float )Math.Round( Math.Sin( angle * 1 ), 4 ), position.Y + scale * ( float )Math.Round( Math.Cos( angle * 1 ), 4 ), 0.0f ), Vector3.Backward, new Vector2() ); pointList[ 1 ] = new VertexPositionNormalTexture( new Vector3( position.X + scale * ( float )Math.Round( Math.Sin( angle * 3 ), 4 ), position.Y + scale * ( float )Math.Round( Math.Cos( angle * 3 ), 4 ), 0.0f ), Vector3.Backward, new Vector2() ); pointList[ 2 ] = new VertexPositionNormalTexture( new Vector3( position.X + scale * ( float )Math.Round( Math.Sin( angle * 5 ), 4 ), position.Y + scale * ( float )Math.Round( Math.Cos( angle * 5 ), 4 ), 0.0f ), Vector3.Backward, new Vector2() ); pointList[ 3 ] = new VertexPositionNormalTexture( new Vector3( position.X + scale * ( float )Math.Round( Math.Sin( angle * 5 ), 4 ), position.Y + scale * ( float )Math.Round( Math.Cos( angle * 5 ), 4 ), 0.0f ), Vector3.Backward, new Vector2() ); pointList[ 4 ] = new VertexPositionNormalTexture( new Vector3( position.X + scale * ( float )Math.Round( Math.Sin( angle * 7 ), 4 ), position.Y + scale * ( float )Math.Round( Math.Cos( angle * 7 ), 4 ), 0.0f ), Vector3.Backward, new Vector2() ); pointList[ 5 ] = new VertexPositionNormalTexture( new Vector3( position.X + scale * ( float )Math.Round( Math.Sin( angle * 1 ), 4 ), position.Y + scale * ( float )Math.Round( Math.Cos( angle * 1 ), 4 ), 0.0f ), Vector3.Backward, new Vector2() ); //頂点バッファを初期化し,拡張点についてメモリを割り当てる vertexBuffer = new VertexBuffer( graphicsDevice, VertexPositionNormalTexture.VertexDeclaration, pointList.Length, BufferUsage.None ); //頂点の配列に頂点バッファのデータを設定 vertexBuffer.SetData( pointList ); }
void setupground() { ground[0] = new VertexPositionNormalTexture(new Vector3(0, 0, 0), Vector3.Up, new Vector2(0, 0)); ground[1] = new VertexPositionNormalTexture(new Vector3(1, 0, 0), Vector3.Up, new Vector2(1, 0)); ground[2] = new VertexPositionNormalTexture(new Vector3(1, 0, 1), Vector3.Up, new Vector2(1, 1)); ground[3] = new VertexPositionNormalTexture(new Vector3(1, 0, 1), Vector3.Up, new Vector2(1, 1)); ground[4] = new VertexPositionNormalTexture(new Vector3(0, 0, 1), Vector3.Up, new Vector2(0, 1)); ground[5] = new VertexPositionNormalTexture(new Vector3(0, 0, 0), Vector3.Up, new Vector2(0, 0)); }
public void AddData(VertexPositionNormalTexture[] VertexList, short[] IndexList) { int _count = this.VertexList.Count; this.VertexList.AddRange(VertexList.ToList<VertexPositionNormalTexture>()); foreach (short index in IndexList) { this.IndexList.Add((short)(index + _count)); } }
protected override void LoadContent() { vertices = new VertexPositionNormalTexture[36]; // Vorne (-y) vertices[00] = new VertexPositionNormalTexture(new Vector3(-0.5f, -0.5f, +0.5f), new Vector3(0, -1f, 0), new Vector2(0f, 0f)); vertices[01] = new VertexPositionNormalTexture(new Vector3(+0.5f, -0.5f, +0.5f), new Vector3(0, -1f, 0), new Vector2(1f, 0f)); vertices[02] = new VertexPositionNormalTexture(new Vector3(-0.5f, -0.5f, -0.5f), new Vector3(0, -1f, 0), new Vector2(0f, 1f)); vertices[03] = new VertexPositionNormalTexture(new Vector3(+0.5f, -0.5f, +0.5f), new Vector3(0, -1f, 0), new Vector2(1f, 0f)); vertices[04] = new VertexPositionNormalTexture(new Vector3(+0.5f, -0.5f, -0.5f), new Vector3(0, -1f, 0), new Vector2(1f, 1f)); vertices[05] = new VertexPositionNormalTexture(new Vector3(-0.5f, -0.5f, -0.5f), new Vector3(0, -1f, 0), new Vector2(0f, 1f)); // Hinten (+y) vertices[06] = new VertexPositionNormalTexture(new Vector3(+0.5f, +0.5f, +0.5f), new Vector3(0, +1f, 0), new Vector2(0f, 0f)); vertices[07] = new VertexPositionNormalTexture(new Vector3(-0.5f, +0.5f, +0.5f), new Vector3(0, +1f, 0), new Vector2(1f, 0f)); vertices[08] = new VertexPositionNormalTexture(new Vector3(+0.5f, +0.5f, -0.5f), new Vector3(0, +1f, 0), new Vector2(0f, 1f)); vertices[09] = new VertexPositionNormalTexture(new Vector3(-0.5f, +0.5f, +0.5f), new Vector3(0, +1f, 0), new Vector2(1f, 0f)); vertices[10] = new VertexPositionNormalTexture(new Vector3(-0.5f, +0.5f, -0.5f), new Vector3(0, +1f, 0), new Vector2(1f, 1f)); vertices[11] = new VertexPositionNormalTexture(new Vector3(+0.5f, +0.5f, -0.5f), new Vector3(0, +1f, 0), new Vector2(0f, 1f)); // Rechts (+x) vertices[12] = new VertexPositionNormalTexture(new Vector3(+0.5f, -0.5f, +0.5f), new Vector3(+1f, 0, 0), new Vector2(0f, 0f)); vertices[13] = new VertexPositionNormalTexture(new Vector3(+0.5f, +0.5f, +0.5f), new Vector3(+1f, 0, 0), new Vector2(1f, 0f)); vertices[14] = new VertexPositionNormalTexture(new Vector3(+0.5f, -0.5f, -0.5f), new Vector3(+1f, 0, 0), new Vector2(0f, 1f)); vertices[15] = new VertexPositionNormalTexture(new Vector3(+0.5f, +0.5f, +0.5f), new Vector3(+1f, 0, 0), new Vector2(1f, 0f)); vertices[16] = new VertexPositionNormalTexture(new Vector3(+0.5f, +0.5f, -0.5f), new Vector3(+1f, 0, 0), new Vector2(1f, 1f)); vertices[17] = new VertexPositionNormalTexture(new Vector3(+0.5f, -0.5f, -0.5f), new Vector3(+1f, 0, 0), new Vector2(0f, 1f)); // Links (-x) vertices[18] = new VertexPositionNormalTexture(new Vector3(-0.5f, +0.5f, +0.5f), new Vector3(-1f, 0, 0), new Vector2(0f, 0f)); vertices[19] = new VertexPositionNormalTexture(new Vector3(-0.5f, -0.5f, +0.5f), new Vector3(-1f, 0, 0), new Vector2(1f, 0f)); vertices[20] = new VertexPositionNormalTexture(new Vector3(-0.5f, +0.5f, -0.5f), new Vector3(-1f, 0, 0), new Vector2(0f, 1f)); vertices[21] = new VertexPositionNormalTexture(new Vector3(-0.5f, -0.5f, +0.5f), new Vector3(-1f, 0, 0), new Vector2(1f, 0f)); vertices[22] = new VertexPositionNormalTexture(new Vector3(-0.5f, -0.5f, -0.5f), new Vector3(-1f, 0, 0), new Vector2(1f, 1f)); vertices[23] = new VertexPositionNormalTexture(new Vector3(-0.5f, +0.5f, -0.5f), new Vector3(-1f, 0, 0), new Vector2(0f, 1f)); // Oben (+z) vertices[24] = new VertexPositionNormalTexture(new Vector3(-0.5f, +0.5f, +0.5f), new Vector3(0, 0, +1f), new Vector2(0f, 0f)); vertices[25] = new VertexPositionNormalTexture(new Vector3(+0.5f, +0.5f, +0.5f), new Vector3(0, 0, +1f), new Vector2(1f, 0f)); vertices[26] = new VertexPositionNormalTexture(new Vector3(-0.5f, -0.5f, +0.5f), new Vector3(0, 0, +1f), new Vector2(0f, 1f)); vertices[27] = new VertexPositionNormalTexture(new Vector3(+0.5f, +0.5f, +0.5f), new Vector3(0, 0, +1f), new Vector2(1f, 0f)); vertices[28] = new VertexPositionNormalTexture(new Vector3(+0.5f, -0.5f, +0.5f), new Vector3(0, 0, +1f), new Vector2(1f, 1f)); vertices[29] = new VertexPositionNormalTexture(new Vector3(-0.5f, -0.5f, +0.5f), new Vector3(0, 0, +1f), new Vector2(0f, 1f)); // Unten (-z) vertices[30] = new VertexPositionNormalTexture(new Vector3(-0.5f, -0.5f, -0.5f), new Vector3(0, 0, -1f), new Vector2(0f, 0f)); vertices[31] = new VertexPositionNormalTexture(new Vector3(+0.5f, -0.5f, -0.5f), new Vector3(0, 0, -1f), new Vector2(1f, 0f)); vertices[32] = new VertexPositionNormalTexture(new Vector3(-0.5f, +0.5f, -0.5f), new Vector3(0, 0, -1f), new Vector2(0f, 1f)); vertices[33] = new VertexPositionNormalTexture(new Vector3(+0.5f, -0.5f, -0.5f), new Vector3(0, 0, -1f), new Vector2(1f, 0f)); vertices[34] = new VertexPositionNormalTexture(new Vector3(+0.5f, +0.5f, -0.5f), new Vector3(0, 0, -1f), new Vector2(1f, 1f)); vertices[35] = new VertexPositionNormalTexture(new Vector3(-0.5f, +0.5f, -0.5f), new Vector3(0, 0, -1f), new Vector2(0f, 1f)); effect = new BasicEffect(GraphicsDevice); simple = Content.Load<Effect>("simple"); wood = Content.Load<Texture2D>("wood"); }
public void CreateVertices() { int vertexCount = 4; vertices = new VertexPositionNormalTexture[vertexCount]; vertices[0] = new VertexPositionNormalTexture(new Vector3(-1.0f, 0.0f, +1.0f), new Vector3(0,1,0), new Vector2(0.0f, 1.0f)); vertices[1] = new VertexPositionNormalTexture(new Vector3(-1.0f, 0.0f, -1.0f), new Vector3(0,1,0), new Vector2(0.0f, 0.0f)); vertices[2] = new VertexPositionNormalTexture(new Vector3(+1.0f, 0.0f, +1.0f), new Vector3(0,1,0), new Vector2(1.0f, 1.0f)); vertices[3] = new VertexPositionNormalTexture(new Vector3(+1.0f, 0.0f, -1.0f), new Vector3(0,1,0), new Vector2(1.0f, 0.0f)); }
public Terrain(Texture2D heightmap, Vector2 dimensions, int resolution) { Heightmap = heightmap; Data = new Color[Heightmap.Width * Heightmap.Height]; Heightmap.GetData<Color>(Data); Vertices = new VertexPositionNormalTexture[resolution * resolution]; Indices = new int[(resolution - 1) * (resolution - 1) * 6]; Vector3 start = new Vector3(-dimensions.X / 2, 0, -dimensions.Y / 2); Vector2 step = dimensions / (resolution - 1); for (int i = 0; i < resolution; i++) { for (int j = 0; j < resolution; j++) { Vertices[i * resolution + j].Position = start + new Vector3(step.X * i, 0, step.Y * j); Vertices[i * resolution + j].Normal = Vector3.Zero; Vertices[i * resolution + j].TextureCoordinate = new Vector2(i / (float)(resolution - 1), j / (float)(resolution - 1)); Vertices[i * resolution + j].Position.Y = GetElevation(Vertices[i * resolution + j].TextureCoordinate); } } int index = 0; for (int i = 0; i < resolution - 1; i++) { for (int j = 0; j < resolution - 1; j++) { Indices[index++] = i * resolution + j; Indices[index++] = (i + 1) * resolution + j; Indices[index++] = (i + 1) * resolution + j + 1; Vector3 normal = Vector3.Cross(Vertices[Indices[index - 2]].Position - Vertices[Indices[index - 3]].Position, Vertices[Indices[index - 1]].Position - Vertices[Indices[index - 3]].Position); Vertices[Indices[index - 3]].Normal += normal; Vertices[Indices[index - 2]].Normal += normal; Vertices[Indices[index - 1]].Normal += normal; Indices[index++] = i * resolution + j; Indices[index++] = (i + 1) * resolution + j + 1; Indices[index++] = i * resolution + j + 1; normal = Vector3.Cross(Vertices[Indices[index - 2]].Position - Vertices[Indices[index - 3]].Position, Vertices[Indices[index - 1]].Position - Vertices[Indices[index - 3]].Position); Vertices[Indices[index - 3]].Normal += normal; Vertices[Indices[index - 2]].Normal += normal; Vertices[Indices[index - 1]].Normal += normal; } } foreach (VertexPositionNormalTexture vertex in Vertices) { vertex.Normal.Normalize(); } }
public SelectableQuadE(Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4, Color c) : base() { color = new Vector3((float)c.R / 255, (float)c.G / 255, (float)c.B / 255); Vector3 upVector = new Vector3(0, 0, 1); Vector2 texCoord = new Vector2(0, 0); screenPoints[0] = new VertexPositionNormalTexture(p1, upVector, texCoord); screenPoints[1] = new VertexPositionNormalTexture(p2, upVector, texCoord); screenPoints[2] = new VertexPositionNormalTexture(p3, upVector, texCoord); screenPoints[3] = new VertexPositionNormalTexture(p4, upVector, texCoord); }
public override void Initialize() { base.Initialize(); // Vertices und Indices erstellen numVertices = (width + 1) * (height + 1); numIndices = width * height * 6; indices = new int[numIndices]; vertices = new Vertex[numVertices]; // Vertices initialisieren for (int i = 0; i < width + 1; i++) { for (int j = 0; j < height + 1; j++) { vertices[GetIndex(i, j)] = new Vertex() { Position = new Vector3(i, 0.0f, -j), Normal = Vector3.Up, TextureCoordinate = new Vector2(i * texRes * totalWidth / width, j * texRes * totalHeight / height) }; } } int count = 0; // Indices initialisieren for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { indices[count++] = GetIndex(i, j); indices[count++] = GetIndex(i, j + 1); indices[count++] = GetIndex(i + 1, j + 1); indices[count++] = GetIndex(i + 1, j + 1); indices[count++] = GetIndex(i + 1, j); indices[count++] = GetIndex(i, j); } } // Vertex- und Indexbuffer ertellen und initialisieren vertexBuffer = new VertexBuffer(GraphicsDevice, typeof(Vertex), numVertices, BufferUsage.WriteOnly); indexBuffer = new IndexBuffer(GraphicsDevice, typeof(int), numIndices, BufferUsage.WriteOnly); vertexBuffer.SetData <Vertex>(vertices); indexBuffer.SetData <int>(indices); }
private void GenerateBuffers(gp.Pnt[] points, gp.Pnt2d[] uvpoints, int[] triangles) { Vector3 edge1, edge2, normal; uint[] normalsCount = new uint[points.Length]; Vector3[] normals = new Vector3[points.Length]; VertexPositionNormalTexture[] vertex = new Microsoft.Xna.Framework.Graphics.VertexPositionNormalTexture[points.Length]; for (int ind = 0; ind < triangles.Length; ind += 3) { edge1 = new Vector3( (float)(points[triangles[ind]].x - points[triangles[ind + 1]].x), (float)(points[triangles[ind]].y - points[triangles[ind + 1]].y), (float)(points[triangles[ind]].z - points[triangles[ind + 1]].z)); edge2 = new Vector3( (float)(points[triangles[ind + 2]].x - points[triangles[ind + 1]].x), (float)(points[triangles[ind + 2]].y - points[triangles[ind + 1]].y), (float)(points[triangles[ind + 2]].z - points[triangles[ind + 1]].z)); Vector3.Cross(ref edge1, ref edge2, out normal); edge1 = new Vector3( (float)(points[triangles[ind]].x), (float)(points[triangles[ind]].y), (float)(points[triangles[ind]].z)); if (Vector3.Dot(normal, edge1) < 0) { normal *= -1.0f; } normal.Normalize(); normals[triangles[ind]] += normal; ++normalsCount[triangles[ind]]; normals[triangles[ind + 1]] += normal; ++normalsCount[triangles[ind + 1]]; normals[triangles[ind + 2]] += normal; ++normalsCount[triangles[ind + 2]]; } for (int i = 0; i < points.Length; i++) { normals[i] /= (float)normalsCount[i]; Vector3 pos = new Vector3((float)points[i].x, (float)points[i].y, (float)points[i].z); //pos *= 5; Vector2 tex = new Vector2((float)uvpoints[i].x, (float)uvpoints[i].y); vertex[i] = new Microsoft.Xna.Framework.Graphics.VertexPositionNormalTexture( pos, normals[i], tex); } indexData.Add(triangles); vertexData.Add(vertex); }
private void InitializeBuffers() { Vertex[] vertices = new Vertex[Size * Size]; for (int i = 0; i < Size; i++) { for (int j = 0; j < Size; j++) { vertices[i + j * Size].Position = new Vector3(i, 0.0f, -j); } } int[] indices = new int[(Size - 1) * (Size - 1) * 6]; int count = 0; for (int i = 0; i < Size - 1; i++) { for (int j = 0; j < Size - 1; j++) { indices[count++] = GetIndex(i, j); indices[count++] = GetIndex(i, j + 1); indices[count++] = GetIndex(i + 1, j); indices[count++] = GetIndex(i, j + 1); indices[count++] = GetIndex(i + 1, j + 1); indices[count++] = GetIndex(i + 1, j); } } int maxIndices = (Size - 1) * (Size - 1) * 6; VertexBuffer = new DynamicVertexBuffer(GraphicsDevice, typeof(Vertex), vertices.Length, BufferUsage.None); IndexBuffer = new DynamicIndexBuffer(GraphicsDevice, typeof(int), maxIndices, BufferUsage.None); BasicIndexBuffer = new IndexBuffer(GraphicsDevice, typeof(int), maxIndices, BufferUsage.None); VertexBuffer.SetData <Vertex>(vertices); BasicIndexBuffer.SetData <int>(indices); }
public override void Initialize() { base.Initialize(); // Grund-Positionen für die Ecken einer Seite eines Würfels positions = new Vector3[4] { new Vector3(-1.0f, -1.0f, 0.0f), new Vector3(-1.0f, 1.0f, 0.0f), new Vector3(1.0f, 1.0f, 0.0f), new Vector3(1.0f, -1.0f, 0.0f) }; // Indices für die zwei Dreiecke einer Seite triangles = new int[2][] { new int[3] { 0, 1, 2 }, new int[3] { 2, 3, 0 } }; // rotes Dreieck für die Anzeige des geschnittenen Dreiecks triangle = new VertexPositionColor[3]; for (int i = 0; i < 3; i++) { triangle[i].Color = Color.Red; } // vier Vertices zum Rendern einer Seite eines Würfels Vertex[] vertices = new Vertex[4]; for (int i = 0; i < 4; i++) { vertices[i].Position = positions[i]; vertices[i].Normal = Vector3.Backward; } // eine Transformationsmatrix pro Seite eines Würfels transformations = new Matrix[6]; for (int i = 0; i < 6; i++) { transformations[i] = Matrix.CreateTranslation(Vector3.Backward); if (i < 4) { transformations[i] *= Matrix.CreateRotationY(i * MathHelper.PiOver2); } else { transformations[i] *= Matrix.CreateRotationX(((i - 4) * 2 - 1) * MathHelper.PiOver2); } } effect = new BasicEffect(GraphicsDevice); vertexBuffer = new VertexBuffer(GraphicsDevice, typeof(Vertex), 4, BufferUsage.WriteOnly); indexBuffer = new IndexBuffer(GraphicsDevice, IndexElementSize.ThirtyTwoBits, 4, BufferUsage.WriteOnly); // Vertex- und Indexbuffer zum Rendern einer Seite eines Würfels vertexBuffer.SetData <Vertex>(vertices); indexBuffer.SetData <int>(new int[4] { 0, 1, 3, 2 }); camera = (ICameraService)Game.Services.GetService(typeof(ICameraService)); Random random = new Random(0); // Würfel zufällig generieren for (int i = 0; i < 100; i++) { AddCube((new Vector3(Next(), Next(), Next()) * 2.0f - Vector3.One) * 100.0f, Next() * 10.0f); } spriteBatch = new SpriteBatch(GraphicsDevice); }
public static bool TryParse(Stream s, out VertexPositionNormalTexture[] verts, out int[] inds, ParsingFlags ps = ParsingFlags.None) { // Default Values verts = null; inds = null; // Encapsulate Stream To A Buffered Stream Reader BufferedStream bs = new BufferedStream(s); StreamReader f = new StreamReader(bs); // List Of Components List <Vector3> pos = new List <Vector3>(100); List <Vector2> uv = new List <Vector2>(100); List <Vector3> norms = new List <Vector3>(100); List <Tri> tris = new List <Tri>(200); // Buffer Vectors Vector3 v3 = Vector3.Zero; Vector2 v2 = Vector2.Zero; // Get All The Information From The Stream string line; string[] spl; while (!f.EndOfStream) { line = f.ReadLine(); spl = Regex.Split(line, @"\s+", RegexOptions.IgnorePatternWhitespace); switch (spl[0].ToLower()) { case "v": // Vertex Position if (spl.Length != 4) { return(false); } if (!float.TryParse(spl[1], out v3.X)) { return(false); } if (!float.TryParse(spl[2], out v3.Y)) { return(false); } if (!float.TryParse(spl[3], out v3.Z)) { return(false); } pos.Add(v3); break; case "vt": // Vertex Texture Coordinate if (spl.Length != 3) { return(false); } if (!float.TryParse(spl[1], out v2.X)) { return(false); } if (!float.TryParse(spl[2], out v2.Y)) { return(false); } // Possibly Flip Tex Coords if (ps.HasFlag(ParsingFlags.FlipTexCoordV)) { v2.Y = 1 - v2.Y; } uv.Add(v2); break; case "vn": // Vertex Normal if (spl.Length != 4) { return(false); } if (!float.TryParse(spl[1], out v3.X)) { return(false); } if (!float.TryParse(spl[2], out v3.Y)) { return(false); } if (!float.TryParse(spl[3], out v3.Z)) { return(false); } norms.Add(v3); break; case "f": // Mesh Triangle if (spl.Length != 4) { return(false); } try { // Add In Correct Triangle Ordering if (ps.HasFlag(ParsingFlags.FlipTriangleOrder)) { tris.Add(new Tri(spl[1], spl[3], spl[2])); } else { tris.Add(new Tri(spl[1], spl[2], spl[3])); } } catch (Exception) { return(false); } break; } } // Create Indices VertDict vd = new VertDict(); inds = new int[tris.Count * 3]; int ii = 0; foreach (Tri tri in tris) { inds[ii++] = vd.Get(tri.V1); inds[ii++] = vd.Get(tri.V2); inds[ii++] = vd.Get(tri.V3); } // Create Vertices verts = new VertexPositionNormalTexture[vd.Count]; foreach (VertDict.Key v in vd) { verts[v.Index].Position = pos[v.Vertex.PosInd]; if (v.Vertex.UVInd < 0) { verts[v.Index].TextureCoordinate = Vector2.Zero; } else { verts[v.Index].TextureCoordinate = uv[v.Vertex.UVInd]; } if (v.Vertex.NormInd < 0) { verts[v.Index].Normal = Vector3.Zero; } else { verts[v.Index].Normal = norms[v.Vertex.NormInd]; } } return(true); }
public ObjTriangle(VertexPositionNormalTexture v1, VertexPositionNormalTexture v2, VertexPositionNormalTexture v3) { // Set Vertices V1 = v1; V2 = v2; V3 = v3; // Calculate Face Normal Normal = Vector3.Cross( V3.Position - V1.Position, V2.Position - V1.Position ); Normal.Normalize(); }