use DrawIndexedPrimitives; fixes #41

GitOrigin-RevId: 6cc7429d0b69ace6da4f965af6f70fff2f1e139b
This commit is contained in:
Colin McMillen 2020-02-12 15:54:28 -05:00
parent f01efcde01
commit 23278334b0

View File

@ -5,25 +5,29 @@ using System;
namespace SemiColinGames {
class LinesOfSight {
const int numConePoints = 60;
VertexPositionColor[] conePoints = new VertexPositionColor[numConePoints];
const int numEdgeVertices = 60;
// coneVertices[0] is the eye position; the rest are the edge vertices.
VertexPositionColor[] coneVertices = new VertexPositionColor[numEdgeVertices + 1];
Color color = Color.FromNonPremultiplied(new Vector4(0, 0, 1, 0.6f));
VertexPositionColor[] vertices = new VertexPositionColor[numConePoints * 3];
// The number of total triangles drawn is one less than the number of edge points.
int[] indices = new int[(numEdgeVertices - 1) * 3];
VertexBuffer vertexBuffer;
IndexBuffer indexBuffer;
public LinesOfSight(GraphicsDevice graphics) {
vertexBuffer = new VertexBuffer(
graphics, typeof(VertexPositionColor), numConePoints * 3, BufferUsage.WriteOnly);
graphics, typeof(VertexPositionColor), numEdgeVertices * 3, BufferUsage.WriteOnly);
indexBuffer = new IndexBuffer(
graphics, typeof(int), indices.Length, BufferUsage.WriteOnly);
}
public void Update(Player player, AABB[] collisionTargets) {
// TODO: DrawIndexedPrimitives
Vector2 eyePos = player.EyePosition;
float visionRange = 150;
float visionRangeSq = visionRange * visionRange;
float fov = FMath.DegToRad(120);
float fovStep = fov / (numConePoints - 1);
float fovStep = fov / (numEdgeVertices - 1);
Vector2 ray = new Vector2(visionRange * player.GetFacing, 0);
if (player.GetPose == Player.Pose.Stretching) {
@ -33,7 +37,8 @@ namespace SemiColinGames {
ray = ray.Rotate(player.GetFacing * FMath.DegToRad(30));
}
for (int i = 0; i < conePoints.Length; i++) {
coneVertices[0] = new VertexPositionColor(new Vector3(player.EyePosition, 0), color);
for (int i = 0; i < numEdgeVertices; i++) {
float angle = -fov / 2 + fovStep * i;
Vector2 rotated = ray.Rotate(angle);
Vector2 closestHit = Vector2.Add(eyePos, rotated);
@ -61,28 +66,27 @@ namespace SemiColinGames {
float tint = 0.6f - hitTime / 2;
Color tinted = Color.FromNonPremultiplied(new Vector4(0, 0, 1, tint));
conePoints[i] = new VertexPositionColor(new Vector3(closestHit, 0), tinted);
coneVertices[i + 1] = new VertexPositionColor(new Vector3(closestHit, 0), tinted);
}
}
public void Draw(Player player, AABB[] collisionTargets, GraphicsDevice graphics,
BasicEffect lightingEffect) {
VertexPositionColor eyeVertex = new VertexPositionColor(
new Vector3(player.EyePosition, 0), color);
for (int i = 0; i < numConePoints - 1; i++) {
vertices[i * 3] = eyeVertex;
vertices[i * 3 + 1] = conePoints[i];
vertices[i * 3 + 2] = conePoints[i + 1];
for (int i = 0; i < numEdgeVertices - 1; i++) {
indices[i * 3] = 0;
indices[i * 3 + 1] = i + 1;
indices[i * 3 + 2] = i + 2;
}
vertexBuffer.SetData<VertexPositionColor>(vertices);
vertexBuffer.SetData(coneVertices);
indexBuffer.SetData(indices);
graphics.SetVertexBuffer(vertexBuffer);
graphics.Indices = indexBuffer;
foreach (EffectPass pass in lightingEffect.CurrentTechnique.Passes) {
pass.Apply();
graphics.DrawPrimitives(PrimitiveType.TriangleList, 0, vertices.Length / 3);
graphics.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, coneVertices.Length, 0, indices.Length / 3);
}
}
}