/// <summary> /// Bind the texture in any texture unit, reusing previously bound textures when possible /// </summary> /// <param name="texture">the texture to bind</param> /// <param name="activate">whether to glActiveTexture the texture unit</param> /// <returns>the texture unit the texture is bound to</returns> public static int BindTexture(BindableTexture texture, bool activate = false) { var samplerUnit = BindTextures(texture)[0]; if (activate) { ActiveTextureUnit = samplerUnit; } return(samplerUnit); }
public void EndRendering() { if (!rendering) { throw new InvalidOperationException("Not rendering"); } primitiveStreamer.Unbind(); shader.End(); currentTexture = null; rendering = false; }
public static void UnbindTexture(BindableTexture texture) { UnbindTexture(texture.TextureId); }
public void Draw(Texture2dRegion texture, float x, float y, float originX, float originY, float scaleX, float scaleY, float rotation, Color4 color, float textureX0, float textureY0, float textureX1, float textureY1) { if (!rendering) { throw new InvalidOperationException("Not rendering"); } if (texture == null) { throw new ArgumentNullException(nameof(texture)); } if (currentTexture != texture.BindableTexture) { DrawState.FlushRenderer(); currentTexture = texture.BindableTexture; } else if (spritesInBatch == maxSpritesPerBatch) { DrawState.FlushRenderer(true); } var width = textureX1 - textureX0; var height = textureY1 - textureY0; float fx = -originX; float fy = -originY; float fx2 = width - originX; float fy2 = height - originY; var flipX = false; var flipY = false; if (scaleX != 1 || scaleY != 1) { flipX = scaleX < 0; flipY = scaleY < 0; var absScaleX = flipX ? -scaleX : scaleX; var absScaleY = flipY ? -scaleY : scaleY; fx *= absScaleX; fy *= absScaleY; fx2 *= absScaleX; fy2 *= absScaleY; } float p1x = fx; float p1y = fy; float p2x = fx; float p2y = fy2; float p3x = fx2; float p3y = fy2; float p4x = fx2; float p4y = fy; float x1; float y1; float x2; float y2; float x3; float y3; float x4; float y4; if (rotation != 0) { var cos = (float)Math.Cos(rotation); var sin = (float)Math.Sin(rotation); x1 = cos * p1x - sin * p1y; y1 = sin * p1x + cos * p1y; x2 = cos * p2x - sin * p2y; y2 = sin * p2x + cos * p2y; x3 = cos * p3x - sin * p3y; y3 = sin * p3x + cos * p3y; x4 = x1 + (x3 - x2); y4 = y3 - (y2 - y1); } else { x1 = p1x; y1 = p1y; x2 = p2x; y2 = p2y; x3 = p3x; y3 = p3y; x4 = p4x; y4 = p4y; } var spritePrimitive = default(SpritePrimitive); spritePrimitive.x1 = x1 + x; spritePrimitive.y1 = y1 + y; spritePrimitive.x2 = x2 + x; spritePrimitive.y2 = y2 + y; spritePrimitive.x3 = x3 + x; spritePrimitive.y3 = y3 + y; spritePrimitive.x4 = x4 + x; spritePrimitive.y4 = y4 + y; var textureUvBounds = texture.UvBounds; var textureUvRatio = texture.UvRatio; var textureU0 = textureUvBounds.Left + textureX0 * textureUvRatio.X; var textureV0 = textureUvBounds.Top + textureY0 * textureUvRatio.Y; var textureU1 = textureUvBounds.Left + textureX1 * textureUvRatio.X; var textureV1 = textureUvBounds.Top + textureY1 * textureUvRatio.Y; float u0, v0, u1, v1; if (flipX) { u0 = textureU1; u1 = textureU0; } else { u0 = textureU0; u1 = textureU1; } if (flipY) { v0 = textureV1; v1 = textureV0; } else { v0 = textureV0; v1 = textureV1; } spritePrimitive.u1 = u0; spritePrimitive.v1 = v0; spritePrimitive.u2 = u0; spritePrimitive.v2 = v1; spritePrimitive.u3 = u1; spritePrimitive.v3 = v1; spritePrimitive.u4 = u1; spritePrimitive.v4 = v0; spritePrimitive.color1 = spritePrimitive.color2 = spritePrimitive.color3 = spritePrimitive.color4 = color.ToRgba(); spriteArray[spritesInBatch] = spritePrimitive; RenderedSpriteCount++; spritesInBatch++; }