GLTools.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 using namespace std;
00026 
00027 #include "GLTools.h"
00028 
00029 
00030 float  GLTools::m_red     = 1.0;
00031 float  GLTools::m_green   = 1.0;
00032 float  GLTools::m_blue    = 1.0;
00033 
00034 bool   GLTools::m_outline = true;
00035 bool   GLTools::m_solid   = true;
00036 
00037 void GLTools::SetColor(float r, float g, float b){
00038   m_red = r;  m_green = g;   m_blue = b;
00039 }
00040 
00041 void GLTools::DrawOutline(bool state){
00042   m_outline = state;
00043 }
00044 void GLTools::DrawSolid(bool state){
00045   m_solid = state;
00046 }
00047 
00048 void GLTools::DrawArc(float min, float max, CMatrix4_t *ref){
00049   if((!m_outline) && (!m_solid))
00050     return;
00051   
00052   glPushMatrix();
00053   if(ref!=NULL)
00054     glMultMatrixf(*ref);
00055 
00056   float stepSize  = pi/32.0;
00057   int   step      = (int)ceil((max - min)*deg2rad / stepSize);
00058   int   i;
00059   float currAngle = min*deg2rad;
00060 
00061   if(m_solid){
00062     glColor3f(m_red, m_green, m_blue);
00063     glBegin(GL_TRIANGLE_FAN);
00064                 glVertex3f( 0.0f, 0.0f,  0.0f);
00065       for(i=0;i<step;i++){
00066         glVertex3f(cos(currAngle), sin(currAngle), 0.0f);
00067         currAngle += stepSize;
00068       }
00069       glVertex3f(cos(max*deg2rad),  sin(max*deg2rad), 0.0f);
00070           glEnd();
00071   }
00072 
00073   if(m_outline){
00074     glScalef(1.001,1.001,1.001);
00075     currAngle = min*deg2rad;
00076     glColor3f(0.0,0.0,0.0);
00077     glBegin(GL_LINE_LOOP);
00078                 glVertex3f( 0.0f, 0.0f,  0.0f);
00079       for(i=0;i<step;i++){
00080         glVertex3f(cos(currAngle),  sin(currAngle), 0.0f);
00081         currAngle += stepSize;
00082       }
00083       glVertex3f(cos(max*deg2rad),  sin(max*deg2rad), 0.0f);
00084           glEnd();  
00085   }
00086 
00087   glPopMatrix();
00088 }
00089 
00090 
00091 void GLTools::Draw3DArc(CVector3_List_t * vl, CMatrix4_t *ref){
00092   if((!m_outline) && (!m_solid))
00093     return;
00094   
00095   glPushMatrix();
00096   if(ref!=NULL)
00097     glMultMatrixf(*ref);
00098 
00099 
00100   int   step      = (int)(*vl).size();
00101   int   i;
00102 
00103   if(m_solid){
00104     glColor3f(m_red, m_green, m_blue);
00105     glBegin(GL_TRIANGLE_FAN);
00106                 glVertex3f( 0.0f, 0.0f,  0.0f);
00107       for(i=0;i<step;i++){ 
00108         glVertex3f(((*vl)[i]).m_Vector[0],((*vl)[i]).m_Vector[1],((*vl)[i]).m_Vector[2]);
00109       }
00110           glEnd();
00111   }
00112 
00113   if(m_outline){
00114     glScalef(1.001,1.001,1.001);
00115     glColor3f(0.0,0.0,0.0);
00116     glBegin(GL_LINE_STRIP);
00117                 //glVertex3f( 0.0f, 0.0f,  0.0f);
00118       for(i=0;i<step;i++){
00119         glVertex3f(((*vl)[i]).m_Vector[0],((*vl)[i]).m_Vector[1],((*vl)[i]).m_Vector[2]);
00120       }
00121           glEnd();  
00122   }
00123 
00124   glPopMatrix();
00125 }
00126 
00127 void GLTools::DrawVector(CVector3_t *v, CMatrix4_t *ref,float width){
00128   if((!m_outline) && (!m_solid))
00129     return;
00130 
00131   CVector3_t vec;
00132   v_normalize((*v),vec);
00133   float norm = sqrt(v_dot((*v),(*v)));
00134 
00135   float orie = atan2(vec[0],vec[2]);
00136   float elev = asin(vec[1]);
00137 
00138   glPushMatrix();
00139   if(ref!=NULL)
00140     glMultMatrixf(*ref);
00141         
00142   glRotatef(orie*rad2deg,0.0,1.0,0.0);
00143   glRotatef(-elev*rad2deg,1.0,0.0,0.0);
00144 
00145   glScalef(width,width,norm);
00146 
00147   glPushMatrix();
00148   glTranslatef(0.0,0.0,0.5);//0.5 0.25
00149   glScalef(0.1,0.1,1.0);
00150   if(m_solid){
00151     glColor3f(m_red, m_green, m_blue);
00152     glutSolidCube(1.0);//1.0 0.5
00153   }
00154   if(m_outline){
00155     glColor3f(0.0,0.0,0.0);
00156     glScalef(1.001,1.001,1.001);
00157     glutWireCube(1.0);//1.0 0.5
00158   }
00159   glPopMatrix();
00160   //cone
00161 #ifdef WITH_CONE
00162   glTranslatef(0.0,0.0,0.5);
00163   glScalef(0.1,0.1,1.0);
00164   if(m_solid){
00165     glColor3f(m_red, m_green, m_blue);
00166     glutSolidCone(1.0,0.5,4,1);
00167   }
00168   if(m_outline){
00169     glColor3f(0.0,0.0,0.0);
00170     glScalef(1.001,1.001,1.001);
00171     glutWireCone(1.0,0.5,4,1);
00172   }  
00173 #endif
00174   glPopMatrix();
00175 }
00176 
00177 void GLTools::DrawPlane(CVector3_t *v, CMatrix4_t *ref){
00178   if((!m_outline) && (!m_solid))
00179     return;
00180   
00181   CVector3_t vec;
00182   v_normalize((*v),vec);
00183   float norm = sqrt(v_dot((*v),(*v)));
00184 
00185   float orie = atan2(vec[0],vec[2]);
00186   float elev = asin(vec[1]);
00187 
00188   glPushMatrix();
00189   if(ref!=NULL)
00190     glMultMatrixf(*ref);
00191         
00192   glRotatef(orie*rad2deg,0.0,1.0,0.0);
00193   glRotatef(-elev*rad2deg,1.0,0.0,0.0);
00194 
00195 
00196   glScalef(norm,norm,1.0);
00197   if(m_solid){
00198     glColor3f(m_red, m_green, m_blue);
00199     glBegin(GL_QUADS);
00200                   glVertex3f(-1.0f,-1.0f,  0.0f);
00201                   glVertex3f(-1.0f, 1.0f,  0.0f);
00202                   glVertex3f( 1.0f, 1.0f,  0.0f);
00203                   glVertex3f( 1.0f,-1.0f,  0.0f);
00204           glEnd();
00205   }
00206   if(m_outline){
00207           glScalef(1.001,1.001,1.0);
00208           glColor3f(0.0f, 0.0f, 0.0f);
00209           glBegin(GL_LINE_STRIP);
00210                 glVertex3f(-1.0f,-1.0f,  0.0f);
00211                   glVertex3f(-1.0f, 1.0f,  0.0f);
00212                   glVertex3f( 1.0f, 1.0f,  0.0f);
00213                   glVertex3f( 1.0f,-1.0f,  0.0f);
00214                   glVertex3f(-1.0f,-1.0f,  0.0f);
00215           glEnd();
00216   }
00217   glPopMatrix();
00218 }
00219 
00220 void GLTools::DisplayText(int x, int y, const char * text){
00221   int index = 0;
00222   int pos   = x;
00223   glColor3f(1.0f,1.0f,1.0f);
00224   while(text[index]!=0){
00225     glRasterPos2f(pos,y);
00226     glutBitmapCharacter   (GLUT_BITMAP_8_BY_13,text[index]);
00227     pos += 8;//glutBitmapWidth(GLUT_BITMAP_8_BY_13,text[index]);
00228     index++;
00229   }
00230 }
 All Data Structures Functions Variables

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