Advertisement
Guest User

triangles check

a guest
Nov 21st, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.59 KB | None | 0 0
  1. // Template1.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <Windows.h>
  6. #include "include\GL\GL.H"
  7. #include "include\GL\GLU.H"
  8. #include "include\GL\glut.h"
  9. #include <stdbool.h>
  10.  
  11. // CONFIGURATION
  12.  
  13. bool rotateAroundWindowCentre = true;
  14. bool rotateAroundMassCentre = true;
  15. bool changeDistanceFromWindowCentre = false;
  16.  
  17. // CONFIGURATION
  18.  
  19.  
  20. GLfloat rotation = 0;
  21. GLfloat rotationMassCentre = 0;
  22.  
  23. void redisplay(int time) {
  24.     glutPostRedisplay();
  25. }
  26.  
  27. void DrawTriangle(void) {
  28.     //glRotatef(rotation * 0.01f, .0f, .0f, 1.0f);
  29.     glBegin(GL_POLYGON);// OpenGL’a state
  30.     glColor4f(1.0f, 0.0f, 0.0f, 1.0f);//Red;
  31.     glVertex2f(0.0f, 0.0f);
  32.     glColor4f(0.0f, 1.0f, 0.0f, 1.0f);//Green
  33.     glVertex2f(60.0f, 0.0f);
  34.     glColor4f(0.0f, 0.0f, 1.0f, 1.0f);//Blue
  35.     glVertex2f(0.0f, 60.0f);
  36.     glEnd();
  37. }
  38.  
  39. void resetMatrix() {
  40.     glPopMatrix();
  41.     glPushMatrix();
  42. }
  43.  
  44. void rotateCentre() {
  45.     if (rotateAroundMassCentre) {
  46.         glTranslatef(20.0f, 20.0f, 0.0f);
  47.         glRotatef(rotationMassCentre, 0.0f, 0.0f, 1.0f);
  48.         glTranslatef(-20.0f, -20.0f, 0.0f);
  49.     }
  50. }
  51.  
  52. void drawFirstSeriesOfTriangles() {
  53.     glPushMatrix();
  54.     for (int i = 0; i < 4; ++i) {
  55.         glPushMatrix();
  56.         rotateCentre();
  57.         DrawTriangle();
  58.         glPopMatrix();
  59.         glRotatef(90.0f, 0.0f, 0.0f, 1.0f);
  60.     }
  61.     glPopMatrix();
  62. }
  63.  
  64. void drawSecondSeriesOfTriangles() {
  65.     glPushMatrix();
  66.     for (int i = 0; i < 4; ++i) {
  67.  
  68.         glRotatef((i) * 90.0f, 0.0f, 0.0f, 1.0f);
  69.  
  70.         // left side triangle
  71.         glPushMatrix();
  72.         glTranslatef(100.0f, 0.0f, 0.0f);
  73.         rotateCentre();
  74.         DrawTriangle();
  75.  
  76.         // right side triangle
  77.         glPopMatrix();
  78.         glTranslatef(0.0f, 100.0f, 0.0f);
  79.         rotateCentre();
  80.         DrawTriangle();
  81.  
  82.         resetMatrix();
  83.     }
  84.     glPopMatrix();
  85. }
  86.  
  87. void drawThirdSeriesOfTriangles() {
  88.     glPushMatrix();
  89.     for (int i = 0; i < 4; ++i) {
  90.         glRotatef((i) * 90.0f, 0.0f, 0.0f, 1.0f);
  91.         // right triangle
  92.         glTranslatef(200.0f, 0.0f, 0.0f);
  93.         glPushMatrix();
  94.         rotateCentre();
  95.         DrawTriangle();
  96.         glPopMatrix();
  97.         // middle triangle
  98.         glTranslatef(-100.0f, 100.0f, 0.0f);
  99.         glPushMatrix();
  100.         rotateCentre();
  101.         DrawTriangle();
  102.         glPopMatrix();
  103.         // left triangle
  104.         glTranslatef(-100.0f, 100.0f, 0.0f);
  105.         glPushMatrix();
  106.         rotateCentre();
  107.         DrawTriangle();
  108.         glPopMatrix();
  109.  
  110.         resetMatrix();
  111.     }
  112.     glPopMatrix();
  113. }
  114.  
  115. void MyDisplay(void) {
  116.     glLoadIdentity(); // does not work with it
  117.     // The new scene
  118.     glClear(GL_COLOR_BUFFER_BIT);
  119.  
  120.     glPushMatrix();
  121.  
  122.     if (rotateAroundWindowCentre)
  123.         glRotatef(rotation * 0.01f, 0.0f, 0.0f, 1.0f);
  124.  
  125.     drawFirstSeriesOfTriangles();
  126.     drawSecondSeriesOfTriangles();
  127.     drawThirdSeriesOfTriangles();
  128.  
  129.     glPopMatrix();
  130.  
  131.     // The end of scene
  132.     glFlush();//start processing buffered OpenGL routines
  133.  
  134.     ++rotation;
  135.     rotationMassCentre += 0.03;
  136.  
  137.     // call MyDisplay function
  138.     glutPostRedisplay();
  139.     //glutTimerFunc(1000, redisplay, 0);
  140. }
  141.  
  142. void MyInit(void) {
  143.     glClearColor(0.0, 0.0, 0.0, 0.0); //select clearing (background) color
  144.                                      /* initialize viewing values */
  145.     glViewport(0, 0, 300, 300); //window origin and size
  146.     glMatrixMode(GL_PROJECTION);
  147.     glLoadIdentity();//=1
  148.     gluOrtho2D(-350.0, 350.0, -350.0, 350.0);
  149.     glMatrixMode(GL_MODELVIEW);
  150.     glLoadIdentity();//=1
  151. }
  152.  
  153. int main(int argc, char** argv) { //<- for normal API
  154.     glutInit(&argc, argv);
  155.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);//single buffer and RGBA
  156.     glutInitWindowSize(500, 500);//initial window size
  157.     glutInitWindowPosition(100, 100);
  158.     glutCreateWindow("My window");//create widnow, hello title bar
  159.     MyInit();
  160.     glutDisplayFunc(MyDisplay);//
  161.     glutMainLoop();//enter main loop and process events
  162.     return 0;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement