Deep copies of SIO2object

View previous topic View next topic Go down

Deep copies of SIO2object

Post  meteors on Tue Dec 16, 2008 3:29 pm

Hi all,

If anyone has time to kill (yeah, right), one thing which would be really helpful is a function which creates a deep copy of an SIO2object. That is, it should be a soveriegn object, with it's own vertexgroups, and not referencing the same geometry as another object, like sio2ObjectDuplicate does.

Any thoughts?


Best,
-joshua
avatar
meteors

Posts : 241
Join date : 2008-11-08
Location : Sunny Florida

View user profile

Back to top Go down

Re: Deep copies of SIO2object

Post  matt on Thu Dec 18, 2008 7:11 pm

I did something like that for ELFrun.

First, here are functions to map/unmap vertex groups like they allready exist for objects:

Code:
unsigned char sio2VertexGroupMapBuffer( SIO2vertexgroup* _SIO2vertexgroup )
{
   GLvoid *buf = NULL;
   
   glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, _SIO2vertexgroup->vbo );
   
   glMapBufferOES( GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY_OES );
   
   glGetBufferPointervOES( GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_MAP_POINTER_OES, &buf );
   
   _SIO2vertexgroup->ind = ( unsigned short * ) buf;
   
   return _SIO2vertexgroup->ind ? 1 : 0;
}

void sio2VertexGroupUnmapBuffer( SIO2vertexgroup *_SIO2vertexgroup )
{
   _SIO2vertexgroup->ind = NULL;
   
   glUnmapBufferOES( GL_ELEMENT_ARRAY_BUFFER );
   
   glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
}

Second, here's some code which was meant to mirror objects on the YZ plane. It didn't really work and when I recognized I was wasting too much time on it, I simply mirrored the objects in Blender. It should give you an idea how to create deep copies, though.

Code:
SIO2object* SIO2Tools::mirrorYZ( SIO2object* obj, v3f& min, v3f& max ) {
   getExtents( obj, min, max );
   
   SIO2object* clone = sio2ObjectInit( obj->name );
   
   memcpy( clone->pos, obj->pos, 12 );
   memcpy( clone->rot, obj->rot, 12 );
   memcpy( clone->scl, obj->scl, 12 );
   memcpy( clone->dim, obj->dim, 12 );
   
   clone->rad = obj->rad;
   clone->flags = obj->flags;
   clone->bounds = obj->bounds;
   clone->mass = obj->mass;
   clone->damp = obj->damp;
   clone->rotdamp = obj->rotdamp;

   sio2ObjectMapBuffer( obj );

   memcpy( &clone->vbo_offset[ 0 ], &obj->vbo_offset[ 0 ], SIO2_OBJECT_NVBO_OFFSET * sizeof( unsigned int ) );
   clone->buf = ( unsigned char * ) malloc( obj->vbo_offset[ SIO2_OBJECT_SIZE ] );
   memcpy( &clone->buf[ 0 ], &obj->buf[ 0 ], obj->vbo_offset[ SIO2_OBJECT_SIZE ] );

   clone->_SIO2vertexgroup = ( SIO2vertexgroup ** ) malloc( obj->n_vgroup * sizeof( SIO2vertexgroup ) );
   clone->n_vgroup = obj->n_vgroup;

   for( int i = 0; i < obj->n_vgroup; ++i ) {
      unsigned int n_ind = obj->_SIO2vertexgroup[ i ]->n_ind;      
      
      clone->_SIO2vertexgroup[ i ] = sio2VertexGroupInit( obj->_SIO2vertexgroup[ i ]->name );
      clone->_SIO2vertexgroup[ i ]->n_ind = obj->_SIO2vertexgroup[ i ]->n_ind;
      clone->_SIO2vertexgroup[ i ]->ind = ( unsigned short * ) malloc( clone->_SIO2vertexgroup[ i ]->n_ind << 1 );
      sio2StringCpy( clone->_SIO2vertexgroup[ i ]->mname, obj->_SIO2vertexgroup[ i ]->mname );
      clone->_SIO2vertexgroup[ i ]->_SIO2material = cloneMaterial( obj->_SIO2vertexgroup[ i ]->_SIO2material );

      sio2VertexGroupMapBuffer( obj->_SIO2vertexgroup[ i ] );
      //
      for( unsigned int j = 0; j < n_ind; ++j ) {
         unsigned short idx = obj->_SIO2vertexgroup[ i ]->ind[ j ];
         clone->_SIO2vertexgroup[ i ]->ind[ j ] = idx;
         
         float source[ 3 ];
         memcpy( &source[ 0 ], &obj->buf[ idx * 12 ], 12 );
         
         float dest[ 3 ];
         dest[ 0 ] = min.x + max.x - source[ 0 ];
         dest[ 1 ] = source[ 1 ];
         dest[ 2 ] = source[ 2 ];
         memcpy( &clone->buf[ idx * 12 ], &dest[ 0 ], 12 );
      }
      // Switch face orientation
      for( unsigned int j = 0; j < n_ind; ++j ) {
         if( j % 3 == 0 ) {
            unsigned short tmp = clone->_SIO2vertexgroup[ i ]->ind[ j + 2 ];
            clone->_SIO2vertexgroup[ i ]->ind[ j + 2 ] = clone->_SIO2vertexgroup[ i ]->ind[ j ];
            clone->_SIO2vertexgroup[ i ]->ind[ j ] = tmp;
         }
      }
      sio2VertexGroupUnmapBuffer( obj->_SIO2vertexgroup[ i ] );
   }
   
   // Care for normals...
   if( obj->vbo_offset[ SIO2_OBJECT_NORMALS ] ) {
      unsigned int nv = 3 * sio2ObjectGetNVert( obj );
      float* ptr = (float*)( obj->buf + obj->vbo_offset[ SIO2_OBJECT_NORMALS ] );
      for( unsigned int i = 0; i < nv; i += 3 ) {
         v3f v( ptr[ 0 ], ptr[ 1 ], ptr[ 2 ] );
         v *= -1;
         ptr[ 0 ] = v.x;
         ptr[ 1 ] = v.y;
         ptr[ 2 ] = v.z;
         ptr += 3;
         
      }
   }

   sio2ObjectUnmapBuffer( obj );

   sio2ResourceAdd( sio2->_SIO2resource, SIO2_OBJECT, clone );
   sio2ObjectGenId( clone );
   for( int i = 0; i < clone->n_vgroup; ++i ) {
      sio2VertexGroupGenId( clone->_SIO2vertexgroup[ i ] );
   }
   
   return clone;
}

Best,
Matt

matt

Posts : 155
Join date : 2008-09-30

View user profile http://elfrun.net

Back to top Go down

Re: Deep copies of SIO2object

Post  iphoniac on Thu Jan 08, 2009 3:26 am

Did you completed the deep copies method. If you didnīt, Iīll probably do it...

iphoniac

Posts : 62
Join date : 2008-12-11

View user profile http://thecaveaniphonegame.blogspot.com/

Back to top Go down

Re: Deep copies of SIO2object

Post  meteors on Thu Jan 08, 2009 7:24 am

sio2interactive has addressed this problem in the next version of SIO2, which I understand is less than a month away.

Personally, I have have learned to work around SIO2object. Pretty much all I use it for is the actually rendering, i.e. just calling sio2ObjectRender. For transformations, I am using raw OpenGL commands and some older matrix code of mine.


best,
-joshua
avatar
meteors

Posts : 241
Join date : 2008-11-08
Location : Sunny Florida

View user profile

Back to top Go down

Re: Deep copies of SIO2object

Post  iphoniac on Thu Jan 08, 2009 7:51 am

So, you just donīt duplicate objects, you use the same SIO2Object and change its properties.

iphoniac

Posts : 62
Join date : 2008-12-11

View user profile http://thecaveaniphonegame.blogspot.com/

Back to top Go down

Re: Deep copies of SIO2object

Post  meteors on Thu Jan 08, 2009 10:18 am

Basically, yeah.

I think the vertex sharing among duplicate SIO2objects confuses people more than anything else. Fortunately, the next release should fix that!


-j
avatar
meteors

Posts : 241
Join date : 2008-11-08
Location : Sunny Florida

View user profile

Back to top Go down

Re: Deep copies of SIO2object

Post  Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top

- Similar topics

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