Le source: ExempleBresenhamSegment.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Mars 2002 */
/* Trace de segments */
/* par l'algorithme de Bresenham */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdarg.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "ModuleCouleurs.h"
#include "ModuleMenus.h"
#include "ModuleFont.h"
#include "ModuleReshape.h"
static int aff = 0 ;
static int yy = 0 ;
static int f1;
static int f2;
static char buffer[2000];
static int pos;
static int p1[] = { 3,2 } ;
static int p2[] = { 18,11 } ;
void dessineQuadrillage(void) {
float i;
glColor4fv(couleurGrisMoyen()) ;
glBegin(GL_LINES);
for ( i = 0.0 ; i < 14 ; i++ ) {
glVertex2d(0.0,i);
glVertex2d(20.0,i); }
for ( i = 0.0 ; i < 21 ; i++ ) {
glVertex2d(i,0.0);
glVertex2d(i,13.0); }
glEnd() ;
}
void line(int *p1,int *p2) {
glLineWidth(3.0);
glColor4fv(couleurBleu()) ;
glBegin(GL_LINES) ;
glVertex2f((float) p1[0]-0.5F,(float) p1[1]-0.5F) ;
glVertex2f((float) p2[0]-0.5F,(float) p2[1]-0.5F) ;
glEnd() ;
glLineWidth(1.0);
}
void pixel(int x,int y,float *c) {
glColor4fv(c) ;
glBegin(GL_QUADS) ;
glVertex2f(x-1,y-1) ;
glVertex2f(x-1,y) ;
glVertex2f(x,y) ;
glVertex2f(x,y-1) ;
glEnd() ;
}
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) ; } }
}
void output(float x,char *format,...) {
va_list args;
va_start(args,format);
vsprintf(buffer,format,args);
va_end(args);
placeFontCursor(x,-pos*20.0F,0.0F) ;
simpleBitmapOutput(1,REGULAR8x13,buffer) ;
}
void newLine() {
pos += 1;
}
void ligneIncompleteTexte(float *c,int *p1,int *p2,int max) {
pos = 1;
glColor4fv(couleurGrisMoyen());
glBegin(GL_LINES);
glVertex2f(35.0F,-35.0F);
glVertex2f(35.0F,-370.0F);
glVertex2f(175.0F,-35.0F);
glVertex2f(175.0F,-370.0F);
glVertex2f(5.0F,-52.0F);
glVertex2f(255.0F,-52.0F);
glEnd();
glColor4fv(couleurBlanc());
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) ;
output(100.0F,"dx = %d, dy = %d",dx,dy) ;
newLine();
if ( dx > dy ) {
cumul = dx / 2 ;
output(20.0F,"x") ;
output(105.0F,"cumul") ;
output(220.0F,"y") ;
newLine();
output(20.0F,"%d",x) ;
output(105.0F,"%d",cumul) ;
output(220.0F,"%d",y) ;
newLine();
for ( int i = 1,cp = 0 ; ( i <= dx ) && ( cp < max ) ; i++,cp++ ) {
int old = cumul;
x += xinc ;
cumul += dy ;
output(20.0F,"%d",x) ;
if (cumul >= dx) {
cumul -= dx ;
y += yinc ;
output(105.0F,"%d+%d-%d -> %d",old,dy,dx,cumul) ;
output(220.0F,"%d -> %2d",y-yinc,y) ; }
else {
output(105.0F,"%d+%d -> %d",old,dy,cumul) ;
output(220.0F,"%d",y) ; }
newLine(); } }
else {
cumul = dy / 2 ;
output(20.0F,"y") ;
output(105.0F,"cumul") ;
output(220.0F,"x") ;
newLine();
output(20.0F,"%d",y) ;
output(105.0F,"%d",cumul) ;
output(220.0F,"%d",x) ;
newLine();
for ( int i = 1,cp = 0 ; ( i <= dy ) && ( cp < max ) ; i++,cp++ ) {
int old = cumul;
y += yinc ;
cumul += dx ;
output(20.0F,"%d",y) ;
if ( cumul >= dy ) {
cumul -= dy ;
x += xinc ;
output(105.0F,"%d+%d-%d -> %d",old,dx,dy,cumul) ;
output(220.0F,"%d -> %2d",x-xinc,x) ; }
else {
output(105.0F,"%d+%d -> %d",old,dx,cumul) ;
output(220.0F,"%d",x) ; }
newLine(); } }
}
void display() {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
dessineQuadrillage();
switch (aff) {
case 0 : line(p1,p2) ;
break ;
case 1 : ligne(couleurRouge(0.5F),p1,p2) ;
line(p1,p2) ;
break ;
case 2 : ligneIncomplete(couleurRouge(0.5F),p1,p2,yy) ;
line(p1,p2) ; }
glutPostWindowRedisplay(f2);
glPopMatrix();
glFlush();
glutSwapBuffers() ;
}
void myinit() {
glEnable(GL_ALPHA_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glClearColor(0.8F,0.8F,0.8F,1.0F);
setAlignement(CENTER);
}
void key(unsigned char key,int x,int y) {
switch ( key ) {
case 32 : yy = (yy+1)%16 ;
glutPostWindowRedisplay(f1);
break;
case 0x0D : yy = 0 ;
aff = (aff+1)%3 ;
glutPostWindowRedisplay(f1);
break;
case 0x1B : exit(0) ;
break; }
}
void display2(void) {
glClearColor(0.0F,0.0F,0.0F,1.0F) ;
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
glPushMatrix();
switch (aff) {
case 2 : ligneIncompleteTexte(couleurRouge(0.5F),p1,p2,yy) ; }
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void reshape2(int w,int h) {
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,w,-h,0,-1.0,1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowSize(320,210);
glutInitWindowPosition(50,10);
f1 = glutCreateWindow("Segment par l'algorithme de Bresenham");
myinit();
creationMenuBasique();
setParametresOrthoBasique(-0.5,13.5,-0.5,13.5,-40.0,40.0);
glutReshapeFunc(reshapeOrthoBasique);
glutKeyboardFunc(key);
glutDisplayFunc(display);
glutInitWindowSize(260,375);
glutInitWindowPosition(60,270);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
f2 = glutCreateWindow("Valeurs");
creationMenuBasique();
glutDisplayFunc(display2);
glutReshapeFunc(reshape2);
glutKeyboardFunc(key);
glutMainLoop();
return(0);
}