Skip to content

Commit cb86bc6

Browse files
feat: drawing <rect> element from SVG
1 parent 65a17b5 commit cb86bc6

10 files changed

Lines changed: 133 additions & 14 deletions

File tree

FreeFrame/Components/Shapes/SVGCircle.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,10 @@ public SVGCircle(int r, int cx, int cy)
4444
}
4545

4646
public override string ToString() => $"cx: {_cx}, cy: {_cy}, r: {_r}";
47+
48+
public override void UpdateProperties()
49+
{
50+
throw new NotImplementedException();
51+
}
4752
}
4853
}

FreeFrame/Components/Shapes/SVGPath.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,10 @@ public override string ToString()
104104
_drawAttribute.ForEach(d => output += d.ToString() + " ");
105105
return output.Trim();
106106
}
107+
108+
public override void UpdateProperties()
109+
{
110+
throw new NotImplementedException();
111+
}
107112
}
108113
}

FreeFrame/Components/Shapes/SVGPolygon.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,10 @@ public override string ToString()
3232
output += string.Format("{0},{1} ", point.x, point.y);
3333
return output.Trim();
3434
}
35+
36+
public override void UpdateProperties()
37+
{
38+
throw new NotImplementedException();
39+
}
3540
}
3641
}

FreeFrame/Components/Shapes/SVGRectangle.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Text;
44
using System.Xml;
5+
using OpenTK.Graphics.OpenGL4;
56

67
namespace FreeFrame.Components.Shapes
78
{
@@ -24,7 +25,7 @@ public SVGRectangle(XmlReader reader) : this(
2425
Convert.ToInt32(reader["x"]),
2526
Convert.ToInt32(reader["y"]),
2627
Convert.ToInt32(reader["rx"]),
27-
Convert.ToInt32(reader["ry"])) // TODO: Error handler if one of the properties in reader or note here, it should be dynamic
28+
Convert.ToInt32(reader["ry"])) // TODO: Error handler if one of the properties in reader is note here, it should be dynamic
2829
{
2930
}
3031
public SVGRectangle(): this(0, 0, 0, 0) { }
@@ -38,8 +39,16 @@ public SVGRectangle(int width, int height, int x, int y, int rx, int ry)
3839
_height = height;
3940
_rx = rx;
4041
_ry = ry;
42+
43+
UpdateProperties();
44+
}
45+
public override void UpdateProperties()
46+
{
47+
// x, y, x, y, x, y, ... (clockwise)
48+
ImplementObjects(ConvertToNDC(_x, _y, _x + _width, _y, _x + _width, _y + _height, _x, _y + _height), new uint[] { 0, 1, 2, 0, 2, 3 });
4149
}
42-
50+
51+
4352
public override string ToString() => $"x: {_x}, y: {_y}, width: {_width}, height: {_height}, rx: {_rx}, ry: {_ry}";
4453
}
4554
}

FreeFrame/Components/Shapes/Shape.cs

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,98 @@
22
using System.Collections.Generic;
33
using System.Text;
44
using System.Xml;
5+
using OpenTK.Graphics.OpenGL4;
6+
using OpenTK.Windowing.Desktop;
57

68
namespace FreeFrame.Components.Shapes
79
{
810
public abstract class Shape
911
{
12+
static GameWindow? _window;
13+
private Shader _shader;
14+
private int _vertexBufferObject;
15+
private int _vertexArrayObject;
16+
private int _indexBufferObject;
17+
18+
private int _indexCount;
19+
1020
public Shape()
1121
{
22+
_vertexArrayObject = GL.GenVertexArray();
23+
_vertexBufferObject = GL.GenBuffer();
24+
_indexBufferObject = GL.GenBuffer();
25+
_shader = new Shader("Shaders/shader.vert", "Shaders/shader.frag");
26+
}
27+
public static void BindWindow(GameWindow window) => _window = window;
28+
29+
/// <summary>
30+
/// Convert given vertex position attributes in px to NDC
31+
/// </summary>
32+
/// <param name="vertexPositions">vertex position attribute</param>
33+
/// <returns>vertex position attribute in NDC</returns>
34+
/// <exception cref="Exception">Window should be binded before calling</exception>
35+
protected static float[] ConvertToNDC(params int[] vertexPositions)
36+
{
37+
if (_window == null)
38+
throw new Exception("Trying to convert to NDC but no Window is binded");
39+
40+
float[] result = new float[vertexPositions.Length];
41+
for (int i = 0; i < vertexPositions.Length; i+=2)
42+
{
43+
result[i] = vertexPositions[i] / (float)_window.ClientSize.X / 2;
44+
result[i+1] = vertexPositions[i + 1] / (float)_window.ClientSize.Y / 2;
45+
}
46+
return result;
47+
}
48+
49+
/// <summary>
50+
/// Add the given vertex and index to the OpenGL buffers. And link those with an VAO.
51+
/// </summary>
52+
/// <param name="vertex">vertex array</param>
53+
/// <param name="index">index array</param>
54+
protected void ImplementObjects(float[] vertex, uint[] index)
55+
{
56+
// VAO
57+
GL.BindVertexArray(_vertexArrayObject);
1258

59+
string label = $"VAO {GetType().Name}:";
60+
GL.ObjectLabel(ObjectLabelIdentifier.VertexArray, _vertexArrayObject, label.Length, label);
61+
62+
// VBO
63+
GL.BindBuffer(BufferTarget.ArrayBuffer, _vertexBufferObject);
64+
GL.BufferData(BufferTarget.ArrayBuffer, vertex.Length * sizeof(float), vertex, BufferUsageHint.StaticDraw);
65+
66+
label = $"VBO {GetType().Name}:";
67+
GL.ObjectLabel(ObjectLabelIdentifier.Buffer, _vertexBufferObject, label.Length, label);
68+
69+
// IBO
70+
GL.BindBuffer(BufferTarget.ElementArrayBuffer, _indexBufferObject);
71+
GL.BufferData(BufferTarget.ElementArrayBuffer, index.Length * sizeof(uint), index, BufferUsageHint.StaticDraw);
72+
73+
label = $"IBO {GetType().Name}:";
74+
GL.ObjectLabel(ObjectLabelIdentifier.Buffer, _indexBufferObject, label.Length, label);
75+
76+
// Link Attributes
77+
GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, 2 * sizeof(float), 0); // x, y;
78+
GL.EnableVertexAttribArray(0);
79+
80+
_indexCount = index.Length;
81+
}
82+
/// <summary>
83+
/// Trigge draw element throw OpenGL context
84+
/// </summary>
85+
public void Draw()
86+
{
87+
_shader.Use();
88+
GL.BindVertexArray(_vertexArrayObject);
89+
GL.DrawElements(PrimitiveType.Triangles, _indexCount, DrawElementsType.UnsignedInt, 0);
1390
}
1491

15-
//TODO: add method to convert shape position and size to NDC
92+
/// <summary>
93+
/// Should update the size and the position to the new Window size
94+
/// </summary>
95+
public abstract void UpdateProperties();
1696
public abstract override string ToString();
1797
}
98+
1899
}

FreeFrame/Program.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@ class Program
1212
{
1313
static void Main()
1414
{
15-
List<Shape> shapes = Importer.ImportFromFile("test.svg");
16-
shapes.ForEach(shape => Console.WriteLine(shape));
17-
1815
NativeWindowSettings nativeWindowSettings = new()
1916
{
2017
Size = new Vector2i(600, 600),
2118
Title = "FreeFrame"
2219
};
23-
using Window window = new(GameWindowSettings.Default, nativeWindowSettings);
20+
using Window window = new(GameWindowSettings.Default, nativeWindowSettings); // Create window context (GLFW, OpenGL)
21+
22+
23+
2424
window.Run();
25+
2526
}
2627
}
2728
}

FreeFrame/Shader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace FreeFrame
99
{
10-
class Shader
10+
public class Shader
1111
{
1212
private int _program;
1313
int CompileShader(string uri, ShaderType type)

FreeFrame/Shaders/shader.frag

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
layout(location = 0) out vec4 color;
44

5-
uniform vec4 u_Color;
5+
//uniform vec4 u_Color;
66

77
void main()
88
{
9-
color = u_Color;
9+
color = vec4(0.8f, 0.5f, 0.1f, 1.0f);
1010
}

FreeFrame/Shaders/shader.vert

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#version 330 core
22

3-
layout(location = 0) in vec4 position;
3+
layout(location = 0) in vec3 position;
44

55
void main()
66
{
7-
gl_Position = position;
7+
gl_Position = vec4(position, 1.0);
88
}

FreeFrame/Window.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using System.Text;
99
using System.Threading.Tasks;
1010
using FreeFrame.Lib.ImGuiTools;
11+
using FreeFrame.Components;
12+
using FreeFrame.Components.Shapes;
1113

1214
namespace FreeFrame
1315
{
@@ -20,6 +22,9 @@ class Window : GameWindow
2022
//Shader _shader;
2123
ImGuiController _ImGuiController;
2224

25+
List<Shape> shapes;
26+
27+
2328
public readonly float[] _vertices =
2429
{
2530
0.5f, 0.5f, 0.0f, // Top-Right
@@ -44,6 +49,11 @@ protected override void OnLoad()
4449

4550
GL.ClearColor(0.1f, 0.1f, 0.1f, 1.0f);
4651

52+
Shape.BindWindow(this);
53+
54+
shapes = Importer.ImportFromFile("test.svg");
55+
//shapes.ForEach(shape => Console.WriteLine(shape));
56+
4757
//// Vertex Buffer Object
4858
//_vertexBuffer = GL.GenBuffer();
4959
//GL.BindBuffer(BufferTarget.ArrayBuffer, _vertexBuffer);
@@ -65,7 +75,7 @@ protected override void OnLoad()
6575
//_shader = new Shader("Shaders/shader.vert", "Shaders/shader.frag");
6676
//_shader.Use();
6777

68-
rectangle = new Object(_vertices, _indices);
78+
//rectangle = new Object(_vertices, _indices);
6979

7080
_ImGuiController = new ImGuiController(ClientSize.X, ClientSize.Y);
7181
}
@@ -105,7 +115,10 @@ protected override void OnRenderFrame(FrameEventArgs e)
105115

106116
//GL.DrawElements(PrimitiveType.Triangles, _indices.Length, DrawElementsType.UnsignedInt, 0);
107117

108-
rectangle.Draw();
118+
//rectangle.Draw();
119+
120+
shapes[0].UpdateProperties();
121+
shapes[0].Draw();
109122

110123
_ImGuiController.Update(this, (float)e.Time); // TODO: Explain what's the point of this. Also explain why this order is necessary
111124
//ImGui.ShowDemoWindow();

0 commit comments

Comments
 (0)