   Manual Texture Creation (DX 8.1)
  How to manualy create a texture in DirectX 8.1? I know, there''s the D3DDEVICE8 -> CreateTexture (...) function, but it doesn''t insert any data into the created texture. How to load the actual image into the texture? (I''ll be using a 4 bit alpha + 4 bit grey color format)   ------------------------------ Master Ar2ro www.hmg.website.pl GG:5412871 | ICQ: 324085219 
  The D3DX Library has just what you need.  Look up D3DXCreateTextureFromFile().  As it''s name implies, it loads an image file into a texture.  You can also use D3DXCreateTextureFromFileInMemory() or D3DXCreateTextureFromResource().  By the way, I''m looking at the DX9 docs, but I''m pretty sure those functions were implemented in DX8. ;  2 ways: 1) Call LockRect on the texture and write the data to it, or 2) render to the texture. ;  Ok... Thx... LockRect() works ok :]As for the D3DXCreateTextureFromFile() I don''t want to use it, because it requires a known file format, and I''ll be using my own...Thanks again   ------------------------------ Master Ar2ro www.hmg.website.pl GG:5412871 | ICQ: 324085219 ;  LockRect is great if you want to do everything yourself. However, I find D3DXLoadSurfaceFromMemory to be even better, as it can be a more flexible solution. It still allows you to use your own image file format, but it automatically handles things like pitch and color conversions (if you some day would like to change the texture format used on the card, while keeping the same image files, for example to support texture compression).That''s just a suggestion. I use a hybrid of LockRect and D3DXLoadSurfaceFromMemory, but I find myself more and more using D3DXLoadSurfaceFromMemory. ;  Could you give an example code - how to use this D3DXLoadSurfaceFromMemory function, because the description in the documentation doesn''t tell me too much.While I can understand the basics on how to use it, I don''t know what should I pass as the SrcPitch nor how do the Palettes work :/...And one more question ...What format should I specify as the SrcFormat, if I use my own? Will D3DFMT_UNKNOWN work, or must I format the texture data in memory to one of the supported formats?   ------------------------------ Master Ar2ro www.hmg.website.pl GG:5412871 | ICQ: 324085219 ;  Sure, I can try. Three things you should be aware of, though: A) I've upgraded my texture routines DX9, so it'll be DX9 code (but comparing the DX81 and DX9 documentation for D3DXLoadSurfaceFromMemory, they appear to be the same).B) This is just a (edited) small part of a larger library, so the code will not work without modification.C) For certain reasons I manually resample the mipmap levels, however, you'll probably want D3DX to do that job. For that reason, the mipmap branch might not be quite what you're looking for. //LPDIRECT3DTEXTURE9 Surface - Previously created texture//Data - pointer to your source texture data//MipData - pointer to your downsampled texture data (usually done in place [Data == MipData]//D3DFORMAT DataFormat - The format of your source texture data (in your case D3DFMT_A4L4)//BitDepth - The number of bits per texel (can be less than 8 for compressed textures)//Width/Height - Size of the texture const unsigned int totallevels = Surface->GetLevelCount();const bool MipMap = (totallevels > 1)?true:false;unsigned int mip = 1; for (int miplevel = 0;miplevel<totallevels;miplevel++){    LPDIRECT3DSURFACE9 surflevel;    Surface->GetSurfaceLevel(miplevel,&surflevel);if (mip == 1)    {   const RECT SourceRect = {0,0,Width,Height};   D3DXLoadSurfaceFromMemory(surflevel,NULL,NULL,Data,DataFormat,(Width*BitDepth)/8,NULL,&SourceRect,D3DX_FILTER_NONE,0);    }    else    {   const unsigned int mipWidth = (Width/mip>1?Width/mip:1);   const unsigned int mipHeight = (Height/mip>1?Height/mip:1);   //Downsample the image here to mipWidth,mipHeight if you need it   const RECT SourceRect = {0,0,mipWidth,mipHeight};   D3DXLoadSurfaceFromMemory(surflevel,NULL,NULL,MipData,DataFormat,(mipWidth*BitDepth)/8,NULL,&SourceRect,D3DX_FILTER_NONE,0);    }    if (MipMap)   mip <<= 1;surflevel->Release();} To answer your specific questions:Normally when you store textures in memory for your own format, you will likely have the simple situation where pitch = width*texelsize.Judging from the texture format you specified, you won't have to care about palettes at all.You must format the texture data in memory in a format that D3DX understands. It can be a format the hardware does not understand, since D3DX does the conversion in software. I really don't know what D3DXLoadSurfaceFromMemory will do if it gets a D3DFMT_UNKNOWN surface as input, but I think you should avoid it.    [edited by - Mattias Welander on February 2, 2004 6:50:03 PM];  Don't quote me on this, but I'm fairly certain that if you say D3DFMT_UNKNOWN D3DX will work the same way it does when loading, ie goes through "Are you a bitmap? No. Are you a JPEG? no. Are you a Targa? No. Are you a PNG?" etc until it either finds a format to apply to it or it runs out of known images formats (In which case it would return an error).Basically you store on the HDD as your own format (compressed or encoded?), but when you load it you put it into memory as a bitmap and tell D3DX to convert that memory based bitmap into a surface. IOW, you have to have an understanding of both bitmaps and your format and how to interchange them. Pallets can be added to the bitmap just as you would any other bitmap.   [edited by - Erzengeldeslichtes on February 2, 2004 6:52:11 PM];  Erzengeldeslichtes, I''m pretty sure it doesn''t, since D3DFMT_ is used to specify the texel format and not the file format. If D3DXLoadSurfaceFromMemory accepts D3DFMT_UNKNOWN input data, my guess is it just copies it straight away with no color conversion at all, just using pitch to control how the copying is done. ;  You''re right, my bad, I was confused on that part. You''d have to check to see how exactly it works, but from my realization of what exactly we''re talking about here, I think (again, could be wrong as I said on the previous post) that it takes the format from the image itself. ie, when it loads the bitmap it checks the header of the bitmap to see if it''s a pallet, 16 bit, 24 bit, etc... 