Moving lights.

Started by
0 comments, last by jpetrie 15 years, 3 months ago
I'm trying to move my light in a constant X axis direction but my current code only executes once but I want it to move along the X axis more then once. This is the code I currently have

void init_light(void)
{
    D3DLIGHT9 light;    // create the light struct
    D3DMATERIAL9 material;    // create the material struct

    ZeroMemory(&light, sizeof(light));    // clear out the struct for use
    light.Type = D3DLIGHT_POINT;    // make the light type 'point light'
    light.Diffuse.r = 0.5f;    // .5 red
    light.Diffuse.g = 0.5f;    // .5 green
    light.Diffuse.b = 0.5f;    // .5 blue
    light.Diffuse.a = 1.0f;    // full alpha (we'll get to that soon)
    light.Range = 100.0f;    // a range of 100
    light.Attenuation0 = 0.0f;    // no constant inverse attenuation
    light.Attenuation1 = 0.125f;    // only .125 inverse attenuation
    light.Attenuation2 = 0.0f;    // no square inverse attenuation

	static float index = 0.0f; index+=0.05f;
	
	D3DVECTOR vec2Position = {0.0f, 5.0f, 8.0f};
    D3DVECTOR vecPosition = {index, 0.0f, 0.0f};    // the position of the light
    light.Position = vecPosition;    // set the position

    d3ddev->SetLight(0, &light);    // send the light struct properties to light #0
    d3ddev->LightEnable(0, TRUE);    // turn on light #0

    ZeroMemory(&material, sizeof(D3DMATERIAL9));    // clear out the struct for use
    material.Diffuse.r = material.Ambient.r = 1.0f;    // set the material to full red
    material.Diffuse.g = material.Ambient.g = 1.0f;    // set the material to full green
    material.Diffuse.b = material.Ambient.b = 1.0f;    // set the material to full blue
    material.Diffuse.a = material.Ambient.a = 1.0f;    // set the material to full alpha

    d3ddev->SetMaterial(&material);    // set the globably-used material to &material

    return;
}
I want to take static float index = 0.0f; index+=0.05f; and put it in my render_frame function but at the same time I want init_light() function to be able to "see" the index variable in the render_frame() function. How would I do that? Thanks!
Advertisement
The naive, trivial (and poor) solution is to make the variable global.

A better solution is to encapsulate the properties of lights into a structure, and pass that structure around to the init_light and rendering functions as appropriate, and perhaps storing a list of these structures in an appropriate place (such as the object representing your scene, if you have such a thing).

Since you are using C++ (you're calling member functions) you can improve this even further, replacing init_light entirely with a constructor for the light properties structure.

Then the light object constructor initializes all your lighting properties and, if necessary, underlying D3D9 objects. In your render update, you move all the appropriate lights stored in your scene object by incrementing their X position or whatever. Then you render using the information contained in those light objects.

This topic is closed to new replies.

Advertisement