void DrawBatch(int startSpriteIndex, int spriteCount) { DataBox mappedData = _deviceContext.MapSubresource(_VB, 0, MapMode.WriteDiscard, MapFlags.None); // Update the vertices unsafe { SpriteVertex *v = (SpriteVertex *)mappedData.DataPointer.ToPointer(); for (int i = 0; i < spriteCount; ++i) { Sprite sprite = _spriteList[startSpriteIndex + i]; SpriteVertex[] quad = new SpriteVertex[4]; BuildSpriteQuad(sprite, ref quad); v[i * 4] = quad[0]; v[i * 4 + 1] = quad[1]; v[i * 4 + 2] = quad[2]; v[i * 4 + 3] = quad[3]; } } _deviceContext.UnmapSubresource(_VB, 0); _deviceContext.DrawIndexed(spriteCount * 6, 0, 0); }
private void DrawBatch(Int32 startSpriteIndex, Int32 spriteCount) { DataBox mappedData = DeviceContext.MapSubresource(this.VertexBuffer, 0, MapMode.WriteDiscard, MapFlags.None); // Update the vertices unsafe { SpriteVertex *spriteVertex = (SpriteVertex *)mappedData.DataPointer.ToPointer(); for (Int32 index = 0; index < spriteCount; index++) { Sprite sprite = this.SpriteList[startSpriteIndex + index]; SpriteVertex[] quad = new SpriteVertex[4]; this.BuildSpriteQuad(sprite, ref quad); spriteVertex[index * 4] = quad[0]; spriteVertex[(index * 4) + 1] = quad[1]; spriteVertex[(index * 4) + 2] = quad[2]; spriteVertex[(index * 4) + 3] = quad[3]; } } DeviceContext.UnmapSubresource(this.VertexBuffer, 0); DeviceContext.DrawIndexed(spriteCount * 6, 0, 0); }
public void UpdateVertexBuffer() { SpriteVertex[] vertices = new SpriteVertex[4]; vertices[0] = new SpriteVertex(new Vector3((Sprite.Position.X - Sprite.Texture.Width / 2) * Sprite.Size, (Sprite.Position.Y - Sprite.Texture.Height / 2) * Sprite.Size, -Sprite.Order), new Vector2(0, 1)); // bottom left vertices[1] = new SpriteVertex(new Vector3((Sprite.Position.X - Sprite.Texture.Width / 2) * Sprite.Size, (Sprite.Position.Y + Sprite.Texture.Height / 2) * Sprite.Size, -Sprite.Order), new Vector2(0, 0)); // top left vertices[2] = new SpriteVertex(new Vector3((Sprite.Position.X + Sprite.Texture.Width / 2) * Sprite.Size, (Sprite.Position.Y + Sprite.Texture.Height / 2) * Sprite.Size, -Sprite.Order), new Vector2(1, 0)); // top right vertices[3] = new SpriteVertex(new Vector3((Sprite.Position.X + Sprite.Texture.Width / 2) * Sprite.Size, (Sprite.Position.Y - Sprite.Texture.Height / 2) * Sprite.Size, -Sprite.Order), new Vector2(1, 1)); // bottom right this.vertexBuffer.AddVertices(vertices); }
internal static void Init() { SpriteVertex[] vertices = new SpriteVertex[] { new SpriteVertex(new Vector3(0.5f, 0.5f, 0f), new Vector2(1f, 1f)), new SpriteVertex(new Vector3(0.5f, -0.5f, 0f), new Vector2(1f, 0f)), new SpriteVertex(new Vector3(-0.5f, -0.5f, 0f), new Vector2(0f, 0f)), new SpriteVertex(new Vector3(-0.5f, 0.5f, 0f), new Vector2(0f, 1f)) }; uint[] indices = new uint[] { 0, 1, 3, 1, 2, 3 }; meshVAO = GL.GenVertexArray(); GL.BindVertexArray(meshVAO); meshVBO = GL.GenBuffer(); GL.BindBuffer(BufferTarget.ArrayBuffer, meshVBO); GL.BufferData(BufferTarget.ArrayBuffer, Unsafe.SizeOf <SpriteVertex>() * vertices.Length, vertices, BufferUsageHint.StaticDraw); meshEBO = GL.GenBuffer(); GL.BindBuffer(BufferTarget.ElementArrayBuffer, meshEBO); GL.BufferData(BufferTarget.ElementArrayBuffer, sizeof(uint) * indices.Length, indices, BufferUsageHint.StaticDraw); GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, sizeof(float) * 5, 0); GL.EnableVertexAttribArray(0); GL.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, false, sizeof(float) * 5, sizeof(float) * 3); GL.EnableVertexAttribArray(1); GL.BindVertexArray(0); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0); string dir = Path.Combine(AppContext.BaseDirectory, "EngineAssets/Shaders/"); shader = Shader.FromSourceFile("sprite-shader", Path.Combine(dir, "sprite.vert"), Path.Combine(dir, "sprite.frag")); }
public void UpdateVertexBuffer() { this.vertexBuffer.Clear(); SpriteVertex[] vertices = new SpriteVertex[4]; if (this.CustomSize == Size.Empty) { vertices[0] = new SpriteVertex(new Vector3(Sprite.Position.X, Sprite.Position.Y, -Sprite.Order), new Vector2(0, 1)); // bottom left vertices[1] = new SpriteVertex(new Vector3(Sprite.Position.X, Sprite.Position.Y + (Sprite.Texture.Height * Sprite.Size.Y), -Sprite.Order), new Vector2(0, 0)); // top left vertices[2] = new SpriteVertex(new Vector3(Sprite.Position.X + (Sprite.Texture.Width * Sprite.Size.X), Sprite.Position.Y + (Sprite.Texture.Height * Sprite.Size.Y), -Sprite.Order), new Vector2(1, 0)); // top right vertices[3] = new SpriteVertex(new Vector3(Sprite.Position.X + (Sprite.Texture.Width * Sprite.Size.X), Sprite.Position.Y, -Sprite.Order), new Vector2(1, 1)); // bottom right } else { vertices[0] = new SpriteVertex(new Vector3(Sprite.Position.X, Sprite.Position.Y, -Sprite.Order), new Vector2(0, 1)); // bottom left vertices[1] = new SpriteVertex(new Vector3(Sprite.Position.X, Sprite.Position.Y + this.CustomSize.Height, -Sprite.Order), new Vector2(0, 0)); // top left vertices[2] = new SpriteVertex(new Vector3(Sprite.Position.X + this.CustomSize.Width, Sprite.Position.Y + this.CustomSize.Height, -Sprite.Order), new Vector2(1, 0)); // top right vertices[3] = new SpriteVertex(new Vector3(Sprite.Position.X + this.CustomSize.Width, Sprite.Position.Y, -Sprite.Order), new Vector2(1, 1)); // bottom right } this.vertexBuffer.AddVertices(vertices); }
void BuildSpriteQuad(Sprite sprite, ref SpriteVertex[] v) { if (v.Length < 4) throw new ArgumentException("must have 4 SpriteVertex's", "v"); Rectangle dest = sprite.DestRect; Rectangle src = sprite.SrcRect; v[0].Pos = PointToNdc(dest.Left, dest.Bottom, sprite.Z); v[1].Pos = PointToNdc(dest.Left, dest.Top, sprite.Z); v[2].Pos = PointToNdc(dest.Right, dest.Top, sprite.Z); v[3].Pos = PointToNdc(dest.Right, dest.Bottom, sprite.Z); v[0].Tex = new Vector2((float)src.Left / _texWidth, (float)src.Bottom / _texHeight); v[1].Tex = new Vector2((float)src.Left / _texWidth, (float)src.Top / _texHeight); v[2].Tex = new Vector2((float)src.Right / _texWidth, (float)src.Top / _texHeight); v[3].Tex = new Vector2((float)src.Right / _texWidth, (float)src.Bottom / _texHeight); v[0].Color = sprite.Color; v[1].Color = sprite.Color; v[2].Color = sprite.Color; v[3].Color = sprite.Color; float tx = 0.5f * (v[0].Pos.X + v[3].Pos.X); float ty = 0.5f * (v[0].Pos.Y + v[1].Pos.Y); Vector2 origin = new Vector2(tx, ty); Vector2 translation = new Vector2(0.0f, 0.0f); Matrix T = Matrix.AffineTransformation2D(sprite.Scale, origin, sprite.Angle, translation); for (int i = 0; i < 4; ++i) { Vector3 p = v[i].Pos; p = Vector3.TransformCoordinate(p, T); v[i].Pos = p; } }
void DrawBatch(int startSpriteIndex, int spriteCount) { DataBox mappedData = _deviceContext.MapSubresource(_VB, 0, MapMode.WriteDiscard, MapFlags.None); // Update the vertices unsafe { SpriteVertex* v = (SpriteVertex*)mappedData.DataPointer.ToPointer(); for (int i = 0; i < spriteCount; ++i) { Sprite sprite = _spriteList[startSpriteIndex + i]; SpriteVertex[] quad = new SpriteVertex[4]; BuildSpriteQuad(sprite, ref quad); v[i * 4] = quad[0]; v[i * 4 + 1] = quad[1]; v[i * 4 + 2] = quad[2]; v[i * 4 + 3] = quad[3]; } } _deviceContext.UnmapSubresource(_VB, 0); _deviceContext.DrawIndexed(spriteCount * 6, 0, 0); }
public Sprite() : base() { mVisible = true; mScaleX = 1; mScaleY = 1; mVertices = new SpriteVertex[4]; mVertices[0] = new SpriteVertex(); mVertices[1] = new SpriteVertex(); mVertices[2] = new SpriteVertex(); mVertices[3] = new SpriteVertex(); mVertices[0].TextureCoordinate.X = 0; mVertices[0].TextureCoordinate.Y = 0; mVertices[0].Scale = new Vector2(-1, 1); mVertices[1].TextureCoordinate.X = 1; mVertices[1].TextureCoordinate.Y = 0; mVertices[1].Scale = new Vector2(1, 1); mVertices[2].TextureCoordinate.X = 1; mVertices[2].TextureCoordinate.Y = 1; mVertices[2].Scale = new Vector2(1, -1); mVertices[3].TextureCoordinate.X = 0; mVertices[3].TextureCoordinate.Y = 1; mVertices[3].Scale = new Vector2(-1, -1); #if WINDOWS_PHONE || MONODROID mVertices[0].Color.X = 1; mVertices[1].Color.X = 1; mVertices[2].Color.X = 1; mVertices[3].Color.X = 1; mVertices[0].Color.Y = 1; mVertices[1].Color.Y = 1; mVertices[2].Color.Y = 1; mVertices[3].Color.Y = 1; mVertices[0].Color.Z = 1; mVertices[1].Color.Z = 1; mVertices[2].Color.Z = 1; mVertices[3].Color.Z = 1; mVertices[0].Color.W = 1; mVertices[1].Color.W = 1; mVertices[2].Color.W = 1; mVertices[3].Color.W = 1; #endif Alpha = GraphicalEnumerations.MaxColorComponentValue; #if FRB_XNA ColorOperation = ColorOperation.Texture; #else ColorOperation = TextureOperation.SelectArg1; #endif mAnimationChains = new AnimationChainList(); mCurrentChainIndex = -1; mAnimationSpeed = 1; mTextureAddressMode = TextureAddressMode.Clamp; mCursorSelectable = true; }
public Sprite() : base() { mVisible = true; mScaleX = 1; mScaleY = 1; mVertices = new SpriteVertex[4]; mVertices[0] = new SpriteVertex(); mVertices[1] = new SpriteVertex(); mVertices[2] = new SpriteVertex(); mVertices[3] = new SpriteVertex(); mVertices[0].TextureCoordinate.X = 0; mVertices[0].TextureCoordinate.Y = 0; mVertices[0].Scale = new Vector2(-1, 1); mVertices[1].TextureCoordinate.X = 1; mVertices[1].TextureCoordinate.Y = 0; mVertices[1].Scale = new Vector2(1, 1); mVertices[2].TextureCoordinate.X = 1; mVertices[2].TextureCoordinate.Y = 1; mVertices[2].Scale = new Vector2(1, -1); mVertices[3].TextureCoordinate.X = 0; mVertices[3].TextureCoordinate.Y = 1; mVertices[3].Scale = new Vector2(-1, -1); #if WINDOWS_PHONE || MONODROID mVertices[0].Color.X = 1; mVertices[1].Color.X = 1; mVertices[2].Color.X = 1; mVertices[3].Color.X = 1; mVertices[0].Color.Y = 1; mVertices[1].Color.Y = 1; mVertices[2].Color.Y = 1; mVertices[3].Color.Y = 1; mVertices[0].Color.Z = 1; mVertices[1].Color.Z = 1; mVertices[2].Color.Z = 1; mVertices[3].Color.Z = 1; mVertices[0].Color.W = 1; mVertices[1].Color.W = 1; mVertices[2].Color.W = 1; mVertices[3].Color.W = 1; #endif Alpha = GraphicalEnumerations.MaxColorComponentValue; ColorOperation = ColorOperation.Texture; mAnimationChains = new AnimationChainList(); mCurrentChainIndex = -1; mAnimationSpeed = 1; mTextureAddressMode = TextureAddressMode.Clamp; mCursorSelectable = true; }
private SpriteVertex[] QuadFromSprite(Sprite sprite) { SpriteVertex[] verts = new SpriteVertex[4]; Rectangle src = sprite.Source; Rectangle dest = sprite.Destination; var width = sprite.Texture.Description.Width; var height = sprite.Texture.Description.Height; verts[0].Position = PointToNdc(dest.Left, dest.Bottom, 0f); verts[1].Position = PointToNdc(dest.Left, dest.Top, 0f); verts[2].Position = PointToNdc(dest.Right, dest.Top, 0f); verts[3].Position = PointToNdc(dest.Right, dest.Bottom, 0f); verts[0].TexCoord = new Vector2((float)src.Left / width, (float)src.Bottom / height); verts[1].TexCoord = new Vector2((float)src.Left / width, (float)src.Top / height); verts[2].TexCoord = new Vector2((float)src.Right / width, (float)src.Top / height); verts[3].TexCoord = new Vector2((float)src.Right / width, (float)src.Bottom / height); for(int i = 0; i < verts.Length; i++) { verts[i].Color = sprite.Color; } var tx = 0.5f * (verts[0].Position.X + verts[3].Position.X); var ty = 0.5f * (verts[0].Position.Y + verts[1].Position.Y); var origin = new Vector2(tx, ty); var translate = new Vector2(0f, 0f); var transform = Matrix.AffineTransformation2D(1f, 0f, translate); for(int i = 0; i < verts.Length; ++i) { Vector3 position = verts[i].Position; position = Vector3.TransformCoordinate(position, transform); verts[i].Position = position; } return verts; }
public virtual bool Draw(SpriteBatch3D spriteBatch, Vector3 position, MouseOverList list) { if (Texture == null || Texture.IsDisposed || !AllowedToDraw || GameObject.IsDisposed) { return(false); } Texture.Ticks = CoreGame.Ticks; SpriteVertex[] vertex; if (Rotation != 0) { float w = Bounds.Width / 2f; float h = Bounds.Height / 2f; Vector3 center = position - new Vector3(Bounds.X - 44 + w, Bounds.Y + h, 0); float sinx = (float)Math.Sin(Rotation) * w; float cosx = (float)Math.Cos(Rotation) * w; float siny = (float)Math.Sin(Rotation) * h; float cosy = (float)Math.Cos(Rotation) * h; vertex = SpriteVertex.PolyBufferFlipped; vertex[0].Position = center; vertex[0].Position.X += cosx - -siny; vertex[0].Position.Y -= sinx + -cosy; vertex[1].Position = center; vertex[1].Position.X += cosx - siny; vertex[1].Position.Y += -sinx + -cosy; vertex[2].Position = center; vertex[2].Position.X += -cosx - -siny; vertex[2].Position.Y += sinx + cosy; vertex[3].Position = center; vertex[3].Position.X += -cosx - siny; vertex[3].Position.Y += sinx + -cosy; } else if (IsFlipped) { vertex = SpriteVertex.PolyBufferFlipped; vertex[0].Position = position; vertex[0].Position.X += Bounds.X + 44f; vertex[0].Position.Y -= Bounds.Y; vertex[0].TextureCoordinate.Y = 0; vertex[1].Position = vertex[0].Position; vertex[1].Position.Y += Bounds.Height; vertex[2].Position = vertex[0].Position; vertex[2].Position.X -= Bounds.Width; vertex[2].TextureCoordinate.Y = 0; vertex[3].Position = vertex[1].Position; vertex[3].Position.X -= Bounds.Width; } else { vertex = SpriteVertex.PolyBuffer; vertex[0].Position = position; vertex[0].Position.X -= Bounds.X; vertex[0].Position.Y -= Bounds.Y; vertex[0].TextureCoordinate.Y = 0; vertex[1].Position = vertex[0].Position; vertex[1].Position.X += Bounds.Width; vertex[1].TextureCoordinate.Y = 0; vertex[2].Position = vertex[0].Position; vertex[2].Position.Y += Bounds.Height; vertex[3].Position = vertex[1].Position; vertex[3].Position.Y += Bounds.Height; } if (IsSelected) { if (_storedHue == Vector3.Zero) { _storedHue = HueVector; } HueVector = RenderExtentions.SelectedHue; } else if (_storedHue != Vector3.Zero) { HueVector = _storedHue; _storedHue = Vector3.Zero; } if (vertex[0].Hue != HueVector) { vertex[0].Hue = vertex[1].Hue = vertex[2].Hue = vertex[3].Hue = HueVector; } if (HasShadow) { SpriteVertex[] vertexS = new SpriteVertex[4] { vertex[0], vertex[1], vertex[2], vertex[3] }; spriteBatch.DrawShadow(Texture, vertexS, new Vector2(position.X + 22f, position.Y + GameObject.Offset.Y - (GameObject.Offset.Z / 4 + GameObject.Position.Z) * 4 + 22f), IsFlipped, ShadowZDepth); } if (!spriteBatch.DrawSprite(Texture, vertex)) { return(false); } MousePick(list, vertex); return(true); }