Skip to content

Commit 7836397

Browse files
Merge pull request #12 from JeremyMeissner/1-import-svg-file
Import SVG successfully
2 parents c2af463 + 2c91b80 commit 7836397

16 files changed

Lines changed: 719 additions & 290 deletions

File tree

FreeFrame/Components/Importer.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,16 @@ namespace FreeFrame.Components
1010
{
1111
public static class Importer
1212
{
13-
static public void Import()
14-
{
15-
//TODO: find a solution for opening a crossplatform file dialog. Do I need a crossplatform dialog?
16-
//using (OpenFileDialog openFileDialog = new OpenFileDialog())
17-
//{
18-
//}
19-
}
20-
static public List<Shape> ImportFromStream(Stream pStream)
13+
static public (List<Shape>, bool) ImportFromStream(Stream pStream)
2114
{
2215
List<Shape> shapes = new List<Shape>();
16+
bool compatibilityFlag = false;
2317

2418
using (XmlReader reader = XmlReader.Create(pStream))
2519
{
2620
while (reader.Read())
2721
{
28-
if (reader.HasAttributes)
22+
//if (reader.HasAttributes)
2923
{
3024
//Console.WriteLine("Attributes of <" + reader.Name + ">");
3125
switch (reader.Name)
@@ -46,15 +40,15 @@ static public List<Shape> ImportFromStream(Stream pStream)
4640
shapes.Add(new SVGCircle(reader));
4741
break;
4842
default:
49-
// TODO: show all the elemnt are not valid, be careful
43+
compatibilityFlag = true; // If an element is unknow, the flag is trigger
5044
break;
5145
}
5246
}
5347
}
5448
}
55-
return (shapes);
49+
return (shapes, compatibilityFlag);
5650
}
57-
static public List<Shape> ImportFromFile(string pFilename)
51+
static public (List<Shape>, bool) ImportFromFile(string pFilename)
5852
{
5953
if (!File.Exists(pFilename))
6054
throw new ArgumentException($"'{pFilename}' file cannot be found.", nameof(pFilename)); // TODO: replace by a simple alert window
@@ -63,7 +57,7 @@ static public List<Shape> ImportFromFile(string pFilename)
6357

6458
return ImportFromStream(new MemoryStream(byteArray));
6559
}
66-
static public List<Shape> ImportFromString(string pString)
60+
static public (List<Shape>, bool) ImportFromString(string pString)
6761
{
6862
byte[] byteArray = Encoding.UTF8.GetBytes(pString);
6963

FreeFrame/Components/Shapes/SVGCircle.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,15 @@ 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 float[] GetVertices()
49+
{
50+
throw new NotImplementedException();
51+
}
52+
53+
public override uint[] GetVerticesIndexes()
54+
{
55+
throw new NotImplementedException();
56+
}
4757
}
4858
}

FreeFrame/Components/Shapes/SVGPath.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,17 @@ public SVGPath(XmlReader reader) //: this()
9898
}
9999
}
100100
}
101+
102+
public override float[] GetVertices()
103+
{
104+
throw new NotImplementedException();
105+
}
106+
107+
public override uint[] GetVerticesIndexes()
108+
{
109+
throw new NotImplementedException();
110+
}
111+
101112
public override string ToString()
102113
{
103114
string output = "d: ";

FreeFrame/Components/Shapes/SVGPolygon.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ public SVGPolygon(XmlReader reader)
2525
_points.Add((Convert.ToInt32(match.Groups[1].Value), Convert.ToInt32(match.Groups[2].Value)));
2626
}
2727

28+
public override float[] GetVertices()
29+
{
30+
throw new NotImplementedException();
31+
}
32+
33+
public override uint[] GetVerticesIndexes()
34+
{
35+
throw new NotImplementedException();
36+
}
37+
2838
public override string ToString()
2939
{
3040
string output = "points: ";

FreeFrame/Components/Shapes/SVGRectangle.cs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,21 @@
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
{
89
public class SVGRectangle : Shape
910
{
11+
#region Default values
12+
const int DefaultX = 0;
13+
const int DefaultY = 0;
14+
const int DefaultWidth = 0;
15+
const int DefaultHeight = 0;
16+
const int DefaultRX = 0;
17+
const int DefaultRY = 0;
18+
#endregion
19+
1020
#region Geometry properties
1121
private int _x;
1222
private int _y;
@@ -24,12 +34,12 @@ public SVGRectangle(XmlReader reader) : this(
2434
Convert.ToInt32(reader["x"]),
2535
Convert.ToInt32(reader["y"]),
2636
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
37+
Convert.ToInt32(reader["ry"])) // TODO: Error handler if one of the properties in reader is note here, it should be dynamic
2838
{
2939
}
30-
public SVGRectangle(): this(0, 0, 0, 0) { }
31-
public SVGRectangle(int width, int height) : this(width, height, 0, 0) { }
32-
public SVGRectangle(int width, int height, int x, int y) : this(width, height, x, y, 0, 0) { }
40+
public SVGRectangle() : this(DefaultWidth, DefaultHeight, DefaultX, DefaultY) { }
41+
public SVGRectangle(int width, int height) : this(width, height, DefaultX, DefaultY) { }
42+
public SVGRectangle(int width, int height, int x, int y) : this(width, height, x, y, DefaultRX, DefaultRY) { }
3343
public SVGRectangle(int width, int height, int x, int y, int rx, int ry)
3444
{
3545
_x = x;
@@ -39,7 +49,17 @@ public SVGRectangle(int width, int height, int x, int y, int rx, int ry)
3949
_rx = rx;
4050
_ry = ry;
4151
}
42-
52+
public override float[] GetVertices()
53+
{
54+
if (_window == null)
55+
throw new Exception("Trying to convert to NDC but no Window is binded");
56+
57+
// x, y, x, y, x, y, ... (clockwise)
58+
return _window.ConvertToNDC(_x, _y, _x + _width, _y, _x + _width, _y + _height, _x, _y + _height);
59+
}
60+
public override uint[] GetVerticesIndexes() => new uint[] { 0, 1, 2, 0, 2, 3 }; // TODO: please dont hardcode
61+
62+
4363
public override string ToString() => $"x: {_x}, y: {_y}, width: {_width}, height: {_height}, rx: {_rx}, ry: {_ry}";
4464
}
4565
}
Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,91 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
4-
using System.Xml;
1+
using OpenTK.Graphics.OpenGL4;
2+
using OpenTK.Windowing.Desktop;
53

64
namespace FreeFrame.Components.Shapes
75
{
86
public abstract class Shape
97
{
10-
public Shape()
8+
protected static Window? _window;
9+
private Shader _shader;
10+
private int _vertexBufferObject;
11+
private int _vertexArrayObject;
12+
private int _indexBufferObject;
13+
14+
private int _indexCount;
15+
16+
public Shape() { }
17+
public static void BindWindow(GameWindow window) => _window = (Window)window;
18+
public void GenerateObjects()
19+
{
20+
_vertexArrayObject = GL.GenVertexArray();
21+
_vertexBufferObject = GL.GenBuffer();
22+
_indexBufferObject = GL.GenBuffer();
23+
_shader = new Shader("Shaders/shader.vert", "Shaders/shader.frag");
24+
}
25+
26+
/// <summary>
27+
/// Add the given vertex and index to the OpenGL buffers. And link those with an VAO.
28+
/// </summary>
29+
/// <param name="vertex">vertex array</param>
30+
/// <param name="index">index array</param>
31+
public void ImplementObjects()
1132
{
33+
float[] vertices = GetVertices();
34+
uint[] indexes = GetVerticesIndexes();
35+
// VAO
36+
GL.BindVertexArray(_vertexArrayObject);
37+
38+
string label = $"VAO {GetType().Name}:";
39+
GL.ObjectLabel(ObjectLabelIdentifier.VertexArray, _vertexArrayObject, label.Length, label);
40+
41+
// VBO
42+
GL.BindBuffer(BufferTarget.ArrayBuffer, _vertexBufferObject);
43+
GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * sizeof(float), vertices, BufferUsageHint.StaticDraw);
44+
45+
label = $"VBO {GetType().Name}:";
46+
GL.ObjectLabel(ObjectLabelIdentifier.Buffer, _vertexBufferObject, label.Length, label);
1247

48+
// IBO
49+
GL.BindBuffer(BufferTarget.ElementArrayBuffer, _indexBufferObject);
50+
GL.BufferData(BufferTarget.ElementArrayBuffer, indexes.Length * sizeof(uint), indexes, BufferUsageHint.StaticDraw);
51+
52+
label = $"IBO {GetType().Name}:";
53+
GL.ObjectLabel(ObjectLabelIdentifier.Buffer, _indexBufferObject, label.Length, label);
54+
55+
// Link Attributes
56+
GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, 2 * sizeof(float), 0); // x, y;
57+
GL.EnableVertexAttribArray(0);
58+
59+
_indexCount = indexes.Length;
1360
}
1461

15-
//TODO: add method to convert shape position and size to NDC
62+
/// <summary>
63+
/// Trigge draw element throw OpenGL context
64+
/// </summary>
65+
public void Draw()
66+
{
67+
_shader.Use();
68+
GL.BindVertexArray(_vertexArrayObject);
69+
GL.DrawElements(PrimitiveType.Triangles, _indexCount, DrawElementsType.UnsignedInt, 0);
70+
}
71+
public void DeleteObjects()
72+
{
73+
GL.DeleteBuffer(_vertexBufferObject);
74+
GL.DeleteBuffer(_indexBufferObject);
75+
GL.DeleteVertexArray(_vertexArrayObject);
76+
_shader.Delete();
77+
}
78+
/// <summary>
79+
/// Should return the vertices position in NDC format
80+
/// </summary>
81+
/// <returns>array of vertices position. x, y, x, y, ... (clockwise)</returns>
82+
public abstract float[] GetVertices();
83+
/// <summary>
84+
/// Should return the indexes position of the triangles
85+
/// </summary>
86+
/// <returns>array of indexes</returns>
87+
public abstract uint[] GetVerticesIndexes();
1688
public abstract override string ToString();
1789
}
90+
1891
}

0 commit comments

Comments
 (0)