#include #include #include #include #include #include #include /* ************************************************** */ static int aff = 0 ; static int yy = 0 ; static float bleu[] = { 0.0F,0.0F,1.0F,1.0F } ; /* ************************************************** */ void line(int *p1,int *p2) { glColor3fv(bleu) ; glBegin(GL_LINES) ; glVertex2f((float) p1[0],(float) p1[1]) ; glVertex2f((float) p2[0],(float) p2[1]) ; glEnd() ; } /* -------------------------------------------------- */ void pixel(int x,int y,float *c) { glColor3fv(c) ; glBegin(GL_QUADS) ; glVertex2f(x-0.5F,y-0.5F) ; glVertex2f(x-0.5F,y+0.5F) ; glVertex2f(x+0.5F,y+0.5F) ; glVertex2f(x+0.5F,y-0.5F) ; glEnd() ; } /* -------------------------------------------------- */ int round(float v) { return((int) ( (v<0.0F) ? (v-0.499999F) : (v+0.499999F))) ; } /* -------------------------------------------------- */ /*void ligne(float *c,int *p1,int *p2) { int dx,dy,i,xinc,yinc,cumul,x,y ; x = p1[0] ; y = p1[1] ; dx = p2[0] - p1[0] ; dy = p2[1] - p1[1] ; xinc = ( dx > 0 ) ? 1 : -1 ; yinc = ( dy > 0 ) ? 1 : -1 ; dx = abs(dx) ; dy = abs(dy) ; pixel(x,y,c) ; if ( dx > dy ) { cumul = dx / 2 ; for ( i = 1 ; i <= dx ; i++ ) { x += xinc ; cumul += dy ; if (cumul >= dx) { cumul -= dx ; y += yinc ; } pixel(x,y,c) ; } } else { cumul = dy / 2 ; for ( i = 1 ; i <= dy ; i++ ) { y += yinc ; cumul += dx ; if ( cumul >= dy ) { cumul -= dy ; x += xinc ; } pixel(x,y,c) ; } } }*/ /* -------------------------------------------------- */ /*void ligneIncomplete(float *c,int *p1,int *p2,int max) { int dx,dy,xinc,yinc,cumul,x,y ; x = p1[0] ; y = p1[1] ; dx = p2[0] - p1[0] ; dy = p2[1] - p1[1] ; xinc = ( dx > 0 ) ? 1 : -1 ; yinc = ( dy > 0 ) ? 1 : -1 ; dx = abs(dx) ; dy = abs(dy) ; pixel(x,y,c) ; if ( dx > dy ) { cumul = dx / 2 ; for ( int i = 1,cp = 0 ; ( i <= dx ) && ( cp < max ) ; i++,cp++ ) { x += xinc ; cumul += dy ; if (cumul >= dx) { cumul -= dx ; y += yinc ; } pixel(x,y,c) ; } } else { cumul = dy / 2 ; for ( int i = 1,cp = 0 ; ( i <= dy ) && ( cp < max ) ; i++,cp++ ) { y += yinc ; cumul += dx ; if ( cumul >= dy ) { cumul -= dy ; x += xinc ; } pixel(x,y,c) ; } } }*/ /* -------------------------------------------------- */ /* Dessin d'un segment tirete (p1,p2) de couleur c, */ /* n pixels allumes suivis de m pixels non modifies : */ /* Utilisation directe d'une fonction de trace */ /* de segment pour tracer plusieurs segments les uns */ /* a cote des autres horizontalement ou verticalement */ /* en fonction de leur orientation. */ /* -------------------------------------------------- */ /*void ligneEpaisseur(float *c,int *p1,int *p2,int n) { int dx,dy ; dx = abs(p2[0] - p1[0]) ; dy = abs(p2[1] - p1[1]) ; int pp1[2] = { p1[0],p1[1] } ; int pp2[2] = { p2[0],p2[1] } ; for ( int i = 0 ; i < n ; i++ ) { if ( dx > dy ) { pp1[1] = p1[1]-n/2+i ; pp2[1] = p2[1]-n/2+i ;} else { pp1[0] = p1[0]-n/2+i ; pp2[0] = p2[0]-n/2+i ;} ligne(c,pp1,pp2) ; } }*/ /* -------------------------------------------------- */ /* Dessin d'un segment tirete (p1,p2) de couleur c */ /* pendant max pixel, n pixels allumes suivis de m */ /* pixels non modifies : */ /* Utilisation directe d'une fonction de trace */ /* de segment pour tracer plusieurs segments les uns */ /* a cote des autres horizontalement ou verticalement */ /* en fonction de leur orientation. */ /* -------------------------------------------------- */ /*void ligneEpaisseurIncomplete(float *c,int *p1,int *p2,int max,int n) { int dx,dy ; dx = abs(p2[0] - p1[0]) ; dy = abs(p2[1] - p1[1]) ; int pp1[2] = { p1[0],p1[1] } ; int pp2[2] = { p2[0],p2[1] } ; for ( int i = 0 ; i < n ; i++ ) { if ( dx > dy ) { pp1[1] = p1[1]-n/2+i ; pp2[1] = p2[1]-n/2+i ;} else { pp1[0] = p1[0]-n/2+i ; pp2[0] = p2[0]-n/2+i ;} ligneIncomplete(c,pp1,pp2,max) ; } }*/ /* -------------------------------------------------- */ /* Fonction de dessin d'une suite horizontale ou */ /* verticale de n pixels autour d'une position */ /* de reference (x,y) avec la couleur c. */ /* dx et dy definissent les dimensions en x et en y */ /* permettant de choisir entre horizontal (dy>dx) */ /* et vertical (sinon). */ /* -------------------------------------------------- */ void pixelAvecEpaisseur(int x,int y,float *c,int n,int dx,int dy) { if ( dx > dy ) for ( int i = y-n/2 ; i < y-n/2+n ; i++ ) pixel(x,i,c) ; else for ( int i = x-n/2 ; i < x-n/2+n ; i++ ) pixel(i,y,c) ; } /* -------------------------------------------------- */ /* Dessin d'un segment tirete (p1,p2) de couleur c, */ /* n pixels allumes suivis de m pixels non modifies : */ /* Algorithme de Bresenham avec fonction de dessin */ /* d'un pixel modifie de maniere a dessiner plusieurs */ /* pixels autour (horizontalement ou verticalement */ /* en fonction de l'orientation du segment) */ /* de la position de reference */ /* -------------------------------------------------- */ void ligneAvecEpaisseur(float *c,int *p1,int *p2,int n) { int dx,dy,i,xinc,yinc,cumul,x,y ; x = p1[0] ; y = p1[1] ; dx = p2[0] - p1[0] ; dy = p2[1] - p1[1] ; xinc = ( dx > 0 ) ? 1 : -1 ; yinc = ( dy > 0 ) ? 1 : -1 ; dx = abs(dx) ; dy = abs(dy) ; pixelAvecEpaisseur(x,y,c,n,dx,dy) ; if ( dx > dy ) { cumul = dx / 2 ; for ( i = 1 ; i <= dx ; i++ ) { x += xinc ; cumul += dy ; if (cumul >= dx) { cumul -= dx ; y += yinc ; } pixelAvecEpaisseur(x,y,c,n,dx,dy) ; } } else { cumul = dy / 2 ; for ( i = 1 ; i <= dy ; i++ ) { y += yinc ; cumul += dx ; if ( cumul >= dy ) { cumul -= dy ; x += xinc ; } pixelAvecEpaisseur(x,y,c,n,dx,dy) ; } } } /* -------------------------------------------------- */ /* Dessin d'un segment tirete (p1,p2) de couleur c */ /* pendant max pixel, n pixels allumes suivis de m */ /* pixels non modifies : */ /* Algorithme de Bresenham avec fonction de dessin */ /* d'un pixel modifie de maniere a dessiner plusieurs */ /* pixels autour (horizontalement ou verticalement */ /* en fonction de l'orientation du segment) */ /* de la position de reference */ /* -------------------------------------------------- */ void ligneAvecEpaisseurIncomplete(float *c,int *p1,int *p2,int max,int n) { int dx,dy,xinc,yinc,cumul,x,y ; x = p1[0] ; y = p1[1] ; dx = p2[0] - p1[0] ; dy = p2[1] - p1[1] ; xinc = ( dx > 0 ) ? 1 : -1 ; yinc = ( dy > 0 ) ? 1 : -1 ; dx = abs(dx) ; dy = abs(dy) ; pixelAvecEpaisseur(x,y,c,n,dx,dy) ; if ( dx > dy ) { cumul = dx / 2 ; for ( int i = 1,cp = 0 ; ( i <= dx ) && ( cp < max ) ; i++,cp++ ) { x += xinc ; cumul += dy ; if (cumul >= dx) { cumul -= dx ; y += yinc ; } pixelAvecEpaisseur(x,y,c,n,dx,dy) ; } } else { cumul = dy / 2 ; for ( int i = 1,cp = 0 ; ( i <= dy ) && ( cp < max ) ; i++,cp++ ) { y += yinc ; cumul += dx ; if ( cumul >= dy ) { cumul -= dy ; x += xinc ; } pixelAvecEpaisseur(x,y,c,n,dx,dy) ; } } } /* -------------------------------------------------- */ /* Fonction de dessin d'un pixel de couleur c */ /* en position (x,y) si le numero p du pixel est tel */ /* que p modulo (n+m) est inferieur strictement a n. */ /* -> n pixels traces consecutivement puis m places */ /* libres et ainsi de suite. */ /* -------------------------------------------------- */ void pixelTirete(int x,int y,float *c,int n,int m,int p) { if ( p%(n+m) < n ) pixel(x,y,c) ; } /* -------------------------------------------------- */ /* Dessin d'un segment tirete (p1,p2) de couleur c, */ /* n pixels allumes suivis de m pixels non modifies : */ /* Algorithme de Bresenham avec fonction de dessin */ /* d'un pixel modifie de maniere a tester le numero */ /* du pixel pour dessiner ou non. */ /* -------------------------------------------------- */ void ligneTirete(float *c,int *p1,int *p2,int n,int m) { int dx,dy,i,xinc,yinc,cumul,x,y ; x = p1[0] ; y = p1[1] ; dx = p2[0] - p1[0] ; dy = p2[1] - p1[1] ; xinc = ( dx > 0 ) ? 1 : -1 ; yinc = ( dy > 0 ) ? 1 : -1 ; dx = abs(dx) ; dy = abs(dy) ; pixelTirete(x,y,c,n,m,0) ; if ( dx > dy ) { cumul = dx / 2 ; for ( i = 1 ; i <= dx ; i++ ) { x += xinc ; cumul += dy ; if (cumul >= dx) { cumul -= dx ; y += yinc ; } pixelTirete(x,y,c,n,m,i) ; } } else { cumul = dy / 2 ; for ( i = 1 ; i <= dy ; i++ ) { y += yinc ; cumul += dx ; if ( cumul >= dy ) { cumul -= dy ; x += xinc ; } pixelTirete(x,y,c,n,m,i) ; } } } /* -------------------------------------------------- */ /* Dessin d'un segment tirete (p1,p2) de couleur c */ /* pendant max pixel, n pixels allumes suivis de m */ /* pixels non modifies : */ /* Algorithme de Bresenham avec fonction de dessin */ /* d'un pixel modifie de maniere a tester le numero */ /* du pixel pour dessiner ou non. */ /* -------------------------------------------------- */ void ligneTireteIncomplete(float *c,int *p1,int *p2,int max,int n,int m) { int dx,dy,xinc,yinc,cumul,x,y ; x = p1[0] ; y = p1[1] ; dx = p2[0] - p1[0] ; dy = p2[1] - p1[1] ; xinc = ( dx > 0 ) ? 1 : -1 ; yinc = ( dy > 0 ) ? 1 : -1 ; dx = abs(dx) ; dy = abs(dy) ; pixelTirete(x,y,c,n,m,0) ; if ( dx > dy ) { cumul = dx / 2 ; for ( int i = 1,cp = 0 ; ( i <= dx ) && ( cp < max ) ; i++,cp++ ) { x += xinc ; cumul += dy ; if (cumul >= dx) { cumul -= dx ; y += yinc ; } pixelTirete(x,y,c,n,m,i) ; } } else { cumul = dy / 2 ; for ( int i = 1,cp = 0 ; ( i <= dy ) && ( cp < max ) ; i++,cp++ ) { y += yinc ; cumul += dx ; if ( cumul >= dy ) { cumul -= dy ; x += xinc ; } pixelTirete(x,y,c,n,m,i) ; } } } /* -------------------------------------------------- */ /* Dessin d'une image. */ /* 4 segments traces : */ /* - 2 tiretes */ /* - 2 avec epaisseur */ /* 2 modes de dessin (Enter) : */ /* - dessin complet direct */ /* - dessin pixel apres pixel (Espace) */ /* Les 4 segments sont surlignes */ /* avec leurs equivalents continus */ /* -------------------------------------------------- */ void CALLBACK display() { float rouge[] = { 1.0F,0.0F,0.0F,1.0F }; float vert[] = { 0.0F,1.0F,0.0F,1.0F }; int p1[] = { -25,-5 } ; int p2[] = { 20,27 } ; int p3[] = { 25,-28 } ; int p4[] = { 7,11 } ; int p5[] = { -15,-28 } ; int p6[] = { -7,3 } ; int p7[] = { -22,27 } ; int p8[] = { 7,21 } ; glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); switch (aff) { case 0 : ligneAvecEpaisseur(rouge,p1,p2,3) ; ligneAvecEpaisseur(rouge,p3,p4,4) ; ligneTirete(vert,p5,p6,2,5) ; ligneTirete(vert,p7,p8,7,3) ; line(p1,p2) ; line(p3,p4) ; line(p5,p6) ; line(p7,p8) ; break ; case 1 : ligneAvecEpaisseurIncomplete(rouge,p1,p2,yy,3) ; ligneAvecEpaisseurIncomplete(rouge,p3,p4,yy,4) ; ligneTireteIncomplete(vert,p5,p6,yy,2,5) ; ligneTireteIncomplete(vert,p7,p8,yy,7,3) ; line(p1,p2) ; line(p3,p4) ; line(p5,p6) ; line(p7,p8) ; break ; } glPopMatrix(); glFlush(); auxSwapBuffers() ; } /* -------------------------------------------------- */ void CALLBACK reshape(int w,int h) { glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if( w <= h) glOrtho(-30.0,30.0,-30.0*(double) h/w,30.0*(double) h/w,-40.0,40.0); else glOrtho(-30.0*(double) w/h,30.0*(double) w/h,-30.0,30.0,-40.0,40.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } /* -------------------------------------------------- */ void CALLBACK init() { glShadeModel(GL_SMOOTH); glClearColor(0.8F,0.8F,0.8F,1.0F); } /* -------------------------------------------------- */ void CALLBACK auxKeyReturn() { yy = 0 ; aff++ ; if ( aff == 2 ) aff = 0 ; } /* -------------------------------------------------- */ void CALLBACK auxKeySpace() { yy++ ; if ( yy == 50 ) yy = 0 ; } /* -------------------------------------------------- */ void main(void) { auxInitDisplayMode(AUX_DOUBLE|AUX_RGBA|AUX_DEPTH); auxInitPosition (0,0,420,420); auxInitWindow("Exercice 3 : Segments tiretes et d'epaisseur differente de 1") ; init(); auxKeyFunc(AUX_RETURN,auxKeyReturn) ; auxKeyFunc(AUX_SPACE,auxKeySpace) ; auxReshapeFunc(reshape); auxMainLoop(display); } /* ************************************************** */