Guest User

Untitled

a guest
Apr 26th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.65 KB | None | 0 0
  1. // aulas.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include <GL/glui.h>
  5.  
  6. #include <math.h>
  7. #include "RGBpixmap.h"
  8. #include "Board.h"
  9. #include "Piece.h"
  10.  
  11. #define DIMX 500
  12. #define DIMY 500
  13. #define INITIALPOS_X 100
  14. #define INITIALPOS_Y 100
  15.  
  16. #define BUFSIZE 512
  17.  
  18.  
  19. #define mesaList 1
  20.  
  21. float xy_aspect;        // aspect ratio da area de visualizacao
  22. int window_w=DIMX;
  23. int window_h=DIMY;
  24.  
  25. // picking
  26. GLuint selectBuf[BUFSIZE];
  27.  
  28. float view_rotate[16] = { 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 };
  29. float obj_pos[] = { 0.0, 0.0, 0.0 };
  30.  
  31.  
  32. // variaveis globais
  33. int main_window;
  34. GLUI *glui, *glui2, *glui3;
  35.  
  36. float mat1_shininess[] = {20.0};
  37. float mat1_specular[] = {0.8, 0.8, 0.8, 1.0}; /* specular reflection. */
  38. float mat1_diffuse[] = {1.0, 0.0, 0.0, 1.0}; /* diffuse reflection. */
  39.  
  40. float mat2_shininess[] = {1.0};
  41. float mat2_specular[] = {0.4, 0.4, 0.4, 1.0}; /* specular reflection. */
  42. float mat2_diffuse[] = {0.3, 0.30, 0.30, 1.0}; /* diffuse reflection. */
  43.  
  44. RGBpixmap pix[2];
  45. GLUquadric* glQ;
  46.  
  47. //Movement
  48. int coord[2];
  49. bool can = false;
  50.  
  51. Board* tabuleiro;
  52. Piece* piece;
  53.  
  54. enum stat_maq {
  55.     pieces, cells, moves
  56. };
  57.  
  58. enum who{
  59.     player1, player2
  60. };
  61.  
  62. stat_maq move = pieces;
  63. who turn = player1;
  64.  
  65. void drawScene(GLenum mode)
  66. {
  67.     glFrustum( -xy_aspect*.08, xy_aspect*.08, -.07, .07, .1, 200.0 );
  68.  
  69.     glMatrixMode( GL_MODELVIEW );
  70.     glLoadIdentity();
  71.  
  72.     glTranslated(0.0,0.0,-30.0);
  73.     glRotated(45.0, 1.0,0.0,0.0 );
  74.     glTranslatef( obj_pos[0], obj_pos[1], -obj_pos[2] );
  75.     glMultMatrixf( view_rotate );
  76.    
  77.     if (mode == GL_SELECT)
  78.         glPushName (0);
  79.  
  80.     piece->buildPiece(mode,glQ);
  81.     tabuleiro->drawBoard(mode);
  82.     if(can)
  83.         tabuleiro->hitZone(coord[0],coord[1]);
  84.    
  85.  
  86.     glDisable(GL_COLOR_MATERIAL);
  87.  
  88.     glPopName();
  89. }
  90.  
  91.  
  92.  
  93. void display(void) {
  94.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  95.     glPolygonMode(GL_FRONT, GL_FILL);
  96.     glMatrixMode (GL_PROJECTION);
  97.     glLoadIdentity();
  98.    
  99.     drawScene(GL_RENDER);
  100.  
  101.     // swapping the buffers causes the rendering above to be shown
  102.     glutSwapBuffers();
  103.    
  104.     glFlush();
  105. }
  106.  
  107. // ACÇÃO DO PICKING
  108. void pickingAction(GLuint answer) {
  109.  
  110.     int x,z;
  111.  
  112.     if(answer > 64 && move == pieces)
  113.     {  
  114.         x = (piece->getX()/2) + 1;
  115.         z = piece->getZ()/2;
  116.         cout << "Piece coords: ";
  117.         cout << z << " " << x << endl;
  118.         coord[0] = x;
  119.         coord[1] = z;
  120.         can = true;
  121.         move = cells;
  122.     }
  123.     else if(answer <= 64 && move == cells){
  124.         x = answer % 8;
  125.         z = answer / 8;
  126.  
  127.         if(x == 0)
  128.             x = 8;
  129.         else
  130.             z += 1;
  131.  
  132.     cout << x << " " << z << endl;
  133.  
  134.     piece->setCoords(2*(x-1),z*2);
  135.     can = false;
  136.     move = pieces;
  137.  
  138.     }
  139.     else{
  140.         printf("%d\n", answer);
  141.         can = false;
  142.         move = pieces;
  143.     }
  144. }
  145.  
  146. // processa os hits no picking
  147. void processHits (GLint hits, GLuint buffer[]) {
  148.     GLuint *ptr = buffer;
  149.     GLuint mindepth = 0xFFFFFFFF;
  150.     GLuint *answer=NULL;
  151.     GLuint nn;
  152.  
  153.     for (int i=0;i<hits;i++) {
  154.         int num = *ptr; ptr++;
  155.         GLuint z1 = *ptr; ptr++;
  156.         ptr++;
  157.         if (z1 < mindepth && num>0) {
  158.             mindepth = z1;
  159.             answer = ptr;
  160.             nn=num;
  161.         }
  162.         for (int j=0; j < num; j++)
  163.             ptr++;
  164.     }
  165.    
  166.     // existe uma resposta
  167.     if (answer!=NULL)
  168.         pickingAction(*answer);
  169.    
  170. }
  171.  
  172. struct g_mouseState{
  173.     bool leftButton;
  174.     bool rightButton;
  175.     bool middleButton;
  176.     int x;
  177.     int y;
  178. } MouseState;
  179.  
  180. /* Mouse handling */
  181. void processMouse(int button, int state, int x, int y) {
  182.     GLint hits;
  183.     GLint viewport[4];
  184.  
  185.     // update our button state
  186.     if(button == GLUT_LEFT_BUTTON) {
  187.         if(state == GLUT_DOWN)
  188.             MouseState.leftButton = true;
  189.         else
  190.             MouseState.leftButton = false;
  191.     }
  192.     if(button == GLUT_RIGHT_BUTTON) {
  193.         if(state == GLUT_DOWN)
  194.             MouseState.rightButton = true;
  195.         else
  196.             MouseState.rightButton = false;
  197.     }
  198.     if(button == GLUT_MIDDLE_BUTTON) {
  199.         if(state == GLUT_DOWN)
  200.             MouseState.middleButton = true;
  201.         else
  202.             MouseState.middleButton = false;
  203.     }
  204.  
  205.     // update our position so we know a delta when the mouse is moved
  206.     MouseState.x = x;
  207.     MouseState.y = y;
  208.        
  209.     if (MouseState.leftButton && !MouseState.rightButton && !MouseState.middleButton) {
  210.         /* obrigatorio para o picking */
  211.         // obter o viewport actual
  212.         glGetIntegerv(GL_VIEWPORT, viewport);
  213.  
  214.         glSelectBuffer (BUFSIZE, selectBuf);
  215.         glRenderMode (GL_SELECT);
  216.  
  217.         // inicia processo de picking
  218.         glInitNames();
  219.         glMatrixMode (GL_PROJECTION);
  220.         glPushMatrix ();
  221.  
  222.         //  cria uma região de 5x5 pixels em torno do click do rato para o processo de picking
  223.         glLoadIdentity ();
  224.         gluPickMatrix ((GLdouble) x, (GLdouble) (window_h - y), 1.0, 1.0, viewport);
  225.        
  226.         drawScene(GL_SELECT);
  227.         glMatrixMode (GL_PROJECTION);
  228.         glPopMatrix ();
  229.         glFlush ();
  230.  
  231.         hits = glRenderMode(GL_RENDER);
  232.         processHits(hits, selectBuf);
  233.     }
  234. }
  235.  
  236.  
  237. void processMouseMoved(int x, int y)
  238. {  
  239.  
  240. }
  241.  
  242. void processPassiveMouseMoved(int x, int y)
  243. {
  244.  
  245.     // pedido de refrescamento da janela
  246.     glutPostRedisplay();               
  247. }
  248.  
  249.  
  250.  
  251. void reshape(int w, int h)
  252. {
  253.     int tx, ty, tw, th;
  254.  
  255.     window_w = w;   //variaveis globais; window_h e' usado em processMouse()
  256.     window_h = h;
  257.  
  258.     GLUI_Master.get_viewport_area( &tx, &ty, &tw, &th );
  259.     glViewport( tx, ty, tw, th );
  260.     // ver, nos manuais, a funcao GLUI_Master.auto_set_viewport()
  261.  
  262.     xy_aspect = (float)tw / (float)th;
  263.  
  264.     glutPostRedisplay();
  265. }
  266.  
  267.  
  268. void keyboard(unsigned char key, int x, int y)
  269. {
  270.    switch (key) {
  271.       case 27:      // tecla de escape termina o programa
  272.          exit(0);
  273.          break;
  274.    }
  275. }
  276.  
  277.  
  278. void myGlutIdle( void )
  279. {
  280.   /* According to the GLUT specification, the current window is
  281.      undefined during an idle callback.  So we need to explicitly change
  282.      it if necessary */
  283.   if ( glutGetWindow() != main_window )
  284.     glutSetWindow(main_window);  
  285.  
  286.  
  287.   glutPostRedisplay();
  288.  
  289.   /****************************************************************/
  290.   /*            This demonstrates GLUI::sync_live()               */
  291.   /*   We change the value of a variable that is 'live' to some   */
  292.   /*   control.  We then call sync_live, and the control          */
  293.   /*   associated with that variable is automatically updated     */
  294.   /*   with the new value.  This frees the programmer from having */
  295.   /*   to always remember which variables are used by controls -  */
  296.   /*   simply change whatever variables are necessary, then sync  */
  297.   /*   the live ones all at once with a single call to sync_live  */
  298.   /****************************************************************/
  299.  
  300. //  glui->sync_live();
  301.  
  302. }
  303.  
  304. void inicializacao()
  305. {
  306.     float ambient[] = {0.5, 0.5, 0.5, 1.0};
  307.     float diffuse0[] = {1.0, 1.0, 1.0, 1.0};
  308.     float diffuse1[] = {0.0, 1.0, 0.0, 1.0};
  309.     float position0[] = {1.0, 0.0, 1.0, 0.0};
  310.     float position1[] = {50.0, 0.0, 0.0, 0.0}; /* To our right. */
  311.     float lmodel_ambient[] = {0.0, 0.0, 0.0, 1.0}; /* Set the background ambient lighting. */
  312.  
  313.     glFrontFace(GL_CCW); /* Front faces defined using a counterclockwise rotation. */
  314.     glDepthFunc(GL_LEQUAL); /* Por defeito e GL_LESS */
  315.     glEnable(GL_DEPTH_TEST); /* Use a depth (z) buffer to draw only visible objects. */
  316.     glEnable(GL_CULL_FACE); /* Use back face culling to improve speed. */
  317.     //glCullFace(GL_BACK); /* Cull only back faces. */
  318.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);  // define luz ambiente
  319.  
  320.     //glEnable(GL_TEXTURE_2D);              //
  321.     pix[0].makeCheckBoard();                // cria texturas
  322.     pix[0].setTexture(2001);
  323.     pix[1].readBMPFile((char*)"wood2.bmp"); //pix[1].readBMPFile("textura.bmp");
  324.     pix[1].setTexture(2002);
  325.  
  326.     // por defeito a cor e de fundo e o preto
  327.     //glClearColor(1.0,1.0,1.0,1.0);
  328.  
  329.     /* Set the light properties */
  330.     glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  331.     glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0);
  332.     glLightfv(GL_LIGHT0, GL_POSITION, position0);
  333.     glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
  334.     glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse1);
  335.     glLightfv(GL_LIGHT1, GL_POSITION, position1);
  336.     glEnable(GL_LIGHTING);
  337.     glEnable(GL_LIGHT0);
  338.     //glEnable(GL_LIGHT1);
  339.     /* Which shade model to use: GL_FLAT / GL_SMOOTH. */
  340.     glShadeModel(GL_SMOOTH);
  341.  
  342.  
  343.     glQ = gluNewQuadric();
  344.     gluQuadricOrientation(glQ, GLU_OUTSIDE);
  345.  
  346.     // display list para a mesa
  347.     /*glNewList(mesaList, GL_COMPILE);
  348.         glPushMatrix();
  349.         //paralelo(16.0,1.0,16.0);
  350.         glPopMatrix();
  351.     glEndList();*/
  352.  
  353.     //Inicialização do tabuleiro
  354.     tabuleiro = new Board(8);
  355.  
  356.     piece = new Piece(0,2,0);
  357. }
  358.  
  359.  
  360.  
  361. int main(int argc, char* argv[])
  362. {
  363.  
  364.    glutInit(&argc, argv);
  365.    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
  366.    glutInitWindowSize (DIMX, DIMY);
  367.    glutInitWindowPosition (INITIALPOS_X, INITIALPOS_Y);
  368.    main_window = glutCreateWindow (argv[0]);
  369.  
  370.    glutDisplayFunc(display);
  371.    GLUI_Master.set_glutReshapeFunc(reshape);
  372.    GLUI_Master.set_glutKeyboardFunc (keyboard);
  373.    GLUI_Master.set_glutMouseFunc(processMouse);
  374.    glutMotionFunc(processMouseMoved);
  375.    glutPassiveMotionFunc(processPassiveMouseMoved);  
  376.    GLUI_Master.set_glutSpecialFunc( NULL );
  377.  
  378.  
  379.     /*** Create the bottom subwindow ***/
  380.     glui2 = GLUI_Master.create_glui_subwindow( main_window, GLUI_SUBWINDOW_BOTTOM );
  381.                                                         /*  GLUI_SUBWINDOW_RIGHT
  382.                                                             GLUI_SUBWINDOW_LEFT
  383.                                                             GLUI_SUBWINDOW_TOP
  384.                                                             GLUI_SUBWINDOW_BOTTOM */
  385.     glui2->set_main_gfx_window( main_window );
  386.  
  387.     GLUI_Rotation *view_rot = glui2->add_rotation( "Rotacao", view_rotate );
  388.     view_rot->set_spin( 1.0 );
  389.     glui2->add_column( false );
  390.  
  391.    
  392.     glui3 = GLUI_Master.create_glui_subwindow( main_window, GLUI_SUBWINDOW_BOTTOM);
  393.     glui3->set_main_gfx_window( main_window );
  394.  
  395.     GLUI_Translation *trans_z =
  396.     glui3->add_translation( "Zoom", GLUI_TRANSLATION_Z, &obj_pos[2] );
  397.     trans_z->set_speed( .02 );
  398.  
  399.    
  400.     /* We register the idle callback with GLUI, not with GLUT */
  401.     GLUI_Master.set_glutIdleFunc( myGlutIdle );
  402.    
  403.     inicializacao();
  404.  
  405.     glutMainLoop();
  406.  
  407.     return 0;
  408. }
Add Comment
Please, Sign In to add comment