I’m working on a simple OpenGL application that allows the user to adjust the background color of a simple window by clicking and dragging to either then left (make it less intense) or the right (more intense). When dragging to the right, for example, I have something like this:
intensity += 0.0001*x; //where x is the current mouse location
…and to the left, the other way:
intensity -= 0.0001*x;
intensity is global static GLfloat that gets passed to glClearColor in the display function, which is called multiple times. This strategy works just fine when the user intensifies the color while dragging right, but it messes up after a certain point when dragging left: the color becomes less intense as I drag along and returns to black as it should, but then intensifies back into the original color.
I think the problem is that the intensity, which should be between 0 and 1, is becoming negative when I drag too far and, thus, glClearColor is fixing the problem by just changing the sign back to positive (?). If that is the case, how can I enforce that the values passed to glClearColor stay in the proper range? I tried to fix the problem by making sure that the current intensity would not become negative/adjustment would not happen if it is already too low, but it didn’t seem to work:
if(x <= xPrev && intensity >= (.0001*x)) //moving left to de-intensify color
intensity -= 0.0001*x;
…Am I doing this incorrectly, or is glClearColor handling negative values differently than I expect?
Another interesting issue that came up is that my “intensity” adjustments only seem to work on the r, g, and/or b values (i.e., I pass “intensity” as one of those in glClearColor. Using the a value didn’t work…but isn’t it supposed to change color intensity?
This sounds a bit like a hysteresis problem. It’s not clear how events are received, or what coordinate system
xis in. Perhaps drop an ‘anchor’ (xStart) when the button is pressed, and handle all subsequent values while dragging as an offset from this value. What toolkit / library are you using for windows and events? If you launch from a console, perhaps add a trace statements like:fprintf(stderr, "%.8e\n", x);to see if the values of
xcorrespond to what you’re expecting on each frame.glClearColor clamps values to
[0, 1], so it’s probably not the issue. Perhaps a trace forintensitywould be useful?