Update project structure

This commit is contained in:
Julian Nießner
2021-05-14 10:59:56 +02:00
parent 014a62b2a1
commit b11a7afab3
5 changed files with 118 additions and 113 deletions

111
Line.cs
View File

@@ -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<Vector2> 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<Vector2> points)
{
this.points = points;
this.LineColor = Color.Black;
}
public Line(List<Vector2> points, float thickness)
{
this.points = points;
this.thickness = thickness;
this.LineColor = Color.Black;
}
public Line(List<Vector2> 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<VertexPositionColorTexture> vertices = new List<VertexPositionColorTexture>();
List<ushort> indices = new List<ushort>();
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<VertexPositionColorTexture>(vertices.ToArray());
this.ibo = new IndexBuffer(device, typeof(ushort), indices.Count,BufferUsage.WriteOnly);
this.ibo.SetData<ushort>(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);
}
}
}

View File

Before

Width:  |  Height:  |  Size: 8.2 KiB

After

Width:  |  Height:  |  Size: 8.2 KiB

114
src/Primitives/Line.cs Normal file
View File

@@ -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<Vector2> 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<Vector2> points)
{
this.points = points;
this.LineColor = Color.Black;
}
public Line(List<Vector2> points, float thickness)
{
this.points = points;
this.thickness = thickness;
this.LineColor = Color.Black;
}
public Line(List<Vector2> 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<VertexPositionColorTexture> vertices = new List<VertexPositionColorTexture>();
List<ushort> indices = new List<ushort>();
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<VertexPositionColorTexture>(vertices.ToArray());
this.ibo = new IndexBuffer(device, typeof(ushort), indices.Count, BufferUsage.WriteOnly);
this.ibo.SetData<ushort>(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);
}
}
}
}

View File

@@ -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<Effect>("line");
lineEffect = Content.Load<Effect>("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)};