00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #ifndef GLH_CONVENIENCE_H
00044 #define GLH_CONVENIENCE_H
00045
00046
00047
00048
00049
00050
00051
00052 #include <iostream>
00053
00054 using namespace std;
00055
00056 #ifdef MACOS
00057 #include <OpenGL/gl.h>
00058 #else
00059 #include <GL/gl.h>
00060 #endif
00061
00062 #include <glh/glh_linear.h>
00063 #include <glh/glh_extensions.h>
00064
00065
00066 namespace glh
00067 {
00068
00069
00070
00071 inline matrix4f get_matrix(GLenum matrix)
00072 {
00073 GLfloat m[16];
00074 glGetFloatv(matrix, m);
00075 return matrix4f(m);
00076 }
00077
00078
00079
00080 inline void glh_rotate(const quaternionf & r)
00081 {
00082 float angle;
00083 vec3f axis;
00084 r.get_value(axis, angle);
00085 glRotatef(to_degrees(angle), axis.v[0], axis.v[1], axis.v[2]);
00086 }
00087
00088
00089 inline matrix4f object_lookat(const vec3f & from, const vec3f & to, const vec3f & Up)
00090 {
00091 vec3f look = to - from;
00092 look.normalize();
00093 vec3f up(Up);
00094 up -= look * look.dot(up);
00095 up.normalize();
00096
00097 quaternionf r(vec3f(0,0,-1), vec3f(0,1,0), look, up);
00098 matrix4f m;
00099 r.get_value(m);
00100 m.set_translate(from);
00101 return m;
00102 }
00103
00104
00105
00106 inline matrix4f camera_lookat(const vec3f & eye, const vec3f & lookpoint, const vec3f & Up)
00107 {
00108 vec3f look = lookpoint - eye;
00109 look.normalize();
00110 vec3f up(Up);
00111 up -= look * look.dot(up);
00112 up.normalize();
00113
00114 matrix4f t;
00115 t.set_translate(-eye);
00116
00117 quaternionf r(vec3f(0,0,-1), vec3f(0,1,0), look, up);
00118 r.invert();
00119 matrix4f rm;
00120 r.get_value(rm);
00121 return rm*t;
00122 }
00123
00124
00125 inline matrix4f frustum(float left, float right,
00126 float bottom, float top,
00127 float zNear, float zFar)
00128 {
00129 matrix4f m;
00130 m.make_identity();
00131
00132 m(0,0) = (2*zNear) / (right - left);
00133 m(0,2) = (right + left) / (right - left);
00134
00135 m(1,1) = (2*zNear) / (top - bottom);
00136 m(1,2) = (top + bottom) / (top - bottom);
00137
00138 m(2,2) = -(zFar + zNear) / (zFar - zNear);
00139 m(2,3) = -2*zFar*zNear / (zFar - zNear);
00140
00141 m(3,2) = -1;
00142 m(3,3) = 0;
00143
00144 return m;
00145 }
00146
00147 inline matrix4f frustum_inverse(float left, float right,
00148 float bottom, float top,
00149 float zNear, float zFar)
00150 {
00151 matrix4f m;
00152 m.make_identity();
00153
00154 m(0,0) = (right - left) / (2 * zNear);
00155 m(0,3) = (right + left) / (2 * zNear);
00156
00157 m(1,1) = (top - bottom) / (2 * zNear);
00158 m(1,3) = (top + bottom) / (2 * zNear);
00159
00160 m(2,2) = 0;
00161 m(2,3) = -1;
00162
00163 m(3,2) = -(zFar - zNear) / (2 * zFar * zNear);
00164 m(3,3) = (zFar + zNear) / (2 * zFar * zNear);
00165
00166 return m;
00167 }
00168
00169 inline matrix4f perspective(float fovy, float aspect, float zNear, float zFar)
00170 {
00171 double tangent = tan(to_radians(fovy/2.0f));
00172 float y = (float)tangent * zNear;
00173 float x = aspect * y;
00174 return frustum(-x, x, -y, y, zNear, zFar);
00175 }
00176
00177 inline matrix4f perspective_inverse(float fovy, float aspect, float zNear, float zFar)
00178 {
00179 double tangent = tan(to_radians(fovy/2.0f));
00180 float y = (float)tangent * zNear;
00181 float x = aspect * y;
00182 return frustum_inverse(-x, x, -y, y, zNear, zFar);
00183 }
00184
00185
00186
00187
00188
00189 inline void set_texgen_planes(GLenum plane_type, const matrix4f & m)
00190 {
00191 GLenum coord[] = {GL_S, GL_T, GL_R, GL_Q };
00192 for(int i = 0; i < 4; i++)
00193 {
00194 vec4f row;
00195 m.get_row(i,row);
00196 glTexGenfv(coord[i], plane_type, row.v);
00197 }
00198 }
00199
00200
00201 inline vec3f range_compress(const vec3f & v)
00202 { vec3f vret(v); vret *= .5f; vret += .5f; return vret; }
00203
00204 inline vec3f range_uncompress(const vec3f & v)
00205 { vec3f vret(v); vret -= .5f; vret *= 2.f; return vret; }
00206
00207 }
00208
00209 #endif