我画一个圆圈,里面有一个+号,看它旋转
void drawcircle(float radius, float x, float y){
// Draw a line loop with vertices at equal angles apart on a circle
// with center at (X, Y) and radius R, The vertices are colored randomly.
float R = radius; // Radius of circle.
float X = x; // X-coordinate of center of circle.
float Y = y; // Y-coordinate of center of circle.
int numVertices = 25; // Number of vertices on circle.
float t = 0; // Angle parameter.
int i;
glColor3f(1.0, 1.0, 1.0);
glPolygonMode( GL_FRONT, GL_FILL );
glBegin(GL_POLYGON);
for(int i = 0; i < numVertices; ++i)
{
//glColor3ub(rand()%256, rand()%256, rand()%256);
glVertex3f(X + R * cos(t), Y + R * sin(t), 0.0);
t += 2 * PI / numVertices;
}
glEnd();
//PLUS SIGN
float distance = radius*2 + y;
glColor3f(0.0, 0.0, 0.0);
glBegin( GL_LINES);
glVertex3f(x, y-radius, 0.0f);
glVertex3f(x, y+radius, 0.0f);
glEnd();
glBegin( GL_LINES);
glVertex3f(x-radius, y, 0.0f);
glVertex3f(x+radius , y, 0.0f);
glEnd();
}
在我的显示功能中,我想像汽车轮胎一样沿着相同的路径移动它,每次旋转 30 度,像
一样向屏幕右侧旋转glPushMatrix();
glTranslatef(circle1_x, 0, 0.0f);
glRotatef(30, 0.0f, 0.0f, 1.0f);
glTranslatef(-circle1_x, 0, 0.0f);
drawcircle(20, circle1_x, 350.0);
glPopMatrix();
我将它平移到一个位置,然后旋转它,然后将它平移回相同的位置,但它就像只移动了一次一样脱落并继续移动。我使用 idle 函数和 glutIdleFunc(idle);将圆圈向右移动并将其重置为屏幕的第一个位置。
void idle() {
if(circle1_x > 600 ){
circle1_x = 0;
}
else{
circle1_x += 0.01;
}
glutPostRedisplay();
}
像这样
int main(int argc, char **argv)
{
glutIdleFunc(idle);
glutMainLoop();
return 0;
}
我怎样才能使圆圈可以移动并且同时旋转
这个不画图有点不好解释。但我会试一试。当你第一次翻译 circle_x(假设它是 10)时,你将你的“绘图轴”移动到 x = 10,之后你将你的“绘图轴”旋转 30 度。当您“将其平移回相同位置”时,问题就来了,因为您进行了旋转,因此您不会回到开始时的相同位置。尝试使用 glPushMatrix() 并仅进行平移和旋转,然后绘制并执行 glPopMatrix()。