Original Post
Hi, I have a quite trouble problem, Here is my code, I am a new user and I want to write my program step by step, I now to write a texture 3d program. Now, I start to load the texture. I have a texture called "1.bmp" in the same folder of the program, but when I run my follow program, it has problem, can anyone tell me what am I wrong?
Please help me and thx so much
#include <windowsx.h>
#include <d3d9.h>
#include <d3dx9.h>
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)
bool InitWindow(HINSTANCE);
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
HRESULT InitD3D(HWND,const char *);
HRESULT InitD3DVertex();
void Render3D();
void SetWorld();
void SetView();
void SetProject();
LPDIRECT3D9 p_D3D = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9 p_D3DD = NULL; // Our rendering device
LPDIRECT3DVERTEXBUFFER9 p_D3DVB = NULL;
LPDIRECT3DTEXTURE9 p_D3DT = NULL;
HWND hWnd;
struct MYVERTEX
{
float x, y, z;
DWORD color;
};
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
if (InitWindow(hInstance) == false)
return 0;
if (FAILED(InitD3D(hWnd, "1.bmp")))
return 0;
MSG msg;
ZeroMemory(&msg, sizeof(MSG));
while (msg.message != WM_QUIT)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
Render3D();
}
}
return 0;
}
bool InitWindow(HINSTANCE hInstance)
{
WNDCLASSEX wcx;
wcx.cbClsExtra = 0;
wcx.cbSize = sizeof(WNDCLASSEX);
wcx.cbWndExtra = 0;
wcx.hbrBackground = NULL;
wcx.hCursor = LoadCursor(NULL, IDI_APPLICATION);
wcx.hIcon = NULL;
wcx.hIconSm = NULL;
wcx.hInstance = hInstance;
wcx.lpfnWndProc = WindowProc;
wcx.lpszClassName = "Maze";
wcx.lpszMenuName = NULL;
wcx.style = CS_CLASSDC;
if (!RegisterClassEx(&wcx))
return false;
hWnd = CreateWindowEx(NULL,"Maze","Maze Game",
WS_OVERLAPPEDWINDOW,
0, 0,
800, 600,
NULL,
NULL,
hInstance,
NULL);
if (hWnd == NULL)
return false;
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
return true;
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch( uMsg )
{
case WM_DESTROY:
PostQuitMessage( 0 );
return 0;
}
return DefWindowProc( hWnd, uMsg, wParam, lParam );
}
HRESULT InitD3D(HWND hWnd, const char *szTextureFilePath)
{
if ((p_D3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
return E_FAIL;
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
if (FAILED(p_D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &p_D3DD)))
{
MessageBox(hWnd, "Device", "D3D", MB_OK);
return E_FAIL;
}
p_D3DD->SetRenderState( D3DRS_LIGHTING, false );
p_D3DD->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
p_D3DD->SetRenderState( D3DRS_ZENABLE, TRUE );
MessageBox(NULL,szTextureFilePath, "Test", MB_OK);
if( FAILED( D3DXCreateTextureFromFile( p_D3DD, szTextureFilePath, &p_D3DT ) ) )
{
MessageBox(NULL,"Test", "Test", MB_OK);
return E_FAIL;
}
MessageBox(NULL,"Test", "Test", MB_OK);
if (FAILED(InitD3DVertex()))
return E_FAIL;
return S_OK;
}
HRESULT InitD3DVertex()
{
MYVERTEX vertex[] =
{
//Bottom
{ -5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(255, 0, 0) },
{ -5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(0, 255, 0) },
{ 5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(0, 0, 255) },
{ 5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(255, 255, 255) },
{ 0.0f, 5.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255) },
{ -5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(255, 0, 0) },
{ 5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(0, 255, 0) },
{ 5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(0, 0, 255) },
{ -5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(0, 0, 255) },
//n¥hµf²Ä¤@ÓÂI
{ -5.0f, -5.0f, -5.0f, D3DCOLOR_XRGB(255, 255, 0) },
// { -5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(255, 0, 0) },
// { -5.0f, 5.0f, 5.0f, D3DCOLOR_XRGB(0, 255, 0) },
// { 5.0f, -5.0f, 5.0f, D3DCOLOR_XRGB(0, 0, 255) },
// { 5.0f, 5.0f, 5.0f, D3DCOLOR_XRGB(255, 255, 255) },
};
void *p_Vertex;
if (FAILED(p_D3DD->CreateVertexBuffer(10*sizeof(vertex),
0,
D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT,
&p_D3DVB,
NULL)))
return E_FAIL;
if (FAILED(p_D3DVB->Lock(0, sizeof(vertex), (void**)&p_Vertex, 0 )))
return E_FAIL;
memcpy(p_Vertex, vertex, sizeof(vertex));
p_D3DVB->Unlock();
return S_OK;
}
void Render3D()
{
p_D3DD->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0L);
if (SUCCEEDED(p_D3DD->BeginScene()))
{
SetWorld();
SetView();
SetProject();
// p_D3DD->SetTexture(0, p_D3DT);
p_D3DD->SetStreamSource(0, p_D3DVB, 0, sizeof(MYVERTEX));
p_D3DD->SetFVF(D3DFVF_CUSTOMVERTEX);
p_D3DD->DrawPrimitive(D3DPT_TRIANGLESTRIP,0, 2);
p_D3DD->DrawPrimitive(D3DPT_TRIANGLEFAN, 4, 4);
p_D3DD->EndScene();
// p_D3DD->SetTexture(0, NULL);
}
p_D3DD->Present( NULL, NULL, NULL, NULL);
}
void SetWorld()
{
D3DXMATRIX maxWorld;
D3DXMATRIX maxWorldPos;
D3DXMATRIX maxWorldX;
D3DXMATRIX maxWorldY;
D3DXMATRIX maxWorldZ;
D3DXMatrixIdentity(&maxWorld);
//D3DXMatrixTranslation(&maxWorldPos, 0.0f, 0.0f, 10.0f);
//D3DXMatrixRotationX(&maxWorldX, timeGetTime()/500.0f);
D3DXMatrixRotationX(&maxWorldX, timeGetTime()/1000.0f);
//D3DXMatrixRotationZ(&maxWorldZ, timeGetTime()/500.0f);
//D3DXMatrixMultiply(&maxWorld, &maxWorldY, &maxWorldX);
D3DXMatrixMultiply(&maxWorld, &maxWorld, &maxWorldX);
p_D3DD->SetTransform(D3DTS_WORLD, &maxWorld);
}
void SetView()
{
D3DXMATRIX maxView;
D3DXMatrixLookAtLH(&maxView,
&D3DXVECTOR3(0.0f, 0.0f, -50.0f),
&D3DXVECTOR3(0.0f, 0.0f, 0.0f),
&D3DXVECTOR3(0.0F, 1.0f, 0.0f));
p_D3DD->SetTransform(D3DTS_VIEW, &maxView);
}
void SetProject()
{
D3DXMATRIX maxProjection;
D3DXMatrixPerspectiveFovLH(&maxProjection,
D3DX_PI / 4,
4.0f / 3.0f, 1.0f, 500.0f);
p_D3DD->SetTransform(D3DTS_PROJECTION, &maxProjection);
}