D3DLIGHT Quick help

Started by
7 comments, last by xM1k3x 15 years, 8 months ago
Hey all im just messing around with what I am learning in class about direct3d light, particularly Point light. Anyways I have some code that creates a cube primitive and a point light. I basically just want to allow the user to move the light left and right over the object. Well it doesnt appear to be moving so perhaps someone can see whats wrong with my code. Here is the code for the movement:

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;
		 case WM_KEYDOWN:
			 if(wp == VK_LEFT) pos.x -= 1.0f ;
			 if(wp == VK_RIGHT) pos.x += 1.0f;
			 break;
      }

   return DefWindowProc(hWnd, msg, wp, lp);
}



Here is the code for the light

bool InitializeObjects()
{
   pos.x = 0.0f;
   pos.y = 0.0f;
   pos.z = 1.0f;

   // 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_POINT;
   g_light.Direction = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
   g_light.Diffuse.r = g_light.Diffuse.g = 1.0f;
   g_light.Diffuse.b = 1.0f;
   g_light.Specular.r = g_light.Specular.g = 1;
   g_light.Specular.b = g_light.Specular.a = 1;
   
   g_light.Position = pos;

   g_light.Attenuation0 = 0.1f;
   g_light.Range = 300.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(D3DXCreateBox(g_D3DDevice, 2, 2, 2, &g_cube, 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;
}

Any help is greatly appreciated thanks for your time! Mike
Advertisement
is pos.x in scope for the message proc? also, is the pos struct within anoth struct?
Quote:Original post by Googol PL3X
is pos.x in scope for the message proc? also, is the pos struct within anoth struct?


Oh im really sorry, pos is a global variable:

D3DVECTOR pos;

Its a Direct 3D vector.
I'm pretty sure D3D makes a copy of the D3DLIGHT structure that you pass into SetLight(), so just changing pos afterwards won't affect the internal light.

I think after you have modified pos, you need to call SetLight() again with the D3DLIGHT structure's Position member updated.
Quote:Original post by EasilyConfused
I'm pretty sure D3D makes a copy of the D3DLIGHT structure that you pass into SetLight(), so just changing pos afterwards won't affect the internal light.

I think after you have modified pos, you need to call SetLight() again with the D3DLIGHT structure's Position member updated.



I changed my movement function to this:

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;		 case WM_KEYDOWN:			 if(wp == VK_LEFT)			 {				 pos.x -= 1.0f ;				 g_D3DDevice->SetLight(0, &g_light);			 }			 if(wp == VK_RIGHT)			 {				 pos.x += 1.0f;				 g_D3DDevice->SetLight(0, &g_light);			 }			 break;      }   return DefWindowProc(hWnd, msg, wp, lp);}



Is this what you meant? Because it doesnt work maybe im not understanding what you mean.
You need to reassign pos to the Position member of g_light before you call SetLight().
Quote:Original post by EasilyConfused
You need to reassign pos to the Position member of g_light before you call SetLight().


Ok I gotcha now it works.

However now it looks kinda choppy can someone look over my light settings?

bool InitializeObjects(){   pos.x = 0.0f;   pos.y = 0.0f;   pos.z = 1.0f;   // 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_POINT;   g_light.Direction = D3DXVECTOR3(0.0f, 0.0f, 1.0f);   g_light.Diffuse.r = g_light.Diffuse.g = 1.0f;   g_light.Diffuse.b = 1.0f;   g_light.Specular.r = g_light.Specular.g = 1;   g_light.Specular.b = g_light.Specular.a = 1;      g_light.Position = pos;   g_light.Attenuation0 = 0.1f;   g_light.Range = 300.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(D3DXCreateBox(g_D3DDevice, 2, 2, 2, &g_cube, 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;}



Any reason why I dont see the light bulb i just get the reflection of light on the primitive it seems to be directly above the primitive anyway I can make the light travel in fron of the primitive from left to right when the buttons are pressed?
Quote:Original post by xM1k3x
Quote:Original post by EasilyConfused
You need to reassign pos to the Position member of g_light before you call SetLight().


Ok I gotcha now it works.

However now it looks kinda choppy can someone look over my light settings?

*** Source Snippet Removed ***


Any reason why I dont see the light bulb i just get the reflection of light on the primitive it seems to be directly above the primitive anyway I can make the light travel in fron of the primitive from left to right when the buttons are pressed?



I just realised its not really a light bulb im saying where the light source is coming from is there a way I can make it look like it is coming from the users direction you know what I mean shining directly in.
Anyone have an idea of how to solve that part of my problem? See above post.

This topic is closed to new replies.

Advertisement