   Simple Lighting ULTRAMALFUNCTION
  Hi, I'm trying to make simple lighting in DirectX, and I'm having major issues.  I had it working yesterday, but when implementing it into a light class, it failed miserably, and now I can't even return it to its original state!!  I'm just getting a black screen here.  I'm working on a DX9 engine, so it's in two files.  main.cpp :#include <engine.h>int Main(){Graphics3D(WINDOW_WIDTH, WINDOW_HEIGHT, false);while (_GraphicsQuit_ != true){UpdateEngine();RenderWorld();}ShutDownEngine();return 0;}and engine.h  (THE REAL TROUBLE, I THINK, IS IN InitalizeObjects())#include<d3d9.h>#include<d3dx9.h>#pragma comment(lib, "d3d9.lib")#pragma comment(lib, "d3dx9.lib")#define WINDOW_CLASS    "TEST"#define WINDOW_NAME"test"#define WINDOW_WIDTH    640#define WINDOW_HEIGHT   480// Function Prototypes...int Main();int WINAPI WinMain(HINSTANCE hInst, HINSTANCE ph, LPSTR cmd, int s);bool InitializeD3D(HWND hWnd, bool fullscreen);bool InitializeObjects();void RenderWorld();void Shutdown();WNDCLASSEX _CreateWNDCLASSEX_();HWND _CreateWindowClass_(int width, int height, WNDCLASSEX & wc);bool Graphics3D(int gWidth, int gHeight, bool FullScreen);void ShutDownEngine();void UpdateEngine();//Window classWNDCLASSEX wc = _CreateWNDCLASSEX_();//Window message classMSG msg;//Quit variablebool _GraphicsQuit_;// Direct3D object and device.LPDIRECT3D9 g_D3D = NULL;LPDIRECT3DDEVICE9 g_D3DDevice = NULL;// Matrices.D3DXMATRIX g_projection;D3DXMATRIX g_ViewMatrix;D3DXMATRIX g_WorldMatrix;// Mesh objectsLPD3DXMESH g_teapot = NULL;LPD3DXMESH g_cube = NULL;LPD3DXMESH g_sphere = NULL;LPD3DXMESH g_torus = NULL;D3DMATERIAL9 g_material;// Scene light source.D3DLIGHT9 g_light;//Light index numberint _lIndex_ = 0;LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp){   switch(msg) {    case WM_DESTROY:  PostQuitMessage(0);  return 0;  break;    case WM_KEYUP:  if(wp == VK_ESCAPE) PostQuitMessage(0);  break; }   return DefWindowProc(hWnd, msg, wp, lp);}int WINAPI WinMain(HINSTANCE hInst, HINSTANCE ph, LPSTR cmd, int s){Main();}bool InitializeD3D(HWND hWnd, bool fullscreen){   D3DDISPLAYMODE displayMode;   // Create the D3D object.   g_D3D = Direct3DCreate9(D3D_SDK_VERSION);   if(g_D3D == NULL) return false;   // Get the desktop display mode.   if(FAILED(g_D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode))) return false;   // Set up the structure used to create the D3DDevice   D3DPRESENT_PARAMETERS d3dpp;   ZeroMemory(&d3dpp, sizeof(d3dpp));   if(fullscreen) {    d3dpp.Windowed = FALSE;    d3dpp.BackBufferWidth = WINDOW_WIDTH;    d3dpp.BackBufferHeight = WINDOW_HEIGHT; }   else d3dpp.Windowed = TRUE;   d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;   d3dpp.BackBufferFormat = displayMode.Format;   // Create the D3DDevice   if(FAILED(g_D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_D3DDevice))) return false;   // Set the projection matrix.   D3DXMatrixPerspectiveFovLH(&g_projection, 45.0f, WINDOW_WIDTH/WINDOW_HEIGHT, 0.1f, 1000.0f);   g_D3DDevice->SetTransform(D3DTS_PROJECTION, &g_projection);   // Initialize any objects we will be displaying.   if(!InitializeObjects()) return false;   return true;}WNDCLASSEX _CreateWNDCLASSEX_(){// Register the window class    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,    GetModuleHandle(NULL), NULL, NULL, NULL, NULL,    WINDOW_CLASS, NULL };RegisterClassEx(&wc);return wc;}HWND _CreateWindowClass_(int width, int height, WNDCLASSEX & wc){HWND hWnd = CreateWindow(WINDOW_CLASS, WINDOW_NAME,WS_OVERLAPPEDWINDOW, 100, 100, width, height,GetDesktopWindow(), NULL, wc.hInstance, NULL);return hWnd;}bool Graphics3D(int gWidth, int gHeight, bool FullScreen){wc = _CreateWNDCLASSEX_();HWND hWnd = _CreateWindowClass_(gWidth, gHeight, wc);InitializeD3D(hWnd, FullScreen);ZeroMemory(&msg, sizeof(msg));// Show the window    ShowWindow(hWnd, SW_SHOWDEFAULT);    UpdateWindow(hWnd);return true;}bool InitializeObjects(){// Set default rendering states.   g_D3DDevice->SetRenderState(D3DRS_LIGHTING, TRUE);   g_D3DDevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_COLORVALUE(0.3f, 0.3f, 0.3f, 1.0f));   // Setup the light source.   g_light.Type = D3DLIGHT_DIRECTIONAL;   g_light.Direction = D3DXVECTOR3(0.0f, 0.0f, 1.0f);   g_light.Diffuse.r = g_light.Diffuse.g = 1;   g_light.Diffuse.b = g_light.Diffuse.a = 1;   g_light.Specular.r = g_light.Specular.g = 1;   g_light.Specular.b = g_light.Specular.a = 1;   g_light.Position = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );   // Register the light.   g_D3DDevice->SetLight(0, &g_light);   g_D3DDevice->LightEnable(0, TRUE);   // Setup the material properties for the teapot.   ZeroMemory(&g_material, sizeof(D3DMATERIAL9));   g_material.Diffuse.r = g_material.Ambient.r = 0.6f;   g_material.Diffuse.g = g_material.Ambient.g = 0.6f;   g_material.Diffuse.b = g_material.Ambient.b = 0.7f;   g_material.Specular.r = 0.4f;   g_material.Specular.g = 0.4f;   g_material.Specular.b = 0.4f;   g_material.Power = 8.0f;   // Create the objects.   if(FAILED(D3DXCreateTeapot(g_D3DDevice, &g_teapot, NULL))) return false;   if(FAILED(D3DXCreateBox(g_D3DDevice, 2, 2, 2, &g_cube, NULL))) return false;   if(FAILED(D3DXCreateSphere(g_D3DDevice, 1.5, 25, 25, &g_sphere, NULL))) return false;   if(FAILED(D3DXCreateTorus(g_D3DDevice, 0.5f, 1.2f, 25, 25, &g_torus, NULL))) return false;   // Define camera information.   D3DXVECTOR3 cameraPos(0.0f, 0.0f, -8.0f);   D3DXVECTOR3 lookAtPos(0.0f, 0.0f, 0.0f);   D3DXVECTOR3 upDir(0.0f, 1.0f, 0.0f);   // Build view matrix.   D3DXMatrixLookAtLH(&g_ViewMatrix, &cameraPos, &lookAtPos, &upDir);   return true;}void UpdateEngine(){if(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE)){TranslateMessage(&msg);   DispatchMessage(&msg);    }if (msg.message == WM_QUIT) _GraphicsQuit_ = true;}void RenderWorld(){   // Clear the backbuffer.   g_D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET,  D3DCOLOR_XRGB(0,0,0), 1.0f, 0);   // Begin the scene.  Start rendering.   g_D3DDevice->BeginScene(); // Apply the view (camera). g_D3DDevice->SetTransform(D3DTS_VIEW, &g_ViewMatrix); // Draw teapot. D3DXMatrixTranslation(&g_WorldMatrix, 2.0f, -2.0, 0.0f); g_D3DDevice->SetTransform(D3DTS_WORLD, &g_WorldMatrix); g_D3DDevice->SetMaterial(&g_material); g_teapot->DrawSubset(0); // Draw Cube. D3DXMatrixTranslation(&g_WorldMatrix, -2.0f, -2.0, 0.0f); g_D3DDevice->SetTransform(D3DTS_WORLD, &g_WorldMatrix); g_D3DDevice->SetMaterial(&g_material); g_cube->DrawSubset(0); // Draw Sphere. D3DXMatrixTranslation(&g_WorldMatrix, 2.0f, 2.0, 0.0f); g_D3DDevice->SetTransform(D3DTS_WORLD, &g_WorldMatrix); g_D3DDevice->SetMaterial(&g_material); g_sphere->DrawSubset(0); // Draw Torus. D3DXMatrixTranslation(&g_WorldMatrix, -2.0f, 2.0, 0.0f); g_D3DDevice->SetTransform(D3DTS_WORLD, &g_WorldMatrix); g_D3DDevice->SetMaterial(&g_material); g_torus->DrawSubset(0);   // End the scene.  Stop rendering.   g_D3DDevice->EndScene();   // Display the scene.   g_D3DDevice->Present(NULL, NULL, NULL, NULL);}void ShutDown(){   // Release all resources.   if(g_D3DDevice != NULL) g_D3DDevice->Release();   if(g_D3D != NULL) g_D3D->Release();   if(g_teapot != NULL) { g_teapot->Release(); g_teapot = NULL; }   if(g_cube != NULL) { g_cube->Release(); g_cube = NULL; }   if(g_sphere != NULL) { g_sphere->Release(); g_sphere = NULL; }   if(g_torus != NULL) { g_torus->Release(); g_torus = NULL; }   g_D3DDevice = NULL;   g_D3D = NULL;}void ShutDownEngine(){ShutDown();UnregisterClass(WINDOW_CLASS, wc.hInstance);}Also, I want to get this to comply with a light class, So that by writing LIGHT mylight(1) will create a point light of default values, but even when I had it working, this wouldn't work.  (And yes, I know that the spotlight option is incomplete, but directional and point don't work either)class entity{public:entity(){}virtual ~entity(){}virtual void PositionEntity() = 0;virtual void RotateEntity() = 0;virtual void MoveEntity() = 0;virtual void TranslateEntity() = 0;virtual void TurnEntity() = 0;protected:float itsX;float itsY;float itsZ;float itsPitch;float itsYaw;float itsRoll;};class LIGHT : public entity{public:D3DLIGHT9 e_light;LIGHT(int type):itsType(type){g_D3DDevice->SetRenderState(D3DRS_LIGHTING, TRUE);D3DLIGHT9 e_light;//Set parameters based on the kind of lightswitch (type){case 1:e_light.Type = D3DLIGHT_POINT;e_light.Attenuation0 = 0;e_light.Attenuation1 = 0.5f;e_light.Attenuation2 = 0;e_light.Range = 1000;case 2:e_light.Type = D3DLIGHT_SPOT;case 3:e_light.Type = D3DLIGHT_DIRECTIONAL;e_light.Direction = D3DXVECTOR3(0.0f, 0.0f, 1.0f);default:e_light.Type = D3DLIGHT_POINT;e_light.Attenuation0 = 0;e_light.Attenuation1 = 0.5f;e_light.Attenuation2 = 0;e_light.Range = 10;}e_light.Diffuse.r = e_light.Diffuse.g = 1;e_light.Diffuse.b = e_light.Diffuse.a = 1;e_light.Specular.r = e_light.Specular.g = 1;e_light.Specular.b = e_light.Specular.a = 1;e_light.Ambient.r = e_light.Ambient.g = 1;e_light.Ambient.b = e_light.Ambient.a = 1;itsIndex = _lIndex_;//Register the lightg_D3DDevice->SetLight(0, &e_light);g_D3DDevice->LightEnable(0, TRUE);//increment the next light index number_lIndex_++;}~LIGHT(){}//light property functions commandsvoid SetAmbient(float r, float g, float b, float a){ambientR = r;ambientG = g;ambientB = b;ambientA = a;UpdateLightProperties();}void SetDiffuse(float r, float g, float b, float a){diffuseR = r;diffuseG = g;diffuseB = b;diffuseA = a;UpdateLightProperties();}void SetSpecular(float r, float g, float b, float a){specularR = r;specularG = g;specularB = b;specularA = a;UpdateLightProperties();}void SetRange(float r) {range = r;UpdateLightProperties();}void SetFallof(float f) {falloff = f;UpdateLightProperties();}void SetAttenuation(float a0, float a1, float a2){attenuation0 = a0;attenuation1 = a1;attenuation2 = a2;UpdateLightProperties();}void SetCone(float th, float ph){theta = th;phi = ph;UpdateLightProperties();}//translation functionsvoid PositionEntity(){}void RotateEntity(){}void MoveEntity(){}void TranslateEntity(){}void TurnEntity(){}private:void UpdateLightProperties(){e_light.Ambient.a = ambientA;e_light.Ambient.r = ambientR;e_light.Ambient.g = ambientG;e_light.Ambient.b = ambientB;e_light.Diffuse.a = diffuseA;e_light.Diffuse.r = diffuseR;e_light.Diffuse.g = diffuseG;e_light.Diffuse.b = diffuseB;e_light.Specular.a = specularA;e_light.Specular.r = specularR;e_light.Specular.g = specularG;e_light.Specular.b = specularB;e_light.Range = range;e_light.Falloff = falloff;e_light.Attenuation0 = attenuation0;e_light.Attenuation1 = attenuation1;e_light.Attenuation2 = attenuation2;e_light.Theta = theta;e_light.Phi = phi;}int itsType;float ambientR, ambientG, ambientB, ambientA;float diffuseR, diffuseG, diffuseB, diffuseA;float specularR, specularG, specularB, specularA;float range;float falloff;float attenuation0, attenuation1, attenuation2;float theta, phi;int itsIndex;};I've been at this for days and any time I make the least bit of progress, my next step undoes it all, so as you can imagine, any help is HUGELY appreciated!![Edited by - Chwaga on June 18, 2008 3:10:37 PM]
  It may not be the source of your problem but you have no 'break;' statements in your "switch(type)" code.;  thanks for pointing that out, it wasnt being included so i didn't get the error, but it still doesn't explain any of the lighting problems.   ;  hmmm...strangly enough, I got it working again and I didn't change anything...I must be being mean to my computer, anyways, I still can't get it into the LIGHT class...   ;  Maybe now you'll appreciate the presence of an SCM tool in your devkit [wink]Also, what debugging and analysis have you done? Any step-through debugging? unit tests? automation tests? reference rasterizer? debug runtimes? PIX? NVPerfHUD? HRESULT checking? If any of this is alien to you then practice your search-fu and do some research. Richard Fine's article on debugging hosted on this very site is a good start.hthJack   