Revision: 6545
          http://playerstage.svn.sourceforge.net/playerstage/?rev=6545&view=rev
Author:   alexcb
Date:     2008-06-11 17:17:52 -0700 (Wed, 11 Jun 2008)

Log Message:
-----------
added perspective camera view option, and simulated laser model with opengl 
depth buffer

Modified Paths:
--------------
    code/stage/trunk/libstage/camera.cc
    code/stage/trunk/libstage/canvas.cc
    code/stage/trunk/libstage/model_camera.cc
    code/stage/trunk/libstage/stage.hh
    code/stage/trunk/libstage/texture_manager.cc
    code/stage/trunk/libstage/worldgui.cc

Modified: code/stage/trunk/libstage/camera.cc
===================================================================
--- code/stage/trunk/libstage/camera.cc 2008-06-11 20:54:18 UTC (rev 6544)
+++ code/stage/trunk/libstage/camera.cc 2008-06-12 00:17:52 UTC (rev 6545)
@@ -3,7 +3,6 @@
  *  Stage
  *
  *  Created by Alex Couture-Beil on 06/06/08.
- *  Copyright 2008 __MyCompanyName__. All rights reserved.
  *
  */
 
@@ -14,15 +13,19 @@
 
 //perspective camera
 //perspective camera
+StgPerspectiveCamera::StgPerspectiveCamera( void ) : 
+               _x( 0 ), _y( 0 ), _z( 0 ), _pitch( 90 ), _yaw( 0 ), _z_near( 
0.1 ), _z_far( 20.0 )
+{
+}
+
 void StgPerspectiveCamera::Draw( void ) const
 {      
        glMatrixMode (GL_MODELVIEW);
        glLoadIdentity ();
-
+       
        glRotatef( - _pitch, 1.0, 0.0, 0.0 );
        glRotatef( - _yaw, 0.0, 0.0, 1.0 );
 
-       std::cout << "y: " << _z << std::endl;
        glTranslatef( - _x, - _y, - _z );
        //zooming needs to happen in the Projection code (don't use glScale for 
zoom)
 
@@ -32,19 +35,25 @@
 {
        glMatrixMode (GL_PROJECTION);
        glLoadIdentity ();
+       
+       gluPerspective( 60.0, pixels_width/pixels_height, _z_near, _z_far );
+       
+       glMatrixMode (GL_MODELVIEW);
+}
 
-       gluPerspective( 120.0, pixels_width/pixels_height, 0.01, 100 );
-
+void StgPerspectiveCamera::SetProjection( float aspect ) const
+{
+       glMatrixMode (GL_PROJECTION);
+       glLoadIdentity ();
+       
+       gluPerspective( 60.0, aspect, _z_near, _z_far );
+       
        glMatrixMode (GL_MODELVIEW);
 }
 
 void StgPerspectiveCamera::update( void )
 {      
-       //      _x = 0;
-       //      _y = 0;
-       //      _z = i;
-       _pitch = 90.0;
-       //      _yaw = 90.0;
+
 }
 
 
@@ -53,7 +62,6 @@
 
 
 
-
 
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //Ortho camera
 
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

Modified: code/stage/trunk/libstage/canvas.cc
===================================================================
--- code/stage/trunk/libstage/canvas.cc 2008-06-11 20:54:18 UTC (rev 6544)
+++ code/stage/trunk/libstage/canvas.cc 2008-06-12 00:17:52 UTC (rev 6545)
@@ -31,6 +31,10 @@
        selected_models = NULL;
        last_selection = NULL;
 
+       use_perspective_camera = false;
+       perspective_camera.setPose( -3.0, 0.0, 1.0 );
+       perspective_camera._pitch = 70.0; //look down
+       
        startx = starty = 0;
        //panx = pany = stheta = sphi = 0.0;
        //scale = 15.0;
@@ -169,7 +173,11 @@
                        }
                        else
                        {
-                               camera.scale( Fl::event_dy(),  Fl::event_x(), 
w(), Fl::event_y(), h() );
+                               if( use_perspective_camera == true ) {
+                                       perspective_camera._z += Fl::event_dy() 
/ 10.0;
+                               } else {
+                                       camera.scale( Fl::event_dy(),  
Fl::event_x(), w(), Fl::event_y(), h() );
+                               }
                                invalidate();
                        }
                        return 1;
@@ -180,22 +188,35 @@
                                int dx = Fl::event_x() - startx;
                                int dy = Fl::event_y() - starty;
 
-                               camera.pitch( 0.5 * static_cast<double>( dy ) );
-                               camera.yaw( 0.5 * static_cast<double>( dx ) );
+                               if( use_perspective_camera == true ) {
+                                       perspective_camera._yaw += -dx;
+                                       perspective_camera._pitch += -dy;
+                               } else {
+                                       camera.pitch( 0.5 * 
static_cast<double>( dy ) );
+                                       camera.yaw( 0.5 * static_cast<double>( 
dx ) );
+                               }
 
                                invalidate();
                        }
                        else if( Fl::event_state( FL_ALT ) )
-                       {       
+                       {   
                                int dx = Fl::event_x() - startx;
                                int dy = Fl::event_y() - starty;
 
-                               camera.move( -dx, dy );
+                               if( use_perspective_camera == true ) {
+                                       perspective_camera._x += -dx / 100.0 * 
perspective_camera._z;
+                                       perspective_camera._y += dy / 100.0 * 
perspective_camera._z;
+                               } else {
+                                       camera.move( -dx, dy );
+
+                               }
                                invalidate();
+
                        }
 
                        startx = Fl::event_x();
                        starty = Fl::event_y();
+                       
                        return 1;
 
                case FL_PUSH: // button pressed
@@ -545,9 +566,13 @@
                // install a font
                gl_font( FL_HELVETICA, 12 );
 
-               stg_bounds3d_t extent = world->GetExtent();
-               camera.SetProjection( w(), h(), extent.y.min, extent.y.max );
-               camera.Draw();
+               if( use_perspective_camera == true ) {
+                       perspective_camera.SetProjection( w(), h(), 0.01, 0.5 );
+               } else {
+                       stg_bounds3d_t extent = world->GetExtent();
+                       camera.SetProjection( w(), h(), extent.y.min, 
extent.y.max );
+                       camera.Draw();
+               }
 
                // enable vertex arrays
                glEnableClientState( GL_VERTEX_ARRAY );
@@ -566,6 +591,10 @@
                camera.Draw();
        }
        
+       if( use_perspective_camera == true ) {
+               perspective_camera.Draw();
+       }
+       
        renderFrame();
 }
 

Modified: code/stage/trunk/libstage/model_camera.cc
===================================================================
--- code/stage/trunk/libstage/model_camera.cc   2008-06-11 20:54:18 UTC (rev 
6544)
+++ code/stage/trunk/libstage/model_camera.cc   2008-06-12 00:17:52 UTC (rev 
6545)
@@ -64,8 +64,41 @@
        StgModel::Update();
 }
 
+float* StgModelCamera::laser()
+{
+       int h = 32;
+       int w = 32;
+       
+       static StgPerspectiveCamera camera;
 
-const char* StgModelCamera::GetFrame( int width, int height )
+       static GLfloat* data_gl = NULL;
+       static GLfloat* data = NULL;
+       if( data == NULL ) {
+               data = new GLfloat[ h * w ];
+               data_gl = new GLfloat[ h * w ];
+               
+       }
+       
+       glViewport( 0, 0, w, h );
+       camera.update();
+       camera.SetProjection( 1.0 );
+       
+       camera.setPose( parent->GetGlobalPose().x, parent->GetGlobalPose().y, 
0.1 );
+       camera.setYaw( rtod( parent->GetGlobalPose().a ) - 90.0 );
+       camera.Draw();
+
+       _canvas->renderFrame( true );
+       
+       glReadPixels(0, h / 2, w, 1, GL_DEPTH_COMPONENT, GL_FLOAT, data_gl );
+
+       for( int i = 0; i < w; i++ ) {
+               data[ w-1-i ] = camera.realDistance( data_gl[ i ] );
+       }
+       _canvas->invalidate();
+       return data;
+}
+
+const char* StgModelCamera::GetFrame( int width, int height, bool depth_buffer 
)
 {
        static StgPerspectiveCamera camera; //shared between _ALL_ instances
        
@@ -75,24 +108,32 @@
        if( width != _frame_data_width || height != _frame_data_height ) {
                if( _frame_data != NULL )
                        delete _frame_data;
-               _frame_data = new char[ 3 * width * height ];
+               _frame_data = new char[ 4 * width * height ]; //assumes a max 
of depth 4
        }
+
        
        glViewport( 0, 0, width, height );
-       
        camera.update();
-       camera.SetProjection( width, height, 0.01, 200.0 );
+       camera.SetProjection( width, height, 0.0, 0.0 );
        camera.setPose( parent->GetGlobalPose().x, parent->GetGlobalPose().y, 
0.1 );
        camera.setYaw( rtod( parent->GetGlobalPose().a ) - 90.0 );
        camera.Draw();
        
        _canvas->renderFrame( true );
        
-       glReadPixels(0, 0, width, height,
-                                GL_RGB,
-                                GL_UNSIGNED_BYTE,
-                                _frame_data );
-       
+       if( depth_buffer == true ) {
+               glReadPixels(0, 0, width, height,
+                                        GL_DEPTH_COMPONENT, //GL_RGB,
+                                        GL_FLOAT, //GL_UNSIGNED_BYTE,
+                                        _frame_data );
+       } else {
+               glReadPixels(0, 0, width, height,
+                                        GL_RGB,
+                                        GL_UNSIGNED_BYTE,
+                                        _frame_data );         
+       }
+
+               
        _canvas->invalidate();
        return _frame_data;
 }

Modified: code/stage/trunk/libstage/stage.hh
===================================================================
--- code/stage/trunk/libstage/stage.hh  2008-06-11 20:54:18 UTC (rev 6544)
+++ code/stage/trunk/libstage/stage.hh  2008-06-12 00:17:52 UTC (rev 6545)
@@ -1703,26 +1703,36 @@
                StgCamera() { }
                virtual ~StgCamera() { }
 
-               virtual void Draw() const = 0;
+               virtual void Draw( void ) const = 0;
                virtual void SetProjection( float pixels_width, float 
pixels_height, float y_min, float y_max ) const = 0;
 };
 
 class StgPerspectiveCamera : public StgCamera
 {
-       private:
+       public: //TODO make this private
                float _x, _y, _z;
                float _pitch; //left-right (about y)
                float _yaw; //up-down (about x)
+       private:
+               float _z_near;
+               float _z_far;
 
        public:
-               StgPerspectiveCamera( void ) : _x( 0 ), _y( 0 ), _z( 0 ), 
_pitch( 0 ), _yaw( 0 ) { }
+               StgPerspectiveCamera( void );
 
-               virtual void Draw() const;
-               virtual void SetProjection( float pixels_width, float 
pixels_height, float y_min, float y_max ) const;                  
+               virtual void Draw( void ) const;
+               virtual void SetProjection( float pixels_width, float 
pixels_height, float y_min, float y_max ) const;
+               void SetProjection( float aspect ) const;
                void update( void );
 
                inline void setPose( float x, float y, float z ) { _x = x; _y = 
y; _z = z; }
                inline void setYaw( float yaw ) { _yaw = yaw; }
+       
+               inline float realDistance( float z_buf_val ) const {
+                       //formulat found at 
http://www.cs.unc.edu/~hoff/techrep/openglz.html
+                       //Z = Zn*Zf / (Zf - z*(Zf-Zn))
+                       return _z_near * _z_far / ( _z_far - z_buf_val * ( 
_z_far - _z_near ) );
+               }
 };
 
 class StgOrthoCamera : public StgCamera
@@ -1738,7 +1748,6 @@
                virtual void Draw() const;
                virtual void SetProjection( float pixels_width, float 
pixels_height, float y_min, float y_max ) const;
 
-
                inline void move( float x, float y ) {
                        //convert screen points into world points
                        x = x / ( _scale );
@@ -1794,6 +1803,8 @@
        GlColorStack colorstack;
 
        StgOrthoCamera camera;
+       StgPerspectiveCamera perspective_camera;
+       bool use_perspective_camera;
 
        int startx, starty;
        bool dragging;
@@ -2369,7 +2380,10 @@
                virtual void Draw( uint32_t flags, StgCanvas* canvas );
        
                ///Take a screenshot from the camera's perspective
-               const char* GetFrame( int width, int height );
+               const char* GetFrame( int width, int height, bool depth_buffer 
);
+       
+               ///Imiate laser scan
+               float* laser();
 
                // static wrapper for the constructor - all models must 
implement
                // this method and add an entry in typetable.cc
@@ -2379,7 +2393,7 @@
                                char* typestr )
                { 
                        return (StgModel*)new StgModelCamera( world, parent, 
id, typestr );
-               }    
+               }
 };
 
 // POSITION MODEL --------------------------------------------------------

Modified: code/stage/trunk/libstage/texture_manager.cc
===================================================================
--- code/stage/trunk/libstage/texture_manager.cc        2008-06-11 20:54:18 UTC 
(rev 6544)
+++ code/stage/trunk/libstage/texture_manager.cc        2008-06-12 00:17:52 UTC 
(rev 6545)
@@ -3,7 +3,6 @@
  *  Stage
  *
  *  Created by Alex Couture-Beil on 03/06/08.
- *  Copyright 2008 __MyCompanyName__. All rights reserved.
  *
  */
 

Modified: code/stage/trunk/libstage/worldgui.cc
===================================================================
--- code/stage/trunk/libstage/worldgui.cc       2008-06-11 20:54:18 UTC (rev 
6544)
+++ code/stage/trunk/libstage/worldgui.cc       2008-06-12 00:17:52 UTC (rev 
6545)
@@ -116,6 +116,7 @@
 static const char* MITEM_VIEW_BLOCKSRISING =  "&View/T&rails/&Blocks rising";
 static const char* MITEM_VIEW_ARROWS =     "&View/T&rails/&Arrows rising";
 static const char* MITEM_VIEW_TRAILS =     "&View/&Trail";
+static const char* MITEM_VIEW_PERSPECTIVE = "&View/Perspective camera";
 
        // hack - get this from somewhere sensible, like CMake's config file
        const char* PACKAGE_STRING = "Stage-3.dev";
@@ -158,7 +159,9 @@
        mbar->add( MITEM_VIEW_FOLLOW,    'f', (Fl_Callback*)view_toggle_cb, 
(void*)canvas, 
                        FL_MENU_TOGGLE| (canvas->showflags & STG_SHOW_FOLLOW ? 
FL_MENU_VALUE : 0 ));
        mbar->add( MITEM_VIEW_CLOCK,    'c', (Fl_Callback*)view_toggle_cb, 
(void*)canvas, 
-                       FL_MENU_TOGGLE| (canvas->showflags & STG_SHOW_CLOCK ? 
FL_MENU_VALUE : 0 ));
+                         FL_MENU_TOGGLE| (canvas->showflags & STG_SHOW_CLOCK ? 
FL_MENU_VALUE : 0 ));
+       mbar->add( MITEM_VIEW_PERSPECTIVE,   'r', (Fl_Callback*)view_toggle_cb, 
(void*)canvas, 
+                         FL_MENU_TOGGLE| (canvas->use_perspective_camera ));
 
        mbar->add( MITEM_VIEW_TRAILS,    't', (Fl_Callback*)view_toggle_cb, 
(void*)canvas, 
                        FL_MENU_TOGGLE| (canvas->showflags & STG_SHOW_TRAILS ? 
FL_MENU_VALUE : 0 ));
@@ -398,6 +401,7 @@
        else if( strcmp(picked, MITEM_VIEW_ARROWS ) == 0 ) canvas->InvertView( 
STG_SHOW_ARROWS );
        else if( strcmp(picked, MITEM_VIEW_TRAILS ) == 0 ) canvas->InvertView( 
STG_SHOW_TRAILS );
        else if( strcmp(picked, MITEM_VIEW_BLOCKSRISING ) == 0 ) 
canvas->InvertView( STG_SHOW_TRAILRISE );
+       else if( strcmp(picked, MITEM_VIEW_PERSPECTIVE ) == 0 ) { 
canvas->use_perspective_camera = ! canvas->use_perspective_camera; 
canvas->invalidate(); }
        else PRINT_ERR1( "Unrecognized menu item \"%s\" not handled", picked );
 
        //printf( "value: %d\n", item->value() );


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Playerstage-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/playerstage-commit

Reply via email to