[3255] | 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 - cass@r3.nu
|
---|
| 41 | */
|
---|
| 42 |
|
---|
| 43 | #ifndef GLH_CONVENIENCE_H
|
---|
| 44 | #define GLH_CONVENIENCE_H
|
---|
| 45 |
|
---|
| 46 | // Convenience methods for using glh_linear objects
|
---|
| 47 | // with opengl...
|
---|
| 48 |
|
---|
| 49 |
|
---|
| 50 |
|
---|
| 51 | // debugging hack...
|
---|
| 52 | #include <iostream>
|
---|
| 53 |
|
---|
| 54 | using namespace std;
|
---|
| 55 |
|
---|
| 56 | #ifdef MACOS
|
---|
| 57 | #include <OpenGL/gl.h>
|
---|
| 58 | #else
|
---|
| 59 | #include <GL/gl.h>
|
---|
| 60 | #endif
|
---|
| 61 |
|
---|
| 62 | #include <glh/glh_linear.h>
|
---|
| 63 | #include <glh/glh_extensions.h>
|
---|
| 64 |
|
---|
| 65 |
|
---|
| 66 | namespace glh
|
---|
| 67 | {
|
---|
| 68 |
|
---|
| 69 | // matrix helpers
|
---|
| 70 |
|
---|
| 71 | inline matrix4f get_matrix(GLenum matrix)
|
---|
| 72 | {
|
---|
| 73 | GLfloat m[16];
|
---|
| 74 | glGetFloatv(matrix, m);
|
---|
| 75 | return matrix4f(m);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | // transform helpers
|
---|
| 79 |
|
---|
| 80 | inline void glh_rotate(const quaternionf & r)
|
---|
| 81 | {
|
---|
| 82 | float angle;
|
---|
| 83 | vec3f axis;
|
---|
| 84 | r.get_value(axis, angle);
|
---|
| 85 | glRotatef(to_degrees(angle), axis.v[0], axis.v[1], axis.v[2]);
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | // inverse of camera_lookat
|
---|
| 89 | inline matrix4f object_lookat(const vec3f & from, const vec3f & to, const vec3f & Up)
|
---|
| 90 | {
|
---|
| 91 | vec3f look = to - from;
|
---|
| 92 | look.normalize();
|
---|
| 93 | vec3f up(Up);
|
---|
| 94 | up -= look * look.dot(up);
|
---|
| 95 | up.normalize();
|
---|
| 96 |
|
---|
| 97 | quaternionf r(vec3f(0,0,-1), vec3f(0,1,0), look, up);
|
---|
| 98 | matrix4f m;
|
---|
| 99 | r.get_value(m);
|
---|
| 100 | m.set_translate(from);
|
---|
| 101 | return m;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 |
|
---|
| 105 | // inverse of object_lookat
|
---|
| 106 | inline matrix4f camera_lookat(const vec3f & eye, const vec3f & lookpoint, const vec3f & Up)
|
---|
| 107 | {
|
---|
| 108 | vec3f look = lookpoint - eye;
|
---|
| 109 | look.normalize();
|
---|
| 110 | vec3f up(Up);
|
---|
| 111 | up -= look * look.dot(up);
|
---|
| 112 | up.normalize();
|
---|
| 113 |
|
---|
| 114 | matrix4f t;
|
---|
| 115 | t.set_translate(-eye);
|
---|
| 116 |
|
---|
| 117 | quaternionf r(vec3f(0,0,-1), vec3f(0,1,0), look, up);
|
---|
| 118 | r.invert();
|
---|
| 119 | matrix4f rm;
|
---|
| 120 | r.get_value(rm);
|
---|
| 121 | return rm*t;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 |
|
---|
| 125 | inline matrix4f frustum(float left, float right,
|
---|
| 126 | float bottom, float top,
|
---|
| 127 | float zNear, float zFar)
|
---|
| 128 | {
|
---|
| 129 | matrix4f m;
|
---|
| 130 | m.make_identity();
|
---|
| 131 |
|
---|
| 132 | m(0,0) = (2*zNear) / (right - left);
|
---|
| 133 | m(0,2) = (right + left) / (right - left);
|
---|
| 134 |
|
---|
| 135 | m(1,1) = (2*zNear) / (top - bottom);
|
---|
| 136 | m(1,2) = (top + bottom) / (top - bottom);
|
---|
| 137 |
|
---|
| 138 | m(2,2) = -(zFar + zNear) / (zFar - zNear);
|
---|
| 139 | m(2,3) = -2*zFar*zNear / (zFar - zNear);
|
---|
| 140 |
|
---|
| 141 | m(3,2) = -1;
|
---|
| 142 | m(3,3) = 0;
|
---|
| 143 |
|
---|
| 144 | return m;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | inline matrix4f frustum_inverse(float left, float right,
|
---|
| 148 | float bottom, float top,
|
---|
| 149 | float zNear, float zFar)
|
---|
| 150 | {
|
---|
| 151 | matrix4f m;
|
---|
| 152 | m.make_identity();
|
---|
| 153 |
|
---|
| 154 | m(0,0) = (right - left) / (2 * zNear);
|
---|
| 155 | m(0,3) = (right + left) / (2 * zNear);
|
---|
| 156 |
|
---|
| 157 | m(1,1) = (top - bottom) / (2 * zNear);
|
---|
| 158 | m(1,3) = (top + bottom) / (2 * zNear);
|
---|
| 159 |
|
---|
| 160 | m(2,2) = 0;
|
---|
| 161 | m(2,3) = -1;
|
---|
| 162 |
|
---|
| 163 | m(3,2) = -(zFar - zNear) / (2 * zFar * zNear);
|
---|
| 164 | m(3,3) = (zFar + zNear) / (2 * zFar * zNear);
|
---|
| 165 |
|
---|
| 166 | return m;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | inline matrix4f perspective(float fovy, float aspect, float zNear, float zFar)
|
---|
| 170 | {
|
---|
| 171 | double tangent = tan(to_radians(fovy/2.0f));
|
---|
| 172 | float y = (float)tangent * zNear;
|
---|
| 173 | float x = aspect * y;
|
---|
| 174 | return frustum(-x, x, -y, y, zNear, zFar);
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | inline matrix4f perspective_inverse(float fovy, float aspect, float zNear, float zFar)
|
---|
| 178 | {
|
---|
| 179 | double tangent = tan(to_radians(fovy/2.0f));
|
---|
| 180 | float y = (float)tangent * zNear;
|
---|
| 181 | float x = aspect * y;
|
---|
| 182 | return frustum_inverse(-x, x, -y, y, zNear, zFar);
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 |
|
---|
| 186 |
|
---|
| 187 | // are these names ok?
|
---|
| 188 |
|
---|
| 189 | inline void set_texgen_planes(GLenum plane_type, const matrix4f & m)
|
---|
| 190 | {
|
---|
| 191 | GLenum coord[] = {GL_S, GL_T, GL_R, GL_Q };
|
---|
| 192 | for(int i = 0; i < 4; i++)
|
---|
| 193 | {
|
---|
| 194 | vec4f row;
|
---|
| 195 | m.get_row(i,row);
|
---|
| 196 | glTexGenfv(coord[i], plane_type, row.v);
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | // handy for register combiners
|
---|
| 201 | inline vec3f range_compress(const vec3f & v)
|
---|
| 202 | { vec3f vret(v); vret *= .5f; vret += .5f; return vret; }
|
---|
| 203 |
|
---|
| 204 | inline vec3f range_uncompress(const vec3f & v)
|
---|
| 205 | { vec3f vret(v); vret -= .5f; vret *= 2.f; return vret; }
|
---|
| 206 |
|
---|
| 207 | } // namespace glh
|
---|
| 208 |
|
---|
| 209 | #endif
|
---|