summaryrefslogtreecommitdiff
path: root/tests/glean/glutils.cpp
blob: 4b38d620305b0eefdea22f4981b9f8e59a3af740 (plain)
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
// BEGIN_COPYRIGHT -*- glean -*-
// 
// Copyright (C) 1999, 2000  Allen Akin   All Rights Reserved.
// 
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the
// Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
// KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL ALLEN AKIN BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
// AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
// OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
// 
// END_COPYRIGHT




// glutils.cpp:  frequently-used OpenGL operations

#define GLX_GLXEXT_PROTOTYPES
#include "glwrap.h"
#include "environ.h"
#include "lex.h"
#include "glutils.h"
#if defined(__X11__)
#   include <dlfcn.h>
#endif
#if defined(__AGL__)
#   include <cstring>
#endif

namespace GLEAN {

namespace GLUtils {

///////////////////////////////////////////////////////////////////////////////
// useScreenCoords:  Map object coords directly to screen coords.
///////////////////////////////////////////////////////////////////////////////
void
useScreenCoords(int windowW, int windowH) {
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0, windowW, 0, windowH, -1, 1);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	glViewport(0, 0, windowW, windowH);
	glTranslatef(0.375, 0.375, 0.0);
} // useScreenCoords

///////////////////////////////////////////////////////////////////////////////
// haveExtensions:  See if the current rendering context supports a given
//	set of extensions.
///////////////////////////////////////////////////////////////////////////////
bool
haveExtensions(const char* required) {
	const char* available = reinterpret_cast<const char*>
		(glGetString(GL_EXTENSIONS));

	if (!required)
		return true;
	if (!available)
		return false;

	bool haveAll = true;
	Lex lRequired(required);
	for (lRequired.next(); lRequired.token != Lex::END; lRequired.next()) {
		if (lRequired.token != Lex::ID)
			continue;
		bool haveOne = false;
		Lex lAvailable(available);
		for (lAvailable.next(); lAvailable.token != Lex::END;
		    lAvailable.next())
			if (lAvailable.token == Lex::ID
			  && lAvailable.id == lRequired.id) {
				haveOne = true;
				break;
			}
		haveAll &= haveOne;
		if (!haveAll)
			break;
	}

	return haveAll;
} // haveExtensions


float
getVersion()
{
   const GLubyte *version = glGetString(GL_VERSION);
   // we rely on atof() stopping parsing at whitespace
   return atof((const char *) version);
}


///////////////////////////////////////////////////////////////////////////////
// getProcAddress: Get address of an OpenGL or window-system-binding function.
//	This belongs here, rather than as a member of RenderingContext, because
//	on Windows it must only be applied to the *current* context.  (The
//	return value on Windows is context-dependent, and wglGetProcAddress
//	doesn't take a rendering context as an argument.)
///////////////////////////////////////////////////////////////////////////////
void
(*getProcAddress(const char* name))() {
#if defined(__X11__)
#   if defined(GLX_ARB_get_proc_address)
	return glXGetProcAddressARB(reinterpret_cast<const GLubyte*>(name));
#   else
	// XXX This isn't guaranteed to work, but it may be the best option
	// we have at the moment.
	void* libHandle = dlopen("libGL.so", RTLD_LAZY);
	if (libHandle) {
		void* funcPointer = dlsym(libHandle, name);
		dlclose(libHandle);
		return funcPointer;
	} else
		return 0;
#   endif
#elif defined(__WIN__)
	// Gotta be a little more explicit about the cast to please MSVC.
	typedef void (__cdecl* VOID_FUNC_VOID) ();
	return reinterpret_cast<VOID_FUNC_VOID>(wglGetProcAddress(name));
#elif defined(__BEWIN__)
#	error "Need GetProcAddress (or equivalent) for BeOS"
	return 0;
#elif defined(__AGL__)
	// Very quick hack to keep things running for a few hours until
	// a better solution is in place:
	if (!strcmp(name, "glLockArraysEXT"))
		return reinterpret_cast<void (*)()> (glLockArraysEXT);
	else if (!strcmp(name, "glUnlockArraysEXT"))
		return reinterpret_cast<void (*)()> (glUnlockArraysEXT);
	else if (!strcmp(name, "glActiveTextureARB"))
		return reinterpret_cast<void (*)()> (glActiveTextureARB);
	else if (!strcmp(name, "glMultiTexCoord2fARB"))
		return reinterpret_cast<void (*)()> (glMultiTexCoord2fARB);
	else if (!strcmp(name, "glLockArraysEXT"))
		return reinterpret_cast<void (*)()> (glLockArraysEXT);
	else if (!strcmp(name, "glUnlockArraysEXT"))
		return reinterpret_cast<void (*)()> (glUnlockArraysEXT);
	else if (!strcmp(name, "glLockArraysEXT"))
		return reinterpret_cast<void (*)()> (glLockArraysEXT);
	else if (!strcmp(name, "glUnlockArraysEXT"))
		return reinterpret_cast<void (*)()> (glUnlockArraysEXT);
	else
		return 0;
#endif
} // getProcAddress

///////////////////////////////////////////////////////////////////////////////
// logGLErrors: Check for OpenGL errors and log any that have occurred.
///////////////////////////////////////////////////////////////////////////////
void
logGLErrors(Environment& env) {
	GLenum err;
	while ((err = glGetError()))
		env.log << "\tOpenGL error: " << gluErrorString(err) << '\n';
} // logGLErrors


///////////////////////////////////////////////////////////////////////////////
// Syntactic sugar for light sources
///////////////////////////////////////////////////////////////////////////////

Light::Light(int l) {
	lightNumber = static_cast<GLenum>(GL_LIGHT0 + l);
} // Light::Light

void
Light::ambient(float r, float g, float b, float a){
	GLfloat v[4];
	v[0] = r; v[1] = g; v[2] = b; v[3] = a;
	glLightfv(lightNumber, GL_AMBIENT, v);
} // Light::ambient

void
Light::diffuse(float r, float g, float b, float a){
	GLfloat v[4];
	v[0] = r; v[1] = g; v[2] = b; v[3] = a;
	glLightfv(lightNumber, GL_DIFFUSE, v);
} // Light::diffuse

void
Light::specular(float r, float g, float b, float a){
	GLfloat v[4];
	v[0] = r; v[1] = g; v[2] = b; v[3] = a;
	glLightfv(lightNumber, GL_SPECULAR, v);
} // Light::specular

void
Light::position(float x, float y, float z, float w){
	GLfloat v[4];
	v[0] = x; v[1] = y; v[2] = z; v[3] = w;
	glLightfv(lightNumber, GL_POSITION, v);
} // Light::position

void
Light::spotDirection(float x, float y, float z){
	GLfloat v[3];
	v[0] = x; v[1] = y; v[2] = z;
	glLightfv(lightNumber, GL_SPOT_DIRECTION, v);
} // Light::spotDirection

void
Light::spotExponent(float e){
	glLightf(lightNumber, GL_SPOT_EXPONENT, e);
} // Light::spotExponent

void
Light::spotCutoff(float c){
	glLightf(lightNumber, GL_SPOT_CUTOFF, c);
} // Light::spotCutoff

void
Light::constantAttenuation(float a){
	glLightf(lightNumber, GL_CONSTANT_ATTENUATION, a);
} // Light::constantAttenuation

void
Light::linearAttenuation(float a){
	glLightf(lightNumber, GL_LINEAR_ATTENUATION, a);
} // Light::linearAttenuation

void
Light::quadraticAttenuation(float a){
	glLightf(lightNumber, GL_QUADRATIC_ATTENUATION, a);
} // Light::quadraticAttenuation

void
Light::enable() {
	glEnable(lightNumber);
} // Light::enable

void
Light::disable() {
	glDisable(lightNumber);
} // Light::disable

///////////////////////////////////////////////////////////////////////////////
// Syntactic sugar for light model
///////////////////////////////////////////////////////////////////////////////

LightModel::LightModel() {
} // LightModel::LightModel

void
LightModel::ambient(float r, float g, float b, float a) {
	GLfloat v[4];
	v[0] = r; v[1] = g; v[2] = b; v[3] = a;
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, v);
} // LightModel::ambient

void
LightModel::localViewer(bool v) {
	glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, static_cast<GLint>(v));
} // LightModel::localViewer

void
LightModel::twoSide(bool v) {
	glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, static_cast<GLint>(v));
} // LightModel::twoSide

void
LightModel::colorControl(GLenum e) {
	glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, e);
} // LightModel::colorControl

///////////////////////////////////////////////////////////////////////////////
// Syntactic sugar for material properties
///////////////////////////////////////////////////////////////////////////////

Material::Material(GLenum f) {
	face = f;
} // Material::Material

void
Material::ambient(float r, float g, float b, float a) {
	GLfloat v[4];
	v[0] = r; v[1] = g; v[2] = b; v[3] = a;
	glMaterialfv(face, GL_AMBIENT, v);
} // Material::ambient

void
Material::diffuse(float r, float g, float b, float a) {
	GLfloat v[4];
	v[0] = r; v[1] = g; v[2] = b; v[3] = a;
	glMaterialfv(face, GL_DIFFUSE, v);
} // Material::diffuse

void
Material::ambientAndDiffuse(float r, float g, float b, float a) {
	GLfloat v[4];
	v[0] = r; v[1] = g; v[2] = b; v[3] = a;
	glMaterialfv(face, GL_AMBIENT_AND_DIFFUSE, v);
} // Material::ambientAndDiffuse

void
Material::specular(float r, float g, float b, float a) {
	GLfloat v[4];
	v[0] = r; v[1] = g; v[2] = b; v[3] = a;
	glMaterialfv(face, GL_SPECULAR, v);
} // Material::specular

void
Material::emission(float r, float g, float b, float a) {
	GLfloat v[4];
	v[0] = r; v[1] = g; v[2] = b; v[3] = a;
	glMaterialfv(face, GL_EMISSION, v);
} // Material::emission

void
Material::shininess(float s) {
	glMaterialf(face, GL_SHININESS, static_cast<GLfloat>(s));
} // Material::shininess


} // namespace GLUtils

} // namespace GLEAN