Misc

Defining a Fixed Light Source in OpenGL

0

OpenGL LogoI just wanted to post this random tidbit of information to make sure that it’s available online for other people who are struggling with this. (And for myself if I ever forget this.)

Defining a light source that follows your camera is relatively easy. Here’s what the OpenGL documentation has to say about this process.

How can I make my light position stay fixed relative to my eye position? How do I make a headlight?

You need to specify your light in eye coordinate space. To do so, set the ModelView matrix to the identity, then specify your light position. To make a headlight (a light that appears to be positioned at or near the eye and shining along the line of sight), set the ModelView to the identity, set the light position at (or near) the origin, and set the direction to the negative Z axis.

When a light’s position is fixed relative to the eye, you don’t need to respecify the light position for every frame. Typically, you specify it once when your program initializes.

Creating a fixed light source is slightly harder, because it isn’t set on a fixed position on your screen but rather a fixed position in the scene. Therefore some transformations are required.

How can I make my light stay fixed relative to my scene? How can I put a light in the corner and make it stay there while I change my view?
As your view changes, your ModelView matrix also changes. This means you’ll need to respecify the light position, usually at the start of every frame. A typical application will display a frame with the following pseudocode:

  • Set the view transform.
  • Set the light position //glLightfv(GL_LIGHT_POSITION,…)
  • Send down the scene or model geometry.
  • Swap buffers.

The trick is to treat your light as any other object. The default model/view transformations will apply to your light source. For example, here’s how you can fix light 0 at scene coordinate (xCoordinate, yCoordinate, zCoordinate).

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(xCoordinate, yCoordinate, zCoordinate);
GLfloat lpos[] = {0.0, 0.0, 0.0, 1.0};
glLightfv(GL_LIGHT0, GL_POSITION, lpos);
glPopMatrix();

While this will initialize your light on the correct scene coordinate, keep in mind that you have to recalculate the light’s position in each paintGL() call. If you don’t, its position relative to the scene will change which is not the desired effect.

This post draws from a Stack Overflow question I posted. You can dig it up here for additional insight.

Yahoo! Jumps on the “Funner” Bandwagon

0

Yahoo! Mail Ad: "Faster is funner"

Is "funner" an acceptable word now that Apple started it?

Chromium Adds “Add to Dictionary” to Spell Checker

0

Add to Dictionary

It’s just one of those small things that makes a big difference.

MSN Messenger Advertising Fail

0

Microsoft is promoting both Internet Explorer 8 and Internet Explorer 9 at the same time in Windows Live Messenger.

Promoting IE8 and IE9 at the same time.

And for those of you in North America who are wondering who uses MSN anymore: it was huge in Europe and is still used by many people who grew up with it. It grew out of fashion when Facebook Chat became the dominant IM platform. (Source: anecdotal evidence from a very small sample group.)

Switching over to English

0

Quick announcement: from now on, I will be posting in English.

Go to Top