Sign in to follow this  
dxdotnet1

building Texture without loading from file

Recommended Posts

how can i build a texture without loading from a file i create my texture using the CreateTexture function from the device but i dont have any idea on how to modify the contents of the texture, for example i want to put a red color on position x, y of the texture? how can i do it?

Share this post


Link to post
Share on other sites
You call the Lock function of your surface/texture. This will give you a LOCKRECT struct filled with a pointer to the pixel data and a pitch.

Now to get the position of your pixel you need to know the format of your surface. The pitch is the number of bytes from line start to line start. Never assume that all the image data is one complete block. Instead always think of it as separate lines.

There's a nice diagram and further explanation at nexe.gamedev.net.

Share this post


Link to post
Share on other sites
thanks for the reply guys

can u guys show me some sample code on how to do the following?

a) get the color at the x,y location of the texture
b) set color at x,y location of the texture

Share this post


Link to post
Share on other sites
First you need to create an array of pixels which will represent your texture(and which you will later pass to DirectX)

char* image = new char[width*height*bpp];

This is your array of pixels.
bpp=3 if you just want R, G, B (Red, Green, Blue)
bpp=4 if you want R, G, B, and Alpha

Each char(byte) represents a color componet (Red, Green, Blue, or alpha)
3 consecutive chars represent a single color.

So for example if you do this

image[0]=255;
image[1]=255;
image[2]=255;

then the first pixel of your image is going to be white (assuming this is an RGB image)

So if you want to put a certain color at a location x, y then you do this (assuming you use zero based coordiantes, and your [0,0] is located in the upper left corner of the image)

image[y*width+x] = Red
image[y*width+x+1] = Green
image[y*width+x+2] = Blue

Where Red, Green, and Blue are the 3 components of the color you want to place.
Be aware though, you can't modify the pixels of the texture once you pass it to DirectX.

Don't forget to delete array after you pass it to DirectX.

Hope this helps.

Share this post


Link to post
Share on other sites
LockRect will fill in a D3DLockedRect Struct when you call it, giving you a pointer to the texture data and the pitch of that texture data (pitch is kind of like the VB 'stride' equivalent in a texture - the DX9 help explains it fully under DirectX Graphics -> Programming Guide -> Getting Started -> Direct3D Surfaces -> Width vs. Pitch).

Using this pointer, you can both write and retrieve the texture information. Note that only certain sorts of textures can be locked, and when they ARE locked it is a copy in system memory that is locked.

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