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
refactor: shape structure
  • Loading branch information
JeremyMeissner committed Apr 11, 2022
commit 2c91b80dae7fce5c546a17831ed3e020ff02b4f2
7 changes: 6 additions & 1 deletion FreeFrame/Components/Shapes/SVGCircle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ public SVGCircle(int r, int cx, int cy)

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

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

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

public override float[] GetVertices()
{
string output = "d: ";
_drawAttribute.ForEach(d => output += d.ToString() + " ");
return output.Trim();
throw new NotImplementedException();
}

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

public override string ToString()
{
string output = "d: ";
_drawAttribute.ForEach(d => output += d.ToString() + " ");
return output.Trim();
}
}
}
15 changes: 10 additions & 5 deletions FreeFrame/Components/Shapes/SVGPolygon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,22 @@ 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: ";
foreach ((int x, int y) point in _points)
output += string.Format("{0},{1} ", point.x, point.y);
return output.Trim();
}

public override void UpdateProperties()
{
throw new NotImplementedException();
}
}
}
23 changes: 18 additions & 5 deletions FreeFrame/Components/Shapes/SVGRectangle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ 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 @@ -28,9 +37,9 @@ public SVGRectangle(XmlReader reader) : this(
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 @@ -40,11 +49,15 @@ public SVGRectangle(int width, int height, int x, int y, int rx, int ry)
_rx = rx;
_ry = ry;
}
public override void UpdateProperties()
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)
ImplementObjects(ConvertToNDC(_x, _y, _x + _width, _y, _x + _width, _y + _height, _x, _y + _height), new uint[] { 0, 1, 2, 0, 2, 3 });
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}";
Expand Down
50 changes: 18 additions & 32 deletions FreeFrame/Components/Shapes/Shape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,33 @@ namespace FreeFrame.Components.Shapes
{
public abstract class Shape
{
static GameWindow? _window;
protected static Window? _window;
private Shader _shader;
private int _vertexBufferObject;
private int _vertexArrayObject;
private int _indexBufferObject;

private int _indexCount;

public Shape()
{

}
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");
}
public static void BindWindow(GameWindow window) => _window = window;

/// <summary>
/// Convert given vertex position attributes in px to NDC
/// </summary>
/// <param name="vertexPositions">vertex position attribute</param>
/// <returns>vertex position attribute in NDC</returns>
/// <exception cref="Exception">Window should be binded before calling</exception>
protected static float[] ConvertToNDC(params int[] vertexPositions)
{
if (_window == null)
throw new Exception("Trying to convert to NDC but no Window is binded");

float[] result = new float[vertexPositions.Length];
for (int i = 0; i < vertexPositions.Length; i+=2)
{
result[i] = vertexPositions[i] / (float)_window.ClientSize.X / 2;
result[i+1] = vertexPositions[i + 1] / (float)_window.ClientSize.Y / 2;
}
return result;
}

/// <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>
protected void ImplementObjects(float[] vertex, uint[] index)
public void ImplementObjects()
{
float[] vertices = GetVertices();
uint[] indexes = GetVerticesIndexes();
// VAO
GL.BindVertexArray(_vertexArrayObject);

Expand All @@ -61,14 +40,14 @@ protected void ImplementObjects(float[] vertex, uint[] index)

// VBO
GL.BindBuffer(BufferTarget.ArrayBuffer, _vertexBufferObject);
GL.BufferData(BufferTarget.ArrayBuffer, vertex.Length * sizeof(float), vertex, BufferUsageHint.StaticDraw);
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, index.Length * sizeof(uint), index, BufferUsageHint.StaticDraw);
GL.BufferData(BufferTarget.ElementArrayBuffer, indexes.Length * sizeof(uint), indexes, BufferUsageHint.StaticDraw);

label = $"IBO {GetType().Name}:";
GL.ObjectLabel(ObjectLabelIdentifier.Buffer, _indexBufferObject, label.Length, label);
Expand All @@ -77,8 +56,9 @@ protected void ImplementObjects(float[] vertex, uint[] index)
GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, 2 * sizeof(float), 0); // x, y;
GL.EnableVertexAttribArray(0);

_indexCount = index.Length;
_indexCount = indexes.Length;
}

/// <summary>
/// Trigge draw element throw OpenGL context
/// </summary>
Expand All @@ -96,9 +76,15 @@ public void DeleteObjects()
_shader.Delete();
}
/// <summary>
/// Should update the size and the position to the new Window size
/// 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>
public abstract void UpdateProperties();
/// <returns>array of indexes</returns>
public abstract uint[] GetVerticesIndexes();
public abstract override string ToString();
}

Expand Down
24 changes: 20 additions & 4 deletions FreeFrame/Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace FreeFrame
{
class Window : GameWindow
public class Window : GameWindow
{
int _ioX;
int _ioY;
Expand Down Expand Up @@ -74,7 +74,7 @@ protected override void OnRenderFrame(FrameEventArgs e)

if (_shapes != null)
{
_shapes[0].UpdateProperties();
_shapes[0].ImplementObjects();
_shapes[0].Draw();
}

Expand All @@ -95,18 +95,34 @@ protected override void OnUnload()
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
GL.BindVertexArray(0);
GL.UseProgram(0);

_shapes.ForEach(shape => shape.DeleteObjects());
}

/// <summary>
/// Convert given vertex position attributes in px to NDC
/// </summary>
/// <param name="vertexPositions">vertex position attribute</param>
/// <returns>vertex position attribute in NDC</returns>
public float[] ConvertToNDC(params int[] vertexPositions)
{
float[] result = new float[vertexPositions.Length];
for (int i = 0; i < vertexPositions.Length; i += 2)
{
result[i] = (float)vertexPositions[i] / ClientSize.X / 2; // TODO: maybe a better code?
result[i + 1] = (float)vertexPositions[i + 1] / ClientSize.Y / 2;
}
return result;
}

public void ShowUI()
{
// ImGui settings
ImGui.GetStyle().WindowRounding = 0.0f;
ImGui.GetStyle().ScrollbarRounding = 0.0f;
ImGui.GetStyle().LogSliderDeadzone = 0.0f;
ImGui.GetStyle().TabRounding = 0.0f;


// Parameters side
ImGui.Begin("Parameters", ImGuiWindowFlags.NoCollapse | ImGuiWindowFlags.NoResize | ImGuiWindowFlags.NoMove | ImGuiWindowFlags.NoTitleBar);
ImGui.SetWindowSize(new System.Numerics.Vector2(200, ClientSize.Y / 2));
Expand Down