TORCS  1.3.10
The Open Racing Car Simulator
Loading...
Searching...
No Matches
glfeatures.cpp
Go to the documentation of this file.
1/***************************************************************************
2
3 file : glfeatures.cpp
4 created : Wed Jun 1 14:56:31 CET 2005
5 copyright : (C) 2005-2026 by Bernhard Wymann
6
7***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18
19/*
20 Functions to check if features seems to be available and requested by the
21 user. The isAvailable functions should return if a feature is working on
22 the system, the isEnabled feature should check if the user wants to enable
23 it as well.
24 It should NOT check if the features are really working, that is subject
25 to another part eventually.
26*/
27
28#include <glfeatures.h>
29#include <portability.h>
30
31/*
32 ----------------------- Texture Compression
33*/
34
35
38
39// Feature checks, GL_ARB_texture_compression.
40void checkCompressARBAvailable(bool &result)
41{
42 // Query if the extension is avaiable at the runtime system (true, if > 0).
43 int compressARB = GfuiGlutExtensionSupported("GL_ARB_texture_compression");
44 // Check if at least one internal format is vailable. This is a workaround for
45 // driver problems and not a bugfix. According to the specification OpenGL should
46 // choose an uncompressed alternate format if it can't provide the requested
47 // compressed one... but it does not on all cards/drivers.
48 if (compressARB) {
49 int numformats;
50 glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS_ARB, &numformats);
51 if (numformats == 0) {
52 compressARB = 0;
53 }
54 }
55
56 result = (compressARB > 0) ? true : false;
57}
58
59
60void checkCompressARBEnabled(bool &result)
61{
63 // Feature not available, do not use it.
64 result = false;
65 } else {
66 // Feature available, check if the user wants to use it.
67 // TODO: put this enabled/disable stuff in one function (it is used in grsound.cpp as well).
68 const char *tcEnabledStr = GR_ATT_TEXTURECOMPRESSION_ENABLED;
69 const int BUFSIZE = 1024;
70 char fnbuf[BUFSIZE];
71 snprintf(fnbuf, BUFSIZE, "%s%s", GetLocalDir(), GR_PARAM_FILE);
72 void *paramHandle = GfParmReadFile(fnbuf, GFPARM_RMODE_REREAD | GFPARM_RMODE_CREAT);
74
75 if (strcmp(optionName, tcEnabledStr) != 0) {
76 result = false;
77 } else {
78 result = true;
79 }
80 GfParmReleaseHandle(paramHandle);
81 }
82}
83
84
89
90
91// GL_ARB_texture_compression
93{
95}
96
97
99 return compressARBEnabled;
100}
101
102
103/*
104 ----------------------- Texture downsizing.
105*/
108
109
110void getGLTextureMaxSize(int &result)
111{
112 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &result);
113 if (result > 16384) {
114 result = 16384;
115 }
116}
117
118
119void getUserTextureMaxSize(int &result)
120{
121 const int BUFSIZE = 1024;
122 char fnbuf[BUFSIZE];
123 snprintf(fnbuf, BUFSIZE, "%s%s", GetLocalDir(), GR_PARAM_FILE);
124 void *paramHandle = GfParmReadFile(fnbuf, GFPARM_RMODE_REREAD | GFPARM_RMODE_CREAT);
125 result = (int) GfParmGetNum(paramHandle, GR_SCT_GLFEATURES, GR_ATT_TEXTURESIZE, (char*)NULL, (tdble) glTextureMaxSize);
126 if (result > glTextureMaxSize) {
127 result = glTextureMaxSize;
128 }
129 GfParmReleaseHandle(paramHandle);
130}
131
132
137
139{
140 return glTextureMaxSize;
141}
142
143
145{
146 return userTextureMaxSize;
147}
148
149
150/*
151 ----------------------- Texture anisotropic filtering.
152*/
156
157
158static int getBestTextureAnisotropy(int requested)
159{
160 static const int textureAnisotropyOptionList[] = {2, 4, 8, 16};
161 int result = 0;
162 int maxAnisotropy = (glTextureMaxAnisotropy < 16) ? glTextureMaxAnisotropy : 16;
163
164 for (int i = 0; i < (int)(sizeof(textureAnisotropyOptionList) / sizeof(textureAnisotropyOptionList[0])); i++) {
165 if (textureAnisotropyOptionList[i] <= requested && textureAnisotropyOptionList[i] <= maxAnisotropy) {
166 result = textureAnisotropyOptionList[i];
167 }
168 }
169
170 return result;
171}
172
173
174void checkTextureAnisotropyAvailable(bool &result, int &maxAnisotropy)
175{
176 result = false;
177 maxAnisotropy = 1;
178
179 if (!GfuiGlutExtensionSupported("GL_EXT_texture_filter_anisotropic")) {
180 return;
181 }
182
183 GLfloat maxValue = 1.0f;
184 glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxValue);
185 if (maxValue >= 2.0f) {
186 result = true;
187 maxAnisotropy = (int)maxValue;
188 }
189}
190
191
193{
195 result = 0;
196 return;
197 }
198
199 const int BUFSIZE = 1024;
200 char fnbuf[BUFSIZE];
201 snprintf(fnbuf, BUFSIZE, "%s%s", GetLocalDir(), GR_PARAM_FILE);
202 void *paramHandle = GfParmReadFile(fnbuf, GFPARM_RMODE_REREAD | GFPARM_RMODE_CREAT);
203 int anisotropy = (int)GfParmGetNum(paramHandle, GR_SCT_GLFEATURES, GR_ATT_TEXTUREANISOTROPY, (char*)NULL, -1);
204 if (anisotropy < 0) {
206 } else if (anisotropy < 2) {
207 result = 0;
208 } else {
209 result = getBestTextureAnisotropy(anisotropy);
210 }
211 GfParmReleaseHandle(paramHandle);
212}
213
214
219
220
225
226
228{
230}
231
232
234{
236}
237
238
240{
242 return;
243 }
244
245 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT,
246 (userTextureAnisotropy >= 2) ? (GLfloat)userTextureAnisotropy : 1.0f);
247}
248
249
250// Initialize
259
bool isCompressARBEnabled(void)
void checkCompressARBEnabled(bool &result)
void applyUserTextureAnisotropy(void)
int getGLTextureMaxSize(void)
void checkCompressARBAvailable(bool &result)
static int userTextureAnisotropy
static int getBestTextureAnisotropy(int requested)
int getUserTextureAnisotropy(void)
static int glTextureMaxAnisotropy
void checkTextureAnisotropyAvailable(bool &result, int &maxAnisotropy)
void updateUserTextureMaxSize(void)
void updateUserTextureAnisotropy(void)
static bool compressARBEnabled
static int glTextureMaxSize
static bool compressARBAvailable
static int userTextureMaxSize
bool isCompressARBAvailable(void)
int getUserTextureMaxSize(void)
static bool textureAnisotropyAvailable
int getGLTextureMaxAnisotropy(void)
void updateCompressARBEnabled(void)
bool isTextureAnisotropyAvailable(void)
void checkGLFeatures(void)
#define GR_ATT_TEXTUREANISOTROPY
Definition graphic.h:55
#define GR_ATT_TEXTURECOMPRESSION_DISABLED
Definition graphic.h:53
#define GR_ATT_TEXTURECOMPRESSION
Definition graphic.h:51
#define GR_ATT_TEXTURESIZE
Definition graphic.h:61
#define GR_ATT_TEXTURECOMPRESSION_ENABLED
Definition graphic.h:52
#define GR_SCT_GLFEATURES
Definition graphic.h:50
#define GR_PARAM_FILE
Definition graphic.h:40
float tdble
Floating point type used in TORCS.
Definition tgf.h:47
const char * GfParmGetStr(void *parmHandle, const char *path, const char *key, const char *deflt)
Get a string parameter from the parameter set handle.
Definition params.cpp:2453
tdble GfParmGetNum(void *handle, const char *path, const char *key, const char *unit, tdble deflt)
Get a numerical parameter from the parameter set handle.
Definition params.cpp:2534
void GfParmReleaseHandle(void *parmHandle)
Release given parameter set handle parmHandle.
Definition params.cpp:1963
void * GfParmReadFile(const char *file, int mode)
Read parameter set from file and return handle to parameter set.
Definition params.cpp:1173
int GfuiGlutExtensionSupported(const char *str)
Query an OpenGL extension through GLUT for the current rendering context.
Definition screen.cpp:1046
static int textureAnisotropyOptionList[]
char * GetLocalDir(void)
Definition tgf.cpp:230
#define GFPARM_RMODE_CREAT
Allow an empty in-memory set on file-open failure; does not write a file.
Definition tgf.h:266
#define GFPARM_RMODE_REREAD
Clear shared cached data and reread the file.
Definition tgf.h:265