forked from REDPOWAR/glDoom
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gl_video.c
480 lines (396 loc) · 12.9 KB
/
gl_video.c
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
// Emacs style mode select -*- C++ -*-
//-----------------------------------------------------------------------------
//
// $Id:$
//
// Copyright (C) 1993-1996 by id Software, Inc.
//
// This source is available for distribution and/or modification
// only under the terms of the DOOM Source Code License as
// published by id Software. All rights reserved.
//
// The source is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// FITNESS FOR A PARTICULAR PURPOSE. See the DOOM Source Code License
// for more details.
//
// $Log:$
//
// DESCRIPTION:
// DOOM graphics stuff for X11, UNIX.
//
//-----------------------------------------------------------------------------
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <wglext.h>
#include <stdlib.h>
#include <math.h>
#include "doomtype.h"
#include "gconsole.h"
#include "sys_win.h"
#include "gl_video.h"
#include "win_inpt.h"
#include "win_video.h"
#include "doomstat.h"
#include "i_system.h"
#include "v_video.h"
#include "m_argv.h"
#include "d_main.h"
#include "w_wad.h"
#include "z_zone.h"
#include "doomdef.h"
#include "doomlib.h"
// Dynamic light map base pattern
void GeneratePointPattern(void);
unsigned char dlm[16*16];
int MonoLightMap, WhiteLightMap, WhiteColorMap;
dboolean grabMouse;
static int multiply = 1;
// print to log file...
void lfprintf(char *message, ... );
// Game stuff
extern video_t video;
extern int starttime;
extern int vsync;
// OpenGL renderer stuff
HGLRC hRC = 0; // A global rendering context
HDC hGDC = 0; // A global device context
char *GL_Vendor; // The company responsible for this GL implementation
char *GL_Renderer; // The name of the type of renderer
char *GL_Version; // The version and/or release number
int GL_EXT_Count; // How many extensions are there?
char *GL_EXT_List[GL_MAX_EXT]; // Here's a list of their names
dboolean GL_3Dlabs = false;
dboolean software = false;
glmode_t glmode;
double glFovY; // Rendering field of view
float SetBack; // 3D setback for 2D displays
float glLeft, glTop, glRight, glBottom, glAspect;
// Windows stuff
extern int iCurrMode;
extern DEVMODE *pModeList;
extern HDC hGDC;
extern windata_t WinData;
void ShutdownOpenGL(void)
{
wglMakeCurrent(NULL, NULL);
wglDeleteContext( hRC );
wglGetSwapIntervalEXT();
}
void I_ShutdownGraphics(void)
{
ShutdownOpenGL();
ReleaseDC(WinData.hWnd, hGDC);
ResetVideoMode();
}
void I_Start2DFrame()
{
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho(0, video.width, 0, video.height, 0.5, 10.0);
glViewport( 0, 0, video.width, video.height);
glTranslatef( 0.0f, 0.0f, -1.0f );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef( 0.0f, 0.0f, 0.0f );
}
void I_Start3DFrame()
{
glFovY = video.fovy;
glAspect = (float)video.width / (float)video.height;
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective((double)glFovY, (double)glAspect, (double)video.nearclip, (double)video.farclip );
glViewport( 0, 0, video.width, video.height);
glTranslatef( 0.0f, 0.0f, 2.0f );
// Define the modelview transformation.
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef( 0.0f, 0.0f, 0.0f );
SetBack = -120.0f / tanf(DEG2RAD(glFovY * 0.5f));
SetBack -= 2.0f;
glTop = 120.0f;
glBottom = -glTop;
glRight = glTop * glAspect;
glLeft = -glRight;
}
//
// I_StartFrame
//
void I_StartFrame (void)
{
// er?
// Will put the OpenGL frame start code in here...
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
I_Start3DFrame();
glmode = gl_3d;
}
//
// I_StartTic
//
void I_StartTic (void)
{
// If the mouse has been captured (windowed mode)
// return the mouse to the center of the screen here
I_GetEvent();
}
//
// I_UpdateNoBlit
//
void I_UpdateNoBlit (void)
{
// what is this?
}
void GL_WriteText(int x, int y, char *msg);
extern int loadtime;
int framecnt = 0;
void I_Finish3D()
{
glPopMatrix();
}
//
// I_FinishUpdate
//
void I_FinishUpdate(void)
{
static DWORD lasttime = 0;
DWORD now, ms;
float fps;
char fps_str[16];
if (devparm) // Draw the frame rate on the screen
{ // which will slow it down...
now = timeGetTime();
ms = now - lasttime;
if ((ms > 0) && (ms < 1000))
{
fps = 1000.0f/(float)ms;
sprintf(fps_str, "%.1f FPS", fps);
GL_WriteText(260, 155, fps_str);
}
lasttime = now;
}
if (glmode == gl_3d)
{
//I_Finish3D();
//glPopMatrix();
}
else
{
glOrtho(0, video.width, 0, video.height, 0.5, 10.0);
glViewport( 0, 0, video.width, video.height);
glTranslatef( 0.0f, 0.0f, -1.0f );
}
// Swap the rendering buffers...
SwapBuffers(hGDC);
}
//
// I_ReadScreen
//
void I_ReadScreen (byte* scr)
{
memcpy (scr, screens[0], SCREENWIDTH*SCREENHEIGHT);
}
typedef struct
{
unsigned char r;
unsigned char g;
unsigned char b;
}MY_PAL;
MY_PAL gamepal[256];
extern MY_PAL statpal[256];
void CreateGLPalette()
{
MY_PAL *buffer;
buffer = (MY_PAL *)W_CacheLumpName("PLAYPAL", PU_CACHE);
memcpy(statpal, buffer, 768);
}
//
// Palette stuff.
//
//
// I_SetPalette
//
void I_SetPalette(byte* palette)
{
int i;
for (i = 0; i < 256; i++)
{
gamepal[i].r = gammatable[usegamma][*palette++];
gamepal[i].g = gammatable[usegamma][*palette++];
gamepal[i].b = gammatable[usegamma][*palette++];
}
}
void OGL_Error( int GL_Code, char *msg )
{
char msgtext[128];
char errmsg[128];
switch(GL_Code)
{
default:
sprintf(errmsg,"Unknown Error Code: %04X", GL_Code);
strcpy(errmsg, msgtext);
break;
}
lfprintf("GLERR: %s\n", errmsg);
lfprintf("%s FAILED\n", msg);
}
// StartUpOpenGL sets the pixel format and a rendering context then returns true/false
dboolean StartUpOpenGL( HWND hWnd )
{
int nMyPixelFormatID;
static PIXELFORMATDESCRIPTOR pfd = {
sizeof (PIXELFORMATDESCRIPTOR), // struct size
1, // Version number
PFD_DRAW_TO_WINDOW | // Flags, draw to a window,
PFD_SUPPORT_OPENGL | // use OpenGL
PFD_DOUBLEBUFFER, // want double buffering
PFD_TYPE_RGBA, // RGBA pixel values
32, // 32-bit color (or max video mode allows)
0, 0, 0, 0, 0, 0, // RGB bits & shift sizes. Don't care about them
0, // No alpha buffer info
0, // shift bit ignored
0, // no accumulation buffer
0, 0, 0, 0, // accum bits ignored
32, // 32-bit depth buffer if we can get it...
0, // No stencil buffer
0, // No auxiliary buffers
PFD_MAIN_PLANE, // main layer
0, // Reserved (must be 0)
0, 0, 0 // No layer, visible or damage masks
};
lfprintf("Attempting to ChoosePixelFormat...\n");
hGDC = GetDC( hWnd );
nMyPixelFormatID = ChoosePixelFormat( hGDC, &pfd );
if (nMyPixelFormatID == 0)
{
ReleaseDC( hWnd, hGDC );
OGL_Error( 1, "ChoosePixelFormat");
return false;
}
if (SetPixelFormat( hGDC, nMyPixelFormatID, &pfd ) == true)
{
DescribePixelFormat(hGDC, nMyPixelFormatID, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
lfprintf("Pixel Format : %d\n", nMyPixelFormatID);
lfprintf("Version : %X\n", pfd.nVersion);
if (pfd.dwFlags & PFD_DRAW_TO_WINDOW)
lfprintf("Flags : PFD_DRAW_TO_WINDOW\n");
if (pfd.dwFlags & PFD_DRAW_TO_BITMAP)
lfprintf("Flags : PFD_DRAW_TO_BITMAP\n");
if (pfd.dwFlags & PFD_SUPPORT_GDI)
lfprintf("Flags : PFD_SUPPORT_GDI\n");
if (pfd.dwFlags & PFD_SUPPORT_OPENGL)
lfprintf("Flags : PFD_SUPPORT_OPENGL\n");
if (pfd.dwFlags & PFD_GENERIC_ACCELERATED)
lfprintf("Flags : PFD_GENERIC_ACCELERATED\n");
if (pfd.dwFlags & PFD_GENERIC_FORMAT)
lfprintf("Flags : PFD_GENERIC_FORMAT\n");
if (pfd.dwFlags & PFD_NEED_PALETTE)
lfprintf("Flags : PFD_NEED_PALETTE\n");
if (pfd.dwFlags & PFD_NEED_SYSTEM_PALETTE)
lfprintf("Flags : PFD_NEED_SYSTEM_PALETTE\n");
if (pfd.dwFlags & PFD_DOUBLEBUFFER)
lfprintf("Flags : PFD_DOUBLEBUFFER\n");
if (pfd.dwFlags & PFD_STEREO)
lfprintf("Flags : PFD_STEREO\n");
if (pfd.dwFlags & PFD_SWAP_LAYER_BUFFERS)
lfprintf("Flags : PFD_SWAP_LAYER_BUFFERS\n");
lfprintf("ColorBits : %d\n", pfd.cColorBits);
lfprintf("RedBits : %d\n", pfd.cRedBits);
lfprintf("RedShift : %d\n", pfd.cRedShift);
lfprintf("GreenBits : %d\n", pfd.cGreenBits);
lfprintf("GreenShift : %d\n", pfd.cGreenShift);
lfprintf("BlueBits : %d\n", pfd.cBlueBits);
lfprintf("BlueShift : %d\n", pfd.cBlueShift);
lfprintf("AlphaBits : %d\n", pfd.cAlphaBits);
lfprintf("AlphaShift : %d\n", pfd.cAlphaShift);
lfprintf("AccumBits : %d\n", pfd.cAccumBits);
lfprintf("AccumRedBits : %d\n", pfd.cAccumRedBits);
lfprintf("AccumGreenBits : %d\n", pfd.cAccumGreenBits);
lfprintf("AccumBlueBits : %d\n", pfd.cAccumBlueBits);
lfprintf("AccumAlphaBits : %d\n", pfd.cAccumAlphaBits);
lfprintf("DepthBits : %d\n", pfd.cDepthBits);
lfprintf("StencilBits : %d\n", pfd.cStencilBits);
lfprintf("AuxBuffers : %d\n", pfd.cAuxBuffers);
lfprintf("LayerType : %d\n", pfd.iLayerType);
}
else
{
ReleaseDC( hWnd, hGDC );
OGL_Error( 1, "SetPixelFormat");
return false;
}
if ((!(pfd.dwFlags & PFD_GENERIC_ACCELERATED)) &&
((pfd.dwFlags & PFD_GENERIC_FORMAT) > 0))
{
con_printf("\nno OpenGL acceleration found...\n");
if (video.allowsoft == false)
{
ChangeDisplaySettings(0, 0);
MessageBox(NULL, "OpenGL Driver is not accelerated\n", "FATAL ERROR", MB_OK);
return false;
}
}
hRC = wglCreateContext( hGDC );
if (hRC == (HGLRC)NULL)
{
ReleaseDC( hWnd, hGDC );
OGL_Error( 1, "wglCreateContext");
return false;
}
wglMakeCurrent(hGDC, hRC);
wglSwapIntervalEXT(VSYNC_ON); //[AB] - 2022: Using GLEXT extensions for vertical synchronisation capability.
glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
glDisable( GL_DEPTH_TEST );
glmode = gl_2d;
con_SetupOpenGL();
GetGLInfo(WinData.hWnd);
return true;
}
void I_InitGraphics(void)
{
}
void GetGLInfo(HWND hWnd)
{
char *szu, *tempstr;
const GLubyte *szt;
szt = glGetString(GL_VENDOR);
GL_Vendor = (char *)malloc(strlen(szt)+1);
strcpy(GL_Vendor, szt);
con_printf("OpenGL Vendor : %s\n", GL_Vendor);
if (D_strcasecmp(GL_Vendor, "3DLABS") == 0)
GL_3Dlabs = true;
szt = glGetString(GL_RENDERER);
GL_Renderer = (char *)malloc(strlen(szt)+1);
strcpy(GL_Renderer, szt);
con_printf("OpenGL Renderer : %s\n", GL_Renderer);
if ((D_strcasecmp(GL_Vendor, "Microsoft Corporation") == 0) &&
(D_strcasecmp(GL_Renderer, "GDI Generic") == 0))
{
// oh CRAP, it's the dreaded Microsoft Software OpenGL Renderer...
con_printf("SOFTWARE OpenGL Renderer!!!\n");
software = true;
}
szt = glGetString(GL_VERSION);
GL_Version = (char *)malloc(strlen(szt)+1);
strcpy(GL_Version, szt);
con_printf("OpenGL Version : %s\n", GL_Version);
szt = glGetString(GL_EXTENSIONS);
tempstr = (char *)malloc(strlen(szt)+2);
strcpy(tempstr, szt);
if (tempstr != NULL)
{
szu = strtok( (char *)tempstr, " " );
GL_EXT_Count = 0;
while (( szu != NULL ) && (GL_EXT_Count < GL_MAX_EXT))
{
GL_EXT_List[GL_EXT_Count] = (char *)malloc(strlen(szu)+1);
strcpy(GL_EXT_List[GL_EXT_Count], szu);
con_printf("OpenGL Extension : %s\n", GL_EXT_List[GL_EXT_Count]);
szu = strtok( NULL, " " );
GL_EXT_Count++;
}
}
free(tempstr);
con_printf("End of OpenGL extensions...\n");
}