-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeometry.cs
125 lines (101 loc) · 4.2 KB
/
Geometry.cs
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
using System;
using System.Collections.Generic;
using OpenTK;
using OpenTK.Graphics.OpenGL;
namespace GLTerrain {
public class Geometry : IDisposable {
public Vector3 Position { get; set; }
public Vector3 Scale { get; set; }
public Quaternion Orientation { get; set; }
public BeginMode DrawMode { get; set; }
private VertexBuffer buffer = null;
private int displayList = 0;
public Geometry(BeginMode drawMode, VertexBuffer buffer) {
this.buffer = buffer;
DrawMode = drawMode;
Position = new Vector3();
Scale = new Vector3(1, 1, 1);
Orientation = Quaternion.Identity;
}
public void Draw() {
if (buffer.IsVBO) {
GL.EnableClientState(ArrayCap.VertexArray);
GL.EnableClientState(ArrayCap.NormalArray);
GL.EnableClientState(ArrayCap.TextureCoordArray);
if (buffer.ColorsID > 0) {
GL.EnableClientState(ArrayCap.ColorArray);
GL.BindBuffer(BufferTarget.ArrayBuffer, buffer.ColorsID);
GL.ColorPointer(4, ColorPointerType.Float, 0, 0);
}
GL.BindBuffer(BufferTarget.ArrayBuffer, buffer.ID);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, buffer.IndicesID);
int stride = BlittableValueType.StrideOf(buffer.Vertices);
GL.TexCoordPointer(2, TexCoordPointerType.Float, stride, 12);
GL.NormalPointer(NormalPointerType.Float, stride, 20);
GL.VertexPointer(3, VertexPointerType.Float, stride, 0);
Matrix4 transform = GetTransform();
GL.PushMatrix();
{
GL.MultMatrix(ref transform);
if (buffer.IndicesSize == 2) {
GL.DrawElements(DrawMode, buffer.NumIndices, DrawElementsType.UnsignedShort, 0);
} else {
GL.DrawElements(DrawMode, buffer.NumIndices, DrawElementsType.UnsignedInt, 0);
}
}
GL.PopMatrix();
GL.DisableClientState(ArrayCap.VertexArray);
GL.DisableClientState(ArrayCap.NormalArray);
GL.DisableClientState(ArrayCap.TextureCoordArray);
if (buffer.ColorsID > 0) {
GL.DisableClientState(ArrayCap.ColorArray);
}
} else {
if (displayList == 0) {
CreateDisplayList(DrawMode);
}
Matrix4 transform = GetTransform();
GL.PushAttrib(AttribMask.CurrentBit);
GL.PushMatrix();
{
GL.Color4(1.0f, 1.0f, 1.0f, 1.0f);
GL.MultMatrix(ref transform);
GL.CallList(displayList);
}
GL.PopMatrix();
GL.PopAttrib();
}
}
private void CreateDisplayList(BeginMode mode) {
if (!buffer.IsValid) {
return;
}
if (displayList > 0) {
GL.DeleteLists(displayList, 1);
}
displayList = GL.GenLists(1);
GL.NewList(displayList, ListMode.Compile);
GL.Begin(mode);
for (int index = 0; index < buffer.Vertices.Length; index++) {
if (buffer.TextureCoords != null) {
GL.TexCoord2(buffer.TextureCoords[index].U, buffer.TextureCoords[index].V);
}
if (buffer.Colors != null) {
GL.Color4(buffer.Colors[index]);
}
GL.Vertex3(buffer.Vertices[index].X, buffer.Vertices[index].Y, buffer.Vertices[index].Z);
}
GL.End();
GL.EndList();
}
public Matrix4 GetTransform() {
return Matrix4.Scale(Scale) * Matrix4.CreateTranslation(Position) * Matrix4.Rotate(Orientation);
}
public void Dispose() {
if (displayList > 0) {
GL.DeleteLists(displayList, 1);
}
buffer.Dispose();
}
}
}