Why Rotation along from center origin

Viewed 130

I'm facing a rotation problem a triangle that draw with opengl drawing primitive. I am using glut key function to rotate the triangle anticlockwise and clockwise by pressing E and R keys. And then also using glut special key function to translate the triangle x axis and y axis by pressing up down left right keys like this

Rotation and Translation

But after translate this triangle to another position than press rotation key it's not rotate form it's last position where it stay. It's rotate from center origin how to fix this.

Doesn't rotate where last time stay

I don't know what i'm doing wrong.

Code:

#include<iostream>
#include <cstdlib>
#include<GL\freeglut.h>

using namespace std;

float posX = 0.0f;
float posY = 0.0f;

float movethis = 0.1f;
float angle = 0.0f;


void init(void) {

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-1.0, 1.0, -1.0, 1.0, 1.0, -1.0);

}


void drawFigure() {
    glPointSize(4);
    glColor3f(1.0f, 1.0f, 0.0f);

    glBegin(GL_LINES);

    //Total 4 liens 9 vertex
    glVertex2f(0.1f, 0.0f);
    glVertex2f(-0.1f, 0.0f);
    glVertex2f(0.0f, 0.1f);
    glVertex2f(-0.1f, 0.0f);
    glVertex2f(0.0f, 0.1f);
    glVertex2f(0.1f, 0.0f);
    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex2f(0.0f, 0.1f);
    glVertex2f(0.0f, 0.0f);

    glEnd();

    glBegin(GL_POINTS);
    glColor3f(0.0f, 0.0f, 1.0f);
    glVertex2f(0.0f, 0.1f);
    glColor3f(1.0f, 0.0f, 0.0f);
    glVertex2f(0.0f, 0.0f);


    glEnd();

}

void SpecialKeys(int key, int xpos, int ypos) {

    if (key == GLUT_KEY_UP) {
            posY = posY + movethis;

        }

    else if (key == GLUT_KEY_DOWN) {
            posY = posY - movethis;
    }

    else if (key == GLUT_KEY_RIGHT) {
            posX = posX + movethis;
    }

    else if (key == GLUT_KEY_LEFT) {
            posX = posX - movethis;
    }


    glutPostRedisplay();
}


void KeysFun(unsigned char key, int xpos, int ypos) {
    if (key == 'e' || key == 'E') {
        angle=angle+10;
    }



    else if (key == 'r' || key == 'R') {
        angle=angle-10;
    }

    glutPostRedisplay();

}


void display() {

    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();


    glRotatef(angle, 0.0f, 0.0f, 1.0f);
    glTranslatef(posX, posY, 0.0f);

    drawFigure();
    glFlush();

}



int main(int argc, char**argv) {
    glutInit(&argc, argv);
    glutInitWindowSize(600, 600);
    glutInitWindowPosition(450, 50);
    glutCreateWindow("Frame");
    init();
    glutDisplayFunc(display);
    glutSpecialFunc(&SpecialKeys);
    glutKeyboardFunc(&KeysFun);
    glutMainLoop();

    return EXIT_SUCCESS;
}
1 Answers
Related