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 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkIOSRenderWindow.mm
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkOpenGLRenderWindow.h"
#import "vtkIOSRenderWindow.h"
#import "vtkRenderWindowInteractor.h"
#import "vtkCommand.h"
#import "vtkIdList.h"
#import "vtkObjectFactory.h"
#import "vtkRendererCollection.h"
#import <sstream>
#include "vtk_glew.h"
vtkStandardNewMacro(vtkIOSRenderWindow);
//----------------------------------------------------------------------------
vtkIOSRenderWindow::vtkIOSRenderWindow()
{
this->WindowCreated = 0;
this->ViewCreated = 0;
this->SetWindowName("Visualization Toolkit - IOS");
this->CursorHidden = 0;
this->ForceMakeCurrent = 0;
this->Capabilities = 0;
this->OnScreenInitialized = 0;
this->OffScreenInitialized = 0;
// it seems that LEFT/RIGHT cause issues on IOS so we just use
// generic BACK/FRONT
this->BackLeftBuffer = static_cast<unsigned int>(GL_BACK);
this->BackRightBuffer = static_cast<unsigned int>(GL_BACK);
this->FrontLeftBuffer = static_cast<unsigned int>(GL_FRONT);
this->FrontRightBuffer = static_cast<unsigned int>(GL_FRONT);
}
//----------------------------------------------------------------------------
vtkIOSRenderWindow::~vtkIOSRenderWindow()
{
if (this->CursorHidden)
{
this->ShowCursor();
}
this->Finalize();
vtkRenderer *ren;
vtkCollectionSimpleIterator rit;
this->Renderers->InitTraversal(rit);
while ( (ren = this->Renderers->GetNextRenderer(rit)) )
{
ren->SetRenderWindow(NULL);
}
delete[] this->Capabilities;
this->Capabilities = 0;
this->SetContextId(NULL);
this->SetPixelFormat(NULL);
this->SetRootWindow(NULL);
this->SetWindowId(NULL);
this->SetParentId(NULL);
}
//----------------------------------------------------------------------------
void vtkIOSRenderWindow::Finalize()
{
if(this->OffScreenInitialized)
{
this->OffScreenInitialized = 0;
this->DestroyOffScreenWindow();
}
if(this->OnScreenInitialized)
{
this->OnScreenInitialized = 0;
this->DestroyWindow();
}
}
//----------------------------------------------------------------------------
void vtkIOSRenderWindow::DestroyWindow()
{
// finish OpenGL rendering
if (this->OwnContext && this->GetContextId())
{
this->MakeCurrent();
this->ReleaseGraphicsResources(this);
}
this->SetContextId(NULL);
this->SetPixelFormat(NULL);
this->SetWindowId(NULL);
this->SetParentId(NULL);
this->SetRootWindow(NULL);
}
int vtkIOSRenderWindow::GetPixelData(int x1, int y1,
int x2, int y2,
int front, unsigned char* data)
{
int y_low, y_hi;
int x_low, x_hi;
// set the current window
this->MakeCurrent();
if (y1 < y2)
{
y_low = y1;
y_hi = y2;
}
else
{
y_low = y2;
y_hi = y1;
}
if (x1 < x2)
{
x_low = x1;
x_hi = x2;
}
else
{
x_low = x2;
x_hi = x1;
}
// Must clear previous errors first.
while(glGetError() != GL_NO_ERROR)
{
;
}
if (front)
{
glReadBuffer(static_cast<GLenum>(this->GetFrontLeftBuffer()));
}
else
{
glReadBuffer(static_cast<GLenum>(this->GetBackLeftBuffer()));
}
glDisable( GL_SCISSOR_TEST );
// Calling pack alignment ensures that we can grab the any size window
glPixelStorei( GL_PACK_ALIGNMENT, 1 );
// iOS has issues with getting RGB so we get RGBA
int width = x_hi-x_low + 1;
int height = y_hi-y_low+1;
unsigned char *localData = new unsigned char[width*height*4];
glReadPixels(x_low, y_low, width, height, GL_RGBA,
GL_UNSIGNED_BYTE, localData);
unsigned char *dPtr = data;
unsigned char *lPtr = localData;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
*(dPtr++) = *(lPtr++);
*(dPtr++) = *(lPtr++);
*(dPtr++) = *(lPtr++);
lPtr++;
}
}
delete [] localData;
if (glGetError() != GL_NO_ERROR)
{
return VTK_ERROR;
}
else
{
return VTK_OK;
}
}
//----------------------------------------------------------------------------
void vtkIOSRenderWindow::SetWindowName( const char * _arg )
{
vtkWindow::SetWindowName(_arg);
}
//----------------------------------------------------------------------------
bool vtkIOSRenderWindow::InitializeFromCurrentContext()
{
// NSOpenGLContext *currentContext = [NSOpenGLContext currentContext];
// if (currentContext != NULL)
// {
// UIView *currentView = [currentContext view];
// if (currentView != NULL)
// {
// UIWindow *window = [currentView window];
// this->SetWindowId(currentView);
// this->SetRootWindow(window);
// this->SetContextId((void*)currentContext);
// this->OpenGLInit();
// this->OwnContext = 0;
// return true;
// }
// }
return false;
}
//----------------------------------------------------------------------------
int vtkIOSRenderWindow::GetEventPending()
{
return 0;
}
//----------------------------------------------------------------------------
// Initialize the rendering process.
void vtkIOSRenderWindow::Start()
{
this->Initialize();
// set the current window
this->MakeCurrent();
}
//----------------------------------------------------------------------------
void vtkIOSRenderWindow::MakeCurrent()
{
// if (this->GetContextId())
// {
// [(NSOpenGLContext*)this->GetContextId() makeCurrentContext];
// }
}
// ----------------------------------------------------------------------------
// Description:
// Tells if this window is the current OpenGL context for the calling thread.
bool vtkIOSRenderWindow::IsCurrent()
{
return true;
}
//----------------------------------------------------------------------------
bool vtkIOSRenderWindow::IsDrawable()
{
// you must initialize it first
// else it always evaluates false
this->Initialize();
return true;
}
//----------------------------------------------------------------------------
void vtkIOSRenderWindow::UpdateContext()
{
}
//----------------------------------------------------------------------------
const char* vtkIOSRenderWindow::ReportCapabilities()
{
this->MakeCurrent();
const char* glVendor = (const char*) glGetString(GL_VENDOR);
const char* glRenderer = (const char*) glGetString(GL_RENDERER);
const char* glVersion = (const char*) glGetString(GL_VERSION);
const char* glExtensions = (const char*) glGetString(GL_EXTENSIONS);
std::ostringstream strm;
strm << "OpenGL vendor string: " << glVendor
<< "\nOpenGL renderer string: " << glRenderer
<< "\nOpenGL version string: " << glVersion
<< "\nOpenGL extensions: " << glExtensions << endl;
delete[] this->Capabilities;
size_t len = strm.str().length() + 1;
this->Capabilities = new char[len];
strlcpy(this->Capabilities, strm.str().c_str(), len);
return this->Capabilities;
}
//----------------------------------------------------------------------------
int vtkIOSRenderWindow::SupportsOpenGL()
{
this->MakeCurrent();
if (!this->GetContextId() || !this->GetPixelFormat())
{
return 0;
}
return 1;
}
//----------------------------------------------------------------------------
int vtkIOSRenderWindow::IsDirect()
{
this->MakeCurrent();
if (!this->GetContextId() || !this->GetPixelFormat())
{
return 0;
}
return 1;
}
//----------------------------------------------------------------------------
void vtkIOSRenderWindow::SetSize(int* a)
{
this->SetSize( a[0], a[1] );
}
//----------------------------------------------------------------------------
void vtkIOSRenderWindow::SetSize(int x, int y)
{
static int resizing = 0;
if ((this->Size[0] != x) || (this->Size[1] != y) || (this->GetParentId()))
{
this->Modified();
this->Size[0] = x;
this->Size[1] = y;
}
}
//----------------------------------------------------------------------------
void vtkIOSRenderWindow::SetForceMakeCurrent()
{
this->ForceMakeCurrent = 1;
}
//----------------------------------------------------------------------------
void vtkIOSRenderWindow::SetPosition(int* a)
{
this->SetPosition( a[0], a[1] );
}
//----------------------------------------------------------------------------
void vtkIOSRenderWindow::SetPosition(int x, int y)
{
static int resizing = 0;
if ((this->Position[0] != x) || (this->Position[1] != y)
|| (this->GetParentId()))
{
this->Modified();
this->Position[0] = x;
this->Position[1] = y;
}
}
//----------------------------------------------------------------------------
// End the rendering process and display the image.
void vtkIOSRenderWindow::Frame()
{
this->MakeCurrent();
if (!this->AbortRender && this->DoubleBuffer && this->SwapBuffers)
{
// [(NSOpenGLContext*)this->GetContextId() flushBuffer];
}
}
//----------------------------------------------------------------------------
// Update system if needed due to stereo rendering.
void vtkIOSRenderWindow::StereoUpdate()
{
// if stereo is on and it wasn't before
if (this->StereoRender && (!this->StereoStatus))
{
switch (this->StereoType)
{
case VTK_STEREO_CRYSTAL_EYES:
this->StereoStatus = 1;
break;
case VTK_STEREO_RED_BLUE:
this->StereoStatus = 1;
break;
case VTK_STEREO_ANAGLYPH:
this->StereoStatus = 1;
break;
case VTK_STEREO_DRESDEN:
this->StereoStatus = 1;
break;
case VTK_STEREO_INTERLACED:
this->StereoStatus = 1;
break;
case VTK_STEREO_CHECKERBOARD:
this->StereoStatus = 1;
break;
case VTK_STEREO_SPLITVIEWPORT_HORIZONTAL:
this->StereoStatus = 1;
break;
}
}
else if ((!this->StereoRender) && this->StereoStatus)
{
switch (this->StereoType)
{
case VTK_STEREO_CRYSTAL_EYES:
this->StereoStatus = 0;
break;
case VTK_STEREO_RED_BLUE:
this->StereoStatus = 0;
break;
case VTK_STEREO_ANAGLYPH:
this->StereoStatus = 0;
break;
case VTK_STEREO_DRESDEN:
this->StereoStatus = 0;
break;
case VTK_STEREO_INTERLACED:
this->StereoStatus = 0;
break;
case VTK_STEREO_CHECKERBOARD:
this->StereoStatus = 0;
break;
case VTK_STEREO_SPLITVIEWPORT_HORIZONTAL:
this->StereoStatus = 0;
break;
}
}
}
//----------------------------------------------------------------------------
// Specify various window parameters.
void vtkIOSRenderWindow::WindowConfigure()
{
// this is all handled by the desiredVisualInfo method
}
//----------------------------------------------------------------------------
void vtkIOSRenderWindow::SetupPixelFormat(void*, void*, int, int, int)
{
vtkErrorMacro(<< "vtkIOSRenderWindow::SetupPixelFormat - IMPLEMENT");
}
//----------------------------------------------------------------------------
void vtkIOSRenderWindow::SetupPalette(void*)
{
vtkErrorMacro(<< "vtkIOSRenderWindow::SetupPalette - IMPLEMENT");
}
//----------------------------------------------------------------------------
// Initialize the window for rendering.
void vtkIOSRenderWindow::CreateAWindow()
{
static unsigned count = 1;
this->CreateGLContext();
this->MakeCurrent();
// wipe out any existing display lists
vtkRenderer *renderer = NULL;
vtkCollectionSimpleIterator rsit;
for ( this->Renderers->InitTraversal(rsit);
(renderer = this->Renderers->GetNextRenderer(rsit));)
{
renderer->SetRenderWindow(0);
renderer->SetRenderWindow(this);
}
this->OpenGLInit();
this->Mapped = 1;
}
//----------------------------------------------------------------------------
void vtkIOSRenderWindow::CreateGLContext()
{
}
//----------------------------------------------------------------------------
// Initialize the rendering window.
void vtkIOSRenderWindow::Initialize ()
{
this->OpenGLInit();
this->Mapped = 1;
}
//-----------------------------------------------------------------------------
void vtkIOSRenderWindow::DestroyOffScreenWindow()
{
}
//----------------------------------------------------------------------------
// Get the current size of the window.
int *vtkIOSRenderWindow::GetSize()
{
// if we aren't mapped then just return the ivar
if (!this->Mapped)
{
return this->Superclass::GetSize();
}
return this->Superclass::GetSize();
}
//----------------------------------------------------------------------------
// Get the current size of the screen in pixels.
int *vtkIOSRenderWindow::GetScreenSize()
{
return this->Size;
}
//----------------------------------------------------------------------------
// Get the position in screen coordinates of the window.
int *vtkIOSRenderWindow::GetPosition()
{
return this->Position;
}
//----------------------------------------------------------------------------
// Change the window to fill the entire screen.
void vtkIOSRenderWindow::SetFullScreen(int arg)
{
}
//----------------------------------------------------------------------------
//
// Set the variable that indicates that we want a stereo capable window
// be created. This method can only be called before a window is realized.
//
void vtkIOSRenderWindow::SetStereoCapableWindow(int capable)
{
if (this->GetContextId() == 0)
{
vtkRenderWindow::SetStereoCapableWindow(capable);
}
else
{
vtkWarningMacro(<< "Requesting a StereoCapableWindow must be performed "
<< "before the window is realized, i.e. before a render.");
}
}
//----------------------------------------------------------------------------
// Set the preferred window size to full screen.
void vtkIOSRenderWindow::PrefFullScreen()
{
int *size = this->GetScreenSize();
vtkWarningMacro(<< "Can only set FullScreen before showing window: "
<< size[0] << 'x' << size[1] << ".");
}
//----------------------------------------------------------------------------
// Remap the window.
void vtkIOSRenderWindow::WindowRemap()
{
vtkWarningMacro(<< "Can't remap the window.");
// Acquire the display and capture the screen.
// Create the full-screen window.
// Add the context.
}
//----------------------------------------------------------------------------
void vtkIOSRenderWindow::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "MultiSamples: " << this->MultiSamples << endl;
os << indent << "RootWindow (UIWindow): " << this->GetRootWindow() << endl;
os << indent << "WindowId (UIView): " << this->GetWindowId() << endl;
os << indent << "ParentId: " << this->GetParentId() << endl;
os << indent << "ContextId: " << this->GetContextId() << endl;
os << indent << "PixelFormat: " << this->GetPixelFormat() << endl;
os << indent << "WindowCreated: " << (this->WindowCreated ? "Yes" : "No") << endl;
os << indent << "ViewCreated: " << (this->ViewCreated ? "Yes" : "No") << endl;
}
//----------------------------------------------------------------------------
int vtkIOSRenderWindow::GetDepthBufferSize()
{
if ( this->Mapped )
{
GLint size = 0;
glGetIntegerv( GL_DEPTH_BITS, &size );
return (int) size;
}
else
{
vtkDebugMacro(<< "Window is not mapped yet!" );
return 24;
}
}
//----------------------------------------------------------------------------
// Returns the UIWindow* associated with this vtkRenderWindow.
void *vtkIOSRenderWindow::GetRootWindow()
{
return NULL;
}
//----------------------------------------------------------------------------
// Sets the UIWindow* associated with this vtkRenderWindow.
void vtkIOSRenderWindow::SetRootWindow(void *vtkNotUsed(arg))
{
}
//----------------------------------------------------------------------------
// Returns the UIView* associated with this vtkRenderWindow.
void *vtkIOSRenderWindow::GetWindowId()
{
return NULL;
}
//----------------------------------------------------------------------------
// Sets the UIView* associated with this vtkRenderWindow.
void vtkIOSRenderWindow::SetWindowId(void *vtkNotUsed(arg))
{
}
//----------------------------------------------------------------------------
// Returns the UIView* that is the parent of this vtkRenderWindow.
void *vtkIOSRenderWindow::GetParentId()
{
return NULL;
}
//----------------------------------------------------------------------------
// Sets the UIView* that this vtkRenderWindow should use as a parent.
void vtkIOSRenderWindow::SetParentId(void *vtkNotUsed(arg))
{
}
//----------------------------------------------------------------------------
// Sets the NSOpenGLContext* associated with this vtkRenderWindow.
void vtkIOSRenderWindow::SetContextId(void *vtkNotUsed(contextId))
{
}
//----------------------------------------------------------------------------
// Returns the NSOpenGLContext* associated with this vtkRenderWindow.
void *vtkIOSRenderWindow::GetContextId()
{
return NULL;
}
//----------------------------------------------------------------------------
// Sets the NSOpenGLPixelFormat* associated with this vtkRenderWindow.
void vtkIOSRenderWindow::SetPixelFormat(void *vtkNotUsed(pixelFormat))
{
}
//----------------------------------------------------------------------------
// Returns the NSOpenGLPixelFormat* associated with this vtkRenderWindow.
void *vtkIOSRenderWindow::GetPixelFormat()
{
return NULL;
}
//----------------------------------------------------------------------------
void vtkIOSRenderWindow::SetWindowInfo(char *info)
{
// The paramater is an ASCII string of a decimal number representing
// a pointer to the window. Convert it back to a pointer.
ptrdiff_t tmp = 0;
if (info)
{
(void)sscanf(info, "%tu", &tmp);
}
this->SetWindowId (reinterpret_cast<void *>(tmp));
}
//----------------------------------------------------------------------------
void vtkIOSRenderWindow::SetParentInfo(char *info)
{
// The paramater is an ASCII string of a decimal number representing
// a pointer to the window. Convert it back to a pointer.
ptrdiff_t tmp = 0;
if (info)
{
(void)sscanf(info, "%tu", &tmp);
}
this->SetParentId (reinterpret_cast<void *>(tmp));
}
//----------------------------------------------------------------------------
void vtkIOSRenderWindow::HideCursor()
{
if (this->CursorHidden)
{
return;
}
this->CursorHidden = 1;
}
//----------------------------------------------------------------------------
void vtkIOSRenderWindow::ShowCursor()
{
if (!this->CursorHidden)
{
return;
}
this->CursorHidden = 0;
}
// ---------------------------------------------------------------------------
int vtkIOSRenderWindow::GetWindowCreated()
{
return this->WindowCreated;
}
//----------------------------------------------------------------------------
void vtkIOSRenderWindow::SetCursorPosition(int x, int y)
{
}
//----------------------------------------------------------------------------
void vtkIOSRenderWindow::SetCurrentCursor(int shape)
{
if (this->InvokeEvent(vtkCommand::CursorChangedEvent, &shape))
{
return;
}
this->Superclass::SetCurrentCursor(shape);
}
|