CzarnyBarszcz

lab_30.05

May 30th, 2021 (edited)
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.80 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,zielony,1-skalaK);
  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.  
  114. glPushMatrix();
  115. glTranslated(0,-5,-12.5);
  116. NapisV(0.01,0.01,0.01,"x [m]");
  117. glPopMatrix();
  118. glPushMatrix();
  119. glTranslated(17,5,-12.5);
  120. NapisV(0.01,0.01,0.01,"y [m]");
  121. glPopMatrix();
  122. glPushMatrix();
  123. glRotated(90,0,1,0);
  124. glTranslated(u_x,u_y,-12.5);
  125. NapisV(0.01,0.01,0.01,"z [m]");
  126. glPopMatrix();
  127.  
  128. glPopMatrix();
  129.  
  130. Napis(-20,20,-12,"Wykres slupkowy");
  131.  
  132. Napis(20,10,-12,"Legenda");
  133. string md = to_string(maxDane);
  134. Napis(22,7.3,-12,md);
  135. glPushMatrix();
  136. glTranslated(20,8,-12);
  137. glColor3d(0,0,1);
  138. glutSolidCube(2);
  139. glPopMatrix();
  140. md = to_string(minDane);
  141. Napis(22,5.0,-12,md);
  142. glPushMatrix();
  143. glTranslated(20,5,-12);
  144. glColor3d(1,0,0);
  145. glutSolidCube(2);
  146. glPopMatrix();
  147. /*
  148. glPopMatrix();
  149. glTranslated(20,10,-12);
  150. glColor3d(1,0,0);
  151. glutSolidCube(1);
  152. glPushMatrix();
  153.  
  154. */
  155. glutSwapBuffers();
  156. }
  157.  
  158.  
  159. static void key(unsigned char key, int x, int y)
  160. {
  161. switch (key)
  162. {
  163. case 27 :
  164. case 'q':
  165. exit(0);
  166. break;
  167.  
  168. case '+':
  169. slices++;
  170. stacks++;
  171. break;
  172.  
  173. case '-':
  174. if (slices>3 && stacks>3)
  175. {
  176. slices--;
  177. stacks--;
  178. }
  179. break;
  180. case 'd':
  181. u_x+=1;
  182. cout<<"X : "<<u_x<<endl;
  183. break;
  184. case 'a':
  185. u_x-=1;
  186. cout<<"X : "<<u_x<<endl;
  187. break;
  188. case 'w':
  189. u_y+=1;
  190. cout<<"Y : "<<u_y<<endl;
  191. break;
  192. case 's':
  193. u_y-=1;
  194. cout<<"Y : "<<u_y<<endl;
  195. break;
  196. }
  197.  
  198. glutPostRedisplay();
  199. }
  200.  
  201. static void idle(void)
  202. {
  203. glutPostRedisplay();
  204. }
  205.  
  206. const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
  207. const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  208. const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  209. const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
  210.  
  211. const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
  212. const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
  213. const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  214. const GLfloat high_shininess[] = { 100.0f };
  215.  
  216. /* Program entry point */
  217. void SpecialKeys( int key, int x, int y )
  218. {
  219. switch( key )
  220. {
  221. case GLUT_KEY_LEFT:
  222. katy+=1;
  223. break;
  224. case GLUT_KEY_RIGHT:
  225. katy-=1;
  226. break;
  227. case GLUT_KEY_UP:
  228. katx+=1;
  229. break;
  230. case GLUT_KEY_DOWN:
  231. katx-=1;
  232. break;
  233. }
  234.  
  235. resize( glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT ) );
  236. }
  237.  
  238. int main(int argc, char *argv[])
  239. {
  240. czytajDane();
  241. glutInit(&argc, argv);
  242. glutInitWindowSize(640,480);
  243. glutInitWindowPosition(10,10);
  244. //glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  245. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  246.  
  247. glutCreateWindow("GLUT Shapes");
  248.  
  249. glutReshapeFunc(resize);
  250. glutDisplayFunc(display);
  251. glutKeyboardFunc(key);
  252. glutSpecialFunc(SpecialKeys);
  253. glutIdleFunc(idle);
  254.  
  255. glClearColor(1,1,1,1);
  256. glEnable(GL_CULL_FACE);
  257. glCullFace(GL_BACK);
  258.  
  259. glEnable(GL_DEPTH_TEST);
  260. //glDepthFunc(GL_LESS);
  261. glDepthFunc(GL_LINES);
  262.  
  263. glEnable(GL_LIGHT0);
  264. glEnable(GL_NORMALIZE);
  265. glEnable(GL_COLOR_MATERIAL);
  266. glEnable(GL_LIGHTING);
  267.  
  268. glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  269. glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  270. glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  271. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  272.  
  273. glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  274. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  275. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  276. glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  277.  
  278. glutMainLoop();
  279.  
  280. return EXIT_SUCCESS;
  281. }
  282.  
Advertisement
Add Comment
Please, Sign In to add comment