| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved. |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* Further, this software is distributed without any warranty that it is |
| 16 |
* free of the rightful claim of any third person regarding infringement |
| 17 |
* or the like. Any license provided herein, whether implied or |
| 18 |
* otherwise, applies only to this software file. Patent licenses, if |
| 19 |
* any, provided herein do not apply to combinations of this program with |
| 20 |
* other software, or any other product whatsoever. |
| 21 |
* |
| 22 |
* You should have received a copy of the GNU Lesser General Public |
| 23 |
* License along with this library; if not, write to the Free Software |
| 24 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 |
* |
| 26 |
* Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 27 |
* Mountain View, CA 94043, or: |
| 28 |
* |
| 29 |
* http://www.sgi.com |
| 30 |
* |
| 31 |
* For further information regarding this notice, see: |
| 32 |
* |
| 33 |
* http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 34 |
* |
| 35 |
*/ |
| 36 |
|
| 37 |
/* |
| 38 |
* Copyright (C) 1990,91,92,93 Silicon Graphics, Inc. |
| 39 |
* |
| 40 |
_______________________________________________________________________ |
| 41 |
______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 42 |
| |
| 43 |
| $Revision: 1.3 $ |
| 44 |
| |
| 45 |
| Classes: |
| 46 |
| SoOffscreenRenderer |
| 47 |
| |
| 48 |
| Author(s) : Dave Immel |
| 49 |
| |
| 50 |
______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 51 |
_______________________________________________________________________ |
| 52 |
*/ |
| 53 |
#include <cstdio> |
| 54 |
#include <assert.h> |
| 55 |
#include <Inventor/SbLinear.h> |
| 56 |
#include <Inventor/SoOffscreenRenderer.h> |
| 57 |
#include <Inventor/SoPath.h> |
| 58 |
#include <Inventor/actions/SoGLRenderAction.h> |
| 59 |
#include <Inventor/errors/SoDebugError.h> |
| 60 |
#include <Inventor/nodes/SoNode.h> |
| 61 |
|
| 62 |
// gross stuff to read images, grabbed from image.h and the image |
| 63 |
// library. Had to grab it since the real .h file is not c++ compatible. |
| 64 |
extern "C" { |
| 65 |
|
| 66 |
typedef struct { |
| 67 |
unsigned short imagic; /* stuff saved on disk . . */ |
| 68 |
unsigned short type; |
| 69 |
unsigned short dim; |
| 70 |
unsigned short xsize; |
| 71 |
unsigned short ysize; |
| 72 |
unsigned short zsize; |
| 73 |
uint32_t min; |
| 74 |
uint32_t max; |
| 75 |
uint32_t wastebytes; |
| 76 |
char name[80]; |
| 77 |
uint32_t colormap; |
| 78 |
} IMAGE; |
| 79 |
extern IMAGE *fiopen(int, const char *, int = 0, int = 0, |
| 80 |
int = 0, int = 0, int = 0); |
| 81 |
extern void putrow(IMAGE *, short *, unsigned int, unsigned int); |
| 82 |
extern void iclose(IMAGE *); |
| 83 |
}; /* end of extern "C" */ |
| 84 |
|
| 85 |
#define TYPEMASK 0xff00 |
| 86 |
#define BPPMASK 0x00ff |
| 87 |
#define ITYPE_VERBATIM 0x0000 |
| 88 |
#define ITYPE_RLE 0x0100 |
| 89 |
#define ISRLE(type) (((type) & 0xff00) == ITYPE_RLE) |
| 90 |
#define ISVERBATIM(type) (((type) & 0xff00) == ITYPE_VERBATIM) |
| 91 |
#define BPP(type) ((type) & BPPMASK) |
| 92 |
#define RLE(bpp) (ITYPE_RLE | (bpp)) |
| 93 |
#define VERBATIM(bpp) (ITYPE_VERBATIM | (bpp)) |
| 94 |
#define IBUFSIZE(pixels) ( (pixels+(pixels>>6))*sizeof(int32_t) ) |
| 95 |
#define RLE_NOP 0x00 |
| 96 |
|
| 97 |
// Offscreen renderings will always be rendered as RGB images |
| 98 |
// Currently the OpenGL does not support storing alpha channel information |
| 99 |
// in the offscreen pixmap. |
| 100 |
static int attributeList[] = { GLX_RGBA, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, |
| 101 |
GLX_BLUE_SIZE, 1, GLX_DEPTH_SIZE, 1, None }; |
| 102 |
|
| 103 |
// |
| 104 |
// KLUDGE!!!!! |
| 105 |
// In order to keep caches from being blown away when the contexts |
| 106 |
// is deleted, assign a unique number to them with the rendering action. |
| 107 |
// Start at 10000 and increment. |
| 108 |
// |
| 109 |
static uint32_t SoContextIncrement = 10000; |
| 110 |
|
| 111 |
|
| 112 |
//////////////////////////////////////////////////////////////////////// |
| 113 |
// |
| 114 |
// Description: |
| 115 |
// Constructor. |
| 116 |
// |
| 117 |
// Use: public |
| 118 |
|
| 119 |
SoOffscreenRenderer::SoOffscreenRenderer( |
| 120 |
const SbViewportRegion &viewportRegion ) |
| 121 |
|
| 122 |
// |
| 123 |
//////////////////////////////////////////////////////////////////////// |
| 124 |
{ |
| 125 |
offAction = new SoGLRenderAction(viewportRegion); |
| 126 |
userAction = NULL; |
| 127 |
pixelBuffer = NULL; |
| 128 |
display = NULL; |
| 129 |
comps = SoOffscreenRenderer::RGB; |
| 130 |
backgroundColor.setValue(0.0, 0.0, 0.0); |
| 131 |
} |
| 132 |
|
| 133 |
//////////////////////////////////////////////////////////////////////// |
| 134 |
// |
| 135 |
// Description: |
| 136 |
// Constructor. |
| 137 |
// |
| 138 |
// Use: public |
| 139 |
|
| 140 |
SoOffscreenRenderer::SoOffscreenRenderer( |
| 141 |
SoGLRenderAction *act ) |
| 142 |
|
| 143 |
// |
| 144 |
//////////////////////////////////////////////////////////////////////// |
| 145 |
{ |
| 146 |
offAction = new SoGLRenderAction(act->getViewportRegion()); |
| 147 |
userAction = act; |
| 148 |
pixelBuffer = NULL; |
| 149 |
display = NULL; |
| 150 |
comps = SoOffscreenRenderer::RGB; |
| 151 |
backgroundColor.setValue(0.0, 0.0, 0.0); |
| 152 |
} |
| 153 |
|
| 154 |
//////////////////////////////////////////////////////////////////////// |
| 155 |
// |
| 156 |
// Description: |
| 157 |
// Destructor. |
| 158 |
// |
| 159 |
// Use: public |
| 160 |
|
| 161 |
SoOffscreenRenderer::~SoOffscreenRenderer() |
| 162 |
|
| 163 |
// |
| 164 |
//////////////////////////////////////////////////////////////////////// |
| 165 |
{ |
| 166 |
delete offAction; |
| 167 |
|
| 168 |
if( pixelBuffer != NULL ) |
| 169 |
delete pixelBuffer; |
| 170 |
|
| 171 |
// Delete the pixmap, window, and context, as it is no longer needed |
| 172 |
if (display != NULL) { |
| 173 |
glXDestroyGLXPixmap( display, pixmap ); |
| 174 |
glXDestroyContext( display, context ); |
| 175 |
XCloseDisplay( display ); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
//////////////////////////////////////////////////////////////////////// |
| 180 |
// |
| 181 |
// Description: |
| 182 |
// Calculate and return the number of pixels per inch on the current |
| 183 |
// device horizontally. |
| 184 |
// |
| 185 |
// Use: static, public |
| 186 |
|
| 187 |
float |
| 188 |
SoOffscreenRenderer::getScreenPixelsPerInch() |
| 189 |
|
| 190 |
// |
| 191 |
//////////////////////////////////////////////////////////////////////// |
| 192 |
{ |
| 193 |
Display *tmpDisplay; |
| 194 |
float pix; |
| 195 |
|
| 196 |
// Create an X Display |
| 197 |
if ((tmpDisplay=XOpenDisplay(NULL)) == NULL) |
| 198 |
return 75.0; |
| 199 |
|
| 200 |
// Get the dimensions of the screen |
| 201 |
pix = DisplayWidth(tmpDisplay, 0) * 25.4 / |
| 202 |
(float)DisplayWidthMM(tmpDisplay, 0); |
| 203 |
XCloseDisplay(tmpDisplay); |
| 204 |
|
| 205 |
return pix; |
| 206 |
} |
| 207 |
|
| 208 |
//////////////////////////////////////////////////////////////////////// |
| 209 |
// |
| 210 |
// Description: |
| 211 |
// Return the maximum allowable resolution of a viewport in which |
| 212 |
// to render a scene. |
| 213 |
// |
| 214 |
// Use: static, public |
| 215 |
|
| 216 |
SbVec2s |
| 217 |
SoOffscreenRenderer::getMaximumResolution() |
| 218 |
|
| 219 |
// |
| 220 |
//////////////////////////////////////////////////////////////////////// |
| 221 |
{ |
| 222 |
Display *dpy = NULL; |
| 223 |
XVisualInfo *vi; |
| 224 |
GLXContext cx; |
| 225 |
GLXPixmap glxpmap; |
| 226 |
Pixmap xpmap; |
| 227 |
SbVec2s tmpvp(2, 2); |
| 228 |
GLint params[2]; |
| 229 |
|
| 230 |
if (!initPixmap(dpy, vi, cx, tmpvp, glxpmap, xpmap)) { |
| 231 |
#ifdef DEBUG |
| 232 |
SoDebugError::post("SoOffscreenRenderer::getMaximumResolution", |
| 233 |
"Could not initialize Pixmap"); |
| 234 |
#endif |
| 235 |
return SbVec2s(-1, -1); |
| 236 |
} |
| 237 |
|
| 238 |
glGetIntegerv(GL_MAX_VIEWPORT_DIMS, params); |
| 239 |
glXDestroyGLXPixmap( dpy, glxpmap ); |
| 240 |
glXDestroyContext( dpy, cx ); |
| 241 |
XCloseDisplay(dpy); |
| 242 |
|
| 243 |
return (SbVec2s((short)params[0], (short)params[1])); |
| 244 |
} |
| 245 |
|
| 246 |
//////////////////////////////////////////////////////////////////////// |
| 247 |
// |
| 248 |
// Description: |
| 249 |
// Set the viewport region. |
| 250 |
// |
| 251 |
// Use: public |
| 252 |
|
| 253 |
void |
| 254 |
SoOffscreenRenderer::setViewportRegion( |
| 255 |
const SbViewportRegion &viewportRegion ) |
| 256 |
|
| 257 |
|
| 258 |
// |
| 259 |
//////////////////////////////////////////////////////////////////////// |
| 260 |
{ |
| 261 |
offAction->setViewportRegion(viewportRegion); |
| 262 |
} |
| 263 |
|
| 264 |
//////////////////////////////////////////////////////////////////////// |
| 265 |
// |
| 266 |
// Description: |
| 267 |
// Get the viewport region. |
| 268 |
// |
| 269 |
// Use: public |
| 270 |
|
| 271 |
const SbViewportRegion & |
| 272 |
SoOffscreenRenderer::getViewportRegion() const |
| 273 |
|
| 274 |
|
| 275 |
// |
| 276 |
//////////////////////////////////////////////////////////////////////// |
| 277 |
{ |
| 278 |
return offAction->getViewportRegion(); |
| 279 |
} |
| 280 |
|
| 281 |
//////////////////////////////////////////////////////////////////////// |
| 282 |
// |
| 283 |
// Description: |
| 284 |
// Set the render action. |
| 285 |
// |
| 286 |
// Use: public |
| 287 |
|
| 288 |
void |
| 289 |
SoOffscreenRenderer::setGLRenderAction( |
| 290 |
SoGLRenderAction *act ) |
| 291 |
|
| 292 |
|
| 293 |
// |
| 294 |
//////////////////////////////////////////////////////////////////////// |
| 295 |
{ |
| 296 |
userAction = act; |
| 297 |
} |
| 298 |
|
| 299 |
//////////////////////////////////////////////////////////////////////// |
| 300 |
// |
| 301 |
// Description: |
| 302 |
// Get the render action. |
| 303 |
// |
| 304 |
// Use: public |
| 305 |
|
| 306 |
SoGLRenderAction * |
| 307 |
SoOffscreenRenderer::getGLRenderAction() const |
| 308 |
|
| 309 |
|
| 310 |
// |
| 311 |
//////////////////////////////////////////////////////////////////////// |
| 312 |
{ |
| 313 |
return userAction; |
| 314 |
} |
| 315 |
|
| 316 |
//////////////////////////////////////////////////////////////////////// |
| 317 |
// |
| 318 |
// Description: |
| 319 |
// Render the given scene graph into a buffer. |
| 320 |
// |
| 321 |
// Use: public |
| 322 |
|
| 323 |
SbBool |
| 324 |
SoOffscreenRenderer::render(SoNode *scene) |
| 325 |
// |
| 326 |
//////////////////////////////////////////////////////////////////////// |
| 327 |
{ |
| 328 |
// Delete the pixel buffer if it has been previously used. |
| 329 |
if (pixelBuffer != NULL) { |
| 330 |
delete pixelBuffer; |
| 331 |
pixelBuffer = NULL; |
| 332 |
} |
| 333 |
|
| 334 |
// Set the render action to use. |
| 335 |
SoGLRenderAction *act; |
| 336 |
if (userAction != NULL) |
| 337 |
act = userAction; |
| 338 |
else |
| 339 |
act = offAction; |
| 340 |
renderedViewport = act->getViewportRegion(); |
| 341 |
|
| 342 |
// Get the offscreen pixmap ready for rendering |
| 343 |
if (!setupPixmap()) { |
| 344 |
#ifdef DEBUG |
| 345 |
SoDebugError::post("SoOffscreenRenderer::render", |
| 346 |
"Could not setup Pixmap"); |
| 347 |
#endif |
| 348 |
return FALSE; |
| 349 |
} |
| 350 |
|
| 351 |
// Set the GL cache context for the action to a unique number |
| 352 |
// (we'll use and increment SoContextIncrement), so that it doesn't try |
| 353 |
// to use display lists from other contexts. |
| 354 |
uint32_t oldContext = act->getCacheContext(); |
| 355 |
act->setCacheContext(SoContextIncrement++); |
| 356 |
act->apply(scene); |
| 357 |
act->setCacheContext(oldContext); |
| 358 |
|
| 359 |
return TRUE; |
| 360 |
} |
| 361 |
|
| 362 |
//////////////////////////////////////////////////////////////////////// |
| 363 |
// |
| 364 |
// Description: |
| 365 |
// Render the given scene graph into a buffer. |
| 366 |
// |
| 367 |
// Use: public |
| 368 |
|
| 369 |
SbBool |
| 370 |
SoOffscreenRenderer::render(SoPath *scene) |
| 371 |
|
| 372 |
// |
| 373 |
//////////////////////////////////////////////////////////////////////// |
| 374 |
{ |
| 375 |
// Delete the pixel buffer if it has been previously used. |
| 376 |
if (pixelBuffer != NULL) { |
| 377 |
delete pixelBuffer; |
| 378 |
pixelBuffer = NULL; |
| 379 |
} |
| 380 |
|
| 381 |
// Set the render action to use. |
| 382 |
SoGLRenderAction *act; |
| 383 |
if (userAction != NULL) |
| 384 |
act = userAction; |
| 385 |
else |
| 386 |
act = offAction; |
| 387 |
renderedViewport = act->getViewportRegion(); |
| 388 |
|
| 389 |
// Get the offscreen pixmap ready for rendering |
| 390 |
if (!setupPixmap()) { |
| 391 |
#ifdef DEBUG |
| 392 |
SoDebugError::post("SoOffscreenRenderer::writeToRGB", |
| 393 |
"Could not setup Pixmap"); |
| 394 |
#endif |
| 395 |
return FALSE; |
| 396 |
} |
| 397 |
|
| 398 |
// Render the scene into the new graphics context |
| 399 |
|
| 400 |
// Set the GL cache context for the action to a unique number |
| 401 |
// (we'll use and increment SoContextIncrement), so that it doesn't try |
| 402 |
// to use display lists from other contexts. |
| 403 |
uint32_t oldContext = act->getCacheContext(); |
| 404 |
act->setCacheContext(SoContextIncrement++); |
| 405 |
act->apply(scene); |
| 406 |
act->setCacheContext(oldContext); |
| 407 |
|
| 408 |
return TRUE; |
| 409 |
} |
| 410 |
|
| 411 |
//////////////////////////////////////////////////////////////////////// |
| 412 |
// |
| 413 |
// Description: |
| 414 |
// Return the rendered buffer. |
| 415 |
// |
| 416 |
// Use: public |
| 417 |
|
| 418 |
unsigned char * |
| 419 |
SoOffscreenRenderer::getBuffer() const |
| 420 |
|
| 421 |
|
| 422 |
// |
| 423 |
//////////////////////////////////////////////////////////////////////// |
| 424 |
{ |
| 425 |
// If the buffer has not been gotten yet, read the pixels into |
| 426 |
// the buffer. Return the buffer to the user. |
| 427 |
if (pixelBuffer == NULL) { |
| 428 |
|
| 429 |
if (!setContext()) |
| 430 |
return NULL; |
| 431 |
((SoOffscreenRenderer *)this)->readPixels(); |
| 432 |
} |
| 433 |
|
| 434 |
return pixelBuffer; |
| 435 |
} |
| 436 |
|
| 437 |
//////////////////////////////////////////////////////////////////////// |
| 438 |
// |
| 439 |
// Description: |
| 440 |
// Write the buffer into a .rgb file. |
| 441 |
// |
| 442 |
// Use: public |
| 443 |
|
| 444 |
SbBool |
| 445 |
SoOffscreenRenderer::writeToRGB( FILE *fp ) const |
| 446 |
|
| 447 |
|
| 448 |
// |
| 449 |
//////////////////////////////////////////////////////////////////////// |
| 450 |
{ |
| 451 |
// Set the graphics context to be the offscreen pixmap |
| 452 |
if (!setContext()) |
| 453 |
return FALSE; |
| 454 |
|
| 455 |
// Open an image file for writing |
| 456 |
int dimensions, components; |
| 457 |
if (comps == (SoOffscreenRenderer::LUMINANCE_TRANSPARENCY) || |
| 458 |
(comps == SoOffscreenRenderer::RGB_TRANSPARENCY)) { |
| 459 |
#ifdef DEBUG |
| 460 |
SoDebugError::post("SoOffscreenRenderer::writeToRGB", |
| 461 |
"The RGB file format does not support transparency"); |
| 462 |
#endif |
| 463 |
return FALSE; |
| 464 |
} |
| 465 |
else if (comps == SoOffscreenRenderer::LUMINANCE) { |
| 466 |
dimensions = 2; |
| 467 |
components = 1; |
| 468 |
} |
| 469 |
else { |
| 470 |
dimensions = 3; |
| 471 |
components = 3; |
| 472 |
} |
| 473 |
|
| 474 |
// Get the correct viewport size. |
| 475 |
const SbVec2s &vpSize = renderedViewport.getViewportSizePixels(); |
| 476 |
|
| 477 |
int ifile = fileno(fp); |
| 478 |
IMAGE *image; |
| 479 |
|
| 480 |
if ((image = fiopen( ifile, "w", RLE(1), dimensions, |
| 481 |
(unsigned int)vpSize[0], (unsigned int)vpSize[1], |
| 482 |
components )) == NULL) |
| 483 |
{ |
| 484 |
#ifdef DEBUG |
| 485 |
SoDebugError::post("SoOffscreenRenderer::writeToRGB", |
| 486 |
"could not open image file"); |
| 487 |
#endif |
| 488 |
return FALSE; |
| 489 |
} |
| 490 |
|
| 491 |
// Get the format of the pixmap data |
| 492 |
GLenum format; |
| 493 |
getFormat(format); |
| 494 |
|
| 495 |
// For each row in the pixel buffer, write the row into the image file |
| 496 |
short *rowBuf = new short[vpSize[0]]; |
| 497 |
unsigned char *pBuf = new unsigned char[vpSize[0]*components*2]; |
| 498 |
|
| 499 |
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 500 |
for (int row=0; row<vpSize[1]; row++) { |
| 501 |
|
| 502 |
// Read the next scanline of pixels from the offscreen pixmap |
| 503 |
glReadPixels(0, row, vpSize[0], 1, format, |
| 504 |
GL_UNSIGNED_BYTE, (GLvoid *)pBuf); |
| 505 |
|
| 506 |
// The pixel in the pixel buffer store pixel information arranged |
| 507 |
// by pixel, whereas the .rgb file stores pixel information arranged |
| 508 |
// by color component. So scanlines of component data must be |
| 509 |
// accumulated before a row can be written. |
| 510 |
unsigned char *tbuf = pBuf; |
| 511 |
|
| 512 |
// Convert each color component |
| 513 |
for (int comp=0; comp<components; comp++) { |
| 514 |
short *trow = rowBuf; |
| 515 |
|
| 516 |
// Convert a row |
| 517 |
tbuf = pBuf + comp; |
| 518 |
for (int j=0; j<vpSize[0]; j++, tbuf += components) |
| 519 |
*trow++ = (short)*tbuf; |
| 520 |
putrow( image, rowBuf, row, comp ); |
| 521 |
} |
| 522 |
} |
| 523 |
iclose( image ); |
| 524 |
delete[] pBuf; |
| 525 |
delete[] rowBuf; |
| 526 |
return TRUE; |
| 527 |
} |
| 528 |
|
| 529 |
//////////////////////////////////////////////////////////////////////// |
| 530 |
// |
| 531 |
// Description: |
| 532 |
// Write a hex value into Encapsulated PostScript. |
| 533 |
// |
| 534 |
// Use: private |
| 535 |
|
| 536 |
void |
| 537 |
SoOffscreenRenderer::putHex( |
| 538 |
FILE *fp, |
| 539 |
char val, |
| 540 |
int &hexPos ) |
| 541 |
|
| 542 |
// |
| 543 |
//////////////////////////////////////////////////////////////////////// |
| 544 |
{ |
| 545 |
fprintf(fp, "%02hx", (unsigned char)val); |
| 546 |
if (++hexPos >= 32) { |
| 547 |
fprintf(fp, "\n"); |
| 548 |
hexPos = 0; |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
//////////////////////////////////////////////////////////////////////// |
| 553 |
// |
| 554 |
// Description: |
| 555 |
// Write the buffer into Encapsulated PostScript. |
| 556 |
// |
| 557 |
// Use: public |
| 558 |
|
| 559 |
SbBool |
| 560 |
SoOffscreenRenderer::writeToPostScript( FILE *fp ) const |
| 561 |
|
| 562 |
|
| 563 |
// |
| 564 |
//////////////////////////////////////////////////////////////////////// |
| 565 |
{ |
| 566 |
const SbVec2s &vpSize = renderedViewport.getViewportSizePixels(); |
| 567 |
SbVec2f printSize; |
| 568 |
float ppi = renderedViewport.getPixelsPerInch(); |
| 569 |
|
| 570 |
printSize[0] = vpSize[0] / ppi; |
| 571 |
printSize[1] = vpSize[1] / ppi; |
| 572 |
|
| 573 |
return (writeToPostScript( fp, printSize )); |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
//////////////////////////////////////////////////////////////////////// |
| 578 |
// |
| 579 |
// Description: |
| 580 |
// Write the buffer into Encapsulated PostScript at the given |
| 581 |
// print size in inches. |
| 582 |
// |
| 583 |
// Use: public |
| 584 |
|
| 585 |
SbBool |
| 586 |
SoOffscreenRenderer::writeToPostScript( |
| 587 |
FILE *fp, |
| 588 |
const SbVec2f &printSize ) const |
| 589 |
|
| 590 |
|
| 591 |
// |
| 592 |
//////////////////////////////////////////////////////////////////////// |
| 593 |
{ |
| 594 |
// Set the graphics context to be the offscreen pixmap |
| 595 |
if (!setContext()) |
| 596 |
return FALSE; |
| 597 |
|
| 598 |
int components; |
| 599 |
if ((comps == LUMINANCE_TRANSPARENCY) || |
| 600 |
(comps == RGB_TRANSPARENCY)) { |
| 601 |
#ifdef DEBUG |
| 602 |
SoDebugError::post("SoOffscreenRenderer::writeToRGB", |
| 603 |
"PostScript does not support image transparency"); |
| 604 |
#endif |
| 605 |
return FALSE; |
| 606 |
} |
| 607 |
else if (comps == SoOffscreenRenderer::LUMINANCE) { |
| 608 |
components = 1; |
| 609 |
} |
| 610 |
else { |
| 611 |
components = 3; |
| 612 |
} |
| 613 |
|
| 614 |
// Get the correct viewport region |
| 615 |
const SbVec2s &vpSize = renderedViewport.getViewportSizePixels(); |
| 616 |
|
| 617 |
// Write the PostScript header |
| 618 |
fprintf(fp, "%%!PS-Adobe-2.0 EPSF-1.2\n"); |
| 619 |
fprintf(fp, "%%%%Creator: IRIS program output\n"); |
| 620 |
fprintf(fp, "%%%%BoundingBox: 0 0 %d %d\n", vpSize[0], vpSize[1]); |
| 621 |
fprintf(fp, "%%%%EndComments\n"); |
| 622 |
fprintf(fp, "gsave\n"); |
| 623 |
|
| 624 |
// Write the image into the PostScript file |
| 625 |
fprintf(fp, "/bwproc {\n"); |
| 626 |
fprintf(fp, " rgbproc\n"); |
| 627 |
fprintf(fp, " dup length 3 idiv string 0 3 0\n"); |
| 628 |
fprintf(fp, " 5 -1 roll {\n"); |
| 629 |
fprintf(fp, " add 2 1 roll 1 sub dup 0 eq\n"); |
| 630 |
fprintf(fp, " { pop 3 idiv 3 -1 roll dup 4 -1 roll dup\n"); |
| 631 |
fprintf(fp, " 3 1 roll 5 -1 roll put 1 add 3 0 }\n"); |
| 632 |
fprintf(fp, " { 2 1 roll } ifelse\n"); |
| 633 |
fprintf(fp, " } forall\n"); |
| 634 |
fprintf(fp, " pop pop pop\n"); |
| 635 |
fprintf(fp, "} def\n"); |
| 636 |
fprintf(fp, "systemdict /colorimage known not {\n"); |
| 637 |
fprintf(fp, " /colorimage {\n"); |
| 638 |
fprintf(fp, " pop\n"); |
| 639 |
fprintf(fp, " pop\n"); |
| 640 |
fprintf(fp, " /rgbproc exch def\n"); |
| 641 |
fprintf(fp, " { bwproc } image\n"); |
| 642 |
fprintf(fp, " } def\n"); |
| 643 |
fprintf(fp, "} if\n"); |
| 644 |
fprintf(fp, "/picstr %d string def\n", vpSize[0] * components); |
| 645 |
fprintf(fp, "%d %d scale\n", (int)(printSize[0]*72.0), |
| 646 |
(int)(printSize[1]*72.0)); |
| 647 |
fprintf(fp, "%d %d %d\n", vpSize[0], vpSize[1], 8); |
| 648 |
fprintf(fp, "[%d 0 0 %d 0 0]\n", vpSize[0], vpSize[1]); |
| 649 |
fprintf(fp, "{currentfile picstr readhexstring pop}\n"); |
| 650 |
fprintf(fp, "false %d\n", components); |
| 651 |
fprintf(fp, "colorimage\n"); |
| 652 |
|
| 653 |
// Get the format of the pixmap data |
| 654 |
GLenum format; |
| 655 |
getFormat(format); |
| 656 |
|
| 657 |
// Convert the pixel values to ASCII hex and write them out. |
| 658 |
unsigned char *pBuf = new unsigned char[vpSize[0]*components*2]; |
| 659 |
|
| 660 |
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 661 |
int numValues = vpSize[0]*components; |
| 662 |
int hexpos = 0; |
| 663 |
|
| 664 |
for (int row=0; row<vpSize[1]; row++) { |
| 665 |
|
| 666 |
// Read the next scanline of pixels from the offscreen pixmap |
| 667 |
glReadPixels(0, row, vpSize[0], 1, format, |
| 668 |
GL_UNSIGNED_BYTE, (GLvoid *)pBuf); |
| 669 |
|
| 670 |
char *tbuf = (char *)pBuf; |
| 671 |
|
| 672 |
// Write out the scanline |
| 673 |
for (int i=0; i<numValues; i++) |
| 674 |
putHex(fp, (char)*tbuf++, hexpos); |
| 675 |
} |
| 676 |
|
| 677 |
if (hexpos) |
| 678 |
fprintf(fp, "\n"); |
| 679 |
|
| 680 |
// Finish up the PostScript file. |
| 681 |
fprintf(fp, "grestore\n"); |
| 682 |
fprintf(fp, "showpage\n"); |
| 683 |
|
| 684 |
delete[] pBuf; |
| 685 |
return TRUE; |
| 686 |
} |
| 687 |
|
| 688 |
//////////////////////////////////////////////////////////////////////// |
| 689 |
// |
| 690 |
// Description: |
| 691 |
// Setup a pixmap into which the rendering will occur. |
| 692 |
// |
| 693 |
// Use: private |
| 694 |
|
| 695 |
SbBool |
| 696 |
SoOffscreenRenderer::setupPixmap() |
| 697 |
|
| 698 |
// |
| 699 |
//////////////////////////////////////////////////////////////////////// |
| 700 |
{ |
| 701 |
const SbVec2s &vpSize = renderedViewport.getViewportSizePixels(); |
| 702 |
|
| 703 |
if (!initPixmap(display, visual, context, vpSize, pixmap, pmap)) |
| 704 |
return FALSE; |
| 705 |
|
| 706 |
// Clear the pixmap to the backgroundColor |
| 707 |
glEnable(GL_DEPTH_TEST); |
| 708 |
glDepthFunc(GL_LEQUAL); |
| 709 |
glClearColor(backgroundColor[0], backgroundColor[1], backgroundColor[2], 0); |
| 710 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 711 |
|
| 712 |
return TRUE; |
| 713 |
} |
| 714 |
|
| 715 |
//////////////////////////////////////////////////////////////////////// |
| 716 |
// |
| 717 |
// Description: |
| 718 |
// Setup a pixmap into which the rendering will occur. |
| 719 |
// |
| 720 |
// Use: static, private |
| 721 |
|
| 722 |
SbBool |
| 723 |
SoOffscreenRenderer::initPixmap( |
| 724 |
Display * &dpy, |
| 725 |
XVisualInfo * &vi, |
| 726 |
GLXContext &cx, |
| 727 |
const SbVec2s &sz, |
| 728 |
GLXPixmap &glxpmap, |
| 729 |
Pixmap &xpmap ) |
| 730 |
|
| 731 |
// |
| 732 |
//////////////////////////////////////////////////////////////////////// |
| 733 |
{ |
| 734 |
// If a display is already open, and is the same size, use it. |
| 735 |
// Otherwise, close it and open up a fresh one. |
| 736 |
if (dpy != NULL) { |
| 737 |
|
| 738 |
// Get the size of the passed pixmap. If it is the same size |
| 739 |
// as the passed size, just return. |
| 740 |
Window root; |
| 741 |
int x, y; |
| 742 |
unsigned int width, height, border_width, depth; |
| 743 |
if (!XGetGeometry(dpy, xpmap, &root, &x, &y, &width, &height, |
| 744 |
&border_width, &depth)) { |
| 745 |
|
| 746 |
// Bad pixmap. Destroy it. |
| 747 |
glXDestroyGLXPixmap( dpy, glxpmap ); |
| 748 |
glXDestroyContext( dpy, cx ); |
| 749 |
XCloseDisplay(dpy); |
| 750 |
} |
| 751 |
else if ((width == sz[0]) && (height == sz[1])) { |
| 752 |
|
| 753 |
// This pixmap is the right size to use for this rendering. |
| 754 |
// Make it the current context for rendering. |
| 755 |
if (!glXMakeCurrent(dpy, glxpmap, cx)) { |
| 756 |
glXDestroyGLXPixmap( dpy, glxpmap ); |
| 757 |
glXDestroyContext( dpy, cx ); |
| 758 |
XCloseDisplay(dpy); |
| 759 |
dpy = NULL; |
| 760 |
return FALSE; |
| 761 |
} |
| 762 |
|
| 763 |
// Store the pixels as byte aligned rows in the Pixmap. |
| 764 |
glPixelStorei(GL_PACK_ALIGNMENT, 1); |
| 765 |
|
| 766 |
return TRUE; |
| 767 |
} |
| 768 |
else { |
| 769 |
|
| 770 |
// The pixmap is not the right size. Destroy it. |
| 771 |
glXDestroyGLXPixmap( dpy, glxpmap ); |
| 772 |
glXDestroyContext( dpy, cx ); |
| 773 |
XCloseDisplay(dpy); |
| 774 |
} |
| 775 |
} |
| 776 |
|
| 777 |
// Create an X Display |
| 778 |
if ((dpy = XOpenDisplay(NULL)) == NULL) |
| 779 |
return FALSE; |
| 780 |
|
| 781 |
// Create a GLX Visual |
| 782 |
if ((vi = glXChooseVisual(dpy, DefaultScreen(dpy), |
| 783 |
attributeList)) == NULL) { |
| 784 |
XCloseDisplay(dpy); |
| 785 |
dpy = NULL; |
| 786 |
return FALSE; |
| 787 |
} |
| 788 |
|
| 789 |
// Create a GLX Context |
| 790 |
if ((cx = glXCreateContext(dpy, vi, NULL, FALSE)) == NULL) { |
| 791 |
XCloseDisplay(dpy); |
| 792 |
dpy = NULL; |
| 793 |
return FALSE; |
| 794 |
} |
| 795 |
|
| 796 |
// Create X and GLX Pixmap |
| 797 |
xpmap = XCreatePixmap( dpy, DefaultRootWindow(dpy), |
| 798 |
(unsigned int)sz[0], (unsigned int)sz[1], vi->depth ); |
| 799 |
glxpmap = glXCreateGLXPixmap( dpy, vi, xpmap ); |
| 800 |
|
| 801 |
if (!glXMakeCurrent(dpy, glxpmap, cx)) { |
| 802 |
glXDestroyGLXPixmap( dpy, glxpmap ); |
| 803 |
glXDestroyContext( dpy, cx ); |
| 804 |
XCloseDisplay(dpy); |
| 805 |
dpy = NULL; |
| 806 |
return FALSE; |
| 807 |
} |
| 808 |
|
| 809 |
// Store the pixels as byte aligned rows in the Pixmap |
| 810 |
glPixelStorei(GL_PACK_ALIGNMENT, 1); |
| 811 |
|
| 812 |
return TRUE; |
| 813 |
} |
| 814 |
|
| 815 |
//////////////////////////////////////////////////////////////////////// |
| 816 |
// |
| 817 |
// Description: |
| 818 |
// Read the pixels from the Pixmap |
| 819 |
// |
| 820 |
// Use: private |
| 821 |
|
| 822 |
void |
| 823 |
SoOffscreenRenderer::readPixels() |
| 824 |
|
| 825 |
// |
| 826 |
//////////////////////////////////////////////////////////////////////// |
| 827 |
{ |
| 828 |
const SbVec2s &vpSize = renderedViewport.getViewportSizePixels(); |
| 829 |
|
| 830 |
GLenum format = 0; |
| 831 |
int allocSize = 0; |
| 832 |
switch (comps) { |
| 833 |
case LUMINANCE: |
| 834 |
format = GL_LUMINANCE; |
| 835 |
allocSize = vpSize[0] * vpSize[1] * 1; |
| 836 |
break; |
| 837 |
case LUMINANCE_TRANSPARENCY: |
| 838 |
format = GL_LUMINANCE_ALPHA; |
| 839 |
allocSize = vpSize[0] * vpSize[1] * 2; |
| 840 |
break; |
| 841 |
case RGB: |
| 842 |
format = GL_RGB; |
| 843 |
allocSize = vpSize[0] * vpSize[1] * 3; |
| 844 |
break; |
| 845 |
case RGB_TRANSPARENCY: |
| 846 |
format = GL_RGBA; |
| 847 |
allocSize = vpSize[0] * vpSize[1] * 4; |
| 848 |
break; |
| 849 |
} |
| 850 |
|
| 851 |
pixelBuffer = new unsigned char[allocSize]; |
| 852 |
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 853 |
glReadPixels(0, 0, vpSize[0], vpSize[1], format, |
| 854 |
GL_UNSIGNED_BYTE, (GLvoid *)pixelBuffer); |
| 855 |
} |
| 856 |
|
| 857 |
//////////////////////////////////////////////////////////////////////// |
| 858 |
// |
| 859 |
// Description: |
| 860 |
// Set the graphics context to point to the offscreen pixmap |
| 861 |
// |
| 862 |
// Use: private |
| 863 |
|
| 864 |
SbBool |
| 865 |
SoOffscreenRenderer::setContext() const |
| 866 |
|
| 867 |
// |
| 868 |
//////////////////////////////////////////////////////////////////////// |
| 869 |
{ |
| 870 |
if (!glXMakeCurrent(display, pixmap, context)) { |
| 871 |
glXDestroyGLXPixmap( display, pixmap ); |
| 872 |
glXDestroyContext( display, context ); |
| 873 |
XCloseDisplay(display); |
| 874 |
return FALSE; |
| 875 |
} |
| 876 |
|
| 877 |
return TRUE; |
| 878 |
} |
| 879 |
|
| 880 |
//////////////////////////////////////////////////////////////////////// |
| 881 |
// |
| 882 |
// Description: |
| 883 |
// Get format information |
| 884 |
// |
| 885 |
// Use: private |
| 886 |
|
| 887 |
void |
| 888 |
SoOffscreenRenderer::getFormat( GLenum &format ) const |
| 889 |
|
| 890 |
// |
| 891 |
//////////////////////////////////////////////////////////////////////// |
| 892 |
{ |
| 893 |
switch (comps) { |
| 894 |
case LUMINANCE: |
| 895 |
format = GL_LUMINANCE; |
| 896 |
break; |
| 897 |
case LUMINANCE_TRANSPARENCY: |
| 898 |
format = GL_LUMINANCE_ALPHA; |
| 899 |
break; |
| 900 |
case RGB: |
| 901 |
format = GL_RGB; |
| 902 |
break; |
| 903 |
case RGB_TRANSPARENCY: |
| 904 |
format = GL_RGBA; |
| 905 |
break; |
| 906 |
} |
| 907 |
} |
| 908 |
|