Moving Light

Hello :slight_smile:

I just started programming a little bit in OpenGL.
I defined a light:


GLfloat light0Position[] = {xLight1, 20.0, 20.0, 20.0}; // Position
		glLightfv(GL_LIGHT1, GL_POSITION, light0Position);
		GLfloat ambient0[]={0.2, 0.2, 0.2, 1.0}; // Farbdefinitionen
		GLfloat diffuse0[]={0.9, 0.9, 0.9, 1.0};
		GLfloat specular0[]={1.0,1.0, 1.0, 1.0};
		glLightfv(GL_LIGHT1, GL_AMBIENT, ambient0);
		glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse0);
		glLightfv(GL_LIGHT1, GL_SPECULAR, specular0);
		glEnable(GL_LIGHTING);
		glEnable(GL_LIGHT1);

The thing i tried to do is to move this light through my scene.

void myOpenGL::moveLight(void){
	
	xLicht1-=50;
		
}

I started it and called the method moveLight. But nothing happenns? Could someone please help me?

I´m sorry but my english is maybe not so good :slight_smile:

Define “nothing happens”. You don’t see anything (screen is black)? You see lighting, but the light source isn’t moving? etc.


GLfloat light0Position[] = {xLight1, 20.0, 20.0, 20.0}; // Position
glLightfv(GL_LIGHT1, GL_POSITION, light0Position);

Ok, so xLight1 is your X light source coordinate. Y=20, Z=20, … Why do you have W=20 as well? That should be 1.0 for a positional light.

Also, the first light source is LIGHT0, not LIGHT1. I don’t know if you’re already using LIGHT0 for something or not, but if not, I’d use LIGHT0. Should work either way, but then again, built-in light sources are legacy functionality.

Also, you may already realize this, but the MODELVIEW matrix that is active when you set your light source POSITION is used to transform the position to eye space. So make sure your MODELVIEW is set appropriately before you call the above.

  GLfloat specular0[]={1.0,1.0, 1.0, 1.0};

I personally would reset these to 0.0 until you figure out what’s going on with your lighting.

Define “nothing happens”. You don’t see anything (screen is black)? You see lighting, but the light source isn’t moving?

I see my scene. As it should be. But if I start the function moveLight, the light doesn´t move. What I want to say: The problem is I don´t see the light moving.

Also, the first light source is LIGHT0, not LIGHT1. I don’t know if you’re already using LIGHT0 for something or not, but if not, I’d use LIGHT0. Should work either way, but then again, built-in light sources are legacy functionality.

LIGHT0 is defined.

What I want to do is moving the LIGHT1 from the left to the right.

your move function needs some improvements


void myOpenGL::moveLight(void){
    xLicht1-=50;
    GLfloat light0Position[] = {xLight1, 20.0, 20.0, 20.0};
    glLightfv(GL_LIGHT1, GL_POSITION, light0Position);
}

hey thanks :slight_smile: thats pretty cool and i tried to do some other simple animations with some objects and everything works fine :slight_smile:

today i tried to move some objects arround just one imaginary point in the middle between of two objects. my objects are moving, but not around my point :slight_smile:

here is the piece of code:


glColor3f(0.0f, 1.0f, 1.0f);
	  glPushMatrix();
	    glTranslatef(1.0f, 1.0f, -radiusObjects);
	    glScalef(0.5f, 0.5f, 0.5f);
	    glutSolidTetrahedron();
	  glPopMatrix();
		glRotatef(winkel3,1.0f,0.0f,0.0f);
	  glPopMatrix();
	  glPushMatrix();
	    glTranslatef(-1.0f, 1.0f, -radiusObjects);
	    glScalef(0.5f, 0.5f, 0.5f);
	    glutSolidOctahedron();
	  glPopMatrix();

where is my mistake?

try this


glColor3f(0.0f, 1.0f, 1.0f);
glPushMatrix();
    glTranslatef(1.0f, 1.0f, -radiusObjects);
    glScalef(0.5f, 0.5f, 0.5f);
    glutSolidTetrahedron();
glPopMatrix();
    
//glPopMatrix(); one too many pop matrix, time to drink that jolt-cola

glPushMatrix();
    glRotatef(winkel3,1.0f,0.0f,0.0f); //moved in here, might work?
    glTranslatef(-1.0f, 1.0f, -radiusObjects);
    glScalef(0.5f, 0.5f, 0.5f);
    glutSolidOctahedron();
glPopMatrix();