Reputation: 21
My code is about creating 2 cubes, then rotating them at the same time, moving them at the same time, and scaling them at the same time. For rotating and scaling they work, but moving doesn't. I think it's something to do with my glTranslatef() but I tried all the ways I know.
The origin is the center of window and the cubes are always rotating.
Fragment Code:
static GLfloat x = 0.0f;
static GLfloat y = 0.0f;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(-(1.5f + x), y + 0.5f, 0.3f);
glScalef(xScale, yScale, zScale);
glRotatef(theta[0], 1.0, 0.0, 0.0);
glRotatef(theta[1], 0.0, 1.0, 0.0);
glRotatef(theta[2], 0.0, 0.0, 1.0);
colorcube();
glFlush();
glLoadIdentity();
glTranslatef(1.5f + x, y + 0.5f, 0.3f);
glScalef(xScale, yScale, zScale);
glRotatef(theta[0], 1.0, 0.0, 0.0);
glRotatef(theta[1], 0.0, 1.0, 0.0);
glRotatef(theta[2], 0.0, 0.0, 1.0);
colorcube();
glFlush();
glutSwapBuffers();
}
void spinCube()
{
// idle callback, spin cube 2 degrees about selected axis
if (negative == true) theta[axis] -= 0.5;
else if (negative == false) theta[axis] += 0.5;
if (theta[axis] > 360.0) theta[axis] -= 360.0;
//display();
glutPostRedisplay();
}
void actionKeys(unsigned char key, int x, int y )
{
switch (key)
{
case 'r': case 'R':
x += 0.5f;
glutPostRedisplay();
break;
case 'l': case 'L':
x += 0.5f;
glutPostRedisplay();
break;
case 'u': case 'U':
y += 0.5f;
glutPostRedisplay();
break;
case 'd': case 'D':
y -= 0.5f;
glutPostRedisplay();
break;
}
}
Upvotes: 0
Views: 499
Reputation: 1143
The problem is with variable scope in your actionKeys function. You have global variables named x and y and the function parameters are also named x and y. Inside the actionKeys function the function parameter x and y are the ones that are being updated, not the global ones. This is why your movement doesn't work since the x and y global variables are never updated. I would suggest renaming the global variables to have a different naming convention than your function parameters so that you don't run into this problem again in the future. A common naming convention for global variables is to prefix them with g (for global) so you don't run into naming clashes with local variables.
Upvotes: 1