| 1 |
/* |
| 2 |
glh - is a platform-indepenedent C++ OpenGL helper library |
| 3 |
|
| 4 |
|
| 5 |
Copyright (c) 2000 Cass Everitt |
| 6 |
Copyright (c) 2000 NVIDIA Corporation |
| 7 |
All rights reserved. |
| 8 |
|
| 9 |
Redistribution and use in source and binary forms, with or |
| 10 |
without modification, are permitted provided that the following |
| 11 |
conditions are met: |
| 12 |
|
| 13 |
* Redistributions of source code must retain the above |
| 14 |
copyright notice, this list of conditions and the following |
| 15 |
disclaimer. |
| 16 |
|
| 17 |
* Redistributions in binary form must reproduce the above |
| 18 |
copyright notice, this list of conditions and the following |
| 19 |
disclaimer in the documentation and/or other materials |
| 20 |
provided with the distribution. |
| 21 |
|
| 22 |
* The names of contributors to this software may not be used |
| 23 |
to endorse or promote products derived from this software |
| 24 |
without specific prior written permission. |
| 25 |
|
| 26 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 27 |
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 28 |
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 29 |
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 30 |
REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 31 |
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 32 |
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 33 |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 34 |
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 35 |
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 36 |
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 37 |
POSSIBILITY OF SUCH DAMAGE. |
| 38 |
|
| 39 |
|
| 40 |
Cass Everitt - [email protected] |
| 41 |
*/ |
| 42 |
|
| 43 |
// some helper classes for making |
| 44 |
// and using cube maps |
| 45 |
|
| 46 |
#ifndef GLH_CUBE_MAP_H |
| 47 |
#define GLH_CUBE_MAP_H |
| 48 |
|
| 49 |
#ifdef MACOS |
| 50 |
#include <OpenGL/gl.h> |
| 51 |
#else |
| 52 |
#include <GL/gl.h> |
| 53 |
#endif |
| 54 |
|
| 55 |
#include <glh/glh_obs.h> |
| 56 |
#include <glh/glh_convenience.h> |
| 57 |
#include <glh/glh_linear.h> |
| 58 |
|
| 59 |
namespace glh |
| 60 |
{ |
| 61 |
|
| 62 |
# ifdef GL_ARB_texture_cube_map |
| 63 |
# define GLH_CUBE_MAP_POSITIVE_X GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB |
| 64 |
# define GLH_CUBE_MAP_POSITIVE_Y GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB |
| 65 |
# define GLH_CUBE_MAP_POSITIVE_Z GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB |
| 66 |
# define GLH_CUBE_MAP_NEGATIVE_X GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB |
| 67 |
# define GLH_CUBE_MAP_NEGATIVE_Y GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB |
| 68 |
# define GLH_CUBE_MAP_NEGATIVE_Z GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB |
| 69 |
# elif GL_EXT_texture_cube_map |
| 70 |
# define GLH_CUBE_MAP_POSITIVE_X GL_TEXTURE_CUBE_MAP_POSITIVE_X_EXT |
| 71 |
# define GLH_CUBE_MAP_POSITIVE_Y GL_TEXTURE_CUBE_MAP_POSITIVE_Y_EXT |
| 72 |
# define GLH_CUBE_MAP_POSITIVE_Z GL_TEXTURE_CUBE_MAP_POSITIVE_Z_EXT |
| 73 |
# define GLH_CUBE_MAP_NEGATIVE_X GL_TEXTURE_CUBE_MAP_NEGATIVE_X_EXT |
| 74 |
# define GLH_CUBE_MAP_NEGATIVE_Y GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT |
| 75 |
# define GLH_CUBE_MAP_NEGATIVE_Z GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT |
| 76 |
# endif |
| 77 |
|
| 78 |
# if GL_EXT_texture_cube_map || GL_ARB_texture_cube_map |
| 79 |
// make a cube map from a functor |
| 80 |
template <class FunctionOfDirection> |
| 81 |
void make_cube_map(FunctionOfDirection & f, GLenum internal_format, |
| 82 |
int size, int level = 0) |
| 83 |
{ |
| 84 |
typedef typename FunctionOfDirection::Type Type; |
| 85 |
int components = f.components; |
| 86 |
GLenum type = f.type; |
| 87 |
GLenum format = f.format; |
| 88 |
Type * image = new Type[size*size*components]; |
| 89 |
Type * ip; |
| 90 |
|
| 91 |
float offset = .5; |
| 92 |
float delta = 1; |
| 93 |
float halfsize = size/2.f; |
| 94 |
vec3f v; |
| 95 |
|
| 96 |
// positive x image |
| 97 |
{ |
| 98 |
ip = image; |
| 99 |
for(int j = 0; j < size; j++) |
| 100 |
{ |
| 101 |
for(int i=0; i < size; i++) |
| 102 |
{ |
| 103 |
v[2] = -(i*delta + offset - halfsize); |
| 104 |
v[1] = -(j*delta + offset - halfsize); |
| 105 |
v[0] = halfsize; |
| 106 |
v.normalize(); |
| 107 |
f(v, ip); |
| 108 |
ip += components; |
| 109 |
} |
| 110 |
} |
| 111 |
glTexImage2D(GLH_CUBE_MAP_POSITIVE_X, |
| 112 |
level, internal_format, size, size, 0, format, type, image); |
| 113 |
} |
| 114 |
// negative x image |
| 115 |
{ |
| 116 |
ip = image; |
| 117 |
for(int j = 0; j < size; j++) |
| 118 |
{ |
| 119 |
for(int i=0; i < size; i++) |
| 120 |
{ |
| 121 |
v[2] = (i*delta + offset - halfsize); |
| 122 |
v[1] = -(j*delta + offset - halfsize); |
| 123 |
v[0] = -halfsize; |
| 124 |
v.normalize(); |
| 125 |
f(v, ip); |
| 126 |
ip += components; |
| 127 |
} |
| 128 |
} |
| 129 |
glTexImage2D(GLH_CUBE_MAP_NEGATIVE_X, |
| 130 |
level, internal_format, size, size, 0, format, type, image); |
| 131 |
} |
| 132 |
|
| 133 |
// positive y image |
| 134 |
{ |
| 135 |
ip = image; |
| 136 |
for(int j = 0; j < size; j++) |
| 137 |
{ |
| 138 |
for(int i=0; i < size; i++) |
| 139 |
{ |
| 140 |
v[0] = (i*delta + offset - halfsize); |
| 141 |
v[2] = (j*delta + offset - halfsize); |
| 142 |
v[1] = halfsize; |
| 143 |
v.normalize(); |
| 144 |
f(v, ip); |
| 145 |
ip += components; |
| 146 |
} |
| 147 |
} |
| 148 |
glTexImage2D(GLH_CUBE_MAP_POSITIVE_Y, |
| 149 |
level, internal_format, size, size, 0, format, type, image); |
| 150 |
} |
| 151 |
// negative y image |
| 152 |
{ |
| 153 |
ip = image; |
| 154 |
for(int j = 0; j < size; j++) |
| 155 |
{ |
| 156 |
for(int i=0; i < size; i++) |
| 157 |
{ |
| 158 |
v[0] = (i*delta + offset - halfsize); |
| 159 |
v[2] = -(j*delta + offset - halfsize); |
| 160 |
v[1] = -halfsize; |
| 161 |
v.normalize(); |
| 162 |
f(v, ip); |
| 163 |
ip += components; |
| 164 |
} |
| 165 |
} |
| 166 |
glTexImage2D(GLH_CUBE_MAP_NEGATIVE_Y, |
| 167 |
level, internal_format, size, size, 0, format, type, image); |
| 168 |
} |
| 169 |
|
| 170 |
// positive z image |
| 171 |
{ |
| 172 |
ip = image; |
| 173 |
for(int j = 0; j < size; j++) |
| 174 |
{ |
| 175 |
for(int i=0; i < size; i++) |
| 176 |
{ |
| 177 |
v[0] = (i*delta + offset - halfsize); |
| 178 |
v[1] = -(j*delta + offset - halfsize); |
| 179 |
v[2] = halfsize; |
| 180 |
v.normalize(); |
| 181 |
f(v, ip); |
| 182 |
ip += components; |
| 183 |
} |
| 184 |
} |
| 185 |
glTexImage2D(GLH_CUBE_MAP_POSITIVE_Z, |
| 186 |
level, internal_format, size, size, 0, format, type, image); |
| 187 |
} |
| 188 |
// negative z image |
| 189 |
{ |
| 190 |
ip = image; |
| 191 |
for(int j = 0; j < size; j++) |
| 192 |
{ |
| 193 |
for(int i=0; i < size; i++) |
| 194 |
{ |
| 195 |
v[0] = -(i*delta + offset - halfsize); |
| 196 |
v[1] = -(j*delta + offset - halfsize); |
| 197 |
v[2] = -halfsize; |
| 198 |
v.normalize(); |
| 199 |
f(v, ip); |
| 200 |
ip += components; |
| 201 |
} |
| 202 |
} |
| 203 |
glTexImage2D(GLH_CUBE_MAP_NEGATIVE_Z, |
| 204 |
level, internal_format, size, size, 0, format, type, image); |
| 205 |
} |
| 206 |
delete [] image; |
| 207 |
} |
| 208 |
# endif |
| 209 |
|
| 210 |
struct normalize_vector |
| 211 |
{ |
| 212 |
typedef GLfloat Type; |
| 213 |
int components; |
| 214 |
GLenum type; |
| 215 |
GLenum format; |
| 216 |
normalize_vector() : components(3), type(GL_FLOAT), format(GL_RGB) {} |
| 217 |
|
| 218 |
void operator() (const vec3f & v, Type * t) |
| 219 |
{ |
| 220 |
vec3f v2 = v; |
| 221 |
v2 *= .5; |
| 222 |
v2 += .5; |
| 223 |
t[0] = v2[0]; |
| 224 |
t[1] = v2[1]; |
| 225 |
t[2] = v2[2]; |
| 226 |
} |
| 227 |
}; |
| 228 |
|
| 229 |
struct cube_map_unextended |
| 230 |
{ |
| 231 |
cube_map_unextended() : |
| 232 |
POSITIVE_X(0), NEGATIVE_X(1), |
| 233 |
POSITIVE_Y(2), NEGATIVE_Y(3), |
| 234 |
POSITIVE_Z(4), NEGATIVE_Z(5), |
| 235 |
lightnum(GL_LIGHT0), angle(90.0) |
| 236 |
{} |
| 237 |
|
| 238 |
// angle is > 90 degrees because we need a border to cull with |
| 239 |
void set_angle(GLint width) |
| 240 |
{ angle = to_degrees((float)atan2(width/2.0f, width/2.0f-1.0f)) * 2.0f; } |
| 241 |
|
| 242 |
|
| 243 |
void cull_for_projection(int i) |
| 244 |
{ |
| 245 |
GLfloat plane[6][4] = |
| 246 |
{ |
| 247 |
{ 1, 0, 0, 0 }, |
| 248 |
{-1, 0, 0, 0 }, |
| 249 |
{ 0, 1, 0, 0 }, |
| 250 |
{ 0,-1, 0, 0 }, |
| 251 |
{ 0, 0, 1, 0 }, |
| 252 |
{ 0, 0,-1, 0 } |
| 253 |
}; |
| 254 |
//glClipPlane(GL_CLIP_PLANE0, plane[i]); |
| 255 |
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 90); |
| 256 |
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, plane[i]); |
| 257 |
} |
| 258 |
|
| 259 |
void cull_for_reflection(int i) |
| 260 |
{ |
| 261 |
GLfloat dir[6][4] = |
| 262 |
{ |
| 263 |
{ 1, 0, 0, 0 }, |
| 264 |
{-1, 0, 0, 0 }, |
| 265 |
{ 0, 1, 0, 0 }, |
| 266 |
{ 0,-1, 0, 0 }, |
| 267 |
{ 0, 0, 1, 0 }, |
| 268 |
{ 0, 0,-1, 0 } |
| 269 |
}; |
| 270 |
glLightfv(GL_LIGHT0, GL_POSITION, dir[i]); |
| 271 |
} |
| 272 |
|
| 273 |
matrix4f get_matrix(int cubeface) |
| 274 |
{ |
| 275 |
matrix4f m; |
| 276 |
m = perspective((float)angle, 1.0f, 0.5f, 1.5f); |
| 277 |
switch(cubeface) |
| 278 |
{ |
| 279 |
case 0: |
| 280 |
m *= camera_lookat(vec3f(0,0,0), vec3f( 1, 0, 0), vec3f(0,-1, 0)); |
| 281 |
return m; |
| 282 |
case 1: |
| 283 |
m *= camera_lookat(vec3f(0,0,0), vec3f(-1, 0, 0), vec3f(0,-1, 0)); |
| 284 |
return m; |
| 285 |
case 2: |
| 286 |
m *= camera_lookat(vec3f(0,0,0), vec3f( 0, 1, 0), vec3f(0, 0, 1)); |
| 287 |
return m; |
| 288 |
case 3: |
| 289 |
m *= camera_lookat(vec3f(0,0,0), vec3f( 0,-1, 0), vec3f(0, 0,-1)); |
| 290 |
return m; |
| 291 |
case 4: |
| 292 |
m *= camera_lookat(vec3f(0,0,0), vec3f( 0, 0, 1), vec3f(0,-1, 0)); |
| 293 |
return m; |
| 294 |
case 5: |
| 295 |
m *= camera_lookat(vec3f(0,0,0), vec3f( 0, 0,-1), vec3f(0,-1, 0)); |
| 296 |
return m; |
| 297 |
default: |
| 298 |
return matrix4f(); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
matrix4f get_matrix_inverse(int cubeface) |
| 303 |
{ |
| 304 |
matrix4f m; |
| 305 |
switch(cubeface) |
| 306 |
{ |
| 307 |
case 0: |
| 308 |
m = object_lookat(vec3f(0,0,0), vec3f( 1, 0, 0), vec3f(0,-1, 0)); |
| 309 |
break; |
| 310 |
case 1: |
| 311 |
m = object_lookat(vec3f(0,0,0), vec3f(-1, 0, 0), vec3f(0,-1, 0)); |
| 312 |
break; |
| 313 |
case 2: |
| 314 |
m = object_lookat(vec3f(0,0,0), vec3f( 0, 1, 0), vec3f(0, 0, 1)); |
| 315 |
break; |
| 316 |
case 3: |
| 317 |
m = object_lookat(vec3f(0,0,0), vec3f( 0,-1, 0), vec3f(0, 0,-1)); |
| 318 |
break; |
| 319 |
case 4: |
| 320 |
m = object_lookat(vec3f(0,0,0), vec3f( 0, 0, 1), vec3f(0,-1, 0)); |
| 321 |
break; |
| 322 |
case 5: |
| 323 |
m = object_lookat(vec3f(0,0,0), vec3f( 0, 0,-1), vec3f(0,-1, 0)); |
| 324 |
break; |
| 325 |
default: |
| 326 |
break; |
| 327 |
} |
| 328 |
return m * perspective_inverse((float)angle, 1.0f, 0.5f, 1.5f); |
| 329 |
} |
| 330 |
const int POSITIVE_X; |
| 331 |
const int NEGATIVE_X; |
| 332 |
const int POSITIVE_Y; |
| 333 |
const int NEGATIVE_Y; |
| 334 |
const int POSITIVE_Z; |
| 335 |
const int NEGATIVE_Z; |
| 336 |
|
| 337 |
GLenum lightnum; |
| 338 |
tex_object_2D face[6]; |
| 339 |
double angle; |
| 340 |
|
| 341 |
matrix4f rotation; |
| 342 |
|
| 343 |
}; |
| 344 |
|
| 345 |
} // namespace glh |
| 346 |
#endif |
| 347 |
|