GLCamera.cpp

00001 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
00002 /* 
00003  * Copyright (C) 2008 Micha Hersch, EPFL
00004  * RobotCub Consortium, European Commission FP6 Project IST-004370
00005  * email:   micha.hersch@robotcub.org
00006  * website: www.robotcub.org
00007  * Permission is granted to copy, distribute, and/or modify this program
00008  * under the terms of the GNU General Public License, version 2 or any
00009  * later version published by the Free Software Foundation.
00010  *
00011  * A copy of the license can be found at
00012  * http://www.robotcub.org/icub/license/gpl.txt
00013  *
00014  * This program is distributed in the hope that it will be useful, but
00015  * WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
00017  * Public License for more details
00018  */
00019 #include <GL/glut.h>
00020 #include <math.h>
00021 #include <stdlib.h>
00022 #include <stdio.h>
00023 #include <vector>
00024 #include <string>
00025 #include <iostream>
00026 #include <fstream>
00027 using namespace std;
00028 
00029 #include "GLCamera.h"
00030 
00031 GLCamera::GLCamera(){
00032   m_ref.Identity();    
00033   v_set(0.0,-150.0,-1500.0,m_ref.m_origin);
00034   m_hold    = false;
00035   m_Width   = 100;
00036   m_Height  = 100;
00037 }
00038 
00039 void GLCamera::Apply(){
00040   glMatrixMode(GL_PROJECTION);
00041   glLoadIdentity();  
00042 
00043   glViewport(0, 0, m_Width, m_Height);
00044 
00045   if(m_Height == 0)
00046     m_Height = 1;
00047 
00048   float ratio = 1.0f * m_Width / m_Height;
00049   
00050   gluPerspective(45,ratio,0.1,10000.0);
00051 
00052   glMatrixMode(GL_MODELVIEW);
00053   glLoadIdentity();
00054   m_ref.Update();
00055   glMultMatrixf(*m_ref.GetRef());
00056 }
00057 
00058 
00059 void GLCamera::Init(int w, int h){
00060   m_Width   = w;
00061   m_Height  = h;
00062 }
00063 
00064 void GLCamera::Move(float dx, float dy, float dz, float ay, float ax) {
00065   if(m_hold)
00066     m_ref.Copy(&m_holdRef);
00067 
00068   CVector3_t tmpSrc,tmpDst;
00069   CMatrix3_t tmpInv;
00070   m_inverse(m_ref.m_orient,tmpInv);
00071 
00072   v_set(dx,dy,dz,tmpSrc);    
00073   v_transform(tmpSrc,tmpInv,tmpDst);
00074   v_add(m_ref.m_origin,tmpSrc,m_ref.m_origin);
00075 
00076   CMatrix3_t tmp1,tmp2;
00077 
00078   m_rotation_x(-ax,tmp1);
00079   m_copy(m_ref.m_orient,tmp2);
00080   m_multiply(tmp1,tmp2,m_ref.m_orient);
00081 
00082   m_rotation_y(-ay,tmp1);
00083   m_copy(m_ref.m_orient,tmp2);
00084   m_multiply(tmp2,tmp1,m_ref.m_orient);
00085 
00086 
00087   m_ref.Update();
00088   glLoadIdentity();
00089   glMultMatrixf(*m_ref.GetRef());
00090 }
00091 
00092 void GLCamera::Hold(){
00093   m_hold = true;
00094   m_holdRef.Copy(&m_ref);
00095 }
00096 
00097 void GLCamera::Accept(){
00098   m_hold = false;    
00099 }
00100 
00101 
00102 void GLCamera::Save(string fname){
00103   ofstream f;
00104   f.open(fname.c_str());
00105   if(f.is_open()){
00106     f << m_ref.m_origin[0] << " "<< m_ref.m_origin[1] << " "<< m_ref.m_origin[2] << endl;
00107     f << m_ref.m_orient[ 0] << " "<< m_ref.m_orient[ 1] << " "<< m_ref.m_orient[ 2] << " "<<m_ref.m_orient[ 3] << endl;
00108     f << m_ref.m_orient[ 4] << " "<< m_ref.m_orient[ 5] << " "<< m_ref.m_orient[ 6] << " "<<m_ref.m_orient[ 7] << endl;
00109     f << m_ref.m_orient[ 8] << " "<< m_ref.m_orient[ 9] << " "<< m_ref.m_orient[10] << " "<<m_ref.m_orient[11] << endl;
00110     f << m_ref.m_orient[12] << " "<< m_ref.m_orient[13] << " "<< m_ref.m_orient[14] << " "<<m_ref.m_orient[15] << endl;
00111     f.close();
00112   }
00113 }
00114 
00115 void GLCamera::Load(string fname){
00116   ifstream f;
00117   f.open(fname.c_str());
00118   if(f.is_open()){
00119     m_ref.Identity();
00120     f >> m_ref.m_origin[0] >>  m_ref.m_origin[1] >>  m_ref.m_origin[2] ;
00121     f >> m_ref.m_orient[ 0] >>  m_ref.m_orient[ 1] >>  m_ref.m_orient[ 2] >> m_ref.m_orient[ 3] ;
00122     f >> m_ref.m_orient[ 4] >>  m_ref.m_orient[ 5] >>  m_ref.m_orient[ 6] >> m_ref.m_orient[ 7] ;
00123     f >> m_ref.m_orient[ 8] >>  m_ref.m_orient[ 9] >>  m_ref.m_orient[10] >> m_ref.m_orient[11] ;
00124     f >> m_ref.m_orient[12] >>  m_ref.m_orient[13] >>  m_ref.m_orient[14] >> m_ref.m_orient[15] ;
00125     f.close();
00126     m_ref.Update();
00127   }
00128 }
 All Data Structures Functions Variables

Generated on Wed Sep 22 16:51:26 2010 for Body_Schema_Learning by  doxygen 1.6.1