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

1 upvote
  flag
And what issue you are facing with that code ? – Chetan Ranpariya
upvote
  flag
drive.google.com/file/d/14HHRiCbOHK9ZSZtDOqSl4GP4aSy7UQLh/… << This is my output . it's not similar to the output of this video >> youtube.com/watch?v=T_Ihh3tF5fk&list=WL&index=32 – Kanzt
upvote
  flag
Warning: What you're learning is a super-old version of OpenGL that has no practical use nowadays, I recommend to learn the current OpenGL or WebGL instead – Kos

2 Answers 11

glRotate rotates about the origin (0,0). Given your projection matrix (that you set with glOrtho) the origin is initially at the lower left corner of your screen, unless you use glTranslate. Your diamond is not centered at the origin, but positioned somewhat away from it. What you need to do is change the vertex values in your void hut(void) method to make the diamond centered at 0,0. Then use glTranslate to move the render origin (and thus also the diamond) to where you want it, then use glRotate.

1 upvote
  flag
You should add that the center is in the lower left corner because of the used projection matrix. By default, the center is in the middle of the screen (NDC ranges from [-1, 1]). – BDL
1 upvote
  flag
@BDL Clarified, thanks. – Max Vollmer
up vote 1 down vote accepted

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 roatet 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 followig 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 take 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); 
upvote
  flag
Thank you for your answer, now i can solve this problem with your guide – Kanzt
upvote
  flag
i have some question , why we must use only glTranslatef(0.5f, 0.5f, 0.0f); and then we use glTranslatef(-0.5f, -0.5f, 0.0f); , when i change it to other value it doesn't work – Kanzt
upvote
  flag
@Kanzt Because the center of your object is about (0.5, 0.5) and you want rotate around the center of your object. – Rabbid76
upvote
  flag
could i use glTranslatef(-0.5f, -0.5f, 0.0f); first > rotate and then glTranslatef(0.5f, 0.5f, 0.0f); ? – Kanzt

Not the answer you're looking for? Browse other questions tagged or ask your own question.