Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.33 KB | None | 0 0
  1. #include "Run.h"
  2. #include <string>
  3.  
  4. Closet* closet = new Closet();
  5.  
  6. // For testing purpose
  7. Shelf shelf;
  8. Drawer drawer;
  9. Hanger hanger;
  10.  
  11. DrawFloor drawFloor;
  12. Estado estado;
  13. SetClosetMeasures setClosetMeasures;
  14. SelectComponent selectComponents;
  15. DrawUtils drawUtils;
  16. InputUtils inputUtils;
  17.  
  18. /*******************************
  19. *** init opengl model ***
  20. *******************************/
  21. void Init(void)
  22. {
  23.  
  24. srand((unsigned)time(NULL));
  25.  
  26. estado.menuActivo = GL_FALSE;
  27. estado.camera.eye.x = 40;
  28. estado.camera.eye.y = 40;
  29. estado.camera.eye.z = 40;
  30.  
  31. estado.camera.up.x = 0;
  32. estado.camera.up.y = 0;
  33. estado.camera.up.z = 0;
  34. estado.ortho = GL_TRUE;
  35. estado.camera.fov = 60;
  36. estado.camera.dir_long = 0;
  37. estado.camera.dir_lat = 0;
  38. closet->xMouse = closet->yMouse = -1;
  39.  
  40. glClearColor(0.0, 0.0, 0.0, 0.0);
  41.  
  42. glEnable(GL_POINT_SMOOTH);
  43. glEnable(GL_LINE_SMOOTH);
  44. glEnable(GL_POLYGON_SMOOTH);
  45. glEnable(GL_DEPTH_TEST);
  46. }
  47.  
  48. /*******************************
  49. *** callbacks de janela ***
  50. *******************************/
  51.  
  52. void Reshape(int width, int height)
  53. {
  54. glViewport(0, 0, (GLint)width, (GLint)height);
  55.  
  56. glMatrixMode(GL_PROJECTION);
  57. glLoadIdentity();
  58.  
  59. if (estado.ortho)
  60. {
  61. if (width < height)
  62. glOrtho(-20, 20, -20 * (GLdouble)height / width, 20 * (GLdouble)height / width, -100, 100);
  63. else
  64. glOrtho(-20 * (GLdouble)width / height, 20 * (GLdouble)width / height, -20, 20, -100, 100);
  65. }
  66. else
  67. gluPerspective(estado.camera.fov, (GLfloat)width / height, 1, 100);
  68.  
  69. glMatrixMode(GL_MODELVIEW);
  70. }
  71.  
  72.  
  73. void setCamera(Camera *cam) {
  74. Pos center;
  75.  
  76. glRotatef(-90, 0, 0, 1);
  77. //glRotatef(180, 0, 1,0);
  78.  
  79. //posicao de do olho
  80. cam->eye.x = -cos(cam->dir_long) * cos(cam->dir_lat);
  81. cam->eye.y = sin(cam->dir_lat);
  82. cam->eye.z = sin(cam->dir_long) * cos(cam->dir_lat);
  83.  
  84. //onde queremos olhar
  85. center.x = cam->eye.x - cos(cam->dir_long) * cos(cam->dir_lat);
  86. center.y = cam->eye.y + sin(cam->dir_lat);
  87. center.z = cam->eye.z + sin(cam->dir_long) * cos(cam->dir_lat);
  88. /*
  89. center.x = 0;
  90. center.y = 0.5;
  91. center.z = 0;*/
  92.  
  93. /*
  94. cam->eye.x = center.x - cos(cam->dir_long);
  95. cam->eye.y = center.y + 0.5;
  96. cam->eye.z = center.z + sin(cam->dir_long);*/
  97.  
  98. //O Z nao ta a deixar dar a volta para cima
  99. gluLookAt(cam->eye.x, cam->eye.y, cam->eye.z, center.x, center.y, center.z, 0, 1, 0);
  100. }
  101. /*******************************
  102. *** callbacks de desenho ***
  103. *******************************/
  104.  
  105. void Draw(void)
  106. {
  107. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  108.  
  109. glLoadIdentity();
  110.  
  111. /*gluLookAt(estado.camera.eye.x, estado.camera.eye.y, estado.camera.eye.z, \
  112. estado.camera.center.x, estado.camera.center.y, estado.camera.center.z, \
  113. estado.camera.up.x, estado.camera.up.y, estado.camera.up.z);*/
  114.  
  115. setCamera(&estado.camera);
  116.  
  117. /*Draw here*/
  118.  
  119. glColor3f(0.5f, 0.5f, 0.5f);
  120. drawFloor.draw(25);
  121.  
  122. glPushMatrix();
  123.  
  124. //Draw closet
  125. closet->draw();
  126. //t.draw();
  127. //Draw Shelf in the middle of the closet
  128.  
  129. //shelf.draw(closet->getWidth() - 2 * closet->getThickness(), closet->getHeight(), closet->getHeight());
  130.  
  131. //Draw Shelf in the middle of the closet
  132. //drawer.open();
  133. //drawer.close();
  134. //drawer.draw(closet->getWidth() - 2 * closet->getThickness(),( closet->getHeight() - 2 * closet->getThickness()) *0.4, closet->getDepth() - 2 * closet->getThickness());
  135. // Draw hanger
  136. /*hanger.setModules(0);
  137. hanger.setVertical_pos(1);
  138. hanger.draw(closet->getWidth() - 2 * closet->getThickness(), (closet->getHeight() - 2 * closet->getThickness()) *0.4, closet->getDepth() - 2 * closet->getThickness());*/
  139.  
  140. //Draw components
  141. closet->drawComponents();
  142.  
  143. /*Draw here*/
  144.  
  145. glPopMatrix();
  146.  
  147.  
  148. glFlush();
  149. if (estado.doubleBuffer)
  150. glutSwapBuffers();
  151. }
  152.  
  153.  
  154.  
  155. /*******************************
  156. *** callbacks timer/idle ***
  157. *******************************/
  158.  
  159. void Timer(int value)
  160. {
  161. glutTimerFunc(DELAY, Timer, 0);
  162. if (estado.menuActivo) return;
  163. glutPostRedisplay();
  164. }
  165.  
  166. /*******************************
  167. *** callbacks de teclado ***
  168. *******************************/
  169.  
  170. void Key(unsigned char key, int x, int y)
  171. {
  172. switch (key) {
  173.  
  174. case 27:
  175. exit(1);
  176. case '1':
  177. setClosetMeasures.readMeasures(closet);
  178. inputUtils.printMenu();
  179. break;
  180. case '2':
  181. selectComponents.addComponent(closet);
  182. inputUtils.printMenu();
  183. break;
  184. case '3':
  185. selectComponents.removeComponent(closet);
  186. inputUtils.printMenu();
  187. break;
  188. case '4':
  189. estado.ortho = !estado.ortho;
  190. Reshape(glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT));
  191. break;
  192. }
  193.  
  194.  
  195. }
  196.  
  197. void KeyUp(unsigned char key, int x, int y)
  198. {
  199. switch (key) {
  200.  
  201.  
  202. }
  203. }
  204.  
  205. void SpecialKey(int key, int x, int y)
  206. {
  207.  
  208. switch (key) {
  209.  
  210. }
  211. }
  212.  
  213. void SpecialKeyUp(int key, int x, int y)
  214. {
  215. switch (key) {
  216. }
  217.  
  218. }
  219.  
  220. void mouseMotion(int x, int y) {
  221. int dif = x - closet->xMouse;
  222. estado.camera.dir_lat -= dif * RAD(EYE_ROTACAO);
  223. //latitude
  224. dif = y - closet->yMouse;
  225. estado.camera.dir_long -= dif * RAD(EYE_ROTACAO);
  226.  
  227. //limitar dir_lat a +-rad(45)
  228. //if()
  229. //atualizar coordenadas do rato
  230. closet->xMouse = x;
  231. closet->yMouse = y;
  232.  
  233. }
  234.  
  235. void mouseNavigate(int button, int state, int x, int y)
  236. {
  237. //se o mouse2 carregado
  238. if (button == GLUT_RIGHT_BUTTON) {
  239. if (state == GLUT_DOWN) {
  240. closet->xMouse = x;
  241. closet->yMouse = y;
  242. //registar callback de movimento do rato
  243. glutMotionFunc(mouseMotion);
  244. }
  245. else glutMotionFunc(NULL);
  246. }
  247. }
  248.  
  249. /*******************************
  250. *** run the program ***
  251. *******************************/
  252. int main(int argc, char **argv)
  253. {
  254. char str[] = " makefile MAKEFILE Makefile ";
  255. estado.doubleBuffer = 1;
  256.  
  257. glutInit(&argc, argv);
  258. glutInitWindowPosition(0, 0);
  259. glutInitWindowSize(640, 480);
  260. glutInitDisplayMode(((estado.doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE) | GLUT_RGB | GLUT_DEPTH);
  261. if (glutCreateWindow("Closet Designer") == GL_FALSE)
  262. exit(1);
  263.  
  264. Init();
  265.  
  266. inputUtils.printMenu();
  267.  
  268. // Registar callbacks do GLUT
  269.  
  270. // callbacks de janelas/desenho
  271. glutReshapeFunc(Reshape);
  272. glutDisplayFunc(Draw);
  273.  
  274. // Callbacks de teclado
  275. glutKeyboardFunc(Key);
  276. glutKeyboardUpFunc(KeyUp);
  277. glutSpecialFunc(SpecialKey);
  278. glutSpecialUpFunc(SpecialKeyUp);
  279.  
  280. // Callbacks do rato
  281. glutMouseFunc(mouseNavigate);
  282.  
  283. // callbacks timer/idle
  284. glutTimerFunc(DELAY, Timer, 0);
  285.  
  286. // COMECAR...
  287. glutMainLoop();
  288. return 0;
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement