TomGineThread.cpp
Go to the documentation of this file.
00001 
00002 #include <blort/ThreadObject/TomGineThread.h>
00003 #include <blort/TomGine/tgEngine.h>
00004 #include <blort/TomGine/tgShapeCreator.h>
00005 #include <stdexcept>
00006 
00007 using namespace TomGine;
00008 using namespace std;
00009 
00010 CTomGineThread::CTomGineThread(int width, int height)
00011 {
00012         m_mutex.Lock();
00013                 m_quit = false;
00014                 m_use_campars = false;
00015                 m_width = width;
00016                 m_height = height;
00017         m_mutex.Unlock();
00018 }
00019 
00020 CTomGineThread::CTomGineThread(int width, int height, const TomGine::tgCamera::Parameter& tgCamParams)
00021 {
00022         m_mutex.Lock();
00023                 m_quit = false;
00024                 m_use_campars = true;
00025                 m_width = width;
00026                 m_height = height;
00027                 m_camPars = tgCamParams;
00028         m_mutex.Unlock();
00029 }
00030 
00031 CTomGineThread::~CTomGineThread()
00032 {
00033         m_mutex.Lock();
00034                 m_quit = true;
00035         m_mutex.Unlock();
00036         
00037         m_running.Wait();
00038 }
00039 
00040 void CTomGineThread::SetModel(const TomGine::tgModel& model)
00041 {
00042         m_mutex.Lock();
00043         
00044                 m_max_vertex_length = 0.0f;
00045                 float l;
00046                 for(unsigned int i=0; i<model.m_vertices.size(); i++){
00047                         l = model.m_vertices[i].pos.length();
00048                         
00049                         if( l > m_max_vertex_length )
00050                                 m_max_vertex_length = l;
00051                 }
00052                 
00053                 m_model = model;
00054         m_mutex.Unlock();
00055 }
00056 
00057 void CTomGineThread::SetPose(const TomGine::tgPose& pose)
00058 {
00059         m_mutex.Lock();
00060                 m_pose = pose;
00061         m_mutex.Unlock();
00062 }
00063 
00064 void CTomGineThread::AddSifts(const std::vector<blortRecognizer::Siftex>& sl)
00065 {
00066         m_mutex.Lock();
00067                 m_lastsiftexlist = sl;
00068                 for(unsigned i=0; i<sl.size(); i++)
00069                   m_siftexlist.push_back(sl[i]);
00070         m_mutex.Unlock();
00071         
00072         double pos[3] = {0,0,0};
00073         double normal[3] = {0,0,0};
00074         double view[3] = {0,0,0};
00075         unsigned sc = sl.size();
00076         
00077         for(unsigned i=0; i<sc; i++){
00078           pos[0] += sl[i].pos.x;
00079           pos[1] += sl[i].pos.y;
00080           pos[2] += sl[i].pos.z;
00081           normal[0] += sl[i].normal.x;
00082           normal[1] += sl[i].normal.y;
00083           normal[2] += sl[i].normal.z;
00084           view[0] += sl[i].viewray.x;
00085           view[1] += sl[i].viewray.y;
00086           view[2] += sl[i].viewray.z;
00087         }
00088         
00089         vec3 vPos = vec3(float(pos[0]/sc), float(pos[1]/sc), float(pos[2]/sc)); 
00090         vec3 vNormal = vec3(float(normal[0]/sc), float(normal[1]/sc), float(normal[2]/sc)); 
00091         vec3 vView = vec3(float(view[0]/sc), float(view[1]/sc), float(view[2]/sc));
00092         
00093         vec3 p = vPos - vView;
00094 //      vec3 p = sl[0].pos - sl[0].viewray;
00095         
00096         vec3 f = vView; f.normalize();
00097         vec3 u = vec3(0,0,1);
00098         vec3 s;
00099         s.cross(u,f); s.normalize();
00100         u.cross(f,s); u.normalize();
00101 
00102         float rad = 0.0f;
00103         for(unsigned i=0; i<sc; i++){
00104                 vec3 d = vPos - sl[i].pos;
00105                 float l = d.length();
00106                 if(rad < l)
00107                   rad=l;
00108         }
00109         m_viewscale.push_back(vec3(rad, rad, vView.length()));
00110 
00111         float fR[9] = { s.x, s.y, s.z,
00112                                         u.x, u.y, u.z,
00113                                         f.x, f.y, f.z};
00114         mat3 R = mat3(fR);
00115         TomGine::tgPose pose;
00116         pose.SetPose(R, p);
00117         m_viewlist.push_back(pose);
00118 }
00119 
00120 void CTomGineThread::AddView(const TomGine::tgPose& view)
00121 {
00122 //      m_mutex.Lock();
00123 //              m_viewlist.push_back(view);
00124 //      m_mutex.Unlock();
00125 }
00126 
00127 BOOL CTomGineThread::OnTask()
00128 {
00129         m_running.Lock();
00130         
00131         m_mutex.Lock();
00132                 tgEngine m_engine(m_width, m_height, 5.0f, 0.01f, "TomGine", true);
00133                 
00134                 if(m_use_campars)
00135                 {
00136                         TomGine::tgCamera cam;
00137                         cam.Load(m_camPars);
00138                         m_engine.SetCamera(cam);
00139                         m_engine.UpdateCameraViews(cam);
00140                 }
00141         m_mutex.Unlock();
00142         
00143         tgMaterial matBlueBlend;
00144         matBlueBlend.Color(0.0f, 0.0f, 1.0f, 0.5f);
00145         m_cone.m_material = matBlueBlend;
00146         
00147         tgShapeCreator m_shape_creator;
00148         m_shape_creator.CreateSphere(m_sphere, 0.1, 5, ICOSAHEDRON);
00149         m_shape_creator.CreateCone(m_cone, 1.0, 1.0, 16, 1, false);
00150 //      tgModel &model, float radius, float height, int slices, int stacks, bool closed
00151         
00152         glClearColor(1,1,1,0);
00153         
00154         while(!m_quit)
00155         {
00156                 m_mutex.Lock();
00157                         
00158                         // Draw model
00159                         m_pose.Activate();
00160                                 m_model.DrawFaces();
00161                                 
00162 //                              for(unsigned i=0; i<m_viewlist.size(); i++)
00163 //                              {
00164 //                                      m_viewlist[i].Activate();
00165 //                                      m_cone.DrawFaces();
00166 //                              }
00167                         
00168                         // TODO test if Siftex is visible
00169                         
00170                         
00171                         // Draw points
00172                         glColor3f(0.0f,1.0f,0.0f);
00173                         glDisable(GL_LIGHTING);
00174                         glPointSize(2);
00175                         glBegin(GL_POINTS);
00176                         for(unsigned int i=0; i<m_siftexlist.size(); i++)
00177                         {
00178                                 glVertex3f(m_siftexlist[i].pos.x, m_siftexlist[i].pos.y, m_siftexlist[i].pos.z);
00179                         }
00180                         glEnd();
00181                         glEnable(GL_LIGHTING);
00182                         
00183                         // Draw normals
00184                         glColor3f(0.0f, 0.0f, 1.0f);
00185                         glDisable(GL_LIGHTING);
00186                         glBegin(GL_LINES);
00187                         for(unsigned int i=0; i<m_lastsiftexlist.size(); i++)
00188                         {
00189                                 glVertex3f(m_lastsiftexlist[i].pos.x, m_lastsiftexlist[i].pos.y, m_lastsiftexlist[i].pos.z);
00190                                 glVertex3f(     m_lastsiftexlist[i].pos.x - m_lastsiftexlist[i].viewray.x,
00191                                                                                 m_lastsiftexlist[i].pos.y - m_lastsiftexlist[i].viewray.y,
00192                                                                                 m_lastsiftexlist[i].pos.z - m_lastsiftexlist[i].viewray.z);
00193                         }
00194                         glEnd();
00195                         glEnable(GL_LIGHTING);
00196                         
00197                         for(unsigned i=0; i<m_viewlist.size(); i++)
00198                         {
00199                           m_viewlist[i].Activate();
00200                           
00201                           glPushMatrix();
00202                           glScalef(m_viewscale[i].x,m_viewscale[i].y,m_viewscale[i].z);
00203                           glEnable(GL_BLEND);
00204                           glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00205                           m_cone.DrawFaces();
00206                           glDisable(GL_BLEND);
00207                           glPopMatrix();
00208                           
00209                           m_viewlist[i].Deactivate();
00210                         }
00211                         
00212                         m_pose.Deactivate();
00213                 m_mutex.Unlock();
00214                 
00215                 Sleep(10);
00216                 
00217                 m_engine.Update();
00218         }
00219 
00220         m_running.Unlock();
00221         return TRUE;
00222 }
00223 
00224 
00225 
00226 


blort
Author(s): Michael Zillich, Thomas Mörwald, Johann Prankl, Andreas Richtsfeld, Bence Magyar (ROS version)
autogenerated on Thu Jan 2 2014 11:38:26