我想了解当我点击鼠标按钮时如何围绕其中心旋转多边形?
glBegin(GL_POLYGON); //drawing a pentagon and coloring it.(using a polygon GL)
glColor3f(0, 0, 1);//Blue
glVertex3f(0.0f, 0.7f, 0.0f);//top point
glColor3f(1, 0.5, 0);//Orange
glVertex3f(-0.5f, 0.3f, 0.0f);//left middle point
glColor3f(0, 1, 0);//Green
glVertex3f(-0.3f, -0.3f, 0.0f);//bottom left point
glColor3f(0.53, 0.12, 0.47);//Purple
glVertex3f(0.3f, -0.3f, 0.0f);//bottom right point
glColor3f(1, 0, 0);//red
glVertex3f(0.5f, 0.3f, 0.0f);//middle right point
glEnd();
glutSwapBuffers();//Sends the Scene to the window
}
void mouse(int button, int state, int x, int y){
if (button == GLUT_RIGHT_BUTTON){
glRotatef(angle, 0, 0, 1);
}
}
答案 0 :(得分:0)
最简单的方法是将旋转角度存储在全局变量中。这将是这样的:
global:
float angle = 0;
draw:
glRotate(angle, 0, 0, 1);
glBegin(GL_POLYGON);
...
void mouse(int button, int state, int x, int y){
if (button == GLUT_RIGHT_BUTTON)
angle += A_VALUE_PER_CLICK;
}
编辑:注意,只要您点击(按要求),上述内容就会旋转。如果你想要旋转对象,只要按下鼠标,你就必须做一点不同的事情:
global:
bool is_mouse_down = false;
float angle = 0;
draw:
if (is_mouse_down)
angle += ANGLE_PER_FRAME;
glRotate(angle, 0, 0, 1);
glBegin(GL_POLYGON);
...
void mouse(int button, int state, int x, int y){
if (button == GLUT_RIGHT_BUTTON) {
if (state == GLUT_DOWN)
is_mouse_down = true;
if (state == GLUT_UP)
is_mouse_down = false;
}
}
编辑2:要旋转特定点(在这种情况下是多边形的中心[0,0.2,0]),必须始终将此点转换为原点,然后旋转然后将其平移。这意味着必须使用类似这样的代码(标志可能会被翻转):
draw:
glTranslate(0, 0.2, 0);
glPushMatrix();
glRotate(angle, 0, 0, 1);
glPushMatrix();
glTranslate(0, -0.2, 0);
//Draw
glPopMatrix();
glPopMatrix();