I am trying to animate several objects in opengl, and I am becoming confused. I have a drawing routine like this.
Code:
void app::draw()
{
glClear(GL_COLOR_BUFFER_BIT);
shape s(dp3(0.1f, 0.1f, 0.1f));
shape p(dp3(0.3f, 0.3f, 0.3f));
/*
Add points to shapes
*/
static float x = 0.0f, y = 0.0f;
static float x2 = 0.0f, y2 = 0.0f;
keyboard keys = window.key_get(); // get status of keyboard
if(keys[VK_LEFT]) x -= 0.0005f;
if(keys[VK_RIGHT]) x += 0.0005f;
if(keys[VK_UP]) y += 0.0005f;
if(keys[VK_DOWN]) y -= 0.0005f;
if(keys[0x41]) x2 -= 0.0005f;
if(keys[0x44]) x2 += 0.0005f;
if(keys[0x57]) y2 += 0.0005f;
if(keys[0x53]) y2 -= 0.0005f;
std::wstringstream ss;
ss << x << " " << y << " " << x2 << " " << y2 << std::endl;
::OutputDebugString(ss.str().c_str());
s.draw(x, y);
p.draw(x2, y2);
window.swap();
}
A shape is like this
Code:
class shape
{
public:
shape();
shape(const dp3 &);
~shape();
void add(const dp3 &);
void color(const dp3 &);
void draw(float x, float y) const;
private:
std::vector<dp3> pts;
dp3 col;
};
And to draw it is like this
Code:
void shape::draw(float x, float y) const
{
std::vector<dp3>::const_iterator i;
glPushMatrix();
glColor3f(col.g(X), col.g(Y), col.g(Z));
glTranslatef(x, y, 0.0f);
glBegin(GL_POLYGON);
for(i = pts.begin(); i != pts.end(); ++i)
{
glVertex3f(i->g(X), i->g(Y), i->g(Z));
}
glEnd();
glPopMatrix();
}
If I do not have the glPush/PopMatrix then the two shapes are moved as a single unit (bad), and it accelerates (good). But if I do not have those, then the objects do move seperately (good) but do not accelerate (bad).
I check the values of x, y, x2, y2, and it seems that only x, y are modified by arrow keys and wasd keys when glPush/PopMatrix is not in the code, but they seem to be reset to 0 when glPush/PopMatrix is in the code. I don't get it, they're static.