main.C

Go to the documentation of this file.
00001 /*
00002  * This file is part of the "Archon" framework.
00003  * (http://files3d.sourceforge.net)
00004  *
00005  * Copyright © 2002 by Kristian Spangsege and Brian Kristiansen.
00006  *
00007  * Permission to use, copy, modify, and distribute this software and
00008  * its documentation under the terms of the GNU General Public License is
00009  * hereby granted. No representations are made about the suitability of
00010  * this software for any purpose. It is provided "as is" without express
00011  * or implied warranty. See the GNU General Public License
00012  * (http://www.gnu.org/copyleft/gpl.html) for more details.
00013  *
00014  * The characters in this file are ISO8859-1 encoded.
00015  *
00016  * The documentation in this file is in "Doxygen" style
00017  * (http://www.doxygen.org).
00018  */
00019 
00034 #include <sys/types.h>
00035 #include <dirent.h>
00036 #include <unistd.h>
00037 #include <fcntl.h>
00038 #include <signal.h>
00039 #include <stropts.h>
00040 
00041 #include <fstream> 
00042 
00043 #include <archon/math/vector.H>
00044 #include <archon/util/thread.H>
00045 #include <archon/util/ref.H>
00046 #include <archon/util/options.H>
00047 #include <archon/util/file.H>
00048 #include <archon/util/polygon_mesher.H>
00049 
00050 #include <archon/x3d/proxy/exception.H>
00051 #include <archon/x3d/proxy/proxy.H>
00052 
00053 using namespace std;
00054 using namespace Archon;
00055 using namespace Archon::Math;
00056 using namespace Archon::Utilities;
00057 using namespace Archon::X3D::Proxy;
00058 
00059 namespace Archon
00060 {
00061   namespace SaiTestApps
00062   {
00063     namespace Textviewer
00064     {
00065       Mutex mutex;
00066       Condition directoryRefresh(mutex);
00067 
00068       bool quit = false;
00069 
00070       void handleClose(Time)
00071       {
00072         quit = true;
00073         directoryRefresh.notifyAll();
00074       }
00075 
00076       void handleBack(Time)
00077       {
00078         directoryRefresh.notifyAll();
00079       }
00080 
00081       void handleNext(Time)
00082       {
00083         directoryRefresh.notifyAll();
00084       }
00085 
00086       class TextPage: public virtual RefObjectBase
00087       {
00088         string textString;
00089         Ref<Application> application;
00090         Ref<Transform> transform;
00091         Ref<X3D::Proxy::Text> text;
00092         Ref<Transform> nextButton;
00093       public:
00094         TextPage(Ref<Application> a) 
00095         {
00096           application = a;
00097 
00098           double width  = 0.210;
00099           double height = 0.297;
00100 
00101           // Create a mesh
00102           vector<Vector3> outline;
00103           outline.push_back(Vector3(0,     0,      0));
00104           outline.push_back(Vector3(width, 0,      0));
00105           outline.push_back(Vector3(width, height, 0));
00106           outline.push_back(Vector3(0,     height, 0));
00107           vector<Vector3> meshVertices;
00108           vector<int> meshPolygons;
00109           meshPolygon(outline, 0.01, meshVertices, meshPolygons);
00110 
00111           // Paper
00112           Ref<Transform> paperTransform = Transform::create(application);
00113 
00114           Ref<Coordinate> coord = Coordinate::create(application);
00115           coord->point = meshVertices;
00116           Ref<IndexedFaceSet> indexedFaceSet = IndexedFaceSet::create(application);
00117           indexedFaceSet->coord = coord;
00118           indexedFaceSet->coordIndex = meshPolygons;
00119           indexedFaceSet->solid = false;
00120           Ref<Shape> paperShape = Shape::create(application);
00121           Ref<Appearance> paperAppearance = Appearance::create(application);
00122           Ref<Material> paperMaterial = Material::create(application);
00123           paperMaterial->diffuseColor = Vector3(0.9, 0.9, 0.9);
00124           paperAppearance->material = paperMaterial;
00125           paperShape->geometry = indexedFaceSet;
00126           paperShape->appearance = paperAppearance;
00127     
00128           paperTransform->children.add(paperShape);
00129 
00130           // Text
00131           Ref<Transform> textTransform = Transform::create(application);
00132           textTransform->translation = Vector3(0.01, height-0.005, 0.001);
00133           textTransform->scale = Vector3(width, height, 1)/60.0;
00134 
00135           Ref<FontStyle> fontStyle = FontStyle::create(application);
00136           vector<string> family;
00137           family.push_back("TYPEWRITER");
00138           fontStyle->family = family;
00139           vector<string> justify;
00140           justify.push_back("BEGIN");
00141           justify.push_back("BEGIN");
00142           fontStyle->justify = justify;
00143 
00144           text = X3D::Proxy::Text::create(application);
00145           text->fontStyle = fontStyle;
00146     
00147           Ref<Shape> textShape = Shape::create(application);
00148           Ref<Appearance> textApp= Appearance::create(application);
00149           Ref<Material> textMaterial = Material::create(application);
00150           textMaterial->diffuseColor = Vector3(0, 0, 0);
00151           textApp->material = textMaterial;
00152           textShape->appearance = textApp;
00153           textShape->geometry = text;
00154           textTransform->children.add(textShape);
00155 
00156           // Buttons
00157           Ref<Transform> closeButton = Transform::create(application);
00158           closeButton->translation = Vector3(width-0.1, height, 0);
00159           closeButton->scale = Vector3(0.01, 0.01, 0.01);
00160           Ref<Shape> b1Shape = Shape::create(application);
00161           Ref<Appearance> b1Appearance = Appearance::create(application);
00162           Ref<Material> b1Material = Material::create(application);
00163           Ref<Box> b1Geometry = Box::create(application);
00164           b1Shape->appearance = b1Appearance;
00165           b1Shape->geometry = b1Geometry;
00166           b1Appearance->material = b1Material;
00167           b1Material->diffuseColor = Vector3(1, 0, 0);
00168           b1Material->transparency = 0.4;
00169           Ref<TouchSensor> b1Touch = TouchSensor::create(application);
00170           b1Touch->touchTime.registerInterest<SFTime>(new FuncCaller<SFTime>(handleClose));
00171           closeButton->children.add(b1Shape);
00172           closeButton->children.add(b1Touch);
00173     
00174           nextButton = Transform::create(application);
00175           nextButton->translation = Vector3(width, height, 0);
00176           nextButton->scale = Vector3(0.01, 0.01, 0.01);
00177           Ref<Shape> b2Shape = Shape::create(application);
00178           Ref<Appearance> b2Appearance = Appearance::create(application);
00179           Ref<Material> b2Material = Material::create(application);
00180           Ref<Sphere> b2Geometry = Sphere::create(application);
00181           b2Shape->appearance = b2Appearance;
00182           b2Shape->geometry = b2Geometry;
00183           b2Appearance->material = b2Material;
00184           b2Material->diffuseColor = Vector3(0, 1, 0);
00185           b2Material->transparency = 0.4;
00186           Ref<TouchSensor> b2Touch = TouchSensor::create(application);
00187           b2Touch->touchTime.registerInterest<SFTime>(new FuncCaller<SFTime>(handleNext));
00188           nextButton->children.add(b2Shape);
00189           nextButton->children.add(b2Touch);
00190     
00191           // Transform
00192           transform = Transform::create(application);
00193           transform->children.add(paperTransform);
00194           transform->children.add(textTransform);
00195           transform->children.add(closeButton);
00196           transform->children.add(nextButton);
00197         }
00198   
00199         Ref<Transform> getTransform()
00200         {
00201           return transform;
00202         }
00203 
00204         Ref<X3D::Proxy::Text> getText()
00205         {
00206           return text;
00207         }
00208   
00209         Ref<Transform> getNextButton()
00210         {
00211           return nextButton;
00212         }
00213 
00214       };
00215 
00216 
00222       int main(int argc, const char *argv[])
00223       {
00224         bool   opt_help              = false;
00225         int    opt_corbaEndpointPort = -1; // Auto assign
00226         string opt_serverName        = "archon";
00227 
00228         Options o;
00229 
00230         o.addSwitch("h", "help", opt_help, true,
00231                     "Describe the parameters");
00232         o.addConfig("p", "corba-endpoint-port", opt_corbaEndpointPort, opt_corbaEndpointPort,
00233                     "The TCP/IP port on which the CORBA Orb listens (-1 for auto assign)",
00234                     Options::wantArg_always);
00235         o.addConfig("n", "server-name", opt_serverName, opt_serverName,
00236                     "The name of the server to connect to",
00237                     Options::wantArg_always);
00238 
00239         if(o.processCommandLine(argc, argv))
00240         {
00241           cerr << "Try --help\n";
00242           return 1;
00243         }
00244 
00245         if(opt_help)
00246         {
00247           cout <<
00248             "Test Application for 3D-Console by Brian Kristiansen & Kristian Spangsege\n"
00249             "\n"
00250             "Synopsis: " << argv[0] << " FILE\n"
00251             "\n"
00252             "Available options:\n";
00253           cout << o.list();
00254           return 0;
00255         }
00256 
00257         // Treat 1st arg as URL of X3D file to load
00258         if(argc < 2)
00259         {
00260           cerr << "Too few arguments\n";
00261           cerr << "Try --help\n";
00262           return 1;
00263         }
00264 
00265         if(argc > 2)
00266         {
00267           cerr << "Too many aguments\n";
00268           cerr << "Try --help\n";
00269           return 1;
00270         }
00271 
00272         string name = argv[1];
00273         std::ifstream source(name.c_str());
00274 
00275         Mutex::Lock l(mutex);
00276 
00277         Ref<Session> session =
00278           Session::create(opt_serverName, opt_corbaEndpointPort);
00279   
00280         Ref<Application> application =
00281           Application::create(session, Uri());
00282   
00283         Ref<Group> rootGroup = Group::create(application);
00284 
00285         bool reachedEOF=false;
00286         string s;
00287 
00288         Ref<TextPage> textPage = new TextPage(application);
00289         Ref<Transform> tr = textPage->getTransform();
00290         Ref<X3D::Proxy::Text> text = textPage->getText();
00291         tr->translation = Vector3(-5, -7, 0);
00292         tr->scale = Vector3(60, 60, 60);
00293         rootGroup->children.add(tr);
00294 
00295         application->setRootGroup(rootGroup);
00296         application->launch();
00297 
00298         vector<string> textLines;
00299 
00300         for(;;)
00301         {
00302           //      session->beginUpdate();
00303           textLines.clear();
00304           for(int x=58; x>0; --x)
00305           {
00306             char ch;
00307             if(!source.get(ch))
00308             {
00309               reachedEOF = true;
00310               s = "";
00311             }
00312             else
00313             {
00314               source.putback(ch);
00315               getline(source, s);
00316               textLines.push_back(s);
00317             }
00318           }
00319           text->string = textLines;
00320           if(reachedEOF)
00321           {
00322             tr->children.remove(textPage->getNextButton());
00323           }
00324           //      session->endUpdate();
00325           directoryRefresh.wait();
00326           if(quit)
00327             return 0;
00328         }
00329         return 0;
00330       }
00331     }
00332   }
00333 }
00334 
00335 int main(int argc, const char *argv[]) throw()
00336 {
00337   using namespace Archon::X3D;
00338 
00339   set_unexpected(Exception::terminal<Proxy::exceptionCatchInfo>);
00340   set_terminate (Exception::terminal<Proxy::exceptionCatchInfo>);
00341 
00342   return Archon::SaiTestApps::Textviewer::main(argc, argv);
00343 }

Generated on Sun Jul 30 22:55:41 2006 for Archon by  doxygen 1.4.4