Advertisement
CzarnyBarszcz

lab18.04

Apr 18th, 2021
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. /*
  2. * GLUT Shapes Demo
  3. *
  4. * Written by Nigel Stewart November 2003
  5. *
  6. * This program is test harness for the sphere, cone
  7. * and torus shapes in GLUT.
  8. *
  9. * Spinning wireframe and smooth shaded shapes are
  10. * displayed until the ESC or q key is pressed. The
  11. * number of geometry stacks and slices can be adjusted
  12. * using the + and - keys.
  13. */
  14. #include <Windows.h>
  15.  
  16.  
  17. #ifdef __APPLE__
  18. #include <GLUT/glut.h>
  19. #else
  20. #include <GL/glut.h>
  21. #endif
  22.  
  23. #include <stdlib.h>
  24. #include <string>
  25. #include <iostream>
  26. #include <fstream>
  27. #include <istream>
  28.  
  29. using namespace std;
  30.  
  31.  
  32. static int slices = 16;
  33. static int stacks = 16;
  34.  
  35. /* GLUT callback Handlers */
  36.  
  37. static void resize(int width, int height)
  38. {
  39. const float ar = (float) width / (float) height;
  40.  
  41. glViewport(0, 0, width, height);
  42. glMatrixMode(GL_PROJECTION);
  43. glLoadIdentity();
  44. glOrtho(-14*ar,14*ar, -14,14,0,20);
  45. glMatrixMode(GL_MODELVIEW);
  46. glLoadIdentity() ;
  47. }
  48.  
  49. struct wezel
  50. {
  51. double x;
  52. double y;
  53. double z;
  54. };
  55. const int nW=6;
  56. const int nE=5;
  57. wezel w[nW];
  58.  
  59. struct element
  60. {
  61. int lW; //liczba wezlow
  62. int nrO; // nr obiektu
  63. int w[4];
  64. };
  65. element e[nE];
  66.  
  67. /*
  68. int czytanie(const string & nazwaplik)
  69. {
  70. int lWez, lEle;
  71. int pomI;
  72. ifstream plik(nazwaplik.c_str());
  73. plik>>lWez;
  74. if(lWez!=nW) cout<<"Popraw w programie liczbe wezlow"<<endl;
  75. for(int i=0; i<nW; i++)
  76. {
  77. plik>>pomI>>w[i].x>>w[i].y>>w[i].z;
  78. }
  79. plik>>lEle;
  80. if(lEle!=nE) cout<<"Popraw w programie liczbe elementow obiektu 1"<<endl;
  81. for(int i=0; i<nE; i++)
  82. {
  83. plik>>pomI;
  84. plik>>e[i].lW;
  85. for(int j=0;j<e[i].lW;j++)
  86. {
  87. plik>>e[i].w[j];
  88. }
  89. plik>>e[i].nrO;
  90. }
  91. plik.close();
  92.  
  93. return 0;
  94. }
  95.  
  96.  
  97. */
  98. int czytanie(const string & nazwaPlik)
  99. {
  100. int lWez,lEle;
  101. int pomI;
  102. ifstream plik(nazwaPlik.c_str());
  103. plik>>lWez;
  104. if(lWez!=nW) cout<<"popraw w programie liczbe wezlow"<<endl;
  105. for(int i=0;i<nW;i++)
  106. {
  107. plik>>pomI>>w[i].x>>w[i].y>>w[i].z;
  108. }
  109. plik>>lEle;
  110. if(lEle!=nE) cout<<"popraw w programie liczbe elementow obiektu 1"<<endl;
  111. for(int i=0;i<nE;i++)
  112. {
  113. plik>>pomI;
  114. plik>>e[i].lW;
  115. for(int j=0;j<e[i].lW;j++)
  116. plik>>e[i].w[j];
  117. plik>>e[i].nrO;
  118. }
  119. plik.close();
  120. return 0;
  121. }
  122.  
  123. static void display(void)
  124. {
  125. const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
  126. const double a = t*90.0;
  127.  
  128. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  129. glColor3d(1,0,0);
  130. glPushMatrix();
  131. glTranslated(-2.4,1.2,-6);
  132. glRotated(a,0.5,1,0.2);
  133. for(int i=0;i<nE;i++)
  134. {
  135. glColor3d(0.5/i,0,i/10.0);
  136. if(e[i].lW==3)
  137. {
  138. glBegin(GL_TRIANGLES);
  139. for(int j=0;j<e[i].lW;j++)
  140. {
  141. glVertex3d(w[e[i].w[j]].x,w[e[i].w[j]].y,w[e[i].w[j]].z);
  142. }
  143. glEnd();
  144. }
  145. if(e[i].lW==4)
  146. {
  147. glBegin(GL_QUADS);
  148. for(int j=0;j<e[i].lW;j++)
  149. {
  150. cout<<e[i].w[j]<<endl;
  151. glVertex3d(w[e[i].w[j]].x,w[e[i].w[j]].y,w[e[i].w[j]].z);
  152. }
  153. glEnd();
  154. }
  155. }
  156. glPopMatrix();
  157.  
  158.  
  159.  
  160. glutSwapBuffers();
  161. }
  162.  
  163.  
  164. static void key(unsigned char key, int x, int y)
  165. {
  166. switch (key)
  167. {
  168. case 27 :
  169. case 'q':
  170. exit(0);
  171. break;
  172.  
  173. case '+':
  174. slices++;
  175. stacks++;
  176. break;
  177.  
  178. case '-':
  179. if (slices>3 && stacks>3)
  180. {
  181. slices--;
  182. stacks--;
  183. }
  184. break;
  185. }
  186.  
  187. glutPostRedisplay();
  188. }
  189.  
  190. static void idle(void)
  191. {
  192. glutPostRedisplay();
  193. }
  194.  
  195. const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
  196. const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  197. const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  198. const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
  199.  
  200. const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
  201. const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
  202. const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  203. const GLfloat high_shininess[] = { 100.0f };
  204.  
  205. /* Program entry point */
  206.  
  207. int main(int argc, char *argv[])
  208. {
  209. czytanie("c:\\tmp\\graniastoslup.txt");
  210. glutInit(&argc, argv);
  211. glutInitWindowSize(640,480);
  212. glutInitWindowPosition(10,10);
  213. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  214.  
  215. glutCreateWindow("GLUT Shapes");
  216.  
  217. glutReshapeFunc(resize);
  218. glutDisplayFunc(display);
  219. glutKeyboardFunc(key);
  220. //glutIdleFunc(idle);
  221.  
  222. glClearColor(1,1,1,1);
  223. glEnable(GL_CULL_FACE);
  224. glCullFace(GL_BACK);
  225.  
  226. glEnable(GL_DEPTH_TEST);
  227. glDepthFunc(GL_LESS);
  228.  
  229. glEnable(GL_LIGHT0);
  230. glEnable(GL_NORMALIZE);
  231. glEnable(GL_COLOR_MATERIAL);
  232. glEnable(GL_LIGHTING);
  233.  
  234. glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  235. glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  236. glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  237. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  238.  
  239. glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  240. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  241. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  242. glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  243.  
  244. glutMainLoop();
  245.  
  246. return EXIT_SUCCESS;
  247. }
  248.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement