VegaPrime Code Example 1

 
World To Screen Coordinate Conversion

 

     

    This example function illustrates how to :

     

    • How to convert a 3d World coordinate point into 2d Screen coordinate point

     

Code :

--
     
     bool
     getWorld2ScreenCoord( vpChannel   *channel,
                           vpTransform *xform,
                           float        *scrX,
                           float       *scrY ){
      
      

    vuMatrixf world2screen;

    vuVec4f point;

    vuVec3d pos;

    vuVec3d posTest1;

       
    //
    // Grab the Current View matrix
    //
    vuMatrix viewMat = channel->getViewMatrix();
     
    //  
    // Look at vector, set to +Y
    //
    vuVec3d  at;
     
    at.set( 0.0, 1.0, 0.0 );
     
    //
    // make Look at vector point in direction of Look
    //
    viewMat.transformPoint( &at );
     
    //  
    // put Look at vector at eyepoint
    //
    at -= viewMat[ 3 ];
     
    //  
    // Next map xform's position to screen space
    //
    xform->getAbsolutePosition( &pos );
     
    //  
    // pos1 used to test whether xform is in front of observer
    //
    posTest1 = pos;
     
    //
    // put xform position vector at eyepoint
    //
    posTest1 -= viewMat[ 3 ];
     
    //  
    // only draw if xform is in front of observer
    //
     
    if ( at.dot( posTest1 ) > 0.0 ) {
     
      //
      // find center point of tracked xform in normalized screen coords
      // screen coords get viewport and world to screen matrix
      //
       int ox, oy, sx, sy;
       
      channel->getVrChannel()->getViewport( &ox, &oy, &sx, &sy );
       
      world2screen = channel->getVrChannel()->getWorldToScreenMatrix();
       
      point.set( pos[0], pos[1], pos[2], 1.0f );
       
      //  
      // transform the points to clip space
      //
      world2screen.transform( &point );
       
      //  
      // divide by w
      //
      float fx = point.m_vec[ 0 ] / point.m_vec[ 3 ];
      float fy = point.m_vec[ 1 ] / point.m_vec[ 3 ];
       
      //  
      // normalize to [0,1] range
      //  
      *scrX = ( fx + 1.0f ) / 2.0f;
      &scrY = ( fy + 1.0f ) / 2.0f;
       
      //  
      // centerX and centerY are the normalized channel coordinates
      //
       
      return true;
      }
     
     
    return false;
     
    }  // getWorld2ScreenCoord
     

     

 

 

starline3.gif

© Copyright 2001-2005 Gordon Tomlinson  All Rights Reserved.

All logos, trademarks and copyrights in this site are property of their respective owner.