Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 7 additions & 13 deletions FreeFrame/Components/Importer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,16 @@ namespace FreeFrame.Components
{
public static class Importer
{
static public void Import()
{
//TODO: find a solution for opening a crossplatform file dialog. Do I need a crossplatform dialog?
//using (OpenFileDialog openFileDialog = new OpenFileDialog())
//{
//}
}
static public List<Shape> ImportFromStream(Stream pStream)
static public (List<Shape>, bool) ImportFromStream(Stream pStream)
{
List<Shape> shapes = new List<Shape>();
bool compatibilityFlag = false;

using (XmlReader reader = XmlReader.Create(pStream))
{
while (reader.Read())
{
if (reader.HasAttributes)
//if (reader.HasAttributes)
{
//Console.WriteLine("Attributes of <" + reader.Name + ">");
switch (reader.Name)
Expand All @@ -46,15 +40,15 @@ static public List<Shape> ImportFromStream(Stream pStream)
shapes.Add(new SVGCircle(reader));
break;
default:
// TODO: show all the elemnt are not valid, be careful
compatibilityFlag = true; // If an element is unknow, the flag is trigger
break;
}
}
}
}
return (shapes);
return (shapes, compatibilityFlag);
}
static public List<Shape> ImportFromFile(string pFilename)
static public (List<Shape>, bool) ImportFromFile(string pFilename)
{
if (!File.Exists(pFilename))
throw new ArgumentException($"'{pFilename}' file cannot be found.", nameof(pFilename)); // TODO: replace by a simple alert window
Expand All @@ -63,7 +57,7 @@ static public List<Shape> ImportFromFile(string pFilename)

return ImportFromStream(new MemoryStream(byteArray));
}
static public List<Shape> ImportFromString(string pString)
static public (List<Shape>, bool) ImportFromString(string pString)
{
byte[] byteArray = Encoding.UTF8.GetBytes(pString);

Expand Down
10 changes: 10 additions & 0 deletions FreeFrame/Components/Shapes/SVGCircle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,15 @@ public SVGCircle(int r, int cx, int cy)
}

public override string ToString() => $"cx: {_cx}, cy: {_cy}, r: {_r}";

public override float[] GetVertices()
{
throw new NotImplementedException();
}

public override uint[] GetVerticesIndexes()
{
throw new NotImplementedException();
}
}
}
11 changes: 11 additions & 0 deletions FreeFrame/Components/Shapes/SVGPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,17 @@ public SVGPath(XmlReader reader) //: this()
}
}
}

public override float[] GetVertices()
{
throw new NotImplementedException();
}

public override uint[] GetVerticesIndexes()
{
throw new NotImplementedException();
}

public override string ToString()
{
string output = "d: ";
Expand Down
10 changes: 10 additions & 0 deletions FreeFrame/Components/Shapes/SVGPolygon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ public SVGPolygon(XmlReader reader)
_points.Add((Convert.ToInt32(match.Groups[1].Value), Convert.ToInt32(match.Groups[2].Value)));
}

public override float[] GetVertices()
{
throw new NotImplementedException();
}

public override uint[] GetVerticesIndexes()
{
throw new NotImplementedException();
}

public override string ToString()
{
string output = "points: ";
Expand Down
30 changes: 25 additions & 5 deletions FreeFrame/Components/Shapes/SVGRectangle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@
using System.Collections.Generic;
using System.Text;
using System.Xml;
using OpenTK.Graphics.OpenGL4;

namespace FreeFrame.Components.Shapes
{
public class SVGRectangle : Shape
{
#region Default values
const int DefaultX = 0;
const int DefaultY = 0;
const int DefaultWidth = 0;
const int DefaultHeight = 0;
const int DefaultRX = 0;
const int DefaultRY = 0;
#endregion

#region Geometry properties
private int _x;
private int _y;
Expand All @@ -24,12 +34,12 @@ public SVGRectangle(XmlReader reader) : this(
Convert.ToInt32(reader["x"]),
Convert.ToInt32(reader["y"]),
Convert.ToInt32(reader["rx"]),
Convert.ToInt32(reader["ry"])) // TODO: Error handler if one of the properties in reader or note here, it should be dynamic
Convert.ToInt32(reader["ry"])) // TODO: Error handler if one of the properties in reader is note here, it should be dynamic
{
}
public SVGRectangle(): this(0, 0, 0, 0) { }
public SVGRectangle(int width, int height) : this(width, height, 0, 0) { }
public SVGRectangle(int width, int height, int x, int y) : this(width, height, x, y, 0, 0) { }
public SVGRectangle() : this(DefaultWidth, DefaultHeight, DefaultX, DefaultY) { }
public SVGRectangle(int width, int height) : this(width, height, DefaultX, DefaultY) { }
public SVGRectangle(int width, int height, int x, int y) : this(width, height, x, y, DefaultRX, DefaultRY) { }
public SVGRectangle(int width, int height, int x, int y, int rx, int ry)
{
_x = x;
Expand All @@ -39,7 +49,17 @@ public SVGRectangle(int width, int height, int x, int y, int rx, int ry)
_rx = rx;
_ry = ry;
}

public override float[] GetVertices()
{
if (_window == null)
throw new Exception("Trying to convert to NDC but no Window is binded");

// x, y, x, y, x, y, ... (clockwise)
return _window.ConvertToNDC(_x, _y, _x + _width, _y, _x + _width, _y + _height, _x, _y + _height);
}
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}";
}
}
85 changes: 79 additions & 6 deletions FreeFrame/Components/Shapes/Shape.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,91 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Windowing.Desktop;

namespace FreeFrame.Components.Shapes
{
public abstract class Shape
{
public Shape()
protected static Window? _window;
private Shader _shader;
private int _vertexBufferObject;
private int _vertexArrayObject;
private int _indexBufferObject;

private int _indexCount;

public Shape() { }
public static void BindWindow(GameWindow window) => _window = (Window)window;
public void GenerateObjects()
{
_vertexArrayObject = GL.GenVertexArray();
_vertexBufferObject = GL.GenBuffer();
_indexBufferObject = GL.GenBuffer();
_shader = new Shader("Shaders/shader.vert", "Shaders/shader.frag");
}

/// <summary>
/// Add the given vertex and index to the OpenGL buffers. And link those with an VAO.
/// </summary>
/// <param name="vertex">vertex array</param>
/// <param name="index">index array</param>
public void ImplementObjects()
{
float[] vertices = GetVertices();
uint[] indexes = GetVerticesIndexes();
// VAO
GL.BindVertexArray(_vertexArrayObject);

string label = $"VAO {GetType().Name}:";
GL.ObjectLabel(ObjectLabelIdentifier.VertexArray, _vertexArrayObject, label.Length, label);

// VBO
GL.BindBuffer(BufferTarget.ArrayBuffer, _vertexBufferObject);
GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * sizeof(float), vertices, BufferUsageHint.StaticDraw);

label = $"VBO {GetType().Name}:";
GL.ObjectLabel(ObjectLabelIdentifier.Buffer, _vertexBufferObject, label.Length, label);

// IBO
GL.BindBuffer(BufferTarget.ElementArrayBuffer, _indexBufferObject);
GL.BufferData(BufferTarget.ElementArrayBuffer, indexes.Length * sizeof(uint), indexes, BufferUsageHint.StaticDraw);

label = $"IBO {GetType().Name}:";
GL.ObjectLabel(ObjectLabelIdentifier.Buffer, _indexBufferObject, label.Length, label);

// Link Attributes
GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, 2 * sizeof(float), 0); // x, y;
GL.EnableVertexAttribArray(0);

_indexCount = indexes.Length;
}

//TODO: add method to convert shape position and size to NDC
/// <summary>
/// Trigge draw element throw OpenGL context
/// </summary>
public void Draw()
{
_shader.Use();
GL.BindVertexArray(_vertexArrayObject);
GL.DrawElements(PrimitiveType.Triangles, _indexCount, DrawElementsType.UnsignedInt, 0);
}
public void DeleteObjects()
{
GL.DeleteBuffer(_vertexBufferObject);
GL.DeleteBuffer(_indexBufferObject);
GL.DeleteVertexArray(_vertexArrayObject);
_shader.Delete();
}
/// <summary>
/// Should return the vertices position in NDC format
/// </summary>
/// <returns>array of vertices position. x, y, x, y, ... (clockwise)</returns>
public abstract float[] GetVertices();
/// <summary>
/// Should return the indexes position of the triangles
/// </summary>
/// <returns>array of indexes</returns>
public abstract uint[] GetVerticesIndexes();
public abstract override string ToString();
}

}
Loading