Advertisement
Guest User

Kocha BobaV2

a guest
Oct 12th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <windows.h>
  4. #include <gl\gl.h>
  5. #include "freeglut.h"
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. GLfloat n; //stopień, liczba zagłębień
  10.  
  11. //Wymiary okna
  12. int screen_width = 640;
  13. int screen_height = 480;
  14.  
  15. //funkcja związana ze zdarzeniem resize
  16. void rozmiar(int width, int height)
  17. {
  18. screen_width = width;
  19. screen_height = height;
  20. //Wyczyszczenie okna aktualnym kolorem czyszczącym
  21. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  22. //Ustawienie widoku
  23. glViewport(0, 0, screen_width, screen_height);
  24.  
  25. //ustawienie układu współrzędnych
  26. glMatrixMode(GL_PROJECTION);
  27. glLoadIdentity(); //ponowne ustawienie - "reset" ukladu współrzędnych
  28.  
  29. gluPerspective(45.0f, (GLfloat)screen_width / (GLfloat)screen_height, 1.0f, 1000.0f);
  30. glutPostRedisplay(); // Przerysowanie sceny
  31. }
  32.  
  33.  
  34. void np(GLfloat odl)
  35. {
  36. glBegin(GL_LINES);
  37. glColor3f(1, 1, 1);
  38. glVertex2f(0, 0);
  39. glVertex2f(0, -odl);
  40. glEnd();
  41. glTranslatef(0, -odl, 0);
  42. };
  43. void ws(GLfloat odl)
  44. {
  45. np(-odl);
  46. };
  47. void pw(GLfloat kt)
  48. {
  49. glRotatef(kt, 0, 0, 1);
  50. };
  51. void lw(GLfloat kt)
  52. {
  53. pw(-kt);
  54. };
  55. // rekurencyjna krzywa Kocha
  56. void kKocha(GLfloat a, GLfloat b)
  57. {
  58. if (b < 2)
  59. {
  60. np(a);
  61. return;
  62. }
  63. kKocha(a / 3, b - 1);
  64. lw(60);
  65. kKocha(a / 3,b - 1);
  66. pw(120);
  67. kKocha(a / 3, b - 1);
  68. lw(60);
  69. kKocha(a / 3, b - 1);
  70. };
  71.  
  72. /**********************************************************
  73. Główna funkcja rysująca
  74. *********************************************************/
  75. void rysuj(void)
  76. {
  77.  
  78. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Kasowanie ekranu
  79. //ustwienie trybu przekształceń na model
  80. glMatrixMode(GL_MODELVIEW);
  81. glLoadIdentity();
  82. glTranslatef(0.0, 0.0, -10); // przesuniecie obiektu przed kamerę
  83. //glClearColor(0, 0, 0, 0);
  84.  
  85. //====================================Płatek Kocha (mnie)
  86. GLfloat bok = 3; //największy bok trójkąta
  87.  
  88. pw(30);
  89. for (int i = 1; i < 4; i+=1)
  90. {
  91. kKocha(bok, n);
  92. pw(120);
  93. }
  94.  
  95. glFlush(); // rysowanie w buforze
  96. glutSwapBuffers(); // Wysłanie na ekran
  97. }
  98.  
  99.  
  100.  
  101. int main(int argc, char **argv)
  102. {
  103. // funkcja inicjująca do pracy bibliotekę GLUT
  104. glutInit(&argc, argv);
  105. //określenie trybu rysowania
  106. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  107. glutInitWindowSize(screen_width, screen_height);
  108. glutInitWindowPosition(0, 0);
  109. glutCreateWindow("Zadania 2 gr1");
  110.  
  111. cout << "Wpisz stopien platku Kocha (liczbe zaglebien):" << endl;
  112. cin >> n;
  113.  
  114. glutDisplayFunc(rysuj); // def. funkcji rysującej tzw. funkcji zwrotnej
  115. glutReshapeFunc(rozmiar);// def. obsługi zdarzenia resize (GLUT)
  116.  
  117. // ustawienie renderowania
  118. glEnable(GL_DEPTH_TEST);
  119.  
  120. glutMainLoop();// start obsługi komunikatów
  121. return(0);
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement