glh_convenience.h
Go to the documentation of this file.
00001 /*
00002     glh - is a platform-indepenedent C++ OpenGL helper library 
00003 
00004 
00005     Copyright (c) 2000 Cass Everitt
00006         Copyright (c) 2000 NVIDIA Corporation
00007     All rights reserved.
00008 
00009     Redistribution and use in source and binary forms, with or
00010         without modification, are permitted provided that the following
00011         conditions are met:
00012 
00013      * Redistributions of source code must retain the above
00014            copyright notice, this list of conditions and the following
00015            disclaimer.
00016 
00017      * Redistributions in binary form must reproduce the above
00018            copyright notice, this list of conditions and the following
00019            disclaimer in the documentation and/or other materials
00020            provided with the distribution.
00021 
00022      * The names of contributors to this software may not be used
00023            to endorse or promote products derived from this software
00024            without specific prior written permission. 
00025 
00026        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027            ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028            LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00029            FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00030            REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00031            INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00032            BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00033            LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00034            CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00035            LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00036            ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
00037            POSSIBILITY OF SUCH DAMAGE. 
00038 
00039 
00040     Cass Everitt - cass@r3.nu
00041 */
00042 
00043 #ifndef GLH_CONVENIENCE_H
00044 #define GLH_CONVENIENCE_H
00045 
00046 // Convenience methods for using glh_linear objects
00047 // with opengl...
00048 
00049 
00050 
00051 // debugging hack...
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   // matrix helpers
00070 
00071   inline matrix4f get_matrix(GLenum matrix) 
00072   {
00073         GLfloat m[16];
00074         glGetFloatv(matrix, m);
00075         return matrix4f(m);
00076   }
00077 
00078   // transform helpers
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   // inverse of camera_lookat
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   // inverse of object_lookat
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   // are these names ok?
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   // handy for register combiners
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 } // namespace glh
00208 
00209 #endif


nao_openni
Author(s): Bener SUAY
autogenerated on Mon Jan 6 2014 11:27:50