在原点和用户给定的旋转点处相对于其坐标保持相同的三角形?

Maintain the same triangle with respect to its coordinates both at origin and at the user given points to rotate?

我一直致力于围绕原点和用户指定的坐标旋转三角形。任务是旋转同一个三角形(相同的维度)。到目前为止,我能够在原点做到这一点。但是当它在用户指定的坐标处旋转时,三角形的形状会变大。

#include<GL/glut.h>
#include<stdio.h>
int x,y;
int where_to_rotate=2;

void draw_pixel(float x1,float y1)
{
    //glColor3f(0.0,0.0,1.0);
    glPointSize(5.0);
    glBegin(GL_POINTS);
        glVertex2f(x1,y1);
    glEnd();
}

void triangle(int x,int y)
{
    glColor3f(1.0,0.0,0.0);
    glBegin(GL_POLYGON); // this is what is bothering me.I have to maintain the same triangle even at user specified coordinates
        glVertex2f(x,y);
        glVertex2f(x+50,0);
        glVertex2f(0,y+50);
    glEnd();
}

float rotate_angle=0.0;
float translate_x=0.0,translate_y=0.0;

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    glColor3f(1,1,1);
    draw_pixel(0.0,0.0);
    if(where_to_rotate==1) //Rotate Around origin
    {
        translate_x=0.0;
        translate_y=0.0;
        rotate_angle+=.2;
        draw_pixel(0.0,0.0);
    }
    if(where_to_rotate==2) //Rotate Around Fixed Point
    {
        translate_x=x;
        translate_y=y;
        rotate_angle+=.2;
        glColor3f(0.0,0.0,1.0);
        draw_pixel(x,y);
    }
    glTranslatef(translate_x,translate_y,0.0); //works fine for origin
    glRotatef(rotate_angle,0.0,0.0,1.0);
    glTranslatef(-translate_x,-translate_y,0.0);
    triangle(translate_x,translate_y);
    glutPostRedisplay();
    glutSwapBuffers();
}

void myInit()
{
    glClearColor(0.0,0.0,0.0,1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-800.0, 800.0, -800.0, 800.0);
    glMatrixMode(GL_MODELVIEW);
}

void rotateMenu (int option)
{
    if(option==1)
        where_to_rotate=1;
    if(option==2)
        where_to_rotate=2;
    if(option==3)
        where_to_rotate=3;
}
int main(int argc, char **argv)
{
    printf( "Enter Fixed Points (x,y) for Rotation: \n");
    scanf("%d %d", &x, &y);
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
    glutInitWindowSize(800, 800);
    glutInitWindowPosition(0, 0);
    glutCreateWindow("Create and Rotate Triangle");
    myInit();
    glutDisplayFunc(display);
    glutCreateMenu(rotateMenu);
    glutAddMenuEntry("Rotate around ORIGIN",1);
    glutAddMenuEntry("Rotate around FIXED POINT",2);
    glutAddMenuEntry("Stop Rotation",3);
    glutAttachMenu(GLUT_RIGHT_BUTTON);
    glutMainLoop();
}

在函数 triangle 中,您必须将平移应用于所有顶点坐标:

glVertex2f(x,y);
glVertex2f(x+50,y);
glVertex2f(x,y+50);

注意,设置模型矩阵和指定静态三角形几何就足够了,并且glTranslatef(-translate_x,-translate_y,0.0);取消了您通过triangle(translate_x,translate_y);

设置的平移


如果你想让三角形保持它的位置并围绕一个固定点旋转,那么你必须这样做:

glTranslatef(translate_x, translate_y, 0.0);
glRotatef(rotate_angle, 0.0, 0.0, 1.0);
glTranslatef(-translate_x, -translate_y, 0.0);
triangle(0.0, 0.0);


解释:

  • 矩阵栈的top操作以这种方式变换三角形,其原点是旋转点:

glTranslatef(-translate_x, -translate_y, 0.0);

  • 第二个操作围绕其当前原点旋转三角形

glRotatef(rotate_angle,0.0,0.0,1.0);

  • 最后旋转的三角形被移回:

glTranslatef(translate_x, translate_y, 0.0);


另见