我遇到了一个奇怪的问题,我试图旋转的对象突然消失了。我想旋转对象的方法是按下一个键,它会将全局变量rotateAnimation
设置为true。当变量rotateAnimation
为false时,对象是可见的,但一旦为true,对象就消失了。
下面是代码:
bool rotateAnimation = false;
bool moveAnimation = false;
int moveDirX = 1, moveDirY = 1;
void DrawSpiral(int x, int y)
{
float tx, ty;
float i;
float a = 0, b = 0;
glPointSize(3);
glBegin(GL_LINE_STRIP);
for (i = 0; i < 20; i = i + 0.025)
{
a = a + .05;
b = b + .05;
tx = x + b * cos(i);
ty = y + a * sin(i);
glVertex2f(tx, ty);
}
glEnd();
}
void DrawSquare(int x, int y, int size)
{
glBegin(GL_POLYGON);
glVertex2f(x, y);
glVertex2f(x - size, y);
glVertex2f(x - size, y - size);
glVertex2f(x, y - size);
glEnd();
}
void DrawPolygon(int x, int y)
{
glBegin(GL_POLYGON);
glVertex2f(x, y);
glVertex2f(x - 25, y - 25);
glVertex2f(x - 15, y - 53);
glVertex2f(x + 15, y - 53);
glVertex2f(x + 25, y - 25);
glEnd();
}
void init()
{
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glMatrixMode(GL_PROJECTION); // Since I want to do rotation in 2D
glLoadIdentity();
glClearColor(1.0, 1.0, 1.0, 0);
glColor3f(0.0f, 0.0f, 0.0f);
gluOrtho2D(0, WINDOW_WIDTH, 0, WINDOW_HEIGHT);
}
void Keypress(int key, int x, int y)
{
switch (key)
{
case 'm':
moveAnimation = !moveAnimation;
break;
case 'o':
rotateAnimation = !rotateAnimation;
break;
}
}
void Display()
{
glClear(GL_COLOR_BUFFER_BIT);
staticScene(); // Some other drawings that should always be there on the screen
glPushMatrix();
if (rotateAnimation)
{
glRotatef(60, 1.0, 0.0, 0.0);
}
glColor3f(0.0f, 0.0f, 0.0f);
glPointSize(2);
if (shape == "square")
DrawSquare(X, Y, 40);
else if (shape == "polygon")
DrawPolygon(X, Y);
else if (shape == "spiral")
DrawSpiral(X, Y);
glPopMatrix();
glutSwapBuffers();
}
void timer(int value)
{
glutPostRedisplay();
glutTimerFunc(1000 / 60, timer, 0);
if (moveAnimation)
{
int dx = 2, dy = 2;
if (X - dx < 0) // Add from X
X += dx, moveDirX = 1;
else if (X + dx > drawingWindow[0].first) // Subtract to X
X -= dx, moveDirX = -1;
else
X += (dx * moveDirX);
if (Y - dy < drawingWindow[2].second) // Add from Y
Y += dy, moveDirY = 1;
else if (Y + dy > drawingWindow[0].second) // Subtract to Y
Y -= dy, moveDirY = -1;
else
Y += (dy * moveDirY);
}
}
当rotateAnimation
为假时:
当rotateAnimation
为真时:
EDIT:我也尝试缩放对象,但是同样的事情发生了,当开关打开时,对象消失,当开关关闭时,对象出现。我注意到的一件事是,当我注解掉TimerFunc
时,什么也没有发生。对象scale
或rotate
也没有发生,这取决于开关打开了什么。
1条答案
按热度按时间dz6r00yl1#
***已编辑:***您没有将矩阵模式切换到
GL_MODELVIEW
。