Ive just gotten started with cocos3d and I'm making a basic card game. I've already got the model with textures, but I don't know how to specify what texture to use for a given object, or node. I've got the textured model loaded and rendering using the basic cocos3d template. Here's my setup code:
//Init
for ( int i = 0; i < 5; ++i )
{
cardNodes[i] = [CC3PODResourceNode nodeFromFile: @"Card.pod"];
[self addChild:cardNodes[i]];
cardNodes[i].location = cc3v ( ( i - 2)*7.5, 0, 0);
cardNodes[i].rotation = cc3v ( 0,-90,180);
}
-(void) startGame
{
srand(0);
//Draw 5 random cards
unsigned int cards[5] = {0};
for ( unsigned int i = 0; i < 5; ++i )
{
unsigned int card = ( rand() % 52 ) + 1;
for ( int other = i-1; other >= 0; --other )
{
if ( cards[other] == card )
{
card = ( rand() % 52 ) + 1;
other = i-1;
}
}
cards[i] = card;
//Set texture of card node[i] somehow here?
}
}
So how can I change what texture is being used so I don't have to create a new pod file for each card?
Repeatedly loading a POD file does not create multiple copies within memory. That is because the underlying CC3PODResource
is cached (on purpose), so you'll end up referencing the same model in each of your array elements.
Instead, you should load the card POD once and then create copies of it using the standard copy
method. This is an efficient copy. It copies configurable properties, but reuses the same underlying mesh vertices. You can then position, color and texture the card copies individually.
To assign a texture to an individual card, simply use the texture
property.
CC3DemoMashUp
contains lots of examples of assigning textures to models, and of making many copies of a single mesh node. For instance, try pressing the grid button when running the demo.