Search code examples
copenglcoordinate-transformation

how to use glTranslatef,glScalef,glRotatef in openGL


I just want something like this video : https://youtu.be/dGWtdYlryQQ It shows how to use glTranslate, glRotate, gluOrtho2d in OpenGL ,but it's not guide me anything

In my case, I draw a diamond instead of triangle and here is my condition

condition :

  1. when I press r or R on the keyboard the diamond will rotate clockwise
  2. when I press t or T on the keyboard the diamond will move to the right side
  3. when I press + on the keyboard the diamond will bigger

here is my code :

#include <stdlib.h>
#include <windows.h>
#include <GL/glut.h>
#include <iostream>
using namespace std;

float angle = 0;
float t,s=0.5,m=0;

void myinit(void){
    glClearColor(1.0,1.0,1.0,0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
}
void keyboard(unsigned char key, int x, int y){
        if(key==27)
        {
            exit(0);

        }else if(key == 82 || key == 114){
            angle-=0.1;

            glRotatef(angle,0,0,1);
            glutPostRedisplay();

        }else if(key == 84 || key == 116 )
        {
             t+=0.01;
             glTranslatef(t,0,0);
             glutPostRedisplay();
        }else if(key == 43){
                 s+=0.01;

               //  m-=0.1;
               //  glTranslatef(m,m,0.0);
                 glScalef(s,s,0);
                 glutPostRedisplay();
        }
        (void)(x);
        (void)(y);
}
void hut(void){

    glClear(GL_COLOR_BUFFER_BIT);

        glBegin(GL_TRIANGLE_FAN);
        glVertex3f(0.5,0.4,0.0);
        glVertex3f(0.42,0.5,0.0);    // GREEN
        glVertex3f(0.44,0.5,0.0);
    glColor3f(1.5,1.0,0.0);
        glVertex3f(0.46,0.5,0.0);
    glColor3f(0.25,0.0,0.0);
        glVertex3f(0.57,0.5,0.0);
        glEnd();
    glFlush();

    glColor3f(1.5,1.0,0.0);
        glBegin(GL_TRIANGLE_FAN);
        glVertex3f(0.44,0.55,0.0);
        glVertex3f(0.42,0.5,0.0);
        glVertex3f(0.46,0.5,0.0);
    glColor3f(0.25,0.0,0.0);
        glVertex3f(0.48,0.55,0.0);
        glEnd();

    glColor3f(1.5,1.0,0.0);
        glBegin(GL_TRIANGLE_FAN);
        glVertex3f(0.48,0.55,0.0);
        glVertex3f(0.46,0.5,0.0);
        glVertex3f(0.50,0.5,0.0);
    glColor3f(0.25,0.0,0.0);
        glVertex3f(0.52,0.55,0.0);
        glEnd();

    glColor3f(1.5,1.0,0.0);
        glBegin(GL_TRIANGLE_FAN);
        glVertex3f(0.52,0.55,0.0);
        glVertex3f(0.50,0.5,0.0);
        glVertex3f(0.54,0.5,0.0);
    glColor3f(0.25,0.0,0.0);
        glVertex3f(0.56,0.55,0.0);
        glEnd();
    glColor3f(1.5,1.0,0.0);
        glBegin(GL_TRIANGLE_FAN);
        glVertex3f(0.56,0.55,0.0);
        glVertex3f(0.54,0.5,0.0);
        glVertex3f(0.57,0.5,0.0);
        glEnd();
    glFlush();

}
int main(int argc,char** argv){
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(640,480);
    glutCreateWindow("Polygon with viewport");
    myinit();
    glutDisplayFunc(hut);

    glutKeyboardFunc(keyboard);
    glutMainLoop();
}

And here is my output : https://drive.google.com/file/d/14HHRiCbOHK9ZSZtDOqSl4GP4aSy7UQLh/view?usp=sharing It it’s not similar to this https://youtu.be/dGWtdYlryQQ


Solution

  • The operations on the matrix stack are based on one another. The reference system of each operation is the current transformation.

    See the documentation of glTranslate:

    glTranslate produces a translation by x y z . The current matrix (see glMatrixMode) is multiplied by this translation matrix, with the product replacing the current matrix, [...]

    and see the documentation of glRotate:

    glRotate produces a rotation of angle degrees around the vector x y z . The current matrix (see glMatrixMode) is multiplied by a rotation matrix with the product replacing the current matrix.

    This means that glRotate does a rotation around the origin of the current local system.

    While glRotatetf followed by glTranslatef results in:

    enter image description here

    glTranslatef followed by glRotatef results in:

    enter image description here


    Since you object is displaced, you have to translate it in that way, that the rotation point is placed in the origin:
    glTranslatef(-0.5f, -0.5f, 0.0f);
    

    Then you can rotate it:

    glRotatef(angle, 0.0f, 0.0f, 1.0f);
    

    And move it back:

    glTranslatef(0.5f, 0.5f, 0.0f);
    

    Note, on the Fixed Function Pipeline stack you have to "push" this operations in the reverse order. Further you should use the GL_MODELVIEW matrix stack. (See glMatrixMode.)

    Remove all the matrix operations from the function keyboard and add the following to the function hut:

    void hut(void)
    {
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glTranslatef(t, 0.0f, 0.0f);
        glTranslatef(0.5f, 0.5f, 0.0f);
        glRotatef(angle, 0.0f, 0.0f, 1.0f);
        glScalef(s, s, 0.0f);
        glTranslatef(-0.5f, -0.5f, 0.0f);
    
        .....
    

    Further, your object gets destroyed by the aspect ratio of the view. This can be fixed by taking care of the aspect ratio when setting up the projection matrix:
    float w = 640.0f;
    float h = 480.0f;
    glOrtho(0.0,w/h,0.0,1.0,-1.0,1.0);