building Texture without loading from file

Started by
5 comments, last by PAiNIC 18 years, 7 months ago
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?
Advertisement
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.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

this thread explains how to do it.

Look at the last post about the function D3DXFillTexture( )
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

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.
thanks
one more question dude
how can i retrieve the color values at location x,y of the texture loaded from a file?
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.

This topic is closed to new replies.

Advertisement