Sign in to follow this  

Apply a color and a texture to a mesh?

This topic is 4381 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, is it possible to set a default color to a mesh, e.g. a box, and then set a texture to two of the box squares? I want to show a texture on the left and right side of the box and the other 4 sides should have the color e.g. red or green. A way would be to create a bitmap that fits on the box, but then I would have to create one bitmap with green color and the 2 pictures for the left and right sides. Similar I would have to create the same bitmap with another color and so on... That's what I want to avoid. Another possible way would be to create a colored bitmap and a 2nd one with the image and then render the colored one on 4 of the box sides and the picture on the last 2 sides. But I don't know if this is possible in DX. Hope that anyone knows a better way... (some code snippets would be helpful) Best regards Pikebu

Share this post


Link to post
Share on other sites
You could set the material you want (with the color), and set the texture to NULL. Then when you want the face to have the texture, call SetTexture again with the new texture. I believe you can set an alpha color on the texture to remove all but the part you want showing.

Share this post


Link to post
Share on other sites
This is called skinning and is usually the best approach. There are other ways to do it mathematically. The best and easiest way in my opinion would be to create a mesh in your favourite modelling package and then create a UV Map and then apply your colors and pictures to the texture. This in my opinion is the easiest way.

I hope this helps.
Take care.

Share this post


Link to post
Share on other sites
Quote:
Original post by stromdotcom
You could set the material you want (with the color), and set the texture to NULL. Then when you want the face to have the texture, call SetTexture again with the new texture. I believe you can set an alpha color on the texture to remove all but the part you want showing.


When I first apply a material to my box and then re render it with the texture applied, the material is replaced with the texture. I have replaced all magenta colored pixels in my bitmap with the alpha value, but now I'm just able to look through the box and don't see the material i set in the 1st. step :-(

Share this post


Link to post
Share on other sites
Quote:
Original post by stromdotcom
You could set the material you want (with the color), and set the texture to NULL. Then when you want the face to have the texture, call SetTexture again with the new texture.

Don't do this. When you set the texture to NULL the pixel pipeline is still reading from a texture, but being undefined it substitutes opaque white. On a good driver, that is. On a dodgy driver anything could happen. So unless you never plan on releasing anything to the public, it's in your best interest to do things more safely.

By default things are set up like this:

device->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_MODULATE);
device->SetTextureStageState(0,D3DTSS_COLORARG1,D3DTA_TEXTURE);
device->SetTextureStageState(0,D3DTSS_COLORARG2,D3DTA_DIFFUSE);

Your diffuse colour is modulated by the texture. Setting the texture to NULL sets that part of the equation to opaque white so you typically will then get just the diffuse colour. But as I mentioned above, it's not a good idea to rely on that.

device->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_SELECTARG2);
device->SetTextureStageState(0,D3DTSS_COLORARG1,D3DTA_TEXTURE);
device->SetTextureStageState(0,D3DTSS_COLORARG2,D3DTA_DIFFUSE);

This setup explicitly states that only the diffuse colour will be used. Explicit is good.



Share this post


Link to post
Share on other sites
The method Pieter is one of the easier ways to do it. The other main way is to draw your cube faces separately, that way you can set any textures or materials you want. This is also one of the most inefficient methods you could choose though, so there is a cost for the flexibility.

Cubes really are a horrible example for doing 3D. They always seem like a great first object but they have annoying issues like this.

Share this post


Link to post
Share on other sites

This topic is 4381 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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