Code Response(s) on features & Request(s)
5 posters
Code Response(s) on features & Request(s)
Concerning the duplicate object request:
sio2_object.cc
sio2_object.h
And a quick example on how to use it inside the templateRender function (or your own rendering loop):
sio2_object.cc
- Code:
#include "sio2.h"
SIO2object *sio2ObjectInit( char *_name )
{
SIO2object *_SIO2object = ( SIO2object * ) calloc( 1, sizeof( SIO2object ) );
strcpy( _SIO2object->name, _name );
_SIO2object->pos = sio2Vec3Init();
_SIO2object->rot = sio2Vec3Init();
_SIO2object->scl = sio2Vec3Init();
_SIO2object->dim = sio2Vec3Init();
_SIO2object->col = sio2Col4Init();
_SIO2object->dst = 1.0f;
_SIO2object->mat = ( float * ) malloc( 64 );
sio2ResourceAdd( sio2->_SIO2resource,
SIO2_OBJECT,
(void *)_SIO2object );
return _SIO2object;
}
SIO2object *sio2ObjectFree( SIO2object *_SIO2object )
{
unsigned int i = 0;
_SIO2object->pos = sio2Vec3Free( _SIO2object->pos );
_SIO2object->rot = sio2Vec3Free( _SIO2object->rot );
_SIO2object->scl = sio2Vec3Free( _SIO2object->scl );
_SIO2object->dim = sio2Vec3Free( _SIO2object->dim );
_SIO2object->col = sio2Col4Free( _SIO2object->col );
free( _SIO2object->mat );
_SIO2object->mat = NULL;
glDeleteBuffers( 1, &_SIO2object->vbo );
while( i != _SIO2object->n_vertexgroup )
{
sio2VertexGroupFree( _SIO2object->_SIO2vertexgroup[i] );
_SIO2object->_SIO2vertexgroup[i] = NULL;
++i;
}
if( _SIO2object->_SIO2vertexgroup )
{
free( _SIO2object->_SIO2vertexgroup );
_SIO2object->_SIO2vertexgroup = NULL;
}
sio2ResourceDel( sio2->_SIO2resource,
SIO2_OBJECT,
(void *)_SIO2object );
free( _SIO2object );
return NULL;
}
void sio2ObjectLoad( SIO2object *_SIO2object,
SIO2stream *_SIO2stream )
{
sio2StreamRead( _SIO2stream, _SIO2object->pos, 12 );
sio2StreamRead( _SIO2stream, _SIO2object->rot, 12 );
sio2StreamRead( _SIO2stream, _SIO2object->scl, 12 );
sio2ObjectGetMatrix( _SIO2object );
sio2StreamRead( _SIO2stream, &_SIO2object->rad, 4 );
sio2StreamRead( _SIO2stream, &_SIO2object->flags, 4 );
sio2StreamRead( _SIO2stream, &_SIO2object->bounds, 1 );
sio2StreamRead( _SIO2stream, &_SIO2object->mass, 4 );
sio2StreamRead( _SIO2stream, &_SIO2object->damp, 4 );
sio2StreamRead( _SIO2stream, &_SIO2object->rotdamp, 4 );
sio2StreamRead( _SIO2stream, _SIO2object->dim, 12 );
sio2StreamRead( _SIO2stream, &_SIO2object->iname[ 0 ], SIO2_MAX_CHAR );
if( _SIO2object->iname[ 0 ] )
{ return; }
sio2StreamRead( _SIO2stream, &_SIO2object->vbo_offset[ 0 ], 20 );
if( _SIO2object->vbo_offset[ SIO2_OBJECT_SIZE ] )
{
_SIO2object->buf = ( unsigned char * ) malloc( _SIO2object->vbo_offset[ SIO2_OBJECT_SIZE ] );
sio2StreamRead( _SIO2stream, &_SIO2object->buf[ 0 ], _SIO2object->vbo_offset[ SIO2_OBJECT_SIZE ] );
}
sio2StreamRead( _SIO2stream,
&_SIO2object->n_vertexgroup, 1 );
if( _SIO2object->n_vertexgroup )
{
unsigned int i = 0;
_SIO2object->_SIO2vertexgroup = ( SIO2vertexgroup ** ) malloc( _SIO2object->n_vertexgroup * sizeof( SIO2vertexgroup ) );
while( i != _SIO2object->n_vertexgroup )
{
char name[ SIO2_MAX_CHAR ] = {""};
sio2StreamRead( _SIO2stream,
&name[ 0 ], SIO2_MAX_CHAR );
_SIO2object->_SIO2vertexgroup[ i ] = sio2VertexGroupInit( name );
sio2VertexGroupLoad( _SIO2object->_SIO2vertexgroup[ i ],
_SIO2stream );
++i;
}
}
}
void sio2ObjectGenId( SIO2object *_SIO2object )
{
if( sio2IsEnabled( _SIO2object->flags, SIO2_OBJECT_GHOST ) )
{
free( _SIO2object->buf );
_SIO2object->buf = NULL;
return;
}
if( _SIO2object->vbo_offset[ SIO2_OBJECT_SIZE ] )
{
glGenBuffers( 1, &_SIO2object->vbo );
glBindBuffer( GL_ARRAY_BUFFER, _SIO2object->vbo );
glBufferData( GL_ARRAY_BUFFER,
_SIO2object->vbo_offset[ SIO2_OBJECT_SIZE ],
&_SIO2object->buf[ 0 ],
GL_STATIC_DRAW );
free( _SIO2object->buf );
_SIO2object->buf = NULL;
}
#ifdef SIO2_DEBUG_GL
sio2ErrorGL( __FILE__, __FUNCTION__, __LINE__ );
#endif
}
void sio2ObjectGetMatrix( SIO2object *_SIO2object )
{
glPushMatrix();
{
glLoadIdentity();
glTranslatef( _SIO2object->pos->x,
_SIO2object->pos->y,
_SIO2object->pos->z );
glRotatef( _SIO2object->rot->z,
0.0f,
0.0f,
1.0f );
glRotatef( _SIO2object->rot->y,
0.0f,
1.0f,
0.0f );
glRotatef( _SIO2object->rot->x,
1.0f,
0.0f,
0.0f );
glScalef( _SIO2object->scl->x,
_SIO2object->scl->y,
_SIO2object->scl->z );
glGetFloatv( GL_MODELVIEW_MATRIX, &_SIO2object->mat[0] );
}
glPopMatrix();
#ifdef SIO2_DEBUG_GL
sio2ErrorGL( __FILE__, __FUNCTION__, __LINE__ );
#endif
}
unsigned char sio2ObjectRender( SIO2object *_SIO2object,
SIO2camera *_SIO2camera,
unsigned char _usematerial )
{
if( sio2IsEnabled( _SIO2object->flags, SIO2_OBJECT_GHOST ) )
{ return 0; }
else if( sio2IsEnabled( _SIO2object->flags, SIO2_OBJECT_DYNAMIC ) )
{
static float scl[ 3 ] = { 1.0f, 1.0f, 1.0f };
_SIO2object->_btRigidBody->getWorldTransform().getOpenGLMatrix( _SIO2object->mat );
if( memcmp( _SIO2object->scl, &scl, 12 ) )
{
_SIO2object->mat[ 0 ] *= _SIO2object->scl->x;
_SIO2object->mat[ 1 ] *= _SIO2object->scl->x;
_SIO2object->mat[ 2 ] *= _SIO2object->scl->x;
_SIO2object->mat[ 4 ] *= _SIO2object->scl->y;
_SIO2object->mat[ 5 ] *= _SIO2object->scl->y;
_SIO2object->mat[ 6 ] *= _SIO2object->scl->y;
_SIO2object->mat[ 8 ] *= _SIO2object->scl->z;
_SIO2object->mat[ 9 ] *= _SIO2object->scl->z;
_SIO2object->mat[ 10 ] *= _SIO2object->scl->z;
}
memcpy( _SIO2object->pos, &_SIO2object->mat[ 12 ], 12 );
}
if( !_SIO2object->dst )
{ return 0; }
glPushMatrix();
{
unsigned int i = 0;
SIO2object *tmp = _SIO2object->_SIO2object ? ( SIO2object * )_SIO2object->_SIO2object : _SIO2object;
glMultMatrixf( &_SIO2object->mat[ 0 ] );
if( sio2IsEnabled( _SIO2object->flags, SIO2_OBJECT_BILLBOARD | SIO2_OBJECT_HALO ) )
{
sio2ObjectBillboard( _SIO2object,
_SIO2camera->pos );
}
if( sio2->last_obj != tmp )
{
sio2->last_obj = tmp;
sio2ObjectBindVBO( tmp,
_usematerial );
}
while( i != tmp->n_vertexgroup )
{
if( !sio2IsEnabled( tmp->flags, SIO2_OBJECT_TWOSIDE ) )
{
sio2VertexGroupRender( tmp->_SIO2vertexgroup[ i ],
_usematerial );
}
else
{
glCullFace( GL_FRONT );
sio2VertexGroupRender( tmp->_SIO2vertexgroup[ i ],
_usematerial );
glCullFace( GL_BACK );
sio2VertexGroupRender( tmp->_SIO2vertexgroup[ i ],
_usematerial );
}
// TODO: FIX ME, instance with sound and physic,
// object is moving but sound source doesn't update.
if( tmp->_SIO2vertexgroup[ i ]->_SIO2sound )
{
sio2SoundSetFx( _SIO2object->_SIO2vertexgroup[ i ]->_SIO2sound,
_SIO2object->pos,
_SIO2object->rad );
}
++i;
}
}
glPopMatrix();
#ifdef SIO2_DEBUG_GL
sio2ErrorGL( __FILE__, __FUNCTION__, __LINE__ );
#endif
return 1;
}
void sio2ObjectBindVBO( SIO2object *_SIO2object,
unsigned char _usematerial )
{
glBindBuffer( GL_ARRAY_BUFFER,
_SIO2object->vbo );
glVertexPointer( 3,
GL_FIXED,
0,
(void *)NULL );
if( _usematerial )
{
if( _SIO2object->vbo_offset[ SIO2_OBJECT_VCOLOR ] )
{
if( !glIsEnabled( GL_COLOR_ARRAY ) )
{ glEnableClientState( GL_COLOR_ARRAY ); }
glColorPointer( 4,
GL_UNSIGNED_BYTE,
0,
(void *)SIO2_BUFFER_OFFSET( _SIO2object->vbo_offset[ SIO2_OBJECT_VCOLOR ] ) );
}
else
{
if( glIsEnabled( GL_COLOR_ARRAY ) )
{ glDisableClientState( GL_COLOR_ARRAY ); }
}
if( glIsEnabled( GL_LIGHTING ) )
{
if( _SIO2object->vbo_offset[ SIO2_OBJECT_NORMALS ] )
{
if( !glIsEnabled( GL_NORMAL_ARRAY ) )
{ glEnableClientState( GL_NORMAL_ARRAY ); }
glNormalPointer( GL_FIXED,
0,
SIO2_BUFFER_OFFSET( _SIO2object->vbo_offset[ SIO2_OBJECT_NORMALS ] ) );
}
else
{
if( glIsEnabled( GL_NORMAL_ARRAY ) )
{ glDisableClientState( GL_NORMAL_ARRAY ); }
}
}
glClientActiveTexture( GL_TEXTURE0 );
if( _SIO2object->vbo_offset[ SIO2_OBJECT_TEXUV0 ] )
{
if( !glIsEnabled( GL_TEXTURE_COORD_ARRAY ) )
{ glEnableClientState( GL_TEXTURE_COORD_ARRAY ); }
glTexCoordPointer( 2,
GL_FIXED,
0,
SIO2_BUFFER_OFFSET( _SIO2object->vbo_offset[ SIO2_OBJECT_TEXUV0 ] ) );
}
else
{
if( glIsEnabled( GL_TEXTURE_COORD_ARRAY ) )
{ glDisableClientState( GL_TEXTURE_COORD_ARRAY ); }
}
glClientActiveTexture( GL_TEXTURE1 );
if( _SIO2object->vbo_offset[ SIO2_OBJECT_TEXUV1 ] )
{
if( !glIsEnabled( GL_TEXTURE_COORD_ARRAY ) )
{ glEnableClientState( GL_TEXTURE_COORD_ARRAY ); }
glTexCoordPointer( 2,
GL_FIXED,
0,
SIO2_BUFFER_OFFSET( _SIO2object->vbo_offset[ SIO2_OBJECT_TEXUV1 ] ) );
}
else
{
if( glIsEnabled( GL_TEXTURE_COORD_ARRAY ) )
{ glDisableClientState( GL_TEXTURE_COORD_ARRAY ); }
}
}
}
void sio2ObjectBillboard( SIO2object *_SIO2object,
vec3 *_vec )
{
float c_angle,
s_angle;
vec3 dir,
objdir,
rot;
dir.x = 0.0f;
dir.y = 1.0f;
dir.z = 0.0f;
objdir.x = 0.0f;
objdir.y = 0.0f;
objdir.z = 0.0f;
objdir.x = _vec->x - _SIO2object->pos->x;
objdir.y = _vec->y - _SIO2object->pos->y;
c_angle = sio2DotProduct( &dir, &objdir );
sio2CrossProduct( &dir,
&objdir,
&rot );
glRotatef( c_angle, rot.x, rot.y, rot.z );
if( sio2IsEnabled( _SIO2object->flags, SIO2_OBJECT_HALO ) )
{
vec3 objdir_proj;
objdir_proj.x = objdir.x;
objdir_proj.y = objdir.y;
objdir_proj.z = _vec->z - _SIO2object->pos->z;
s_angle = sio2DotProduct( &objdir_proj,
&objdir );
if( objdir.z < 0.0f )
{ glRotatef( s_angle, -1.0f, 0.0f, 0.0f ); }
else
{ glRotatef( s_angle, 1.0f, 0.0f, 0.0f ); }
}
}
void sio2ObjectReset( void )
{
sio2->last_obj = NULL;
sio2->last_vgp = NULL;
glBindBuffer( GL_ARRAY_BUFFER, 0 );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
if( glIsEnabled( GL_COLOR_ARRAY ) )
{ glDisableClientState( GL_COLOR_ARRAY ); }
if( glIsEnabled( GL_NORMAL_ARRAY ) )
{ glDisableClientState( GL_NORMAL_ARRAY ); }
#ifdef SIO2_DEBUG_GL
sio2ErrorGL( __FILE__, __FUNCTION__, __LINE__ );
#endif
sio2MaterialReset();
}
unsigned int sio2ObjectGetNVert( SIO2object *_SIO2object )
{
SIO2object *tmp = _SIO2object->_SIO2object ? ( SIO2object * )_SIO2object->_SIO2object : _SIO2object;
if( tmp->vbo_offset[ SIO2_OBJECT_VCOLOR ] )
{ return tmp->vbo_offset[ SIO2_OBJECT_VCOLOR ] / 36; }
else if( tmp->vbo_offset[ SIO2_OBJECT_NORMALS ] )
{ return tmp->vbo_offset[ SIO2_OBJECT_NORMALS ] / 36; }
else if( tmp->vbo_offset[ SIO2_OBJECT_TEXUV0 ] )
{ return tmp->vbo_offset[ SIO2_OBJECT_TEXUV0 ] / 36; }
return 0;
}
unsigned char sio2ObjectMapBuffer( SIO2object *_SIO2object )
{
GLvoid *buf = NULL;
glBindBuffer( GL_ARRAY_BUFFER, _SIO2object->vbo );
glMapBufferOES( GL_ARRAY_BUFFER, GL_WRITE_ONLY_OES );
glGetBufferPointervOES( GL_ARRAY_BUFFER, GL_BUFFER_MAP_POINTER_OES, &buf );
_SIO2object->buf = ( unsigned char * )buf;
return _SIO2object->buf ? 1 : 0;
}
void sio2ObjectUnmapBuffer( SIO2object *_SIO2object )
{
_SIO2object->buf = NULL;
glUnmapBufferOES( GL_ARRAY_BUFFER );
glBindBuffer( GL_ARRAY_BUFFER, 0 );
}
SIO2object *sio2ObjectDuplicate( SIO2object *_SIO2object,
char *_name )
{
SIO2object *_new = sio2ObjectInit( _name );
memcpy( _new->pos, _SIO2object->pos, 12 );
memcpy( _new->rot, _SIO2object->rot, 12 );
memcpy( _new->scl, _SIO2object->scl, 12 );
memcpy( _new->dim, _SIO2object->dim, 12 );
sio2ObjectGetMatrix( _new );
_new->rad = _SIO2object->rad;
_new->flags = _SIO2object->flags;
_new->bounds = _SIO2object->bounds;
_new->mass = _SIO2object->mass;
_new->damp = _SIO2object->damp;
_new->rotdamp = _SIO2object->rotdamp;
strcpy( _new->iname, _SIO2object->name );
_new->_SIO2object = _SIO2object;
return _new;
}
sio2_object.h
- Code:
#ifndef SIO2_OBJECT_H
#define SIO2_OBJECT_H
typedef enum
{
SIO2_OBJECT_ACTOR = ( 1 << 0 ),
SIO2_OBJECT_GHOST = ( 1 << 1 ),
SIO2_OBJECT_DYNAMIC = ( 1 << 2 ),
SIO2_OBJECT_RIGIDBODY = ( 1 << 3 ),
SIO2_OBJECT_SOFTBODY = ( 1 << 4 ),
SIO2_OBJECT_BILLBOARD = ( 1 << 5 ),
SIO2_OBJECT_HALO = ( 1 << 6 ),
SIO2_OBJECT_TWOSIDE = ( 1 << 7 )
} SIO2_PHYSIC_TYPE;
typedef enum
{
SIO2_OBJECT_SIZE = 0,
SIO2_OBJECT_VCOLOR,
SIO2_OBJECT_NORMALS,
SIO2_OBJECT_TEXUV0,
SIO2_OBJECT_TEXUV1,
SIO2_OBJECT_NVBO_OFFSET
} SIO2_OBJECT_VBO_OFFSET;
typedef struct
{
char name[ SIO2_MAX_CHAR ];
vec3 *pos;
vec3 *rot;
vec3 *scl;
col4 *col;
float *mat;
float rad;
float dst;
unsigned int vbo;
unsigned char *buf;
unsigned int vbo_offset[ SIO2_OBJECT_NVBO_OFFSET ];
unsigned int flags;
unsigned char bounds;
float mass;
float damp;
float rotdamp;
vec3 *dim;
char iname[ SIO2_MAX_CHAR ];
void *_SIO2object;
unsigned char n_vertexgroup;
SIO2vertexgroup **_SIO2vertexgroup;
btRigidBody *_btRigidBody;
} SIO2object;
SIO2object *sio2ObjectInit( char *_name );
SIO2object *sio2ObjectFree( SIO2object *_SIO2object );
void sio2ObjectLoad( SIO2object *_SIO2object,
SIO2stream *_SIO2stream );
void sio2ObjectGenId( SIO2object *_SIO2object );
void sio2ObjectGetMatrix( SIO2object *_SIO2object );
unsigned char sio2ObjectRender( SIO2object *_SIO2object,
SIO2camera *_SIO2camera,
unsigned char _usematerial );
void sio2ObjectBindVBO( SIO2object *_SIO2object,
unsigned char _usematerial );
void sio2ObjectReset( void );
void sio2ObjectBillboard( SIO2object *_SIO2object,
vec3 *_vec );
unsigned int sio2ObjectGetNVert( SIO2object *_SIO2object );
unsigned char sio2ObjectMapBuffer( SIO2object *_SIO2object );
void sio2ObjectUnmapBuffer( SIO2object *_SIO2object );
SIO2object *sio2ObjectDuplicate( SIO2object *_SIO2object,
char *_name );
#endif
And a quick example on how to use it inside the templateRender function (or your own rendering loop):
- Code:
// New object
if( new_object )
{
char tmp_name[ SIO2_MAX_CHAR ] = {""};
static unsigned int i = 0;
SIO2object *_SIO2object = ( SIO2object * )sio2ResourceGet( sio2->_SIO2resource,
SIO2_OBJECT,
"object/Sphere" );
new_object = 0;
if( _SIO2object )
{
sprintf( tmp_name, "%s%d", _SIO2object->name, i );
SIO2object *_new = sio2ObjectDuplicate( _SIO2object, tmp_name );
if( sio2ObjectMapBuffer( _SIO2object ) )
{
_new->buf = _SIO2object->buf;
sio2PhysicAddObject( sio2->_SIO2physic,
_new );
_new->buf = NULL;
sio2ObjectUnmapBuffer( _SIO2object );
}
++i;
}
}
Last edited by sio2interactive on Thu Oct 02, 2008 4:04 pm; edited 1 time in total
Re: Code Response(s) on features & Request(s)
Great, thanks alot! I'll try this as soon as possible.
Matt
Matt
Re: Code Response(s) on features & Request(s)
Note that there's a #endif missing in the header you posted. Besides that, it seems to be working great, thank you!
Best,
Matt
Best,
Matt
Re: Code Response(s) on features & Request(s)
awesome, I'll be using this also
sodapunk- Posts : 7
Join date : 2008-09-28
Re: Code Response(s) on features & Request(s)
Hi,
does someone test the above sio2ObjectDuplicate method with 1.3.1.
I used the following code with the tutorial_6_1.
Afterwords I moved the new object to an other position with the following code:
But the new object does not appear in the 3D world.
Does anyone have an idea?
Thanks
Ludi
does someone test the above sio2ObjectDuplicate method with 1.3.1.
I used the following code with the tutorial_6_1.
- Code:
sprintf( tmp_name, "%s%d", selection->name, numCreatedBalls );
_newObject = sio2ObjectDuplicate( selection, tmp_name );
NSLog(@"object duplicated = %s", _newObject->name);
if( sio2ObjectMapBuffer( selection ) )
{
_newObject->buf = selection->buf;
sio2PhysicAddObject( sio2->_SIO2physic, _newObject );
_newObject->buf = NULL;
sio2ObjectUnmapBuffer( selection );
NSLog(@"object added");
}
Afterwords I moved the new object to an other position with the following code:
- Code:
selection = _newObject;
btVector3 newPos;
newPos.setX(3);
newPos.setY(3);
newPos.setZ(3);
btTransform startTransform;
startTransform.setIdentity();
startTransform.setOrigin(newPos);
selection->_btRigidBody->setWorldTransform( startTransform );
selection->_btRigidBody->setActivationState( ACTIVE_TAG );
But the new object does not appear in the 3D world.
Does anyone have an idea?
Thanks
Ludi
ludi- Posts : 14
Join date : 2008-10-19
Re: Code Response(s) on features & Request(s)
Has this been committed to the source tree for future versions?
uprise78- Posts : 228
Join date : 2008-10-31
Re: Code Response(s) on features & Request(s)
uprise78 wrote:Has this been committed to the source tree for future versions?
Maybe you are right, the sio2ObjectDuplicate method looks a little bit different:
- Code:
SIO2object *sio2ObjectDuplicate( SIO2object *_SIO2object,
char *_name )
{
SIO2object *dup = sio2ObjectInit( _name );
memcpy( dup->pos, _SIO2object->pos, 12 );
memcpy( dup->rot, _SIO2object->rot, 12 );
memcpy( dup->scl, _SIO2object->scl, 12 );
memcpy( dup->dim, _SIO2object->dim, 12 );
dup->rad = _SIO2object->rad;
dup->flags = _SIO2object->flags;
dup->bounds = _SIO2object->bounds;
dup->mass = _SIO2object->mass;
dup->damp = _SIO2object->damp;
dup->rotdamp = _SIO2object->rotdamp;
sio2StringCpy( dup->instname, _SIO2object->name );
dup->_SIO2instance = _SIO2object;
return dup;
}
The following code is missing or different and some member variables are renamed:
- Code:
sio2ObjectGetMatrix( _new );
strcpy( _new->iname, _SIO2object->name );
I tried it with the changes but my code still does not work.
Do you have any further idea?
ludi- Posts : 14
Join date : 2008-10-19
Re: Code Response(s) on features & Request(s)
SIO2 v1.3.2 include objects databablocks, so all theses functions will be rewritten with new parameters and as well extend the flexibility...
The new revision should be out in a few weeks...
The new revision should be out in a few weeks...
Similar topics
» Request: code comments
» tutorial request
» Request: function to fade between render callbacks
» REQUEST: Lua to Python
» [request] About iphone 3d racing game tutorial
» tutorial request
» Request: function to fade between render callbacks
» REQUEST: Lua to Python
» [request] About iphone 3d racing game tutorial
Permissions in this forum:
You cannot reply to topics in this forum