• Create Account

Calling all IT Pros from Canada and Australia.. we need your help! Support our site by taking a quick sponsored surveyand win a chance at a \$50 Amazon gift card. Click here to get started!

# krikri

Member Since 16 May 2012
Offline Last Active May 19 2012 10:28 PM

### 2D OpenGL lighting

16 May 2012 - 11:57 PM

Good evening .....
I've been testing with OpenGL lights, more specifically in SFML and I still can't understand how to position the light.
I used coordinates (x, y, z, w) and if w = 0 then is a Directional Light (like the sun), and if w> 0 then it is a Positional Light (like a lamp).

But .... I need an opinion / explanation from someone who already knows, because I do not get the results I wanted.

In one of the main, before the main game loop I have this:
```// Valores posibles: GL_SMOOTH - GL_FLAT

// Vector 4f que representa el color en RGBA del "ambient"
GLfloat light_ambient[4] = { 0.0, 0.0, 0.0, 1.0 };
// Vector 4f que representa el color en RGBA del "diffuse"
GLfloat light_diffuse[4] = { 1.0, 1.0, 1.0, 1.0 };
// Vector 4f que representa el color en RGBA del "specular"
GLfloat light_specular[4] = { 1.0, 1.0, 1.0, 1.0 };
// valor que va entre [0,128]
GLfloat mat_shininess[1] = { 50.0 };
GLfloat light_position[4] = { -2.0, 2.0, 1.0, 1.0 };

GLfloat spot_direction[] = { -1.0, -1.0, 0.0 };

// Asignacion de valores a la luz 0
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);

glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 45.0);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spot_direction);
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 2.0);

glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);

//
glEnable(GL_LIGHTING);
//
glEnable(GL_LIGHT0);
```
To give a sense of relief in 2d, I use a shader with normal map .... but first I want positioning the light and then make use of a Spot Light.
I had tried was to move the light with the mouse position, which is not as straightforwardas come and do this:

[size=4]
```if(Event.type == sf::Event::MouseMoved) {
light_position[0] = (float)(Event.mouseMove.x);
light_position[1] = (float)(Event.mouseMove.y);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
}[/background][/size][/font][/color]
[color=#333333][font=arial, sans-serif][background=rgb(245, 245, 245)]```

I hope someone can help me...

PARTNERS