Creating Texture from Buffer

Started by
6 comments, last by Daivuk 18 years, 7 months ago
Hi, I search a lot in the MSDN and didnt find anything about that. I was creating 3d for long time with OpenGL. Now for school I have to make a demo in Directx. So for loading texture I'm using this :

D3DXCreateTextureFromFileEx(
	D3DDevice, 
	"main/textures/textureTest.tga", 
	D3DX_DEFAULT, 
	D3DX_DEFAULT,
	D3DX_DEFAULT, 
	0, 
	D3DFMT_UNKNOWN, 
	D3DPOOL_MANAGED,
	D3DX_DEFAULT, 
	D3DX_DEFAULT, 
	0, 
	NULL, 
	NULL, 
	&Texture);

Thats work fine for texture from file. Now, I want to use my own texture format, that I use in many of my engines :P This file contain Base texture, detail texture, specular texture, normal map, etc. I have my own loader for this, and for each layer I have the following input :

unsigned char *Buffer
int Width
int Height
int BytePerPixel

So with that I can create easily a texture in opengl. But HOW in directx from this buffer??? The buffer is just an array of all color RGB or RGBA. What I did first is to try this function :

D3DXCreateTextureFromFileInMemoryEx(      
	D3DDevice,
	Buffer,
	BytePerPixel*Width*Height,
	Width,
	Height,
	D3DX_DEFAULT,
	0,
	(BytePerPixel == 3) ? D3DFMT_R8G8B8 : D3DFMT_A8B8G8R8,
	D3DPOOL_MANAGED,
	D3DX_DEFAULT,
	D3DX_DEFAULT,
	0,
	NULL,
	NULL,
	&Texture);

Using this function the result it White, thats mean NULL, didnt work ;) So, I found this on MSDN :

D3DXCreateTexture(      
	D3DDevice,
	Width,
	Height,
	D3DX_DEFAULT,
	0,
	(BytePerPixel == 3) ? D3DFMT_R8G8B8 : D3DFMT_A8B8G8R8,
	D3DPOOL_MANAGED,
	&Texture);

Hum, this create me a texture, but empty (black) Now, how to put my buffer in it then it will automaticly create my Mipmap or there is a function to create a texture from my buffer?? thanks!
Advertisement
D3DXCreateTextureFromFileInMemoryEx isn't what you want, it loads a texture from file data in memory, a jpeg file copied into memory for example, not a raw buffer of image data.

To create a texture from a buffer in memory, first create a texture of the appropriate size ( although this method can also automatically resize the image if the size of your texture is different ), then get a pointer to its top level surface using the GetSurfaceLevel method. Then call D3DXLoadSurfaceFromMemory, and call the GenerateMipSubLevels method of the texture. (your texture must be created with the AUTOGENMIPMAPS flag for this to work)
Thanks!! its working perfectly!!!

It will be worked before if MSDN explained it like this ;)
Quote:Original post by Oxyacetylene
D3DXCreateTextureFromFileInMemoryEx isn't what you want, it loads a texture from file data in memory, a jpeg file copied into memory for example, not a raw buffer of image data.


How can i copy a file into memory, eg. jpg??

Quote:Original post by Anonymous Poster
Quote:Original post by Oxyacetylene
D3DXCreateTextureFromFileInMemoryEx isn't what you want, it loads a texture from file data in memory, a jpeg file copied into memory for example, not a raw buffer of image data.


How can i copy a file into memory, eg. jpg??


You need a white chicken and red paint.
Make a pentagram with the red paint. Slice the chickens throat and pour its blood at each edge of the pentagram. Cast the magic word "Iksalamir!".
If all goes well (and the moon is in the right conjugation to Alpha Centauri), your JPEG file will be copied to memory.
Hum, nice answer but not very useful lol

To load a file into memory its simple :
Open the file, and read all the bytes and put them in an array.
Then close the file, and pass this array to the function :P

So its like the same file, but in memory ;)

This is useful if you pack all your files into something like a .pk3 ;)
thx, is it realy needed to be an array of char?
Hum, probably an array of what you want.
It recieve an void*, so D3D convert it into what he want ;)
But it have to be exactly the file

This topic is closed to new replies.

Advertisement