glutgraphics.cc
Go to the documentation of this file.
1 /*
2  * Player - One Hell of a Robot Server
3  * glutgraphics.cc: GLUT-based graphics3d + 2d driver
4  * Copyright (C) 2007
5  * Brian Gerkey, Richard Vaughan
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  */
22 
23 /*
24  * A simple example of how to write a driver that will be built as a
25  * shared object.
26  */
27 
28 // ONLY if you need something that was #define'd as a result of configure
29 // (e.g., HAVE_CFMAKERAW), then #include <config.h>, like so:
30 /*
31 #if HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34  */
35 
36 #include <unistd.h>
37 #include <string.h>
38 
39 #include <libplayercore/playercore.h>
40 
41 #include <GLUT/glut.h>
42 #include <stdio.h>
43 #include <math.h>
44 
45 
46 GLfloat light0_ambient[] =
47 {0.2, 0.2, 0.2, 1.0};
48 GLfloat light0_diffuse[] =
49 {0.0, 0.0, 0.0, 1.0};
50 GLfloat light1_diffuse[] =
51 {1.0, 0.0, 0.0, 1.0};
52 GLfloat light1_position[] =
53 {1.0, 1.0, 1.0, 0.0};
54 GLfloat light2_diffuse[] =
55 {0.0, 1.0, 0.0, 1.0};
56 GLfloat light2_position[] =
57 {-1.0, -1.0, 1.0, 0.0};
58 float s = 0.0;
59 GLfloat angle1 = 0.0, angle2 = 0.0;
60 
61  void
62 output(GLfloat x, GLfloat y, char *text)
63 {
64  char *p;
65 
66  glPushMatrix();
67  glTranslatef(x, y, 0);
68  for (p = text; *p; p++)
69  glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
70  glPopMatrix();
71 }
72 
73  void
74 display(void)
75 {
76  static GLfloat amb[] =
77  {0.4, 0.4, 0.4, 0.0};
78  static GLfloat dif[] =
79  {1.0, 1.0, 1.0, 0.0};
80 
81  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
82  glEnable(GL_LIGHT1);
83  glDisable(GL_LIGHT2);
84  amb[3] = dif[3] = cos(s) / 2.0 + 0.5;
85  glMaterialfv(GL_FRONT, GL_AMBIENT, amb);
86  glMaterialfv(GL_FRONT, GL_DIFFUSE, dif);
87 
88  glPushMatrix();
89  glTranslatef(-0.3, -0.3, 0.0);
90  glRotatef(angle1, 1.0, 5.0, 0.0);
91  glCallList(1); /* render ico display list */
92  glPopMatrix();
93 
94  glClear(GL_DEPTH_BUFFER_BIT);
95  glEnable(GL_LIGHT2);
96  glDisable(GL_LIGHT1);
97  amb[3] = dif[3] = 0.5 - cos(s * .95) / 2.0;
98  glMaterialfv(GL_FRONT, GL_AMBIENT, amb);
99  glMaterialfv(GL_FRONT, GL_DIFFUSE, dif);
100 
101  glPushMatrix();
102  glTranslatef(0.3, 0.3, 0.0);
103  glRotatef(angle2, 1.0, 0.0, 5.0);
104  glCallList(1); /* render ico display list */
105  glPopMatrix();
106 
107  glPushAttrib(GL_ENABLE_BIT);
108  glDisable(GL_DEPTH_TEST);
109  glDisable(GL_LIGHTING);
110  glMatrixMode(GL_PROJECTION);
111  glPushMatrix();
112  glLoadIdentity();
113  gluOrtho2D(0, 1500, 0, 1500);
114  glMatrixMode(GL_MODELVIEW);
115  glPushMatrix();
116  glLoadIdentity();
117  /* Rotate text slightly to help show jaggies. */
118  glRotatef(4, 0.0, 0.0, 1.0);
119  output(200, 225, "This is antialiased.");
120  glDisable(GL_LINE_SMOOTH);
121  glDisable(GL_BLEND);
122  output(160, 100, "This text is not.");
123  glPopMatrix();
124  glMatrixMode(GL_PROJECTION);
125  glPopMatrix();
126  glPopAttrib();
127  glMatrixMode(GL_MODELVIEW);
128 
129  glutSwapBuffers();
130 }
131 
132  void
133 idle(void)
134 {
135  angle1 = (GLfloat) fmod(angle1 + 0.8, 360.0);
136  angle2 = (GLfloat) fmod(angle2 + 1.1, 360.0);
137  s += 0.05;
138 
139  //usleep( 100000 );
140 
141  static int g=1;
142  printf( "cycle %d\n", g++ );
143 
144  glutPostRedisplay();
145 }
146 
147  void
148 redraw( int val )
149 {
150  angle1 = (GLfloat) fmod(angle1 + 0.8, 360.0);
151  angle2 = (GLfloat) fmod(angle2 + 1.1, 360.0);
152  s += 0.05;
153 
154  //usleep( 100000 );
155 
156  static int g=1;
157  printf( "cycle %d\n", g++ );
158 
159  glutPostRedisplay();
160 
161  glutTimerFunc( 100, redraw, 0 );
162 }
163 
164  void
165 visible(int vis)
166 {
167  if (vis == GLUT_VISIBLE)
168  //glutTimerFunc( 100, redraw, 0 );
169  glutIdleFunc( idle );
170  else
171  glutIdleFunc(NULL);
172 }
173 
174 
176 // The class for the driver
177 class ExampleDriver : public Driver
178 {
179  public:
180 
181  // Constructor; need that
182  ExampleDriver(ConfigFile* cf, int section);
183 
184  // Must implement the following methods.
185  virtual int Setup();
186  virtual int Shutdown();
187 
188  // This method will be invoked on each incoming message
189  virtual int ProcessMessage(MessageQueue* resp_queue,
190  player_msghdr * hdr,
191  void * data);
192 
193  private:
194 
195  // Main function for device thread.
196  virtual void Main();
197 
198  virtual void Update();
199 
200  int foop;
201 };
202 
203 // A factory creation function, declared outside of the class so that it
204 // can be invoked without any object context (alternatively, you can
205 // declare it static in the class). In this function, we create and return
206 // (as a generic Driver*) a pointer to a new instance of this driver.
207  Driver*
208 ExampleDriver_Init(ConfigFile* cf, int section)
209 {
210  puts( "my init" );
211  // Create and return a new instance of this driver
212  return((Driver*)(new ExampleDriver(cf, section)));
213 }
214 
215 // A driver registration function, again declared outside of the class so
216 // that it can be invoked without object context. In this function, we add
217 // the driver into the given driver table, indicating which interface the
218 // driver can support and how to create a driver instance.
219 void ExampleDriver_Register(DriverTable* table)
220 {
221  table->AddDriver("glutgraphics", ExampleDriver_Init);
222 }
223 
225 // Constructor. Retrieve options from the configuration file and do any
226 // pre-Setup() setup.
227  ExampleDriver::ExampleDriver(ConfigFile* cf, int section)
228 : Driver(cf, section, false, PLAYER_MSGQUEUE_DEFAULT_MAXLEN,
229  PLAYER_GRAPHICS3D_CODE)
230 {
231  // Read an option from the configuration file
232  //this->foop = cf->ReadInt(section, "foo", 0);
233 
234  return;
235 }
236 
237 
238 static int argc = 1;
239 static char *argv = "fake";
240 
242 // Set up the device. Return 0 if things go well, and -1 otherwise.
244 {
245  puts("Example driver initialising");
246 
247  // Here you do whatever is necessary to setup the device, like open and
248  // configure a serial port.
249 
250  //printf("Was foo option given in config file? %d\n", this->foop);
251 
252  glutInit( &argc, &argv );
253  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
254  glutCreateWindow("blender");
255  glutDisplayFunc(display);
256  glutVisibilityFunc(visible);
257 
258  glNewList(1, GL_COMPILE); /* create ico display list */
259  glutSolidIcosahedron();
260  glEndList();
261 
262  glEnable(GL_LIGHTING);
263  glEnable(GL_LIGHT0);
264  glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
265  glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
266  glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
267  glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
268  glLightfv(GL_LIGHT2, GL_DIFFUSE, light2_diffuse);
269  glLightfv(GL_LIGHT2, GL_POSITION, light2_position);
270  glEnable(GL_DEPTH_TEST);
271  glEnable(GL_CULL_FACE);
272  glEnable(GL_BLEND);
273  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
274  glEnable(GL_LINE_SMOOTH);
275  glLineWidth(2.0);
276 
277  glMatrixMode(GL_PROJECTION);
278  gluPerspective( /* field of view in degree */ 40.0,
279  /* aspect ratio */ 1.0,
280  /* Z near */ 1.0, /* Z far */ 10.0);
281  glMatrixMode(GL_MODELVIEW);
282  gluLookAt(0.0, 0.0, 5.0, /* eye is at (0,0,5) */
283  0.0, 0.0, 0.0, /* center is at (0,0,0) */
284  0.0, 1.0, 0.); /* up is in positive Y direction */
285  glTranslatef(0.0, 0.6, -1.0);
286 
287 
288  puts("Example driver ready");
289 
290  // Start the device thread; spawns a new thread and executes
291  // ExampleDriver::Main(), which contains the main loop for the driver.
292  //StartThread();
293 
294  return(0);
295 }
296 
297 
299 // Shutdown the device
301 {
302  puts("Shutting example driver down");
303 
304  // Stop and join the driver thread
305  StopThread();
306 
307  // Here you would shut the device down by, for example, closing a
308  // serial port.
309 
310  puts("Example driver has been shutdown");
311 
312  return(0);
313 }
314 
315 int ExampleDriver::ProcessMessage(MessageQueue* resp_queue,
316  player_msghdr * hdr,
317  void * data)
318 {
319  // Process messages here. Send a response if necessary, using Publish().
320  // If you handle the message successfully, return 0. Otherwise,
321  // return -1, and a NACK will be sent for you, if a response is required.
322 
323  // call this from the driver's private thread - on a timer?
324  // receive messages and do drawing
325 
326  return(0);
327 }
328 
329 
331 {
332  ProcessMessages();
333  glutCheckLoop();
334  return;
335 }
336 
338 // Main function for device thread
340 {
341  printf( "entering main loop" );
342  //glutMainLoop();
343 
344  //return 0; /* ANSI C requires main to return int. */
345 
346  // The main loop; interact with the device here
347  for(;;)
348  {}
349  //{
350  // test if we are supposed to cancel
351  //pthread_testcancel();
352 
353  // Process incoming messages. ExampleDriver::ProcessMessage() is
354  // called on each message.
355  //ProcessMessages();
356 
357  // Interact with the device, and push out the resulting data, using
358  // Driver::Publish()
359 
360  // Sleep (you might, for example, block on a read() instead)
361  //usleep(100000);
362 }
363 
365 // Extra stuff for building a shared object.
366 
367 /* need the extern to avoid C++ name-mangling */
368  extern "C" {
369  int player_driver_init(DriverTable* table)
370  {
371  puts("Example driver initializing");
372  ExampleDriver_Register(table);
373  puts("Example driver done");
374  return(0);
375  }
376  }
377 
void redraw(int val)
virtual void Main()
GLfloat angle1
Definition: glutgraphics.cc:59
GLfloat light2_position[]
Definition: glutgraphics.cc:56
void idle(void)
GLfloat light0_ambient[]
Definition: glutgraphics.cc:46
Driver * ExampleDriver_Init(ConfigFile *cf, int section)
void output(GLfloat x, GLfloat y, char *text)
Definition: glutgraphics.cc:62
void ExampleDriver_Register(DriverTable *table)
float s
Definition: glutgraphics.cc:58
GLfloat light2_diffuse[]
Definition: glutgraphics.cc:54
void visible(int vis)
virtual int Setup()
GLfloat light1_diffuse[]
Definition: glutgraphics.cc:50
static int argc
ExampleDriver(ConfigFile *cf, int section)
GLfloat angle2
Definition: glutgraphics.cc:59
static char * argv
virtual int ProcessMessage(MessageQueue *resp_queue, player_msghdr *hdr, void *data)
void display(void)
Definition: glutgraphics.cc:74
int player_driver_init(DriverTable *table)
GLfloat light1_position[]
Definition: glutgraphics.cc:52
virtual void Update()
virtual int Shutdown()
GLfloat light0_diffuse[]
Definition: glutgraphics.cc:48


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 Mon Jun 10 2019 15:06:09