Skip to content
Newer
Older
100644 378 lines (305 sloc) 9.39 KB
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
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
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
32 #include <config.h>
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
33 #endif
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
34 */
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
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
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
61 void
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
62 output(GLfloat x, GLfloat y, char *text)
63 {
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
64 char *p;
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
65
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
66 glPushMatrix();
67 glTranslatef(x, y, 0);
68 for (p = text; *p; p++)
69 glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
70 glPopMatrix();
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
71 }
72
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
73 void
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
74 display(void)
75 {
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
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();
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
130 }
131
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
132 void
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
133 idle(void)
134 {
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
135 angle1 = (GLfloat) fmod(angle1 + 0.8, 360.0);
136 angle2 = (GLfloat) fmod(angle2 + 1.1, 360.0);
137 s += 0.05;
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
138
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
139 //usleep( 100000 );
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
140
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
141 static int g=1;
142 printf( "cycle %d\n", g++ );
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
143
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
144 glutPostRedisplay();
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
145 }
146
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
147 void
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
148 redraw( int val )
149 {
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
150 angle1 = (GLfloat) fmod(angle1 + 0.8, 360.0);
151 angle2 = (GLfloat) fmod(angle2 + 1.1, 360.0);
152 s += 0.05;
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
153
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
154 //usleep( 100000 );
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
155
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
156 static int g=1;
157 printf( "cycle %d\n", g++ );
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
158
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
159 glutPostRedisplay();
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
160
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
161 glutTimerFunc( 100, redraw, 0 );
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
162 }
163
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
164 void
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
165 visible(int vis)
166 {
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
167 if (vis == GLUT_VISIBLE)
168 //glutTimerFunc( 100, redraw, 0 );
169 glutIdleFunc( idle );
170 else
171 glutIdleFunc(NULL);
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
172 }
173
174
175 ////////////////////////////////////////////////////////////////////////////////
176 // The class for the driver
177 class ExampleDriver : public Driver
178 {
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
179 public:
180
181 // Constructor; need that
182 ExampleDriver(ConfigFile* cf, int section);
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
183
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
184 // Must implement the following methods.
185 virtual int Setup();
186 virtual int Shutdown();
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
187
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
188 // This method will be invoked on each incoming message
189 virtual int ProcessMessage(MessageQueue* resp_queue,
190 player_msghdr * hdr,
191 void * data);
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
192
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
193 private:
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
194
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
195 // Main function for device thread.
196 virtual void Main();
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
197
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
198 virtual void Update();
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
199
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
200 int foop;
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
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.
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
207 Driver*
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
208 ExampleDriver_Init(ConfigFile* cf, int section)
209 {
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
210 puts( "my init" );
211 // Create and return a new instance of this driver
212 return((Driver*)(new ExampleDriver(cf, section)));
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
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 {
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
221 table->AddDriver("glutgraphics", ExampleDriver_Init);
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
222 }
223
224 ////////////////////////////////////////////////////////////////////////////////
225 // Constructor. Retrieve options from the configuration file and do any
226 // pre-Setup() setup.
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
227 ExampleDriver::ExampleDriver(ConfigFile* cf, int section)
228 : Driver(cf, section, false, PLAYER_MSGQUEUE_DEFAULT_MAXLEN,
229 PLAYER_GRAPHICS3D_CODE)
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
230 {
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
231 // Read an option from the configuration file
232 //this->foop = cf->ReadInt(section, "foo", 0);
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
233
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
234 return;
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
235 }
236
237
238 static int argc = 1;
239 static char *argv = "fake";
240
241 ////////////////////////////////////////////////////////////////////////////////
242 // Set up the device. Return 0 if things go well, and -1 otherwise.
243 int ExampleDriver::Setup()
244 {
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
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);
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
295 }
296
297
298 ////////////////////////////////////////////////////////////////////////////////
299 // Shutdown the device
300 int ExampleDriver::Shutdown()
301 {
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
302 puts("Shutting example driver down");
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
303
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
304 // Stop and join the driver thread
305 StopThread();
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
306
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
307 // Here you would shut the device down by, for example, closing a
308 // serial port.
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
309
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
310 puts("Example driver has been shutdown");
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
311
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
312 return(0);
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
313 }
314
315 int ExampleDriver::ProcessMessage(MessageQueue* resp_queue,
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
316 player_msghdr * hdr,
317 void * data)
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
318 {
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
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.
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
322
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
323 // call this from the driver's private thread - on a timer?
324 // receive messages and do drawing
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
325
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
326 return(0);
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
327 }
328
329
330 void ExampleDriver::Update()
331 {
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
332 ProcessMessages();
333 glutCheckLoop();
334 return;
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
335 }
336
337 ////////////////////////////////////////////////////////////////////////////////
338 // Main function for device thread
339 void ExampleDriver::Main()
340 {
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
341 printf( "entering main loop" );
342 //glutMainLoop();
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
343
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
344 //return 0; /* ANSI C requires main to return int. */
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
345
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
346 // The main loop; interact with the device here
347 for(;;)
348 {}
349 //{
350 // test if we are supposed to cancel
351 //pthread_testcancel();
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
352
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
353 // Process incoming messages. ExampleDriver::ProcessMessage() is
354 // called on each message.
355 //ProcessMessages();
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
356
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
357 // Interact with the device, and push out the resulting data, using
358 // Driver::Publish()
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
359
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
360 // Sleep (you might, for example, block on a read() instead)
361 //usleep(100000);
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
362 }
363
364 ////////////////////////////////////////////////////////////////////////////////
365 // Extra stuff for building a shared object.
366
367 /* need the extern to avoid C++ name-mangling */
22a1279 fixed indentation issues (converted to tabs)
alexcb authored Jun 10, 2008
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 }
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored Jan 15, 2008
377
Something went wrong with that request. Please try again.