Archived

This topic is now archived and is closed to further replies.

Shadow1234567890

How do I get a moving light?

Recommended Posts

I am trying to set up the light properties using these arrays: GLfloat yrot; float AmbientLight[] = {.25f, .25f, .25f, 1.0f}; float AmbientPos[] = {yrot, 0.0f, -2.0f, 1.0f}; This is just an example program for me to figure out how to get moving lights. For some reason this does not do the trick. I have also tried turning off the lights and re initializing them with the updated variables by recalling gllightfv and setting up the lights parameters again. Both of these methods have seem to have failed. Was I at least on the right track? [edited by - Shadow1234567890 on November 9, 2002 1:05:25 PM]

Share this post


Link to post
Share on other sites
Have you updated light position after rotation (in render function)?

Anyway, here''s an example of rotating light:

(controls: 1, 2, 3 = red/green/blue light, r = toggle cube rotation, f = change view)

//////////////////////////
// ambient and diffuse light
float ambient_light [] = {0.4f, 0.4f, 0.4f, 1.0f};
float diffuse_light [] = {11.0f, 0.0f, 0.0f, 1.0f};
float light0_pos [] = {0.0f, 0.0f, -50.0f, 1.0f};

// spotlight
float spotlight_dir [] = {0.0f, 0.0f, -200.0f};

void SetLighting ()
{
glEnable (GL_LIGHTING);

// materials
glEnable (GL_COLOR_MATERIAL);
glColorMaterial (GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glColorMaterial (GL_FRONT, GL_SPECULAR);
glMaterialf (GL_FRONT, GL_SHININESS, 100.0f);

// lights
glLightModelfv (GL_LIGHT_MODEL_AMBIENT, ambient_light);
glLightModeli (GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
glLightModeli (GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);

glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse_light);
glLightfv (GL_LIGHT0, GL_POSITION, light0_pos);

glEnable (GL_LIGHT0);
}



float angle1 = 0.0f;
float angle2 = 0.0f;

bool far_view = false;
bool rotate_cube = true;
int light_color = 1; // 1 = red, 2 = green, 3 = blue

void RenderGLScene ()
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity ();

if (far_view)
{
gluLookAt (0.0f, 400.0f, 200.0f, 0.0f, 0.0f, -200.0f, 0.0f, 1.0f, 0.0f);
}

glPushMatrix ();
glDisable (GL_LIGHTING);
glRotatef (angle2, 0.0f, 1.0f, 0.0f);
glRotatef (angle2, 1.0f, 0.0f, 0.0f);
glTranslatef (light0_pos [0], light0_pos [1], light0_pos[2] - 200.0f);

if (light_color == 1) { glColor3f (1.0f, 0.0f, 0.0f); }
if (light_color == 2) { glColor3f (0.0f, 1.0f, 0.0f); }
if (light_color == 3) { glColor3f (0.0f, 0.0f, 1.0f); }
auxSolidSphere (5.0f);
glLightfv (GL_LIGHT0, GL_POSITION, light0_pos);
glEnable (GL_LIGHTING);
glPopMatrix ();

glPushMatrix ();

glTranslatef (0.0f, 0.0f, -150.0f);
glRotatef (angle1, 1.0f, -1.0f, -1.0f);
glScalef (25.0f, 25.0f, 25.0f);

glColor3f (1.0f, 1.0f, 1.0f);

auxSolidCube (2.0f);

glPopMatrix ();

if (rotate_cube)
{
angle1 += 0.4f;
if (angle1 >= 360.0f) { angle1 = 0.0f; }
}

angle2 += 0.5f;
if (angle2 >= 360.0f) { angle2 = 0.0f; }


//// input ////
if (keys [''F''])
{
far_view = !far_view;
keys [''F''] = FALSE;
}

if (keys [''R''])
{
rotate_cube = !rotate_cube;
keys [''R''] = FALSE;
}

if (keys [''1''])
{
light_color = 1;
diffuse_light [0] = 11.0f;
diffuse_light [1] = 0.0f;
diffuse_light [2] = 0.0f;
keys [''1''] = FALSE;
}

if (keys [''2''])
{
light_color = 2;
diffuse_light [1] = 11.0f;
diffuse_light [0] = 0.0f;
diffuse_light [2] = 0.0f;
keys [''2''] = FALSE;
}

if (keys [''3''])
{
light_color = 3;
diffuse_light [2] = 11.0f;
diffuse_light [0] = 0.0f;
diffuse_light [1] = 0.0f;
keys [''3''] = FALSE;
}
glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse_light);
}
//////////////////

Share this post


Link to post
Share on other sites