User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Game Development section within the Software Development category of DaniWeb, a massive community of 375,201 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,254 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Game Development advertiser:
Views: 409 | Replies: 1
Reply
Join Date: May 2008
Posts: 3
Reputation: pcbrainbuster is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
pcbrainbuster pcbrainbuster is offline Offline
Newbie Poster

A DX9 Lighting Issue -->

  #1  
May 12th, 2008
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2008
Posts: 4
Reputation: i_Robot is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
i_Robot i_Robot is offline Offline
Newbie Poster

Re: A DX9 Lighting Issue -->

  #2  
May 31st, 2008
You need to set some material on you geometry(triiangles). When you use fixed function pipeline you must set material.
Create D3DMATERIAL and call setmaterial
before you render the geometry
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Game Development Marketplace
Thread Tools Display Modes

Other Threads in the Game Development Forum

All times are GMT -4. The time now is 2:38 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC