Sign in to follow this  
phb5000

[MDX] Texture from Color

Recommended Posts

Is there a simple way of creating a Direct3D.Texture by specifying a ColorValue? Would I first have to create a Drawing.Bitmap, and then create the texture using that stream? Or is there an easier way?

Share this post


Link to post
Share on other sites
I don't quite follow, do you want to create a texture that just consists of one colour? If so, why? Just use vertex colour.
If you absolutely must, you'll almost certainly have to create a 1x1 texture, lock it, put one pixel into it, then unlock it.

Share this post


Link to post
Share on other sites
You can simply do this by locking the texture and writing the color information to it in the appropriate color format. The format you write has to match the format you used to create the texture, so for an A8R8G8B8 texture you can use this code:


// lock texture
int[,] data = (int[,])frameTexture.LockRectangle( typeof(int), 0, LockFlags.None, width, height );

for (int v = 0; v < height; v++)
{
for (int u = 0; u < width; u++)
{
// set color data on texture
data[v,u] = unchecked((int)0xffff0000); // red, you can use Color.ToARGB() to obtain this
}
}

// unlock texture
frameTexture.UnlockRectangle(0);



@Steve:

As for why you would want to use these textures over vertex color, I've found it to be a good thing for HLSL shaders and multi subset meshes. I had some meshes with subsets that either used material diffuse color or a texture. By using a texture for all subsets (even when they're only a single color) you'll only need to use the texture for the final color, saving you at least a multiplication and at best an if statement in the pixel shader.

Share this post


Link to post
Share on other sites
Quote:
Original post by remigius
As for why you would want to use these textures over vertex color, I've found it to be a good thing for HLSL shaders and multi subset meshes. I had some meshes with subsets that either used material diffuse color or a texture. By using a texture for all subsets (even when they're only a single color) you'll only need to use the texture for the final color, saving you at least a multiplication and at best an if statement in the pixel shader.
Ah, I always forget about pixel shaders [smile]

Share this post


Link to post
Share on other sites
Thanks for the replies,

Im not using the vertex color since Im creating a 2D application using sprites :P, but thanks for that info as well.

Share this post


Link to post
Share on other sites
Quote:
Original post by phb5000
Thanks for the replies,

Im not using the vertex color since Im creating a 2D application using sprites :P, but thanks for that info as well.


That's not a reason not to use vertex colors. You can get some nifty effects by altering the vertex color of your sprites.

Share this post


Link to post
Share on other sites
An MDX Sprite is stored internally as a textured quad or pair of triangles. You'll notice that the Draw() and Draw2D() methods include a color parameter. This parameter will modulate the vertex colors by the specified value. Normally, you would specify Color.White for this value and get the original texture colors. However, you can specify other colors to get nifty effects like fading, shades of red to indicate damage levels, etc.

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