Facebook
From Putrid Plover, 4 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 361
  1. #include <GL/gl.h>
  2. #include <GL/glut.h>
  3. #include <cstdlib>
  4. #include <ctime>
  5.  
  6. float randomFloat()
  7. {
  8.     return static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
  9. }
  10.  
  11. void drawSquare(const float x, const float y, const float size)
  12. {
  13.     glBegin(GL_POLYGON);
  14.     glColor3f(randomFloat(), randomFloat(), randomFloat());
  15.     glVertex2f(x, y);
  16.     glColor3f(randomFloat(), randomFloat(), randomFloat());
  17.     glVertex2f(x + size, y);
  18.     glColor3f(randomFloat(), randomFloat(), randomFloat());
  19.     glVertex2f(x + size, y - size);
  20.     glColor3f(randomFloat(), randomFloat(), randomFloat());
  21.     glVertex2f(x, y - size);
  22.     glEnd();
  23. }
  24.  
  25. void drawFractal(const int levelsCount, const float randomness, const float x, const float y, const float rectSize)
  26. {
  27.     float singleRectSize = rectSize / 3;
  28.  
  29.     for (int i = 0; i < 3; i++)
  30.     {
  31.         for (int j = 0; j < 3; j++)
  32.         {
  33.             if (i == 1 && j == 1)
  34.                 continue;
  35.  
  36.             if (levelsCount == 0)
  37.                 drawSquare(x + i * singleRectSize  + singleRectSize * randomFloat() * randomness, y - j * singleRectSize + singleRectSize * randomFloat() * randomness, singleRectSize);
  38.             else
  39.                 drawFractal(levelsCount - 1, randomness, x + i * singleRectSize + singleRectSize * randomFloat() * randomness, y - j * singleRectSize + singleRectSize * randomFloat() * randomness, singleRectSize);
  40.         }
  41.     }
  42. }
  43.  
  44. void renderScene()
  45. {
  46.     glClear(GL_COLOR_BUFFER_BIT);
  47.  
  48.     glColor3f(0.0F, 1.0F, 0.0F);
  49.  
  50.     int levelCount = 2;
  51.     float startX = -75.0F;
  52.     float startY = 75.0F;
  53.     float fractalSize = 150.0F;
  54.     float randomness = 0.05;
  55.  
  56.     drawFractal(levelCount, randomness, startX, startY, fractalSize);
  57.  
  58.     glFlush();
  59. }
  60.  
  61. void changeSize(int width, int height)
  62. {
  63.     if (height == 0)
  64.         height = 1;
  65.  
  66.     glViewport(0, 0, width, height);
  67.  
  68.     glMatrixMode(GL_PROJECTION);
  69.     glLoadIdentity();
  70.  
  71.     float aspectRatio = static_cast<float>(width) / static_cast<float>(height);
  72.  
  73.     if (width <= height)
  74.         glOrtho(-100.0, 100.0, -100.0 / aspectRatio, 100.0 / aspectRatio, 1.0, -1.0);
  75.     else
  76.         glOrtho(-100.0 * aspectRatio, 100.0 * aspectRatio, -100.0, 100.0, 1.0, -1.0);
  77.  
  78.     glMatrixMode(GL_MODELVIEW);
  79.     glLoadIdentity();
  80. }
  81.  
  82. void myInit()
  83. {
  84.     std::srand(std::time(nullptr));
  85.     glClearColor(0.5F, 0.5F, 0.5F, 1.0F);
  86. }
  87.  
  88. int main(int argc, char *argv[])
  89. {
  90.     glutInit(&argc, argv);
  91.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
  92.     glutCreateWindow("Dywan Sierpinskiego");
  93.  
  94.     glutDisplayFunc(renderScene);
  95.     glutReshapeFunc(changeSize);
  96.     myInit();
  97.  
  98.     glutMainLoop();
  99.  
  100.     return 0;
  101. }