From b11a7afab36cf6b30c094de559a04059ba82fa0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20Nie=C3=9Fner?= Date: Fri, 14 May 2021 10:59:56 +0200 Subject: [PATCH 1/2] Update project structure --- Line.cs | 111 ----------------------------- ball.png => assets/ball.png | Bin line.fx => assets/shaders/line.fx | 0 src/Primitives/Line.cs | 114 ++++++++++++++++++++++++++++++ Program.cs => src/Program.cs | 6 +- 5 files changed, 118 insertions(+), 113 deletions(-) delete mode 100644 Line.cs rename ball.png => assets/ball.png (100%) rename line.fx => assets/shaders/line.fx (100%) create mode 100644 src/Primitives/Line.cs rename Program.cs => src/Program.cs (95%) diff --git a/Line.cs b/Line.cs deleted file mode 100644 index de79d9e..0000000 --- a/Line.cs +++ /dev/null @@ -1,111 +0,0 @@ -using System.Collections.Generic; -using System.Diagnostics; -using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Graphics; - -public class Line -{ - private List points; - private float thickness = 1.0f; - - public enum JoinType - { - None, - Miter, - Bevel, - Round - } - - public enum LineCaps - { - None, - Round - } - - private Color LineColor { get; set; } - - //Graphical representation - private VertexBuffer vbo; - private IndexBuffer ibo; - private GraphicsDevice gDevice; - - public Line(List points) - { - this.points = points; - this.LineColor = Color.Black; - } - - public Line(List points, float thickness) - { - this.points = points; - this.thickness = thickness; - this.LineColor = Color.Black; - } - - public Line(List points, float thickness, Color color) - { - this.points = points; - this.thickness = thickness; - this.LineColor = color; - } - - public void InitOnGraphicalDevice(GraphicsDevice device) - { - this.gDevice = device; - Debug.Assert(this.points.Count >= 2); - List vertices = new List(); - List indices = new List(); - - for(int i = 0; i < this.points.Count - 1 ; i++) - { - var p = this.points[i]; - var pNext = this.points[i+1]; - - Vector2 dir = pNext - p; - Vector2 perpendincularVector = new Vector2(dir.Y, -dir.X); - perpendincularVector.Normalize(); - - var offsetPoint1 = new Vector3(p.X, p.Y ,0.0f); - var offsetPoint2 = new Vector3(p.X, p.Y ,0.0f); - vertices.Add(new VertexPositionColorTexture(offsetPoint1,this.LineColor, perpendincularVector)); - vertices.Add(new VertexPositionColorTexture(offsetPoint2,this.LineColor, perpendincularVector * -1)); - var offsetPoint1Next = new Vector3(pNext.X, pNext.Y ,0.0f); - var offsetPoint2Next = new Vector3(pNext.X, pNext.Y ,0.0f); - vertices.Add(new VertexPositionColorTexture(offsetPoint1Next,this.LineColor, perpendincularVector)); - vertices.Add(new VertexPositionColorTexture(offsetPoint2Next,this.LineColor, perpendincularVector * -1)); - } - - for (int i = 0; i < vertices.Count; i+=4) - { - indices.Add((ushort)(i)); - indices.Add((ushort)(i + 1)); - indices.Add((ushort)(i + 2)); - - indices.Add((ushort)(i + 2)); - indices.Add((ushort)(i + 1)); - indices.Add((ushort)(i + 3)); - } - - - this.vbo = new VertexBuffer(device, typeof(VertexPositionColorTexture), (this.points.Count - 1) * 4, BufferUsage.WriteOnly); - this.vbo.SetData(vertices.ToArray()); - this.ibo = new IndexBuffer(device, typeof(ushort), indices.Count,BufferUsage.WriteOnly); - this.ibo.SetData(indices.ToArray()); - } - - public void Draw(Effect lineEffect) - { - this.gDevice.SetVertexBuffer(this.vbo); - this.gDevice.Indices = this.ibo; - RasterizerState rasterizerState = new RasterizerState(); - rasterizerState.CullMode = CullMode.None; - this.gDevice.RasterizerState = rasterizerState; - lineEffect.Parameters["thickness"].SetValue(this.thickness); - foreach (EffectPass pass in lineEffect.CurrentTechnique.Passes) - { - pass.Apply(); - this.gDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList,0,0,this.ibo.IndexCount / 3); - //this.gDevice.DrawPrimitives(PrimitiveType.LineList, 0, this.vbo.VertexCount); - } - } -} \ No newline at end of file diff --git a/ball.png b/assets/ball.png similarity index 100% rename from ball.png rename to assets/ball.png diff --git a/line.fx b/assets/shaders/line.fx similarity index 100% rename from line.fx rename to assets/shaders/line.fx diff --git a/src/Primitives/Line.cs b/src/Primitives/Line.cs new file mode 100644 index 0000000..67f5678 --- /dev/null +++ b/src/Primitives/Line.cs @@ -0,0 +1,114 @@ +using System.Collections.Generic; +using System.Diagnostics; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; + +namespace MyGame.Primitives +{ + public class Line + { + private List points; + private float thickness = 1.0f; + + public enum JoinType + { + None, + Miter, + Bevel, + Round + } + + public enum LineCaps + { + None, + Round + } + + private Color LineColor { get; set; } + + //Graphical representation + private VertexBuffer vbo; + private IndexBuffer ibo; + private GraphicsDevice gDevice; + + public Line(List points) + { + this.points = points; + this.LineColor = Color.Black; + } + + public Line(List points, float thickness) + { + this.points = points; + this.thickness = thickness; + this.LineColor = Color.Black; + } + + public Line(List points, float thickness, Color color) + { + this.points = points; + this.thickness = thickness; + this.LineColor = color; + } + + public void InitOnGraphicalDevice(GraphicsDevice device) + { + this.gDevice = device; + Debug.Assert(this.points.Count >= 2); + List vertices = new List(); + List indices = new List(); + + for (int i = 0; i < this.points.Count - 1; i++) + { + var p = this.points[i]; + var pNext = this.points[i + 1]; + + Vector2 dir = pNext - p; + Vector2 perpendincularVector = new Vector2(dir.Y, -dir.X); + perpendincularVector.Normalize(); + + var offsetPoint1 = new Vector3(p.X, p.Y, 0.0f); + var offsetPoint2 = new Vector3(p.X, p.Y, 0.0f); + vertices.Add(new VertexPositionColorTexture(offsetPoint1, this.LineColor, perpendincularVector)); + vertices.Add(new VertexPositionColorTexture(offsetPoint2, this.LineColor, perpendincularVector * -1)); + var offsetPoint1Next = new Vector3(pNext.X, pNext.Y, 0.0f); + var offsetPoint2Next = new Vector3(pNext.X, pNext.Y, 0.0f); + vertices.Add(new VertexPositionColorTexture(offsetPoint1Next, this.LineColor, perpendincularVector)); + vertices.Add(new VertexPositionColorTexture(offsetPoint2Next, this.LineColor, perpendincularVector * -1)); + } + + for (int i = 0; i < vertices.Count; i += 4) + { + indices.Add((ushort)(i)); + indices.Add((ushort)(i + 1)); + indices.Add((ushort)(i + 2)); + + indices.Add((ushort)(i + 2)); + indices.Add((ushort)(i + 1)); + indices.Add((ushort)(i + 3)); + } + + + this.vbo = new VertexBuffer(device, typeof(VertexPositionColorTexture), (this.points.Count - 1) * 4, BufferUsage.WriteOnly); + this.vbo.SetData(vertices.ToArray()); + this.ibo = new IndexBuffer(device, typeof(ushort), indices.Count, BufferUsage.WriteOnly); + this.ibo.SetData(indices.ToArray()); + } + + public void Draw(Effect lineEffect) + { + this.gDevice.SetVertexBuffer(this.vbo); + this.gDevice.Indices = this.ibo; + RasterizerState rasterizerState = new RasterizerState(); + rasterizerState.CullMode = CullMode.None; + this.gDevice.RasterizerState = rasterizerState; + lineEffect.Parameters["thickness"].SetValue(this.thickness); + foreach (EffectPass pass in lineEffect.CurrentTechnique.Passes) + { + pass.Apply(); + this.gDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, this.ibo.IndexCount / 3); + //this.gDevice.DrawPrimitives(PrimitiveType.LineList, 0, this.vbo.VertexCount); + } + } + } +} \ No newline at end of file diff --git a/Program.cs b/src/Program.cs similarity index 95% rename from Program.cs rename to src/Program.cs index 822cb5d..ebc4be4 100644 --- a/Program.cs +++ b/src/Program.cs @@ -3,6 +3,8 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; +using MyGame.Primitives; + namespace Barbs { class Program : Game @@ -19,7 +21,7 @@ namespace Barbs public Program() { graphics = new GraphicsDeviceManager(this); - Content.RootDirectory = "."; + Content.RootDirectory = "assets"; } protected override void Initialize() @@ -43,7 +45,7 @@ namespace Barbs projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), 800f / 480f, 0.01f, 100f); basicEffect = new BasicEffect(GraphicsDevice); - lineEffect = Content.Load("line"); + lineEffect = Content.Load("shaders/line"); Vector2[] arr = { new Vector2(0.0f,0.0f), new Vector2(0.2f,0.5f), new Vector2(0.2f,0.0f), new Vector2(0.5f,0.0f), new Vector2(0.7f,-0.5f)}; From e907ff08bfcf7f80d69a9be9b4d583a547a183bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20Nie=C3=9Fner?= Date: Fri, 14 May 2021 11:14:17 +0200 Subject: [PATCH 2/2] Merge remote-tracking branch 'origin/main' --- Screens/IScreen.cs | 9 ++ Screens/MenuScreen.cs | 15 +++ assets/shaders/line.fx | 2 +- src/Primitives/Line.cs | 293 ++++++++++++++++++++++++++++++++++++----- src/Program.cs | 28 ++-- 5 files changed, 303 insertions(+), 44 deletions(-) create mode 100644 Screens/IScreen.cs create mode 100644 Screens/MenuScreen.cs diff --git a/Screens/IScreen.cs b/Screens/IScreen.cs new file mode 100644 index 0000000..7b4130f --- /dev/null +++ b/Screens/IScreen.cs @@ -0,0 +1,9 @@ + +namespace MyGame.Screens +{ + public interface IScreen + { + void Update(); + void Draw(); + } +} diff --git a/Screens/MenuScreen.cs b/Screens/MenuScreen.cs new file mode 100644 index 0000000..a0681c0 --- /dev/null +++ b/Screens/MenuScreen.cs @@ -0,0 +1,15 @@ +namespace MyGame.Screens +{ + public class MenuScreen : IScreen + { + public void Update() + { + + } + + public void Draw() + { + + } + } +} diff --git a/assets/shaders/line.fx b/assets/shaders/line.fx index 55c59e9..19f3259 100644 --- a/assets/shaders/line.fx +++ b/assets/shaders/line.fx @@ -40,7 +40,7 @@ float4 MainPS(VertexShaderOutput input) : COLOR float4 res = input.Color; float distFromLine = length(input.Normal); float alpha = 1.0 - ((clamp(distFromLine, (1.0 - feather),1.0) - (1.0 - feather)) / feather); - res.a = alpha; + //res.a = alpha; return res; } diff --git a/src/Primitives/Line.cs b/src/Primitives/Line.cs index 67f5678..a89c0ef 100644 --- a/src/Primitives/Line.cs +++ b/src/Primitives/Line.cs @@ -1,14 +1,20 @@ using System.Collections.Generic; +using System.Linq; using System.Diagnostics; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; +using System.Collections; +using System; + namespace MyGame.Primitives { + + public class Line { private List points; - private float thickness = 1.0f; + private float thickness; public enum JoinType { @@ -25,71 +31,289 @@ namespace MyGame.Primitives } private Color LineColor { get; set; } + private LineCaps LineEndings { get; set; } + private JoinType JoinTypes { get; set; } //Graphical representation private VertexBuffer vbo; private IndexBuffer ibo; private GraphicsDevice gDevice; - public Line(List points) + private class LinkedMeshList : IEnumerable { - this.points = points; - this.LineColor = Color.Black; + public int Count { get; set; } + + public LinkedMeshListNode FirstNode { get; private set; } + + public LinkedMeshListNode LastNode { get; private set; } + + public LinkedMeshList() + { + this.Count = 0; + } + + public void Remove(LinkedMeshListNode node) + { + throw new NotImplementedException(); + } + + public void AddBefore(LinkedMeshListNode node, LinkedMeshListNode beforeNode) + { + var oldBefore = node.Previous; + + oldBefore.Next = beforeNode; + beforeNode.Previous = oldBefore; + + beforeNode.Next = node; + node.Previous = beforeNode; + + Count++; + } + + public void AddAfter(LinkedMeshListNode node, LinkedMeshListNode afterNode) + { + var oldNext = node.Next; + + node.Next = afterNode; + afterNode.Previous = node; + + afterNode.Next = oldNext; + oldNext.Previous = afterNode; + + Count++; + } + + public void Append(LinkedMeshListNode node) + { + if (this.Count == 0) + { + this.FirstNode = node; + this.LastNode = node; + Count++; + return; + } + + this.LastNode.Next = node; + node.Previous = this.LastNode; + this.LastNode = node; + this.Count++; + } + + public List GetVerticeList() + { + List verticesList = new List(); + foreach (var node in this) + { + verticesList.AddRange(node.GenerateVerticeList()); + } + return verticesList; + } + + public List GetIndiceList() + { + List indeicesList = new List(); + foreach (var node in this) + { + indeicesList.AddRange(node.GenerateIndiceList()); + } + return indeicesList; + } + + public IEnumerator GetEnumerator() + { + LinkedMeshListNode curNode = this.FirstNode; + while (curNode != null) + { + yield + return curNode; + curNode = curNode.Next; + } + } + + IEnumerator IEnumerable.GetEnumerator() + { + throw new NotImplementedException(); + } } + private abstract class LinkedMeshListNode + { + public LinkedMeshListNode Next { get; set; } + + private LinkedMeshListNode previous; + public LinkedMeshListNode Previous + { + get + { + return this.previous; + } + set + { + this.IndexOffset = value.IndexOffset + value.GetVertexCount(); + this.previous = value; + } + } + + public int IndexOffset { get; private set; } + + public abstract int GetVertexCount(); + + public abstract List GenerateVerticeList(); + public abstract List GenerateIndiceList(); + + public LinkedMeshListNode() + { + this.IndexOffset = 0; + } + } + + private class Segment : LinkedMeshListNode + { + public Vector2 Start { get; private set; } + public Vector2 End { get; private set; } + public Vector2 Normal { get; private set; } + public const ushort INDEX_BL = 0; + public const ushort INDEX_BR = 1; + public const ushort INDEX_TL = 2; + public const ushort INDEX_TR = 3; + private Color color { get; set; } + + public Segment(Vector2 start, Vector2 end, Color color) : base() + { + this.Start = start; + this.End = end; + Vector2 dir = end - start; + Vector2 perpendincularVector = new Vector2(dir.Y, -dir.X); + perpendincularVector.Normalize(); + this.Normal = perpendincularVector; + this.color = color; + } + + public override List GenerateVerticeList() + { + List vertices = new List(); + var offsetPoint1 = new Vector3(this.Start.X, this.Start.Y, 0.0f); + vertices.Add(new VertexPositionColorTexture(offsetPoint1, this.color, this.Normal)); + vertices.Add(new VertexPositionColorTexture(offsetPoint1, this.color, this.Normal * -1)); + var offsetPoint1Next = new Vector3(this.End.X, this.End.Y, 0.0f); + vertices.Add(new VertexPositionColorTexture(offsetPoint1Next, this.color, this.Normal)); + vertices.Add(new VertexPositionColorTexture(offsetPoint1Next, this.color, this.Normal * -1)); + return vertices; + } + + public override List GenerateIndiceList() + { + List indices = new List(); + indices.Add((ushort)(this.IndexOffset + INDEX_BL)); + indices.Add((ushort)(this.IndexOffset + INDEX_BR)); + indices.Add((ushort)(this.IndexOffset + INDEX_TL)); + + indices.Add((ushort)(this.IndexOffset + INDEX_TL)); + indices.Add((ushort)(this.IndexOffset + INDEX_BR)); + indices.Add((ushort)(this.IndexOffset + INDEX_TR)); + + return indices; + } + + public override int GetVertexCount() + { + return 4; + } + } + + private class BevelLineJoin : LinkedMeshListNode + { + private Color color { get; set; } + + public BevelLineJoin(Color color) : base() + { + this.color = color; + } + + public override List GenerateIndiceList() + { + Debug.Assert(this.Previous != null); + Debug.Assert(this.Previous is Segment); + Debug.Assert(this.Next != null); + Debug.Assert(this.Next is Segment); + var next = this.Next as Segment; + var previous = this.Previous as Segment; + + ushort lastOffset = (ushort)previous.IndexOffset; + ushort nextOffset = (ushort)next.IndexOffset; + + List indices = new List(); + indices.Add((ushort)(lastOffset + Segment.INDEX_TL)); + indices.Add((ushort)(lastOffset + Segment.INDEX_TR)); + indices.Add((ushort)(nextOffset + Segment.INDEX_BL)); + + indices.Add((ushort)(lastOffset + Segment.INDEX_TL)); + indices.Add((ushort)(lastOffset + Segment.INDEX_TR)); + indices.Add((ushort)(nextOffset + Segment.INDEX_BR)); + return indices; + } + + public override List GenerateVerticeList() + { + return new List(); + } + + public override int GetVertexCount() + { + return 0; + } + } + + public Line(List points) + : this(points, 1.0f) + { } + public Line(List points, float thickness) - { - this.points = points; - this.thickness = thickness; - this.LineColor = Color.Black; - } + : this(points, thickness, Color.Black) + { } public Line(List points, float thickness, Color color) + : this(points, thickness, color, JoinType.None) + { } + + public Line(List points, float thickness, Color color, JoinType joinType) + : this(points, thickness, color, joinType, LineCaps.None) + { } + + public Line(List points, float thickness, Color color, JoinType joinType, LineCaps caps) { this.points = points; this.thickness = thickness; this.LineColor = color; + this.JoinTypes = joinType; + this.LineEndings = caps; } public void InitOnGraphicalDevice(GraphicsDevice device) { this.gDevice = device; Debug.Assert(this.points.Count >= 2); - List vertices = new List(); - List indices = new List(); + + LinkedMeshList meshList = new LinkedMeshList(); for (int i = 0; i < this.points.Count - 1; i++) { var p = this.points[i]; var pNext = this.points[i + 1]; - - Vector2 dir = pNext - p; - Vector2 perpendincularVector = new Vector2(dir.Y, -dir.X); - perpendincularVector.Normalize(); - - var offsetPoint1 = new Vector3(p.X, p.Y, 0.0f); - var offsetPoint2 = new Vector3(p.X, p.Y, 0.0f); - vertices.Add(new VertexPositionColorTexture(offsetPoint1, this.LineColor, perpendincularVector)); - vertices.Add(new VertexPositionColorTexture(offsetPoint2, this.LineColor, perpendincularVector * -1)); - var offsetPoint1Next = new Vector3(pNext.X, pNext.Y, 0.0f); - var offsetPoint2Next = new Vector3(pNext.X, pNext.Y, 0.0f); - vertices.Add(new VertexPositionColorTexture(offsetPoint1Next, this.LineColor, perpendincularVector)); - vertices.Add(new VertexPositionColorTexture(offsetPoint2Next, this.LineColor, perpendincularVector * -1)); + meshList.Append(new Segment(p, pNext, this.LineColor)); } - for (int i = 0; i < vertices.Count; i += 4) + var curNode = meshList.FirstNode; + while (curNode != null && curNode.Next != null) { - indices.Add((ushort)(i)); - indices.Add((ushort)(i + 1)); - indices.Add((ushort)(i + 2)); - - indices.Add((ushort)(i + 2)); - indices.Add((ushort)(i + 1)); - indices.Add((ushort)(i + 3)); + meshList.AddAfter(curNode, new BevelLineJoin(this.LineColor)); + curNode = curNode.Next.Next; } + List vertices = meshList.GetVerticeList(); + List indices = meshList.GetIndiceList(); - this.vbo = new VertexBuffer(device, typeof(VertexPositionColorTexture), (this.points.Count - 1) * 4, BufferUsage.WriteOnly); + this.vbo = new VertexBuffer(device, typeof(VertexPositionColorTexture), vertices.Count, BufferUsage.WriteOnly); this.vbo.SetData(vertices.ToArray()); this.ibo = new IndexBuffer(device, typeof(ushort), indices.Count, BufferUsage.WriteOnly); this.ibo.SetData(indices.ToArray()); @@ -100,7 +324,8 @@ namespace MyGame.Primitives this.gDevice.SetVertexBuffer(this.vbo); this.gDevice.Indices = this.ibo; RasterizerState rasterizerState = new RasterizerState(); - rasterizerState.CullMode = CullMode.None; + // rasterizerState.CullMode = CullMode.None; + //rasterizerState.FillMode = FillMode.WireFrame; this.gDevice.RasterizerState = rasterizerState; lineEffect.Parameters["thickness"].SetValue(this.thickness); foreach (EffectPass pass in lineEffect.CurrentTechnique.Passes) diff --git a/src/Program.cs b/src/Program.cs index ebc4be4..1e6af42 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; @@ -7,7 +8,7 @@ using MyGame.Primitives; namespace Barbs { - class Program : Game + class Program : Game, IDisposable { Texture2D ballTexture; private GraphicsDeviceManager graphics; @@ -26,10 +27,10 @@ namespace Barbs protected override void Initialize() { - // TODO: Add your initialization logic here - graphics.PreferredBackBufferHeight = 1824; - graphics.PreferredBackBufferWidth = 2736; + graphics.PreferredBackBufferHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height; + graphics.PreferredBackBufferWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width; graphics.ApplyChanges(); + Window.IsBorderless = true; base.Initialize(); } @@ -41,18 +42,24 @@ namespace Barbs ballTexture = Content.Load("ball"); world = Matrix.CreateTranslation(0, 0, 0); - view = Matrix.CreateLookAt(new Vector3(0, 0, 3), new Vector3(0, 0, 0), new Vector3(0, 1, 0)); + view = Matrix.CreateLookAt(new Vector3(0.3f, 0, 2.0f), new Vector3(0, 0, 0), new Vector3(0, 1, 0)); projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), 800f / 480f, 0.01f, 100f); basicEffect = new BasicEffect(GraphicsDevice); lineEffect = Content.Load("shaders/line"); Vector2[] arr = { new Vector2(0.0f,0.0f), new Vector2(0.2f,0.5f), new Vector2(0.2f,0.0f), new Vector2(0.5f,0.0f), - new Vector2(0.7f,-0.5f)}; + new Vector2(0.7f,-0.5f), new Vector2(1.0f, 1.0f)}; this.testLine = new Line(new List(arr), 0.01f, Color.DarkSlateGray); this.testLine.InitOnGraphicalDevice(GraphicsDevice); } + protected void Dispose() + { + //Cleanup + // OR Final call before shutdown + } + protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) @@ -84,8 +91,11 @@ namespace Barbs static void Main(string[] args) { - var pro = new Program(); - pro.Run(); + + using (Program game = new Program()) + { + game.Run(); + } } } }