glutgraphics.cc
Go to the documentation of this file.
00001 /*
00002  *  Player - One Hell of a Robot Server
00003  *  glutgraphics.cc: GLUT-based graphics3d + 2d driver
00004  *  Copyright (C) 2007  
00005  *     Brian Gerkey, Richard Vaughan
00006  *                      
00007  *  This program is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU General Public License as published by
00009  *  the Free Software Foundation; either version 2 of the License, or
00010  *  (at your option) any later version.
00011  *
00012  *  This program is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *  GNU General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU General Public License
00018  *  along with this program; if not, write to the Free Software
00019  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  *
00021  */
00022 
00023 /*
00024  * A simple example of how to write a driver that will be built as a
00025  * shared object.
00026  */
00027 
00028 // ONLY if you need something that was #define'd as a result of configure 
00029 // (e.g., HAVE_CFMAKERAW), then #include <config.h>, like so:
00030 /*
00031 #if HAVE_CONFIG_H
00032 #include <config.h>
00033 #endif
00034  */
00035 
00036 #include <unistd.h>
00037 #include <string.h>
00038 
00039 #include <libplayercore/playercore.h>
00040 
00041 #include <GLUT/glut.h>
00042 #include <stdio.h>
00043 #include <math.h>
00044 
00045 
00046 GLfloat light0_ambient[] =
00047 {0.2, 0.2, 0.2, 1.0};
00048 GLfloat light0_diffuse[] =
00049 {0.0, 0.0, 0.0, 1.0};
00050 GLfloat light1_diffuse[] =
00051 {1.0, 0.0, 0.0, 1.0};
00052 GLfloat light1_position[] =
00053 {1.0, 1.0, 1.0, 0.0};
00054 GLfloat light2_diffuse[] =
00055 {0.0, 1.0, 0.0, 1.0};
00056 GLfloat light2_position[] =
00057 {-1.0, -1.0, 1.0, 0.0};
00058 float s = 0.0;
00059 GLfloat angle1 = 0.0, angle2 = 0.0;
00060 
00061         void 
00062 output(GLfloat x, GLfloat y, char *text)
00063 {
00064         char *p;
00065 
00066         glPushMatrix();
00067         glTranslatef(x, y, 0);
00068         for (p = text; *p; p++)
00069                 glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
00070         glPopMatrix();
00071 }
00072 
00073         void 
00074 display(void)
00075 {
00076         static GLfloat amb[] =
00077         {0.4, 0.4, 0.4, 0.0};
00078         static GLfloat dif[] =
00079         {1.0, 1.0, 1.0, 0.0};
00080 
00081         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00082         glEnable(GL_LIGHT1);
00083         glDisable(GL_LIGHT2);
00084         amb[3] = dif[3] = cos(s) / 2.0 + 0.5;
00085         glMaterialfv(GL_FRONT, GL_AMBIENT, amb);
00086         glMaterialfv(GL_FRONT, GL_DIFFUSE, dif);
00087 
00088         glPushMatrix();
00089         glTranslatef(-0.3, -0.3, 0.0);
00090         glRotatef(angle1, 1.0, 5.0, 0.0);
00091         glCallList(1);        /* render ico display list */
00092         glPopMatrix();
00093 
00094         glClear(GL_DEPTH_BUFFER_BIT);
00095         glEnable(GL_LIGHT2);
00096         glDisable(GL_LIGHT1);
00097         amb[3] = dif[3] = 0.5 - cos(s * .95) / 2.0;
00098         glMaterialfv(GL_FRONT, GL_AMBIENT, amb);
00099         glMaterialfv(GL_FRONT, GL_DIFFUSE, dif);
00100 
00101         glPushMatrix();
00102         glTranslatef(0.3, 0.3, 0.0);
00103         glRotatef(angle2, 1.0, 0.0, 5.0);
00104         glCallList(1);        /* render ico display list */
00105         glPopMatrix();
00106 
00107         glPushAttrib(GL_ENABLE_BIT);
00108         glDisable(GL_DEPTH_TEST);
00109         glDisable(GL_LIGHTING);
00110         glMatrixMode(GL_PROJECTION);
00111         glPushMatrix();
00112         glLoadIdentity();
00113         gluOrtho2D(0, 1500, 0, 1500);
00114         glMatrixMode(GL_MODELVIEW);
00115         glPushMatrix();
00116         glLoadIdentity();
00117         /* Rotate text slightly to help show jaggies. */
00118         glRotatef(4, 0.0, 0.0, 1.0);
00119         output(200, 225, "This is antialiased.");
00120         glDisable(GL_LINE_SMOOTH);
00121         glDisable(GL_BLEND);
00122         output(160, 100, "This text is not.");
00123         glPopMatrix();
00124         glMatrixMode(GL_PROJECTION);
00125         glPopMatrix();
00126         glPopAttrib();
00127         glMatrixMode(GL_MODELVIEW);
00128 
00129         glutSwapBuffers();
00130 }
00131 
00132         void 
00133 idle(void)
00134 {
00135         angle1 = (GLfloat) fmod(angle1 + 0.8, 360.0);
00136         angle2 = (GLfloat) fmod(angle2 + 1.1, 360.0);
00137         s += 0.05;
00138 
00139         //usleep( 100000 );
00140 
00141         static int g=1;
00142         printf( "cycle %d\n", g++ );
00143 
00144         glutPostRedisplay();
00145 }
00146 
00147         void 
00148 redraw( int val )
00149 {
00150         angle1 = (GLfloat) fmod(angle1 + 0.8, 360.0);
00151         angle2 = (GLfloat) fmod(angle2 + 1.1, 360.0);
00152         s += 0.05;
00153 
00154         //usleep( 100000 );
00155 
00156         static int g=1;
00157         printf( "cycle %d\n", g++ );
00158 
00159         glutPostRedisplay();
00160 
00161         glutTimerFunc( 100, redraw, 0 );
00162 }
00163 
00164         void 
00165 visible(int vis)
00166 {
00167         if (vis == GLUT_VISIBLE)
00168                 //glutTimerFunc( 100, redraw, 0 );
00169                 glutIdleFunc( idle );
00170         else
00171                 glutIdleFunc(NULL);
00172 }
00173 
00174 
00176 // The class for the driver
00177 class ExampleDriver : public Driver
00178 {
00179         public:
00180 
00181                 // Constructor; need that
00182                 ExampleDriver(ConfigFile* cf, int section);
00183 
00184                 // Must implement the following methods.
00185                 virtual int Setup();
00186                 virtual int Shutdown();
00187 
00188                 // This method will be invoked on each incoming message
00189                 virtual int ProcessMessage(MessageQueue* resp_queue, 
00190                                 player_msghdr * hdr,
00191                                 void * data);
00192 
00193         private:
00194 
00195                 // Main function for device thread.
00196                 virtual void Main();
00197 
00198                 virtual void Update();
00199 
00200                 int foop;
00201 };
00202 
00203 // A factory creation function, declared outside of the class so that it
00204 // can be invoked without any object context (alternatively, you can
00205 // declare it static in the class).  In this function, we create and return
00206 // (as a generic Driver*) a pointer to a new instance of this driver.
00207         Driver* 
00208 ExampleDriver_Init(ConfigFile* cf, int section)
00209 {
00210         puts( "my init" );
00211         // Create and return a new instance of this driver
00212         return((Driver*)(new ExampleDriver(cf, section)));
00213 }
00214 
00215 // A driver registration function, again declared outside of the class so
00216 // that it can be invoked without object context.  In this function, we add
00217 // the driver into the given driver table, indicating which interface the
00218 // driver can support and how to create a driver instance.
00219 void ExampleDriver_Register(DriverTable* table)
00220 {
00221         table->AddDriver("glutgraphics", ExampleDriver_Init);
00222 }
00223 
00225 // Constructor.  Retrieve options from the configuration file and do any
00226 // pre-Setup() setup.
00227         ExampleDriver::ExampleDriver(ConfigFile* cf, int section)
00228 : Driver(cf, section, false, PLAYER_MSGQUEUE_DEFAULT_MAXLEN, 
00229                 PLAYER_GRAPHICS3D_CODE)
00230 {
00231         // Read an option from the configuration file
00232         //this->foop = cf->ReadInt(section, "foo", 0);
00233 
00234         return;
00235 }
00236 
00237 
00238 static int argc = 1;
00239 static char *argv = "fake";
00240 
00242 // Set up the device.  Return 0 if things go well, and -1 otherwise.
00243 int ExampleDriver::Setup()
00244 {   
00245         puts("Example driver initialising");
00246 
00247         // Here you do whatever is necessary to setup the device, like open and
00248         // configure a serial port.
00249 
00250         //printf("Was foo option given in config file? %d\n", this->foop);
00251 
00252         glutInit( &argc, &argv );
00253         glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
00254         glutCreateWindow("blender");
00255         glutDisplayFunc(display);
00256         glutVisibilityFunc(visible);
00257 
00258         glNewList(1, GL_COMPILE);  /* create ico display list */
00259         glutSolidIcosahedron();
00260         glEndList();
00261 
00262         glEnable(GL_LIGHTING);
00263         glEnable(GL_LIGHT0);
00264         glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
00265         glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
00266         glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
00267         glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
00268         glLightfv(GL_LIGHT2, GL_DIFFUSE, light2_diffuse);
00269         glLightfv(GL_LIGHT2, GL_POSITION, light2_position);
00270         glEnable(GL_DEPTH_TEST);
00271         glEnable(GL_CULL_FACE);
00272         glEnable(GL_BLEND);
00273         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00274         glEnable(GL_LINE_SMOOTH);
00275         glLineWidth(2.0);
00276 
00277         glMatrixMode(GL_PROJECTION);
00278         gluPerspective( /* field of view in degree */ 40.0,
00279                         /* aspect ratio */ 1.0,
00280                         /* Z near */ 1.0, /* Z far */ 10.0);
00281         glMatrixMode(GL_MODELVIEW);
00282         gluLookAt(0.0, 0.0, 5.0,  /* eye is at (0,0,5) */
00283                         0.0, 0.0, 0.0,      /* center is at (0,0,0) */
00284                         0.0, 1.0, 0.);      /* up is in positive Y direction */
00285         glTranslatef(0.0, 0.6, -1.0);
00286 
00287 
00288         puts("Example driver ready");
00289 
00290         // Start the device thread; spawns a new thread and executes
00291         // ExampleDriver::Main(), which contains the main loop for the driver.
00292         //StartThread();
00293 
00294         return(0);
00295 }
00296 
00297 
00299 // Shutdown the device
00300 int ExampleDriver::Shutdown()
00301 {
00302         puts("Shutting example driver down");
00303 
00304         // Stop and join the driver thread
00305         StopThread();
00306 
00307         // Here you would shut the device down by, for example, closing a
00308         // serial port.
00309 
00310         puts("Example driver has been shutdown");
00311 
00312         return(0);
00313 }
00314 
00315 int ExampleDriver::ProcessMessage(MessageQueue* resp_queue, 
00316                 player_msghdr * hdr,
00317                 void * data)
00318 {
00319         // Process messages here.  Send a response if necessary, using Publish().
00320         // If you handle the message successfully, return 0.  Otherwise,
00321         // return -1, and a NACK will be sent for you, if a response is required.
00322 
00323         // call this from the driver's private thread - on a timer?
00324         // receive messages and do drawing
00325 
00326         return(0);
00327 }
00328 
00329 
00330 void ExampleDriver::Update()
00331 {
00332         ProcessMessages();
00333         glutCheckLoop();
00334         return;
00335 }
00336 
00338 // Main function for device thread
00339 void ExampleDriver::Main() 
00340 {
00341         printf( "entering main loop" );
00342         //glutMainLoop();
00343 
00344         //return 0;             /* ANSI C requires main to return int. */
00345 
00346         // The main loop; interact with the device here
00347         for(;;)
00348         {}
00349         //{
00350         // test if we are supposed to cancel
00351         //pthread_testcancel();
00352 
00353         // Process incoming messages.  ExampleDriver::ProcessMessage() is
00354         // called on each message.
00355         //ProcessMessages();
00356 
00357         // Interact with the device, and push out the resulting data, using
00358         // Driver::Publish()
00359 
00360         // Sleep (you might, for example, block on a read() instead)
00361         //usleep(100000);
00362 }
00363 
00365 // Extra stuff for building a shared object.
00366 
00367 /* need the extern to avoid C++ name-mangling  */
00368         extern "C" {
00369                 int player_driver_init(DriverTable* table)
00370                 {
00371                         puts("Example driver initializing");
00372                         ExampleDriver_Register(table);
00373                         puts("Example driver done");
00374                         return(0);
00375                 }
00376         }
00377 


stage
Author(s): Richard Vaughan , Brian Gerkey , Reed Hedges , Andrew Howard , Toby Collett , Pooya Karimian , Jeremy Asher , Alex Couture-Beil , Geoff Biggs , Rich Mattes , Abbas Sadat
autogenerated on Thu Aug 27 2015 15:20:57