Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void checkId(int id){
- printf("id:%d\n",id);
- selectedPiece[0] = pieces[id-1]->getX();
- selectedPiece[1] = pieces[id-1]->getY();
- selectedPiece[2] = pieces[id-1]->getZ();
- for(int i = 0; i < 3; i++){
- printf("%f\n",selectedPiece[i]);
- }
- }
- // ACÇÃO DO PICKING
- void pickingAction(GLuint answer) {
- if(answer <= 64 && answer > 0){
- int col = answer % 8;
- int row = answer / 8;
- if(col == 0)
- col = 8;
- else
- row += 1;
- printf("Celula: %d %d\n",row,col);
- if (pick == cell){
- z = cellPos[row-1][col-1].z;
- x = cellPos[row-1][col-1].x;
- pieces[idSelected-1]->setCoord(x,pieces[idSelected-1]->getY()+1,z);
- if(pieces[idSelected-1]->getId() > 8)
- pieces[idSelected-1]->setId(-66);
- else pieces[idSelected-1]->setId(-65);
- if (turn == player1)
- turn = player2;
- else turn = player1;
- pick = piece;
- }
- }
- else if ( answer > 64 && answer <= 80){
- if ((answer < 73 && turn == player1) || (answer >= 73 && turn == player2)){
- if (pick == piece){
- idSelected = answer - 64;
- checkId(idSelected);
- pick = cell;
- }
- else printf("%d\n", answer);
- }
- else printf("%d\n", answer);
- }
- else printf("%d\n", answer);
- }
- void processHits (GLint hits, GLuint buffer[]) {
- GLuint *ptr = buffer;
- GLuint mindepth = 0xFFFFFFFF;
- GLuint *answer=NULL;
- GLuint nn;
- for (int i=0;i<hits;i++) {
- int num = *ptr; ptr++;
- GLuint z1 = *ptr; ptr++;
- ptr++;
- if (z1 < mindepth && num>0) {
- mindepth = z1;
- answer = ptr;
- nn=num;
- }
- for (int j=0; j < num; j++)
- ptr++;
- }
- // existe uma resposta
- if (answer!=NULL){
- pickingAction(*answer);
- }
- }
- struct g_mouseState{
- bool leftButton;
- bool rightButton;
- bool middleButton;
- int x;
- int y;
- } MouseState;
- /* Mouse handling */
- void processMouse(int button, int state, int x, int y) {
- GLint hits;
- GLint viewport[4];
- // update our button state
- if(button == GLUT_LEFT_BUTTON) {
- if(state == GLUT_DOWN)
- MouseState.leftButton = true;
- else
- MouseState.leftButton = false;
- }
- if(button == GLUT_RIGHT_BUTTON) {
- if(state == GLUT_DOWN)
- MouseState.rightButton = true;
- else
- MouseState.rightButton = false;
- }
- if(button == GLUT_MIDDLE_BUTTON) {
- if(state == GLUT_DOWN)
- MouseState.middleButton = true;
- else
- MouseState.middleButton = false;
- }
- // update our position so we know a delta when the mouse is moved
- MouseState.x = x;
- MouseState.y = y;
- if (MouseState.leftButton && !MouseState.rightButton && !MouseState.middleButton) {
- /* obrigatorio para o picking */
- // obter o viewport actual
- glGetIntegerv(GL_VIEWPORT, viewport);
- glSelectBuffer (BUFSIZE, selectBuf);
- glRenderMode (GL_SELECT);
- // inicia processo de picking
- glInitNames();
- glMatrixMode (GL_PROJECTION);
- glPushMatrix ();
- // cria uma região de 5x5 pixels em torno do click do rato para o processo de picking
- glLoadIdentity ();
- gluPickMatrix ((GLdouble) x, (GLdouble) (window_h - y), 1.0, 1.0, viewport);
- drawScene(GL_SELECT);
- glMatrixMode (GL_PROJECTION);
- glPopMatrix ();
- glFlush ();
- hits = glRenderMode(GL_RENDER);
- processHits(hits, selectBuf);
- }
- }
- void processMouseMoved(int x, int y)
- {
- }
- void processPassiveMouseMoved(int x, int y)
- {
- // pedido de refrescamento da janela
- glutPostRedisplay();
- }
- void drawScene(GLenum mode)
- {
- float y = 0.6;
- float initx = -5.0;
- float initz = 5.0 - 5/4.0;
- float dx = 5.0/4.0;
- float dz = 5.0/4.0;
- float interval = 0.25;
- int cells = 1;
- glFrustum( -xy_aspect*.04, xy_aspect*.04, -.04, .04, .1, 500.0 );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- glPopMatrix();
- glTranslated(0.0,0.0,-30.0);
- glRotated(45.0, 1.0,0.0,0.0 );
- glTranslatef( obj_pos[0], obj_pos[1], -obj_pos[2] );
- glMultMatrixf( view_rotate );
- if (mode == GL_SELECT)
- glPushName(0);
- glPushMatrix();
- glTranslated(0.0,8.0,0);
- glCallList(mesaList);
- pieceSupport();
- if(mode == GL_SELECT){
- for (unsigned int row = 0; row < 8; row++){
- for(unsigned int col = 0; col < 8; col++){
- glLoadName(cells++);
- glEnable(GL_COLOR_MATERIAL);
- glColor3f(1.0,1.0,1.0);
- glBegin(GL_POLYGON);
- glNormal3d(0.0,1.0,0.0);
- glVertex3d(initx+interval,y,initz+interval);
- glVertex3d(initx+interval,y,initz+dz-interval);
- glVertex3d(initx+dx-interval,y,initz+dz-interval);
- glVertex3d(initx+dx-interval,y,initz+interval);
- glEnd();
- cellPos[row][col].x = initx + dx/2.0;
- cellPos[row][col].z = initz + dz/2.0;
- initx += dx;
- }
- initx = -5.0;
- initz -= dz;
- }
- }
- glPopMatrix();
- glDisable(GL_CULL_FACE);
- drawPieces(mode);
- glEnable(GL_CULL_FACE);
- glDisable(GL_COLOR_MATERIAL);
- glPopName();
- backWall();
- frontWall();
- leftWall();
- rightWall();
- floor();
- ceilling();
- lamp();
- table();
- if(draw_tree==true){
- tree();
- }
- }
Add Comment
Please, Sign In to add comment