-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrenderer_ogl1.cpp
More file actions
729 lines (633 loc) · 18.4 KB
/
renderer_ogl1.cpp
File metadata and controls
729 lines (633 loc) · 18.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
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
// wkbre - WK (Battles) recreated game engine
// Copyright (C) 2015-2016 Adrien Geets
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "global.h"
#include <gl/gl.h>
#include <gl/glu.h>
#define glprocvalid(x) ( (((scpuint)(x)) < -1) || (((scpuint)(x)) > 3) )
#ifndef GL_BGRA_EXT
#define GL_BGRA_EXT 0x80E1
#endif
typedef BOOL (APIENTRY *gli_wglSwapIntervalEXT)(int n);
typedef int (APIENTRY *gli_wglGetSwapIntervalEXT)();
#ifndef DONT_USE_OWN_OPENGL_DEFINITIONS
#define GLsizeiptrARB ucpuint
#define GL_ARRAY_BUFFER_ARB 0x8892
#define GL_ELEMENT_ARRAY_BUFFER_ARB 0x8893
#define GL_STREAM_DRAW_ARB 0x88E0
#define GL_STREAM_READ_ARB 0x88E1
#define GL_STREAM_COPY_ARB 0x88E2
#define GL_STATIC_DRAW_ARB 0x88E4
#define GL_STATIC_READ_ARB 0x88E5
#define GL_STATIC_COPY_ARB 0x88E6
#define GL_DYNAMIC_DRAW_ARB 0x88E8
#define GL_DYNAMIC_READ_ARB 0x88E9
#define GL_DYNAMIC_COPY_ARB 0x88EA
#define GL_READ_ONLY_ARB 0x88B8
#define GL_WRITE_ONLY_ARB 0x88B9
#define GL_READ_WRITE_ARB 0x88BA
#endif
typedef void (APIENTRY *gli_glBindBufferARB)(GLenum target, GLuint buffer);
typedef void (APIENTRY *gli_glDeleteBuffersARB)(GLsizei n, /*const*/ GLuint *buffers);
typedef void (APIENTRY *gli_glGenBuffersARB)(GLsizei n, GLuint *buffers);
typedef void (APIENTRY *gli_glBufferDataARB)(GLenum target, GLsizeiptrARB size, /*const*/ void *data, GLenum usage);
typedef void* (APIENTRY *gli_glMapBufferARB)(GLenum target, GLenum access);
typedef GLboolean (APIENTRY *gli_glUnmapBufferARB)(GLenum target);
gli_glBindBufferARB glBindBufferARB;
gli_glDeleteBuffersARB glDeleteBuffersARB;
gli_glGenBuffersARB glGenBuffersARB;
gli_glBufferDataARB glBufferDataARB;
gli_glMapBufferARB glMapBufferARB;
gli_glUnmapBufferARB glUnmapBufferARB;
HDC whdc; HGLRC glrc;
bool rglUseBufferObjects = 0;
void OGL1Quit()
{
if(fullscreen)
ChangeDisplaySettings(NULL, 0);
}
#define BGRA_TO_RGBA(x) ( (((x)&0xFF)<<16) | (((x)&0xFF0000)>>16) | ((x)&0xFF00FF00) )
struct RBatchOGL1 : public RBatch
{
batchVertex *vbuf; ushort *ibuf;
~RBatchOGL1() {delete [] vbuf; delete [] ibuf;}
void begin() {}
void end() {}
void next(uint nverts, uint nindis, batchVertex **vpnt, ushort **ipnt, uint *fi)
{
if(nverts > maxverts) ferr("Too many vertices to fit in the batch.");
if(nindis > maxindis) ferr("Too many indices to fit in the batch.");
if((curverts + nverts > maxverts) || (curindis + nindis > maxindis))
flush();
*vpnt = vbuf + curverts;
*ipnt = ibuf + curindis;
*fi = curverts;
curverts += nverts; curindis += nindis;
}
void flush()
{
if(!curverts) return;
//for(int i = 0; i < curverts; i++)
// vbuf[i].color = BGRA_TO_RGBA(vbuf[i].color);
glVertexPointer(3, GL_FLOAT, sizeof(batchVertex), &vbuf->x);
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(batchVertex), &vbuf->color);
glTexCoordPointer(2, GL_FLOAT, sizeof(batchVertex), &vbuf->u);
glDrawElements(GL_TRIANGLES, curindis, GL_UNSIGNED_SHORT, ibuf);
curverts = curindis = 0;
}
};
void chkglerr()
{/*
GLenum e = glGetError();
if(e != GL_NO_ERROR)
ferr("OpenGL error %04X.", e);*/
}
struct RBatchOGL1_BO : public RBatch
{
GLuint vbuf, ibuf;
batchVertex *vlock;
ushort *ilock;
bool locked;
//DynListEntry<RBatchOGL1_BO*> *rble;
~RBatchOGL1_BO()
{
/* glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbuf);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, ibuf);
*/
if(locked) unlock();
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
glDeleteBuffersARB(1, &vbuf);
glDeleteBuffersARB(1, &ibuf);
//rblist.remove(rble);
}
void lock()
{
/* glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbuf);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, ibuf);
*/
glBufferDataARB(GL_ARRAY_BUFFER_ARB, maxverts * sizeof(batchVertex), NULL, GL_STREAM_DRAW_ARB);
glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, maxindis * 2, NULL, GL_STREAM_DRAW_ARB);
vlock = (batchVertex*)glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB); chkglerr();
ilock = (ushort*)glMapBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB); chkglerr();
if(!vlock) ferr("glbo vlock is 0");
if(!ilock) ferr("glbo ilock is 0");
locked = 1;
}
void unlock()
{
glUnmapBufferARB(GL_ARRAY_BUFFER_ARB); chkglerr();
glUnmapBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB); chkglerr();
locked = 0;
}
void begin()
{
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbuf);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, ibuf);
}
void end()
{
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
}
void next(uint nverts, uint nindis, batchVertex **vpnt, ushort **ipnt, uint *fi)
{
/* glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbuf);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, ibuf);
*/
if(nverts > maxverts) ferr("Too many vertices to fit in the batch.");
if(nindis > maxindis) ferr("Too many indices to fit in the batch.");
if((curverts + nverts > maxverts) || (curindis + nindis > maxindis))
flush();
if(!locked) lock();
*vpnt = vlock + curverts;
*ipnt = ilock + curindis;
*fi = curverts;
curverts += nverts; curindis += nindis;
}
void flush()
{
/* glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbuf);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, ibuf);
*/
if(locked) unlock();
if(!curverts) goto flshend;
glVertexPointer(3, GL_FLOAT, sizeof(batchVertex), &((batchVertex*)0)->x);
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(batchVertex), &((batchVertex*)0)->color);
glTexCoordPointer(2, GL_FLOAT, sizeof(batchVertex), &((batchVertex*)0)->u);
glDrawElements(GL_TRIANGLES, curindis, GL_UNSIGNED_SHORT, 0);
flshend: curverts = curindis = 0;
/* glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
*/
}
};
struct RVertexBufferOGL1 : public RVertexBuffer
{
batchVertex *m;
~RVertexBufferOGL1() {delete [] m;}
batchVertex *lock() {return m;}
void unlock()
{
for(int i = 0; i < size; i++)
m[i].color = BGRA_TO_RGBA(m[i].color);
}
};
struct RIndexBufferOGL1 : public RIndexBuffer
{
ushort *m;
~RIndexBufferOGL1() {delete [] m;}
ushort *lock() {return m;}
void unlock() {}
};
struct OGL1Renderer : public IRenderer
{
batchVertex *curvtxmem; ushort *curidxmem;
void InitGL()
{
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glEnable(GL_TEXTURE_2D);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
}
void ResizeGL()
{
glViewport(0, 0, scrw, scrh);
}
void Init()
{
whdc = GetDC(hWindow);
// Set the pixel format
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR)); // Be sure that pfd is filled with 0.
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 32;
pfd.dwLayerMask = PFD_MAIN_PLANE;
int i = ChoosePixelFormat(whdc, &pfd);
SetPixelFormat(whdc, i, &pfd);
glrc = wglCreateContext(whdc);
wglMakeCurrent(whdc, glrc);
//char *glexts = (char*)glGetString(GL_EXTENSIONS);
//printf("%s\n", glexts);
if(fullscreen)
{
DEVMODE dm;
memset(&dm, 0, sizeof(dm));
dm.dmSize = sizeof(dm);
dm.dmPelsWidth = scrw;
dm.dmPelsHeight = scrh;
//dm.dmBitsPerPel = 32;
dm.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT; // | DM_BITSPERPEL;
ChangeDisplaySettings(&dm, CDS_FULLSCREEN);
}
// Set VSYNC if enabled
if(VSYNCenabled)
{
gli_wglSwapIntervalEXT wglSwapIntervalEXT = (gli_wglSwapIntervalEXT)wglGetProcAddress("wglSwapIntervalEXT");
//gli_wglGetSwapIntervalEXT wglGetSwapIntervalEXT = (gli_wglGetSwapIntervalEXT)wglGetProcAddress("wglGetSwapIntervalEXT");
if(glprocvalid(wglSwapIntervalEXT))
{
//__asm int 3
//printf(":)\n");
if(!wglSwapIntervalEXT(1))
printf("wglSwapIntervalEXT returned FALSE.\n");
//if(wglGetSwapIntervalEXT)
// printf("wglGetSwapIntervalEXT returns %i.\n", wglGetSwapIntervalEXT());
}
else printf("wglSwapIntervalEXT unsupported.\n");
}
if(rglUseBufferObjects)
{
glBindBufferARB = (gli_glBindBufferARB)wglGetProcAddress("glBindBufferARB");
glDeleteBuffersARB = (gli_glDeleteBuffersARB)wglGetProcAddress("glDeleteBuffersARB");
glGenBuffersARB = (gli_glGenBuffersARB)wglGetProcAddress("glGenBuffersARB");
glBufferDataARB = (gli_glBufferDataARB)wglGetProcAddress("glBufferDataARB");
glMapBufferARB = (gli_glMapBufferARB)wglGetProcAddress("glMapBufferARB");
glUnmapBufferARB = (gli_glUnmapBufferARB)wglGetProcAddress("glUnmapBufferARB");
if(!glBindBufferARB || !glDeleteBuffersARB || !glGenBuffersARB ||
!glBufferDataARB || !glMapBufferARB || !glUnmapBufferARB)
ferr("GL_ARB_vertex_buffer_object functions not found.");
}
atexit(OGL1Quit);
InitGL(); ResizeGL();
}
void Reset()
{
ResizeGL();
}
void BeginDrawing()
{
uint c = mapfogcolor;
glClearColor(((c>>16)&255)/255.0f, ((c>>8)&255)/255.0f, (c&255)/255.0f, ((c>>24)&255)/255.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void EndDrawing()
{
SwapBuffers(whdc); drawframes++;
//glFinish();
}
texture CreateTexture(Bitmap *bm, int mipmaps)
{
Bitmap *c = bm;
if(bm->form != BMFORMAT_B8G8R8A8)
c = ConvertBitmapToB8G8R8A8(bm);
GLuint gltex;
glGenTextures(1, &gltex);
glBindTexture(GL_TEXTURE_2D, gltex);
if(mipmaps != 1)
{
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, c->w, c->h, GL_BGRA_EXT, GL_UNSIGNED_BYTE, c->pix);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_NEAREST);
}
else
{
glTexImage2D(GL_TEXTURE_2D, 0, 4, c->w, c->h, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, c->pix);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
if(bm->form != BMFORMAT_B8G8R8A8)
FreeBitmap(c);
return (texture)gltex;
}
void FreeTexture(texture t)
{
GLuint gltex = (GLuint)t;
glDeleteTextures(1, &gltex);
}
void SetTransformMatrix(Matrix *m)
{
glLoadMatrixf(m->v);
}
void SetTexture(uint x, texture t)
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, (GLuint)t);
}
void NoTexture(uint x)
{
glDisable(GL_TEXTURE_2D);
}
void SetFog()
{
glEnable(GL_FOG);
float fc[4]; int c = mapfogcolor;
fc[0] = ((c>>16)&255)/255.0f;
fc[1] = ((c>> 8)&255)/255.0f;
fc[2] = ((c )&255)/255.0f;
fc[3] = ((c>>24)&255)/255.0f;
//fc[0] = fc[1] = fc[2] = fc[3] = 0;
glFogi(GL_FOG_MODE, GL_LINEAR);
glFogf(GL_FOG_START, farzvalue/2);
glFogf(GL_FOG_END, farzvalue-8);
glFogfv(GL_FOG_COLOR, fc);
}
void DisableFog()
{
glDisable(GL_FOG);
}
void EnableAlphaTest()
{
glEnable(GL_ALPHA_TEST);
}
void DisableAlphaTest()
{
glDisable(GL_ALPHA_TEST);
}
void EnableColorBlend()
{
;
}
void DisableColorBlend()
{
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}
void SetBlendColor(int c)
{
glColor4ub((c>>16)&255, (c>>8)&255, c&255, (c>>24)&255);
}
void EnableAlphaBlend()
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
void DisableAlphaBlend()
{
glDisable(GL_BLEND);
}
void InitRectDrawing()
{
glDisable(GL_ALPHA_TEST);
glDisable(GL_CULL_FACE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_DEPTH_TEST);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Matrix m; CreateZeroMatrix(&m);
m._11 = 2.0f / scrw;
m._22 = -2.0f / scrh;
m._41 = -1;
m._42 = 1;
m._44 = 1;
glLoadMatrixf(m.v);
}
void DrawRect(int x, int y, int w, int h, int c, float u, float v, float o, float p)
{
glColor4ub((c>>16)&255, (c>>8)&255, c&255, (c>>24)&255);
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2f(u , v ); glVertex3f(x , y , 0.5f);
glTexCoord2f(u+o, v ); glVertex3f(x+w, y , 0.5f);
glTexCoord2f(u , v+p); glVertex3f(x , y+h, 0.5f);
glTexCoord2f(u+o, v+p); glVertex3f(x+w, y+h, 0.5f);
glEnd();
}
void DrawGradientRect(int x, int y, int w, int h, int c0, int c1, int c2, int c3)
{
glBegin(GL_TRIANGLE_STRIP);
glColor4ub((c0>>16)&255, (c0>>8)&255, c0&255, (c0>>24)&255);
glTexCoord2f(0, 0); glVertex3f(x , y , 0.5f);
glColor4ub((c1>>16)&255, (c1>>8)&255, c1&255, (c1>>24)&255);
glTexCoord2f(1, 0); glVertex3f(x+w, y , 0.5f);
glColor4ub((c2>>16)&255, (c2>>8)&255, c2&255, (c2>>24)&255);
glTexCoord2f(0, 1); glVertex3f(x , y+h, 0.5f);
glColor4ub((c3>>16)&255, (c3>>8)&255, c3&255, (c3>>24)&255);
glTexCoord2f(1, 1); glVertex3f(x+w, y+h, 0.5f);
glEnd();
}
void DrawFrame(int x, int y, int w, int h, int c)
{
glColor4ub((c>>16)&255, (c>>8)&255, c&255, (c>>24)&255);
glBegin(GL_LINE_STRIP);
glVertex3f(x , y , 0.5f);
glVertex3f(x+w, y , 0.5f);
glVertex3f(x+w, y+h, 0.5f);
glVertex3f(x , y+h, 0.5f);
glVertex3f(x , y , 0.5f);
glEnd();
}
void CreateMapPart(MapPart *p, int x, int y, int w, int h)
{
p->glverts = new float[(h+1)*(w+1)*5];
float *vp = p->glverts;
for(int b = 0; b < h+1; b++)
for(int a = 0; a < w+1; a++)
{
*(vp++) = a+x;
*(vp++) = himap[(b+y)*(mapwidth+1)+(a+x)];
*(vp++) = b+y;
*(vp++) = a+x; *(vp++) = b+y;
}
p->glindices = new GLushort[(2*(w+1)+1)*h];
GLushort *os = p->glindices;
for(int cy = 0; cy < h; cy++)
{
if(!(cy&1))
{
for(int cx = 0; cx < w+1; cx++)
{
*(os++) = cy*(w+1)+cx;
*(os++) = (cy+1)*(w+1)+cx;
if(cx == w) *(os++) = (cy+1)*(w+1)+cx;
}
} else {
for(int cx = w+1-1; cx >= 0; cx--)
{
*(os++) = cy*(w+1)+cx;
*(os++) = (cy+1)*(w+1)+cx;
if(cx == 0) *(os++) = (cy+1)*(w+1)+cx;
}
}
}
}
void FreeMapPart(MapPart *p)
{
delete [] p->glverts; delete [] p->glindices;
}
void BeginMapDrawing()
{
glDisable(GL_ALPHA_TEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
}
void DrawPart(MapPart *p)
{
int x = p->x, y = p->y, w = p->w, h = p->h;
glVertexPointer(3, GL_FLOAT, 5*4, p->glverts);
glTexCoordPointer(2, GL_FLOAT, 5*4, &(p->glverts[3]));
glDrawElements(GL_TRIANGLE_STRIP, (2*(w+1)+1)*h, GL_UNSIGNED_SHORT, p->glindices);
}
void CreateMesh(Mesh *m)
{
;
}
void BeginMeshDrawing()
{
glEnable(GL_TEXTURE_2D);
glAlphaFunc(GL_GEQUAL, 240.0f/255.0f);
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); //GL_LINEAR_MIPMAP_NEAREST);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); //GL_LINEAR);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
}
void DrawMesh(Mesh *m, int iwtcolor)
{
glVertexPointer(3, GL_FLOAT, 3*4, m->mverts.gb.memory);
glTexCoordPointer(2, GL_FLOAT, 2*4, m->muvlist[iwtcolor]->gb.memory);
for(int g = 0; g < m->ngrp; g++)
{
if(m->lstmatflags[g]) glEnable(GL_ALPHA_TEST);
else glDisable(GL_ALPHA_TEST);
glBindTexture(GL_TEXTURE_2D, (GLuint)m->lstmattex[g]);
glDrawElements(GL_TRIANGLES, m->mstartix[g+1]-m->mstartix[g], GL_UNSIGNED_SHORT, ((short*)m->mindices.gb.memory) + m->mstartix[g]);
}
}
RBatch *CreateBatch(int mv, int mi)
{
//ferr("Batch unsupported in OpenGL driver.");
if(rglUseBufferObjects)
{
RBatchOGL1_BO *b = new RBatchOGL1_BO;
b->maxverts = mv; b->maxindis = mi;
b->curverts = b->curindis = 0;
glGenBuffersARB(1, &b->vbuf);
glGenBuffersARB(1, &b->ibuf);
//glBindBufferARB(GL_ARRAY_BUFFER_ARB, b->vbuf);
//glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, b->ibuf);
//glBufferDataARB(GL_ARRAY_BUFFER_ARB, mv * sizeof(batchVertex), NULL, GL_DYNAMIC_DRAW_ARB);
//glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, mi * 2, NULL, GL_DYNAMIC_DRAW_ARB);
//glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
//glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
b->locked = 0;
return b;
}
else
{
RBatchOGL1 *b = new RBatchOGL1;
b->maxverts = mv; b->maxindis = mi;
b->curverts = b->curindis = 0;
b->vbuf = new batchVertex[mv];
b->ibuf = new ushort[mi];
return b;
}
}
void BeginBatchDrawing()
{
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
//glColor4ub(255, 255, 255, 255);
}
RVertexBuffer *CreateVertexBuffer(int nv)
{
RVertexBufferOGL1 *r = new RVertexBufferOGL1;
r->size = nv;
r->m = new batchVertex[nv];
return r;
}
RIndexBuffer *CreateIndexBuffer(int ni)
{
RIndexBufferOGL1 *r = new RIndexBufferOGL1;
r->size = ni;
r->m = new ushort[ni];
return r;
}
void SetVertexBuffer(RVertexBuffer *_rv)
{
curvtxmem = ((RVertexBufferOGL1*)_rv)->m;
}
void SetIndexBuffer(RIndexBuffer *_ri)
{
curidxmem = ((RIndexBufferOGL1*)_ri)->m;
}
void DrawBuffer(int first, int count)
{
glVertexPointer(3, GL_FLOAT, sizeof(batchVertex), &curvtxmem->x);
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(batchVertex), &curvtxmem->color);
glTexCoordPointer(2, GL_FLOAT, sizeof(batchVertex), &curvtxmem->u);
glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_SHORT, curidxmem + first);
}
void EnableScissor()
{
glEnable(GL_SCISSOR_TEST);
}
void DisableScissor()
{
glDisable(GL_SCISSOR_TEST);
}
void SetScissorRect(int x, int y, int w, int h)
{
glScissor(x, scrh-1-(y+h), w, h);
}
void InitImGuiDrawing()
{
glDisable(GL_ALPHA_TEST);
glDisable(GL_CULL_FACE);
glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Matrix m; CreateZeroMatrix(&m);
m._11 = 2.0f / scrw;
m._22 = -2.0f / scrh;
m._41 = -1;
m._42 = 1;
m._44 = 1;
glLoadMatrixf(m.v);
}
void BeginLakeDrawing()
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
int ConvertColor(int c)
{
return BGRA_TO_RGBA(c);
}
void UpdateTexture(texture t, Bitmap *bmp)
{
glBindTexture(GL_TEXTURE_2D, (GLuint)t);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bmp->w, bmp->h, GL_BGRA_EXT, GL_UNSIGNED_BYTE, bmp->pix);
}
void EnableDepth()
{
glDepthMask(GL_TRUE);
}
void DisableDepth()
{
glDepthMask(GL_FALSE);
}
};
IRenderer *CreateOGL1Renderer() {return new OGL1Renderer;}