-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRN_SampleInterfaces.pas
404 lines (359 loc) · 9.73 KB
/
RN_SampleInterfaces.pas
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
//
// Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
//
unit RN_SampleInterfaces;
interface
uses Math, OpenGL, StrUtils, SysUtils,
RN_Helper, RN_Recast, RN_DebugDraw;
// These are example implementations of various interfaces used in Recast and Detour.
type
/// Recast build context.
TBuildContext = class(TrcContext)
private
m_startTime: array [TrcTimerLabel] of Int64;
m_accTime: array [TrcTimerLabel] of Integer;
const MAX_MESSAGES = 1000;
var
m_messages: array [0..MAX_MESSAGES-1] of string;
m_messageCount: Integer;
//const TEXT_POOL_SIZE = 8000;
//var
//m_textPool: array [0..TEXT_POOL_SIZE-1] of Char;
//m_textPoolSize: Integer;
protected
/// Virtual functions for custom implementations.
///@
procedure doResetLog(); override;
procedure doLog(category: TrcLogCategory; msg: string); override;
procedure doResetTimers(); override;
procedure doStartTimer(const &label: TrcTimerLabel); override;
procedure doStopTimer(const &label: TrcTimerLabel); override;
function doGetAccumulatedTime(const &label: TrcTimerLabel): Integer; override;
///@
public
/// Dumps the log to stdout.
procedure dumpLog(format: string);
/// Returns number of log messages.
function getLogCount: Integer;
/// Returns log message text.
function getLogText(const i: Integer): string;
end;
/// OpenGL debug draw implementation.
TDebugDrawGL = class(TduDebugDraw)
public
procedure depthMask(state: Boolean); override;
procedure texture(state: Boolean); override;
procedure &begin(prim: TduDebugDrawPrimitives; size: Single = 1.0); override;
procedure vertex(const pos: PSingle; color: Cardinal); override;
procedure vertex(const x,y,z: Single; color: Cardinal); override;
procedure vertex(const pos: PSingle; color: Cardinal; uv: PSingle); override;
procedure vertex(const x,y,z: Single; color: Cardinal; u,v: Single); override;
procedure &end(); override;
end;
{/// stdio file implementation.
TFileIO = class(TduFileIO)
private
FILE* m_fp;
int m_mode;
public
FileIO();
virtual ~FileIO();
bool openForWrite(const char* path);
bool openForRead(const char* path);
virtual bool isWriting() const;
virtual bool isReading() const;
virtual bool write(const void* ptr, const size_t size);
virtual bool read(void* ptr, const size_t size);
end;}
implementation
uses RN_PerfTimer;
////////////////////////////////////////////////////////////////////////////////////////////////////
// Virtual functions for custom implementations.
procedure TBuildContext.doResetLog();
begin
m_messageCount := 0;
//m_textPoolSize := 0;
end;
procedure TBuildContext.doLog(category: TrcLogCategory; msg: string);
const Cat: array [TrcLogCategory] of String = ('','W','E');
begin
if (msg = '') then Exit;
if (m_messageCount >= MAX_MESSAGES) then
Exit;
// Store message
m_messages[m_messageCount] := cat[category] + ' ' + msg;
Inc(m_messageCount);
end;
procedure TBuildContext.doResetTimers();
var i: TrcTimerLabel;
begin
for i := Low(TrcTimerLabel) to High(TrcTimerLabel) do
m_accTime[i] := -1;
end;
procedure TBuildContext.doStartTimer(const &label: TrcTimerLabel);
begin
m_startTime[&label] := getPerfTime();
end;
procedure TBuildContext.doStopTimer(const &label: TrcTimerLabel);
var endTime: Int64; deltaTime: Integer;
begin
endTime := getPerfTime();
deltaTime := Integer(endTime - m_startTime[&label]);
if (m_accTime[&label] = -1) then
m_accTime[&label] := deltaTime
else
Inc(m_accTime[&label], deltaTime);
end;
function TBuildContext.doGetAccumulatedTime(const &label: TrcTimerLabel): Integer;
begin
Result := m_accTime[&label];
end;
procedure TBuildContext.dumpLog(format: string);
begin
{// Print header.
va_list ap;
va_start(ap, format);
vprintf(format, ap);
va_end(ap);
printf("\n");
// Print messages
const int TAB_STOPS[4] := begin 28, 36, 44, 52 end;;
for (int i := 0; i < m_messageCount; ++i)
begin
const char* msg := m_messages[i]+1;
int n := 0;
while ( *msg)
begin
if ( *msg == '\t')
begin
int count := 1;
for (int j := 0; j < 4; ++j)
begin
if (n < TAB_STOPS[j])
begin
count := TAB_STOPS[j] - n;
break;
end;
end;
while (--count)
begin
putchar(' ');
n++;
end;
end;
else
begin
putchar( *msg);
n++;
end;
msg++;
end;
putchar('\n');
end;}
end;
function TBuildContext.getLogCount: Integer;
begin
Result := m_messageCount;
end;
function TBuildContext.getLogText(const i: Integer): string;
begin
Result := m_messages[i];
end;
////////////////////////////////////////////////////////////////////////////////////////////////////
{class GLCheckerTexture
begin
unsigned int m_texId;
public:
GLCheckerTexture() : m_texId(0)
begin
end;
~GLCheckerTexture()
begin
if (m_texId != 0)
glDeleteTextures(1, &m_texId);
end;
void bind()
begin
if (m_texId == 0)
begin
// Create checker pattern.
const unsigned int col0 := duRGBA(215,215,215,255);
const unsigned int col1 := duRGBA(255,255,255,255);
static const int TSIZE := 64;
unsigned int data[TSIZE*TSIZE];
glGenTextures(1, &m_texId);
glBindTexture(GL_TEXTURE_2D, m_texId);
int level := 0;
int size := TSIZE;
while (size > 0)
begin
for (int y := 0; y < size; ++y)
for (int x := 0; x < size; ++x)
data[x+y*size] := (x==0 || y==0) ? col0 : col1;
glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, size,size, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
size /= 2;
level++;
end;
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
end;
else
begin
glBindTexture(GL_TEXTURE_2D, m_texId);
end;
end;
end;;
GLCheckerTexture g_tex;}
procedure TDebugDrawGL.depthMask(state: Boolean);
begin
if state then
glDepthMask(GL_TRUE)
else
glDepthMask(GL_FALSE);
end;
procedure TDebugDrawGL.texture(state: Boolean);
begin
if (state) then
begin
glEnable(GL_TEXTURE_2D);
//todo: g_tex.bind();
end
else
begin
glDisable(GL_TEXTURE_2D);
end;
end;
procedure TDebugDrawGL.&begin(prim: TduDebugDrawPrimitives; size: Single);
begin
case prim of
DU_DRAW_POINTS: begin
glPointSize(size);
glBegin(GL_POINTS);
end;
DU_DRAW_LINES: begin
glLineWidth(size);
glBegin(GL_LINES);
end;
DU_DRAW_TRIS:
glBegin(GL_TRIANGLES);
DU_DRAW_QUADS:
glBegin(GL_QUADS);
end;
end;
procedure TDebugDrawGL.vertex(const pos: PSingle; color: Cardinal);
begin
glColor4ubv(PGLubyte(@color));
{$IFDEF SWAP_XZY}
{$POINTERMATH ON}
glVertex3f(pos^, (pos+2)^, (pos+1)^);
{$POINTERMATH OFF}
{$ENDIF}
{$IFNDEF SWAP_XZY}
glVertex3fv(PGLfloat(pos));
{$ENDIF}
end;
procedure TDebugDrawGL.vertex(const x,y,z: Single; color: Cardinal);
begin
glColor4ubv(PGLubyte(@color));
{$IFDEF SWAP_XZY}
{$POINTERMATH ON}
glVertex3f(x,z,y);
{$POINTERMATH OFF}
{$ENDIF}
{$IFNDEF SWAP_XZY}
glVertex3f(x,y,z);
{$ENDIF}
end;
procedure TDebugDrawGL.vertex(const pos: PSingle; color: Cardinal; uv: PSingle);
begin
glColor4ubv(PGLubyte(@color));
glTexCoord2fv(PGLfloat(uv));
{$IFDEF SWAP_XZY}
{$POINTERMATH ON}
glVertex3f(pos^, (pos+2)^, (pos+1)^);
{$POINTERMATH OFF}
{$ENDIF}
{$IFNDEF SWAP_XZY}
glVertex3fv(PGLfloat(pos));
{$ENDIF}
end;
procedure TDebugDrawGL.vertex(const x,y,z: Single; color: Cardinal; u,v: Single);
begin
glColor4ubv(PGLubyte(@color));
glTexCoord2f(u,v);
{$IFDEF SWAP_XZY}
{$POINTERMATH ON}
glVertex3f(x,z,y);
{$POINTERMATH OFF}
{$ENDIF}
{$IFNDEF SWAP_XZY}
glVertex3f(x,y,z);
{$ENDIF}
end;
procedure TDebugDrawGL.&end();
begin
glEnd();
glLineWidth(1.0);
glPointSize(1.0);
end;
{////////////////////////////////////////////////////////////////////////////////////////////////////
FileIO::FileIO() :
m_fp(0),
m_mode(-1)
begin
end;
FileIO::~FileIO()
begin
if (m_fp) fclose(m_fp);
end;
bool FileIO::openForWrite(const char* path)
begin
if (m_fp) return false;
m_fp := fopen(path, "wb");
if (!m_fp) return false;
m_mode := 1;
return true;
end;
bool FileIO::openForRead(const char* path)
begin
if (m_fp) return false;
m_fp := fopen(path, "rb");
if (!m_fp) return false;
m_mode := 2;
return true;
end;
bool FileIO::isWriting() const
begin
return m_mode == 1;
end;
bool FileIO::isReading() const
begin
return m_mode == 2;
end;
bool FileIO::write(const void* ptr, const size_t size)
begin
if (!m_fp || m_mode != 1) return false;
fwrite(ptr, size, 1, m_fp);
return true;
end;
bool FileIO::read(void* ptr, const size_t size)
begin
if (!m_fp || m_mode != 2) return false;
size_t readLen := fread(ptr, size, 1, m_fp);
return readLen == 1;
end;}
end.