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
|
/*=========================================================================
Program: Visualization Toolkit
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.
=========================================================================*/
#import "MyGLKViewController.h"
#import "vtkIOSRenderWindow.h"
#import "vtkIOSRenderWindowInteractor.h"
#include "vtkRenderingOpenGL2ObjectFactory.h"
#include "vtkParametricTorus.h"
#include "vtkParametricBoy.h"
#include "vtkParametricConicSpiral.h"
#include "vtkParametricCrossCap.h"
#include "vtkParametricDini.h"
#include "vtkParametricEllipsoid.h"
#include "vtkParametricEnneper.h"
#include "vtkParametricFigure8Klein.h"
#include "vtkParametricKlein.h"
#include "vtkParametricMobius.h"
#include "vtkParametricRandomHills.h"
#include "vtkParametricRoman.h"
#include "vtkParametricSpline.h"
#include "vtkParametricSuperEllipsoid.h"
#include "vtkParametricSuperToroid.h"
#include "vtkParametricTorus.h"
#include "vtkSmartPointer.h"
#include "vtkActor.h"
#include "vtkActor2D.h"
#include "vtkCamera.h"
#include "vtkCommand.h"
#include "vtkInteractorStyleMultiTouchCamera.h"
#include "vtkMath.h"
#include "vtkParametricFunctionSource.h"
#include "vtkPoints.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderer.h"
#include "vtkTextMapper.h"
#include "vtkTextProperty.h"
#include <deque>
/* 2 or 3 -- needs to match VTK version */
#define GL_ES_VERSION 2
@interface MyGLKViewController () {
std::deque<vtkSmartPointer<vtkParametricFunction> > parametricObjects;
}
@property (strong, nonatomic) EAGLContext *context;
- (void)tearDownGL;
@end
@implementation MyGLKViewController
//----------------------------------------------------------------------------
- (vtkIOSRenderWindow *)getVTKRenderWindow
{
return _myVTKRenderWindow;
}
//----------------------------------------------------------------------------
- (void)setVTKRenderWindow:(vtkIOSRenderWindow *)theVTKRenderWindow
{
_myVTKRenderWindow = theVTKRenderWindow;
}
//----------------------------------------------------------------------------
- (vtkIOSRenderWindowInteractor *)getInteractor
{
if (_myVTKRenderWindow)
{
return (vtkIOSRenderWindowInteractor *)_myVTKRenderWindow->GetInteractor();
}
else
{
return NULL;
}
}
- (void)initializeParametricObjects
{
parametricObjects.push_back(vtkSmartPointer<vtkParametricBoy>::New());
parametricObjects.push_back(vtkSmartPointer<vtkParametricConicSpiral>::New());
parametricObjects.push_back(vtkSmartPointer<vtkParametricCrossCap>::New());
parametricObjects.push_back(vtkSmartPointer<vtkParametricDini>::New());
auto ellipsoid = vtkSmartPointer<vtkParametricEllipsoid>::New();
ellipsoid->SetXRadius(0.5);
ellipsoid->SetYRadius(2.0);
parametricObjects.push_back(ellipsoid);
parametricObjects.push_back(vtkSmartPointer<vtkParametricEnneper>::New());
parametricObjects.push_back(vtkSmartPointer<vtkParametricFigure8Klein>::New());
parametricObjects.push_back(vtkSmartPointer<vtkParametricKlein>::New());
auto mobius = vtkSmartPointer<vtkParametricMobius>::New();
mobius->SetRadius(2.0);
mobius->SetMinimumV(-0.5);
mobius->SetMaximumV(0.5);
parametricObjects.push_back(mobius);
vtkSmartPointer<vtkParametricRandomHills> randomHills =
vtkSmartPointer<vtkParametricRandomHills>::New();
randomHills->AllowRandomGenerationOff();
parametricObjects.push_back(randomHills);
parametricObjects.push_back(vtkSmartPointer<vtkParametricRoman>::New());
auto superEllipsoid = vtkSmartPointer<vtkParametricSuperEllipsoid>::New();
superEllipsoid->SetN1(0.5);
superEllipsoid->SetN2(0.1);
parametricObjects.push_back(superEllipsoid);
auto superToroid = vtkSmartPointer<vtkParametricSuperToroid>::New();
superToroid->SetN1(0.2);
superToroid->SetN2(3.0);
parametricObjects.push_back(superToroid);
parametricObjects.push_back(vtkSmartPointer<vtkParametricTorus>::New());
// The spline needs points
vtkSmartPointer<vtkParametricSpline> spline =
vtkSmartPointer<vtkParametricSpline>::New();
vtkSmartPointer<vtkPoints> inputPoints =
vtkSmartPointer<vtkPoints>::New();
vtkMath::RandomSeed(8775070);
for (int p = 0; p < 10; p++)
{
double x = vtkMath::Random(0.0, 1.0);
double y = vtkMath::Random(0.0, 1.0);
double z = vtkMath::Random(0.0, 1.0);
inputPoints->InsertNextPoint(x, y, z);
}
spline->SetPoints(inputPoints);
parametricObjects.push_back(spline);
}
- (void)setupPipeline
{
// Register GL2 objects
vtkObjectFactory::RegisterFactory(vtkRenderingOpenGL2ObjectFactory::New());
//
// Create the parametric objects
//
[self initializeParametricObjects];
std::vector<vtkSmartPointer<vtkParametricFunctionSource> > parametricFunctionSources;
std::vector<vtkSmartPointer<vtkRenderer> > renderers;
std::vector<vtkSmartPointer<vtkPolyDataMapper> > mappers;
std::vector<vtkSmartPointer<vtkActor> > actors;
// No text mappers/actors in VTK GL2 yet
#if 0
//std::vector<vtkSmartPointer<vtkTextMapper> > textmappers;
//std::vector<vtkSmartPointer<vtkActor2D> > textactors;
// Create one text property for all
//vtkSmartPointer<vtkTextProperty> textProperty =
//vtkSmartPointer<vtkTextProperty>::New();
//textProperty->SetFontSize(10);
//textProperty->SetJustificationToCentered();
#endif
// Create a parametric function source, renderer, mapper, and actor
// for each object
for(unsigned int i = 0; i < parametricObjects.size(); i++)
{
parametricFunctionSources.push_back(vtkSmartPointer<vtkParametricFunctionSource>::New());
parametricFunctionSources[i]->SetParametricFunction(parametricObjects[i]);
parametricFunctionSources[i]->Update();
auto mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
assert(mapper);
mappers.push_back(mapper);
mappers[i]->SetInputConnection(parametricFunctionSources[i]->GetOutputPort());
actors.push_back(vtkSmartPointer<vtkActor>::New());
actors[i]->SetMapper(mappers[i]);
// No text mappers/actors in VTK GL2 yet
#if 0
textmappers.push_back(vtkSmartPointer<vtkTextMapper>::New());
textmappers[i]->SetInput(parametricObjects[i]->GetClassName());
textmappers[i]->SetTextProperty(textProperty);
textactors.push_back(vtkSmartPointer<vtkActor2D>::New());
textactors[i]->SetMapper(textmappers[i]);
textactors[i]->SetPosition(100, 16);
#endif
renderers.push_back(vtkSmartPointer<vtkRenderer>::New());
}
unsigned int gridDimensions = 4;
// Need a renderer even if there is no actor
for(size_t i = parametricObjects.size();
i < gridDimensions * gridDimensions;
i++)
{
renderers.push_back(vtkSmartPointer<vtkRenderer>::New());
}
vtkIOSRenderWindow *renWin = vtkIOSRenderWindow::New();
//renWin->DebugOn();
[self setVTKRenderWindow:renWin];
int rendererSize = 200;
renWin->SetSize(rendererSize*gridDimensions, rendererSize*gridDimensions);
for(int row = 0; row < static_cast<int>(gridDimensions); row++)
{
for(int col = 0; col < static_cast<int>(gridDimensions); col++)
{
int index = row*gridDimensions + col;
// (xmin, ymin, xmax, ymax)
double viewport[4] =
{static_cast<double>(col) * rendererSize / (gridDimensions * rendererSize),
static_cast<double>(gridDimensions - (row+1)) * rendererSize / (gridDimensions * rendererSize),
static_cast<double>(col+1)*rendererSize / (gridDimensions * rendererSize),
static_cast<double>(gridDimensions - row) * rendererSize / (gridDimensions * rendererSize)};
renWin->AddRenderer(renderers[index]);
renderers[index]->SetViewport(viewport);
if(index > static_cast<int>(parametricObjects.size() - 1))
{
continue;
}
renderers[index]->AddActor(actors[index]);
//renderers[index]->AddActor(textactors[index]);
renderers[index]->SetBackground(.2, .3, .4);
renderers[index]->ResetCamera();
renderers[index]->GetActiveCamera()->Azimuth(30);
renderers[index]->GetActiveCamera()->Elevation(-30);
renderers[index]->GetActiveCamera()->Zoom(0.9);
renderers[index]->ResetCameraClippingRange();
}
}
// this example uses VTK's built in interaction but you could choose
// to use your own instead.
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renWin);
vtkInteractorStyleMultiTouchCamera *ismt = vtkInteractorStyleMultiTouchCamera::New();
iren->SetInteractorStyle(ismt);
ismt->Delete();
iren->Start();
}
- (void)viewDidLoad
{
[super viewDidLoad];
#if GL_ES_VERSION == 2
self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
#else
self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3];
#endif
if (!self.context) {
NSLog(@"Failed to create ES context");
}
GLKView *view = (GLKView *)self.view;
view.context = self.context;
view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
//view.drawableMultisample = GLKViewDrawableMultisample4X;
// setup the vis pipeline
[self setupPipeline];
[EAGLContext setCurrentContext:self.context];
[self resizeView];
[self getVTKRenderWindow]->Render();
}
- (void)dealloc
{
[self tearDownGL];
if ([EAGLContext currentContext] == self.context) {
[EAGLContext setCurrentContext:nil];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
if ([self isViewLoaded] && ([[self view] window] == nil)) {
self.view = nil;
[self tearDownGL];
if ([EAGLContext currentContext] == self.context) {
[EAGLContext setCurrentContext:nil];
}
self.context = nil;
}
// Dispose of any resources that can be recreated.
}
- (void)tearDownGL
{
[EAGLContext setCurrentContext:self.context];
// free GL resources
// ...
}
-(void) resizeView
{
double scale = self.view.contentScaleFactor;
double newWidth = scale * self.view.bounds.size.width;
double newHeight = scale * self.view.bounds.size.height;
[self getVTKRenderWindow]->SetSize(newWidth, newHeight);
}
- (void)viewWillLayoutSubviews
{
[self resizeView];
}
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
//std::cout << [self getVTKRenderWindow]->ReportCapabilities() << std::endl;
[self getVTKRenderWindow]->Render();
}
//=================================================================
// this example uses VTK's built in interaction but you could choose
// to use your own instead. The remaining methods forward touch events
// to VTKs interactor.
// Handles the start of a touch
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
vtkIOSRenderWindowInteractor *interactor = [self getInteractor];
if (!interactor)
{
return;
}
vtkIOSRenderWindow *renWin = [self getVTKRenderWindow];
if (!renWin)
{
return;
}
CGRect bounds = [self.view bounds];
double scale = self.view.contentScaleFactor;
// set the position for all contacts
NSSet *myTouches = [event touchesForView:self.view];
for (UITouch *touch in myTouches)
{
// Convert touch point from UIView referential to OpenGL one (upside-down flip)
CGPoint location = [touch locationInView:self.view];
location.y = bounds.size.height - location.y;
// Account for the content scaling factor
location.x *= scale;
location.y *= scale;
int index = interactor->GetPointerIndexForContact((size_t)(__bridge void *)touch);
if (index < VTKI_MAX_POINTERS)
{
interactor->SetEventInformation((int)round(location.x),
(int)round(location.y),
0, 0,
0, 0, 0, index);
}
}
// handle begin events
for (UITouch *touch in touches)
{
int index = interactor->GetPointerIndexForContact((size_t)(__bridge void *)touch);
interactor->SetPointerIndex(index);
interactor->InvokeEvent(vtkCommand::LeftButtonPressEvent,NULL);
//NSLog(@"Starting left mouse");
}
// Display the buffer
[(GLKView *)self.view display];
}
// Handles the continuation of a touch.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
vtkIOSRenderWindowInteractor *interactor = [self getInteractor];
if (!interactor)
{
return;
}
vtkIOSRenderWindow *renWin = [self getVTKRenderWindow];
if (!renWin)
{
return;
}
CGRect bounds = [self.view bounds];
double scale = self.view.contentScaleFactor;
// set the position for all contacts
int index;
NSSet *myTouches = [event touchesForView:self.view];
for (UITouch *touch in myTouches)
{
// Convert touch point from UIView referential to OpenGL one (upside-down flip)
CGPoint location = [touch locationInView:self.view];
location.y = bounds.size.height - location.y;
// Account for the content scaling factor
location.x *= scale;
location.y *= scale;
index = interactor->GetPointerIndexForContact((size_t)(__bridge void *)touch);
if (index < VTKI_MAX_POINTERS)
{
interactor->SetEventInformation((int)round(location.x),
(int)round(location.y),
0, 0,
0, 0, 0, index);
}
}
// fire move event on last index
interactor->SetPointerIndex(index);
interactor->InvokeEvent(vtkCommand::MouseMoveEvent,NULL);
// NSLog(@"Moved left mouse");
// Display the buffer
[(GLKView *)self.view display];
}
// Handles the end of a touch event when the touch is a tap.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
vtkIOSRenderWindowInteractor *interactor = [self getInteractor];
if (!interactor)
{
return;
}
vtkIOSRenderWindow *renWin = [self getVTKRenderWindow];
if (!renWin)
{
return;
}
CGRect bounds = [self.view bounds];
double scale = self.view.contentScaleFactor;
// set the position for all contacts
NSSet *myTouches = [event touchesForView:self.view];
for (UITouch *touch in myTouches)
{
// Convert touch point from UIView referential to OpenGL one (upside-down flip)
CGPoint location = [touch locationInView:self.view];
location.y = bounds.size.height - location.y;
// Account for the content scaling factor
location.x *= scale;
location.y *= scale;
int index = interactor->GetPointerIndexForContact((size_t)(__bridge void *)touch);
if (index < VTKI_MAX_POINTERS)
{
interactor->SetEventInformation((int)round(location.x),
(int)round(location.y),
0, 0,
0, 0, 0, index);
}
}
// handle begin events
for (UITouch *touch in touches)
{
int index = interactor->GetPointerIndexForContact((size_t)(__bridge void *)touch);
interactor->SetPointerIndex(index);
interactor->InvokeEvent(vtkCommand::LeftButtonReleaseEvent,NULL);
interactor->ClearContact((size_t)(__bridge void *)touch);
//NSLog(@"lifting left mouse");
}
// Display the buffer
[(GLKView *)self.view display];
}
// Handles the end of a touch event.
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
vtkIOSRenderWindowInteractor *interactor = [self getInteractor];
if (!interactor)
{
return;
}
vtkIOSRenderWindow *renWin = [self getVTKRenderWindow];
if (!renWin)
{
return;
}
CGRect bounds = [self.view bounds];
double scale = self.view.contentScaleFactor;
UITouch* touch = [[event touchesForView:self.view] anyObject];
// Convert touch point from UIView referential to OpenGL one (upside-down flip)
CGPoint location = [touch locationInView:self.view];
location.y = bounds.size.height - location.y;
// Account for the content scaling factor
location.x *= scale;
location.y *= scale;
interactor->SetEventInformation((int)round(location.x),
(int)round(location.y),
0, 0,
0, 0);
interactor->InvokeEvent(vtkCommand::LeftButtonReleaseEvent, NULL);
// NSLog(@"Ended left mouse");
// Display the buffer
[(GLKView *)self.view display];
}
@end
|