Le source: PolyLinePolygone.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Mars 2002 */
/* Ligne brisee et polygone */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "ModuleCouleurs.h"
#include "ModuleManipulateur.h"
#include "ModuleMenus.h"
#include "ModuleReshape.h"
#include "ModuleFont.h"
struct coord2D {
float x;
float y; } ;
struct polygone {
int np;
coord2D *p; } ;
static int aff = 0 ;
static int pol = 0 ;
static coord2D pt1[5] = { { 25,0 },
{ 5,18 },
{ -10,14 },
{ -21,-12 },
{ 14,-17 } };
static coord2D pt2[7] = { { 26,2 },
{ 7,22 },
{ -10,22 },
{ -21,-1 },
{ -7,-17 },
{ 10,-22 },
{ 19,-19 } };
static polygone p1 = { 5,pt1 };
static polygone p2 = { 7,pt2 };
void myinit() {
setAlignement(CENTER);
glClearColor(0.8F,0.8F,0.8F,1.0F);
}
void afficheSommets(polygone *p) {
int i;
glColor4fv(couleurBleu()) ;
for ( i = 0 ; i < p->np ; i++ ) {
float x = p->p[i].x;
float y = p->p[i].y;
float d = sqrt(x*x+y*y);
x /= d/12.0F;
y /= d/12.0F;
placeFontCursor(p->p[i].x,p->p[i].y,0.0) ;
deplacementCursor(x,-y,0) ;
simpleBitmapOutput(1,REGULAR8x13,"P") ;
deplacementCursor(x+5,-y+2,0) ;
simpleBitmapOutput(1,REGULAR6x10,"%d",i) ; }
}
void dessineLigneBrisee(polygone *p) {
int i ;
glPushMatrix() ;
glColor4fv(couleurNoir()) ;
glLineWidth(2.0) ;
glBegin(GL_LINE_STRIP) ;
for ( i = 0 ; i < p->np ; i++ )
glVertex2fv((float *) &p->p[i]);
glEnd() ;
glLineWidth(1.0) ;
afficheSommets(p);
glPopMatrix() ;
}
void dessinePolygone(float *coul,polygone *p) {
int i;
glPushMatrix() ;
glColor4fv(coul) ;
glBegin(GL_POLYGON) ;
for ( i = 0 ; i < p->np ; i++ )
glVertex2fv((float *) &p->p[i]);
glEnd() ;
glColor4fv(couleurNoir()) ;
glLineWidth(2.0) ;
glBegin(GL_LINE_LOOP) ;
for ( i = 0 ; i < p->np ; i++ )
glVertex2fv((float *) &p->p[i]);
glEnd() ;
glLineWidth(1.0) ;
afficheSommets(p);
glPopMatrix() ;
}
void display() {
polygone *p;
switch ( pol ) {
case 0 : p = &p1;
break;
case 1 : p = &p2;
break; }
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
manipulateurSouris();
manipulateurClavier();
switch (aff) {
case 0 : dessineLigneBrisee(p) ;
break ;
case 1 : dessinePolygone(couleurRose(),p) ;
break ; }
glPopMatrix();
glFlush();
glutSwapBuffers() ;
}
void key(unsigned char key,int x,int y) {
if ( keyManipulateur(key,x,y) )
glutPostRedisplay();
else
switch ( key ) {
case 0x0D : aff = (aff+1)%2;
glutPostRedisplay();
break;
case 0x20 : pol = (pol+1)%2;
glutPostRedisplay();
break; }
}
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowSize(230,200);
glutInitWindowPosition(50,50);
glutCreateWindow("Ligne brisee et polygone");
myinit();
creationMenuBasique();
setParametresOrthoBasique(-30.0,30.0,-30.0,30.0,-50.0,50.0);
setManipulateurDistance(1.0F);
glutReshapeFunc(reshapeOrthoBasique);
glutKeyboardFunc(key);
glutSpecialFunc(specialBasique);
glutMotionFunc(motionBasique);
glutMouseFunc(sourisBasique);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}