Main Page | Namespace List | Class List | File List | Class Members | File Members

utils.cpp

Go to the documentation of this file.
00001 #include "utils.h"
00002 
00003 #include <math.h>
00004 #include <stdio.h>
00005 #include <gfx/gl.h>
00006 
00007 #include <ClanLib/core.h>
00008 #include <ClanLib/display.h>
00009 #include <ClanLib/gl.h>
00010 
00011 Vec3 world_to_screen(Vec3 world)
00012 {
00013     Vec3 screen;
00014     double mview[16], proj[16];
00015     int view[4];
00016 
00017     glGetDoublev( GL_MODELVIEW_MATRIX, mview ),
00018     glGetDoublev( GL_PROJECTION_MATRIX, proj ),
00019     glGetIntegerv( GL_VIEWPORT, view ),
00020 
00021     gluProject( world[0], world[1], world[2],
00022             mview, proj, view,
00023             &screen[0], &screen[1], &screen[2] );
00024 
00025     // y coord is upside-down
00026     screen[1] = CL_Display::get_height() - screen[1];
00027 
00028     return screen;
00029 }
00030 
00031 Vec3 screen_to_world(Vec3 screen)
00032 {
00033     Vec3 world;
00034     double mview[16], proj[16];
00035     int view[4];
00036 
00037     glGetDoublev( GL_MODELVIEW_MATRIX, mview ),
00038     glGetDoublev( GL_PROJECTION_MATRIX, proj ),
00039     glGetIntegerv( GL_VIEWPORT, view ),
00040 
00041     // y coord is upside-down
00042     screen[1] = CL_Display::get_height() - screen[1];
00043 
00044     gluUnProject( screen[0], screen[1], screen[2],
00045             mview, proj, view,
00046             &world[0], &world[1], &world[2] );
00047 
00048     return world;
00049 }
00050 
00051 void set_translation( Mat4& mat, const Vec3& translate )
00052 {
00053     mat( 0, 3 ) = translate[ 0 ];
00054     mat( 1, 3 ) = translate[ 1 ];
00055     mat( 2, 3 ) = translate[ 2 ];
00056 }
00057 
00058 // Milkshape3D likes to store their rotations as (rx, ry, rz) triples,
00059 // where each is an angle to rotate around that axis.
00060 Mat4 rotation_matrix_angles_rad( const Vec3& angles )
00061 {
00062     double cr = cos( angles[ 0 ] );
00063     double sr = sin( angles[ 0 ] );
00064     double cp = cos( angles[ 1 ] );
00065     double sp = sin( angles[ 1 ] );
00066     double cy = cos( angles[ 2 ] ); 
00067     double sy = sin( angles[ 2 ] );
00068     double srsp = sr * sp;
00069     double crsp = cr * sp;
00070 
00071     Mat4 mat;
00072 
00073     mat( 0, 0 ) = cp * cy;
00074     mat( 1, 0 ) = cp * sy;
00075     mat( 2, 0 ) = -sp;
00076 
00077     mat( 0, 1 ) = srsp * cy - cr * sy;
00078     mat( 1, 1 ) = srsp * sy + cr * cy;
00079     mat( 2, 1 ) = sr * cp;
00080 
00081     mat( 0, 2 ) = crsp * cy + sr * sy;
00082     mat( 1, 2 ) = crsp * sy - sr * cy;
00083     mat( 2, 2 ) = cr * cp;
00084 
00085     mat( 3, 3 ) = 1.0;
00086 
00087     return mat;
00088 }
00089 
00090 Mat4 inverse_t_r( const Mat4& mat )
00091 {
00092     // T = translation matrix, R = rotation matrix, tv = T.col(3); 
00093     // (TR)^-1 = R^-1 * T^-1 = Rt * T^-1 = [Rt, -tv]
00094     Vec3 translate = proj( mat.col( 3 ) );
00095 
00096     Mat4 inv = transpose( mat );
00097     inv[ 3 ] = Vec4( 0.0, 0.0, 0.0, 1.0 );
00098     set_translation( inv, inv * -translate );
00099 
00100     return inv;
00101 }
00102 
00103 void draw_cylinder( Vec3 center, Vec3 dir, const Material& mat, double width, double height )
00104 {
00105     mat.make_current();
00106 
00107     glPushMatrix();
00108 
00109     Vec3 raxis = Vec3( 0, 0, 1 ) ^ dir;
00110 
00111     GLUquadric *qobj = gluNewQuadric();
00112 
00113     glTranslated( center[0], center[1], center[2] );
00114 
00115     if( !( norm( raxis ) < 0.1 ) ) {
00116         glRotated( RAD_TO_DEG( -acos ( Vec3( 0.0, 0.0, 1.0 ) * dir ) ),
00117                 raxis[0], raxis[1], raxis[2] );
00118     }
00119 
00120     glTranslated( 0, 0, -height / 2 );
00121 
00122     gluCylinder( qobj, width, width, height, 5, 1 );
00123 
00124     gluDeleteQuadric( qobj );
00125 
00126     glPopMatrix();
00127 }
00128 
00129 string convert_path( string path )
00130 {
00131     string::size_type pos = 0;
00132 
00133     while ( ( pos = path.find( BAD_PATH_SEP, 0 ) ) != string::npos ) {
00134         path[pos] = PATH_SEP;
00135     }
00136 
00137     return path;
00138 }

Generated on Sat Mar 13 14:58:32 2004 for Ars Physica by doxygen 1.3.5