I am trying to complete some basic example in OpenGL from the OGLSuperbible. Basically, I am going to make the background shift from red to orange to green and back.
Here is the code that I am using:
typedef float F32;
void Example1::Render(void) {
time += 0.20f;
const GLfloat color[] = { (F32)sin(time) * 0.5f + 0.5f,
(F32)cos(time) * 0.5f + 0.5f,
0.0f, 1.0f };
glClearBufferfv(GL_COLOR, 0, color);
}
I have a precision timer that will measure the delta time of the last frame, but, anytime that I call the sin
and cos
functions with anything less than 1
, it just keeps the screen at green. However, If I hard code the value to change by, as I have, if I increase it by 1
or more, it will flash between the colors very quickly (like a rave). I am not sure why the functions wont work for floating point numbers. I am using visual studio, and have included the math.h
header. Has anyone seen anything like this before?
Update: Based of suggestions, I have tried a few things with the code. I got the program to have the effect that I was looking for by adding the following:
Testing my code, I manually input the follow:
In the constructor:
Example1(void): red(0.0f), green(1.0f), interval(0.002f), redUp(true), greenUp(false).....
In the render loop
if (red >= 1.0f) { redUp = false; }
else if (red <= 0.0f) { redUp = true; }
if (green >= 1.0f) { greenUp = false; }
else if (green <= 0.0f) { greenUp = true; }
if (redUp) { red += interval; }
else { red -= interval; }
if (greenUp) { green += interval; }
else { green -= interval; }
const GLfloat color[] = { red, green, 0.0f, 1.0f };
It does what its supposed to, but using the sin and cos functions with the floating point numbers has no change. I am baffled by why, I has assumed that giving sin and cos the floating point values that are time would work. I have tried counting time manually, incrementing it by 1/60th of a second manually, but any time I use sin and cos with anything less than 1.0f, it just remains green.