Creating an object in run time

View previous topic View next topic Go down

Creating an object in run time

Post  andre.brito on Fri Feb 13, 2009 5:55 am

Hello,

I'm attempting to load an object in run time. My idea was to use a string containing information similar to the information of an object produced by the blender exporter script. I then pass that string to sio2StreamParse, bind the material, the instance and the matrix, and generate the id. A sample of the code I'm using follows:

Code:
   NSString *contents = [NSString stringWithString:@"object( \"object/Plane2\" )\n"
                  "{\n"
                    "pos( 0 0 0 )\n"
                    "rot( 340 10 0 )\n"
                    "scl( 2.5 2.5 2.5 )\n"
                    "rad( 3.536 )\n"
                    "bounds( 4 )\n"
                    "dim( 1 1 0 )\n"
                    "vbo_offset( 48 0 0 0 0 )\n"
                    "vert( 1 1 0 )\n"
                    "vert( -1 1 0 )\n"
                    "vert( -1 -1 0 )\n"
                    "vert( 1 -1 0 )\n"
                    "n_vgroup( 1 )\n"
                    "vgroup( \"null\" )\n"
                    "n_ind( 6 )\n"
                    "ind( 0 1 2 )\n"
                    "ind( 0 2 3 )\n"
                  "}\n"];
   
   SIO2stream *_SIO2stream = sio2StreamInit( "Plane2" );
   
   _SIO2stream->size = [contents lengthOfBytesUsingEncoding:NSASCIIStringEncoding];;
   _SIO2stream->buf = new unsigned char [_SIO2stream->size + 1];
   NSRange copyRange;
   copyRange.location = 0;
   copyRange.length = [contents length];
   [contents getBytes:_SIO2stream->buf maxLength:(_SIO2stream->size + 1) usedLength:NULL encoding:NSASCIIStringEncoding options:NULL range:copyRange remainingRange:NULL];
   _SIO2stream->buf[_SIO2stream->size] = 0;
   _SIO2stream->cur = _SIO2stream->buf;
   
   sio2StreamParse( _SIO2stream, sio2->_SIO2resource->n_entry, sio2->_SIO2resource->_SIO2entry );
   _SIO2stream = sio2StreamClose( _SIO2stream );
   
   SIO2object *plane2 = ( SIO2object * )sio2ResourceGet( sio2->_SIO2resource, SIO2_OBJECT, "object/Plane2" );
   sio2ResourceBindMaterial(sio2->_SIO2resource, plane2);
   sio2ResourceBindInstance(sio2->_SIO2resource, plane2);
   sio2ObjectBindMatrix(plane2);
   sio2ObjectGenId(plane2);
   sio2ResourceResetState();
   sio2->_SIO2camera = ( SIO2camera * )sio2ResourceGet( sio2->_SIO2resource, SIO2_CAMERA, "camera/Camera" );

The original scene loaded from the .sio2 file already contains an object called "Plane" which loads successfully. On the code presented above, I'm trying to load an object called "Plane2", that differs from "Plane" only on the rotation values. Everything else is the same.

The code above runs perfectly fine, however I'm getting an 'EXC_BAD_ACCESS' exception in the templateRender function. I tracked the problem down to the invocation of glDrawElements inside sio2VertexGroupRender. I then compared the contents of _SIO2vertexgroup[0]->ind for the Plane and Plane2 objects and, while I can read the contents for Plane2 and they are the same as what is on the input string (0,1,2,0,2,3), for the Plane object the numbers don't make any sense (64386, ...). I checked that the values are read correctly for the Plane object when the .sio2 file is read, however, at some point, the contents of _SIO2vertexgroup[0]->ind are getting modified. As this is probably happening on another thread, I have no way of determining what exactly is happening.

So, I would like to get some help here, to understand what I'm missing here, if anyone can please. I think I explained the problem well, but if something is not clear, I can try to explain it better.

Also, if anyone has any suggestion of a different approach to create an object in run time, I'd be happy to hear it.

Thanks in advance,
André de Brito
avatar
andre.brito

Posts : 13
Join date : 2009-01-05
Location : Lisbon, Portugal

View user profile http://www.evolve.pt/

Back to top Go down

Re: Creating an object in run time

Post  sio2interactive on Fri Feb 13, 2009 4:04 pm

Why you don't just use sio2ObjectDuplicate, check tutorial06_1 for more info...
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: Creating an object in run time

Post  andre.brito on Mon Feb 16, 2009 5:48 am

Thanks very much for the tip. I checked that function and it confirmed what I suspected. While it does help to understand the necessary steps to create an object in run time, it doesn't fit my needs. I need to create a new object, not duplicate an existing one. I know that the code I posted above is doing exactly that (creating an object similar to an existing one), but that is just to minimize the problems at this early stage and to make it as simple as possible.

Anyway, the problem was a missing call to the function sio2VertexGroupGenId (GenId again, I know ... Embarassed).

I'll move on to a more elaborate scenario now, where I'll be creating an object from live data - object's dimensions, number of vertexes and vertex values are unknown at compile time. Shall there be any problem, I'll come back to this, but for now, I just want to thank you for the help.

André
avatar
andre.brito

Posts : 13
Join date : 2009-01-05
Location : Lisbon, Portugal

View user profile http://www.evolve.pt/

Back to top Go down

Re: Creating an object in run time

Post  kzv on Thu Jun 11, 2009 11:11 pm

Hi,

I've been using SIO2 for like a month, and now I'm stuck in creating a Cube programmatically through Code at run time.

I want to avoid using sio2ObjectDuplicate. However I can use it if I have to.

I have tried many ways suggested on forum, but they don't really work for me.



Basically, I just need to create a 3D cube of Height: 10 units, Width: 10 units, Length: 10 units. (units can be anything, I can adjust it myself)

And I want to put it at X:0 Y:0 Z:0 (at the centre of a empty world)

At last I want to put 6 different images on to all 6 faces of the cube.




Just wondering if it's easy to the above in SIO2?

(if using sio2ObjectDuplicate, i'm not sure how to set the 6 faces of the cube with new images..)


If someone could give a hand that will be great!

Thanks very much!
KE

kzv

Posts : 8
Join date : 2009-06-11

View user profile

Back to top Go down

Re: Creating an object in run time

Post  sio2interactive on Fri Jun 12, 2009 12:20 am


_________________
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

A cheap work-around

Post  SuperDave on Mon Sep 07, 2009 9:10 am

I've been considering this problem while designing a puzzle game of sorts. My solution has the objects that need to be loaded in an area of the map that is unaccessible to the camera, that way a duplication can be called whenever the object is needed in gameplay. It's a cheap, and rather inefficient fix, but isn't a headache to implement.

SuperDave

Posts : 7
Join date : 2009-06-28

View user profile

Back to top Go down

Re: Creating an object in run time

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