Show
Ignore:
Timestamp:
06/08/07 12:16:33 (8 years ago)
Author:
robert
Message:

From Eric Wing, "So, something strange happened to the repo and some of this project's
files got messed up, most notiably the Nib and also the Localized
strings file. I didn't notice the latter until now so Martin is
missing this file.

Anyway, the attached tar contains all new versions of all the
necessary files. There are cleanups and fixes to a lot of things.
Martin did a good job porting the thing to osg::Viewer so most of the
code changes I made address other areas.

Two things I noticed in the new port you might want to consider as
feedback. First, there might be a bug with osgViewer when the view
size goes to 0. If you play with the splitviews in this program and
shrink the view until it is closed, and then re-expand it, the model
doesn't come back, not even after a home() call. SimpleViewer? didn't
have this problem.

Second, a more minor thing, this program has a
take-screenshot--and-copy-to-clipboard feature via Cmd-C (or Menu
item). I achieve this by using osg::Camera to render to an FBO and
then copy the contents to Cocoa. To insert the camera, I manipulate
the scenegraph so I can get the camera node in and out. I end up
calling setSceneData at the end of eveything to restore everything to
the original state before I started mucking with the scenegraph. This
unfortunately, triggers a home() reset. So in this particular case, it
make Copy look like it's changing the scene. The old SimpleViewer? had
the same problem, but I was able to work around it by directly
invoking the underlying SceneView?'s setSceneData so the home()
mechanism was bypassed. The viewer design seems to protect this data
more carefully so the bypass trick won't work. My feedback is that
maybe a flag or extra parameter can be introduced so a reset is not
triggered if not desired.

I have checked in a ton of Xcode fixes for the entire build process in
general so once this piece gets checked in, hopefully everything will
build cleanly."

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/examples/osgviewerCocoa/ViewerCocoa.mm

    r6892 r6901  
    268268 
    269269//      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 
    272272        // Builts in Stats handler 
    273         Viewer->addEventHandler(new osgViewer::StatsHandler); 
     273        theViewer->addEventHandler(new osgViewer::StatsHandler); 
    274274#ifdef VIEWER_USE_SHARED_CONTEXTS 
    275275        // Workaround: osgViewer::Viewer automatically increments its context ID values. 
     
    281281 
    282282        // 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); 
    284284        // Use a trackball manipulator...matches nicely with the Mighty Mouse Scrollball. 
    285         Viewer->setCameraManipulator(new osgGA::TrackballManipulator); 
     285        theViewer->setCameraManipulator(new osgGA::TrackballManipulator); 
    286286         
    287287} 
     
    326326        [animationTimer invalidate]; 
    327327        [animationTimer release]; 
    328         delete Viewer; 
    329         Viewer = NULL; 
     328        delete theViewer; 
     329        theViewer = NULL; 
    330330        [super dealloc]; 
    331331} 
     
    333333- (void) finalize 
    334334{ 
    335         delete Viewer; 
    336         Viewer = NULL; 
     335        delete theViewer; 
     336        theViewer = NULL; 
    337337        [super finalize]; 
    338338} 
     
    358358        // See Technical Note TN2085 Enabling multi-threaded execution of the OpenGL framework 
    359359        // 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. 
    360363        uint64_t num_cpus = 0; 
    361364        size_t num_cpus_length = sizeof(num_cpus); 
    362365        // 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. 
    363367        if(sysctlbyname("hw.activecpu", &num_cpus, &num_cpus_length, NULL, 0) == 0) 
    364368        { 
     
    367371                { 
    368372                        // 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)); 
    370376                        if(error_val != 0) 
    371377                        { 
     
    423429        the_geode->addDrawable(default_text.get()); 
    424430 
    425         Viewer->setSceneData(the_geode.get()); 
     431        theViewer->setSceneData(the_geode.get()); 
    426432} 
    427433 
     
    458464- (void) prepareForMiniaturization:(NSNotification*)notification 
    459465{ 
     466        [[self openGLContext] makeCurrentContext]; 
    460467        NSBitmapImageRep* ns_image_rep = [self renderOpenGLSceneToFramebuffer]; 
    461468        if([self lockFocusIfCanDraw]) 
     
    518525    NSPoint converted_point = [self convertPoint:the_point fromView:nil]; 
    519526         
    520         Viewer->getEventQueue()->mouseMotion(converted_point.x, converted_point.y); 
     527        theViewer->getEventQueue()->mouseMotion(converted_point.x, converted_point.y); 
    521528        [self setNeedsDisplay:YES]; 
    522529} 
     
    556563    NSPoint converted_point = [self convertPoint:the_point fromView:nil]; 
    557564         
    558         Viewer->getEventQueue()->mouseMotion(converted_point.x, converted_point.y); 
     565        theViewer->getEventQueue()->mouseMotion(converted_point.x, converted_point.y); 
    559566        [self setNeedsDisplay:YES]; 
    560567} 
     
    591598    NSPoint converted_point = [self convertPoint:the_point fromView:nil]; 
    592599         
    593         Viewer->getEventQueue()->mouseMotion(converted_point.x, converted_point.y); 
     600        theViewer->getEventQueue()->mouseMotion(converted_point.x, converted_point.y); 
    594601        [self setNeedsDisplay:YES]; 
    595602} 
     
    645652        if([the_event clickCount] == 1) 
    646653        { 
    647                 Viewer->getEventQueue()->mouseButtonPress(converted_point.x, converted_point.y, 1); 
     654                theViewer->getEventQueue()->mouseButtonPress(converted_point.x, converted_point.y, 1); 
    648655        } 
    649656        else 
    650657        { 
    651                 Viewer->getEventQueue()->mouseDoubleButtonPress(converted_point.x, converted_point.y, 1); 
     658                theViewer->getEventQueue()->mouseDoubleButtonPress(converted_point.x, converted_point.y, 1); 
    652659        } 
    653660        [self setNeedsDisplay:YES]; 
     
    661668    NSPoint converted_point = [self convertPoint:the_point fromView:nil]; 
    662669         
    663         Viewer->getEventQueue()->mouseButtonRelease(converted_point.x, converted_point.y, 1); 
     670        theViewer->getEventQueue()->mouseButtonRelease(converted_point.x, converted_point.y, 1); 
    664671        [self setNeedsDisplay:YES]; 
    665672} 
     
    673680        if([the_event clickCount] == 1) 
    674681        { 
    675                 Viewer->getEventQueue()->mouseButtonPress(converted_point.x, converted_point.y, 3); 
     682                theViewer->getEventQueue()->mouseButtonPress(converted_point.x, converted_point.y, 3); 
    676683        } 
    677684        else 
    678685        { 
    679                 Viewer->getEventQueue()->mouseDoubleButtonPress(converted_point.x, converted_point.y, 3); 
     686                theViewer->getEventQueue()->mouseDoubleButtonPress(converted_point.x, converted_point.y, 3); 
    680687        } 
    681688        [self setNeedsDisplay:YES]; 
     
    690697    NSPoint converted_point = [self convertPoint:the_point fromView:nil]; 
    691698         
    692         Viewer->getEventQueue()->mouseButtonRelease(converted_point.x, converted_point.y, 3); 
     699        theViewer->getEventQueue()->mouseButtonRelease(converted_point.x, converted_point.y, 3); 
    693700        [self setNeedsDisplay:YES]; 
    694701} 
     
    703710        if([the_event clickCount] == 1) 
    704711        { 
    705                 Viewer->getEventQueue()->mouseButtonPress(converted_point.x, converted_point.y, 2); 
     712                theViewer->getEventQueue()->mouseButtonPress(converted_point.x, converted_point.y, 2); 
    706713        } 
    707714        else 
    708715        { 
    709                 Viewer->getEventQueue()->mouseDoubleButtonPress(converted_point.x, converted_point.y, 2); 
     716                theViewer->getEventQueue()->mouseDoubleButtonPress(converted_point.x, converted_point.y, 2); 
    710717        } 
    711718        [self setNeedsDisplay:YES]; 
     
    721728        if([the_event clickCount] == 1) 
    722729        { 
    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); 
    724731        } 
    725732        else 
    726733        { 
    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); 
    728735        } 
    729736        [self setNeedsDisplay:YES]; 
     
    737744        NSPoint the_point = [the_event locationInWindow]; 
    738745        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); 
    740747        [self setNeedsDisplay:YES]; 
    741748} 
     
    747754        NSPoint the_point = [the_event locationInWindow]; 
    748755        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); 
    750757         
    751758        [self setNeedsDisplay:YES]; 
     
    765772        // The camera manipulators don't seem to implement any code that utilize the scroll values. 
    766773        // 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]); 
    768775 
    769776        // With the absense of a useful mouseScroll2D API, we can manually simulate the desired effect. 
    770777        NSPoint the_point = [the_event locationInWindow]; 
    771778        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); 
    775782 
    776783        [self setNeedsDisplay:YES]; 
     
    798805        unichar unicode_character = [event_characters characterAtIndex:0]; 
    799806//      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)); 
    801808 
    802809        [self setNeedsDisplay:YES]; 
     
    809816//      NSString* event_characters = [the_event charactersIgnoringModifiers]; 
    810817        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)); 
    812819        [self setNeedsDisplay:YES]; 
    813820} 
     
    843850        // This coordinate system conversion seems to make things work with Quartz Debug. 
    844851        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); 
    846853        graphicsWindow->resized(0, 0, size_in_window_coordinates.width, size_in_window_coordinates.height); 
    847854} 
     
    862869        if([[NSGraphicsContext currentContext] isDrawingToScreen]) 
    863870        { 
    864 //              [[self openGLContext] makeCurrentContext]; 
    865                 Viewer->frame(); 
     871                [[self openGLContext] makeCurrentContext]; 
     872                theViewer->frame(); 
    866873                [[self openGLContext] flushBuffer]; 
    867874        } 
    868875        else // This is usually the print case 
    869876        { 
    870 //              [[self openGLContext] makeCurrentContext]; 
     877                [[self openGLContext] makeCurrentContext]; 
    871878 
    872879                // FIXME: We should be computing a size that fits best to the paper target 
     
    904911        NSSize size_in_points = [self bounds].size; 
    905912        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(); 
    907914 
    908915        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]]; 
     
    912919- (NSBitmapImageRep*) renderOpenGLSceneToFramebufferAsFormat:(int)gl_format viewWidth:(float)view_width viewHeight:(float)view_height 
    913920{ 
    914         const osg::Vec4& clear_color = Viewer->getCamera()->getClearColor(); 
     921        const osg::Vec4& clear_color = theViewer->getCamera()->getClearColor(); 
    915922 
    916923        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]]; 
     
    10041011        NSSize original_size_in_points = [self bounds].size; 
    10051012        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); 
    10091016        graphicsWindow->resized(0, 0, viewport_width, viewport_height); 
    10101017 
     
    10251032         */ 
    10261033        //NSLog(@"Before camera glGetError: %s", gluErrorString(glGetError())); 
    1027         osg::Camera* root_camera = Viewer->getCamera(); 
     1034        osg::Camera* root_camera = theViewer->getCamera(); 
    10281035 
    10291036        // I originally tried the clone() method and the copy construction, but it didn't work right, 
     
    10491056        // We need to insert the new (second) camera into the scene (below the root camera) and attach  
    10501057        // 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(); 
    10521059 
    10531060        the_camera->addChild(root_node.get()); 
    10541061        // Don't call (bypass) Viewer's setSceneData, but the underlying SceneView's setSceneData. 
    10551062        // Otherwise, the camera position gets reset to the home position. 
    1056         Viewer->setSceneData(the_camera); 
     1063        theViewer->setSceneData(the_camera); 
    10571064 
    10581065        // set the camera to render before the main camera. 
     
    10711078 
    10721079        // Render the scene 
    1073         Viewer->frame(); 
     1080        theViewer->frame(); 
    10741081 
    10751082        // Not sure if I really need this (seems to work without it), and if so, not sure if I need flush or finish 
     
    10891096        // Don't call (bypass) Viewer's setSceneData, but the underlying SceneView's setSceneData. 
    10901097        // 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); 
    10931100        graphicsWindow->resized(0, 0, original_size_in_window_coordinates.width, original_size_in_window_coordinates.height); 
    10941101 
     
    11711178                        return NO; 
    11721179                } 
    1173                 Viewer->setSceneData(loaded_model.get()); 
     1180                theViewer->setSceneData(loaded_model.get()); 
    11741181                return YES; 
    11751182    } 
     
    11901197                        return NO; 
    11911198                } 
    1192                 Viewer->setSceneData(loaded_model.get()); 
     1199                theViewer->setSceneData(loaded_model.get()); 
    11931200                return YES; 
    11941201        } 
     
    12251232- (NSData*) dataWithTIFFOfContentView 
    12261233{ 
     1234        [[self openGLContext] makeCurrentContext]; 
    12271235        NSBitmapImageRep * image = [self renderOpenGLSceneToFramebuffer]; 
    12281236        NSData* data = nil; 
     
    12591267        // Create the image that will be dragged 
    12601268        NSString * type = NSTIFFPboardType; 
     1269 
     1270        [[self openGLContext] makeCurrentContext]; 
    12611271 
    12621272        // I want two images. One to be rendered for the target, and one as the drag-image. 
     
    13281338- (IBAction) resetPosition:(id)the_sender 
    13291339{ 
    1330         //      osgGA::MatrixManipulator* camera_manipulator = Viewer->getCameraManipulator(); 
     1340        //      osgGA::MatrixManipulator* camera_manipulator = theViewer->getCameraManipulator(); 
    13311341        // This only resets the position 
    13321342        //      camera_manipulator->home(0.0); 
    13331343         
    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. 
    13351345        // 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(); 
    13391351        [self setNeedsDisplay:YES]; 
    13401352} 
     
    13481360        NSColor* the_color = [the_sender color]; 
    13491361 
    1350         Viewer->getCamera()->setClearColor( 
     1362        theViewer->getCamera()->setClearColor( 
    13511363                osg::Vec4( 
    13521364                        [the_color redComponent],