• ### Announcements

#### Archived

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

# light position question

## Recommended Posts

This is a performance related question i think... I have a simple 2d car that moves around the screen. Everytime it is updated I also update the position of a light so that it is within the bounds of the current viewing area. Therefore, to me that would mean the light should be flickering around the screen at a seizure inducing rate, pretty much when the car moves, the light should move. Instead, the light seems to move about once per second, whereas I have had the car clamped anywhere between 22 fps and 80 fps (ie the car is getting updated that fast, but the light doesnt seem to be ). My question is: does changing the position of the light have a drastic performance hit in regards to the fact that my machine would not be able to handle the light changing position 22 - 80 times a second. I am very interested in your responses.

##### Share on other sites
does anyone have any ideas on this . . . someone has had to have some experience with this perhaps . . .

##### Share on other sites
I have no idea what is causing your problem; your description seems unclear. It is nothing to do with performance anyway; if updating the position of the light took a second, it would affect the overall framerate (ie, the car would update at 1fps).

I suspect the problem lies in your code which updates the light position.

Just out of interest, how are you implementing lighting in 2D? 2D usually implies uniform normals (straight down the Z axis), and that makes lighting pretty pointless, since the light distribution would be uniform too.

##### Share on other sites
sorry, didnt mean 2d, the car itself is a flat polygon and the camera is lookng top-down, so in my mind, everything is pretty flat and well 2d. I understand what you are saying. i just realized that the headlights of the car are updated every frame properly, so i must be updating my other lights position incorrectly. I want to say that the car''s lights move with it because I update them in the car''s draw method, whereas the other lights are updated in the glutTimerFunction. what do you think about that . . .

##### Share on other sites
Don''t use the glutTimer function to update your lights. Do it once per frame in the main loop.

##### Share on other sites
in my timer function i keep track of a boolean that tells me whether or not the right click menu has been accessed. If it has not then we are animating and in that function i move everything, the car, other objects, and the lights.

What main loop do you mean.

I have my display function, timer function, mouse, keyboard, and special char functions.

##### Share on other sites
I meant somewhere in the glut main loop. The display function is probably a good idea.

Just make sure you''re not putting anything which needs to be done per frame in the timer function. This includes updating animated light positions, drawing code, physics etc. The timer function, as you probably know, isn''t guaranteed to be executed once per frame.

##### Share on other sites
well that is my problem them. I was actually under the impression that the timer function is what controlled the frame-rate, therefore it would have to be invoked once per frame. In my mind I was thinking timer() --> move() --> draw() --> timer() --> move() . . . etc.

Would you care to elaborate more on the more probably flow of events.

thanks again.

##### Share on other sites
THe event structure has nothing to do with your issue. But rather that you are using glut and its timer. the timer will update at the time you say give or take 1.00001 second or something. just because the drawing is occuring does not mean that your lights will be updated in that frame. because if you have something like a your car that is running at 80 fps then your timer would have something like 0.012 seconds to update the lights and return.

Note: that it is best to update your values while in the main loop of your program. the main loop is the control loop or what ever someone might call it.

while(!done){
draw(*myObject);

}

The way you are building your application you must have hundreds of global variables. that have keystate, mouse, menu and render state.

It would be best to not use glut. but obviously this is not going to be a highend game so it should be ok. However functions with parameters and pointers to data structures should be used when ever possible. Its easier to push a pointer unto the stack of a function than copy the whole variable from one function to another.

Before continuing RTMF .... ANSI C v2 ... or better known by the best c programmers as R&K reference manual. They wrote the language at AT&T. So best to read from the guys who thought of it first than to read a interputation of the original.

##### Share on other sites
You mean you have your glutPostRedesplay call in your timer function? In that case, yes, it should be called once per frame. However, that approach is likely to have a negative impact on your framerate overall, as event driven timers are not usually very accurate. Whenever I''m forced to use GLUT, I put a glutPostRedisplay int the display function. The reason is, when you''re coding a game, you always want the window to be redrawn every frame. I''m not sure what the cause of your problem is, but perhaps you should try this approach.

##### Share on other sites
as the code is not in front of me, i really cannot remember, but i think my glutPostRedisplay IS in my display, NOT my timer, which would be way my lights for my car are updated everyframe (i do it from display when the car is drawn) and that the other lights are not updated every frame (i do it in timerFunc)

thanks you

##### Share on other sites
okay, i got rid of my timer function, because i can have all of the same functionality built into the display function, and your right, it was holding back my FPS.

But my lights still do not update every frame drawn. I was wondering if anyone new how rand() worked. I believe I learned/heard once that rand() is based on time. Right now the light position updates once every second, and im getting 80 fps. I set the lights new position via a call to rand() as follows:

  class FlickerLight : public virtual Light {	public:		FlickerLight(GLenum gle) {			type = FLICKER;			srand(time(NULL));			light_id = gle;		}		FlickerLight() {};		~FlickerLight() {};		virtual void updatePosition() {			printf("light moved\n");						position[0] = (((float)(rand() % 200) / 200) * 2) - 1.0;			position[1] = (((float)(rand() % 200) / 200) * 2) - 1.0;			printf("%d\t%d\n", position[0], position[1]);			this->setPosition(this->position);			//glLightfv(light_id, GL_POSITION, position);		}};

could it be that rand() is giving me the same number for a second at a time. It appears that when in the values that print out. I guess my question is why??? and how do i get around it. I dont need the light to change every frame, but more than once a second would be good.

• ### Forum Statistics

• Total Topics
627737
• Total Posts
2978880

• 10
• 10
• 21
• 14
• 13