Jump to content
  • Advertisement
Sign in to follow this  
blueshogun96

Dynamic Texture problem... again.

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

Hi, I'm trying to get this Dynamic texture thing to work, but I'm doing something wrong, can anyone help me? The code is below. It's written in C++ for simplicity. Thanks
#define WIN32_LEAN_AND_MEAN
#define WIN32_EXTRA_LEAN

#include <windows.h>
#include <stdio.h>

#include <d3d9.h>
#include <d3dx9.h>

#define D3DFVF_TLVERTEX D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1

typedef struct _D3DTLVERTEX {
    float sx; /* Screen coordinates */
    float sy;
    float sz;
    float rhw; /* Reciprocal of homogeneous w */
    D3DCOLOR color; /* Vertex color */
    float tu; /* Texture coordinates */
    float tv;
    _D3DTLVERTEX() { }
    _D3DTLVERTEX(const D3DVECTOR& v, float _rhw,
                 D3DCOLOR _color, 
                 float _tu, float _tv)
    { sx = v.x; sy = v.y; sz = v.z; rhw = _rhw;
      color = _color; 
      tu = _tu; tv = _tv;
    }
} D3DTLVERTEX, *LPD3DTLVERTEX;


IDirect3D9*			lpD3D=NULL;
IDirect3DDevice9*	lpD3DDevice=NULL;
IDirect3DTexture9*	lpDynamicTexture=NULL;

BOOL InitialiseWindow(HWND hWnd,HINSTANCE hThisInstance);
BOOL InitialiseDirect3D(HWND hWnd);
void ShutdownDirect3D(void);
HRESULT Blit2DTexture(float left,float top,float right,float bottom,
					  DWORD DiffuseColor,float z,IDirect3DTexture9* lpTex,
					  IDirect3DDevice9* lpDevice);

BOOL InitialiseScene(void);
void ShutdownScene(void);
void Render(void);

LRESULT CALLBACK WindowProcedure(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam)
{
	switch(Msg)
	{
	case WM_KEYDOWN:
	case WM_CLOSE:
		PostQuitMessage(0);
		break;
	}

	return DefWindowProc(hWnd,Msg,wParam,lParam);
}

BOOL InitialiseWindow(HWND hWnd,HINSTANCE hThisInstance)
{
	WNDCLASSEX wc;

	wc.cbSize=sizeof(WNDCLASSEX);
	wc.cbClsExtra=0;
	wc.cbWndExtra=0;
	wc.style=CS_HREDRAW|CS_VREDRAW|CS_OWNDC;
	wc.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
	wc.hCursor=LoadCursor(NULL,IDC_CROSS);
	wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
	wc.hIconSm=wc.hIcon;
	wc.hInstance=hThisInstance;
	wc.lpfnWndProc=WindowProcedure;
	wc.lpszClassName="dt";
	wc.lpszMenuName=NULL;

	if(!RegisterClassEx(&wc))
		return FALSE;

	hWnd=CreateWindowEx(0,"dt","Dynamic Texture",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
						400,300,NULL,NULL,hThisInstance,NULL);
	if(!hWnd)
		return FALSE;

	ShowWindow(hWnd,SW_SHOW);
	UpdateWindow(hWnd);

	if(!InitialiseDirect3D(hWnd))
	{
		ShutdownDirect3D();
		return FALSE;
	}

	return TRUE;
}

int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
					 LPSTR lpCmdLine,int nShowCmd)
{
	HWND hWnd=NULL;
	MSG msg;

	if(!InitialiseWindow(hWnd,hInstance))
		return EXIT_FAILURE;

	if(!InitialiseScene())
	{
		ShutdownScene();
		ShutdownDirect3D();
		return EXIT_FAILURE;
	}

	while(TRUE)
	{
		if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
		{
			if(msg.message==WM_QUIT)
				break;
			else
			{
				TranslateMessage(&msg);
				DispatchMessage (&msg);
			}
		}
		else
		{
			Render();
		}
	}

	ShutdownScene();
	ShutdownDirect3D();
	DestroyWindow(hWnd);

	return (int)msg.wParam;
}

BOOL InitialiseDirect3D(HWND hWnd)
{
	D3DDISPLAYMODE d3ddm;
	D3DPRESENT_PARAMETERS d3dpp;
	HRESULT d3drval;

	lpD3D=Direct3DCreate9(D3D_SDK_VERSION);

	if(!lpD3D)
	{
		MessageBox(NULL,"Direct3D Init Failed\n\nDirect3DCreate9","Error",MB_OK);
		return FALSE;
	}

	d3drval=lpD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3ddm);
	if(FAILED(d3drval))
	{
		MessageBox(NULL,"Direct3D Init Failed\n\nIDirect3D9::GetAdapterDisplaymode","Error",MB_OK);
		return FALSE;
	}

	ZeroMemory(&d3dpp,sizeof(d3dpp));
	d3dpp.BackBufferCount=1;
	d3dpp.BackBufferWidth=400;
	d3dpp.BackBufferHeight=300;
	d3dpp.BackBufferFormat=D3DFMT_X8R8G8B8;
	d3dpp.hDeviceWindow=hWnd;
	d3dpp.Windowed=TRUE;
	d3dpp.EnableAutoDepthStencil=FALSE;
	d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;


	d3drval=lpD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,
								D3DCREATE_SOFTWARE_VERTEXPROCESSING,
								&d3dpp,&lpD3DDevice);
	if(FAILED(d3drval))
	{
		MessageBox(NULL,"Direct3D Init Failed\n\nIDirect3D9::CreateDevice","Error",MB_OK);
		return FALSE;
	}

	return TRUE;
}

void ShutdownDirect3D(void)
{
	if(lpD3DDevice)
		lpD3DDevice->Release();

	if(lpD3D)
		lpD3D->Release();
}

BOOL InitialiseScene(void)
{
	HRESULT d3drval;
	D3DLOCKED_RECT LockedRect;

	d3drval=lpD3DDevice->CreateTexture(64,64,0,D3DUSAGE_DYNAMIC,D3DFMT_X8R8G8B8,
								D3DPOOL_DEFAULT,&lpDynamicTexture,NULL);
	if(FAILED(d3drval))
	{
		MessageBox(NULL,"Init Scene fail\n\nIDirect3DDevice9::CreateTexture","Error",MB_OK);
		return FALSE;
	}

	d3drval=lpDynamicTexture->LockRect(0,&LockedRect,NULL,D3DLOCK_DISCARD);
	if(FAILED(d3drval))
	{
		MessageBox(NULL,"Init Scene fail\n\nIDirect3DTexture9::LockRect","Error",MB_OK);
		return FALSE;
	}

	DWORD* dataPtr=(DWORD*)LockedRect.pBits;

	for(int x=0;x<64;x++)
	{
		for(int y=0;y<64;y++)
		{
			dataPtr[(((LockedRect.Pitch/sizeof(DWORD))*y)+x)]=(DWORD)rand()%0xffffffff;
		}
	}

	lpDynamicTexture->UnlockRect(0);

	return TRUE;
}

void ShutdownScene(void)
{
	if(lpDynamicTexture)
		lpDynamicTexture->Release();
}

HRESULT Blit2DTexture(float left,float top,float right,float bottom,
					  DWORD DiffuseColor,float z,IDirect3DTexture9* lpTex,
					  IDirect3DDevice9* lpDevice)
{
	float rhw=1.0f/(z*990.0f+10.0f);
	D3DTLVERTEX VertexPtr[4];

	VertexPtr[0]=D3DTLVERTEX(D3DXVECTOR3(left-0.5f,top-0.5f,z),rhw,DiffuseColor,0.0f,0.0f);
	VertexPtr[1]=D3DTLVERTEX(D3DXVECTOR3(right-0.5f,top-0.5f,z),rhw,DiffuseColor,1.0f,0.0f);
	VertexPtr[2]=D3DTLVERTEX(D3DXVECTOR3(right-0.5f,bottom-0.5f,z),rhw,DiffuseColor,1.0f,1.0f);
	VertexPtr[3]=D3DTLVERTEX(D3DXVECTOR3(left-0.5f,bottom-0.5f,z),rhw,DiffuseColor,0.0f,1.0f);

	lpDevice->SetTexture(0,lpTex);
	lpDevice->SetFVF(D3DFVF_TLVERTEX);
	lpDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP,2,VertexPtr,0);

	return TRUE;
}

void Render(void)
{
	lpD3DDevice->Clear(0,NULL,D3DCLEAR_TARGET,0x0,1.0f,0);

	lpD3DDevice->BeginScene();
	Blit2DTexture(0,0,64,64,0xffffffff,0.1f,lpDynamicTexture,lpD3DDevice);
	lpD3DDevice->EndScene  ();

	lpD3DDevice->Present(NULL,NULL,NULL,NULL);
}

[Edited by - Coder on July 28, 2005 2:39:47 PM]

Share this post


Link to post
Share on other sites
Advertisement
1) [source][/source] tags will help.

#define WIN32_LEAN_AND_MEAN
#define WIN32_EXTRA_LEAN

#include <windows.h>
#include <stdio.h>

#include <d3d9.h>
#include <d3dx9.h>

#define D3DFVF_TLVERTEX D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1

typedef struct _D3DTLVERTEX {
float sx; /* Screen coordinates */
float sy;
float sz;
float rhw; /* Reciprocal of homogeneous w */
D3DCOLOR color; /* Vertex color */
float tu; /* Texture coordinates */
float tv;
_D3DTLVERTEX() { }
_D3DTLVERTEX(const D3DVECTOR& v, float _rhw,
D3DCOLOR _color,
float _tu, float _tv)
{ sx = v.x; sy = v.y; sz = v.z; rhw = _rhw;
color = _color;
tu = _tu; tv = _tv;
}
} D3DTLVERTEX, *LPD3DTLVERTEX;


IDirect3D9* lpD3D=NULL;
IDirect3DDevice9* lpD3DDevice=NULL;
IDirect3DTexture9* lpDynamicTexture=NULL;

BOOL InitialiseWindow(HWND hWnd,HINSTANCE hThisInstance);
BOOL InitialiseDirect3D(HWND hWnd);
void ShutdownDirect3D(void);
HRESULT Blit2DTexture(float left,float top,float right,float bottom,
DWORD DiffuseColor,float z,IDirect3DTexture9* lpTex,
IDirect3DDevice9* lpDevice);

BOOL InitialiseScene(void);
void ShutdownScene(void);
void Render(void);

LRESULT CALLBACK WindowProcedure(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam)
{
switch(Msg)
{
case WM_KEYDOWN:
case WM_CLOSE:
PostQuitMessage(0);
break;
}

return DefWindowProc(hWnd,Msg,wParam,lParam);
}

BOOL InitialiseWindow(HWND hWnd,HINSTANCE hThisInstance)
{
WNDCLASSEX wc;

wc.cbSize=sizeof(WNDCLASSEX);
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.style=CS_HREDRAW|CS_VREDRAW|CS_OWNDC;
wc.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wc.hCursor=LoadCursor(NULL,IDC_CROSS);
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wc.hIconSm=wc.hIcon;
wc.hInstance=hThisInstance;
wc.lpfnWndProc=WindowProcedure;
wc.lpszClassName="dt";
wc.lpszMenuName=NULL;

if(!RegisterClassEx(&wc))
return FALSE;

hWnd=CreateWindowEx(0,"dt","Dynamic Texture",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
400,300,NULL,NULL,hThisInstance,NULL);
if(!hWnd)
return FALSE;

ShowWindow(hWnd,SW_SHOW);
UpdateWindow(hWnd);

if(!InitialiseDirect3D(hWnd))
{
ShutdownDirect3D();
return FALSE;
}

return TRUE;
}

int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
LPSTR lpCmdLine,int nShowCmd)
{
HWND hWnd=NULL;
MSG msg;

if(!InitialiseWindow(hWnd,hInstance))
return EXIT_FAILURE;

if(!InitialiseScene())
{
ShutdownScene();
ShutdownDirect3D();
return EXIT_FAILURE;
}

while(TRUE)
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if(msg.message==WM_QUIT)
break;
else
{
TranslateMessage(&msg);
DispatchMessage (&msg);
}
}
else
{
Render();
}
}

ShutdownScene();
ShutdownDirect3D();
DestroyWindow(hWnd);

return (int)msg.wParam;
}

BOOL InitialiseDirect3D(HWND hWnd)
{
D3DDISPLAYMODE d3ddm;
D3DPRESENT_PARAMETERS d3dpp;
HRESULT d3drval;

lpD3D=Direct3DCreate9(D3D_SDK_VERSION);

if(!lpD3D)
{
MessageBox(NULL,"Direct3D Init Failed\n\nDirect3DCreate9","Error",MB_OK);
return FALSE;
}

d3drval=lpD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3ddm);
if(FAILED(d3drval))
{
MessageBox(NULL,"Direct3D Init Failed\n\nIDirect3D9::GetAdapterDisplaymode","Error",MB_OK);
return FALSE;
}

ZeroMemory(&d3dpp,sizeof(d3dpp));
d3dpp.BackBufferCount=1;
d3dpp.BackBufferWidth=400;
d3dpp.BackBufferHeight=300;
d3dpp.BackBufferFormat=D3DFMT_X8R8G8B8;
d3dpp.hDeviceWindow=hWnd;
d3dpp.Windowed=TRUE;
d3dpp.EnableAutoDepthStencil=FALSE;
d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;


d3drval=lpD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,&lpD3DDevice);
if(FAILED(d3drval))
{
MessageBox(NULL,"Direct3D Init Failed\n\nIDirect3D9::CreateDevice","Error",MB_OK);
return FALSE;
}

return TRUE;
}

void ShutdownDirect3D(void)
{
if(lpD3DDevice)
lpD3DDevice->Release();

if(lpD3D)
lpD3D->Release();
}

BOOL InitialiseScene(void)
{
HRESULT d3drval;
D3DLOCKED_RECT LockedRect;

d3drval=lpD3DDevice->CreateTexture(64,64,0,D3DUSAGE_DYNAMIC,D3DFMT_X8R8G8B8,
D3DPOOL_DEFAULT,&lpDynamicTexture,NULL);
if(FAILED(d3drval))
{
MessageBox(NULL,"Init Scene fail\n\nIDirect3DDevice9::CreateTexture","Error",MB_OK);
return FALSE;
}

d3drval=lpDynamicTexture->LockRect(0,&LockedRect,NULL,D3DLOCK_DISCARD);
if(FAILED(d3drval))
{
MessageBox(NULL,"Init Scene fail\n\nIDirect3DTexture9::LockRect","Error",MB_OK);
return FALSE;
}

DWORD* dataPtr=(DWORD*)LockedRect.pBits;

for(int x=0;x<64;x++)
{
for(int y=0;y<64;y++)
{
dataPtr[(((LockedRect.Pitch/sizeof(DWORD))*y)+x)]=(DWORD)rand()%0xffffffff;
}
}

lpDynamicTexture->UnlockRect(0);

return TRUE;
}

void ShutdownScene(void)
{
if(lpDynamicTexture)
lpDynamicTexture->Release();
}

HRESULT Blit2DTexture(float left,float top,float right,float bottom,
DWORD DiffuseColor,float z,IDirect3DTexture9* lpTex,
IDirect3DDevice9* lpDevice)
{
float rhw=1.0f/(z*990.0f+10.0f);
D3DTLVERTEX VertexPtr[4];

VertexPtr[0]=D3DTLVERTEX(D3DXVECTOR3(left-0.5f,top-0.5f,z),rhw,DiffuseColor,0.0f,0.0f);
VertexPtr[1]=D3DTLVERTEX(D3DXVECTOR3(right-0.5f,top-0.5f,z),rhw,DiffuseColor,1.0f,0.0f);
VertexPtr[2]=D3DTLVERTEX(D3DXVECTOR3(right-0.5f,bottom-0.5f,z),rhw,DiffuseColor,1.0f,1.0f);
VertexPtr[3]=D3DTLVERTEX(D3DXVECTOR3(left-0.5f,bottom-0.5f,z),rhw,DiffuseColor,0.0f,1.0f);

lpDevice->SetTexture(0,lpTex);
lpDevice->SetFVF(D3DFVF_TLVERTEX);
lpDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP,2,VertexPtr,0);

return TRUE;
}

void Render(void)
{
lpD3DDevice->Clear(0,NULL,D3DCLEAR_TARGET,0x0,1.0f,0);

lpD3DDevice->BeginScene();
Blit2DTexture(0,0,64,64,0xffffffff,0.1f,lpDynamicTexture,lpD3DDevice);
lpD3DDevice->EndScene ();

lpD3DDevice->Present(NULL,NULL,NULL,NULL);
}



2) What's your actual problem? If you let us know what it's doing, or what it's not doing, that would help us figure out specifically what code to look at.

Share this post


Link to post
Share on other sites
Original post by Agony
1)

tags will help.
*** Source Snippet Removed ***

2) What's your actual problem? If you let us know what it's doing, or what it's not doing, that would help us figure out specifically what code to look at.[/quote]

Sorry, I'm still new to this site, and I haven't figured how everything works yet.

Anyway, sorry I didn't get more specific. I was trying to put random pixels into a blank texture. I was just testing to make sure I'm doing this dynamic texture thing right before actually trying to load like a .pgm file or somethin.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
for(int x=0;x<

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
For some reason my post didn't work. It might be the way you are copying colors into the LockedRect.pBits. I'm not sure you can use an array operation, and I'm not clear on the purpose of dividing by the sizeof a DWORD (I guess to figure out how many 32 bit blocks are there? But what if it isn't a 32 bit texture?)

You might want to check this thread too, if you haven't already,

http://www.gamedev.net/community/forums/topic.asp?topic_id=331855

SpaceCowboy

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!