Advertisement
Monika__

3D cube with rotation

Nov 21st, 2020
753
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. // Template1.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <math.h>
  6. #include <Windows.h>
  7. #include "include\GL\GL.H"
  8. #include "include\GL\GLU.H"
  9. #include "include\GL\glut.h"
  10. #include "Template1.h"
  11.  
  12. using namespace std;
  13.  
  14. double rotate_y = 0;
  15. double rotate_x = 0;
  16.  
  17.  
  18. void MyDisplay(void) {
  19.     glLoadIdentity();
  20.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  21.    
  22.     glPushMatrix();
  23.     glTranslated(0.0, 0.0, -7.0);
  24.     glRotatef(rotate_x, 1.0, 0.0, 0.0);
  25.     glRotatef(rotate_y, 0.0, 1.0, 0.0);
  26.  
  27.  
  28.     //back
  29.     glBegin(GL_POLYGON);
  30.     glColor3f(1.0f, 0.0f, 0.0f);
  31.     glVertex3d(1, 1, 1);
  32.     glVertex3d(-1, 1, 1);
  33.     glVertex3d(-1, -1, 1);
  34.     glVertex3d(1, -1, 1);
  35.     glEnd();
  36.  
  37.     //front
  38.     glBegin(GL_POLYGON);
  39.     glColor3f(0.0f, 1.0f, 0.0f);
  40.     glVertex3d(1, 1, -1);
  41.     glVertex3d(-1, 1, -1);
  42.     glVertex3d(-1, -1, -1);
  43.     glVertex3d(1, -1, -1);
  44.     glEnd();
  45.  
  46.     //right side
  47.     glBegin(GL_POLYGON);
  48.     glColor3f(0.0f, 0.0f, 1.0f);
  49.     glVertex3d(1, 1, -1);
  50.     glVertex3d(1, 1, 1);
  51.     glVertex3d(1, -1, 1);
  52.     glVertex3d(1, -1, -1);
  53.     glEnd();
  54.  
  55.     //left side
  56.     glBegin(GL_POLYGON);
  57.     glColor3f(1.0f, 1.0f, 0.0f);
  58.     glVertex3d(-1, 1, -1);
  59.     glVertex3d(-1, 1, 1);
  60.     glVertex3d(-1, -1, 1);
  61.     glVertex3d(-1, -1, -1);
  62.     glEnd();
  63.  
  64.     //top
  65.     glBegin(GL_POLYGON);
  66.     glColor3f(1.0f, 0.0f, 1.0f);
  67.     glVertex3d(1, 1, -1);
  68.     glVertex3d(-1, 1, -1);
  69.     glVertex3d(-1, 1, 1);
  70.     glVertex3d(1, 1, 1);
  71.     glEnd();
  72.  
  73.     //bottom
  74.     glBegin(GL_POLYGON);
  75.     glColor3f(0.0f, 1.0f, 1.0f);
  76.     glVertex3d(1, -1, -1);
  77.     glVertex3d(-1, -1, -1);
  78.     glVertex3d(-1, -1, 1);
  79.     glVertex3d(1, -1, 1);
  80.     glEnd();
  81.  
  82.     glPopMatrix();
  83.     glFlush();
  84.     //necessary for GLU_DOUBLE displayMode
  85.     //glutSwapBuffers();
  86.  
  87. }
  88. void keyboard(int key, int x, int y) {
  89.  
  90.     if (key == GLUT_KEY_RIGHT)
  91.         rotate_y += 5;
  92.  
  93.     else if (key == GLUT_KEY_LEFT)
  94.         rotate_y -= 5;
  95.  
  96.     else if (key == GLUT_KEY_UP)
  97.         rotate_x -= 5;
  98.  
  99.     else if (key == GLUT_KEY_DOWN)
  100.         rotate_x += 5;
  101.  
  102.     // Request display update
  103.     glutPostRedisplay();
  104.    
  105. }
  106.  
  107.  
  108. void MyInit(void) {
  109.     glClearColor(0.0, 0.0, 0.0, 0.0);
  110.     glViewport(0, 500, 500, 500);
  111.     glMatrixMode(GL_PROJECTION);
  112.     glLoadIdentity();
  113.     gluPerspective(60, 1, 1, 20.0);
  114.     glMatrixMode(GL_MODELVIEW);
  115.     glLoadIdentity();
  116.     glDrawBuffer(GL_BACK);
  117.     glEnable(GL_DEPTH_TEST);
  118. }
  119.  
  120. void Rotate(int){}
  121.  
  122. int main(int argc, char** argv) {
  123.     glutInit(&argc, argv);
  124.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);//single buffer and RGBA
  125.     glutInitWindowSize(500, 500);//initial window size
  126.     glutInitWindowPosition(100, 100);
  127.     glutCreateWindow("Cube");//create widnow, hello title bar
  128.     MyInit();
  129.     glutDisplayFunc(MyDisplay);
  130.     glutSpecialFunc(keyboard);
  131.     glutMainLoop();//enter main loop and process events
  132.     return 0;
  133. }
  134.  
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement