Advertisement
Lorky

OPENGL Rotating rectangle

Dec 8th, 2022 (edited)
1,146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | Source Code | 0 0
  1. #include <GL/glut.h>
  2. #include <cmath>
  3.  
  4. float angle = 0.0f;
  5.  
  6. void drawRectangle() {
  7.   // Set the color of the rectangle
  8.   glColor3f(1.0f, 0.0f, 0.0f);
  9.  
  10.   glPushMatrix();
  11.  
  12.   // Rotate y-axis
  13.   glRotatef(angle, 0.0f, 1.0f, 0.0f);
  14.  
  15.   // Draw rectangle
  16.   glBegin(GL_QUADS);
  17.   glVertex2f(-0.5f, -0.5f);
  18.   glVertex2f(0.5f, -0.5f);
  19.   glVertex2f(0.5f, 0.5f);
  20.   glVertex2f(-0.5f, 0.5f);
  21.   glEnd();
  22.  
  23.   glPopMatrix();
  24. }
  25.  
  26. void update(int value) {
  27.   // Update the angle of rotation
  28.   angle += 1.0f;
  29.   if (angle > 360) {
  30.     angle -= 360;
  31.   }
  32.  
  33.   glutPostRedisplay();
  34.  
  35.   // 10ms
  36.   glutTimerFunc(10, update, 0);
  37. }
  38.  
  39. int main(int argc, char** argv) {
  40.   // Initialize GLUT
  41.   glutInit(&argc, argv);
  42.   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  43.   glutInitWindowSize(500, 500);
  44.  
  45.   glutCreateWindow("Rotating Rectangle");
  46.  
  47.   glutDisplayFunc(drawRectangle);
  48.  
  49.   glutTimerFunc(10, update, 0);
  50.  
  51.   glutMainLoop();
  52.  
  53.   return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement