Draw basic trapezoidal skeleton.

This commit is contained in:
Colin McMillen 2020-07-16 11:34:10 -04:00
parent df436f8c32
commit d6addc15e0

View File

@ -1,18 +1,19 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
namespace SemiColinGames {
public sealed class TreeScene : IScene {
const int MAX_LINE_VERTICES = 4;
const int MAX_SEGMENTS = 1000;
const int MAX_VERTICES = MAX_SEGMENTS * 6; // 2 triangles per segment
private readonly Color backgroundColor = Color.SkyBlue;
private readonly GraphicsDevice graphics;
private readonly BasicEffect basicEffect;
private VertexPositionColor[] lineVertices;
private VertexPositionColor[] vertices;
private VertexBuffer vertexBuffer;
public TreeScene(GraphicsDevice graphics) {
@ -21,13 +22,13 @@ namespace SemiColinGames {
basicEffect = new BasicEffect(graphics) {
World = Matrix.CreateTranslation(0, 0, 0),
View = Matrix.CreateLookAt(Vector3.Backward, Vector3.Zero, Vector3.Up),
VertexColorEnabled = true
VertexColorEnabled = true,
Projection = Matrix.CreateOrthographicOffCenter(-1920 / 2, 1920 / 2, -1080 / 4, 1080 * 3 / 4, -1, 1)
};
basicEffect.Projection = Matrix.CreateOrthographicOffCenter(0, 1920, 1080, 0, -1, 1);
lineVertices = new VertexPositionColor[MAX_LINE_VERTICES];
vertices = new VertexPositionColor[MAX_VERTICES];
vertexBuffer = new VertexBuffer(
graphics, typeof(VertexPositionColor), MAX_LINE_VERTICES, BufferUsage.WriteOnly);
graphics, typeof(VertexPositionColor), MAX_VERTICES, BufferUsage.WriteOnly);
}
~TreeScene() {
@ -39,19 +40,53 @@ namespace SemiColinGames {
GC.SuppressFinalize(this);
}
public struct Trapezoid {
public Vector2 p1, p2, p3, p4;
}
public void Draw(bool isRunningSlowly, IWorld iworld, bool paused) {
var skeleton = new List<Vector2> {
new Vector2(0, 0),
new Vector2(0, 100),
new Vector2(10, 200),
new Vector2(30, 300),
new Vector2(60, 400),
new Vector2(100, 500),
};
graphics.Clear(backgroundColor);
lineVertices[0] = new VertexPositionColor(new Vector3(100, 100, 0), Color.Black);
lineVertices[1] = new VertexPositionColor(new Vector3(300, 200, 0), Color.Black);
lineVertices[2] = new VertexPositionColor(new Vector3(200, 200, 0), Color.Black);
lineVertices[3] = new VertexPositionColor(new Vector3(100, 100, 0), Color.Black);
var segments = new List<Trapezoid>();
for (int i = 0; i < skeleton.Count - 1; i++) {
Vector2 low = skeleton[i];
Vector2 high = skeleton[i + 1];
Trapezoid t;
int width = 10 - i;
t.p1 = new Vector2(low.X - width, low.Y);
t.p2 = new Vector2(low.X + width, low.Y);
t.p3 = new Vector2(high.X - width + 4, high.Y);
t.p4 = new Vector2(high.X + width - 4, high.Y);
segments.Add(t);
}
Color color = Color.SaddleBrown;
for (int i = 0; i < segments.Count; i++) {
Trapezoid t = segments[i];
vertices[i * 6] = new VertexPositionColor(new Vector3(t.p1.X, t.p1.Y, 0), color);
vertices[i * 6 + 1] = new VertexPositionColor(new Vector3(t.p2.X, t.p2.Y, 0), color);
vertices[i * 6 + 2] = new VertexPositionColor(new Vector3(t.p3.X, t.p3.Y, 0), color);
vertices[i * 6 + 3] = new VertexPositionColor(new Vector3(t.p2.X, t.p2.Y, 0), color);
vertices[i * 6 + 4] = new VertexPositionColor(new Vector3(t.p3.X, t.p3.Y, 0), color);
vertices[i * 6 + 5] = new VertexPositionColor(new Vector3(t.p4.X, t.p4.Y, 0), color);
}
graphics.SetVertexBuffer(vertexBuffer);
vertexBuffer.SetData(lineVertices);
vertexBuffer.SetData(vertices);
foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes) {
pass.Apply();
graphics.DrawPrimitives(PrimitiveType.LineStrip, 0, 3 /* num lines */);
graphics.DrawPrimitives(PrimitiveType.TriangleList, 0, segments.Count * 2);
}
}
}