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


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 Feb 28 2022 23:48:55