Advertisement Jump to content
Sign in to follow this  

building Texture without loading from file

This topic is 4894 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

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

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


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
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!