Guest User

Untitled

a guest
Apr 25th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.87 KB | None | 0 0
  1. void checkId(int id){
  2.     printf("id:%d\n",id);
  3.     selectedPiece[0] = pieces[id-1]->getX();
  4.     selectedPiece[1] = pieces[id-1]->getY();
  5.     selectedPiece[2] = pieces[id-1]->getZ();
  6.    
  7.     for(int i = 0; i < 3; i++){
  8.         printf("%f\n",selectedPiece[i]);
  9.     }
  10. }
  11.  
  12. // ACÇÃO DO PICKING
  13. void pickingAction(GLuint answer) {
  14.     if(answer <= 64 && answer > 0){
  15.  
  16.         int col = answer % 8;
  17.         int row = answer / 8;
  18.  
  19.         if(col == 0)
  20.             col = 8;
  21.         else
  22.             row += 1;
  23.  
  24.         printf("Celula: %d %d\n",row,col);
  25.  
  26.         if (pick == cell){
  27.             z = cellPos[row-1][col-1].z;
  28.             x = cellPos[row-1][col-1].x;
  29.             pieces[idSelected-1]->setCoord(x,pieces[idSelected-1]->getY()+1,z);
  30.  
  31.             if(pieces[idSelected-1]->getId() > 8)
  32.                 pieces[idSelected-1]->setId(-66);
  33.             else pieces[idSelected-1]->setId(-65);
  34.  
  35.             if (turn == player1)
  36.                 turn = player2;
  37.             else turn = player1;
  38.             pick = piece;
  39.         }
  40.  
  41.     }
  42.     else if ( answer > 64 && answer <= 80){
  43.         if ((answer < 73 && turn == player1) || (answer >= 73 && turn == player2)){
  44.             if (pick == piece){
  45.                 idSelected = answer - 64;
  46.                 checkId(idSelected);
  47.                 pick = cell;
  48.             }
  49.             else printf("%d\n", answer);
  50.         }
  51.         else printf("%d\n", answer);
  52.  
  53.     }
  54.     else printf("%d\n", answer);
  55. }
  56.  
  57.  
  58. void processHits (GLint hits, GLuint buffer[]) {
  59.     GLuint *ptr = buffer;
  60.     GLuint mindepth = 0xFFFFFFFF;
  61.     GLuint *answer=NULL;
  62.     GLuint nn;
  63.  
  64.     for (int i=0;i<hits;i++) {
  65.         int num = *ptr; ptr++;
  66.         GLuint z1 = *ptr; ptr++;
  67.         ptr++;
  68.         if (z1 < mindepth && num>0) {
  69.             mindepth = z1;
  70.             answer = ptr;
  71.             nn=num;
  72.         }
  73.         for (int j=0; j < num; j++)
  74.             ptr++;
  75.     }
  76.    
  77.     // existe uma resposta
  78.     if (answer!=NULL){
  79.         pickingAction(*answer);
  80.     }
  81.  
  82.    
  83.    
  84. }
  85. struct g_mouseState{
  86.     bool leftButton;
  87.     bool rightButton;
  88.     bool middleButton;
  89.     int x;
  90.     int y;
  91. } MouseState;
  92.  
  93. /* Mouse handling */
  94. void processMouse(int button, int state, int x, int y) {
  95.     GLint hits;
  96.     GLint viewport[4];
  97.  
  98.     // update our button state
  99.     if(button == GLUT_LEFT_BUTTON) {
  100.         if(state == GLUT_DOWN)
  101.             MouseState.leftButton = true;
  102.         else
  103.             MouseState.leftButton = false;
  104.     }
  105.     if(button == GLUT_RIGHT_BUTTON) {
  106.         if(state == GLUT_DOWN)
  107.             MouseState.rightButton = true;
  108.         else
  109.             MouseState.rightButton = false;
  110.     }
  111.     if(button == GLUT_MIDDLE_BUTTON) {
  112.         if(state == GLUT_DOWN)
  113.             MouseState.middleButton = true;
  114.         else
  115.             MouseState.middleButton = false;
  116.     }
  117.  
  118.     // update our position so we know a delta when the mouse is moved
  119.     MouseState.x = x;
  120.     MouseState.y = y;
  121.    
  122.     if (MouseState.leftButton && !MouseState.rightButton && !MouseState.middleButton) {
  123.         /* obrigatorio para o picking */
  124.         // obter o viewport actual
  125.         glGetIntegerv(GL_VIEWPORT, viewport);
  126.  
  127.         glSelectBuffer (BUFSIZE, selectBuf);
  128.         glRenderMode (GL_SELECT);
  129.  
  130.         // inicia processo de picking
  131.         glInitNames();
  132.         glMatrixMode (GL_PROJECTION);
  133.         glPushMatrix ();
  134.  
  135.         //  cria uma região de 5x5 pixels em torno do click do rato para o processo de picking
  136.         glLoadIdentity ();
  137.         gluPickMatrix ((GLdouble) x, (GLdouble) (window_h - y), 1.0, 1.0, viewport);
  138.  
  139.         drawScene(GL_SELECT);
  140.  
  141.         glMatrixMode (GL_PROJECTION);
  142.         glPopMatrix ();
  143.         glFlush ();
  144.  
  145.         hits = glRenderMode(GL_RENDER);
  146.         processHits(hits, selectBuf);
  147.     }
  148. }
  149.  
  150. void processMouseMoved(int x, int y)
  151. {  
  152. }
  153.  
  154. void processPassiveMouseMoved(int x, int y)
  155. {
  156.  
  157.     // pedido de refrescamento da janela
  158.     glutPostRedisplay();               
  159. }
  160.  
  161. void drawScene(GLenum mode)
  162. {
  163.     float y = 0.6;
  164.     float initx = -5.0;
  165.     float initz = 5.0 - 5/4.0;
  166.     float dx = 5.0/4.0;
  167.     float dz = 5.0/4.0;
  168.     float interval = 0.25;
  169.     int cells = 1;
  170.  
  171.     glFrustum( -xy_aspect*.04, xy_aspect*.04, -.04, .04, .1, 500.0 );
  172.  
  173.     glMatrixMode( GL_MODELVIEW );
  174.     glLoadIdentity();
  175.  
  176.     glPopMatrix();
  177.     glTranslated(0.0,0.0,-30.0);
  178.     glRotated(45.0, 1.0,0.0,0.0 );
  179.     glTranslatef( obj_pos[0], obj_pos[1], -obj_pos[2] );
  180.     glMultMatrixf( view_rotate );
  181.  
  182.     if (mode == GL_SELECT)
  183.         glPushName(0);
  184.  
  185.     glPushMatrix();
  186.     glTranslated(0.0,8.0,0);
  187.     glCallList(mesaList);
  188.     pieceSupport();
  189.     if(mode == GL_SELECT){
  190.         for (unsigned int row = 0; row < 8; row++){
  191.             for(unsigned int col = 0; col < 8; col++){ 
  192.  
  193.                 glLoadName(cells++);
  194.                 glEnable(GL_COLOR_MATERIAL);
  195.                 glColor3f(1.0,1.0,1.0);
  196.                 glBegin(GL_POLYGON);
  197.                     glNormal3d(0.0,1.0,0.0);
  198.                     glVertex3d(initx+interval,y,initz+interval);
  199.                     glVertex3d(initx+interval,y,initz+dz-interval);
  200.                     glVertex3d(initx+dx-interval,y,initz+dz-interval);
  201.                     glVertex3d(initx+dx-interval,y,initz+interval);
  202.                 glEnd();                               
  203.  
  204.                 cellPos[row][col].x = initx + dx/2.0;
  205.                 cellPos[row][col].z = initz + dz/2.0;
  206.                 initx += dx;
  207.             }
  208.             initx = -5.0;
  209.             initz -= dz;
  210.         }
  211.     }
  212.     glPopMatrix(); 
  213.  
  214.     glDisable(GL_CULL_FACE);
  215.     drawPieces(mode);
  216.     glEnable(GL_CULL_FACE);
  217.  
  218.    
  219.  
  220.     glDisable(GL_COLOR_MATERIAL);
  221.  
  222.     glPopName();
  223.     backWall();
  224.     frontWall();
  225.     leftWall();
  226.     rightWall();
  227.     floor();
  228.     ceilling();
  229.     lamp();
  230.     table();
  231.     if(draw_tree==true){
  232.         tree();
  233.     }
  234.    
  235. }
Add Comment
Please, Sign In to add comment