OpenGL light flickering when I update its position each frame

前提是你 提交于 2020-01-24 13:39:46

问题


I am learning OpenGL and just started getting into lighting. I enable lighting and light 0, set its ambient/diffuse/specular to default values in my init method, and then each frame I do something like this:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// do my 3rd-person camera transforms and rotations
glLightfv(GL_LIGHT0, GL_POSITION, {0.1f, 0.0f, 0.0f});

(note if it's slightly wrong, I'm writing my app in Java using JOGL so I trimmed the gl. and GL. from each line for better readability for all you C++ folks :) )

I then proceed to draw my triangles and such, and at the end I do a glFlush().

The odd problem is that when I run my application, the light flickers. I have no other calls to glLightfv in my whole program (other than, as mentioned above, the calls in my init method, which I have debugged to make sure it does only run once). It flickers very fast and the screen shows tearing. I also do not glDisable(GL_LIGHTING) at any point in my scene drawing; not that I think that should cause the lit objects to flicker.

I have also, for debugging purposes, commented out chunks of my drawing code, and nothing seems to be breaking the lighting - and of course, I wrote all that and tested it before lighting the scene anyway, so I see no reason it should be wrong.

Basically I know this isn't much to go by, but has anyone else had this problem before? Is there some simple solution I just can't find? Please request any additional information that could help you help me. And in the end if I simply can't get an answer, I'll probably have to break down and write a test case where I can just post the entire rendering code here and demonstrate the problem.

Here is a really bad animated gif I tried to make... But it kinda shows what I'm talking about, as far as the light being sporadic. The lit frames are what it's supposed to look like. Also it's not so much flickering when I slow it down to 5fps, more just kinda randomly on or off. I don't get it...

Thanks!!


回答1:


Aha! I found the answer already... I only specified 3 floats for the position, when it wants 4 as documented here. So the fourth number, which must have some affect on the light's brightness, was being read from some other random area in memory, and of course odd things happened.

Sorry to waste your time!



来源:https://stackoverflow.com/questions/1039208/opengl-light-flickering-when-i-update-its-position-each-frame

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!