#include #include #include "triangle.h" int m_w; //MainWindow float angle=1.0; /* LIGHT ATTRIBUTES */ GLfloat light0pos[] = {1.0,1.0,1.0,1.0}; GLfloat light0dir[] = {0.0,0.0,0.0,0.0}; GLfloat light0amb[] = {1.0,0.0,0.0,1.0}; GLfloat light0spec[] = {0.0,0.0,0.0,1.0}; GLfloat ptX[3]={-0.5,0.5,0.0}; GLfloat ptY[3]={-0.5,-0.5,0.5}; GLfloat ptZ[3]={0.0,0.0,0.0}; triangle t(ptX, ptY, ptZ);//Dichiarazione di un oggetto bool RIGHT_BTN_DOWN = false; void mouse(int btn, int state, int x, int y){ if(btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN){ RIGHT_BTN_DOWN = false; t.last_x = x; t.last_y = y; } else if (btn == GLUT_RIGHT_BUTTON && state == GLUT_DOWN){ RIGHT_BTN_DOWN = true; t.last_scale = y; } } void mouse_motion(int x, int y){ if(RIGHT_BTN_DOWN){ if ((y-t.last_scale) > 0) t.scale += (float)0.05; else t.scale -= (float)0.05; t.last_scale = y; } else{ t.rotationX += (float) (y - t.last_y); t.rotationY += (float) (x - t.last_x); t.last_x = x; t.last_y = y; } glutPostRedisplay(); } void m_wDisplay(void){ glClearColor( .0f, .0f, .5f, 1.0f ); //Colore di sfondo glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); t.Draw(); glutSwapBuffers(); } /*CODICE STANDARD PER IL RESHAPE*/ void m_wReshape(int w, int h){ glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if(w<=h) glOrtho(-1.0,1.0, -1.0*(GLfloat)h/(GLfloat)w, 1.0*(GLfloat)h/(GLfloat)w,-1.0,1.0); else glOrtho(-1.0*(GLfloat)w/(GLfloat)h, 1.0*(GLfloat)w/(GLfloat)h,-1.0,1.0,-1.0,1.0); glMatrixMode(GL_MODELVIEW); // glLoadIdentity(); // Se ci sono mi ridisegnano il triangolo Originale. } void Init_GLUT_ENV(void){ /*GLUT INITIALIZATION*/ glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowPosition( 50, 50 ); glutInitWindowSize( 300, 300 ); /* main_wind Display Registration & GLUT registration */ m_w = glutCreateWindow( "Triangolo" ); glutDisplayFunc(m_wDisplay); glutReshapeFunc(m_wReshape); glutMotionFunc( mouse_motion ); glutMouseFunc(mouse); } void InitGL(void){ glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClearColor( .0f, .0f, .5f, 1.0f ); //Colore di sfondo glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glShadeModel(GL_SMOOTH); /* LUCI */ glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0, GL_POSITION, light0pos); glLightfv(GL_LIGHT0, GL_AMBIENT, light0amb); glLightfv(GL_LIGHT0, GL_SPECULAR, light0spec); glEnable(GL_DEPTH_TEST); glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); } void main(int argc, char *argv[]){ glutInit(&argc, argv); Init_GLUT_ENV(); InitGL(); glutMainLoop(); exit(0); }