Revision: 6552
          http://playerstage.svn.sourceforge.net/playerstage/?rev=6552&view=rev
Author:   alexcb
Date:     2008-06-12 16:35:26 -0700 (Thu, 12 Jun 2008)

Log Message:
-----------
camera model refactoring

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

Modified: code/stage/trunk/libstage/camera.cc
===================================================================
--- code/stage/trunk/libstage/camera.cc 2008-06-12 23:35:17 UTC (rev 6551)
+++ code/stage/trunk/libstage/camera.cc 2008-06-12 23:35:26 UTC (rev 6552)
@@ -14,7 +14,7 @@
 //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 )
+               _x( 0 ), _y( 0 ), _z( 0 ), _pitch( 90 ), _yaw( 0 ), _z_near( 
0.2 ), _z_far( 40.0 ), _fov( 60 )
 {
 }
 
@@ -33,12 +33,7 @@
 
 void StgPerspectiveCamera::SetProjection( float pixels_width, float 
pixels_height, float y_min, float y_max ) const
 {
-       glMatrixMode (GL_PROJECTION);
-       glLoadIdentity ();
-       
-       gluPerspective( 60.0, pixels_width/pixels_height, _z_near, _z_far );
-       
-       glMatrixMode (GL_MODELVIEW);
+       SetProjection( pixels_width/pixels_height );
 }
 
 void StgPerspectiveCamera::SetProjection( float aspect ) const
@@ -46,7 +41,7 @@
        glMatrixMode (GL_PROJECTION);
        glLoadIdentity ();
        
-       gluPerspective( 60.0, aspect, _z_near, _z_far );
+       gluPerspective( _fov, aspect, _z_near, _z_far );
        
        glMatrixMode (GL_MODELVIEW);
 }

Modified: code/stage/trunk/libstage/canvas.cc
===================================================================
--- code/stage/trunk/libstage/canvas.cc 2008-06-12 23:35:17 UTC (rev 6551)
+++ code/stage/trunk/libstage/canvas.cc 2008-06-12 23:35:26 UTC (rev 6552)
@@ -34,7 +34,7 @@
 
        use_perspective_camera = false;
        perspective_camera.setPose( -3.0, 0.0, 1.0 );
-       perspective_camera._pitch = 70.0; //look down
+       perspective_camera.setPitch( 70.0 ); //look down
        
        startx = starty = 0;
        //panx = pany = stheta = sphi = 0.0;
@@ -175,7 +175,7 @@
                        else
                        {
                                if( use_perspective_camera == true ) {
-                                       perspective_camera._z += Fl::event_dy() 
/ 10.0;
+                                       perspective_camera.scroll( 
Fl::event_dy() / 10.0 );
                                } else {
                                        camera.scale( Fl::event_dy(),  
Fl::event_x(), w(), Fl::event_y(), h() );
                                }
@@ -190,8 +190,8 @@
                                int dy = Fl::event_y() - starty;
 
                                if( use_perspective_camera == true ) {
-                                       perspective_camera._yaw += -dx;
-                                       perspective_camera._pitch += -dy;
+                                       perspective_camera.addYaw( -dx );
+                                       perspective_camera.addPitch( -dy );
                                } else {
                                        camera.pitch( 0.5 * 
static_cast<double>( dy ) );
                                        camera.yaw( 0.5 * static_cast<double>( 
dx ) );
@@ -205,8 +205,7 @@
                                int dy = Fl::event_y() - starty;
 
                                if( use_perspective_camera == true ) {
-                                       perspective_camera._x += -dx / 100.0 * 
perspective_camera._z;
-                                       perspective_camera._y += dy / 100.0 * 
perspective_camera._z;
+                                       perspective_camera.move( -dx, dy, 0.0 );
                                } else {
                                        camera.move( -dx, dy );
 
@@ -359,15 +358,13 @@
 
 void StgCanvas::renderFrame( bool robot_camera )
 {      
-
        uint32_t showflags = this->showflags;
        if( robot_camera == true )
                showflags = STG_SHOW_BLOCKS;
        
-       if( ! (showflags & STG_SHOW_TRAILS) || robot_camera )
+       if( ! (showflags & STG_SHOW_TRAILS) || robot_camera == true )
                glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
 
-
        glPushMatrix();
 
        // draw the world size rectangle in white, using the polygon offset
@@ -532,9 +529,11 @@
 void StgCanvas::draw()
 {
        static bool loaded_texture = false;
-       //  static int centerx = 0, centery = 0;
-       //puts( "CANVAS" );
 
+       //Enable the following to debug camera model
+//     if( loaded_texture == true && use_perspective_camera == true )
+//             return;
+
        if (!valid()) 
        { 
                valid(1);

Modified: code/stage/trunk/libstage/model.cc
===================================================================
--- code/stage/trunk/libstage/model.cc  2008-06-12 23:35:17 UTC (rev 6551)
+++ code/stage/trunk/libstage/model.cc  2008-06-12 23:35:26 UTC (rev 6552)
@@ -967,7 +967,7 @@
                DrawBlinkenlights();
 
        // draw speech bubble
-       if( this->say_string )
+       if( say_string && flags & STG_SHOW_STATUS )
        {
                float stheta = -dtor( canvas->camera.getPitch() );
                float sphi = dtor( canvas->camera.getYaw() );

Modified: code/stage/trunk/libstage/model_camera.cc
===================================================================
--- code/stage/trunk/libstage/model_camera.cc   2008-06-12 23:35:17 UTC (rev 
6551)
+++ code/stage/trunk/libstage/model_camera.cc   2008-06-12 23:35:26 UTC (rev 
6552)
@@ -55,7 +55,11 @@
 {
        StgModel::Load();
        Worldfile* wf = world->GetWorldFile();
-
+       int fov = wf->ReadLength( id, "fov",    -1 );
+       if( fov > 0 ) {
+               _camera.setFov( fov );
+       }
+       
 }
 
 
@@ -66,11 +70,10 @@
 
 float* StgModelCamera::laser()
 {
-       int h = 32;
-       int w = 32;
+       //TODO allow the h and w to be passed by user
+       int h = 320;
+       int w = 320;
        
-       static StgPerspectiveCamera camera;
-
        static GLfloat* data_gl = NULL;
        static GLfloat* data = NULL;
        if( data == NULL ) {
@@ -80,20 +83,25 @@
        }
        
        glViewport( 0, 0, w, h );
-       camera.update();
-       camera.SetProjection( 1.0 );
+       _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();
+       _camera.setPose( parent->GetGlobalPose().x, parent->GetGlobalPose().y, 
0.3 );
+       _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 ] );
+               data[ w-1-i ] = _camera.realDistance( data_gl[ i ] );
        }
+       for( int i = 0; i < 32; i++ ) {
+               for( int j = 1; j < 10; j++ )
+                       data[ i ] += data[ i * 10 + j ];
+               data[ i ] /= 10.0;
+       }
        _canvas->invalidate();
        return data;
 }

Modified: code/stage/trunk/libstage/stage.hh
===================================================================
--- code/stage/trunk/libstage/stage.hh  2008-06-12 23:35:17 UTC (rev 6551)
+++ code/stage/trunk/libstage/stage.hh  2008-06-12 23:35:26 UTC (rev 6552)
@@ -521,6 +521,7 @@
        const uint32_t STG_SHOW_FOOTPRINT =  (1<<10);
        const uint32_t STG_SHOW_BLOCKS_2D =  (1<<10);
        const uint32_t STG_SHOW_TRAILRISE =  (1<<11);
+       const uint32_t STG_SHOW_STATUS =     (1<<12);
 
        // forward declare
        class StgWorld;
@@ -1712,13 +1713,14 @@
 
 class StgPerspectiveCamera : public StgCamera
 {
-       public: //TODO make this private
+       private:
                float _x, _y, _z;
                float _pitch; //left-right (about y)
                float _yaw; //up-down (about x)
-       private:
+
                float _z_near;
                float _z_far;
+               float _fov;
 
        public:
                StgPerspectiveCamera( void );
@@ -1729,13 +1731,33 @@
                void update( void );
 
                inline void setPose( float x, float y, float z ) { _x = x; _y = 
y; _z = z; }
+               inline void addPose( float x, float y, float z ) { _x += x; _y 
+= y; _z += z; if( _z < 0.1 ) _z = 0.1; }
+               inline void move( float x, float y, float z )
+               {
+                       //scale relative to zoom level
+                       _x *= _z;
+                       _y *= _z;
+                       
+                       //adjust for yaw angle
+                       std::cout << "yaw:" << _yaw << std::endl;
+                       _x += cos( dtor( _yaw ) ) * x;
+                       _y += -sin( dtor( _yaw ) ) * x;
+                       
+                       _x += sin( dtor( _yaw ) ) * y;
+                       _y += cos( dtor( _yaw ) ) * y;
+               }
+               inline void setFov( float fov ) { _fov = fov; }
                inline void setYaw( float yaw ) { _yaw = yaw; }
+               inline void addYaw( float yaw ) { _yaw += yaw; }
+               inline void setPitch( float pitch ) { _pitch = pitch; }
+               inline void addPitch( float pitch ) { _pitch += pitch; }
        
                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 ) );
                }
+               inline void scroll( float dy ) { _z += dy; }
 };
 
 class StgOrthoCamera : public StgCamera
@@ -1759,7 +1781,7 @@
                        //adjust for pitch angle
                        y = y / cos( dtor( _pitch ) );
 
-                       //adjust for yaw andle
+                       //adjust for yaw angle
                        _x += cos( dtor( _yaw ) ) * x;
                        _y += -sin( dtor( _yaw ) ) * x;
 
@@ -2369,6 +2391,8 @@
                int _frame_data_width;
                int _frame_data_height;
        
+               StgPerspectiveCamera _camera;
+       
        public:
 
                StgModelCamera( StgWorld* world,


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