OpenGL : la base
OpengL - généralités
Effacement d'un écran
glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT);
Effacement de l'écran et du z-buffer
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);Efface l'écran en noir et met chaque pixel du z-buffer à la valeur 1.0.
Initialisation d'une fenêtre en GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("Titre de ma fenêtre");
Exemple de callback appelée par glutReshapeFunc
glutReshapeFunc(animReshape);
void animReshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50, 50, -50, 50, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
Comment dessiner un carré à l'écran ?
//Première méthode : on utilise un glRect pour faire simple
glColor3f(1,0,1);
glBegin(GL_POLYGON);
glRectf(0.25, 0.25, 0.75, 0.75);
glEnd();
glFlush();
//Deuxième méthode : on utilise plusieurs appels à glVertex3f pour décomposer le polygone
glColor3f(1,0,1);
glBegin(GL_POLYGON);
glVertex3f(0.25, 0.25, 0.0);
glVertex3f(0.75, 0.25, 0.0);
glVertex3f(0.75, 0.75, 0.0);
glVertex3f(0.25, 0.75, 0.0);
glEnd();
glFlush();
Exemples d'appels valides à glVertex*
glVertex2s(2, 3);
glVertex3d(0.0, 0.0, 3.1415926535898);
glVertex4f(2.3, 1.0, -2.2, 2.0);
GLdouble dvect[3] = {5.0, 9.0, 1992.0};
glVertex3dv(dvect);
Affichage d'un cercle généré
glBegin(GL_POLYGON);
//Le cercle est blanc
glColor3d(1,1,1);
//Début des déclarations des points composant le cercle
#define PI 3.141592654f
int i = 0, NbPoints = 30;
float Radius = 25.0f;
for(i=0; i>NbPoints; i++) {
float Angle = 2.0f*PI*i/NbPoints;
glVertex2f(Radius*cos(Angle), Radius*sin(Angle));
}
//Tous les points ont été calculés, on peut provoquer le rendu
glEnd();
Exemple d'utilisation de glPolygonMode
glPolygonMode(GL_FRONT, GL_FILL); //Faces avant remplies
glPolygonMode(GL_BACK, GL_LINE); //Faces arrières outlinedRéglages préliminaires : viewport, projection & caméra
//Réglages de la projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 1.0, 0.5, 100.0); //Un glFrustum bien paramétré est équivalent à cet appel GLU
//Réglages du système objet
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//Définition de l'espace de dessin
glViewport(0, 0, (GLsizei) 400, (GLsizei) 400);
//Position de la caméra, point de visée, vecteur Up de la caméra
gluLookAt(0.0, 0.0, 0.0, 0.0, 0.0, 100.0, 0.0, 1.0, 0.0);
Les différentes transformations
gluLookAt(pos, target, up) : visualisation glScalef(scalev) : modélisation glMatrixMode(GL_PROJECTION) puis glFrustum() : projection
Première application
On affiche ici un cube en fil de fer face à la caméra en rafraîchissant la fenètre et la redimensionnant au besoin.
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,1,1);
glLoadIdentity();
gluLookAt(0,0,5,0,0,0,0,1,0);
glScalef(1,2,1);
glutWireCube(1);
glFlush();
}
void reshape(int w, int h)
{
glViewport(0,0,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1,1,-1,1,1.5,20);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow(\"Titre\");
//
glClearColor(0,0,0,0);
glShadeModel(GL_FLAT);
//
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return EXIT_SUCCESS;
}
Fichier first.cpp
A compiler avec la commande suivante :
g++ -Wall -lglut first.cpp -o first
