- Timestamp:
- 06/08/07 12:16:33 (6 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/examples/osgviewerCocoa/ViewerCocoa.mm
r6892 r6901 268 268 269 269 // osg::setNotifyLevel( osg::DEBUG_FP ); 270 Viewer = new osgViewer::Viewer;271 graphicsWindow = Viewer->setUpViewerAsEmbeddedInWindow(0,0,800,800);270 theViewer = new osgViewer::Viewer; 271 graphicsWindow = theViewer->setUpViewerAsEmbeddedInWindow(0,0,740,650); // numbers from Nib 272 272 // Builts in Stats handler 273 Viewer->addEventHandler(new osgViewer::StatsHandler);273 theViewer->addEventHandler(new osgViewer::StatsHandler); 274 274 #ifdef VIEWER_USE_SHARED_CONTEXTS 275 275 // Workaround: osgViewer::Viewer automatically increments its context ID values. … … 281 281 282 282 // Cocoa follows the same coordinate convention as OpenGL. osgViewer's default is inverted. 283 Viewer->getEventQueue()->getCurrentEventState()->setMouseYOrientation(osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS);283 theViewer->getEventQueue()->getCurrentEventState()->setMouseYOrientation(osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS); 284 284 // Use a trackball manipulator...matches nicely with the Mighty Mouse Scrollball. 285 Viewer->setCameraManipulator(new osgGA::TrackballManipulator);285 theViewer->setCameraManipulator(new osgGA::TrackballManipulator); 286 286 287 287 } … … 326 326 [animationTimer invalidate]; 327 327 [animationTimer release]; 328 delete Viewer;329 Viewer = NULL;328 delete theViewer; 329 theViewer = NULL; 330 330 [super dealloc]; 331 331 } … … 333 333 - (void) finalize 334 334 { 335 delete Viewer;336 Viewer = NULL;335 delete theViewer; 336 theViewer = NULL; 337 337 [super finalize]; 338 338 } … … 358 358 // See Technical Note TN2085 Enabling multi-threaded execution of the OpenGL framework 359 359 // http://developer.apple.com/technotes/tn2006/tn2085.html 360 // For this simple viewer, you are probably not going to see a speed boost, but possibly a speed hit, 361 // since we probably don't have much data to dispatch, 362 // but it is enabled anyway for demonstration purposes. 360 363 uint64_t num_cpus = 0; 361 364 size_t num_cpus_length = sizeof(num_cpus); 362 365 // Multithreaded engine only benefits with muliple CPUs, so do CPU count check 366 // I've been told that Apple has started doing this check behind the scenes in some version of Tiger. 363 367 if(sysctlbyname("hw.activecpu", &num_cpus, &num_cpus_length, NULL, 0) == 0) 364 368 { … … 367 371 { 368 372 // Cleared to enable multi-threaded engine 369 CGLError error_val = CGLEnable((CGLContextObj)[[self openGLContext] CGLContextObj], kCGLCEMPEngine); 373 // kCGLCEMPEngine may not be defined before certain versions of Tiger/Xcode, 374 // so use the numeric value 313 instead to keep things compiling. 375 CGLError error_val = CGLEnable((CGLContextObj)[[self openGLContext] CGLContextObj], static_cast<CGLContextEnable>(313)); 370 376 if(error_val != 0) 371 377 { … … 423 429 the_geode->addDrawable(default_text.get()); 424 430 425 Viewer->setSceneData(the_geode.get());431 theViewer->setSceneData(the_geode.get()); 426 432 } 427 433 … … 458 464 - (void) prepareForMiniaturization:(NSNotification*)notification 459 465 { 466 [[self openGLContext] makeCurrentContext]; 460 467 NSBitmapImageRep* ns_image_rep = [self renderOpenGLSceneToFramebuffer]; 461 468 if([self lockFocusIfCanDraw]) … … 518 525 NSPoint converted_point = [self convertPoint:the_point fromView:nil]; 519 526 520 Viewer->getEventQueue()->mouseMotion(converted_point.x, converted_point.y);527 theViewer->getEventQueue()->mouseMotion(converted_point.x, converted_point.y); 521 528 [self setNeedsDisplay:YES]; 522 529 } … … 556 563 NSPoint converted_point = [self convertPoint:the_point fromView:nil]; 557 564 558 Viewer->getEventQueue()->mouseMotion(converted_point.x, converted_point.y);565 theViewer->getEventQueue()->mouseMotion(converted_point.x, converted_point.y); 559 566 [self setNeedsDisplay:YES]; 560 567 } … … 591 598 NSPoint converted_point = [self convertPoint:the_point fromView:nil]; 592 599 593 Viewer->getEventQueue()->mouseMotion(converted_point.x, converted_point.y);600 theViewer->getEventQueue()->mouseMotion(converted_point.x, converted_point.y); 594 601 [self setNeedsDisplay:YES]; 595 602 } … … 645 652 if([the_event clickCount] == 1) 646 653 { 647 Viewer->getEventQueue()->mouseButtonPress(converted_point.x, converted_point.y, 1);654 theViewer->getEventQueue()->mouseButtonPress(converted_point.x, converted_point.y, 1); 648 655 } 649 656 else 650 657 { 651 Viewer->getEventQueue()->mouseDoubleButtonPress(converted_point.x, converted_point.y, 1);658 theViewer->getEventQueue()->mouseDoubleButtonPress(converted_point.x, converted_point.y, 1); 652 659 } 653 660 [self setNeedsDisplay:YES]; … … 661 668 NSPoint converted_point = [self convertPoint:the_point fromView:nil]; 662 669 663 Viewer->getEventQueue()->mouseButtonRelease(converted_point.x, converted_point.y, 1);670 theViewer->getEventQueue()->mouseButtonRelease(converted_point.x, converted_point.y, 1); 664 671 [self setNeedsDisplay:YES]; 665 672 } … … 673 680 if([the_event clickCount] == 1) 674 681 { 675 Viewer->getEventQueue()->mouseButtonPress(converted_point.x, converted_point.y, 3);682 theViewer->getEventQueue()->mouseButtonPress(converted_point.x, converted_point.y, 3); 676 683 } 677 684 else 678 685 { 679 Viewer->getEventQueue()->mouseDoubleButtonPress(converted_point.x, converted_point.y, 3);686 theViewer->getEventQueue()->mouseDoubleButtonPress(converted_point.x, converted_point.y, 3); 680 687 } 681 688 [self setNeedsDisplay:YES]; … … 690 697 NSPoint converted_point = [self convertPoint:the_point fromView:nil]; 691 698 692 Viewer->getEventQueue()->mouseButtonRelease(converted_point.x, converted_point.y, 3);699 theViewer->getEventQueue()->mouseButtonRelease(converted_point.x, converted_point.y, 3); 693 700 [self setNeedsDisplay:YES]; 694 701 } … … 703 710 if([the_event clickCount] == 1) 704 711 { 705 Viewer->getEventQueue()->mouseButtonPress(converted_point.x, converted_point.y, 2);712 theViewer->getEventQueue()->mouseButtonPress(converted_point.x, converted_point.y, 2); 706 713 } 707 714 else 708 715 { 709 Viewer->getEventQueue()->mouseDoubleButtonPress(converted_point.x, converted_point.y, 2);716 theViewer->getEventQueue()->mouseDoubleButtonPress(converted_point.x, converted_point.y, 2); 710 717 } 711 718 [self setNeedsDisplay:YES]; … … 721 728 if([the_event clickCount] == 1) 722 729 { 723 Viewer->getEventQueue()->mouseButtonPress(converted_point.x, converted_point.y, button_number+1);730 theViewer->getEventQueue()->mouseButtonPress(converted_point.x, converted_point.y, button_number+1); 724 731 } 725 732 else 726 733 { 727 Viewer->getEventQueue()->mouseDoubleButtonPress(converted_point.x, converted_point.y, button_number+1);734 theViewer->getEventQueue()->mouseDoubleButtonPress(converted_point.x, converted_point.y, button_number+1); 728 735 } 729 736 [self setNeedsDisplay:YES]; … … 737 744 NSPoint the_point = [the_event locationInWindow]; 738 745 NSPoint converted_point = [self convertPoint:the_point fromView:nil]; 739 Viewer->getEventQueue()->mouseButtonRelease(converted_point.x, converted_point.y, 2);746 theViewer->getEventQueue()->mouseButtonRelease(converted_point.x, converted_point.y, 2); 740 747 [self setNeedsDisplay:YES]; 741 748 } … … 747 754 NSPoint the_point = [the_event locationInWindow]; 748 755 NSPoint converted_point = [self convertPoint:the_point fromView:nil]; 749 Viewer->getEventQueue()->mouseButtonRelease(converted_point.x, converted_point.y, button_number+1);756 theViewer->getEventQueue()->mouseButtonRelease(converted_point.x, converted_point.y, button_number+1); 750 757 751 758 [self setNeedsDisplay:YES]; … … 765 772 // The camera manipulators don't seem to implement any code that utilize the scroll values. 766 773 // This this call does nothing. 767 // Viewer->getEventQueue()->mouseScroll2D([the_event deltaX], [the_event deltaY]);774 // theViewer->getEventQueue()->mouseScroll2D([the_event deltaX], [the_event deltaY]); 768 775 769 776 // With the absense of a useful mouseScroll2D API, we can manually simulate the desired effect. 770 777 NSPoint the_point = [the_event locationInWindow]; 771 778 NSPoint converted_point = [self convertPoint:the_point fromView:nil]; 772 Viewer->getEventQueue()->mouseButtonPress(converted_point.x, converted_point.y, 1);773 Viewer->getEventQueue()->mouseMotion(converted_point.x + -[the_event deltaX], converted_point.y + [the_event deltaY]);774 Viewer->getEventQueue()->mouseButtonRelease(converted_point.x + -[the_event deltaX], converted_point.y + [the_event deltaY], 1);779 theViewer->getEventQueue()->mouseButtonPress(converted_point.x, converted_point.y, 1); 780 theViewer->getEventQueue()->mouseMotion(converted_point.x + -[the_event deltaX], converted_point.y + [the_event deltaY]); 781 theViewer->getEventQueue()->mouseButtonRelease(converted_point.x + -[the_event deltaX], converted_point.y + [the_event deltaY], 1); 775 782 776 783 [self setNeedsDisplay:YES]; … … 798 805 unichar unicode_character = [event_characters characterAtIndex:0]; 799 806 // NSLog(@"unicode_character: %d", unicode_character); 800 Viewer->getEventQueue()->keyPress(static_cast<osgGA::GUIEventAdapter::KeySymbol>(unicode_character));807 theViewer->getEventQueue()->keyPress(static_cast<osgGA::GUIEventAdapter::KeySymbol>(unicode_character)); 801 808 802 809 [self setNeedsDisplay:YES]; … … 809 816 // NSString* event_characters = [the_event charactersIgnoringModifiers]; 810 817 unichar unicode_character = [event_characters characterAtIndex:0]; 811 Viewer->getEventQueue()->keyRelease(static_cast<osgGA::GUIEventAdapter::KeySymbol>(unicode_character));818 theViewer->getEventQueue()->keyRelease(static_cast<osgGA::GUIEventAdapter::KeySymbol>(unicode_character)); 812 819 [self setNeedsDisplay:YES]; 813 820 } … … 843 850 // This coordinate system conversion seems to make things work with Quartz Debug. 844 851 NSSize size_in_window_coordinates = [self convertSize:size_in_points toView:nil]; 845 Viewer->getEventQueue()->windowResize(0, 0, size_in_window_coordinates.width, size_in_window_coordinates.height);852 theViewer->getEventQueue()->windowResize(0, 0, size_in_window_coordinates.width, size_in_window_coordinates.height); 846 853 graphicsWindow->resized(0, 0, size_in_window_coordinates.width, size_in_window_coordinates.height); 847 854 } … … 862 869 if([[NSGraphicsContext currentContext] isDrawingToScreen]) 863 870 { 864 //[[self openGLContext] makeCurrentContext];865 Viewer->frame();871 [[self openGLContext] makeCurrentContext]; 872 theViewer->frame(); 866 873 [[self openGLContext] flushBuffer]; 867 874 } 868 875 else // This is usually the print case 869 876 { 870 //[[self openGLContext] makeCurrentContext];877 [[self openGLContext] makeCurrentContext]; 871 878 872 879 // FIXME: We should be computing a size that fits best to the paper target … … 904 911 NSSize size_in_points = [self bounds].size; 905 912 NSSize size_in_window_coordinates = [self convertSize:size_in_points toView:nil]; 906 const osg::Vec4& clear_color = Viewer->getCamera()->getClearColor();913 const osg::Vec4& clear_color = theViewer->getCamera()->getClearColor(); 907 914 908 915 return [self renderOpenGLSceneToFramebufferAsFormat:GL_RGB viewWidth:size_in_window_coordinates.width viewHeight:size_in_window_coordinates.height clearColorRed:clear_color[0] clearColorGreen:clear_color[1] clearColorBlue:clear_color[2] clearColorAlpha:clear_color[3]]; … … 912 919 - (NSBitmapImageRep*) renderOpenGLSceneToFramebufferAsFormat:(int)gl_format viewWidth:(float)view_width viewHeight:(float)view_height 913 920 { 914 const osg::Vec4& clear_color = Viewer->getCamera()->getClearColor();921 const osg::Vec4& clear_color = theViewer->getCamera()->getClearColor(); 915 922 916 923 return [self renderOpenGLSceneToFramebufferAsFormat:gl_format viewWidth:view_width viewHeight:view_height clearColorRed:clear_color[0] clearColorGreen:clear_color[1] clearColorBlue:clear_color[2] clearColorAlpha:clear_color[3]]; … … 1004 1011 NSSize original_size_in_points = [self bounds].size; 1005 1012 NSSize original_size_in_window_coordinates = [self convertSize:original_size_in_points toView:nil]; 1006 // Viewer->getEventQueue()->windowResize(0, 0, original_size_in_window_coordinates.width, original_size_in_window_coordinates.height);1007 1008 Viewer->getEventQueue()->windowResize(0, 0, viewport_width, viewport_height);1013 // theViewer->getEventQueue()->windowResize(0, 0, original_size_in_window_coordinates.width, original_size_in_window_coordinates.height); 1014 1015 theViewer->getEventQueue()->windowResize(0, 0, viewport_width, viewport_height); 1009 1016 graphicsWindow->resized(0, 0, viewport_width, viewport_height); 1010 1017 … … 1025 1032 */ 1026 1033 //NSLog(@"Before camera glGetError: %s", gluErrorString(glGetError())); 1027 osg::Camera* root_camera = Viewer->getCamera();1034 osg::Camera* root_camera = theViewer->getCamera(); 1028 1035 1029 1036 // I originally tried the clone() method and the copy construction, but it didn't work right, … … 1049 1056 // We need to insert the new (second) camera into the scene (below the root camera) and attach 1050 1057 // the scene data to the new camera. 1051 osg::ref_ptr<osg::Node> root_node = Viewer->getSceneData();1058 osg::ref_ptr<osg::Node> root_node = theViewer->getSceneData(); 1052 1059 1053 1060 the_camera->addChild(root_node.get()); 1054 1061 // Don't call (bypass) Viewer's setSceneData, but the underlying SceneView's setSceneData. 1055 1062 // Otherwise, the camera position gets reset to the home position. 1056 Viewer->setSceneData(the_camera);1063 theViewer->setSceneData(the_camera); 1057 1064 1058 1065 // set the camera to render before the main camera. … … 1071 1078 1072 1079 // Render the scene 1073 Viewer->frame();1080 theViewer->frame(); 1074 1081 1075 1082 // Not sure if I really need this (seems to work without it), and if so, not sure if I need flush or finish … … 1089 1096 // Don't call (bypass) Viewer's setSceneData, but the underlying SceneView's setSceneData. 1090 1097 // Otherwise, the camera position gets reset to the home position. 1091 Viewer->setSceneData(root_node.get());1092 Viewer->getEventQueue()->windowResize(0, 0, original_size_in_window_coordinates.width, original_size_in_window_coordinates.height);1098 theViewer->setSceneData(root_node.get()); 1099 theViewer->getEventQueue()->windowResize(0, 0, original_size_in_window_coordinates.width, original_size_in_window_coordinates.height); 1093 1100 graphicsWindow->resized(0, 0, original_size_in_window_coordinates.width, original_size_in_window_coordinates.height); 1094 1101 … … 1171 1178 return NO; 1172 1179 } 1173 Viewer->setSceneData(loaded_model.get());1180 theViewer->setSceneData(loaded_model.get()); 1174 1181 return YES; 1175 1182 } … … 1190 1197 return NO; 1191 1198 } 1192 Viewer->setSceneData(loaded_model.get());1199 theViewer->setSceneData(loaded_model.get()); 1193 1200 return YES; 1194 1201 } … … 1225 1232 - (NSData*) dataWithTIFFOfContentView 1226 1233 { 1234 [[self openGLContext] makeCurrentContext]; 1227 1235 NSBitmapImageRep * image = [self renderOpenGLSceneToFramebuffer]; 1228 1236 NSData* data = nil; … … 1259 1267 // Create the image that will be dragged 1260 1268 NSString * type = NSTIFFPboardType; 1269 1270 [[self openGLContext] makeCurrentContext]; 1261 1271 1262 1272 // I want two images. One to be rendered for the target, and one as the drag-image. … … 1328 1338 - (IBAction) resetPosition:(id)the_sender 1329 1339 { 1330 // osgGA::MatrixManipulator* camera_manipulator = Viewer->getCameraManipulator();1340 // osgGA::MatrixManipulator* camera_manipulator = theViewer->getCameraManipulator(); 1331 1341 // This only resets the position 1332 1342 // camera_manipulator->home(0.0); 1333 1343 1334 // There is no external API from Viewer that I can see that will stop movement.1344 // There is no external API from SimpleViewer that I can see that will stop movement. 1335 1345 // So fake the 'spacebar' to stop things and reset. 1336 printf("I'am here"); 1337 1338 Viewer->home(); 1346 // (Changed in Viewer?) 1347 // printf("I'am here"); 1348 1349 // Reset to start position 1350 theViewer->home(); 1339 1351 [self setNeedsDisplay:YES]; 1340 1352 } … … 1348 1360 NSColor* the_color = [the_sender color]; 1349 1361 1350 Viewer->getCamera()->setClearColor(1362 theViewer->getCamera()->setClearColor( 1351 1363 osg::Vec4( 1352 1364 [the_color redComponent],
