Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: doing rounded rectangle
  • Loading branch information
JeremyMeissner committed May 5, 2022
commit d48906f71843fa8f78ab8a1bea0310043e79754b
11 changes: 4 additions & 7 deletions FreeFrame/Components/Shapes/SVGRectangle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ public class SVGRectangle : Shape
#endregion

#region Geometry properties
private int _rx; // Rounded in the x axes
private int _ry; // Rounded in the y axes
public int Rx { get => _rx; set => _rx = value; }
public int Ry { get => _ry; set => _ry = value; }
private int _radius; // Rounded radius
public int Radius { get => _radius; set => _radius = value; }
#endregion

public SVGRectangle(XmlReader reader) : this(
Expand All @@ -43,8 +41,7 @@ public SVGRectangle(int width, int height, int x, int y, int rx, int ry)
Y = y;
Width = width;
Height = height;
Rx = rx;
Ry = ry;
Radius = Math.Max(rx, ry);

ImplementObject();
}
Expand All @@ -59,7 +56,7 @@ public override void ImplementObject()
public override float[] GetVertices() => new float[] { X, Y, X + Width, Y, X + Width, Y + Height, X, Y + Height }; // x, y, x, y, x, y, ... (clockwise)
public override uint[] GetVerticesIndexes() => new uint[] { 0, 1, 2, 0, 2, 3 }; // TODO: please dont hardcode

public override string ToString() => $"x: {X}, y: {Y}, width: {Width}, height: {Height}, rx: {Rx}, ry: {Ry}";
public override string ToString() => $"x: {X}, y: {Y}, width: {Width}, height: {Height}, rx: {Radius}, ry: {Radius}";

public override List<Vector2i> GetSelectablePoints()
{
Expand Down
9 changes: 3 additions & 6 deletions FreeFrame/Shaders/circle.frag
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ uniform vec4 u_Color;
uniform vec2 u_Position;
uniform float u_Radius;

// Draw a circle at position with radius and color
// Draw a circle at a given position with radius and color
vec4 circle(vec2 uv, vec2 pos, float rad, vec4 color) {
float d = length(pos - uv) - rad;
float t = clamp(d, 0.0, 1.0);
Expand All @@ -18,10 +18,7 @@ void main() {

vec2 uv = gl_FragCoord.xy;

vec2 position = vec2(u_Position.x, u_Resolution.y - u_Position.y); // Invert y axis
vec2 position = vec2(u_Position.x, u_Resolution.y - u_Position.y); // Invert y axis

// Circle
vec4 layer2 = circle(uv, position, u_Radius, u_Color);

color = layer2;
color = circle(uv, position, u_Radius, u_Color);
}
36 changes: 36 additions & 0 deletions FreeFrame/Shaders/rectangle.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#version 330

layout(location = 0) out vec4 color;

uniform vec2 u_Resolution;
uniform vec4 u_Color;
uniform vec2 u_Position;
uniform vec2 u_Size;
uniform float u_Radius;


float roundexBox(vec2 centerPosition, vec2 size, float rad) {
return length(max(abs(centerPosition) - size + rad, 0.0)) - rad;
}
void main() {
// How soft the edges should be (in pixels). Higher values could be used to simulate a drop shadow.
float edgeSoftness = 1.0f;

// The radius of the corners (in pixels).
float radius = clamp(u_Radius, 0.0f, max(u_Size.x, u_Size.y) / 2.0f);

vec2 position = vec2(u_Position.x, u_Resolution.y - u_Position.y);

vec2 size = vec2(u_Size.x/2.0f, u_Size.y/2.0f);

// Calculate distance to edge.
float distance = roundexBox(gl_FragCoord.xy - position - size, size, radius);

// Smooth the result (free antialiasing).
float smoothedAlpha = 1.0f - smoothstep(0.0f, edgeSoftness * 2.0f, distance);

// Return the resultant shape.
vec4 quadColor = vec4(u_Color.xyz, smoothedAlpha * u_Color.w);

color = quadColor;
}
18 changes: 15 additions & 3 deletions FreeFrame/VertexArrayObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ public VertexArrayObject(float[] vertices, uint[] indexes, PrimitiveType primiti
public VertexArrayObject(float[] vertices, uint[] indexes, PrimitiveType primitiveType, Shape shape) : this(primitiveType)
{
Type type = shape.GetType();
if (type == typeof(SVGCircle))
_shader = new Shader("Shaders/shader.vert", "Shaders/circle.frag"); // Shader is different for circle
if (type == typeof(SVGCircle)) // Shader depend on the shape
_shader = new Shader("Shaders/shader.vert", "Shaders/circle.frag");
else if (type == typeof(SVGRectangle))
_shader = new Shader("Shaders/shader.vert", "Shaders/rectangle.frag");
ImplementObjects(vertices, indexes);
}
public void Draw(Vector2i clientSize, Color4 color)
Expand Down Expand Up @@ -83,7 +85,17 @@ public void Draw(Vector2i clientSize, Color4 color, Shape shape)
int uPosition = _shader.GetUniformLocation("u_Position");

_shader.SetUniformFloat(uRadius, shape.Width / 2);
_shader.SetUniformVec2(uPosition, new Vector2(shape.X + shape.Width / 2, shape.Y + shape.Height / 2));
_shader.SetUniformVec2(uPosition, new Vector2(shape.X + shape.Width / 2, shape.Y + shape.Height / 2));
}
else if (type == typeof(SVGRectangle))
{
int uRadius = _shader.GetUniformLocation("u_Radius");
int uSize = _shader.GetUniformLocation("u_Size");
int uPosition = _shader.GetUniformLocation("u_Position");

_shader.SetUniformFloat(uRadius, ((SVGRectangle)shape).Radius);
_shader.SetUniformVec2(uSize, new Vector2(shape.Width, shape.Height));
_shader.SetUniformVec2(uPosition, new Vector2(shape.X, shape.Y)); // Invert y axis
}

GL.BindVertexArray(_vertexArrayObject);
Expand Down