void InitAtmosphere() { if(!Directory.Exists("cache/Atmosphere")) return; var tindexes = new List<int>(); int rowsCount = 25; int columnsCount = 25; for (int row = 0; row < rowsCount - 1; row++) { for (int col = 0; col < columnsCount - 1; col++) { tindexes.Add(col + row * columnsCount); tindexes.Add(col + (row + 1) * columnsCount); tindexes.Add(col + 1 + row * columnsCount); tindexes.Add(col + 1 + row * columnsCount); tindexes.Add(col + (row + 1) * columnsCount); tindexes.Add(col + 1 + (row + 1) * columnsCount); } } atmosIB = new IndexBuffer(Game.GraphicsDevice, tindexes.Count); atmosIB.SetData(tindexes.ToArray(), 0, tindexes.Count); atmosVB = new VertexBuffer(Game.GraphicsDevice, typeof(GeoVert), rowsCount * columnsCount); atmosTexture = new Texture2D(Game.GraphicsDevice, columnsCount, rowsCount, ColorFormat.Rgb32F, false); atmosNextTexture = new Texture2D(Game.GraphicsDevice, columnsCount, rowsCount, ColorFormat.Rgb32F, false); arrowTex = Game.Content.Load<Texture2D>("arrow.tga"); var verts = LoadGrid(Directory.GetFiles("cache/Atmosphere/Temperature")[0]); atmosVB.SetData(verts.ToArray(), 0, verts.Count); LoadWindAndTempFromFolder(); atmosTexture.SetData(atmosData[0]); atmosNextTexture.SetData(atmosData[1]); }
/// <summary> /// Load content /// </summary> public void LoadContent () { uberShader = Content.Load<Ubershader>("render"); factory = new StateFactory(uberShader, typeof(RenderFlags), Primitive.TriangleList, VertexInputElement.FromStructure<CubeVertex>()); texture = Content.Load<Texture2D>(@"Scenes\lena"); vb = new VertexBuffer(GraphicsDevice, typeof(CubeVertex), 24); ib = new IndexBuffer(GraphicsDevice, 36); // create a new space with physics space = new Space(); // update gravity force space.ForceUpdater.Gravity = new Vector3BEPU(0, -9.81f, 0); // add ground, ground has infinite mass Box ground = new Box(new Vector3BEPU(0, 0, 0), 50, 1, 50); space.Add(ground); // create boxes with random position and add color as a tag, then add box to space for ( int i = 0; i < numberOfBoxes; i++ ) { Vector3Fusion vector = RandomExt.NextVector3(random, new Vector3Fusion(-10, 20, -10), new Vector3Fusion(10, 80, 10)); Box box = new Box(new Vector3BEPU(vector.X, vector.Y, vector.Z), 1, 1, 1, 1); box.Tag = RandomExt.NextColor(random); space.Add(box); } }
void CreateSphere(int Stacks, int Slices) { //calculates the resulting number of vertices and indices int nVertices = (Stacks + 1) * (Slices + 1); int dwIndices = (3 * Stacks * (Slices + 1)) * 2; int[] indices = new int[dwIndices]; GeoVert[] vertices = new GeoVert[nVertices]; double stackAngle = Math.PI / Stacks; double sliceAngle = (Math.PI * 2.0) / Slices; int wVertexIndex = 0; //Generate the group of Stacks for the sphere int vertcount = 0; int indexcount = 0; for (int stack = 0; stack < (Stacks + 1); stack++) { double phi = stack * stackAngle - Math.PI / 2.0; //Generate the group of segments for the current Stack for (int slice = 0; slice < (Slices + 1); slice++) { double lambda = slice * sliceAngle; vertices[vertcount].Lon = lambda + Math.PI; vertices[vertcount].Lat = phi; vertices[vertcount].Position = new Vector3(); vertices[vertcount].Tex = new Vector4((float)slice / (float)Slices, 1.0f - (float)stack / (float)Stacks, 0, 0); vertices[vertcount].Color = Color.White; //vertices[vertcount].TextureCoordinate = new Vector2((float)slice / (float)Slices, (float)stack / (float)Stacks); vertcount++; if (stack != (Stacks - 1)) { indices[indexcount] = wVertexIndex; indexcount++; indices[indexcount] = wVertexIndex + 1; indexcount++; indices[indexcount] = wVertexIndex + (Slices + 1); indexcount++; indices[indexcount] = wVertexIndex; indexcount++; indices[indexcount] = wVertexIndex + (Slices + 1); indexcount++; indices[indexcount] = wVertexIndex + (Slices); indexcount++; wVertexIndex++; } } } if (gridVertexBuffer != null) gridVertexBuffer.Dispose(); if (gridIndexBuffer != null) gridIndexBuffer.Dispose(); if (gridTex == null) gridTex = Game.Content.Load<Texture2D>("NE2_50M_SR_W_4096.jpg"); gridVertexBuffer = new VertexBuffer(Game.GraphicsDevice, typeof(GeoVert), vertices.Length); gridIndexBuffer = new IndexBuffer(Game.GraphicsDevice, indices.Length); gridVertexBuffer.SetData(vertices, 0, vertices.Length); gridIndexBuffer.SetData(indices, 0, indices.Length); return; }
/// <summary> /// /// </summary> /// <param name="name"></param> /// <param name="contour"></param> void AddMunicipalDivision(string name, List<DVector2> contour) { if (municipalDivisions.ContainsKey(name)) return; var mesh = new TriangleNet.Mesh(); mesh.Behavior.Quality = true; mesh.Behavior.MinAngle = 25; mesh.Behavior.Convex = false; var ig = new InputGeometry(); ig.AddPoint(contour[0].X, contour[0].Y); for (int v = 1; v < contour.Count; v++) { ig.AddPoint(contour[v].X, contour[v].Y); ig.AddSegment(v - 1, v); } ig.AddSegment(contour.Count - 1, 0); mesh.Triangulate(ig); int n = mesh.Vertices.Count; mesh.Renumber(); // Vertices var moVerts = new GeoVert[n]; int i = 0; foreach (var pt in mesh.Vertices) { moVerts[i] = new GeoVert { Lon = pt.X * Math.PI / 180.0, Lat = pt.Y * Math.PI / 180.0, Position = Vector3.Zero, Tex = Vector4.Zero, Color = Color.White }; i++; } // Triangles var triangles = new int[3 * mesh.Triangles.Count]; i = 0; foreach (var tri in mesh.Triangles) { triangles[i * 3 + 0] = tri.P0; triangles[i * 3 + 1] = tri.P1; triangles[i * 3 + 2] = tri.P2; i++; } // Contour vertices var contourVerts = new GeoVert[contour.Count*2]; contourVerts[1] = new GeoVert { Lon = contour[0].X * Math.PI / 180.0, Lat = contour[0].Y * Math.PI / 180.0, Position = Vector3.Zero, Tex = Vector4.Zero, Color = Color.Red }; for (int j = 1; j < contour.Count; j++) { contourVerts[2*j+1] = new GeoVert { Lon = contour[j].X * Math.PI / 180.0, Lat = contour[j].Y * Math.PI / 180.0, Position = Vector3.Zero, Tex = Vector4.Zero, Color = Color.Red }; contourVerts[2*j] = contourVerts[2*(j - 1) + 1]; } contourVerts[0] = contourVerts[contourVerts.Length-1]; // Create buffers var vb = new VertexBuffer(Game.GraphicsDevice, typeof (GeoVert), moVerts.Length); var inds = new IndexBuffer(Game.GraphicsDevice, triangles.Length); var cont = new VertexBuffer(Game.GraphicsDevice, typeof (GeoVert), contourVerts.Length); vb.SetData(moVerts, 0, moVerts.Length); inds.SetData(triangles, 0, triangles.Length); cont.SetData(contourVerts, 0, contourVerts.Length); municipalDivisions.Add(name, new MD { Contour = cont, Indeces = inds, Vertices = vb, Value = r.NextFloat(0.0f, 1.0f) }); }
/// <summary> /// Sets index and vertex buffer. /// </summary> /// <param name="vertexBuffer">Vertex buffer to apply</param> /// <param name="indexBuffer">Index buffer to apply.</param> public void SetupVertexInput( VertexBuffer vertexBuffer, IndexBuffer indexBuffer ) { SetupVertexInput( new[]{ vertexBuffer }, new[]{ 0 }, indexBuffer ); }
/// <summary> /// Sets index and vertex buffers. /// </summary> /// <param name="vertexBuffers">Vertex buffers to apply. Provide null if no vertex buffers are required.</param> /// <param name="offsets">Offsets in each vertex buffer.</param> /// <param name="indexBuffer">Index buffers to apply.</param> public void SetupVertexInput( VertexBuffer[] vertexBuffers, int[] offsets, IndexBuffer indexBuffer ) { lock (deviceContext) { if (indexBuffer!=null) { deviceContext.InputAssembler.SetIndexBuffer( indexBuffer.Buffer, DXGI.Format.R32_UInt, 0 ); } else { deviceContext.InputAssembler.SetIndexBuffer( null, Format.Unknown, 0 ); } if (vertexBuffers==null) { deviceContext.InputAssembler.SetVertexBuffers( 0, new VertexBufferBinding( null, 0, 0 ) ); } else { if (vertexBuffers.Length!=offsets.Length) { throw new InvalidOperationException("vertexBuffers.Length != offsets.Length"); } if (vertexBuffers.Length>16) { throw new InvalidOperationException("vertexBuffers.Length > 16"); } int count = vertexBuffers.Length; //#warning Remove allocation! var inputVertexBufferBinding = new VertexBufferBinding[count]; for (int i=0; i<count; i++) { inputVertexBufferBinding[i].Buffer = ( vertexBuffers[i]==null ) ? null : vertexBuffers[i].Buffer; inputVertexBufferBinding[i].Stride = ( vertexBuffers[i]==null ) ? 0 : vertexBuffers[i].Stride; inputVertexBufferBinding[i].Offset = offsets[i]; } deviceContext.InputAssembler.SetVertexBuffers( 0, inputVertexBufferBinding ); } } }
//void GetTileIndexByMerc(DVector2 coords, int level, out int x, out int y) //{ // int numTiles = 1 << level; // // x = (int)(coords.X * numTiles); // y = (int)(coords.Y * numTiles); //} /// <summary> /// /// </summary> /// <param name="density"></param> /// <param name="vb"></param> /// <param name="ib"></param> /// <param name="left"></param> /// <param name="right"></param> /// <param name="top"></param> /// <param name="bottom"></param> void GenerateTileGrid(int density, out VertexBuffer vb, out IndexBuffer ib, double left, double right, double top, double bottom, int zoom) { int[] indexes; GeoVert[] vertices; CalculateVertices(out vertices, out indexes, density, left, right, top, bottom, zoom); vb = new VertexBuffer(Game.GraphicsDevice, typeof(GeoVert), vertices.Length); ib = new IndexBuffer(Game.GraphicsDevice,indexes.Length); ib.SetData(indexes); vb.SetData(vertices, 0, vertices.Length); }
/// <summary> /// /// </summary> /// <param name="gameTime"></param> /// <param name="stereoEye"></param> /// <returns></returns> public abstract void PrepareMesh(TContext context, Mesh mesh, VertexBuffer vb, IndexBuffer ib);
void CreateRoadFromLine(RailroadPoint[] line, double width, out VertexBuffer vb, out IndexBuffer ib) { if (line.Length == 0) { vb = null; ib = null; return; } float distMul = 4.0f; List<GeoVert> vertices = new List<GeoVert>(); List<int> indeces = new List<int>(); for (int i = 0; i < line.Length-1; i++) { var p0 = line[i]; var p1 = line[i+1]; var cPos0 = SphericalToCartesian(new DVector2(p0.LonLat.X * (Math.PI / 180.0), p0.LonLat.Y * (Math.PI / 180.0)), Config.earthRadius); var cPos1 = SphericalToCartesian(new DVector2(p1.LonLat.X * (Math.PI / 180.0), p1.LonLat.Y * (Math.PI / 180.0)), Config.earthRadius); var normal = cPos0; normal.Normalize(); DVector3 dir = cPos1 - cPos0; DVector3 sideVec = DVector3.Cross(normal, dir); sideVec.Normalize(); DVector3 sideOffset = sideVec * width; // Plane var finalPosRight = cPos0 + sideOffset; var finalPosLeft = cPos0 - sideOffset; var lonLatRight = CartesianToSpherical(finalPosRight); var lonLatLeft = CartesianToSpherical(finalPosLeft); vertices.Add(new GeoVert { Lon = lonLatRight.X, Lat = lonLatRight.Y, Color = Color.Yellow, Tex = new Vector4((float)p0.Distance * distMul, 0.0f, 0.0f, 0.0f), Position = Vector3.Zero }); vertices.Add(new GeoVert { Lon = lonLatLeft.X, Lat = lonLatLeft.Y, Color = Color.Yellow, Tex = new Vector4((float)p0.Distance * distMul, 1.0f, 0.0f, 0.0f), Position = Vector3.Zero }); indeces.Add(i * 2); indeces.Add(i * 2 + 1); indeces.Add((i+1) * 2); indeces.Add(i * 2 + 1); indeces.Add((i + 1) * 2 + 1); indeces.Add((i + 1) * 2); } { var p0 = line[line.Length-1]; var p1 = line[line.Length-2]; var cPos0 = SphericalToCartesian(new DVector2(p0.LonLat.X * (Math.PI / 180.0), p0.LonLat.Y * (Math.PI / 180.0)), Config.earthRadius); var cPos1 = SphericalToCartesian(new DVector2(p1.LonLat.X * (Math.PI / 180.0), p1.LonLat.Y * (Math.PI / 180.0)), Config.earthRadius); var normal = cPos0; normal.Normalize(); DVector3 dir = cPos1 - cPos0; DVector3 sideVec = DVector3.Cross(normal, -dir); sideVec.Normalize(); DVector3 sideOffset = sideVec * width; // Plane var finalPosRight = cPos0 + sideOffset; var finalPosLeft = cPos0 - sideOffset; var lonLatRight = CartesianToSpherical(finalPosRight); var lonLatLeft = CartesianToSpherical(finalPosLeft); vertices.Add(new GeoVert { Lon = lonLatRight.X, Lat = lonLatRight.Y, Color = Color.Yellow, Tex = new Vector4((float)p0.Distance * distMul, 0.0f, 0.0f, 0.0f), Position = Vector3.Zero }); vertices.Add(new GeoVert { Lon = lonLatLeft.X, Lat = lonLatLeft.Y, Color = Color.Yellow, Tex = new Vector4((float)p0.Distance * distMul, 1.0f, 0.0f, 0.0f), Position = Vector3.Zero }); } vb = new VertexBuffer(Game.GraphicsDevice, typeof(GeoVert), vertices.Count); vb.SetData(vertices.ToArray(), 0, vertices.Count); ib = new IndexBuffer(Game.GraphicsDevice, indeces.Count); ib.SetData(indeces.ToArray(), 0, indeces.Count); }
/// <summary> /// /// </summary> public override void Initialize() { vertexBuffer = new VertexBuffer( Game.GraphicsDevice, typeof(SpriteVertex), MaxVertices, VertexBufferOptions.Dynamic ); indexBuffer = new IndexBuffer( Game.GraphicsDevice, MaxIndices ); var indices = new int[MaxIndices]; for (int i=0; i<MaxQuads; i++) { indices[ i*6 + 0 ] = i * 4 + 0; indices[ i*6 + 1 ] = i * 4 + 1; indices[ i*6 + 2 ] = i * 4 + 2; indices[ i*6 + 3 ] = i * 4 + 0; indices[ i*6 + 4 ] = i * 4 + 2; indices[ i*6 + 5 ] = i * 4 + 3; } indexBuffer.SetData( indices ); batches.Capacity = 128; LoadContent(); vertices = new SpriteVertex[MaxVertices]; constBuffer = new ConstantBuffer(Game.GraphicsDevice, typeof(ConstData)); TextureWhite = new Texture2D( Game.GraphicsDevice, 8, 8, ColorFormat.Rgba8, false, false ); TextureBlack = new Texture2D( Game.GraphicsDevice, 8, 8, ColorFormat.Rgba8, false, false ); TextureRed = new Texture2D( Game.GraphicsDevice, 8, 8, ColorFormat.Rgba8, false, false ); TextureGreen = new Texture2D( Game.GraphicsDevice, 8, 8, ColorFormat.Rgba8, false, false ); TextureBlue = new Texture2D( Game.GraphicsDevice, 8, 8, ColorFormat.Rgba8, false, false ); var range = Enumerable.Range(0, 8*8); TextureWhite .SetData( range.Select( i => Color.White ).ToArray() ); TextureBlack .SetData( range.Select( i => Color.Black ).ToArray() ); TextureRed .SetData( range.Select( i => Color.Red ).ToArray() ); TextureGreen .SetData( range.Select( i => Color.Green ).ToArray() ); TextureBlue .SetData( range.Select( i => Color.Blue ).ToArray() ); Game.Reloading += (s,e) => LoadContent(); }
/// <summary> /// Creates buffer from given indices /// </summary> /// <param name="device"></param> /// <param name="indices"></param> /// <returns></returns> public static IndexBuffer Create( GraphicsDevice device, int[] indices ) { var ib = new IndexBuffer( device, indices.Length ); ib.SetData( indices ); return ib; }