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

Engine.cc

Go to the documentation of this file.
00001 /*
00002  * Engine class
00003  * by hackervalley@free.fr
00004  * http://hackervalley.free.fr
00005  * June 2004
00006  * this program is licensed under the GPL.
00007  */
00008 
00009 #include "Engine.hh"
00010 #include "Container.hh"
00011 #include "Tokenizer.hh"
00012 
00013 #include <iostream>
00014 
00015 extern "C"
00016 {
00017 #include <GL/gl.h>
00018 #include <GL/glu.h>
00019 }
00020 
00021 using namespace FE;
00022 using namespace std;
00023 
00024 Engine::Engine ()
00025 {
00026         x0 = y0 = x1 = y1 = 0;
00027         StateDrawRectangle = false;
00028         StateMove = false;
00029 }
00030 
00031 Engine::~Engine ()
00032 {
00033         VertexSet.clear ();
00034 }
00035 
00036 void
00037 Engine::OpenFile (char *path)
00038 {
00039         char buffer[256];
00040         string inputstring;
00041 
00042         ifstream readfile (path, ios::in);
00043         if (!readfile.is_open ())
00044         {
00045                 cout << "Error opening file" << endl;
00046         }
00047         else
00048         {
00049                 /* Clear all container */
00050                 VertexSet.clear();
00051                 
00052                 /* Tokenize file */
00053                 /* Line per Line */
00054                 while (!readfile.eof ())
00055                 {
00056                         /* getline returns buff on success, and the NULL pointer on end-of-file */
00057                         if (readfile.getline (buffer, 256) != NULL)
00058                         {
00059                                 inputstring.clear ();
00060                                 inputstring = buffer;
00061 
00062                                 /* Tokenize line */
00063                                 vector < string > Result;
00064                                 Result.clear ();
00065                                 CTokenizer < CSeparator >::Tokenize (Result,
00066                                                                      inputstring,
00067                                                                      CSeparator
00068                                                                      ("() ={},;"));
00069 
00070                                 /* Create Objects */
00071                                 /* Point(1) = {1,1,1,1}; */
00072                                 if (Result[0] == "Point")
00073                                 {
00074                                         Vertex <> *p1;
00075                                         p1 = new Vertex <>;
00076                                         p1->Identifier = atoi(Result[1].c_str());
00077                                         p1->x = atof(Result[2].c_str());
00078                                         p1->y = atof (Result[3].
00079                                                                c_str ());
00080                                         p1->z = atof (Result[4].
00081                                                                c_str ());
00082                                         p1->lc = atof(Result[5].c_str());
00083 
00084                                         cout << "Vertex create " << p1->Identifier << " " << p1->
00085                                                 x << " " << p1->
00086                                                 y << " " << p1->z << " " << p1-> lc << endl;
00087                                         VertexSet.push_back (p1);
00088 
00089                                         cout << " Size of VertexSet " <<
00090                                                 VertexSet.size () << endl;
00091                                 }
00092                         }
00093                 }
00094 
00095                 cout << "Read File:" << WorkFilename << endl;
00096         }
00097         readfile.close ();
00098 }
00099 
00100 void Engine::SaveFile(char *path)
00101 {
00102         ofstream writefile (path, ios::out | ios::trunc);
00103         
00104         if (!writefile.is_open ())
00105         {
00106                 cout << "Error opening file" << endl;
00107         }
00108         else
00109         {
00110                 /* Store Point(1) = {1,1,1,1}; */
00111                 for (VertexSetIterator = VertexSet.begin ();
00112              VertexSetIterator != VertexSet.end (); VertexSetIterator++)
00113         {
00114                 writefile << "Point(" << (*VertexSetIterator)->Identifier << ") = {"<< (*VertexSetIterator)->x << "," << (*VertexSetIterator)->y << "," << (*VertexSetIterator)->z << "," << (*VertexSetIterator)->lc << "};"<< endl; 
00115         }
00116 }
00117         writefile.close();
00118 }
00119 
00120 void
00121 Engine::draw ()
00122 {
00123         glBegin (GL_POINTS);
00124         for (VertexSetIterator = VertexSet.begin ();
00125              VertexSetIterator != VertexSet.end (); VertexSetIterator++)
00126         {
00127                 if ((*VertexSetIterator)->IsSelect)
00128                         glColor3f (1.0, 0.0, 0.0);
00129 
00130                 else
00131                         glColor3f (1.0, 1.0, 1.0);
00132 
00133                 glVertex3d ((*VertexSetIterator)->x, (*VertexSetIterator)->y,
00134                             (*VertexSetIterator)->z);
00135 
00136         }
00137         glEnd ();
00138 
00139 }
00140 
00141 void
00142 Engine::drawRectangle (int &x0, int &y0, int &x1, int &y1)
00143 {
00144         GLint viewport[4];
00145         GLdouble mvmatrix[16], projmatrix[16];
00146         GLdouble wx0, wy0, wz0; /* returned world x0, y0, z0 coords, first rectangle corner  */
00147         GLdouble wx1, wy1, wz1; /* returned world x1, y1, z1 coords, second rectangle corner */
00148         GLdouble winx, winy, winz;      /* retruned window coords */
00149 
00150         glGetIntegerv (GL_VIEWPORT, viewport);
00151         glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);
00152         glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);
00153 
00154         /* Project Origin (0,0,0) to have winz */
00155         gluProject (0, 0, 0, mvmatrix, projmatrix, viewport, &winx, &winy,
00156                     &winz);
00157 
00158         /* z Mouse is winz */
00159         gluUnProject ((GLdouble) x0, (GLdouble) y0, winz,
00160                       mvmatrix, projmatrix, viewport, &wx0, &wy0, &wz0);
00161 
00162         /* z Mouse is winz */
00163         gluUnProject ((GLdouble) x1, (GLdouble) y1, winz,
00164                       mvmatrix, projmatrix, viewport, &wx1, &wy1, &wz1);
00165 
00166         /* Draw OpenGL rectangle */
00167         /* Remarks OpenGL Axis Y and Fltk Window are opposite */
00168         glColor3d (0.7, 0.7, 0.7);
00169         glBegin (GL_LINE_LOOP);
00170         glVertex3d (wx0, -wy0, wz0);
00171         glVertex3d (wx1, -wy0, wz0);
00172         glVertex3d (wx1, -wy1, wz0);
00173         glVertex3d (wx0, -wy1, wz0);
00174         glEnd ();
00175 
00176 }
00177 
00178 void
00179 Engine::DrawMoveLine (int &x0, int &y0, int &x1, int &y1)
00180 {
00181         GLint viewport[4];
00182         GLdouble mvmatrix[16], projmatrix[16];
00183         GLdouble wx0, wy0, wz0; /* returned world x0, y0, z0 coords, first rectangle corner  */
00184         GLdouble wx1, wy1, wz1; /* returned world x1, y1, z1 coords, second rectangle corner */
00185         GLdouble winx, winy, winz;      /* retruned window coords */
00186 
00187         glGetIntegerv (GL_VIEWPORT, viewport);
00188         glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);
00189         glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);
00190 
00191         /* Project Origin (0,0,0) to have winz */
00192         gluProject (0, 0, 0, mvmatrix, projmatrix, viewport, &winx, &winy,
00193                     &winz);
00194 
00195         /* z Mouse is winz */
00196         gluUnProject ((GLdouble) x0, (GLdouble) y0, winz,
00197                       mvmatrix, projmatrix, viewport, &wx0, &wy0, &wz0);
00198 
00199         /* z Mouse is winz */
00200         gluUnProject ((GLdouble) x1, (GLdouble) y1, winz,
00201                       mvmatrix, projmatrix, viewport, &wx1, &wy1, &wz1);
00202 
00203         /* Draw OpenGL rectangle */
00204         /* Remarks OpenGL Axis Y and Fltk Window are opposite */
00205         glColor3d (0.7, 0.7, 0.7);
00206         glBegin (GL_LINES);
00207         glVertex3d (wx0, -wy0, wz0);
00208         glVertex3d (wx1, -wy1, wz0);
00209         glEnd ();
00210 
00211 }
00212 
00213 void
00214 Engine::createvertex (int &x, int &y)
00215 {
00216         GLint viewport[4];
00217         GLdouble mvmatrix[16], projmatrix[16];
00218         GLdouble wx, wy, wz;    /* returned world x, y, z coords  */
00219         GLdouble winx, winy, winz;      /* returned window coords */
00220 
00221         glGetIntegerv (GL_VIEWPORT, viewport);
00222         glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);
00223         glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);
00224 
00225         /* Project Origin (0,0,0) to have winz */
00226         gluProject (0, 0, 0, mvmatrix, projmatrix, viewport, &winx, &winy,
00227                     &winz);
00228 
00229         /* z Mouse is winz */
00230         gluUnProject ((GLdouble) x, (GLdouble) y, winz,
00231                       mvmatrix, projmatrix, viewport, &wx, &wy, &wz);
00232         
00233         /* Define Identifier */
00234         int id=0;
00235 
00236         if(VertexSet.size()!=0)
00237         {
00238                 id = (VertexSet.back()->Identifier) + 1;
00239         }
00240         
00241         /* Define Vertex */
00242         Vertex <> *p1;
00243         p1 = new Vertex <>;
00244         p1->Identifier = id;
00245         p1->x = wx;
00246         p1->y = -wy;
00247         p1->z = wz;
00248         p1->lc = 0.1;
00249 
00250         cout << "Vertex create " << p1->x << " " << p1->y << " " << p1->
00251                 z << endl;
00252 
00253         VertexSet.push_back (p1);
00254 
00255         cout << " Size of VertexSet " << VertexSet.size () << endl;
00256 
00257 }
00258 
00259 void
00260 Engine::selectvertex (int &x, int &y)
00261 {
00262         GLdouble zone = 5;
00263         GLint viewport[4];
00264         GLdouble mvmatrix[16], projmatrix[16];
00265         GLdouble winx, winy, winz;      /*  returned window x, y, z coords  */
00266 
00267         glGetIntegerv (GL_VIEWPORT, viewport);
00268         glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);
00269         glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);
00270 
00271         for (VertexSetIterator = VertexSet.begin ();
00272              VertexSetIterator != VertexSet.end (); VertexSetIterator++)
00273         {
00274                 /* Remarks OpenGL Axis Y and Fltk Window are opposite */
00275                 gluProject ((GLdouble) (*VertexSetIterator)->x,
00276                             -((GLdouble) (*VertexSetIterator)->y),
00277                             (GLdouble) (*VertexSetIterator)->z, mvmatrix,
00278                             projmatrix, viewport, &winx, &winy, &winz);
00279 
00280                 if (winx > (x - zone) && winx < (x + zone)
00281                     && winy > (y - zone) && winy < (y + zone))
00282                 {
00283                         // Change State of vertex
00284                         (*VertexSetIterator)->IsSelect =
00285                                 !(*VertexSetIterator)->IsSelect;
00286 
00287                         cout << "Vertex state " << (*VertexSetIterator)->
00288                                 IsSelect << endl;
00289                 }
00290         }
00291 }
00292 
00293 void
00294 Engine::RectangleSelectVertex (int &x0, int &y0, int &x1, int &y1)
00295 {
00296         /* Unused GLdouble zone = 5; */
00297         GLint viewport[4];
00298         GLdouble mvmatrix[16], projmatrix[16];
00299         GLdouble winx, winy, winz;      /*  returned window x, y, z coords  */
00300 
00301         glGetIntegerv (GL_VIEWPORT, viewport);
00302         glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);
00303         glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);
00304 
00305         for (VertexSetIterator = VertexSet.begin ();
00306              VertexSetIterator != VertexSet.end (); VertexSetIterator++)
00307         {
00308                 /* Remarks OpenGL Axis Y and Fltk Window are opposite */
00309                 gluProject ((GLdouble) (*VertexSetIterator)->x,
00310                             -((GLdouble) (*VertexSetIterator)->y),
00311                             (GLdouble) (*VertexSetIterator)->z, mvmatrix,
00312                             projmatrix, viewport, &winx, &winy, &winz);
00313 
00314                 if ((winx > x0 && winx < x1 && winy > y0 && winy < y1)
00315                     || (winx > x1 && winx < x0 && winy > y1 && winy < y0)
00316                     || (winx > x0 && winx < x1 && winy > y1 && winy < y0)
00317                     || (winx > x1 && winx < x0 && winy > y0 && winy < y1))
00318                 {
00319                         // Change State of vertex
00320                         (*VertexSetIterator)->IsSelect =
00321                                 !(*VertexSetIterator)->IsSelect;
00322 
00323                         cout << "Vertex state " << (*VertexSetIterator)->
00324                                 IsSelect << endl;
00325                 }
00326 
00327         }
00328 }
00329 
00330 
00331 void
00332 Engine::DeleteSelectVertex ()
00333 {
00334         /* Create a container of iterator from vertex selected */
00335 
00336         list < list < Vertex <> *>::iterator > tmpVertexSetIterator;
00337         list < list <
00338                 Vertex <> *>::iterator >::
00339                 iterator tmpVertexSetIteratorIterator;
00340 
00341         for (VertexSetIterator = VertexSet.begin ();
00342              VertexSetIterator != VertexSet.end (); VertexSetIterator++)
00343         {
00344                 if ((*VertexSetIterator)->IsSelect == true)
00345                 {
00346                         tmpVertexSetIterator.push_back (VertexSetIterator);
00347                         cout << " Address " << *VertexSetIterator << endl;
00348                 }
00349         }
00350 
00351         /* Erase vertex selected */
00352 
00353         for (tmpVertexSetIteratorIterator = tmpVertexSetIterator.begin ();
00354              tmpVertexSetIteratorIterator != tmpVertexSetIterator.end ();
00355              tmpVertexSetIteratorIterator++)
00356         {
00357                 VertexSet.erase (*tmpVertexSetIteratorIterator);
00358 
00359                 cout << " Size of VertexSet " << VertexSet.size () << endl;
00360         }
00361 
00362 }
00363 
00364 void
00365 Engine::MoveSelected (int &x0, int &y0, int &x1, int &y1)
00366 {
00367         /* Define vector of translation */
00368         GLint viewport[4];
00369         GLdouble mvmatrix[16], projmatrix[16];
00370         GLdouble wx0, wy0, wz0; /* returned world x0, y0, z0 coords, first rectangle corner  */
00371         GLdouble wx1, wy1, wz1; /* returned world x1, y1, z1 coords, second rectangle corner */
00372         GLdouble winx, winy, winz;      /* retruned window coords */
00373 
00374         glGetIntegerv (GL_VIEWPORT, viewport);
00375         glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);
00376         glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);
00377 
00378         /* Project Origin (0,0,0) to have winz */
00379         gluProject (0, 0, 0, mvmatrix, projmatrix, viewport, &winx, &winy,
00380                     &winz);
00381 
00382         /* z Mouse is winz */
00383         gluUnProject ((GLdouble) x0, (GLdouble) y0, winz,
00384                       mvmatrix, projmatrix, viewport, &wx0, &wy0, &wz0);
00385 
00386         /* z Mouse is winz */
00387         gluUnProject ((GLdouble) x1, (GLdouble) y1, winz,
00388                       mvmatrix, projmatrix, viewport, &wx1, &wy1, &wz1);
00389 
00390         /* Translate selected */
00391         for (VertexSetIterator = VertexSet.begin ();
00392              VertexSetIterator != VertexSet.end (); VertexSetIterator++)
00393         {
00394                 if ((*VertexSetIterator)->IsSelect == true)
00395                 {
00396                         (*VertexSetIterator)->x += wx1 - wx0;
00397                         (*VertexSetIterator)->y -= wy1 - wy0;
00398                 }
00399         }
00400 
00401 }

Generated on Sat Aug 7 18:49:02 2004 for FE by doxygen 1.3.6-20040222