forked from rtv/Stage
-
Notifications
You must be signed in to change notification settings - Fork 3
/
p_graphics.cc
440 lines (394 loc) · 12.1 KB
/
p_graphics.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
* Stage
* Copyright (C) Richard Vaughan
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
/*
* Desc: A plugin driver for Player that gives access to Stage devices.
* Author: Richard Vaughan
* Date: 10 December 2004
* CVS: $Id$
*/
// DOCUMENTATION ------------------------------------------------------------
/** @addtogroup player
@par Graphics2D interface
- PLAYER_GRAPHICS2D_CMD_POINTS
- PLAYER_GRAPHICS2D_CMD_POLYGON
- PLAYER_GRAPHICS2D_CMD_MULTILINE
- PLAYER_GRAPHICS2D_CMD_POLYLINE
- PLAYER_GRAPHICS2D_CMD_CLEAR
@par Graphics3D interface
- PLAYER_GRAPHICS3D_CMD_DRAW
- PLAYER_GRAPHICS3D_CMD_PUSH
- PLAYER_GRAPHICS3D_CMD_POP
- PLAYER_GRAPHICS3D_CMD_CLEAR
- PLAYER_GRAPHICS3D_CMD_TRANSLATE
- PLAYER_GRAPHICS3D_CMD_ROTATE
*/
#include "p_driver.h"
#include <iostream>
using namespace std;
#ifdef G_OS_WIN32
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
#endif
struct clientDisplaylist {
int DisplayList;
vector<Message> RenderItems;
};
class PlayerGraphicsVis: public Stg::Visualizer {
private:
public:
PlayerGraphicsVis() :
Stg::Visualizer("Graphics", "custom_vis") {
}
;
virtual ~PlayerGraphicsVis(void) {
for (queuemap::iterator itr = ClientDisplayLists.begin(); itr
!= ClientDisplayLists.end(); ++itr)
RemoveDisplayList(itr->second);
}
;
virtual void Visualize(Stg::Model* mod, Stg::Camera* cam) {
GLint OldDepthFunc;
glGetIntegerv(GL_DEPTH_FUNC, &OldDepthFunc);
glDepthFunc(GL_LEQUAL);
for (queuemap::iterator itr = ClientDisplayLists.begin(); itr
!= ClientDisplayLists.end(); ++itr) {
glPushMatrix();
glTranslatef(0, 0, 0.01);// tiny Z offset raises rect above grid
glCallList(itr->second.DisplayList);
glPopMatrix();
}
glDepthFunc(OldDepthFunc);
}
void Clear(MessageQueue * client) {
struct clientDisplaylist &list = GetDisplayList(client);
list.RenderItems.clear();
glNewList(list.DisplayList, GL_COMPILE );
glEndList();
}
;
bool HasActiveDisplayList(MessageQueue * client) {
queuemap::iterator found = ClientDisplayLists.find(client);
if (found == ClientDisplayLists.end())
return false;
else if (found->second.DisplayList == -1)
return false;
return true;
}
struct clientDisplaylist & GetDisplayList(MessageQueue * client) {
queuemap::iterator found = ClientDisplayLists.find(client);
if (found == ClientDisplayLists.end()) // Display list not created yet
{
clientDisplaylist & clientDisp = ClientDisplayLists[client];
clientDisp.DisplayList = glGenLists(1);
return clientDisp;
}
else if (found->second.DisplayList == -1)
found->second.DisplayList = glGenLists(1);
return found->second;
}
void BuildDisplayList(MessageQueue * client) {
struct clientDisplaylist &list = GetDisplayList(client);
glNewList(list.DisplayList, GL_COMPILE );
glPushMatrix();
for (vector<Message>::iterator itr = list.RenderItems.begin(); itr
!= list.RenderItems.end(); ++itr)
RenderItem(*itr);
glPopMatrix();
glEndList();
}
void RemoveDisplayList(struct clientDisplaylist & list) {
if (list.DisplayList > 0)
glDeleteLists(list.DisplayList, 1);
}
virtual void AppendItem(MessageQueue * client, Message & item) {
struct clientDisplaylist & list = GetDisplayList(client);
list.RenderItems.push_back(item);
}
;
void Subscribe(QueuePointer &queue) {
if (queue == NULL)
return;
clientDisplaylist & clientDisp = ClientDisplayLists[queue.get()];
// delay creation of display list so we don't get race conditions on internal subscriptions.
clientDisp.DisplayList = -1;
}
void Unsubscribe(QueuePointer &queue) {
if (queue == NULL)
return;
if (HasActiveDisplayList(queue.get()))
{
struct clientDisplaylist & list = GetDisplayList(queue.get());
RemoveDisplayList(list);
}
ClientDisplayLists.erase(queue.get());
}
void glPlayerColour(const player_color_t & colour) {
glColor4f(static_cast<float> (colour.red) / 255.0,
static_cast<float> (colour.green) / 255.0,
static_cast<float> (colour.blue) / 255.0, 1
- static_cast<float> (colour.alpha) / 255.0);
}
virtual void RenderItem(Message & item) = 0;
private:
typedef map<MessageQueue *, struct clientDisplaylist> queuemap;
queuemap ClientDisplayLists;
};
class PlayerGraphics2dVis: public PlayerGraphicsVis {
public:
PlayerGraphics2dVis() :
PlayerGraphicsVis() {
}
;
~PlayerGraphics2dVis() {
}
;
void RenderItem(Message & item);
};
class PlayerGraphics3dVis: public PlayerGraphicsVis {
public:
PlayerGraphics3dVis() :
PlayerGraphicsVis() {
}
;
~PlayerGraphics3dVis() {
}
;
void RenderItem(Message & item);
};
InterfaceGraphics2d::InterfaceGraphics2d(player_devaddr_t addr,
StgDriver* driver, ConfigFile* cf, int section) :
InterfaceModel(addr, driver, cf, section, "") {
vis = new PlayerGraphics2dVis;
mod->AddVisualizer( vis, true );
}
InterfaceGraphics2d::~InterfaceGraphics2d() {
mod->RemoveVisualizer(vis);
delete vis;
}
void InterfaceGraphics2d::Subscribe(QueuePointer &queue) {
vis->Subscribe(queue);
}
void InterfaceGraphics2d::Unsubscribe(QueuePointer &queue) {
vis->Unsubscribe(queue);
}
int InterfaceGraphics2d::ProcessMessage(QueuePointer & resp_queue,
player_msghdr_t* hdr, void* data) {
if (Message::MatchMessage(hdr, PLAYER_MSGTYPE_CMD,
PLAYER_GRAPHICS2D_CMD_CLEAR, this->addr)) {
vis->Clear(resp_queue.get());
return 0; //ok
}
if (Message::MatchMessage(hdr, PLAYER_MSGTYPE_CMD,
PLAYER_GRAPHICS2D_CMD_POINTS, this->addr)
|| Message::MatchMessage(hdr, PLAYER_MSGTYPE_CMD,
PLAYER_GRAPHICS2D_CMD_POLYLINE, this->addr)
// Draw Multiline is new in Player 3.1, this ifdef allows Stage to build against older versions of Player
#if defined PLAYER_GRAPHICS2D_CMD_MULTILINE
|| Message::MatchMessage(hdr, PLAYER_MSGTYPE_CMD,
PLAYER_GRAPHICS2D_CMD_MULTILINE, this->addr)
#endif
|| Message::MatchMessage(hdr, PLAYER_MSGTYPE_CMD,
PLAYER_GRAPHICS2D_CMD_MULTILINE, this->addr)
|| Message::MatchMessage(hdr, PLAYER_MSGTYPE_CMD,
PLAYER_GRAPHICS2D_CMD_POLYGON, this->addr)) {
Message msg(*hdr, data);
vis->AppendItem(resp_queue.get(), msg);
vis->BuildDisplayList(resp_queue.get());
return 0;
}
PLAYER_WARN2("stage graphics2d doesn't support message %d:%d.", hdr->type,
hdr->subtype);
return -1;
}
void PlayerGraphics2dVis::RenderItem(Message & item) {
glDepthMask(GL_FALSE);
int type = item.GetHeader()->subtype;
switch (type) {
case PLAYER_GRAPHICS2D_CMD_POINTS: {
player_graphics2d_cmd_points_t
& data =
*reinterpret_cast<player_graphics2d_cmd_points_t*> (item.GetPayload());
glPlayerColour(data.color);
glBegin(GL_POINTS);
for (unsigned ii = 0; ii < data.points_count; ++ii)
glVertex3f(data.points[ii].px, data.points[ii].py, 0);
glEnd();
}
break;
case PLAYER_GRAPHICS2D_CMD_POLYLINE: {
player_graphics2d_cmd_polyline_t
& data =
*reinterpret_cast<player_graphics2d_cmd_polyline_t*> (item.GetPayload());
glPlayerColour(data.color);
glBegin(GL_LINE_STRIP);
for (unsigned ii = 0; ii < data.points_count; ++ii)
glVertex3f(data.points[ii].px, data.points[ii].py, 0);
glEnd();
}
break;
// Draw Multiline is new in Player 3.1, this ifdef allows Stage to build against older versions of Player
#if defined PLAYER_GRAPHICS2D_CMD_MULTILINE
case PLAYER_GRAPHICS2D_CMD_MULTILINE: {
player_graphics2d_cmd_multiline_t
& data =
*reinterpret_cast<player_graphics2d_cmd_multiline_t*> (item.GetPayload());
glPlayerColour(data.color);
glBegin(GL_LINES);
for (unsigned ii = 0; ii < data.points_count; ++ii)
glVertex3f(data.points[ii].px, data.points[ii].py, 0);
glEnd();
}
break;
#endif
case PLAYER_GRAPHICS2D_CMD_POLYGON: {
player_graphics2d_cmd_polygon_t
& data =
*reinterpret_cast<player_graphics2d_cmd_polygon_t*> (item.GetPayload());
if (data.filled) {
glPlayerColour(data.fill_color);
glBegin(GL_POLYGON);
for (unsigned ii = 0; ii < data.points_count; ++ii)
glVertex3f(data.points[ii].px, data.points[ii].py, 0);
glEnd();
}
glPlayerColour(data.color);
glBegin(GL_LINE_LOOP);
for (unsigned ii = 0; ii < data.points_count; ++ii)
glVertex3f(data.points[ii].px, data.points[ii].py, 0);
glEnd();
}
break;
}
glDepthMask(GL_TRUE);
}
InterfaceGraphics3d::InterfaceGraphics3d(player_devaddr_t addr,
StgDriver* driver, ConfigFile* cf, int section) :
InterfaceModel(addr, driver, cf, section, "") {
vis = new PlayerGraphics3dVis;
mod->AddVisualizer( vis, true );
}
InterfaceGraphics3d::~InterfaceGraphics3d() {
mod->RemoveVisualizer(vis);
delete vis;
}
void InterfaceGraphics3d::Subscribe(QueuePointer &queue) {
vis->Subscribe(queue);
}
void InterfaceGraphics3d::Unsubscribe(QueuePointer &queue) {
vis->Unsubscribe(queue);
}
int InterfaceGraphics3d::ProcessMessage(QueuePointer & resp_queue,
player_msghdr_t* hdr, void* data) {
if (Message::MatchMessage(hdr, PLAYER_MSGTYPE_CMD,
PLAYER_GRAPHICS3D_CMD_CLEAR, this->addr)) {
vis->Clear(resp_queue.get());
return 0; //ok
}
if (Message::MatchMessage(hdr, PLAYER_MSGTYPE_CMD,
PLAYER_GRAPHICS3D_CMD_PUSH, this->addr) || Message::MatchMessage(
hdr, PLAYER_MSGTYPE_CMD, PLAYER_GRAPHICS3D_CMD_POP, this->addr)
|| Message::MatchMessage(hdr, PLAYER_MSGTYPE_CMD,
PLAYER_GRAPHICS3D_CMD_DRAW, this->addr)
|| Message::MatchMessage(hdr, PLAYER_MSGTYPE_CMD,
PLAYER_GRAPHICS3D_CMD_TRANSLATE, this->addr)
|| Message::MatchMessage(hdr, PLAYER_MSGTYPE_CMD,
PLAYER_GRAPHICS3D_CMD_ROTATE, this->addr)) {
Message msg(*hdr, data);
vis->AppendItem(resp_queue.get(), msg);
vis->BuildDisplayList(resp_queue.get());
return 0;
}
PLAYER_WARN2("stage graphics2d doesn't support message %d:%d.", hdr->type,
hdr->subtype);
return -1;
}
void PlayerGraphics3dVis::RenderItem(Message & item) {
int type = item.GetHeader()->subtype;
switch (type) {
case PLAYER_GRAPHICS3D_CMD_DRAW: {
player_graphics3d_cmd_draw_t
& data =
*reinterpret_cast<player_graphics3d_cmd_draw_t*> (item.GetPayload());
glPlayerColour(data.color);
switch (data.draw_mode) {
case PLAYER_DRAW_POINTS:
glBegin(GL_POINTS);
break;
case PLAYER_DRAW_LINES:
glBegin(GL_LINES);
break;
case PLAYER_DRAW_LINE_STRIP:
glBegin(GL_LINE_STRIP);
break;
case PLAYER_DRAW_LINE_LOOP:
glBegin(GL_LINE_LOOP);
break;
case PLAYER_DRAW_TRIANGLES:
glBegin(GL_TRIANGLES);
break;
case PLAYER_DRAW_TRIANGLE_STRIP:
glBegin(GL_TRIANGLE_STRIP);
break;
case PLAYER_DRAW_TRIANGLE_FAN:
glBegin(GL_TRIANGLE_FAN);
break;
case PLAYER_DRAW_QUADS:
glBegin(GL_QUADS);
break;
case PLAYER_DRAW_QUAD_STRIP:
glBegin(GL_QUAD_STRIP);
break;
case PLAYER_DRAW_POLYGON:
glBegin(GL_POLYGON);
break;
default:
fprintf(stderr,"Unknown graphics 3d draw mode\n");
return;
}
for (unsigned ii = 0; ii < data.points_count; ++ii)
glVertex3f(data.points[ii].px, data.points[ii].py,
data.points[ii].pz);
glEnd();
}
break;
case PLAYER_GRAPHICS3D_CMD_TRANSLATE: {
player_graphics3d_cmd_translate_t
& data =
*reinterpret_cast<player_graphics3d_cmd_translate_t*> (item.GetPayload());
glTranslatef(data.x, data.y, data.z);
}
break;
case PLAYER_GRAPHICS3D_CMD_ROTATE: {
player_graphics3d_cmd_rotate_t
& data =
*reinterpret_cast<player_graphics3d_cmd_rotate_t*> (item.GetPayload());
glRotatef(data.a, data.x, data.y, data.z);
}
break;
case PLAYER_GRAPHICS3D_CMD_PUSH: {
glPushMatrix();
}
break;
case PLAYER_GRAPHICS3D_CMD_POP: {
glPopMatrix();
}
break;
}
}