interreality.org [VOS]
[Home] [About]
[Screenshots]
[Download]
[News]
[Community]
[Documentation] [Manual]
[Bugs & Requests] [Wiki]

/home/tetron/hack/vos/apps/tutorials/vostut8server.cc

Go to the documentation of this file.
00001 /* Eighth VOS tutorial.  Creating your own MetaObjects II
00002 
00003    See vostut8creature.hh for the beginning of this tutorial.
00004 
00005    This file (vostut8server.cc) is released into the public domain.  No
00006    restrictions are placed on its use, distribution or inclusion into
00007    other works.
00008 */
00009 
00010 #include <stdexcept>                    // Has std::exception
00011 #include <string>
00012 #include <vos/metaobjects/a3dl/a3dl.hh> // Defines 3D metaobjects
00013 #include "vostut8creature.hh"           // Defines Creature and Animal metaobjects
00014 
00015 using namespace VUtil;
00016 using namespace VOS;
00017 using namespace std;
00018 
00019 #ifdef WIN32
00020 #define sleep _sleep
00021 #endif
00022 
00023 int main(int, char**)
00024 {
00025     std::cout << "VOS Tutorial 8 Server\n\n";
00026 
00027     Site site;
00028     site.addSiteExtension(new LocalVipSiteExtension());
00029 
00030     // We will set the default site policy for core VOS object operations
00031     // to accept all
00032     site.setDefaultPolicy("core:accept-all,property:accept-all");
00033 
00034 
00035     // Next, create an Animal object. This object will also have the
00036     // "a3dl:object3D.model" type.
00037     vRef<Animal> coyote = site.createVobject2<Animal, A3DL::Model>("coyote");
00038     coyote->setDefaultPolicy("core:accept-all,property:accept-all");
00039 
00040     // Set some properties using the API we put in the Creature and Animal
00041     // classes. Since Animal derives from Creature we could meta_cast
00042     // coyote to a Creature first if we wanted to, it's the same either way.
00043     coyote->setGenus("Canis");
00044     coyote->setSpecies("latrans");
00045     coyote->setCommonName("Coyote");
00046     meta_cast<Creature>(coyote)->setDescription("Carnivore, member of the dog "
00047         "family. Coat long, course, grey and black. Long, narrow muzzle. Bushy "
00048         "tail, black tipped. Runs up to 40 miles per hour.");
00049     coyote->setLength(120);
00050     coyote->setTailLength(33);
00051 
00052 
00053     vRef<Animal> mouse = site.createVobject2<Animal, A3DL::Model>("mouse");
00054     mouse->setDefaultPolicy("core:accept-all,property:accept-all");
00055     mouse->setGenus("Peromyscus");
00056     mouse->setSpecies("leucopus");
00057     mouse->setCommonName("White-footed mouse");
00058     mouse->setDescription("Red-brown. Large eyes and ears. Tail long and "
00059         "finely haired. Nocturnal year round.");
00060     mouse->setLength(17.8);
00061     mouse->setTailLength(7.62);
00062 
00063 
00064     // A mushroom is not an animal, but we'll call it a "creature".
00065     vRef<Creature> mushroom = site.createVobject<Creature>("mushroom");
00066     mushroom->setDefaultPolicy("core:accept-all,property:accept-all");
00067     mushroom->setGenus("Amanita");
00068     mushroom->setSpecies("brunnescens");
00069     mushroom->setCommonName("Cleft-foot Amanita");
00070     mushroom->setDescription("Wide convex cap, white. Gills: free, close, "
00071         "broad.  Universal white veil. Spores 7-10 microns. Possibly poisonous.");
00072     mushroom->setLength(10);
00073 
00074     // If you want to add a new MetaObject after creating a local Vobject,
00075     // here's how.
00076     mushroom->getVobjectBase()->addMetaObjectExtension(new A3DL::Model(mushroom->getVobjectBase()));
00077 
00078     // Coyotes might eat mice.
00079     coyote->addPrey(mouse);
00080     mouse->addPredator(coyote);
00081 
00082     // Our objects are also 3D objects, lets set up those properties.
00083     meta_cast<A3DL::Object3D>(coyote)->setPosition(6, 0, 6);
00084     try {
00085         meta_cast<A3DL::Model>(coyote)->setModelToFile("vostut8_coyote.3ds", "model/x-3ds");
00086     } catch(exception& e) {
00087         LOG("vostut8server", 0, "Error using model file \"vostut8_coyote.3ds\": " << e.what());
00088     }
00089 
00090     meta_cast<A3DL::Object3D>(mouse)->setPosition(3, 0, 0);
00091     try {
00092         meta_cast<A3DL::Model>(mouse)->setModelToFile("vostut8_mouse.3ds", "model/x-3ds");
00093     } catch(exception& e) {
00094         LOG("vostut8server", 0, "Error using model file \"vostut8_mouse.3ds\": " << e.what());
00095     }
00096 
00097     meta_cast<A3DL::Object3D>(mushroom)->setPosition(3, 0, 3);
00098     try {
00099         meta_cast<A3DL::Model>(mushroom)->setModelToFile("vostut8_mushroom.3ds", "model/x-3ds");
00100     } catch(exception& e) {
00101         LOG("vostut8server", 0, "Error using model file \"vostut8_mushroom.3ds\": " << e.what());
00102     }
00103 
00104 
00105     // We'll put them all in a sector so you can look at them in 3D
00106     vRef<A3DL::Sector> world = site.createVobject<A3DL::Sector>("world", "core:accept-all,property:accept-all");
00107     world->setAmbientLightColor(0.5, 0.5, 0.5);
00108     world->insertChild(-1, "coyote", coyote);
00109     world->insertChild(-1, "mouse", mouse);
00110     world->insertChild(-1, "mushroom", mushroom);
00111 
00112     LOG("vostut8server", 2, "Ok, running. Try \"mesh " << world->getURLstr() << "\"");
00113     while(true) sleep(1000);
00114 }
00115