# Problem Rotating z-axis openGL c++

i am trying to rotate a quad at z-axis but the problem is , it is not rotating but translating towards some other direction rather than rotating at a particular point.I believe my display function is correct, and the problem exist in init() function and is related to modelView or projection matrix, i downt know the purpose of them and what will be the sequence, here is my code

#include<GL\freeglut.h>
#include<Windows.h>
#include <math.h>
#include"F:\BSCS VI\cg\cg\SOIL.h"
double moveX = 0.0;
double x = 0, b = 0;
int degree = 0;
void display() {

glEnable(GL_TEXTURE_2D);

glClear(GL_COLOR_BUFFER_BIT);

glPushMatrix();
glRotatef(degree, 0.0f, 0.0f, 1.0f);
glColor3f(0, 0, 0);
glVertex2d(30, 300);
glVertex2d(100, 300);
glVertex2d(100, 320);
glVertex2d(30, 320);
glEnd();
glPopMatrix();
////////////////quad to be rotated , end

degree++;
glFlush();
}

void init(void) {
glClearColor(.75, .75, .75, 0.0); /* background color select clearing color */
//glEnable(GL_TEXTURE_2D);

glMatrixMode(GL_PROJECTION);
gluOrtho2D(0, 500, 0, 500);

}

void timer(int x){
glutTimerFunc(100, timer, x);
glutPostRedisplay();
}

int main(int argc, char * argv[]) {
glutInit(&argc, argv);
glutInitWindowPosition(100, 100);   glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 500);
glutCreateWindow("simple");  // default win. 300 x 300
glutTimerFunc(100, timer, x);
init();
glutDisplayFunc(display);  // set callback for display function

glutMainLoop();
return 0;
}


Actually, your code is working fine. Rotations always rotate around the origin, which based on your projection setup, is currently the lower-left corner of the screen. So the solution? Move the origin. We can do this by rotating first, then moving the rotated object to the desired rotation point, in this case I've chosen the center of the screen. (Remember in OpenGL, operations happen in reverse order from what they are in the code.) This causes the rectangle to rotate around the center in a counter-clockwise fashion, sort of like a (backwards) hand of a clock.

void display() {
glEnable(GL_TEXTURE_2D);

glClear(GL_COLOR_BUFFER_BIT);

glPushMatrix();
glTranslatef(250.0f, 250.0f, 0.0f); // Point we want to rotate around
glRotatef(degree, 0.0f, 0.0f, 1.0f);
glColor3f(0, 0, 0);
// These coordinates adjusted to be relative to point above
glVertex2d(-220, 50);
glVertex2d(-150, 50);
glVertex2d(-150, 70);
glVertex2d(-220, 70);
glEnd();
glPopMatrix();
////////////////quad to be rotated , end

degree++;
glFlush();
}


In addition, generally I would set the mode to GL_MODELVIEW and load the identity at the end of init(), but that's up to you. If you would like information on the OpenGL matrix modes, you can see this answer