Revision: 6585
          http://playerstage.svn.sourceforge.net/playerstage/?rev=6585&view=rev
Author:   alexcb
Date:     2008-06-16 10:48:06 -0700 (Mon, 16 Jun 2008)

Log Message:
-----------
camera model data visualization

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

Modified: code/stage/trunk/libstage/camera.cc
===================================================================
--- code/stage/trunk/libstage/camera.cc 2008-06-16 16:15:18 UTC (rev 6584)
+++ code/stage/trunk/libstage/camera.cc 2008-06-16 17:48:06 UTC (rev 6585)
@@ -18,6 +18,20 @@
 {
 }
 
+void StgPerspectiveCamera::move( float x, float y, float z )
+{
+       //scale relative to zoom level
+       x *= _z / 100.0;
+       y *= _z / 100.0;
+       
+       //adjust for yaw angle
+       _x += cos( dtor( _yaw ) ) * x;
+       _x += -sin( dtor( _yaw ) ) * y;
+       
+       _y += sin( dtor( _yaw ) ) * x;
+       _y += cos( dtor( _yaw ) ) * y;
+       }
+
 void StgPerspectiveCamera::Draw( void ) const
 {      
        glMatrixMode (GL_MODELVIEW);
@@ -33,7 +47,26 @@
 
 void StgPerspectiveCamera::SetProjection( float pixels_width, float 
pixels_height, float y_min, float y_max ) const
 {
-       SetProjection( pixels_width/pixels_height );
+//     SetProjection( pixels_width/pixels_height );
+       
+       glMatrixMode (GL_PROJECTION);
+       glLoadIdentity ();
+       
+       float near_clip = _z_near;
+       float far_clip = _z_far;
+       float aspect = 1.0;
+       float top = tan( dtor( _fov ) /2.0 ) * near_clip;
+       float bottom = -top;
+       float left = - aspect * top;
+       float right = -left;
+       
+       top = 0.001;
+       bottom = -top;
+
+       glFrustum( left, right, bottom, top, near_clip, far_clip );
+       
+       glMatrixMode (GL_MODELVIEW);
+       
 }
 
 void StgPerspectiveCamera::SetProjection( float aspect ) const
@@ -42,6 +75,7 @@
        glLoadIdentity ();
        
        gluPerspective( _fov, aspect, _z_near, _z_far );
+//     glFrustum( -10, 10, -10, 10, _z_near, _z_far );
        
        glMatrixMode (GL_MODELVIEW);
 }

Modified: code/stage/trunk/libstage/canvas.cc
===================================================================
--- code/stage/trunk/libstage/canvas.cc 2008-06-16 16:15:18 UTC (rev 6584)
+++ code/stage/trunk/libstage/canvas.cc 2008-06-16 17:48:06 UTC (rev 6585)
@@ -574,7 +574,7 @@
                gl_font( FL_HELVETICA, 12 );
 
                if( use_perspective_camera == true ) {
-                       perspective_camera.SetProjection( w(), h(), 0.01, 0.5 );
+                       perspective_camera.SetProjection( w() / h() );
                } else {
                        stg_bounds3d_t extent = world->GetExtent();
                        camera.SetProjection( w(), h(), extent.y.min, 
extent.y.max );

Modified: code/stage/trunk/libstage/model_camera.cc
===================================================================
--- code/stage/trunk/libstage/model_camera.cc   2008-06-16 16:15:18 UTC (rev 
6584)
+++ code/stage/trunk/libstage/model_camera.cc   2008-06-16 17:48:06 UTC (rev 
6585)
@@ -19,7 +19,7 @@
                stg_id_t id,
                char* typestr )
 : StgModel( world, parent, id, typestr ),
-_frame_data( NULL ), _frame_data_width( 0 ), _frame_data_height( 0 )
+_frame_data( NULL ), _frame_data_width( 0 ), _frame_data_height( 0 ), _width( 
0 )
 {
        PRINT_DEBUG2( "Constructing StgModelCamera %d (%s)\n", 
                        id, typestr );
@@ -55,55 +55,60 @@
 {
        StgModel::Load();
        Worldfile* wf = world->GetWorldFile();
-       int fov = wf->ReadLength( id, "fov",    -1 );
-       if( fov > 0 ) {
-               _camera.setFov( fov );
-       }
+
+       _camera.setFov( wf->ReadLength( id, "fov", _camera.fov() ) );
+       _camera.setYaw( wf->ReadLength( id, "yaw", _camera.yaw() ) );
+       _camera.setPitch( wf->ReadLength( id, "pitch", _camera.pitch() ) );
+
+       _width = wf->ReadLength( id, "width", _width );
+
+       //TODO move to constructor
+       _frame_data_width = _width;
+       _frame_data_height = 100;
        
 }
 
 
 void StgModelCamera::Update( void )
 {   
+       GetFrame( _frame_data_width, _frame_data_height, true );
+       
        StgModel::Update();
 }
 
 float* StgModelCamera::laser()
 {
-       //TODO allow the h and w to be passed by user
-       int h = 320;
-       int w = 320;
-       
-       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.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 ] );
-       }
-       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;
+       return NULL;
+//     //TODO allow the h and w to be passed by user
+//     int w = _width;
+//     int h = 1;
+//     
+//     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( w, h, 0.1, 40.0 );
+//     
+//     _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, 0, w, h, 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 
)
@@ -113,7 +118,7 @@
        if( width == 0 || height == 0 )
                return NULL;
        
-       if( width != _frame_data_width || height != _frame_data_height ) {
+       if( width != _frame_data_width || height != _frame_data_height || 
_frame_data == NULL ) {
                if( _frame_data != NULL )
                        delete _frame_data;
                _frame_data = new char[ 4 * width * height ]; //assumes a max 
of depth 4
@@ -134,6 +139,11 @@
                                         GL_DEPTH_COMPONENT, //GL_RGB,
                                         GL_FLOAT, //GL_UNSIGNED_BYTE,
                                         _frame_data );
+               //transform length into linear length
+               float* data = ( float* )( _frame_data ); //TODO use static_cast 
here
+               int buf_size = width * height;
+               for( int i = 0; i < buf_size; i++ )
+                       data[ i ] = _camera.realDistance( data[ i ] ) + 0.1;
        } else {
                glReadPixels(0, 0, width, height,
                                         GL_RGB,
@@ -146,7 +156,50 @@
        return _frame_data;
 }
 
+//TODO create lines outlineing camera frustrum, then iterate over each depth 
measurement and create a square
+void StgModelCamera::DataVisualize( void )
+{
+       float w_fov = _camera.fov();
+       float h_fov = _camera.fov();
+       float length = 8.0;
+       
+       float start_fov = 90 + w_fov / 2.0;
+       float end_fov = 90 - w_fov / 2.0;
+       
+       float x, y;
+               
+       glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); //TODO this doesn't seem 
to work.
 
+       float z = cos( dtor( h_fov / 2.0 ) ) * length;
+       
+       glColor4f(1.0, 0.0, 0.0, 1.0 ); 
+       
+       const float* data = ( float* )( _frame_data ); //TODO use static_cast 
here
+       int w = _frame_data_width, h = _frame_data_height;
+       for( int i = 0; i < w; i++ ) {
+               float z_a = h_fov / _width * static_cast< float >( i );
+               for( int j = 0; j < h; j++ ) {
+                       float length = data[ i + j * w ];
+                       float a;
+                       float z = cos( z_a ) * length;
+                       //TODO rename j to not conflict with loop
+                       int j = i + 1;
+                       if( j < _width/2 ) 
+                               a = 90 - w_fov / _width * static_cast< float >( 
_width/2 - 1 - j );
+                       else
+                               a = 90 + w_fov / _width * static_cast< float >( 
j - _width/2 );
+                       x = sin( dtor( a ) ) * length;
+                       y = cos( dtor( a ) ) * length;
+                       glBegin( GL_LINE_LOOP );
+                       glVertex3f( 0.0, 0.0, 0.0 );
+                       glVertex3f( x, y, 0.0 );
+                       glEnd();
+                       
+               }
+       }
+       
+}
+
 void StgModelCamera::Draw( uint32_t flags, StgCanvas* canvas )
 {
        StgModel::Draw( flags, canvas );

Modified: code/stage/trunk/libstage/stage.hh
===================================================================
--- code/stage/trunk/libstage/stage.hh  2008-06-16 16:15:18 UTC (rev 6584)
+++ code/stage/trunk/libstage/stage.hh  2008-06-16 17:48:06 UTC (rev 6585)
@@ -1769,22 +1769,12 @@
 
                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;
-               }
+               void move( float x, float y, float z );
                inline void setFov( float fov ) { _fov = fov; }
                inline void setYaw( float yaw ) { _yaw = yaw; }
+               inline float yaw( void ) const { return _yaw; }
+               inline float pitch( void ) const { return _pitch; }
+               inline float fov( void ) const { return _fov; }
                inline void addYaw( float yaw ) { _yaw += yaw; }
                inline void setPitch( float pitch ) { _pitch = pitch; }
                inline void addPitch( float pitch ) { _pitch += pitch; }
@@ -2428,6 +2418,8 @@
                int _frame_data_width;
                int _frame_data_height;
        
+               int _width; //TODO merge into frame_data_width
+       
                StgPerspectiveCamera _camera;
        
        public:
@@ -2442,7 +2434,10 @@
                virtual void Load();
                virtual void Update();
                virtual void Draw( uint32_t flags, StgCanvas* canvas );
+               virtual void DataVisualize();
        
+               inline int getWidth( void ) const { return _width; }
+       
                ///Take a screenshot from the camera's perspective
                const char* GetFrame( int width, int height, bool depth_buffer 
);
        


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