Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming...
-
Upload
aymeric-fievet -
Category
Documents
-
view
143 -
download
0
Transcript of Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming...
![Page 1: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/1.jpg)
Introduction to OpenGL
![Page 2: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/2.jpg)
OpenGL: Open Graphics Library
• API graphique
( Application Programming Interface)– Couche entre le programmeur et le matériel (ou
d’autres programmes)
• Environ 250 procédures et fonctions– Définition des objets– Opérations pour applications interacives
![Page 3: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/3.jpg)
Vue du programmeurApplication
Graphics Package
OpenGL API
Matériel et programmes
Périphérique de sortie Périphérique d’entrée
Application
Périphérique d’entrée
![Page 4: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/4.jpg)
Qu’est-ce que OpenGL ?
• Machine à états reconfigurable– En entrée, données 2D ou 3D– En sortie un tampon image– On modifie l’état pour changer les
fonctionnalités
![Page 5: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/5.jpg)
Qu’est-ce que OpenGL ?
• Largement utilisé et maintenu
• Très bien documenté : www.opengl.org
• Facile à utiliser
• Traitement de la géométrie et des pixels
![Page 6: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/6.jpg)
Histoire d’OpenGL
• Développé par SGI au début des années 90
• SGI n’est plus propriétaire
• License gratuite
![Page 7: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/7.jpg)
Histoire OpenGL
• Evolution controlée par OpenGL ARB(architecture review board)– Une voix par société
• Microsoft (parti en mars 2003), Dell, IBM, Intel, Matrox, 3Dlabs, Apple, Sun Microsystems, HP, SGI, nVidia, ATI, Evans & Sutherland
![Page 8: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/8.jpg)
OpenGL est utilisé pour
• Applications temps réel (3D Studio Max, Maya, Blender, …)
• Environnements virtuels interactifs
• Jeux videos (Quake, Warcraft 3, Medal of Honor, Doom 3, …)
![Page 9: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/9.jpg)
Fonctionnement de OpenGL
• Interprétation client / serveur
– Le programme (client) invoque des commandes• Eg.activation des lumières, rendu de triangles, etc.
– Les commandes sont interprétées et traités par le serveur
• “GL”
![Page 10: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/10.jpg)
OpenGL
• Ne fournit pas le moyen de construire des scènes complexes– Utiliser pour cela une API plus haut-niveau
(OpenInventor, Java3D)
• Ne gère pas les périphériques– Souris, son...
• Ne gère pas l’interface-homme-machine– Pour cela nous utiliserons la GLUT
• (Graphics Library Utility Toolkit)
![Page 11: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/11.jpg)
Bibliothèques
• #include <GL/gl.h>
• #include <GL/glu.h>
• #include <GL/glut.h>
![Page 12: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/12.jpg)
Interaction avec le système de fenêtrage
• OpenGL est indépendant des plateformes• Un système de fenêtrage est nécessaire pour
– Interaction– Ouverture / fermeture fenêtres– Gestion des événements
• Options:– GLX (*nix)– WGL (windows)– GLUT (indépendant du système de fenêtrage)
![Page 13: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/13.jpg)
Création d’une fenêtre avec GLUT
• glutInitWindowSize
• glutInitDisplayMode
• glutCreateWindow
![Page 14: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/14.jpg)
Rendu d’une primitive géométrique
• Dans un tampon d’images
• Primitives OpenGL– Un ensemble de sommets– Un sommet définit:
• Un point
• L’extrémité d’un segment
• Le sommet d’un polygone
![Page 15: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/15.jpg)
Rendu OpenGL
• Données associées à un sommet– coordonnées– couleur– normale– coordonnées de texture
![Page 16: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/16.jpg)
Primitives OpenGL
Points Ligne Polygone
Triangle Quad Quad strip
Triangle strip Triangle Fan
![Page 17: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/17.jpg)
Modes de rendu
• Fil de fer (Wireframe)
• Plat (Flat Shading)– Une seule couleur par polygone
• Interpolé (Smooth, Gouraud)– Interpolation des couleurs des sommets sur le polygone
• Plaquage de textures– La couleur de chaque pixel est récupérée dans une image
![Page 18: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/18.jpg)
Configuration de ces options
• glEnable, glDisable,
• glCullFace, glPolygonMode, glLightModel, etc.
![Page 19: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/19.jpg)
Pipeline OpenGL
Sommets
Images ouPixels
Operationssur primitives Géométriques
Conversionen mode
Point
Operations
sur les fragments
OpérationsSur pixels
Mémoiretexture
Mém
oire tampon
![Page 20: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/20.jpg)
Opérations sur les pixels
• Manipulation de rectangles de pixels• glDrawPixels, glReadPixels, glCopyPixels
déplacent des rectangles de pixels de et vers le tampon image
• glBitmap prend en entrée une image binaire dont les pixels à 1 sont dessinés dans la mémoire image (utile pour dessiner du texte)
![Page 21: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/21.jpg)
Elimination des parties cachées
• A chaque fois qu’un fragment i est dessiné, le zi (distance au point de vue) est comparé et peut-être stocké dans le tampon de profondeur (Z-buffer)
• Soit zj la valeur présente dans le z-buffer – Si zj > zi le fragment est dessiné– Sinon rien n’est fait
![Page 22: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/22.jpg)
Elimination des parties cachées
• Spécifier l’utilisation d’un tampon de profondeur:– glutInitDisplayMode(GLUT_DEPTH);
• A la mise à jour s’assurer que :– glClear(GL_DEPTH_BUFFER_BIT);
• glEnable(GL_DEPTH_TEST);
![Page 23: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/23.jpg)
Dessiner avec OpenGL
• glBegin()
• glEnd() termine une liste de sommets et de ses attributs
• Les coordonnées d’une primitive sont données dans le sens inverse des aiguilles d’un montre
![Page 24: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/24.jpg)
Exemple ; dessin d’un point
glBegin(GL_POINTS);
glVertex3f(0.0f, 0.0f, 0.0f);
glEnd();
![Page 25: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/25.jpg)
Un triangle
glBegin(GL_TRIANGLES);glVertex3f(0.0f, 1.0f, 0.0f);glVertex3f(-1.0f, -1.0f, 0.0f);glVertex3f(1.0f, -1.0f, 0.0f);
glEnd();
![Page 26: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/26.jpg)
Un triangle avec des sommets de couleurs
différentesglBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f); //pure redglVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f); //pure greenglVertex3f(-1.0f, -1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f); //pure blueglVertex3f(1.0f, -1.0f, 0.0f);
glEnd();
![Page 27: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/27.jpg)
Modèles de couleurs avec OpenGL
• RGBA– Red, Green, Blue, Alpha– Un canal pour chaque couleur– 8 bits/canal = 16 million de couleurs
• Couleur indexée (Indexed Color)– Un petit nombre de couleur accédées grâce à un
indice dans une table de couleurs• 8 bits = 256 colors
![Page 28: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/28.jpg)
Transparence avec OpenGL
• Utilisation d’un modèle RGBA, ma 4ème composante (alpha) spécifie la transparence– Alpha = 0 ; polygone complètement transparent
– Alpha = 1 ; polygone opaque
• Deux objets de couleurs (Cs, Cf) sont composés au moment du rendu– C = alpha*Cs + (1 - alpha) Cf
– Cs est la couleur du nouveau fragment transparent
– Cf est la couleur déjà présente dans la mémoire tampon
![Page 29: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/29.jpg)
Visualisation avec OpenGL
• La visualisation est réalisée en 2 étapes– Positionnement des objets
• Matrice de transformation : model view
– Projection dans la vue• matrice de transformation projection
– OpenGL fournit des matices de projection perspectives et parallèles (orthographic)
– La caméra par défaut est toujours située à l’origine et pointe vers la direction des z négatifs
![Page 30: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/30.jpg)
Matrice ModelView
• Positionne les objets dans la scène
• Formée par la concaténation de transformations simples– glRotate(theta, x,y,z)– glTranslate(x,y,z)– glScale(x,y,z)
• L’ordre est important
![Page 31: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/31.jpg)
Transformations de modélisation
glTranslatef(0.0f, 0.0f, -10.0f);glRotatef(45.0f, 0.0f, 1.0f, 0.0f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(1.0f, -1.0f, 0.0f);
glEnd();
![Page 32: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/32.jpg)
Visualisation
• Projection orthographique– Projection parallèle, le volume de vue est un cube– glOrtho(left, right, bottom, top, front, back)
![Page 33: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/33.jpg)
Visualisation• Projection perspective
– glFrustum, gluPerspective– Le volume de vue et de découpage est une pyramide
![Page 34: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/34.jpg)
Positionnement de la camera
• gluLookAt spécifie– La position de la caméra (position de l’oeil)– Le point visé (look at)– L’orientation de la caméra (vecteur vertical, up)
• gluLookAt(10,10,10,1,2,3,0,0,1);– Oeil en (10,10,10)– Point visé (1,2,3)– Up (0,0,1)
![Page 35: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/35.jpg)
Exemple de visualisation complet//ProjectionglMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(60, 1, 1, 100);gluLookAt(10,10,10,1,2,3,0,0,1)
//Transformations sur les objetsglMatrixMode(GL_MODELVIEW)glLoad Identity();glTranslate(1,1,1);glRotatef(90, 1,0,0);DrawObject();
![Page 36: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/36.jpg)
Piles de matrices
• glPushMatrix retourne une copie du sommet de la matrice
• glPopMatrix détruit le sommet de la matrice
![Page 37: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/37.jpg)
Attention !! Les matrices de
transformation sous OpenGL sont des matrices colones • En C, si l’on déclare la matrice m[4][4],
l’élément m[I][j] est sur la ième ligne et la jème colonne.
• Avec OpenGL, l’élément m[I][j] correspond à la ième colonne et à la jème ligne de la matrice de transformation. Pour éviter les erreurs une méthode consiste à déclarer les matrices sous la forme m[16]
![Page 38: Introduction to OpenGL. OpenGL: Open Graphics Library API graphique ( Application Programming Interface) –Couche entre le programmeur et le matériel (ou.](https://reader035.fdocuments.net/reader035/viewer/2022081422/551d9d7e497959293b8b6405/html5/thumbnails/38.jpg)
Matrice OpenGL
m0 m4 m8 m12
m1 m5 m9 m13
m2 m6 m10 m14
m3 m7 m11 m15