/* Dessin d'un segment de droite par Bresenham */ /* Remplissage 2D d'un triangle */ /* */ /* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Novembre 2010 */ #if defined(WIN32) || defined(WIN64) #define _CRTDBG_MAP_ALLOC #if defined(_DEBUG) #define _AFXDLL #include #endif #endif #include #if defined(WIN32) || defined(WIN64) #include #endif #include #include #include #include #include #include "Position2D.h" #include "Segment2D.h" #include "Triangle2D.h" #include "Trace.h" /* Variables globales */ static int sc = 0; /* Fonction executee lors d'un changement */ /* de la taille de la fenetre OpenGL */ /* Configuration d'une camera de visualisation */ /* en projection en parallele orthographique */ void reshape(int tx,int ty) { glViewport(0,0,tx,ty); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho((-tx/2)/10.0,(-tx/2+tx)/10.0, (-ty/2)/10.0,(-ty/2+ty)/10.0, -1.0,1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } /* Fonction executee lors d'un rafraichissement */ /* de la fenetre de dessin */ void display(void) { glClearColor(0.6F,0.6F,0.6F,1.0F) ; glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) ; Position2D *ps11 = new Position2D(-15.0,9.0); Position2D *ps12 = new Position2D(10.0,-10.0); Segment2D *s1 = new Segment2D(ps11,ps12); Position2D *ps21 = new Position2D(3.0,2.0); Position2D *ps22 = new Position2D(15.0,6.0); Segment2D *s2 = new Segment2D(ps21,ps22); Position2D *ps32 = new Position2D(-7.0,-13.0); Position2D *ps31 = new Position2D(-17.0,5.0); Segment2D *s3 = new Segment2D(ps31,ps32); Position2D *pt1 = new Position2D(-13.0,9.0); Position2D *pt2 = new Position2D(11.0,13.0); Position2D *pt3 = new Position2D(2.0,-10.0); Triangle2D *t = new Triangle2D(pt1,pt2,pt3); glPushMatrix(); if ( sc == 0 ) { glColor3f(0.0F,1.0F,0.0F); s1->trace(); s2->trace(); s3->trace(); } if ( ( sc == 2 ) || ( sc == 3 ) ) { glColor3f(0.0F,0.0F,1.0F); t->remplit(); } if ( ( sc == 1 ) || ( sc == 3 ) ) { glColor3f(1.0F,0.0F,0.0F); t->trace(); } glPopMatrix(); delete(s1); delete(ps11); delete(ps12); delete(s2); delete(ps21); delete(ps22); delete(s3); delete(ps31); delete(ps32); delete(pt1); delete(pt2); delete(pt3); delete(t); glFlush(); glutSwapBuffers(); int error = glGetError(); if ( error != GL_NO_ERROR ) printf("Attention, erreur OpenGL %d\n",error); } /* Fonction executee lors de la frappe */ /* d'une touche alphanumerique du clavier */ void keyboard(unsigned char key,int x,int y) { switch ( key ) { case 0x0D : sc = (sc+1)%4; glutPostRedisplay(); break; case 0x1B : exit(0); } } /* Fonction d'initialisation d'OpenGL */ void init(void) { glPointSize(10.0F); } /* Fonction principale */ int main(int argc,char **argv) { #if defined(WIN32) || defined(WIN64) _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); //_crtBreakAlloc = 109; #endif glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH); glutInitWindowSize(400,300); glutInitWindowPosition(50,50); glutCreateWindow("Bresenham & remplissage triangle"); init(); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutDisplayFunc(display); glutMainLoop(); return(0); }