forked from Robmaister/SharpNav
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgentCylinder.cs
More file actions
186 lines (143 loc) · 4.4 KB
/
AgentCylinder.cs
File metadata and controls
186 lines (143 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright (c) 2015 Robert Rouhani <robert.rouhani@gmail.com> and other contributors (see CONTRIBUTORS file).
// Licensed under the MIT License - https://raw.github.com/Robmaister/SharpNav/master/LICENSE
using System;
using System.Collections.Generic;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
//Prevents name collision under the Standalone configuration
#if STANDALONE
using Vector3 = OpenTK.Vector3;
using SVector3 = SharpNav.Geometry.Vector3;
#elif OPENTK
using SVector3 = OpenTK.Vector3;
#endif
//Doesn't compile if in an unsupported configuration
#if STANDALONE || OPENTK
namespace SharpNav.Examples
{
public class AgentCylinder
{
private int segments;
private float radius;
private float height;
private int vbo, ibo, numIndices;
public AgentCylinder(int segments, float radius, float height)
{
this.segments = segments;
this.radius = radius;
this.height = height;
this.Color = Color4.LightGray;
Update();
}
public Color4 Color { get; set; }
public int Segments
{
get { return segments; }
set { segments = value; Update(); }
}
public float Radius
{
get { return radius; }
set { radius = value; Update(); }
}
public float Height
{
get { return height; }
set { height = value; Update(); }
}
private void Update()
{
List<Vector3> verts = new List<Vector3>();
List<uint> inds = new List<uint>();
//verts
for (int i = 0; i < 2; i++)
{
for (int j = 0; j <= segments; j++)
{
float theta = ((float)j / (float)segments) * 2 * (float)Math.PI;
float st = (float)Math.Sin(theta), ct = (float)Math.Cos(theta);
verts.Add(new Vector3(radius * st, height * i, radius * ct));
verts.Add(i == 0 ? -Vector3.UnitY : Vector3.UnitY);
}
}
for (int i = 0; i < 2; i++)
{
for (int j = 0; j <= segments; j++)
{
float theta = ((float)j / (float)segments) * 2 * (float)Math.PI;
float st = (float)Math.Sin(theta), ct = (float)Math.Cos(theta);
verts.Add(new Vector3(radius * st, height * i, radius * ct));
verts.Add(new Vector3(st, 0, ct));
}
}
//inds
int start = 0;
//bottom cap
for (int i = 1; i < segments - 1; i++)
{
inds.Add((uint)(start));
inds.Add((uint)(start + i + 1));
inds.Add((uint)(start + i));
}
start = segments + 1;
//top cap
for (int i = 1; i < segments - 1; i++)
{
inds.Add((uint)(start));
inds.Add((uint)(start + i));
inds.Add((uint)(start + i + 1));
}
start += segments + 1;
//edge
for (int i = 0; i <= segments; i++)
{
inds.Add((uint)(start + i));
inds.Add((uint)(start + segments + i + 1));
inds.Add((uint)(start + segments + i));
inds.Add((uint)(start + i));
inds.Add((uint)(start + i + 1));
inds.Add((uint)(start + segments + i + 1));
}
if (vbo == 0)
vbo = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(verts.Count * 3 * 4), verts.ToArray(), BufferUsageHint.StaticDraw);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
if (ibo == 0)
ibo = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ElementArrayBuffer, ibo);
GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(inds.Count * 4), inds.ToArray(), BufferUsageHint.StaticDraw);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
numIndices = inds.Count;
}
public void Draw(Vector3 pos)
{
if (vbo == 0 || ibo == 0)
return;
Matrix4 trans;
Matrix4.CreateTranslation(ref pos, out trans);
GL.EnableClientState(ArrayCap.VertexArray);
GL.EnableClientState(ArrayCap.NormalArray);
GL.Enable(EnableCap.Lighting);
GL.Enable(EnableCap.Light0);
GL.Light(LightName.Light0, LightParameter.Position, new Vector4(0.5f, 1, 0.5f, 0));
GL.PushMatrix();
GL.MultMatrix(ref trans);
GL.Color4(Color);
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
GL.VertexPointer(3, VertexPointerType.Float, 6 * 4, 0);
GL.NormalPointer(NormalPointerType.Float, 6 * 4, 3 * 4);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, ibo);
GL.DrawElements(PrimitiveType.Triangles, numIndices, DrawElementsType.UnsignedInt, 0);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.PopMatrix();
GL.Disable(EnableCap.Light0);
GL.Disable(EnableCap.Lighting);
GL.DisableClientState(ArrayCap.NormalArray);
GL.DisableClientState(ArrayCap.VertexArray);
}
}
}
#endif