public SpriteBatch(GraphicsDevice graphicsDevice) { if (graphicsDevice == null) { throw new ArgumentNullException("graphicsDevice"); } GraphicsDevice = graphicsDevice; vertexInfo = new VertexPositionColorTexture[MAX_VERTICES]; spriteData = new SpriteInfo[MAX_SPRITES]; vertexBuffer = new DynamicVertexBuffer( graphicsDevice, typeof(VertexPositionColorTexture), MAX_VERTICES, BufferUsage.WriteOnly ); indexBuffer = new DynamicIndexBuffer( graphicsDevice, IndexElementSize.SixteenBits, MAX_INDICES, BufferUsage.WriteOnly ); indexBuffer.SetData(indexData); spriteEffect = new Effect( graphicsDevice, spriteEffectCode ); spriteMatrixTransform = spriteEffect.Parameters["MatrixTransform"]; spriteEffectPass = spriteEffect.CurrentTechnique.Passes[0]; beginCalled = false; numSprites = 0; }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // TODO: Add your initialization logic here zNear = 0.001f; zFar = 1000.0f; fov = MathHelper.Pi * 70.0f / 180.0f; eye = new Vector3(0.0f, 0.7f, 1.5f); at = new Vector3(0.0f, 0.0f, 0.0f); up = new Vector3(0.0f, 1.0f, 0.0f); cube = new VertexPositionColor[8]; cube[0] = new VertexPositionColor(new Vector3(-0.5f, -0.5f, -0.5f), new Color(0.0f, 0.0f, 0.0f)); cube[1] = new VertexPositionColor(new Vector3(-0.5f, -0.5f, 0.5f), new Color(0.0f, 0.0f, 1.0f)); cube[2] = new VertexPositionColor(new Vector3(-0.5f, 0.5f, -0.5f), new Color(0.0f, 1.0f, 0.0f)); cube[3] = new VertexPositionColor(new Vector3(-0.5f, 0.5f, 0.5f), new Color(0.0f, 1.0f, 1.0f)); cube[4] = new VertexPositionColor(new Vector3( 0.5f, -0.5f, -0.5f), new Color(1.0f, 0.0f, 0.0f)); cube[5] = new VertexPositionColor(new Vector3( 0.5f, -0.5f, 0.5f), new Color(1.0f, 0.0f, 1.0f)); cube[6] = new VertexPositionColor(new Vector3( 0.5f, 0.5f, -0.5f), new Color(1.0f, 1.0f, 0.0f)); cube[7] = new VertexPositionColor(new Vector3( 0.5f, 0.5f, 0.5f), new Color(1.0f, 1.0f, 1.0f)); vertexBuffer = new DynamicVertexBuffer(graphics.GraphicsDevice, typeof(VertexPositionColor), 8, BufferUsage.WriteOnly); indexBuffer = new DynamicIndexBuffer(graphics.GraphicsDevice, typeof(ushort), 36, BufferUsage.WriteOnly); basicEffect = new BasicEffect(graphics.GraphicsDevice); //(device, null); basicEffect.LightingEnabled = false; basicEffect.VertexColorEnabled = true; basicEffect.TextureEnabled = false; graphics.SupportedOrientations = DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight; base.Initialize(); }
private void Allocate() { if (this.vertexBuffer == null || this.vertexBuffer.IsDisposed) { this.vertexBuffer = new DynamicVertexBuffer(this.graphicsDevice, typeof(VertexPositionColorTexture), 8192, BufferUsage.WriteOnly); this.vertexBufferPosition = 0; this.vertexBuffer.ContentLost += delegate(object sender, EventArgs e) { this.vertexBufferPosition = 0; }; } if (this.indexBuffer == null || this.indexBuffer.IsDisposed) { if (this.fallbackIndexData == null) { this.fallbackIndexData = new short[12288]; for (int i = 0; i < 2048; i++) { this.fallbackIndexData[i * 6] = (short)(i * 4); this.fallbackIndexData[i * 6 + 1] = (short)(i * 4 + 1); this.fallbackIndexData[i * 6 + 2] = (short)(i * 4 + 2); this.fallbackIndexData[i * 6 + 3] = (short)(i * 4); this.fallbackIndexData[i * 6 + 4] = (short)(i * 4 + 2); this.fallbackIndexData[i * 6 + 5] = (short)(i * 4 + 3); } } this.indexBuffer = new DynamicIndexBuffer(this.graphicsDevice, typeof(short), 12288, BufferUsage.WriteOnly); this.indexBuffer.SetData<short>(this.fallbackIndexData); this.indexBuffer.ContentLost += delegate(object sender, EventArgs e) { this.indexBuffer.SetData<short>(this.fallbackIndexData); }; } }
private void CreateBuffers() { var vertexDeclaration = new VertexDeclaration(Format.Stride, nativeVertexFormat.ConvertFrom(Format)); vertexBuffer = new DynamicVertexBuffer(nativeDevice, vertexDeclaration, NumberOfVertices, BufferUsage.WriteOnly); indexBuffer = new DynamicIndexBuffer(nativeDevice, IndexElementSize.SixteenBits, NumberOfIndices, BufferUsage.WriteOnly); }
public DebugDraw(GraphicsDevice device) { vertexBuffer = new DynamicVertexBuffer(device, typeof(VertexPositionColor), MAX_VERTS, BufferUsage.WriteOnly); indexBuffer = new DynamicIndexBuffer(device, typeof(ushort), MAX_INDICES, BufferUsage.WriteOnly); basicEffect = new BasicEffect(device); //(device, null); basicEffect.LightingEnabled = false; basicEffect.VertexColorEnabled = true; basicEffect.TextureEnabled = false; }
public ISurfaceAlgorithm(GraphicsDevice device, int resolution, int size, bool _3d, bool indexed = true, int vertex_size = 262144, int index_size = 4000000) { Device = device; Resolution = resolution; Size = size; Is3D = _3d; IsIndexed = indexed; VertexBuffer = new DynamicVertexBuffer(device, VertexPositionColorNormal.VertexDeclaration, vertex_size, BufferUsage.None); OutlineBuffer = new DynamicVertexBuffer(device, VertexPositionColor.VertexDeclaration, index_size, BufferUsage.None); if (indexed) { IndexBuffer = new DynamicIndexBuffer(device, IndexElementSize.ThirtyTwoBits, index_size, BufferUsage.None); Indices = new List<int>(); } Vertices = new List<VertexPositionColorNormal>(); }
public static bool ShowDialogSynchronous(string title = null, string saveFile = null) { Path = null; // Create dialog resources XNAFileDialog_Init( CreateTextureDelegate, BufferDataDelegate, DrawPrimitivesDelegate, ReceivePathDelegate, StartDirectory, saveFile, title, GraphicsDevice.PresentationParameters.BackBufferWidth, GraphicsDevice.PresentationParameters.BackBufferHeight ); // Store previous GL state Rectangle prevScissor = GraphicsDevice.ScissorRectangle; Texture prevTexture = GraphicsDevice.Textures[0]; SamplerState prevSampler = GraphicsDevice.SamplerStates[0]; BlendState prevBlend = GraphicsDevice.BlendState; DepthStencilState prevDepthStencil = GraphicsDevice.DepthStencilState; RasterizerState prevRasterizer = GraphicsDevice.RasterizerState; VertexBufferBinding[] prevVerts = GraphicsDevice.GetVertexBuffers(); IndexBuffer prevIndex = GraphicsDevice.Indices; // Set new GL state GraphicsDevice.Textures[0] = texture; GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap; GraphicsDevice.BlendState = BlendState.NonPremultiplied; GraphicsDevice.DepthStencilState = DepthStencilState.None; GraphicsDevice.RasterizerState = RasterizerState.CullNone; // Time to block, yayyyyy pathSent = false; do { GraphicsDevice.Clear(Color.Black); XNAFileDialog_Update(); XNAFileDialog_Render(); GraphicsDevice.Present(); } while (!pathSent); // Restore GL state GraphicsDevice.ScissorRectangle = prevScissor; GraphicsDevice.Textures[0] = prevTexture; GraphicsDevice.SamplerStates[0] = prevSampler; GraphicsDevice.BlendState = prevBlend; GraphicsDevice.DepthStencilState = prevDepthStencil; GraphicsDevice.RasterizerState = prevRasterizer; GraphicsDevice.SetVertexBuffers(prevVerts); GraphicsDevice.Indices = prevIndex; // Clean up. We out. texture.Dispose(); texture = null; vertBuffer.Dispose(); vertBuffer = null; vertBufferSize = 0; indexBuffer.Dispose(); indexBuffer = null; indexBufferSize = 0; XNAFileDialog_Shutdown(); return !String.IsNullOrEmpty(Path); }
private void SetDefaultIndices(int indicesArraySize) { indices = new int[indicesArraySize]; for (int i = 0, j = 1; i < indicesArraySize; i += 3) { indices[i] = 0; indices[i + 1] = j; ++j; indices[i + 2] = j; } if (indexBuffer == null) indexBuffer = new DynamicIndexBuffer(gxtRoot.Singleton.Graphics, typeof(int), indicesArraySize, BufferUsage.WriteOnly); indexBuffer.SetData<int>(indices); this.primitiveCount = indicesArraySize / 3; }
private void SetUpBuffers() { v_buffer = new DynamicVertexBuffer(Game.device, VertexPositionTexture.VertexDeclaration, vertices.Length, BufferUsage.WriteOnly); i_buffer = new DynamicIndexBuffer(Game.device, typeof(int), indices.Length, BufferUsage.WriteOnly); v_buffer.SetData(vertices); i_buffer.SetData(indices); }
protected override void LoadContent() { //Loading textures and effects from content terrainMap = Game.Content.Load<Texture2D>("Models/Terrain/Heightmaps/heightmap4"); grassTexture = Game.Content.Load<Texture2D>("Models/Terrain/Textures/grass"); sandTexture = Game.Content.Load<Texture2D>("Models/Terrain/Textures/sand"); snowTexture = Game.Content.Load<Texture2D>("Models/Terrain/Textures/snow"); rockTexture = Game.Content.Load<Texture2D>("Models/Terrain/Textures/rock"); TerrainUtils.LoadHeightData(terrainMap, ref heightData, ref width, ref height); TerrainUtils.SetUpVertices(heightData, ref vertices, width, height); indices = TerrainUtils.CreateTerrainIndices(width, height); vertices = TerrainUtils.GenerateNormalsForTriangleStrip(vertices, indices); VertexMultiTextured[,] vertexArray = TerrainUtils.Reshape1DTo2D<VertexMultiTextured>(vertices, width, height); rootNode = new QTNode(vertexArray, device, grassTexture, 64, effect, rockTexture, snowTexture, sandTexture); TerrainUtils.CopyToBuffer(ref vertexBuffer, ref indexBuffer, vertices, indices, device); int terrainSize = 32; Triangle leftTriangle = new Triangle(null, new Vector2(0, 0), new Vector2(terrainSize, 0), new Vector2(0, terrainSize), heightData); Triangle rightTriangle = new Triangle(null, new Vector2(terrainSize, terrainSize), new Vector2(0, terrainSize), new Vector2(terrainSize, 0), heightData); leftTriangle.AddNeighs(null, null, rightTriangle); rightTriangle.AddNeighs(null, null, leftTriangle); triangleList = new List<Triangle>(); triangleList.Add(leftTriangle); triangleList.Add(rightTriangle); indicesList = new List<int>(); foreach (Triangle t in triangleList) t.AddIndices(ref indicesList); dynamicIndexBuffer = new DynamicIndexBuffer(device, typeof(int), indicesList.Count, BufferUsage.WriteOnly); dynamicIndexBuffer.SetData(indicesList.ToArray(), 0, indicesList.Count, SetDataOptions.Discard); dynamicIndexBuffer.ContentLost += dynamicIndexBuffer_ContentLost; }
public void SetupMesh(Vector2[] verts, int[] indices, Vector2[] textureCoordinates) { gxtDebug.Assert(verts != null && verts.Length >= 3, "Vertices array is null or less than 3 vertices"); gxtDebug.Assert(indices != null && indices.Length >= 3, "Indices array is null or less than 3 indices"); gxtDebug.Assert(textureCoordinates != null && textureCoordinates.Length >= 3, "Texture Coordinates array is null or less than 3 UV coordinates"); gxtDebug.Assert(verts.Length == textureCoordinates.Length, "The vertices array and texture coordinates array have a size mismatch!"); // set up verts vertices = new VertexPositionColorTexture[verts.Length]; if (vertexBuffer == null) vertexBuffer = new DynamicVertexBuffer(gxtRoot.Singleton.Graphics, typeof(VertexPositionColorTexture), verts.Length, BufferUsage.WriteOnly); Color overlay = (material != null) ? material.ColorOverlay : gxtMaterial.DEFAULT_COLOR_OVERLAY; for (int i = 0; i < verts.Length; ++i) { vertices[i] = new VertexPositionColorTexture(new Vector3(verts[i].X, verts[i].Y, 0.0f), overlay, textureCoordinates[i]); } vertexBuffer.SetData<VertexPositionColorTexture>(vertices); localAABB = gxtGeometry.ComputeAABB(verts); // set up indices // deep copy necessary? for (int i = 0; i < indices.Length; ++i) { this.indices[i] = indices[i]; } if (indexBuffer == null) indexBuffer = new DynamicIndexBuffer(gxtRoot.Singleton.Graphics, typeof(int), indices.Length, BufferUsage.WriteOnly); indexBuffer.SetData<int>(indices); this.primitiveCount = indices.Length / 3; }
/// <summary> /// Copies the terrain patch vertices and indices into buffers. /// </summary> private void CreateBuffers() { this.vertexBuffer = new DynamicVertexBuffer(EngineManager.Engine.GraphicsDevice, this.vertices.Length * VertexPositionNormalMultipleTexture.SizeInBytes, BufferUsage.WriteOnly); this.vertexBuffer.ContentLost += new EventHandler(this.TerrainVertexBuffer_ContentLost); this.vertexBuffer.SetData<VertexPositionNormalMultipleTexture>(this.vertices); this.indexBuffer = new DynamicIndexBuffer(EngineManager.Engine.GraphicsDevice, typeof(int), this.indexes.Length, BufferUsage.WriteOnly); this.indexBuffer.ContentLost += new EventHandler(this.TerrainIndexBuffer_ContentLost); this.indexBuffer.SetData<int>(this.indexes); }
public void generateVertices() { #region neighborChecks patternOrChunk adjChunk = MainGame.mainGame.getPatternOrChunk(position + new IntVector3(1, 0, 0)); if (adjChunk.chunk == null) { //Logger.Log("Skipping " + position + ", north (" + (position + new IntVector3(1,0,0)) + ") unset"); return; } adjChunk = MainGame.mainGame.getPatternOrChunk(position + new IntVector3(-1, 0, 0)); if (adjChunk.chunk == null) { //Logger.Log("Skipping " + position + ", south (" + (position + new IntVector3(-1, 0, 0)) + ") unset"); return; } adjChunk = MainGame.mainGame.getPatternOrChunk(position + new IntVector3(0, 1, 0)); if (adjChunk.chunk == null) { //Logger.Log("Skipping " + position + ", top (" + (position + new IntVector3(0, 1, 0)) + ") unset"); return; } adjChunk = MainGame.mainGame.getPatternOrChunk(position + new IntVector3(0, -1, 0)); if (adjChunk.chunk == null) { //Logger.Log("Skipping " + position + ", bottom (" + (position + new IntVector3(0, -1, 0)) + ") unset"); return; } adjChunk = MainGame.mainGame.getPatternOrChunk(position + new IntVector3(0, 0, 1)); if (adjChunk.chunk == null) { //Logger.Log("Skipping " + position + ", east (" + (position + new IntVector3(0, 0, 1)) + ") unset"); return; } adjChunk = MainGame.mainGame.getPatternOrChunk(position + new IntVector3(0, 0, -1)); if (adjChunk.chunk == null) { //Logger.Log("Skipping " + position + ", west (" + (position + new IntVector3(0, 0, -1)) + ") unset"); return; } #endregion neighborChecks Logger.Log("Drawing " + position); Screens.LoadingScreen.ChangeMessage("Generating vertices for " + position); //there SHOULD be something set up so that it only needs to rebuild the vertices for changed sections //but i don't believe there currently is //ofc that does mean significantly more memory cost... maybe it's not worth it. List<VertexPositionColorNormal> verts = new List<VertexPositionColorNormal>(4096); List<short> inds = new List<short>(4096); //Having the 4096 should make it not have to increase the capacity often, since 4096 is the number of blocks in a chunk, //and not all of them will be distinct, probably. //(Of course there's... 125 * 24 potential vertices per block, but still.) for (int x = 0; x < size; x++) { for (int y = 0; y < size; y++) { for (int z = 0; z < size; z++) { blocks[x, y, z].createVertices(verts, inds, new Vector3(position.X * Sizes.ChunkSize + x, position.Y * Sizes.ChunkSize + y, position.Z * Sizes.ChunkSize + z)); } } } vertices = verts.ToArray(); //this could be problematic when you remove the last block in a chunk... if (vertices.Length > 0) { if (vertices.Length > maxVerts) { GraphicsDevice graphics = vBuff.GraphicsDevice; if (graphics == null) throw new InvalidOperationException("Graphics device no longer exists!"); if (vBuff != null) vBuff.Dispose(); maxVerts = vertices.Length + 1000; vBuff = new DynamicVertexBuffer(graphics, typeof(VertexPositionColorNormal), maxVerts, BufferUsage.WriteOnly); //vBuff.ContentLost += new EventHandler<EventArgs>(verticesLostHandler); //Logger.Log("Vertices extended to " + maxVerts); } vBuff.SetData<VertexPositionColorNormal>(vertices, 0, vertices.Length); Logger.Log("Vertices: " + vertices.Length); } indices = inds.ToArray(); if (indices.Length > 0) { if (indices.Length > maxInds) { GraphicsDevice graphics = iBuff.GraphicsDevice; if (iBuff != null) iBuff.Dispose(); maxInds = indices.Length + 1500; iBuff = new DynamicIndexBuffer(graphics, IndexElementSize.SixteenBits, maxInds, BufferUsage.WriteOnly); //iBuff.ContentLost += new EventHandler<EventArgs>(indicesLostHandler); //Logger.Log("Indices extended to " + maxInds); } iBuff.SetData<short>(indices, 0, indices.Length); Logger.Log("Indices: " + indices.Length); } Screens.LoadingScreen.ChangeMessage("Finished generating vertices for " + position); }
// SpriteManager public SpriteManager() { #if !RELEASE RasterizerState_WireFrame = new RasterizerState(); RasterizerState_WireFrame.FillMode = FillMode.WireFrame; #endif GraphicsDevice = _UI.Game.GraphicsDevice; PresentationParameters pp = GraphicsDevice.PresentationParameters; BackBufferSize = new Vector2( pp.BackBufferWidth, pp.BackBufferHeight ); SpriteCountMax = _UI.Settings.Sprite_Count; Sprites = new Sprite[ SpriteCountMax ]; SpriteCount = 0; CurrentSprite = 0; TopLayer = _UI.Settings.Sprite_LayerCount - 1; RenderPassLayerSprites = new List< List< int >[] >( _UI.Settings.Sprite_RenderPassCount ); RenderPass3dMask = _UI.Settings.Sprite_RenderPass3dMask; for ( int i = 0; i < _UI.Settings.Sprite_RenderPassCount; ++i ) { List< int >[] layerSprites = new List< int >[ _UI.Settings.Sprite_LayerCount ]; for ( int j = 0; j < layerSprites.Length; ++j ) layerSprites[ j ] = new List< int >(); RenderPassLayerSprites.Add( layerSprites ); } PreMatrixPool = new Matrix[ _UI.Settings.Sprite_PreMatrixCount ]; PreMatrixCount = 0; PostMatrixPool = new Matrix[ _UI.Settings.Sprite_PostMatrixCount ]; PostMatrixCount = 0; Matrix identity = Matrix.Identity; // reserve identity StorePreMatrix( ref identity ); StorePostMatrix( ref identity ); RenderStatePool = new object[ _UI.Settings.Sprite_RenderStateCount ]; RenderStateCount = 0; // default renderstates StoreRenderState( DepthStencilState.None ); StoreRenderState( RasterizerState.CullNone ); RenderStateStack_Blend = new Stack< int >( _UI.Settings.Sprite_RenderStateStackCount ); RenderStateStack_DepthStencil = new Stack< int >( _UI.Settings.Sprite_RenderStateStackCount ); RenderStateStack_Rasterizer = new Stack< int >( _UI.Settings.Sprite_RenderStateStackCount ); VertexCorners = new Vector2[ VertexCount ] { new Vector2( 0.0f, 0.0f ), new Vector2( 1.0f, 0.0f ), new Vector2( 1.0f, 1.0f ), new Vector2( 0.0f, 1.0f ), }; VertexCornersFlipped = new Vector2[ VertexCount ] { new Vector2( 0.0f, 1.0f ), new Vector2( 1.0f, 1.0f ), new Vector2( 1.0f, 0.0f ), new Vector2( 0.0f, 0.0f ), }; VertexOffsetAligned = new Vector2[ (int)E_Align.Count ] { new Vector2( 0.0f, 0.0f ), // None new Vector2( 0.0f, 0.0f ), // TopLeft new Vector2( 0.5f, 0.0f ), // TopCentre new Vector2( 1.0f, 0.0f ), // TopRight new Vector2( 0.0f, 0.5f ), // MiddleLeft new Vector2( 0.5f, 0.5f ), // MiddleCentre new Vector2( 1.0f, 0.5f ), // MiddleRight new Vector2( 0.0f, 1.0f ), // BottomLeft new Vector2( 0.5f, 1.0f ), // BottomCentre new Vector2( 1.0f, 1.0f ), // BottomRight }; VertexOffsetAlignedFlipped = new Vector2[ (int)E_Align.Count ] { new Vector2( 0.0f, 1.0f ), // None new Vector2( 0.0f, 1.0f ), // TopLeft new Vector2( 0.5f, 1.0f ), // TopCentre new Vector2( 1.0f, 1.0f ), // TopRight new Vector2( 0.0f, 0.5f ), // MiddleLeft new Vector2( 0.5f, 0.5f ), // MiddleCentre new Vector2( 1.0f, 0.5f ), // MiddleRight new Vector2( 0.0f, 0.0f ), // BottomLeft new Vector2( 0.5f, 0.0f ), // BottomCentre new Vector2( 1.0f, 0.0f ), // BottomRight }; VertexCornersAligned = new Vector3[ (int)E_Align.Count ][]; for ( int i = 0; i < VertexCornersAligned.Length; ++i ) VertexCornersAligned[ i ] = new Vector3[ VertexCount ]; VertexCornersAlignedFlipped = new Vector3[ (int)E_Align.Count ][]; for ( int i = 0; i < VertexCornersAlignedFlipped.Length; ++i ) VertexCornersAlignedFlipped[ i ] = new Vector3[ VertexCount ]; for ( int i = 0; i < (int)E_Align.Count; ++i ) { for ( int j = 0; j < VertexCount; ++j ) { VertexCornersAligned[ i ][ j ] = new Vector3( VertexCorners[ j ] - VertexOffsetAligned[ i ], 0.0f ); VertexCornersAlignedFlipped[ i ][ j ] = new Vector3( VertexCornersFlipped[ j ] - VertexOffsetAlignedFlipped[ i ], 0.0f ); } } TransformMatrix2D = Matrix.CreateTranslation( -0.5f, -0.5f, 0.0f ) * Matrix.CreateOrthographicOffCenter( 0.0f, pp.BackBufferWidth, pp.BackBufferHeight, 0.0f, -1500.0f, 1500.0f ); TransformMatrix = TransformMatrix2D; // default AutoTransformIndex = 0; AutoTransformOffset = new Vector3(); VertexData = new VertexSprite[ VertexCount * SpriteCountMax ]; VertexPosition = 0; DynamicVB = new DynamicVertexBuffer( GraphicsDevice, VertexSprite.VertexDeclaration, SpriteCountMax * VertexCount, BufferUsage.WriteOnly ); DynamicIB = new DynamicIndexBuffer( GraphicsDevice, IndexElementSize.SixteenBits, SpriteCountMax * IndexCount, BufferUsage.WriteOnly ); // IB never changes short[] dataIB = new short[ SpriteCountMax * IndexCount ]; short primitiveCount = 0; for ( int i = 0; i < dataIB.Length; ) { short offset = (short)( primitiveCount * VertexCount ); dataIB[ i++ ] = (short)( 0 + offset ); dataIB[ i++ ] = (short)( 1 + offset ); dataIB[ i++ ] = (short)( 2 + offset ); dataIB[ i++ ] = (short)( 2 + offset ); dataIB[ i++ ] = (short)( 3 + offset ); dataIB[ i++ ] = (short)( 0 + offset ); ++primitiveCount; } DynamicIB.SetData( dataIB ); }
public void SetIndices(ushort[] indices, int indexCount) { if (indices == null) throw new ArgumentNullException("indices"); if (indexCount < 0 || indices.Length < indexCount) throw new ArgumentOutOfRangeException("vertexCount"); IndexCount = indexCount; PrimitiveCount = indexCount / 3; if (indexCount != 0) { if (indexBuffer != null && indexBuffer.IndexCount != indexCount) { indexBuffer.Dispose(); indexBuffer = null; } if (indexBuffer == null) indexBuffer = new DynamicIndexBuffer( graphicsDevice, IndexElementSize.SixteenBits, indexCount, BufferUsage.WriteOnly); indexBuffer.SetData(indices, 0, indexCount, SetDataOptions.Discard); } else { if (indexBuffer != null) { indexBuffer.Dispose(); indexBuffer = null; } } }
private void CheckCreateIndexBuffer(int indexCount = 600) { if (indexBuffer != null) return; indexBuffer = new DynamicIndexBuffer(device.NativeDevice, IndexElementSize.SixteenBits, indexCount, BufferUsage.WriteOnly); }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { OrientationSensor = OrientationSensor.GetDefault(); if (OrientationSensor == null) { SimpleOrientationSensor = SimpleOrientationSensor.GetDefault(); if (SimpleOrientationSensor == null) throw new Exception("No way of determining orientation"); } TouchPanel.EnabledGestures = GestureType.Hold | GestureType.Flick | GestureType.HorizontalDrag | GestureType.VerticalDrag | GestureType.DragComplete; vertexBuffer = new DynamicVertexBuffer(graphics.GraphicsDevice, typeof(VertexPositionColor), 8, BufferUsage.WriteOnly); indexBuffer = new DynamicIndexBuffer(graphics.GraphicsDevice, typeof(ushort), 36, BufferUsage.WriteOnly); basicEffect = new BasicEffect(graphics.GraphicsDevice); //(device, null); basicEffect.LightingEnabled = false; basicEffect.VertexColorEnabled = true; basicEffect.TextureEnabled = false; DepthStencilState depthBufferState = new DepthStencilState(); depthBufferState.DepthBufferEnable = true; GraphicsDevice.DepthStencilState = depthBufferState; TetrisState.Initialize(graphics.GraphicsDevice); Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().SuppressSystemOverlays = true; graphics.SupportedOrientations = DisplayOrientation.Portrait; base.Initialize(); }
/// <summary> /// Creates a new index buffer with a specific size /// </summary> /// <param name="elementCount">Size of the index buffer</param> protected override void CreateBuffer(int elementCount) { IndexBuffer buffer = null; switch (_bufferType) { case BufferType.Static: buffer = new IndexBuffer(_device, _elementSize, elementCount, BufferUsage.None); break; case BufferType.StaticWriteOnly: buffer = new IndexBuffer(_device, _elementSize, elementCount, BufferUsage.WriteOnly); break; case BufferType.Dynamic: buffer = new DynamicIndexBuffer(_device, _elementSize, elementCount, BufferUsage.None); break; case BufferType.DynamicWriteOnly: buffer = new DynamicIndexBuffer(_device, _elementSize, elementCount, BufferUsage.WriteOnly); break; } if (buffer == null) throw new InvalidOperationException("Failed to create an index buffer"); CreateWrapper(buffer); BufferAllocatedEventArgs e = new BufferAllocatedEventArgs(this, elementCount); OnIndexBufferAllocated(e); }
private static void BufferData( IntPtr vertexData, int vertexDataLen, IntPtr indexData, int indexDataLen ) { if (vertexDataLen > vertBufferSize) { vertBufferSize = vertexDataLen; if (vertBuffer != null) { vertBuffer.Dispose(); } vertBuffer = new DynamicVertexBuffer( GraphicsDevice, vertDecl, vertBufferSize / vertDecl.VertexStride, BufferUsage.WriteOnly ); } if (indexDataLen > indexBufferSize) { indexBufferSize = indexDataLen; if (indexBuffer != null) { indexBuffer.Dispose(); } indexBuffer = new DynamicIndexBuffer( GraphicsDevice, IndexElementSize.SixteenBits, indexBufferSize / 2, BufferUsage.WriteOnly ); } byte[] vertices = new byte[vertBufferSize]; short[] indices = new short[indexBufferSize / 2]; Marshal.Copy(vertexData, vertices, 0, vertices.Length); Marshal.Copy(indexData, indices, 0, indices.Length); vertBuffer.SetData(0, vertices, 0, vertices.Length, 1, SetDataOptions.None); indexBuffer.SetData(indices, 0, indices.Length, SetDataOptions.None); }
private void CreateGeometry(int capacity) { var device = GameApp.Instance.GraphicsDevice; m_vertices1 = new DynamicVertexBuffer(device, typeof(ParticleVertex1), capacity * 4, 0); m_vertices2 = new DynamicVertexBuffer(device, typeof(ParticleVertex2), capacity * 4, 0); m_indices = new DynamicIndexBuffer(device, IndexElementSize.SixteenBits, capacity * 6, 0); }
/// <summary> /// Creates a new Chunk of size size x size x size, filled with null blocks /// This was intended for chunks that are part of blueprints, but may still be useful (vehicles?) /// </summary> /// <param name="layers"></param> /// <param name="filename"></param> public Chunk(int size, String filename, GraphicsDevice graphics) { this.size = size; blocks = new Block[size, size, size]; this.filename = MainGame.WorldFolder + filename; vBuff = new DynamicVertexBuffer(graphics, typeof(VertexPositionColorNormal), maxVerts, BufferUsage.WriteOnly); //vBuff.ContentLost += new EventHandler<EventArgs>(verticesLostHandler); iBuff = new DynamicIndexBuffer(graphics, IndexElementSize.SixteenBits, maxInds, BufferUsage.WriteOnly); //iBuff.ContentLost += new EventHandler<EventArgs>(indicesLostHandler); }
private void CreateGeometry(int capacity) { if (m_vertices1 != null) { m_vertices1.Dispose(); m_vertices2.Dispose(); m_indices.Dispose(); } m_vertices1 = new DynamicVertexBuffer(GraphicsDevice, typeof(ParticleVertex1), capacity * 4, BufferUsage.WriteOnly); m_vertices2 = new DynamicVertexBuffer(GraphicsDevice, typeof(ParticleVertex2), capacity * 4, BufferUsage.WriteOnly); m_indices = new DynamicIndexBuffer(GraphicsDevice, IndexElementSize.SixteenBits, capacity * 6, BufferUsage.WriteOnly); m_shadowedVertices1 = new ParticleVertex1[capacity * 4]; m_shadowedVertices2 = new ParticleVertex2[capacity * 4]; m_shadowedIndices = new UInt16[capacity * 6]; for (int i = 0; i < m_shadowedVertices1.Length; ++i) { m_shadowedVertices1[i].corner = (Byte)(i & 0x3); } }
/// <summary> /// Creates a new Chunk of size chunkSize x chunkSize x chunkSize, filled with null blocks. /// This is for chunks that are part of the map. /// It will assume that its north-west-bottom corner is at pos * chunkSize, /// and (when implemented...) it will save its data to "pos.x,pos.y,pos.z".xml /// </summary> /// <param name="layers"></param> /// <param name="pos"></param> public Chunk(IntVector3 pos, GraphicsDevice graphics) { size = Sizes.ChunkSize; blocks = new Block[Sizes.ChunkSize, Sizes.ChunkSize, Sizes.ChunkSize]; structures = new List<MapStructure>(); position = pos; filename = MainGame.WorldFolder + position.X + "," + position.Y + "," + position.Z; try { vBuff = new DynamicVertexBuffer(graphics, typeof(VertexPositionColorNormal), maxVerts, BufferUsage.WriteOnly); //vBuff.ContentLost += new EventHandler<EventArgs>(verticesLostHandler); iBuff = new DynamicIndexBuffer(graphics, IndexElementSize.SixteenBits, maxInds, BufferUsage.WriteOnly); //iBuff.ContentLost += new EventHandler<EventArgs>(indicesLostHandler); //so do I need to add a custom test for content lost before drawing? } catch(OpenTK.Graphics.GraphicsContextException e) { Logger.Log(e.ToString()); } vertices = new VertexPositionColorNormal[0]; indices = new short[0]; }
void initializeGraphics() { world = Matrix.Identity; projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), (float)graphics.Viewport.Width / (float)graphics.Viewport.Height, .01f, 10.0f); view = Matrix.CreateLookAt(new Vector3(-4, 0, 0), new Vector3(0, 0, 0), Vector3.Up); drawingEngine = new BasicEffect(graphics); drawingEngine.World = world; drawingEngine.Projection = projection; drawingEngine.View = view; drawingEngine.TextureEnabled = false; drawingEngine.VertexColorEnabled = true; drawingEngine.EnableDefaultLighting(); drawingEngine.SpecularColor = new Vector3(0, 0, 0); //*/ /* voxelEffect = content.Load<Effect>("CubeShader"); voxelEffect.Parameters["World"].SetValue(world); voxelEffect.Parameters["Projection"].SetValue(projection); voxelEffect.Parameters["View"].SetValue(view); voxelEffect.Parameters["ambientColor"].SetValue(new Vector4(1.0f, 1.0f, 1.0f, 1.0f)); voxelEffect.Parameters["sunColor"].SetValue(new Vector4(1.0f, 1.0f, 0.8f, 0.0f)); voxelEffect.Parameters["sunDir"].SetValue(new Vector3(-0.1f, -1.0f, -0.5f)); //*/ #region selected sIBuff = new DynamicIndexBuffer(graphics, IndexElementSize.SixteenBits, 24, BufferUsage.WriteOnly); //iBuff.ContentLost += new EventHandler<EventArgs>(indicesLostHandler); selectionIndices = new short[24]{0, 1, 0, 2, 1, 3, 2, 3, 0, 4, 1, 5, 2, 6, 3, 7, 4, 5, 4, 6, 5, 7, 6, 7}; sIBuff.SetData(selectionIndices); sVBuff = new DynamicVertexBuffer(graphics, typeof(VertexPositionColor), 8, BufferUsage.WriteOnly); //vBuff.ContentLost += new EventHandler<EventArgs>(verticesLostHandler); #endregion selected transLeft = Matrix.CreateTranslation(0, 0, -2); transRight = Matrix.CreateTranslation(0, 0, 2); }
public void LoadParticles() { particleArray = new BillboardParticleElement[partCount * 4]; for (int p = 0; p < partCount; p += 4) { for (int thisP = 0; thisP < 4; thisP++) { int currentParticle = p + thisP; particleArray[currentParticle] = new BillboardParticleElement(); particleArray[currentParticle].Position = myPosition; particleArray[currentParticle].Color = particleColor; particleArray[currentParticle].Data = new Vector4(1f, 1f, 0f, 0f); switch (thisP) { case 0: particleArray[currentParticle].TextureCoordinate = Vector2.Zero; break; case 1: particleArray[currentParticle].TextureCoordinate = new Vector2(1, 0); break; case 2: particleArray[currentParticle].TextureCoordinate = new Vector2(0, 1); break; case 3: particleArray[currentParticle].TextureCoordinate = Vector2.One; break; } } } short[] indices = new short[6 * partCount]; for (int part = 0; part < partCount; part++) { int off = part * 6; int offVal = part * 4; indices[off + 0] = (short)(0 + offVal); indices[off + 1] = (short)(1 + offVal); indices[off + 2] = (short)(2 + offVal); indices[off + 3] = (short)(1 + offVal); indices[off + 4] = (short)(3 + offVal); indices[off + 5] = (short)(2 + offVal); } ib = new DynamicIndexBuffer(REngine.Instance._game.GraphicsDevice, typeof(short), 6 * partCount, BufferUsage.WriteOnly); ib.SetData(indices); }
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 void SetIndices(int[] indices) { gxtDebug.Assert(indices != null && indices.Length >= 3); if (this.indices == null) this.indices = new int[indices.Length]; if (indexBuffer == null) indexBuffer = new DynamicIndexBuffer(gxtRoot.Singleton.Graphics, typeof(int), indices.Length, BufferUsage.WriteOnly); for (int i = 0; i < indices.Length; ++i) { this.indices[i] = indices[i]; } indexBuffer.SetData<int>(this.indices); this.primitiveCount = this.indices.Length / 3; }
public void Initialize(GraphicsDevice graphicsDevice) { VertexBuffer = new DynamicVertexBuffer(graphicsDevice, typeof(VertexPositionColor), 8, BufferUsage.WriteOnly); IndexBuffer = new DynamicIndexBuffer(graphicsDevice, typeof(ushort), 36, BufferUsage.WriteOnly); }