Programatically applying a texture
3 posters
Programatically applying a texture
Hi,
maybe this is an easy one, and maybe I should get it from some other thread here, however I need to apply a texture to a SIOobject at runtime. I have tried to scrape together code from different sections here in to forum and come up with something, which does not work... =) even in the debugger everything seems to be correctly populated etc. but I sure am missing something:
I have also tried with just loading a texture from a sio resource e.g.:
And I get no image. There probably is something fundamental that I am missing...
maybe this is an easy one, and maybe I should get it from some other thread here, however I need to apply a texture to a SIOobject at runtime. I have tried to scrape together code from different sections here in to forum and come up with something, which does not work... =) even in the debugger everything seems to be correctly populated etc. but I sure am missing something:
- Code:
SIO2object * plane = sio2ResourceGetObject( sio2->_SIO2resource, "object/Plane");
if(plane)
{
char name[ SIO2_MAX_CHAR ];
// Create a name for our new object
sprintf( name, "%s_%u_%u", plane->name, x, y);
// Get the raw image
CFDataRef rawImage = CGDataProviderCopyData(CGImageGetDataProvider(image.image.CGImage));
const UInt8 * rawImagePtr = CFDataGetBytePtr(rawImage);
int length = CFDataGetLength(rawImage);
// Create a sio image
SIO2image * mapImage = sio2ImageInit(name);
mapImage->tex = (unsigned char *) malloc (length);
memcpy(mapImage->tex, rawImagePtr, length);
// Set the position
plane->_SIO2transform->loc->x = image.screenLocation.origin.x;
plane->_SIO2transform->loc->y = image.screenLocation.origin.y;
plane = sio2ObjectDuplicate( plane, plane->_SIO2transform, name, 0 );
// N_VGROUP
plane->n_vgroup = 1;
if( plane->n_vgroup )
{ plane->_SIO2vertexgroup = ( SIO2vertexgroup ** ) malloc( plane->n_vgroup * sizeof( SIO2vertexgroup ) ); }
// VGROUP
plane->_SIO2vertexgroup[0] = sio2VertexGroupInit(name);
SIO2vertexgroup * vert = plane->_SIO2vertexgroup[0];
vert->_SIO2material = sio2MaterialInit(name);
vert->_SIO2material->_SIO2image[SIO2_MATERIAL_CHANNEL0] = mapImage;
sio2VertexGroupGenId(vert);
sio2ResourceBindMaterial(sio2->_SIO2resource, plane);
sio2ObjectReset();
sio2ResourceResetState();
}
I have also tried with just loading a texture from a sio resource e.g.:
- Code:
vert->_SIO2material->_SIO2image[SIO2_MATERIAL_CHANNEL0] = sio2ResourceGetImage(sio2->_SIO2resource, "smoke" );
And I get no image. There probably is something fundamental that I am missing...
pushkin- Posts : 2
Join date : 2009-03-05
Re: Programatically applying a texture
Hi,
I have been trying to assign an image to 1 side of a 3D cube programatically through code.
and I have read almost all the posts in the forum relating to assigning textures, but some of them are 2D, and some are talking about other things (i believe).. so I couldn't really find a clear answer..
1. Can anyone please tell me how to assign an image to 1 side of a cube..?
2. and when assigning the image, you obviously need to choose which of the 6 sides you want to put on, so how do you choose the side? (i only want to choose 1 of the 6 sides of a cube to apply the image)
3. does the code in this post does the "picking of the side"..? if so, which lines...? i'm a bit confused of this code.
Thanks in advance for any help!
KE
I have been trying to assign an image to 1 side of a 3D cube programatically through code.
and I have read almost all the posts in the forum relating to assigning textures, but some of them are 2D, and some are talking about other things (i believe).. so I couldn't really find a clear answer..
1. Can anyone please tell me how to assign an image to 1 side of a cube..?
2. and when assigning the image, you obviously need to choose which of the 6 sides you want to put on, so how do you choose the side? (i only want to choose 1 of the 6 sides of a cube to apply the image)
3. does the code in this post does the "picking of the side"..? if so, which lines...? i'm a bit confused of this code.
Thanks in advance for any help!
KE
kzv- Posts : 8
Join date : 2009-06-11
Re: Programatically applying a texture
1. Can anyone please tell me how to assign an image to 1 side of a cube..?
>> Multimaterial, assign multiple vertex group and assign a different material to each of them, check my channel on youtube, I create a quick tutorial on that.
2. and when assigning the image, you obviously need to choose which of the 6 sides you want to put on, so how do you choose the side? (i only want to choose 1 of the 6 sides of a cube to apply the image)
>> You simply have to check the vertexgroup name, simply loop through the vertex group of your object then assign the image to the material.
3. does the code in this post does the "picking of the side"..? if so, which lines...? i'm a bit confused of this code.
>> There's no mechanism to check which "face" that you pick, you can either go with 6 objects or do a physic ray pick and depending on the normal picked you basically know which face. You can create your own color picking method that create a unique color for each faces of the object.
>> Multimaterial, assign multiple vertex group and assign a different material to each of them, check my channel on youtube, I create a quick tutorial on that.
2. and when assigning the image, you obviously need to choose which of the 6 sides you want to put on, so how do you choose the side? (i only want to choose 1 of the 6 sides of a cube to apply the image)
>> You simply have to check the vertexgroup name, simply loop through the vertex group of your object then assign the image to the material.
3. does the code in this post does the "picking of the side"..? if so, which lines...? i'm a bit confused of this code.
>> There's no mechanism to check which "face" that you pick, you can either go with 6 objects or do a physic ray pick and depending on the normal picked you basically know which face. You can create your own color picking method that create a unique color for each faces of the object.
Re: Programatically applying a texture
Hi SIO2,
Thanks for your answer, that makes alot easier for me to understand it..
so I have came up with the follwing code:
So this assigns all 6 sides with a image, but i just don't know which side it goes to. Is my thinking correct..? Please let me know.. cos i don't have my OSX with me.. i'll have to try it out tomorrow...
so just wondering if i'm missing anything in between..?
for example, do I need the following line for each of the vertex? (total 6 of them)
also, do I need any of those 3 lines?
Thanks alot for your help!!
Thanks for your answer, that makes alot easier for me to understand it..
so I have came up with the follwing code:
- Code:
// Init all images
SIO2image * img1 = sio2ImageInit(name1);
SIO2image * img2 = sio2ImageInit(name2);
SIO2image * img3 = sio2ImageInit(name3);
SIO2image * img4 = sio2ImageInit(name4);
SIO2image * img5 = sio2ImageInit(name5);
SIO2image * img6 = sio2ImageInit(name6);
// Get all the vertex
SIO2vertexgroup * vert1 = cube001->_SIO2vertexgroup[0];
SIO2vertexgroup * vert2 = cube001->_SIO2vertexgroup[1];
SIO2vertexgroup * vert3 = cube001->_SIO2vertexgroup[2];
SIO2vertexgroup * vert4 = cube001->_SIO2vertexgroup[3];
SIO2vertexgroup * vert5 = cube001->_SIO2vertexgroup[4];
SIO2vertexgroup * vert6 = cube001->_SIO2vertexgroup[5];
// Apply all the images
vert1->_SIO2material->_SIO2image[SIO2_MATERIAL_CHANNEL0] = img1;
vert2->_SIO2material->_SIO2image[SIO2_MATERIAL_CHANNEL0] = img2;
vert3->_SIO2material->_SIO2image[SIO2_MATERIAL_CHANNEL0] = img3;
vert4->_SIO2material->_SIO2image[SIO2_MATERIAL_CHANNEL0] = img4;
vert5->_SIO2material->_SIO2image[SIO2_MATERIAL_CHANNEL0] = img5;
vert6->_SIO2material->_SIO2image[SIO2_MATERIAL_CHANNEL0] = img6;
So this assigns all 6 sides with a image, but i just don't know which side it goes to. Is my thinking correct..? Please let me know.. cos i don't have my OSX with me.. i'll have to try it out tomorrow...
so just wondering if i'm missing anything in between..?
for example, do I need the following line for each of the vertex? (total 6 of them)
- Code:
sio2VertexGroupGenId(vert1);
also, do I need any of those 3 lines?
- Code:
sio2ResourceBindMaterial(sio2->_SIO2resource, cube001);
sio2ObjectReset();
sio2ResourceResetState();
Thanks alot for your help!!
kzv- Posts : 8
Join date : 2009-06-11
Re: Programatically applying a texture
SIO2vertexgroup->name
where name is whatever you set in Blender...
where name is whatever you set in Blender...
Similar topics
» Swapping a texture
» Applying a texture to a vertex array
» Draw font to texture or render to texture possible?
» Programatically Creating Objects
» Why md2 can not do a transparent texture?
» Applying a texture to a vertex array
» Draw font to texture or render to texture possible?
» Programatically Creating Objects
» Why md2 can not do a transparent texture?
Permissions in this forum:
You cannot reply to topics in this forum