sio2CameraSetPerspective

View previous topic View next topic Go down

sio2CameraSetPerspective

Post  Faikus on Tue May 26, 2009 4:27 am

Hi,

I have started to use SIO2 for my app and like it so far. I have one problem with sio2CameraPerspective. It seems this function not only sets the perspective projection, but also moves the camera. This messes with some of my computations. To see this effect, replace the function templateRender from tutorial02 with this code:

Code:


void gluPerspective(float fov, float aspect, float zNear, float zFar)
{
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   
   GLfloat size = zNear * tanf(M_PI * fov / 360.0);
   glFrustumf(-size, size, -size / aspect, size /aspect, zNear, zFar);
   
}

void drawPointAt(SIO2camera *_SIO2camera, GLfloat x, GLfloat y, GLfloat z)
{   
   GLfloat vector[] = { _SIO2camera->_SIO2transform->loc->x + x,
            _SIO2camera->_SIO2transform->loc->y + y,
            _SIO2camera->_SIO2transform->loc->z + z};
   
   glVertexPointer(3, GL_FLOAT, 0, vector);
   glDrawArrays(GL_POINTS, 0, 1);
}

void templateRender( void )
{   
   SIO2camera *_SIO2camera = ( SIO2camera * )sio2ResourceGet( sio2->_SIO2resource,
                                   SIO2_CAMERA,
                                   "camera/Camera" );
   
   if( _SIO2camera )
   {
      glClearColor(0.7, 0.7, 0.7, 1.0);
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      
      _SIO2camera->_SIO2transform->loc->x = 0.0f;
      _SIO2camera->_SIO2transform->loc->y = 0.0f;
      _SIO2camera->_SIO2transform->loc->z = 0.0f;
      _SIO2camera->fov = 90.0f;
      
      sio2CameraSetPerspective( _SIO2camera, sio2->_SIO2window );

//      comment the line above and uncomment the three below to try gluPerspective
//      gluPerspective(_SIO2camera->fov,
//         sio2->_SIO2window->scl->x / sio2->_SIO2window->scl->y,
//         _SIO2camera->cstart, _SIO2camera->cend);

      
      
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();

      
      sio2CameraRender( _SIO2camera );

      glPointSize(10);
      glEnableClientState(GL_TEXTURE_COORD_ARRAY);

      glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
      for (int i=1;  i < 100;  i++)
         drawPointAt(_SIO2camera, -0.1f*i, 0.1f*i, 0.0f);
   
      glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
      drawPointAt(_SIO2camera, 0.0f, 0.0f, 0.0f);
   
      glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
      for (int i=1;  i < 100;  i++)
         drawPointAt(_SIO2camera, 0.1f*i, -0.1f*i, 0.0f);
   }
}

The code draws green points going away forwards from the camera, a red point at the camera position, and blue points going away backwards from the camera. Note that all points are along a line going through the camera position, so you should be able to see one point only. With the code above you actually see a line of points, green, red and blue. So points that should be behind the camera are visible as well. If you replace the call to sio2CameraSetPerspective with a call to gluPerspective by commenting/uncommenting, you actually see only one point. Why does sio2CameraSetPerspective move the camera?

Regards,

Faikus

Posts : 23
Join date : 2009-05-26
Location : Berlin

View user profile

Back to top Go down

Re: sio2CameraSetPerspective

Post  sio2interactive on Tue May 26, 2009 4:49 am

Yeah I've seen this one I personally prefer to directly affect the projection like this:

Code:


void sio2CameraSetPerspective( SIO2camera *_SIO2camera,
                       SIO2window *_SIO2window )
{
   glMatrixMode( GL_PROJECTION );
   glLoadIdentity();
   {
      float m[ 16 ],
           s,
           c,
           d = _SIO2camera->cend - _SIO2camera->cstart,
           r = _SIO2camera->fov * 0.5f * SIO2_DEG_TO_RAD;

      s = sinf( r );

      c = cosf( r ) / s;

      memset( &m[ 0 ], 0, 64 );

      m[ 0  ] = c / ( _SIO2window->scl->x / _SIO2window->scl->y );
      m[ 5  ] = c;
      m[ 10 ] = -( _SIO2camera->cend + _SIO2camera->cstart ) / d;
      m[ 11 ] = -1.0f;
      m[ 14 ] = -2.0f * _SIO2camera->cend * _SIO2camera->cstart / d;
      m[ 15 ] = 0.0f;
      
      glMultMatrixf( &m[ 0 ] );
   }
   glMatrixMode( GL_MODELVIEW );
   
   
   #ifdef SIO2_DEBUG_GL

      sio2ErrorGL( __FILE__, __FUNCTION__, __LINE__ );
   #endif   
}


This have already been fixed in 1.3.6... use the code above... this will give you the same as the Blender viewport... Just a question why you mention gluPerspective it is not part of the iPhone SDK...

_________________
SIO2 Interactive
Free Open Source 3D Game Engine for iPhone and iPod Touch
http://sio2interactive.com
avatar
sio2interactive

Posts : 1526
Join date : 2008-08-26
Age : 38
Location : Shanghai

View user profile http://sio2interactive.com

Back to top Go down

works

Post  Faikus on Tue May 26, 2009 5:06 am

Thanks, this version works. The only difference between this version of sio2CameraSetPerspective and the one from the source tree is in the last matrix element, m[15]. The source I checked out set it 1, your new version to 0. Since that last coordinate is the additional homogeneous coordinate w, the resulting matrix caused a translation.

Faikus

Posts : 23
Join date : 2009-05-26
Location : Berlin

View user profile

Back to top Go down

Re: sio2CameraSetPerspective

Post  sio2interactive on Tue May 26, 2009 5:09 am

yes and no... the w coordinate will push the far plane to be infinite... and will cause the perspective to be slightly distorted.

_________________
SIO2 Interactive
Free Open Source 3D Game Engine for iPhone and iPod Touch
http://sio2interactive.com
avatar
sio2interactive

Posts : 1526
Join date : 2008-08-26
Age : 38
Location : Shanghai

View user profile http://sio2interactive.com

Back to top Go down

Re: sio2CameraSetPerspective

Post  Faikus on Tue May 26, 2009 5:37 am

Aha. It looked to me like a simple translation, because changing the value from 1 to 10 moves the camera further back. Anyway, I'm glad i have it working now. Thanks again!

Faikus

Posts : 23
Join date : 2009-05-26
Location : Berlin

View user profile

Back to top Go down

Re: sio2CameraSetPerspective

Post  Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum