1 #ifndef _OGL_SETUP_
2 #define _OGL_SETUP_
3 /*
4 
5 	Copyright (C) 1991-2001 and beyond by Bungie Studios, Inc.
6 	and the "Aleph One" developers.
7 
8 	This program is free software; you can redistribute it and/or modify
9 	it under the terms of the GNU General Public License as published by
10 	the Free Software Foundation; either version 3 of the License, or
11 	(at your option) any later version.
12 
13 	This program is distributed in the hope that it will be useful,
14 	but WITHOUT ANY WARRANTY; without even the implied warranty of
15 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 	GNU General Public License for more details.
17 
18 	This license is contained in the file "COPYING",
19 	which is included with this source code; it is available online at
20 	http://www.gnu.org/licenses/gpl.html
21 
22 	OpenGL Interface File,
23 	by Loren Petrich,
24 	March 12, 2000
25 
26 	This contains functions intended for finding out OpenGL's presence
27 	in the host system, for setting parameters for OpenGL rendering,
28 	and for deciding whether to use OpenGL for rendering.
29 
30 	May 27, 2000 (Loren Petrich)
31 
32 	Added support for flat static effect
33 
34 	Added XML support
35 
36 	May 31, 2000 (Loren Petrich)
37 
38 	Added support for texture resetting. This clears all the textures in memory
39 	and forces them to be reloaded. This may be good in cases of textures dropping out.
40 
41 	June 11, 2000 (Loren Petrich)
42 
43 	Added support for see-through liquids as an OpenGL parameter.
44 	Also added an opacity-value shift for making dark areas more visible
45 
46 Sep 9, 2000:
47 
48 	Added flag for AppleGL texturing fix
49 
50 Dec 3. 2000 (Loren Petrich):
51 	Changed 16-bit internal representation of textures from 5551 to 4444
52 
53 Dec 17, 2000 (Loren Petrich):
54 	Eliminated fog parameters from the preferences;
55 	there is still a "fog present" switch, which is used to indicate
56 	whether fog will not be suppressed.
57 
58 Apr 27, 2001 (Loren Petrich):
59 	Modified the OpenGL fog support so as to enable below-liquid fogs
60 
61 Aug 21, 2001 (Loren Petrich):
62 	Adding support for 3D-model inhabitant objects
63 */
64 
65 
66 #include "OGL_Subst_Texture_Def.h"
67 #include "OGL_Model_Def.h"
68 
69 #include <cmath>
70 #include <string>
71 
72 #if (defined(__WIN32__) || (defined(__APPLE__) && defined(__MACH__)))
73 #define OPENGL_DOESNT_COPY_ON_SWAP
74 #endif
75 
76 /* These OpenGL extensions are very new, and not present in any glext.h I could
77    find except Mesa's. Adding them here is harmless as the tokens are
78    standardized, and not used unless the extensions are detected, and has the
79    benefit of simplifying the sRGB code and making it so that when Apple adds
80    support to its OpenGL renderer AlephOnes built on older versions of OSX will
81    still be able to make use of it (ditto other OSes) -SB */
82 #ifndef GL_FRAMEBUFFER_SRGB_EXT
83 #define GL_FRAMEBUFFER_SRGB_EXT           0x8DB9
84 #endif
85 #ifndef GL_SRGB
86 #define GL_SRGB                           0x8C40
87 #endif
88 #ifndef GL_SRGB_ALPHA
89 #define GL_SRGB_ALPHA                     0x8C42
90 #endif
91 #if defined(GL_ARB_texture_compression) && defined(GL_COMPRESSED_RGB_S3TC_DXT1_EXT)
92 #ifndef GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
93 #define GL_COMPRESSED_SRGB_S3TC_DXT1_EXT  0x8C4C
94 #endif
95 #ifndef GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
96 #define GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT 0x8C4D
97 #endif
98 #ifndef GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
99 #define GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT 0x8C4E
100 #endif
101 #ifndef GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
102 #define GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT 0x8C4F
103 #endif
104 
105 #endif
106 
107 /* These probably need to be somewhere else */
108 /* Whether we're doing sRGB right now */
109 extern bool Using_sRGB;
110 /* Whether we'll be using sRGB at all */
111 extern bool Wanting_sRGB;
112 /* Whether to use sRGB framebuffer for bloom */
113 extern bool Bloom_sRGB;
114 /* Whether we can use framebuffer objects */
115 extern bool FBO_Allowed;
116 
117 extern bool npotTextures;
118 
119 #ifdef HAVE_OPENGL
120 
121 /* Using the EXT_framebuffer_sRGB spec as reference */
sRGB_frob(GLfloat f)122 static inline float sRGB_frob(GLfloat f) {
123 	if (Using_sRGB) {
124 		return (f <= 0.04045f ? f * (1.f/12.92f) : std::pow((f + 0.055) * (1.0/1.055), 2.4));
125 	} else {
126 		return f;
127 	}
128 }
129 
130 void SglColor3f(GLfloat r, GLfloat g, GLfloat b);
131 void SglColor3fv(const GLfloat* v);
132 void SglColor3ub(GLubyte r, GLubyte g, GLubyte b);
133 void SglColor3us(GLushort r, GLushort g, GLushort b);
134 void SglColor3usv(const GLushort* v);
135 void SglColor4f(GLfloat r, GLfloat g, GLfloat b, GLfloat a);
136 void SglColor4fv(const GLfloat* v);
137 void SglColor4usv(const GLushort* v);
138 #endif
139 
140 // Initializer; returns whether or not OpenGL is present
141 bool OGL_Initialize();
142 
143 // Test for presence of OpenGL
144 bool OGL_IsPresent();
145 
146 // Test for whether OpenGL is currently active
147 bool OGL_IsActive();
148 
149 // Test whether an extension exists
150 bool OGL_CheckExtension(const std::string);
151 
152 void OGL_StartProgress(int total_progress);
153 void OGL_ProgressCallback(int delta_progress);
154 void OGL_StopProgress();
155 
156 // Here are some OpenGL configuration options and how to access them
157 // (they are in the preferences data)
158 
159 // There are separate texturing options for each kind of texture,
160 // as listed below; this is so that one can degrade texture quality independently,
161 // and have (say) high-quality walls and weapons in hand, medium-quality inhabitant sprites,
162 // and low-quality landscapes.
163 enum
164 {
165 	OGL_Txtr_Wall,
166 	OGL_Txtr_Landscape,
167 	OGL_Txtr_Inhabitant,
168 	OGL_Txtr_WeaponsInHand,
169 	OGL_NUMBER_OF_TEXTURE_TYPES
170 };
171 
172 /*
173 	All enumeration starts from 0, in contrast to MacOS popup menus, for example,
174 	which start from one.
175 
176 	The filters are OpenGL filter types
177 	GL_NEAREST		(Pixelated)
178 	GL_LINEAR		(Smoothed)
179 	GL_NEAREST_MIPMAP_NEAREST
180 	GL_LINEAR_MIPMAP_NEAREST
181 	GL_NEAREST_MIPMAP_LINEAR
182 	GL_LINEAR_MIPMAP_LINEAR
183 
184 	Nearby textures have only the first two;
185 	distant textures have the additional four, which feature mipmapping.
186 
187 	The resolutions are how much to shrink the textures before using them,
188 	in order to save VRAM space; they are, in order
189 	x1
190 	x1/2
191 	x1/4
192 
193 	The color depths indicate what numbers of bits for each color channel:
194 
195 	32-bit (8888)
196 	16-bit (4444)
197 	8-bit  (2222)
198 */
199 
200 struct OGL_Texture_Configure
201 {
202 	int16 NearFilter;
203 	int16 FarFilter;
204 	int16 Resolution;
205 	int16 ColorFormat;
206 	int16 MaxSize;
207 };
208 
209 // Here are some control flags
210 enum
211 {
212 	OGL_Flag_ZBuffer	= 0x0001,	// Whether to use a Z-buffer
213 	OGL_Flag_VoidColor	= 0x0002,	// Whether to color the void
214 	OGL_Flag_FlatLand	= 0x0004,	// Whether to use flat-textured landscapes
215 	OGL_Flag_Fog		= 0x0008,	// Whether to make fog
216 	OGL_Flag_3D_Models	= 0x0010,	// Whether to use 3D models
217 	OGL_Flag_2DGraphics	= 0x0020,	// Whether to pipe 2D graphics through OpenGL
218 	OGL_Flag_FlatStatic	= 0x0040,	// Whether to make the "static" effect look flat
219 	OGL_Flag_Fader		= 0x0080,	// Whether to do the fader effects in OpenGL
220 	OGL_Flag_LiqSeeThru	= 0x0100,	// Whether the liquids can be seen through
221 	OGL_Flag_Map		= 0x0200,	// Whether to do the overhead map with OpenGL
222 	OGL_Flag_TextureFix	= 0x0400,	// Whether to apply a texture fix for old Apple OpenGL
223 	OGL_Flag_HUD		= 0x0800,	// Whether to do the HUD with OpenGL
224 	OGL_Flag_Blur		= 0x1000,   // Whether to blur landscapes and glowing textures
225 	OGL_Flag_BumpMap	= 0x2000,   // Whether to use bump mapping
226 	OGL_Flag_MimicSW    = 0x4000,   // Whether to mimic software perspective
227 };
228 
229 struct OGL_ConfigureData
230 {
231 	// Configure textures
232 	OGL_Texture_Configure TxtrConfigList[OGL_NUMBER_OF_TEXTURE_TYPES];
233 
234 	// Configure models
235 	OGL_Texture_Configure ModelConfig;
236 
237 	// Overall rendering flags
238 	uint16 Flags;
239 
240 	// Color of the Void
241 	RGBColor VoidColor;
242 
243 	// Landscape Flat Colors
244 	// First index: which landscape
245 	// (day, night, moon, outer space)
246 	// Second index: ground, sky
247 	RGBColor LscpColors[4][2];
248 
249 	// Anisotropy setting
250 	float AnisotropyLevel;
251 	int16 Multisamples;
252 
253 	bool GeForceFix;
254 	bool WaitForVSync;
255   bool Use_sRGB;
256 	bool Use_NPOT;
257 };
258 
259 OGL_ConfigureData& Get_OGL_ConfigureData();
260 
261 // The OpenGL-configuration dialog box; returns whether its changes had been selected
262 // bool OGL_ConfigureDialog(OGL_ConfigureData& Data);
263 
264 // Set defaults
265 void OGL_SetDefaults(OGL_ConfigureData& Data);
266 
267 
268 // for managing the model and image loading and unloading;
269 int OGL_CountModelsImages(short Collection);
270 void OGL_LoadModelsImages(short Collection);
271 void OGL_UnloadModelsImages(short Collection);
272 
273 // Reset the textures (walls, sprites, and model skins) (good if they start to crap out)
274 // Implemented in OGL_Textures.cpp
275 void OGL_ResetTextures();
276 
277 #ifdef MOVED_OUT
278 
279 #ifdef HAVE_OPENGL
280 
281 // 3D-Model and Skin Support
282 
283 // Model-skin options
284 struct OGL_SkinData: public OGL_TextureOptionsBase
285 {
286 	short CLUT;				// Which color table is this skin for? (-1 is all)
287 
OGL_SkinDataOGL_SkinData288 	OGL_SkinData(): CLUT(ALL_CLUTS) {}
289 };
290 
291 // Manages skins, in case we decide to have separate static and animated models
292 struct OGL_SkinManager
293 {
294 	// List of skins that a model will "own"
295 	vector<OGL_SkinData> SkinData;
296 
297 	// OpenGL skin ID's (one for each possible
298 	// Copied from TextureState in OGL_Textures
299 	// Which member textures?
300 	enum
301 	{
302 		Normal,		// Used for all normally-shaded and shadeless textures
303 		Glowing,	// Used for self-luminous textures
304 		NUMBER_OF_TEXTURES
305 	};
306 	GLuint IDs[NUMBER_OF_OPENGL_BITMAP_SETS][NUMBER_OF_TEXTURES];		// Texture ID's
307 	bool IDsInUse[NUMBER_OF_OPENGL_BITMAP_SETS][NUMBER_OF_TEXTURES];	// Which ID's are being used?
308 
309 	void Reset(bool Clear_OGL_Txtrs);		// Resets the skins so that they may be reloaded;
310 											// indicate whether to clear OpenGL textures
311 
312 	OGL_SkinData *GetSkin(short CLUT);		// Gets a pointer to a skin-data object; NULL for no skin available
313 	bool Use(short CLUT, short Which);		// Uses a skin; returns whether to load one
314 
315 	// For convenience
316 	void Load();
317 	void Unload();
318 };
319 
320 
321 // Mode
322 enum
323 {
324 	OGL_MLight_Fast,			// Fast method -- only one miner's-light calculation
325 	OGL_MLight_Fast_NoFade,		// Like above, but miner's light doesn't fade toward sides
326 	OGL_MLight_Indiv,			// Miner's light calculated for each vertex
327 	OGL_MLight_Indiv_NoFade,	// Like above, but miner's light doesn't fade toward sides
328 	NUMBER_OF_MODEL_LIGHT_TYPES
329 };
330 
331 
332 // Static 3D-Model Data and Options
333 class OGL_ModelData: public OGL_SkinManager
334 {
335 public:
336 	// Name of the model file;
337 	// there are two extra names here for handling ggadwa's Dim3 multiple files
338 	vector<char> ModelFile, ModelFile1, ModelFile2;
339 
340 	// Type of model-file data (guess the model-file type if empty)
341 	vector<char> ModelType;
342 
343 	// Preprocessing: rotation scaling, shifting
344 	// Scaling and rotation are applied before shifting
345 	// Scaling can be negative, thus producing mirroring
346 	float Scale;					// From model units to engine internal units (not World Units)
347 	float XRot, YRot, ZRot;			// In degrees
348 	float XShift, YShift, ZShift;	// In internal units
349 	short Sidedness;				// Which side of the polygons is visible?
350 									// (+: clockwise, -: counterclockwise, 0: both)
351 	short NormalType;				// What type of normals?
352 	float NormalSplit;				// Threshold for splitting the vertex normals
353 	short LightType;				// What type of lighting?
354 	short DepthType;				// What sort of depth reference to use?
355 									// (+: farthest point, -: nearest point, 0: center point)
356 
357 	// Should a rotation rate be included, in order to get that Quake look?
358 
359 	// The model itself (static, single-skin [only one skin at a time])
360 	Model3D Model;
ModelPresent()361 	bool ModelPresent() {return !Model.VertIndices.empty();}
362 
363 	// For convenience
364 	void Load();
365 	void Unload();
366 
OGL_ModelData()367 	OGL_ModelData():
368 		Scale(1), XRot(0), YRot(0), ZRot(0), XShift(0), YShift(0), ZShift(0), Sidedness(1),
369 			NormalType(1), NormalSplit(0.5), LightType(0), DepthType(0) {}
370 };
371 
372 
373 // Returns NULL if a collectiona and sequence do not have an associated model;
374 // also returns which model sequence was found (
375 OGL_ModelData *OGL_GetModelData(short Collection, short Sequence, short& ModelSequence);
376 
377 // Resets all model skins; arg is whether to clear OpenGL textures
378 void OGL_ResetModelSkins(bool Clear_OGL_Txtrs);
379 
380 #endif // def HAVE_OPENGL
381 
382 #endif
383 
384 #ifdef HAVE_OPENGL
385 
386 // Resets all model skins; arg is whether to clear OpenGL textures
387 void OGL_ResetModelSkins(bool Clear_OGL_Txtrs);
388 
389 #endif // def HAVE_OPENGL
390 
391 
392 // Fog data record
393 struct OGL_FogData
394 {
395 	rgb_color Color;
396 	float Depth;		// In World Units (1024 internal units)
397 	bool IsPresent;
398 	bool AffectsLandscapes;
399 };
400 
401 // Fog types
402 enum
403 {
404 	OGL_Fog_AboveLiquid,
405 	OGL_Fog_BelowLiquid,
406 	OGL_NUMBER_OF_FOG_TYPES
407 };
408 
409 OGL_FogData *OGL_GetFogData(int Type);
410 
411 
412 class InfoTree;
413 void parse_mml_opengl(const InfoTree& root);
414 void reset_mml_opengl();
415 
416 #endif
417