Sign in to follow this  
xM1k3x

D3DLIGHT Quick help

Recommended Posts

xM1k3x    122
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

Share this post


Link to post
Share on other sites
xM1k3x    122
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.

Share this post


Link to post
Share on other sites
Aardvajk    13207
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.

Share this post


Link to post
Share on other sites
xM1k3x    122
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.

Share this post


Link to post
Share on other sites
xM1k3x    122
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?

Share this post


Link to post
Share on other sites
xM1k3x    122
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this