Advertisement
Guest User

Untitled

a guest
May 21st, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.32 KB | None | 0 0
  1. //cieniowanie_003
  2.  
  3. #include <stdlib.h>
  4. #include <GL/glut.h>
  5. #include <math.h>
  6. #include <iostream>
  7. #include <string>
  8. #include <fstream>
  9.  
  10.  
  11.  
  12. using namespace std;
  13.  
  14.  
  15.  
  16. struct color
  17. {
  18.  
  19. float a,b,c;
  20.  
  21.  
  22.  
  23. /*
  24. color CMYnaRGB(color &CMY){
  25.  
  26. color RGB;
  27. RGB.a=1-CMY.a;
  28. RGB.b=1-CMY.b;
  29. RGB.c=1-CMY.c;
  30.  
  31. return RGB;
  32.  
  33. }
  34. */
  35. };
  36.  
  37.  
  38. class punkt{
  39.  
  40. public:
  41.  
  42. float x,y,wartosc;
  43. int id;
  44. color kolor;
  45.  
  46. };
  47.  
  48.  
  49. class dane : public punkt{
  50.  
  51. public:
  52.  
  53. punkt tab[192];
  54. float max,min,odc,skok,przedzial;
  55. bool paleta;
  56.  
  57.  
  58. void wczytaj(string nazwa_pliku){
  59.  
  60. ifstream plik;
  61. plik.open(nazwa_pliku.c_str());
  62. if(!plik.good())
  63. cout << "Blad wczytywania pliku... plik nie istnieje!" << endl;
  64. else{
  65. for(int i = 0; i<192; i++){
  66. plik>>tab[i].x;
  67. plik>>tab[i].y;
  68. plik>>tab[i].wartosc;
  69. tab[i].id=i;
  70. }
  71.  
  72. plik.close();
  73. }
  74. }
  75.  
  76. void MaxMin(){
  77.  
  78. min=10000,max=0;
  79. for(int i=0;i<192;i++)
  80. {
  81. if(tab[i].wartosc<min)
  82. min=tab[i].wartosc;
  83. if(tab[i].wartosc>max)
  84. max=tab[i].wartosc;
  85. }
  86.  
  87. przedzial=max-min;
  88. skok=przedzial/100;
  89.  
  90. }
  91.  
  92.  
  93. void PrzypiszKolor(){
  94.  
  95. float podzial;
  96. podzial=(max-min)/4;
  97.  
  98. float a,b;
  99. a=min;
  100. b=min+skok;
  101. float x=0.0,y=0.0,z=1.0;
  102.  
  103. for(int j=0;j<=100;j++)
  104. {
  105. for(int i=0;i<192;i++)
  106. {
  107. if(tab[i].wartosc>=a && tab[i].wartosc<=b)
  108. {
  109. tab[i].kolor.a=x;
  110. tab[i].kolor.b=y;
  111. tab[i].kolor.c=z;
  112. }
  113. }
  114. if(j<25)
  115. {
  116. y+=0.04;
  117. }
  118.  
  119. if(j>=25&&j<50)
  120. {
  121. z-=0.04;
  122. }
  123.  
  124. if(j>=50&&j<75)
  125. {
  126. x+=0.04;
  127. }
  128. if(j>=75&&j<100)
  129. {
  130. y-=0.04;
  131. }
  132.  
  133.  
  134. a=b;
  135. b+=skok;
  136. if(j==99)
  137. {b+=10;
  138. x=0;}
  139. }
  140.  
  141.  
  142. for (int i=0;i<192;i++)
  143. {
  144. tab[i].x/=10;
  145. tab[i].y/=100;
  146. tab[i].wartosc=-tab[i].wartosc;
  147. }
  148.  
  149. }
  150. void Rysuj(){
  151.  
  152. glBegin(GL_QUAD_STRIP);
  153.  
  154. glVertex2f(-1,0.2);
  155.  
  156. glVertex2f(-1,0);
  157.  
  158.  
  159. glVertex2f(0,0.2);
  160. glVertex2f(0,0);
  161.  
  162. glEnd();
  163.  
  164. glEnable( GL_BLEND );
  165. glBlendFunc( GL_ONE, GL_ONE );
  166.  
  167. glBegin(GL_QUAD_STRIP);
  168.  
  169. glColor3f(0.3,0.0,0.7); glVertex2f(-0.7,0.);
  170.  
  171. glColor3f(0.3,0.0,0.7); glVertex2f(-0.7,0);
  172.  
  173.  
  174. glColor3f(0.3,0.0,0.7); glVertex2f(0,0.7);
  175. glColor3f(0.3,0.0,0.7); glVertex2f(0,0);
  176.  
  177. glEnd();
  178.  
  179.  
  180.  
  181.  
  182.  
  183. for(int i=0;i<31;i++)
  184. {
  185. glBegin(GL_QUAD_STRIP);
  186. glColor3f(tab[i].kolor.a,tab[i].kolor.b,tab[i].kolor.c); glVertex2f(tab[i].x,tab[i].y);
  187.  
  188. glColor3f(tab[i+1].kolor.a,tab[i+1].kolor.b,tab[i+1].kolor.c); glVertex2f(tab[i+1].x,tab[i+1].y);
  189.  
  190. glColor3f(tab[i+32].kolor.a,tab[i+32].kolor.b,tab[i+32].kolor.c); glVertex2f(tab[i+32].x,tab[i+32].y);
  191.  
  192. glColor3f(tab[i+33].kolor.a,tab[i+33].kolor.b,tab[i+33].kolor.c); glVertex2f(tab[i+33].x,tab[i+33].y);
  193.  
  194. glEnd();
  195. }
  196.  
  197. for(int i=32;i<63;i++)
  198. {
  199. glBegin(GL_QUAD_STRIP);
  200. glColor3f(tab[i].kolor.a,tab[i].kolor.b,tab[i].kolor.c); glVertex2f(tab[i].x,tab[i].y);
  201.  
  202. glColor3f(tab[i+1].kolor.a,tab[i+1].kolor.b,tab[i+1].kolor.c); glVertex2f(tab[i+1].x,tab[i+1].y);
  203.  
  204. glColor3f(tab[i+32].kolor.a,tab[i+32].kolor.b,tab[i+32].kolor.c); glVertex2f(tab[i+32].x,tab[i+32].y);
  205.  
  206. glColor3f(tab[i+33].kolor.a,tab[i+33].kolor.b,tab[i+33].kolor.c); glVertex2f(tab[i+33].x,tab[i+33].y);
  207.  
  208. glEnd();
  209. }
  210.  
  211. for(int i=64;i<95;i++)
  212. {
  213. glBegin(GL_QUAD_STRIP);
  214. glColor3f(tab[i].kolor.a,tab[i].kolor.b,tab[i].kolor.c); glVertex2f(tab[i].x,tab[i].y);
  215.  
  216. glColor3f(tab[i+1].kolor.a,tab[i+1].kolor.b,tab[i+1].kolor.c); glVertex2f(tab[i+1].x,tab[i+1].y);
  217.  
  218. glColor3f(tab[i+32].kolor.a,tab[i+32].kolor.b,tab[i+32].kolor.c); glVertex2f(tab[i+32].x,tab[i+32].y);
  219.  
  220. glColor3f(tab[i+33].kolor.a,tab[i+33].kolor.b,tab[i+33].kolor.c); glVertex2f(tab[i+33].x,tab[i+33].y);
  221.  
  222. glEnd();
  223. }
  224.  
  225. for(int i=96;i<127;i++)
  226. {
  227. glBegin(GL_QUAD_STRIP);
  228. glColor3f(tab[i].kolor.a,tab[i].kolor.b,tab[i].kolor.c); glVertex2f(tab[i].x,tab[i].y);
  229.  
  230. glColor3f(tab[i+1].kolor.a,tab[i+1].kolor.b,tab[i+1].kolor.c); glVertex2f(tab[i+1].x,tab[i+1].y);
  231.  
  232. glColor3f(tab[i+32].kolor.a,tab[i+32].kolor.b,tab[i+32].kolor.c); glVertex2f(tab[i+32].x,tab[i+32].y);
  233.  
  234. glColor3f(tab[i+33].kolor.a,tab[i+33].kolor.b,tab[i+33].kolor.c); glVertex2f(tab[i+33].x,tab[i+33].y);
  235.  
  236. glEnd();
  237. }
  238.  
  239.  
  240.  
  241. for(int i=128;i<159;i++)
  242. {
  243. glBegin(GL_QUAD_STRIP);
  244. glColor3f(tab[i].kolor.a,tab[i].kolor.b,tab[i].kolor.c); glVertex2f(tab[i].x,tab[i].y);
  245.  
  246. glColor3f(tab[i+1].kolor.a,tab[i+1].kolor.b,tab[i+1].kolor.c); glVertex2f(tab[i+1].x,tab[i+1].y);
  247.  
  248. glColor3f(tab[i+32].kolor.a,tab[i+32].kolor.b,tab[i+32].kolor.c); glVertex2f(tab[i+32].x,tab[i+32].y);
  249.  
  250. glColor3f(tab[i+33].kolor.a,tab[i+33].kolor.b,tab[i+33].kolor.c); glVertex2f(tab[i+33].x,tab[i+33].y);
  251.  
  252. glEnd();
  253.  
  254.  
  255. }
  256. }
  257.  
  258.  
  259. /*
  260. color RGBnaCMY (){
  261.  
  262. this->kolor.a = 1 - this->kolor.a;
  263. this->kolor.b = 1 - this->kolor.b;
  264. this->kolor.c = 1 - this->kolor.c;
  265.  
  266. return this->kolor;
  267. }
  268. void wybierz_palete(){
  269.  
  270. cout<< "podaj czy paleta ma byc RGB(1) czy CMY(0)?" << endl;
  271. cin>>this->paleta;
  272. if (paleta == 0){
  273. for (int i = 0; i<192; i++){
  274. tab[i].kolor =
  275. }
  276. }
  277.  
  278. }*/
  279.  
  280. };
  281.  
  282. void Display() // funkcja generuj�ca scen� 3D
  283. {
  284. glClearColor( 0.0, 0.0, 0.0, 1.0 ); // kolor t�a - zawarto�� bufora koloru
  285.  
  286. glClear( GL_COLOR_BUFFER_BIT ); // czyszczenie bufora koloru
  287.  
  288. glColor3f( 0.0, 1.0, 0.0 ); // kolor rysowania
  289.  
  290. /* tu wywoluje sie wszystkie funkcje... */
  291.  
  292. string test = "dane_testowe.txt";
  293. dane d1;
  294. d1.wczytaj(test);
  295. d1.MaxMin();
  296. //d1.wybierz_palete();
  297. d1.PrzypiszKolor();
  298. d1.Rysuj();
  299.  
  300. glFlush();
  301.  
  302. glutSwapBuffers(); // zamiana bufor�w koloru
  303. }
  304.  
  305.  
  306.  
  307.  
  308. void Reshape( int width, int height )
  309. {
  310. Display(); // generowanie sceny 3D
  311. }
  312.  
  313.  
  314.  
  315. int main( int argc, char * argv[] )
  316. {
  317.  
  318.  
  319.  
  320. glutInit( & argc, argv ); // inicjalizacja biblioteki GLUT
  321.  
  322. glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB ); // inicjalizacja bufora ramki
  323.  
  324. glutInitWindowSize( 400, 400 ); // rozmiary g��wnego okna programu
  325.  
  326. glutCreateWindow( "Cieniowanie | Mieszanie | Przezroczystosc" ); // utworzenie g��wnego okna programu
  327.  
  328. glutDisplayFunc( Display ); // do��czenie funkcji generuj�cej scen� 3D
  329.  
  330. glutReshapeFunc( Reshape ); // do��czenie funkcji wywo�ywanej przy zmianie rozmiaru okna
  331.  
  332. glutMainLoop(); // wprowadzenie programu do obs�ugi p�tli komunikat�w
  333.  
  334. return 0;
  335.  
  336. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement