]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - vid_3dfxsvga.c
rain splatters on the ground and water, and turns to steam on slime/lava, bubbles...
[xonotic/darkplaces.git] / vid_3dfxsvga.c
1 /*
2         vid_3dfxsvga.c
3
4         OpenGL device driver for 3Dfx chipsets running Linux
5
6         Copyright (C) 1996-1997  Id Software, Inc.
7         Copyright (C) 1999,2000  Nelson Rush.
8         Copyright (C) 1999,2000  contributors of the QuakeForge project
9         Please see the file "AUTHORS" for a list of contributors
10
11         This program is free software; you can redistribute it and/or
12         modify it under the terms of the GNU General Public License
13         as published by the Free Software Foundation; either version 2
14         of the License, or (at your option) any later version.
15
16         This program is distributed in the hope that it will be useful,
17         but WITHOUT ANY WARRANTY; without even the implied warranty of
18         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19
20         See the GNU General Public License for more details.
21
22         You should have received a copy of the GNU General Public License
23         along with this program; if not, write to:
24
25                 Free Software Foundation, Inc.
26                 59 Temple Place - Suite 330
27                 Boston, MA  02111-1307, USA
28
29         $Id$
30 */
31
32 #include "quakedef.h"
33 #include "sys.h"
34 #include "console.h"
35 #include "sbar.h"
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <signal.h>
40 #include <string.h>
41
42 #include <dlfcn.h>
43
44 #include <GL/gl.h>
45 #include <GL/fxmesa.h>
46 #include <glide/sst1vid.h>
47
48 #define WARP_WIDTH              320
49 #define WARP_HEIGHT             200
50
51
52 //unsigned short        d_8to16table[256];
53 unsigned                d_8to24table[256];
54 unsigned char   d_15to8table[65536];
55
56 cvar_t          vid_mode = {"vid_mode","0",false};
57
58 viddef_t        vid;    // global video state
59
60 static void     *dlhand = NULL;
61
62 static fxMesaContext fc = NULL;
63 static int      scr_width, scr_height;
64
65 int     VID_options_items = 0;
66
67 /*-----------------------------------------------------------------------*/
68
69 int     texture_extension_number = 1;
70
71 float           gldepthmin, gldepthmax;
72
73 const char *gl_vendor;
74 const char *gl_renderer;
75 const char *gl_version;
76 const char *gl_extensions;
77
78 void (*qglColorTableEXT) (int, int, int, int, int, const void*);
79 void (*qgl3DfxSetPaletteEXT) (GLuint *);
80 void (*qglMTexCoord2f) (GLenum, GLfloat, GLfloat);
81 void (*qglSelectTexture) (GLenum);
82
83 int gl_mtex_enum = 0;
84
85 // LordHavoc: in GLX these are never set, simply provided to make the rest of the code work
86 qboolean is8bit = false;
87 qboolean isPermedia = false;
88 qboolean isATI = false;
89 qboolean isG200 = false;
90 qboolean isRagePro = false;
91 qboolean gl_mtexable = false;
92 qboolean gl_arrays = false;
93
94 /*-----------------------------------------------------------------------*/
95 void D_BeginDirectRect (int x, int y, byte *pbitmap, int width, int height)
96 {
97 }
98
99 void D_EndDirectRect (int x, int y, int width, int height)
100 {
101 }
102
103 void VID_Shutdown(void)
104 {
105         if (!fc)
106                 return;
107
108         fxMesaDestroyContext(fc);
109 }
110
111 void signal_handler(int sig)
112 {
113         printf("Received signal %d, exiting...\n", sig);
114         Host_Shutdown();
115         abort();
116         //Sys_Quit();
117         exit(0);
118 }
119
120 void InitSig(void)
121 {
122         signal(SIGHUP, signal_handler);
123         signal(SIGINT, signal_handler);
124         signal(SIGQUIT, signal_handler);
125         signal(SIGILL, signal_handler);
126         signal(SIGTRAP, signal_handler);
127 //      signal(SIGIOT, signal_handler);
128         signal(SIGBUS, signal_handler);
129 //      signal(SIGFPE, signal_handler);
130         signal(SIGSEGV, signal_handler);
131         signal(SIGTERM, signal_handler);
132 }
133
134 /*
135         CheckMultiTextureExtensions
136
137         Check for ARB, SGIS, or EXT multitexture support
138 */
139 void
140 CheckMultiTextureExtensions ( void )
141 {
142         Con_Printf ("Checking for multitexture... ");
143         if (COM_CheckParm ("-nomtex"))
144         {
145                 Con_Printf ("disabled\n");
146                 return;
147         }
148         dlhand = dlopen (NULL, RTLD_LAZY);
149         if (dlhand == NULL)
150         {
151                 Con_Printf ("unable to check\n");
152                 return;
153         }
154         if (strstr(gl_extensions, "GL_ARB_multitexture "))
155         {
156                 Con_Printf ("GL_ARB_multitexture\n");
157                 qglMTexCoord2f = (void *)dlsym(dlhand, "glMultiTexCoord2fARB");
158                 qglSelectTexture = (void *)dlsym(dlhand, "glActiveTextureARB");
159                 gl_mtex_enum = GL_TEXTURE0_ARB;
160                 gl_mtexable = true;
161         } else if (strstr(gl_extensions, "GL_SGIS_multitexture "))
162         {
163                 Con_Printf ("GL_SGIS_multitexture\n");
164                 qglMTexCoord2f = (void *)dlsym(dlhand, "glMTexCoord2fSGIS");
165                 qglSelectTexture = (void *)dlsym(dlhand, "glSelectTextureSGIS");
166                 gl_mtex_enum = TEXTURE0_SGIS;
167                 gl_mtexable = true;
168         } else {
169                 Con_Printf ("none found\n");
170         }
171         dlclose(dlhand);
172         dlhand = NULL;          
173 }
174
175
176 typedef void (GLAPIENTRY *gl3DfxSetDitherModeEXT_FUNC) (GrDitherMode_t mode);
177
178 /*
179 ===============
180 GL_Init
181 ===============
182 */
183 void GL_Init (void)
184 {
185         gl_vendor = glGetString (GL_VENDOR);
186         Con_Printf ("GL_VENDOR: %s\n", gl_vendor);
187         gl_renderer = glGetString (GL_RENDERER);
188         Con_Printf ("GL_RENDERER: %s\n", gl_renderer);
189
190         gl_version = glGetString (GL_VERSION);
191         Con_Printf ("GL_VERSION: %s\n", gl_version);
192         gl_extensions = glGetString (GL_EXTENSIONS);
193         Con_Printf ("GL_EXTENSIONS: %s\n", gl_extensions);
194
195         CheckMultiTextureExtensions ();
196
197         glCullFace(GL_FRONT);
198         glEnable(GL_TEXTURE_2D);
199
200         glEnable(GL_ALPHA_TEST);
201         glAlphaFunc(GL_GREATER, 0.666);
202
203         glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
204         glShadeModel (GL_FLAT);
205
206         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
207         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
208         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
209         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
210
211         glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
212
213         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
214
215         Con_Printf ("Dithering: ");
216
217         dlhand = dlopen (NULL, RTLD_LAZY);
218
219         if (dlhand == NULL) {
220                 Con_SafePrintf ("unable to set.\n");
221                 return;
222         }
223
224         if (strstr(gl_extensions, "3DFX_set_dither_mode")) {
225                 gl3DfxSetDitherModeEXT_FUNC dither_select = NULL;
226
227                 dither_select = (void *) dlsym(dlhand, "gl3DfxSetDitherModeEXT");
228
229                 if (COM_CheckParm ("-dither_2x2")) {
230                         dither_select(GR_DITHER_2x2);
231                         Con_Printf ("2x2.\n");
232                 } else if (COM_CheckParm ("-dither_4x4")) {
233                         dither_select(GR_DITHER_4x4);
234                         Con_Printf ("4x4.\n");
235                 } else {
236                         glDisable(GL_DITHER);
237                         Con_Printf ("disabled.\n");
238                 }
239         }
240         dlclose(dlhand);
241         dlhand = NULL;
242 }
243
244 /*
245 =================
246 GL_BeginRendering
247
248 =================
249 */
250 void GL_BeginRendering (int *x, int *y, int *width, int *height)
251 {
252         *x = *y = 0;
253         *width = scr_width;
254         *height = scr_height;
255
256 //    if (!wglMakeCurrent( maindc, baseRC ))
257 //              Sys_Error ("wglMakeCurrent failed");
258
259 //      glViewport (*x, *y, *width, *height);
260 }
261
262
263 void GL_EndRendering (void)
264 {
265         if (!r_render.value)
266                 return;
267         glFlush();
268         fxMesaSwapBuffers();
269 }
270
271 static int resolutions[][3]={
272         { 320,  200,    GR_RESOLUTION_320x200 },
273         { 320,  240,    GR_RESOLUTION_320x240 },
274         { 400,  256,    GR_RESOLUTION_400x256 },
275         { 400,  300,    GR_RESOLUTION_400x300 },
276         { 512,  256,    GR_RESOLUTION_512x256 },
277         { 512,  384,    GR_RESOLUTION_512x384 },
278         { 640,  200,    GR_RESOLUTION_640x200 },
279         { 640,  350,    GR_RESOLUTION_640x350 },
280         { 640,  400,    GR_RESOLUTION_640x400 },
281         { 640,  480,    GR_RESOLUTION_640x480 },
282         { 800,  600,    GR_RESOLUTION_800x600 },
283         { 856,  480,    GR_RESOLUTION_856x480 },
284         { 960,  720,    GR_RESOLUTION_960x720 },
285 #ifdef GR_RESOLUTION_1024x768
286         { 1024, 768,    GR_RESOLUTION_1024x768 },
287 #endif
288 #ifdef GR_RESOLUTION_1152x864
289         { 1152, 864,    GR_RESOLUTION_1152x864 },
290 #endif
291 #ifdef GR_RESOLUTION_1280x960
292         { 1280, 960,    GR_RESOLUTION_1280x960 },
293 #endif
294 #ifdef GR_RESOLUTION_1280x1024
295         { 1280, 1024,   GR_RESOLUTION_1280x1024 },
296 #endif
297 #ifdef GR_RESOLUTION_1600x1024
298         { 1600, 1024,   GR_RESOLUTION_1600x1024 },
299 #endif
300 #ifdef GR_RESOLUTION_1600x1200
301         { 1600, 1200,   GR_RESOLUTION_1600x1200 },
302 #endif
303 #ifdef GR_RESOLUTION_1792x1344
304         { 1792, 1344,   GR_RESOLUTION_1792x1344 },
305 #endif
306 #ifdef GR_RESOLUTION_1856x1392
307         { 1856, 1392,   GR_RESOLUTION_1856x1392 },
308 #endif
309 #ifdef GR_RESOLUTION_1920x1440
310         { 1920, 1440,   GR_RESOLUTION_1920x1440 },
311 #endif
312 #ifdef GR_RESOLUTION_2048x1536
313         { 2048, 1536,   GR_RESOLUTION_2048x1536 },
314 #endif
315 #ifdef GR_RESOLUTION_2048x2048
316         { 2048, 2048,   GR_RESOLUTION_2048x2048 }
317 #endif
318 };
319
320 #define NUM_RESOLUTIONS         (sizeof(resolutions)/(sizeof(int)*3))
321
322
323 static int
324 findres(int *width, int *height)
325 {
326         int i;
327
328         for(i=0; i < NUM_RESOLUTIONS; i++) {
329                 if((*width <= resolutions[i][0]) &&
330                    (*height <= resolutions[i][1])) {
331                         *width = resolutions[i][0];
332                         *height = resolutions[i][1];
333                         return resolutions[i][2];
334                 }
335         }
336
337         *width = 640;
338         *height = 480;
339         return GR_RESOLUTION_640x480;
340 }
341
342 qboolean VID_Is8bit(void)
343 {
344         return is8bit;
345 }
346
347 typedef void (GLAPIENTRY *glColorTableEXT_FUNC) (GLenum, GLenum, GLsizei, 
348                 GLenum, GLenum, const GLvoid *);
349 typedef void (GLAPIENTRY *gl3DfxSetPaletteEXT_FUNC) (GLuint *pal);
350
351 void VID_Init8bitPalette()
352 {
353         // Check for 8bit Extensions and initialize them.
354         int i;
355
356         dlhand = dlopen (NULL, RTLD_LAZY);
357
358         Con_SafePrintf ("8-bit GL extensions: ");
359
360         if (dlhand == NULL) {
361                 Con_SafePrintf ("unable to check.\n");
362                 return;
363         }
364
365         if (COM_CheckParm("-no8bit")) {
366                 Con_SafePrintf("disabled.\n");
367                 return;
368         }
369
370         if (strstr(gl_extensions, "3DFX_set_global_palette") && (qgl3DfxSetPaletteEXT = dlsym(dlhand, "gl3DfxSetPaletteEXT")) != NULL)
371         {
372                 GLubyte table[256][4];
373                 char *oldpal;
374
375                 Con_SafePrintf("3DFX_set_global_palette.\n");
376                 glEnable( GL_SHARED_TEXTURE_PALETTE_EXT );
377                 oldpal = (char *) d_8to24table; //d_8to24table3dfx;
378                 for (i=0;i<256;i++)
379                 {
380                         table[i][2] = *oldpal++;
381                         table[i][1] = *oldpal++;
382                         table[i][0] = *oldpal++;
383                         table[i][3] = 255;
384                         oldpal++;
385                 }
386                 qgl3DfxSetPaletteEXT((GLuint *)table);
387                 is8bit = true;
388         } else if (strstr(gl_extensions, "GL_EXT_shared_texture_palette")) {
389                 char thePalette[256*3];
390                 char *oldPalette, *newPalette;
391                 glColorTableEXT_FUNC load_texture = NULL;
392
393                 Con_SafePrintf("GL_EXT_shared.\n");
394                 load_texture = (void *) dlsym(dlhand, "glColorTableEXT");
395
396                 glEnable( GL_SHARED_TEXTURE_PALETTE_EXT );
397                 oldPalette = (char *) d_8to24table; //d_8to24table3dfx;
398                 newPalette = thePalette;
399                 for (i=0;i<256;i++) {
400                         *newPalette++ = *oldPalette++;
401                         *newPalette++ = *oldPalette++;
402                         *newPalette++ = *oldPalette++;
403                         oldPalette++;
404                 }
405                 load_texture(GL_SHARED_TEXTURE_PALETTE_EXT, GL_RGB, 256, GL_RGB, GL_UNSIGNED_BYTE, (void *) thePalette);
406                 is8bit = true;
407         } else {
408                 Con_SafePrintf ("not found.\n");
409         }
410
411         dlclose(dlhand);
412         dlhand = NULL;
413 }
414
415 extern void Check_Gamma (unsigned char *pal);
416 void VID_Setup15to8Palette ();
417
418 void VID_Init(unsigned char *palette)
419 {
420         int i;
421         GLint attribs[32];
422         char    gldir[MAX_OSPATH];
423         int width = 640, height = 480;
424
425 // set vid parameters
426         attribs[0] = FXMESA_DOUBLEBUFFER;
427         attribs[1] = FXMESA_ALPHA_SIZE;
428         attribs[2] = 1;
429         attribs[3] = FXMESA_DEPTH_SIZE;
430         attribs[4] = 1;
431         attribs[5] = FXMESA_NONE;
432
433         if ((i = COM_CheckParm("-width")) != 0)
434                 width = atoi(com_argv[i+1]);
435         if ((i = COM_CheckParm("-height")) != 0)
436                 height = atoi(com_argv[i+1]);
437
438         if ((i = COM_CheckParm("-conwidth")) != 0)
439                 vid.conwidth = atoi(com_argv[i+1]);
440         else
441                 vid.conwidth = 640;
442
443         vid.conwidth &= 0xfff8; // make it a multiple of eight
444
445         if (vid.conwidth < 320)
446                 vid.conwidth = 320;
447
448         // pick a conheight that matches with correct aspect
449         vid.conheight = vid.conwidth*3 / 4;
450
451         if ((i = COM_CheckParm("-conheight")) != 0)
452                 vid.conheight = atoi(com_argv[i+1]);
453         if (vid.conheight < 200)
454                 vid.conheight = 200;
455
456         fc = fxMesaCreateContext(0, findres(&width, &height), GR_REFRESH_75Hz,
457                 attribs);
458         if (!fc)
459                 Sys_Error("Unable to create 3DFX context.\n");
460
461         scr_width = width;
462         scr_height = height;
463
464         fxMesaMakeCurrent(fc);
465
466         if (vid.conheight > height)
467                 vid.conheight = height;
468         if (vid.conwidth > width)
469                 vid.conwidth = width;
470         vid.width = vid.conwidth;
471         vid.height = vid.conheight;
472
473         vid.aspect = ((float)vid.height / (float)vid.width) * (320.0 / 240.0);
474
475         InitSig(); // trap evil signals
476
477         GL_Init();
478
479         snprintf(gldir, sizeof(gldir), "%s/glquake", com_gamedir);
480         Sys_mkdir (gldir);
481
482         VID_SetPalette(palette);
483
484         Check_Gamma(palette);
485
486         // Check for 3DFX Extensions and initialize them.
487         VID_Init8bitPalette();
488
489         if (is8bit) // LordHavoc: avoid calculating 15to8 table if it won't be used
490                 VID_Setup15to8Palette ();
491
492         Con_SafePrintf ("Video mode %dx%d initialized.\n", width, height);
493
494         vid.recalc_refdef = 1;                          // force a surface cache flush
495 }
496
497 void VID_ExtraOptionDraw(unsigned int options_draw_cursor)
498 {
499 /* Port specific Options menu entrys */
500 }
501
502 void VID_ExtraOptionCmd(int option_cursor)
503 {
504 /*
505         switch(option_cursor)
506         {
507         case 12:  // Always start with 12
508         break;
509         }
510 */
511 }
512 void VID_InitCvars ()
513 {
514 }
515
516 void VID_SetCaption (char *text)
517 {
518 }
519
520 void VID_HandlePause (qboolean pause)
521 {
522 }