Wazup guys,
In the following code I can't fingure out why the lights do not work correctly and so I require your help --->
#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
#define ScreenW 640
#define ScreenH 480
#define CustomFVF (D3DFVF_XYZ | D3DFVF_TEX1 | D3DFVF_NORMAL)
const char *ClsName = "BasicApp";
const char *WndName = "DirectX";
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
VOID InitD3D();
VOID InitGraphics();
VOID InitLights();
VOID Display();
VOID CleanProgram();
struct CustomV
{
FLOAT x, y, z;
FLOAT u, v;
D3DVECTOR vnormal;
};
HINSTANCE hInstance;
HWND hWnd;
DWORD StartingCount;
D3DPRESENT_PARAMETERS D3DPP;
D3DLIGHT9 Light;
D3DMATERIAL9 LightM;
LPDIRECT3D9 D3D;
LPDIRECT3DDEVICE9 D3DDev;
LPDIRECT3DVERTEXBUFFER9 PrimitiveB;
LPDIRECT3DTEXTURE9 TexA;
D3DXMATRIX ViewM, ProjectionM;
VOID* VertMemLoc;
CustomV Primitive[] =
{
{-3.0f, 3.0f, 1.0f, 0, 0, 0.0f, 1.0f, 0.0f, },
{3.0f, 3.0f, 1.0f, 1, 0, 0.0f, 1.0f, 0.0f, },
{-3.0f, -3.0f, 1.0f, 0, 1, 0.0f, 1.0f, 0.0f, },
{3.0f, -3.0f, 1.0f, 1, 1, 0.0f, 1.0f, 0.0f, },
};
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG Msg;
WNDCLASSEX WndClsEx;
// Create the application window
WndClsEx.cbSize = sizeof(WNDCLASSEX);
WndClsEx.style = CS_HREDRAW | CS_VREDRAW;
WndClsEx.lpfnWndProc = WndProc;
WndClsEx.cbClsExtra = 0;
WndClsEx.cbWndExtra = 0;
WndClsEx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClsEx.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
WndClsEx.lpszMenuName = NULL;
WndClsEx.lpszClassName = ClsName;
WndClsEx.hInstance = hInstance;
WndClsEx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
// Register the application
RegisterClassEx(&WndClsEx);
// Create the window object
hWnd = CreateWindowEx(WS_EX_TOPMOST,
ClsName,
WndName,
WS_POPUP,
0,
0,
ScreenW,
ScreenH,
NULL,
NULL,
hInstance,
NULL);
// Find out if the window was created
if( !hWnd ) // If the window was not created,
return 0; // stop the application
// Display the window to the user
ShowWindow(hWnd, SW_SHOWNORMAL);
UpdateWindow(hWnd);
InitD3D();
// Decode and treat the messages
// as long as the application is running
while(true)
{
StartingCount = GetTickCount();
if (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
{
if (Msg.message == WM_QUIT) break;
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
Display();
while((GetTickCount() - StartingCount) < 25);
}
CleanProgram();
return Msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch (Msg)
{
case WM_DESTROY :
PostQuitMessage(WM_QUIT);
break;
case WM_CLOSE :
DestroyWindow(hWnd);
break;
case WM_KEYUP :
switch(wParam)
{
case VK_ESCAPE :
DestroyWindow(hWnd);
break;
}
break;
}
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
VOID InitD3D()
{
D3D = Direct3DCreate9(D3D_SDK_VERSION);
ZeroMemory(&D3DPP, sizeof(D3DPP));
D3DPP.SwapEffect = D3DSWAPEFFECT_DISCARD;
D3DPP.hDeviceWindow = hWnd;
D3DPP.Windowed = false;
D3DPP.BackBufferWidth = ScreenW;
D3DPP.BackBufferHeight = ScreenH;
D3DPP.BackBufferFormat = D3DFMT_X8R8G8B8;
D3DPP.EnableAutoDepthStencil = true;
D3DPP.AutoDepthStencilFormat = D3DFMT_D16;
D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &D3DPP, &D3DDev);
InitGraphics();
InitLights();
}
VOID InitGraphics()
{
D3DDev->CreateVertexBuffer(4 * sizeof(CustomV), NULL, CustomFVF, D3DPOOL_MANAGED, &PrimitiveB, NULL);
PrimitiveB->Lock(NULL, NULL, &VertMemLoc, NULL);
memcpy(VertMemLoc, Primitive, sizeof(Primitive));
PrimitiveB->Unlock();
D3DDev->SetRenderState(D3DRS_LIGHTING, true);
D3DDev->SetRenderState(D3DRS_ZENABLE, true);
D3DDev->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(50, 50, 50));
D3DXCreateTextureFromFile(D3DDev, "C:\\Tile.bmp", &TexA);
}
VOID InitLights()
{
D3DVECTOR LightDir = {0.0f, 0.0f, 0.0f};
ZeroMemory(&Light, sizeof(Light));
ZeroMemory(&LightM, sizeof(LightM));
Light.Type = D3DLIGHT_DIRECTIONAL;
Light.Diffuse.r = 0.5f;
Light.Diffuse.g = 0.5f;
Light.Diffuse.b = 0.5f;
Light.Diffuse.a = 1.0f;
Light.Direction = LightDir;
LightM.Diffuse.r = LightM.Ambient.r = 1.0f;
LightM.Diffuse.g = LightM.Ambient.g = 1.0f;
LightM.Diffuse.b = LightM.Ambient.b = 1.0f;
LightM.Diffuse.a = LightM.Ambient.a = 1.0f;
D3DDev->SetLight(0, &Light);
D3DDev->SetMaterial(&LightM);
D3DDev->LightEnable(0, true);
}
VOID Display()
{
D3DXMatrixLookAtLH(&ViewM, &D3DXVECTOR3(0.0f, 0.0f, -20.0f), &D3DXVECTOR3(0.0f, 0.0f, 0.0f), &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
D3DXMatrixPerspectiveFovLH(&ProjectionM, D3DXToRadian(45), (FLOAT)ScreenW / (FLOAT)ScreenH, 1.0f, 100.0f);
D3DDev->Clear(NULL, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, NULL);
D3DDev->BeginScene();
D3DDev->SetFVF(CustomFVF);
D3DDev->SetStreamSource(NULL, PrimitiveB, NULL, sizeof(CustomV));
D3DDev->SetTransform(D3DTS_VIEW, &ViewM);
D3DDev->SetTransform(D3DTS_PROJECTION, &ProjectionM);
D3DDev->SetTexture(NULL, TexA);
D3DDev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
D3DDev->EndScene();
D3DDev->Present(NULL, NULL, NULL, NULL);
}
VOID CleanProgram()
{
D3D->Release();
D3DDev->Release();
PrimitiveB->Release();
TexA->Release();
}
The vertex normals are probably badly set but that is not the main problem as far as I know.
Thanks.