Sign in to follow this  
vecchia

Apply a texture to one object of the mesh

Recommended Posts

Hi all,
I have a mesh that contains 9 objects, I would like to texture the object that the user clicks. I alredy have the code that manage the mouse click of the user but I don't understand how could I apply the texture. Can someone help me?

Share this post


Link to post
Share on other sites
It's not clear what you mean by "object." Do you mean, perhaps, that you have an object with 9 meshes?

If so, can you select one of the meshes with your mouse pick?

Also, are the texture coordinates setup for each mesh and you just want to apply a texture to that mesh, or are you trying to apply texture coordinates to the area of the mesh where you click the mouse?

What API are you using? OpenGL? DirectX?

Share this post


Link to post
Share on other sites
I'm using DirectX 9, I'm loading a single .x file that contains 9 spheres and I draw them. I would be able to click with the mouse on one of them and draw a texture that texturize the ball

Share this post


Link to post
Share on other sites
Are the 9 spheres separate meshes, or are they all part of a single mesh? That is, do you have 9 LPD3DXMESH objects or just a single mesh?

If the 9 spheres are all part of a single mesh, it will extremely difficult to single out just a portion of that single mesh as a sphere and assign a texture to just that portion. You'll be better off creating 9 separate meshes ( e.g., with D3DXCreateSphere, or another algorithm ). Alternatively, if you are creating the x-file in an editor, you'll need to create 9 separate x-files so you can load each sphere mesh separately.

Also, as asked above, have the texture coordinates already been created for the spheres? If not, you'll have to assign appropriate texture coordinates to each sphere mesh using an algorithm. If you're creating the spheres in another editor (such as Blender), assign the texture coordinates in the editor, if you can.

Also, as asked above, do you know already know how to pick a mesh with the mouse, or are you asking how to do that? If you need to know how to pick a mesh, take a look at the picking example in the SDK, search here on gamedev for "mouse picking," or google for "DirectX mouse pick."

Share this post


Link to post
Share on other sites
the mesh is only one and I have alredy created the code to pick a mesh with the mouse. I'm usingm blender to create the 3D model and export it to .x format. Basically what I want to do is to load the mesh with predefines texture and change the texture of the picked sphere when the user click on it

Share this post


Link to post
Share on other sites
As mentioned, it will be extremely difficult to determine what boundaries of the mesh comprise a sphere in one mesh.

Do you render the mesh using something like: for( WORD a=0; a < numMaterials; a++ ) DrawSubset(a)?

If so,then it appears you've assigned a different material/texture to each sphere. The mesh you load should have an attribute table - a table containing a number for each face of the mesh which specifies which subset that face belongs to.

It's still going to be a little difficult, but, once you've picked the mesh and determined which face you've picked, you can look in the attribute table to see which subset the picked face belongs to. Then set the desired texture for that subset when you render it.

Share this post


Link to post
Share on other sites
[quote name='Buckeye' timestamp='1302557221' post='4797277']Do you render the mesh using something like: for( WORD a=0; a < numMaterials; a++ ) DrawSubset(a)?[/quote]
yes, I'm using that code

[quote name='Buckeye' timestamp='1302557221' post='4797277']As mentioned, it will be extremely difficult to determine what boundaries of the mesh comprise a sphere in one mesh.[/quote]
Ok but wouldn't be more difficult to render N spheres? I must rotate the 3D model, so one mesh is simpler to rotate...

Share this post


Link to post
Share on other sites
[quote name='Stefano A.' timestamp='1302618566' post='4797510']
so anyway it's impossible to apply a new texture to a single sphere on one mesh?
[/quote]
From previous post:
[quote]once you've picked the mesh and determined which face you've picked, you can look in the attribute table to see which subset the picked face belongs to. Then set the desired texture for that subset when you render it.[/quote]

Share this post


Link to post
Share on other sites
[quote name='Buckeye' timestamp='1302557221' post='4797277']It's still going to be a little difficult, but, once you've picked the mesh and determined which face you've picked, you can look in the attribute table to see which subset the picked face belongs to. Then set the desired texture for that subset when you render it.
[/quote]
do you have an example of how to do it? I know that I can get the face index with the D3DXIntersect function but I don't know how to use it

Share this post


Link to post
Share on other sites
[quote name='Stefano A.' timestamp='1302704583' post='4797957']
[quote name='Buckeye' timestamp='1302557221' post='4797277']It's still going to be a little difficult, but, once you've picked the mesh and determined which face you've picked, you can look in the attribute table to see which subset the picked face belongs to. Then set the desired texture for that subset when you render it.
[/quote]
do you have an example of how to do it? I know that I can get the face index with the D3DXIntersect function but I don't know how to use it
[/quote]
I don't have an example but I can describe how to do it.

Once you have the face index from your pick:

1. Lock the attribute buffer of your mesh.
2. Get the face-attribute-number = attributeBuffer[ faceIndex ]
3. Unlock the attribute buffer.

The face-attribute-number is a mesh subset number. I.e., in your rendering loop where you have [i]for( WORD subsetNum=0; subsetNum < numMaterials; subsetNum++)[/i].

Set the texture for subset face-attribute-number to the texture you want, perhaps something like:
[code]
texture[ face-attribute-number ] = someTexture;
//... then, in your rendering loop
for( WORD subsetNum=0; subsetNum < numMaterials; subsetNum++ )
{
device->SetTexture( 0, texture[ subsetNum ] );
mesh->DrawSubset( subsetNum );
}
[/code]

The texture you set for the picked sphere will be rendered for that subset.

Share this post


Link to post
Share on other sites
[quote name='Stefano A.' timestamp='1302721654' post='4798082']
mmm, should the 3D model have alredy a texture or can I load it after?[/quote]
The model doesn't have to be rendered with a texture. But you should call device->SetTexture(0,NULL) for a subset that doesn't have a texture assigned.

If you're asking whether the textures can be loaded after the model file has been loaded, that's a yes.

[quote]Also, should I have the texture coordinates when I call SetTexture and DrawSubset?
[/quote]
The texture coordinates should be a part of the vertex structure for the mesh. If the mesh vertices don't include texture coordinates then you can't successfully render a texture to any portion of it.

Have you looked at some of the examples in the SDK which render meshes with textures? You might want to do that to get a better idea how vertices and texture coordinates are arranged.

Share this post


Link to post
Share on other sites
mmm, I need help:

[code]DWORD *attributes;
DWORD attrId;
mesh->LockAttributeBuffer(D3DLOCK_READONLY, &attributes);
attrId = attributes[pFaceIndex];
mesh->UnlockAttributeBuffer();[/code]

with this code I should get the face attribute number. So, now to draw a new texture instead of the one alredy rendered?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this