Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // aulas.cpp : Defines the entry point for the console application.
- //
- #include <GL/glui.h>
- #include <math.h>
- #include "RGBpixmap.h"
- #include "Board.h"
- #include "Piece.h"
- #define DIMX 500
- #define DIMY 500
- #define INITIALPOS_X 100
- #define INITIALPOS_Y 100
- #define BUFSIZE 512
- #define mesaList 1
- float xy_aspect; // aspect ratio da area de visualizacao
- int window_w=DIMX;
- int window_h=DIMY;
- // picking
- GLuint selectBuf[BUFSIZE];
- float view_rotate[16] = { 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 };
- float obj_pos[] = { 0.0, 0.0, 0.0 };
- // variaveis globais
- int main_window;
- GLUI *glui, *glui2, *glui3;
- float mat1_shininess[] = {20.0};
- float mat1_specular[] = {0.8, 0.8, 0.8, 1.0}; /* specular reflection. */
- float mat1_diffuse[] = {1.0, 0.0, 0.0, 1.0}; /* diffuse reflection. */
- float mat2_shininess[] = {1.0};
- float mat2_specular[] = {0.4, 0.4, 0.4, 1.0}; /* specular reflection. */
- float mat2_diffuse[] = {0.3, 0.30, 0.30, 1.0}; /* diffuse reflection. */
- RGBpixmap pix[2];
- GLUquadric* glQ;
- //Movement
- int coord[2];
- bool can = false;
- Board* tabuleiro;
- Piece* piece;
- enum stat_maq {
- pieces, cells, moves
- };
- enum who{
- player1, player2
- };
- stat_maq move = pieces;
- who turn = player1;
- void drawScene(GLenum mode)
- {
- glFrustum( -xy_aspect*.08, xy_aspect*.08, -.07, .07, .1, 200.0 );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- 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);
- piece->buildPiece(mode,glQ);
- tabuleiro->drawBoard(mode);
- if(can)
- tabuleiro->hitZone(coord[0],coord[1]);
- glDisable(GL_COLOR_MATERIAL);
- glPopName();
- }
- void display(void) {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glPolygonMode(GL_FRONT, GL_FILL);
- glMatrixMode (GL_PROJECTION);
- glLoadIdentity();
- drawScene(GL_RENDER);
- // swapping the buffers causes the rendering above to be shown
- glutSwapBuffers();
- glFlush();
- }
- // ACÇÃO DO PICKING
- void pickingAction(GLuint answer) {
- int x,z;
- if(answer > 64 && move == pieces)
- {
- x = (piece->getX()/2) + 1;
- z = piece->getZ()/2;
- cout << "Piece coords: ";
- cout << z << " " << x << endl;
- coord[0] = x;
- coord[1] = z;
- can = true;
- move = cells;
- }
- else if(answer <= 64 && move == cells){
- x = answer % 8;
- z = answer / 8;
- if(x == 0)
- x = 8;
- else
- z += 1;
- cout << x << " " << z << endl;
- piece->setCoords(2*(x-1),z*2);
- can = false;
- move = pieces;
- }
- else{
- printf("%d\n", answer);
- can = false;
- move = pieces;
- }
- }
- // processa os hits no picking
- 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 reshape(int w, int h)
- {
- int tx, ty, tw, th;
- window_w = w; //variaveis globais; window_h e' usado em processMouse()
- window_h = h;
- GLUI_Master.get_viewport_area( &tx, &ty, &tw, &th );
- glViewport( tx, ty, tw, th );
- // ver, nos manuais, a funcao GLUI_Master.auto_set_viewport()
- xy_aspect = (float)tw / (float)th;
- glutPostRedisplay();
- }
- void keyboard(unsigned char key, int x, int y)
- {
- switch (key) {
- case 27: // tecla de escape termina o programa
- exit(0);
- break;
- }
- }
- void myGlutIdle( void )
- {
- /* According to the GLUT specification, the current window is
- undefined during an idle callback. So we need to explicitly change
- it if necessary */
- if ( glutGetWindow() != main_window )
- glutSetWindow(main_window);
- glutPostRedisplay();
- /****************************************************************/
- /* This demonstrates GLUI::sync_live() */
- /* We change the value of a variable that is 'live' to some */
- /* control. We then call sync_live, and the control */
- /* associated with that variable is automatically updated */
- /* with the new value. This frees the programmer from having */
- /* to always remember which variables are used by controls - */
- /* simply change whatever variables are necessary, then sync */
- /* the live ones all at once with a single call to sync_live */
- /****************************************************************/
- // glui->sync_live();
- }
- void inicializacao()
- {
- float ambient[] = {0.5, 0.5, 0.5, 1.0};
- float diffuse0[] = {1.0, 1.0, 1.0, 1.0};
- float diffuse1[] = {0.0, 1.0, 0.0, 1.0};
- float position0[] = {1.0, 0.0, 1.0, 0.0};
- float position1[] = {50.0, 0.0, 0.0, 0.0}; /* To our right. */
- float lmodel_ambient[] = {0.0, 0.0, 0.0, 1.0}; /* Set the background ambient lighting. */
- glFrontFace(GL_CCW); /* Front faces defined using a counterclockwise rotation. */
- glDepthFunc(GL_LEQUAL); /* Por defeito e GL_LESS */
- glEnable(GL_DEPTH_TEST); /* Use a depth (z) buffer to draw only visible objects. */
- glEnable(GL_CULL_FACE); /* Use back face culling to improve speed. */
- //glCullFace(GL_BACK); /* Cull only back faces. */
- glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient); // define luz ambiente
- //glEnable(GL_TEXTURE_2D); //
- pix[0].makeCheckBoard(); // cria texturas
- pix[0].setTexture(2001);
- pix[1].readBMPFile((char*)"wood2.bmp"); //pix[1].readBMPFile("textura.bmp");
- pix[1].setTexture(2002);
- // por defeito a cor e de fundo e o preto
- //glClearColor(1.0,1.0,1.0,1.0);
- /* Set the light properties */
- glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0);
- glLightfv(GL_LIGHT0, GL_POSITION, position0);
- glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
- glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse1);
- glLightfv(GL_LIGHT1, GL_POSITION, position1);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- //glEnable(GL_LIGHT1);
- /* Which shade model to use: GL_FLAT / GL_SMOOTH. */
- glShadeModel(GL_SMOOTH);
- glQ = gluNewQuadric();
- gluQuadricOrientation(glQ, GLU_OUTSIDE);
- // display list para a mesa
- /*glNewList(mesaList, GL_COMPILE);
- glPushMatrix();
- //paralelo(16.0,1.0,16.0);
- glPopMatrix();
- glEndList();*/
- //Inicialização do tabuleiro
- tabuleiro = new Board(8);
- piece = new Piece(0,2,0);
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
- glutInitWindowSize (DIMX, DIMY);
- glutInitWindowPosition (INITIALPOS_X, INITIALPOS_Y);
- main_window = glutCreateWindow (argv[0]);
- glutDisplayFunc(display);
- GLUI_Master.set_glutReshapeFunc(reshape);
- GLUI_Master.set_glutKeyboardFunc (keyboard);
- GLUI_Master.set_glutMouseFunc(processMouse);
- glutMotionFunc(processMouseMoved);
- glutPassiveMotionFunc(processPassiveMouseMoved);
- GLUI_Master.set_glutSpecialFunc( NULL );
- /*** Create the bottom subwindow ***/
- glui2 = GLUI_Master.create_glui_subwindow( main_window, GLUI_SUBWINDOW_BOTTOM );
- /* GLUI_SUBWINDOW_RIGHT
- GLUI_SUBWINDOW_LEFT
- GLUI_SUBWINDOW_TOP
- GLUI_SUBWINDOW_BOTTOM */
- glui2->set_main_gfx_window( main_window );
- GLUI_Rotation *view_rot = glui2->add_rotation( "Rotacao", view_rotate );
- view_rot->set_spin( 1.0 );
- glui2->add_column( false );
- glui3 = GLUI_Master.create_glui_subwindow( main_window, GLUI_SUBWINDOW_BOTTOM);
- glui3->set_main_gfx_window( main_window );
- GLUI_Translation *trans_z =
- glui3->add_translation( "Zoom", GLUI_TRANSLATION_Z, &obj_pos[2] );
- trans_z->set_speed( .02 );
- /* We register the idle callback with GLUI, not with GLUT */
- GLUI_Master.set_glutIdleFunc( myGlutIdle );
- inicializacao();
- glutMainLoop();
- return 0;
- }
Add Comment
Please, Sign In to add comment