Jump to content

  • Log In with Google      Sign In   
  • Create Account


2D OpenGL lighting


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 krikri   Members   -  Reputation: 103

Like
0Likes
Like

Posted 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
	glShadeModel(GL_SMOOTH);
  
	// 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 };
	// valor expresado en Coordenadas x,y,z,w
	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...

Edited by krikri, 17 May 2012 - 12:02 AM.


Sponsor:

#2 szecs   Members   -  Reputation: 2092

Like
1Likes
Like

Posted 17 May 2012 - 12:13 AM

What were the results? What didn't work? It's clear what you expected, but it's not clear what actually happened.
Coordinate systems may be different between SFML and openGL, maybe the y coordinate is flipped, maybe their origin is different, etc.

Maybe something like light_position[1] = (float)(ClientAreaHeight - Event.mouseMove.y); would work, but it's hard to say without a good explanation (BTW making a good explanation of what you expect and what are the actual behaviour is the first and most important thing in solving problems/bugs like this one)

Another thing may be a problem that openGL behaves a bit differently with light transformation
Read this

Edited by szecs, 17 May 2012 - 12:16 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS