Once you obtain the pointer to the IDirect3DTexture9 object, you need to lock the texture with the
LockRect member function to obtain a pointer to the pixel data.
Here's some code for an example DLL I've written, which sets each mipmap level to a solid colour. You should be able to figure out how to access the pixel data from this. Note that it assumes that the texture is 32-bit.
main.cpp:
// Precompiler stuff
#define DLL extern "C" _declspec(dllexport)
#define EXPORT _declspec(dllexport)
#include <d3d9.h>
#pragma comment (lib, "user32.lib")
IDirect3DTexture9 * (*GetImagePtr)(int);
void FillMemory4(void * dest, long fillDword, int sizeDwords);
// Array of colours to set each texture level
DWORD colors[6] = {
RGB(255, 0, 0),
RGB(0, 255, 0),
RGB(0, 0, 255),
RGB(255, 255, 0),
RGB(255, 255, 255),
RGB(0, 255, 0)
};
// Initialization function to get address of GetPointer()
EXPORT void Constructor()
{
// Load the DLL
HMODULE hDll = LoadLibrary("DBProImageDebug.dll");
if (!hDll) {
MessageBox(0, "Failed to load DBProImageDebug.dll", 0, 0);
return;
}
// Get the address of GetPointer()
GetImagePtr = (IDirect3DTexture9 * (*)(int)) GetProcAddress(hDll, "?GetPointer@@YAPAUIDirect3DTexture9@@H@Z");
if (!GetImagePtr) {
MessageBox(0, "Failed to get address of GetPointer()", 0, 0);
return;
}
}
DLL void ModifyTexture(int imgNum)
{
// Get the D3D texture pointer
IDirect3DTexture9 * pTexture = GetImagePtr(imgNum);
// For each level
for (int a = 0; a < pTexture->GetLevelCount(); a++) {
// Get the texture information
D3DSURFACE_DESC texDesc;
pTexture->GetLevelDesc(a, &texDesc);
// Lock the texture
D3DLOCKED_RECT locked;
HRESULT hr = pTexture->LockRect(a, &locked, NULL, 0);
// Fill the texture with a block colour
FillMemory4(locked.pBits, colors[a%6], texDesc.Width*texDesc.Height);
// Unlock the texture
pTexture->UnlockRect(a);
}
}
void FillMemory4(void * dest, long fillDword, int sizeDwords)
{
__asm
{
pushfd
cld
mov edi, dest
mov ecx, sizeDwords
mov eax, fillDword
rep stosd
popfd
}
}
resource.rc:
STRINGTABLE
BEGIN
1, "MODIFY TEXTURE%L%ModifyTexture"
END
dbp:
sync on
sync rate 60
color backdrop 0
load image "img.jpg", 1
make object cube 1, 10
texture object 1, 1
do
if spacekey()
modify texture 1
repeat : until not spacekey()
endif
if upkey() then move camera 3
if downkey() then move camera -3
sync
loop