-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMeshBatcher.cs
More file actions
335 lines (328 loc) · 11.3 KB
/
MeshBatcher.cs
File metadata and controls
335 lines (328 loc) · 11.3 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
using System;
using System.Collections.Generic;
using System.Text;
using OpenTK;
using OpenTK.Graphics.OpenGL;
namespace ManicDigger
{
public class MeshBatcher
{
[Inject]
public IFrustumCulling frustumculling;
public MeshBatcher()
{
}
private void GenLists()
{
int lists = GL.GenLists(listincrease);
if (lists == 0)
{
throw new Exception();
}
this.lists.Add(lists);
}
public int listincrease = 1024;
private int GetList(int i)
{
return lists[i / listincrease] + i % listincrease;
}
private void ClearLists()
{
if (lists != null)
{
foreach (int l in lists)
{
GL.DeleteLists(l, listincrease);
}
}
}
List<int> lists = new List<int>();
int count = 0;
public void Remove(int p)
{
GetListInfo(p).empty = true;
empty[p] = 0;
}
struct ToAdd
{
public ushort[] indices;
public VertexPositionTexture[] vertices;
public int id;
public bool transparent;
public int texture;
public Vector3 center;
public float radius;
}
Queue<ToAdd> toadd = new Queue<ToAdd>();
//Just saves provided arguments.
//Display lists are created later, in Draw() called from the main thread.
public int Add(ushort[] indices, VertexPositionTexture[] vertices, bool transparent, int texture, Vector3 center, float radius)
{
int id;
lock (toadd)
{
if (empty.Count > 0)
{
id = System.Linq.Enumerable.First(empty.Keys);
empty.Remove(id);
}
else
{
id = count;
count++;
}
toadd.Enqueue(new ToAdd()
{
indices = indices,
vertices = vertices,
id = id,
transparent = transparent,
texture = texture,
center = center,
radius = radius
});
}
return id;
}
private void AllocateLists()
{
while (count >= lists.Count * listincrease)
{
GenLists();
}
while (count >= listinfoCount)
{
Array.Resize(ref listinfo, listinfoCount + listincrease);
for (int i = listinfoCount; i < listinfoCount + listincrease; i++)
{
listinfo[i] = new ListInfo();
}
listinfoCount += listincrease;
}
}
Dictionary<int, int> empty = new Dictionary<int, int>();
float addperframe = 0.5f;
float addcounter = 0;
Vector3 playerpos;
public void Draw(Vector3 playerpos)
{
this.playerpos = playerpos;
AllocateLists();
//Create display lists, which were saved with Add() call
//in another thread.
lock (toadd)
{
//The addcounter prevents slowdown caused by creating
//too many display lists in a single frame.
//It's disabled because it's probably not needed.
addcounter += addperframe;
while (//addcounter >= 1 &&
toadd.Count > 0)
{
addcounter -= 1;
ToAdd t = toadd.Dequeue();
GL.NewList(GetList(t.id), ListMode.Compile);
//We can't bind texture inside display list
//because it doesn't work on Linux and some on graphics cards.
GL.EnableClientState(ArrayCap.TextureCoordArray);
GL.EnableClientState(ArrayCap.VertexArray);
GL.EnableClientState(ArrayCap.ColorArray);
unsafe
{
fixed (VertexPositionTexture* p = t.vertices)
{
GL.VertexPointer(3, VertexPointerType.Float, StrideOfVertices, (IntPtr)(0 + (byte*)p));
GL.TexCoordPointer(2, TexCoordPointerType.Float, StrideOfVertices, (IntPtr)(12 + (byte*)p));
GL.ColorPointer(4, ColorPointerType.UnsignedByte, StrideOfVertices, (IntPtr)(20 + (byte*)p));
GL.DrawElements(BeginMode.Triangles, t.indices.Length, DrawElementsType.UnsignedShort, t.indices);
}
}
GL.DisableClientState(ArrayCap.TextureCoordArray);
GL.DisableClientState(ArrayCap.VertexArray);
GL.DisableClientState(ArrayCap.ColorArray);
GL.EndList();
ListInfo li = GetListInfo(t.id);
li.indicescount = t.indices.Length;
li.center = t.center;
li.radius = t.radius;
li.transparent = t.transparent;
li.empty = false;
li.texture = GetTextureId(t.texture);
}
if (toadd.Count == 0)
{
addcounter = 0;
}
}
UpdateCulling();
//Group display lists by used texture to minimize
//number of GL.BindTexture() calls.
SortListsByTexture();
//Need to first render all solid lists (to fill z-buffer), then transparent.
for (int i = 0; i < texturesCount; i++)
{
if (tocallSolid[i].Count == 0) { continue; }
GL.BindTexture(TextureTarget.Texture2D, glTextures[i]);
GL.CallLists(tocallSolid[i].Count, ListNameType.Int, tocallSolid[i].Lists);
}
GL.Disable(EnableCap.CullFace);//for water.
for (int i = 0; i < texturesCount; i++)
{
if (tocallTransparent[i].Count == 0) { continue; }
GL.BindTexture(TextureTarget.Texture2D, glTextures[i]);
GL.CallLists(tocallTransparent[i].Count, ListNameType.Int, tocallTransparent[i].Lists);
}
GL.Enable(EnableCap.CullFace);
}
//Finds an index in glTextures array.
private int GetTextureId(int glTexture)
{
int id = Array.IndexOf(glTextures, glTexture);
if (id != -1)
{
return id;
}
id = Array.IndexOf(glTextures, 0);
if (id != -1)
{
glTextures[id] = glTexture;
return id;
}
int increase = 10;
Array.Resize(ref glTextures, glTextures.Length + increase);
glTextures[glTextures.Length - increase] = glTexture;
return glTextures.Length - increase;
}
//Maps from our inner texture id to real opengl texture id.
int[] glTextures = new int[10];
ToCall[] tocallSolid;
ToCall[] tocallTransparent;
class ToCall
{
public int[] Lists;
public int Count;
}
//todo dynamic
public int texturesCount = 10;
private void SortListsByTexture()
{
if (tocallSolid == null)
{
tocallSolid = new ToCall[texturesCount];
tocallTransparent = new ToCall[texturesCount];
for (int i = 0; i < texturesCount; i++)
{
tocallSolid[i] = new ToCall();
tocallTransparent[i] = new ToCall();
}
for (int i = 0; i < texturesCount; i++)
{
tocallSolid[i].Lists = new int[MAX_DISPLAY_LISTS];
tocallTransparent[i].Lists = new int[MAX_DISPLAY_LISTS];
}
}
for (int i = 0; i < texturesCount; i++)
{
tocallSolid[i].Count = 0;
tocallTransparent[i].Count = 0;
}
for (int i = 0; i < count; i++)
{
ListInfo li = listinfo[i];
if (!li.render)
{
continue;
}
if (li.empty)
{
continue;
}
if (!li.transparent)
{
ToCall tocall = tocallSolid[li.texture];
tocall.Lists[tocall.Count] = GetList(i);
tocall.Count++;
}
else
{
ToCall tocall = tocallTransparent[li.texture];
tocall.Lists[tocall.Count] = GetList(i);
tocall.Count++;
}
}
}
//Not really needed because display lists perform (at least on some computers)
//their own frustum culling automatically.
private void UpdateCulling()
{
int licount = this.count;
for (int i = 0; i < licount; i++)
{
ListInfo li = listinfo[i];
Vector3 center = li.center;
li.render = frustumculling.SphereInFrustum(center.X, center.Y, center.Z, li.radius);
}
}
public int MAX_DISPLAY_LISTS = 32 * 1024;
int strideofvertices = -1;
int StrideOfVertices
{
get
{
if (strideofvertices == -1) strideofvertices = BlittableValueType.StrideOf(new VertexPositionTexture());
return strideofvertices;
}
}
class ListInfo
{
public bool empty;
public int indicescount;
public Vector3 center;
public float radius;
public bool transparent;
public bool render = true;
public int texture;
}
ListInfo[] listinfo = new ListInfo[0];
int listinfoCount = 0;
private ListInfo GetListInfo(int id)
{
return listinfo[id];
}
public void Clear()
{
ClearLists();
count = 0;
empty.Clear();
toadd.Clear();
listinfo = new ListInfo[0];
listinfoCount = 0;
}
public int TotalTriangleCount
{
get
{
if (listinfo == null)
{
return 0;
}
int sum = 0;
for (int i = 0; i < count; i++)
{
if (!empty.ContainsKey(i))
{
if (i < listinfoCount)
{
ListInfo li = GetListInfo(i);
if (li.render)
{
sum += li.indicescount;
}
}
}
}
return sum / 3;
}
}
}
}