CzarnyBarszcz

30.5_gotowe

Jun 6th, 2021
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.46 KB | None | 0 0
  1. #include <windows.h>
  2.  
  3. #ifdef __APPLE__
  4. #include <GLUT/glut.h>
  5. #else
  6. #include <GL/glut.h>
  7. #endif
  8. #include <iostream>
  9. #include <fstream>
  10. #include <string>
  11. #include <stdlib.h>
  12.  
  13. using namespace std;
  14.  
  15. static int slices = 16;
  16. static int stacks = 16;
  17.  
  18. double dane[25][25];
  19. double maxDane=-1000;
  20. double minDane=1000;
  21. double skalaZ=0;
  22. double katx = 0;
  23. double katy = 0;
  24.  
  25. double u_x=0;
  26. double u_y=0;
  27.  
  28.  
  29. int czytajDane()
  30. {
  31. double x,y;
  32. ifstream plik("C:\\numeryczne\\wizualizacja\\5.dat");
  33. for(int i=0;i<25;i++)
  34. for(int j=0;j<25;j++)
  35. plik>>x>>y>>dane[i][j];
  36. plik.close();
  37. for(int i=0;i<25;i++)
  38. for(int j=0;j<25;j++)
  39. {
  40. if(maxDane<dane[i][j]) maxDane=dane[i][j];
  41. if(minDane>dane[i][j]) minDane=dane[i][j];
  42. }
  43. cout<<"Min = "<<minDane<<"Max = "<<maxDane<<endl;
  44. skalaZ = (maxDane-minDane+1)/10.;
  45. return 0;
  46. }
  47.  
  48. static void resize(int width, int height)
  49. {
  50. const float ar = (float) width / (float) height;
  51.  
  52. glViewport(0, 0, width, height);
  53. glMatrixMode(GL_PROJECTION);
  54. glLoadIdentity();
  55. glOrtho(-ar*25, ar*25, -1.0*25, 1.0*25, 2.0, 100.0);
  56.  
  57. glMatrixMode(GL_MODELVIEW);
  58. glLoadIdentity() ;
  59. }
  60. void NapisV(const double& sx, const double& sy,const double& sz, const string& liczba)
  61. {
  62. glTranslated(0,-5,-12.5);
  63. glScalef(sx,sy,sz);
  64. glColor3d(1,1,1);
  65. glLineWidth(2);
  66. for (int i = 0; i < liczba.length(); i++)
  67. {
  68. glutStrokeCharacter(GLUT_STROKE_ROMAN, liczba[i]);
  69. }
  70. }
  71.  
  72. void Napis(const double& x, const double& y,const double& z, const string& liczba)
  73. {
  74.  
  75. glRasterPos3d(x, y,z);
  76. for (int i = 0; i < liczba.length(); i++)
  77. {
  78. glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, liczba[i]);
  79. }
  80. }
  81. static void display(void)
  82. {
  83. const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
  84. const double a = t*90.0;
  85.  
  86. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  87. glColor3d(1,0,0);
  88.  
  89. glPushMatrix();
  90. glTranslated(0,-10,-50);
  91. glRotated(katx,1,0,0);
  92. glRotated(katy,0,1,0);
  93. for(int i=0;i<25;i++)
  94. for(int j=0;j<25;j++)
  95. {
  96.  
  97. double skalaK=1/10.*(dane[i][j]-minDane)/skalaZ-0.5;
  98. double zielony=skalaK;
  99. if(skalaK>0.5)zielony=1-skalaK;
  100. zielony*=2;
  101. skalaK*=2;
  102. zielony =0;
  103. // glColor3f(i/24.,j/24.,0);
  104. glColor3d(skalaK,1-skalaK,zielony);
  105. glPushMatrix();
  106. glTranslated(-12+i,0,-12+j);
  107. glTranslated(0,((dane[i][j]+1-minDane)/skalaZ)/2.,0);
  108. glScaled(1,((dane[i][j]+1-minDane)/skalaZ),1);
  109. glutSolidCube(1);
  110. glPopMatrix();
  111.  
  112. }
  113. glBegin( GL_LINES );
  114. glVertex3d(-15,-2,-12.5 );
  115. glVertex3d(-15,12,-12.5 );
  116. glEnd();
  117.  
  118. glBegin( GL_LINES );
  119. glVertex3d(-15,12,-12.5 );
  120. glVertex3d(-14.5,11,-12.5 );
  121. glEnd();
  122.  
  123. glBegin( GL_LINES );
  124. glVertex3d(-15,12,-12.5 );
  125. glVertex3d(-15.5,11,-12.5 );
  126. glEnd();
  127.  
  128.  
  129.  
  130. glBegin( GL_LINES );
  131. glVertex3d(-15,-2,-12.5 );
  132. glVertex3d(17,-2,-12.5 );
  133. glEnd();
  134.  
  135. glBegin( GL_LINES );
  136. glVertex3d(17,-2,-12.5 );
  137. glVertex3d(16,-2.5,-12.5 );
  138. glEnd();
  139.  
  140. glBegin( GL_LINES );
  141. glVertex3d(17,-2,-12.5 );
  142. glVertex3d(16,-1.5,-12.5 );
  143. glEnd();
  144. glPushMatrix();
  145. glTranslated(-21,17,-12.5);
  146. NapisV(0.01,0.01,0.01,"y [m]");
  147. glPopMatrix();
  148. glPushMatrix();
  149. glTranslated(17,5,-12.5);
  150. NapisV(0.01,0.01,0.01,"x [m]");
  151. glPopMatrix();
  152.  
  153. glPopMatrix();
  154.  
  155. Napis(-20,20,-12,"Wykres slupkowy");
  156.  
  157. Napis(20,10,-12,"Legenda");
  158. string md = to_string(maxDane);
  159. Napis(22,7.3,-12,md);
  160. glPushMatrix();
  161. glTranslated(20,8,-12);
  162. glColor3d(1,0,0);
  163. glutSolidCube(2);
  164. glPopMatrix();
  165. md = to_string(minDane);
  166. Napis(22,5.0,-12,md);
  167. glPushMatrix();
  168. glTranslated(20,5,-12);
  169. glColor3d(0,1,0);
  170. glutSolidCube(2);
  171. glPopMatrix();
  172. /*
  173. glPopMatrix();
  174. glTranslated(20,10,-12);
  175. glColor3d(1,0,0);
  176. glutSolidCube(1);
  177. glPushMatrix();
  178.  
  179. */
  180. glutSwapBuffers();
  181. }
  182.  
  183.  
  184. static void key(unsigned char key, int x, int y)
  185. {
  186. switch (key)
  187. {
  188. case 27 :
  189. case 'q':
  190. exit(0);
  191. break;
  192.  
  193. case '+':
  194. slices++;
  195. stacks++;
  196. break;
  197.  
  198. case '-':
  199. if (slices>3 && stacks>3)
  200. {
  201. slices--;
  202. stacks--;
  203. }
  204. break;
  205. case 'd':
  206. u_x+=1;
  207. cout<<"X : "<<u_x<<endl;
  208. break;
  209. case 'a':
  210. u_x-=1;
  211. cout<<"X : "<<u_x<<endl;
  212. break;
  213. case 'w':
  214. u_y+=1;
  215. cout<<"Y : "<<u_y<<endl;
  216. break;
  217. case 's':
  218. u_y-=1;
  219. cout<<"Y : "<<u_y<<endl;
  220. break;
  221. }
  222.  
  223. glutPostRedisplay();
  224. }
  225.  
  226. static void idle(void)
  227. {
  228. glutPostRedisplay();
  229. }
  230.  
  231. const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
  232. const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  233. const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  234. const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
  235.  
  236. const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
  237. const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
  238. const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  239. const GLfloat high_shininess[] = { 100.0f };
  240.  
  241. /* Program entry point */
  242. void SpecialKeys( int key, int x, int y )
  243. {
  244. switch( key )
  245. {
  246. case GLUT_KEY_LEFT:
  247. katy+=1;
  248. break;
  249. case GLUT_KEY_RIGHT:
  250. katy-=1;
  251. break;
  252. case GLUT_KEY_UP:
  253. katx+=1;
  254. break;
  255. case GLUT_KEY_DOWN:
  256. katx-=1;
  257. break;
  258. }
  259.  
  260. resize( glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT ) );
  261. }
  262.  
  263. int main(int argc, char *argv[])
  264. {
  265. cout<<"Sterowanie gora(KEY_UP) dol(KEY_DOWN) "<<endl;
  266. cout<<"Sterowanie prawo(KEY_RIGHT) lewo (KEY_LEFT) "<<endl;
  267.  
  268. czytajDane();
  269. glutInit(&argc, argv);
  270. glutInitWindowSize(640,480);
  271. glutInitWindowPosition(10,10);
  272. //glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  273. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  274.  
  275. glutCreateWindow("GLUT Shapes");
  276.  
  277. glutReshapeFunc(resize);
  278. glutDisplayFunc(display);
  279. glutKeyboardFunc(key);
  280. glutSpecialFunc(SpecialKeys);
  281. glutIdleFunc(idle);
  282.  
  283. glClearColor(1,1,1,1);
  284. glEnable(GL_CULL_FACE);
  285. glCullFace(GL_BACK);
  286.  
  287. glEnable(GL_DEPTH_TEST);
  288. //glDepthFunc(GL_LESS);
  289. glDepthFunc(GL_LINES);
  290.  
  291. glEnable(GL_LIGHT0);
  292. glEnable(GL_NORMALIZE);
  293. glEnable(GL_COLOR_MATERIAL);
  294. glEnable(GL_LIGHTING);
  295.  
  296. glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  297. glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  298. glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  299. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  300.  
  301. glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  302. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  303. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  304. glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  305.  
  306. glutMainLoop();
  307.  
  308. return EXIT_SUCCESS;
  309. }
  310.  
Advertisement
Add Comment
Please, Sign In to add comment