Search code examples
c++openglrotationglut

OpenGL rotating glRectf() function?


I'am working on a project which one my homework. I need rotate a car(not exactly but like a car this is not important I think). My car 2d. I create it with glReactf(); function. Because of I can create a rectangle with this function pixel by pixel. Like;


// Create a rectangle (0,0) to (30,30) :) Like a square, yeap :) I use it.
// Because I am working on a lot of rectangles and you know, squares are rectangles in math :)

    glRectf(0, 0, 30, 30);

But I have a code. It works but it is 3d. I can't turn it 2d. Can you help me? This is not important, I am working on 2d. While I say "I can't turn it 2d" I mean I didn't get algorithm and logic on this code. This is 3d quads code, and I can turn it with rotatef() function;


#include <stdio.h>
#include <GL/glut.h>

double rotate_y = 0;


void display() {
       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
       glLoadIdentity();
glRotatef(rotate_y, 0.0, 1.0, 0.0);

    glBegin(GL_POLYGON);
    glColor3f(1.0, 1.0, 1.0);
    glVertex3f(0.5, -0.5, 0.5);

    glColor3f(1.0, 1.0, 0.0);
    glVertex3f(0.5, 0.5, 0.5);

    glColor3f(1.0, 0.0, 1.0);
    glVertex3f(-0.5, 0.5, 0.5);

    glColor3f(1.0, 1.0, 1.0);
    glVertex3f(-0.5, -0.5, 0.5);

    glEnd();
glFlush();
glutSwapBuffers();
}



void keyboard(int key, int x, int y) {
    if (key == GLUT_KEY_RIGHT) {rotate_y += 45;}
    else if (key == GLUT_KEY_LEFT) {rotate_y -= 45;}
glutPostRedisplay();
}



int main(int argc, char* argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
        glutInitWindowPosition(0, 0);
        glutInitWindowSize(800, 800);
glutCreateWindow("Rotating Test");
glutDisplayFunc(display);
glutSpecialFunc(keyboard);
glutMainLoop();
return 0;
    }

I need a car(like 30x30 px quad in 2d) and I need turn it 180 degree on y axis. I want to create it like write it my first code.


Solution

  • I solve it, how I did it I don't know, I am serious but it done. This is my display function;

    void display() {
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glLoadIdentity();
            glRotatef(rotate_y, 0.0, 1.0, 0.0);
            glColor3f(1.0, 1.0, 1.0);
            glRectf(1, 1, 11, 11);
            glFlush();
            glutSwapBuffers();
        }