Académique Documents
Professionnel Documents
Culture Documents
Programmation OpenGL
Licence 3 Professionnelle - Multimédia
Philippe Esling (esling@ircam.fr)
Maître de conférences – UPMC
Equipe représentations musicales (IRCAM, Paris)
Synthèse d’images 3D
Modeling
Geometry
3D scanning
...
Synthèse d’images
2 approches duales en SI
Impossible d'a!cher Impossible d'a!cher
l'image. Votre ordinateur l'image. Votre ordinateur
Impossible Impossible
d'a!cher d'a!cher
l'image. Votre l'image. Votre
ordinateur ordinateur
manque manque
peut-être de peut-être de
mémoire pour mémoire pour
ouvrir l'image ouvrir l'image
ou l'image est ou l'image est
endommagée. endommagée.
Redémarrez Redémarrez
l'ordinateur, l'ordinateur,
puis ouvrez à puis ouvrez à
nouveau le nouveau le
fichier. Si le x fichier. Si le x
rouge est rouge est
Image Image
Oeil (pixels) Oeil (pixels)
Ap A
oeil
Pipeline
1. Clipping des polygones en 3D suivant la pyramide de vue
2. Projection des points sur le plan image
3.! Remplissage des triangles (Rasterizing) dans l’image
a.! Suppression des parties cachées : Z-Buffer
b.! Calcul de la couleur : illumination
Cours de synthèse d’images 6
Projection perspective
Projection perspective
!! Besoin de perspective
!! Configuration simple :
Y
P(x,y,z)
Q
CDP -Z
O d C A
Plan image
Si et
d=distance focale
Cours de synthèse d’images 19
Matrice
Matricededeprojection
projection : M I!C
Rendu projectif
Plan image
(viewport)
l
Ap A
CDP
h
d
Ambiguïtés
+inf +inf +inf +inf +inf +inf +inf +inf +inf +inf +inf +inf
Exemple en OpenGL
OpenGL : par exemple
!! Effacer le buffer et le zbuffer entre chaque image
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
non
activé
• OpenGL est …
– hardware-independent
– operating system independent
– Gratuit et open-source
• OpenGL est un moteur de rendu par une machine
à états
– Mettre en place un état
– Passer la donnée
– La donnée est modifiée par l’état existant
– Très différent du modèle POO ou la donnée
“transporte” son propre état
3D screen space
2D display space
Object definition
Local space
Backface culling
Viewing
space
Frustum culling
HUD definition
Backface culling
Viewing
space
Frustum culling
HUD definition
3D screen space
Hidden-surface
Scan conversion V2S
Shading
Backface culling
Viewing
space
Frustum culling
HUD definition
3D screen space
Hidden-surface
Display space
S2D Scan conversion
Image V2S
Shading
Backface culling
P’ = S2D • V2S • W2V • L2W • PL Viewing
space
Frustum culling
HUD definition
3D screen space
Hidden-surface
Display space
S2D Scan conversion
Image V2S
Shading
Opération spécifique
à appeler
Fini ?
De-initialization
glBegin(Glenum mode)
glEnd()
glVertex*() → glVertex{234}{dfis}{v}()
glBegin(GL_TRIANGLES);
glVertex3f(-1.0f, -0.5f, 0.0f);
glVertex3f(1.0f, -0.5f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glEnd();
y
x
x X
P2
y V
P1
x X
Repérage
Se repérerdans l’espace 2D
dans l’espace
V
P
A
|θ|<90°
Sens trigo
V
P
B
|θ|>90° X
\
Devant/Derrière : calculer les angles V \
, PA et V , PB
\
Gauche/Droite : calculer les angles V , PA et V\, PB avec le signe
signe !
Vincent Guigue LI260 - Course de Voiture 39/45
A
|θ|<90°
Sens trigo
V
P
B
|θ|>90° X
Produit scalaire
[
U · V = hU, V i = kUkkV k cos(U, V ) = Ux V x + Uy V y
Corollaire : ✓ ◆
[ U ·V
U, V = acos
kUkkV k
[
Attention : U, V 2 [0, ⇡], pas de signe ici...
Le signe de U · V permet de résoudre le pb devant/derrière
Vincent Guigue LI260 - Course de Voiture 40/45
Détail
Détaildu
ducalcul
calculd’angle
d’angle (suite)
(suite)
Produitvectoriel
Produit vectoriel: :kU kUkkVkksin(
kU^^VVkk==kUkkV [
[
sin(U,
U, V)
V
Corollaire: :
Corollaire
2
2 3
3
✓✓ ◆◆ u22 v33 u33v22
U ^ V
[
U,[
U, asin U ^ V
V V==asin =4
UU ^^ VV = 4 uu3vv1
3 1
uu11vv33 5
5
kUkkVkk
kUkkV uu1vv2 1 2
uu22vv11
L’étudededeu1uv12v2 uu2 v2 1v1permet
L’étude permetde
de connaitre
connaitre le
le signe
signe de
de
l’angle...
l’angle...
Vincent Guigue LI260 - Course de Voiture 41/45
Vincent Guigue LI260 - Course de Voiture 41/45
P. Esling - JAVA - Cours 1. Fondamentaux 59
Exemple: Course de voitures
Informations générales Fichiers : lecture Ecriture Fichiers ASCII Outils Géométrie
Pré-définition de la voiture
P (t+1)
D*v
D
P
La voiture sera définie géométriquement par : P (t+1)
Sa position : P P (t+1)
Exemple: Collisions
Approfondissement : collisions
Y D
B
Comment détecter la collision de deux
A
C
vecteurs ?
Si C et D sont à gauche et à droite
X de AB
(Problématique de base
ET que A et B sont à gauche et à
dans les cartes
droite de CD
graphiques/moteur
physique)
Résultat :
S’ils sont de part et d’autre, l’un des produit vectoriel est positif,
l’autre négatif...
Addition, soustraction
- génération d’un nouveau vecteur
- auto-opérateur
produit scalaire
produit vectoriel (composante en z)
multiplication par un scalaire
rotation
calcul de la norme
clonage
test d’égalité (structurelle)
• Exercice de hiérarchie
– Dessiner un premier grand cube
– Dessiner un second plus petit cube
• Posé sur le premier
• Tourné de 30°
– Dessiner un troisième plus petit cube
• Posé sur le second
• Tourné de 60°
– Animer les 3 cubes (rotations à vitesse
variable)
• Scaling • Perspective
x 0 0 0 d/h 0 0 0
0 y 0 0 0 d/h 0 0
0 0 z 0 0 0 f/(f-d) –df/(f-d)
0 0 0 1 0 0 1 0
(Watt, pp.149—153)
Model View
Vertex Data
Transformation Transformation
(x, y, z, w)
Matrix World Matrix
Coordinates
Eye
Coordinates
Projection
Viewport Perspective
Transformation
Transformation Division
Normalized Matrix
Device Clip
Window Coordinates Coordinates
Coordinates
(x,y)
• Comment le coder
1. Choisir la ModelView Matrix :
glMatrixMode(GL_ModelView);
2. Vider la ModelView Matrix :
glLoadIdentity()
3. Appliquer différentes transformations
4. Relancer le rendu de l’objet
Personne
• Un graphe de scène est un
arbre des éléments de la scène
ou la transformation d’un fils Torse …
est relatif à son parent.
Bras Bras Jambe …
• La transformation finale du fils
est le produit ordonné de tous
ses ancêtres dans l’arbre. Main … …
Doigt
• La pile des matrices OpenGL se
fait en profondeur dans l’arbre MdoigtToWorld =
de scènes. (Mperson • Mtorso • Marm • Mhand • Mfinger)
• Many 2D GUIs today favor an event model in which events ‘bubble up’
from child windows to parents. This is sometimes mirrored in a scene
graph.
– Ex: a child changes size, which changes the size of the parent’s bounding box
– Ex: the user drags a movable control in the scene, triggering an update event
• If you do choose this approach, consider using the model/ view/ controller
design pattern. 3D geometry objects are good for displaying data but they
are not the proper place for control logic.
– For example, the class that stores the geometry of the rocket should not be the
same class that stores the logic that moves the rocket.
– Always separate logic from representation.
Fonctionnement de la caméra :
1 GLvoid gluLookAt ( GLdouble eyex , GLdouble eyey ,
2 GLdouble eyez , GLdouble c e n t e r x , GLdouble
3 c e n t e r y , G L d o u b l e c e n t e r z , G L d o u b l e upx ,
4 G L d o u b l e upy , G L d o u b l e upz )
OpenGL
Définition de la fenêtre
• Texture Coordinates
• Texture Parameters
• Applying textures to surface
• Minimaps
Complexité de l’image ne
change rien à la complexité
du calcul géométrique
(transformation, clipping…)
(0,0) (1,0)
1. projection
3. patch texel
2D image
A) Nearest neighbor
B) Linear Interpolation
C) Other filters
• Etapes du programme
1) Spécifier la texture
- Lire ou générer une image
- Assigner à une texture
2) Specifier les paramètres de texture mapping
- Wrapping, filtering, etc.
3) Activer le texture mapping (GL_TEXTURE_2D)
4) Assigner les coordonnnées de texture aux points
5) Dessiner les objets
6) Désactiver le texture mapping (une fois que tous
les objets texturés sont rendus)
• Example:
q glTeximage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0,
GL_RGB, GL_UNSIGNED_BYTE, myImage);
(myImage est un 2D array: GLuByte myImage[64][64][3]; )
100 128
Textures
Pour des rendus plus agréables et pour le terrain, il faut gérer les
images en plus de la 3D :
Plaquage de la texture
(0,1)
0-12$1231450.12614
(1,1)
!"#$%"&'
(1,0)
(0,0)
0-72$7237450.72674
()*%'+,'-,./'
! A chaque sommet de la face
! Coordonnées textures (u,v)
Vincent Guigue LI260 - Course de Voiture 23/46
! *+,-.#/011"!203$#4#+3"#20!&5&03#$(3!#6%&'()"#*$(3!#6(#5"75+1".
P. Esling - JAVA - Cours 1. Fondamentaux 107
Texture mapping
• Que se passe-t’il si les coordonnées de texture (s,t) sont en
dehors de l’intervalle [0,1]?
Magnification Minification
glTexParameteri(GL_TEXTURE_2D, glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MIN_FILTER, GL_NEAREST); GL_TEXTURE_MIN_FILTER,
GL_LINEAR)
Or GL_TEXTURE_MAX_FILTER
Example: glTexEnvf(GL_TEXTURE_ENV,
GL_TEXTURE_ENV_MODE, GL_REPLACE);
…
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
…
glEnable(GL_TEXTURE_2D);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGB,
GL_UNSIGNED_BYTE, mytexture);
1 ...
2 GL11 . g l B i n d T e x t u r e ( GL11 . GL_TEXTURE_2D, t e x t u r e . g e t T e x t u r e I D ( ) ) ;
3 GL11 . g l P i x e l S t o r e i ( GL11 . GL_UNPACK_ALIGNMENT, 1 ) ;
4
5 // draw a quad t e x t u r e d t o match t h e s p r i t e
6 GL11 . g l B e g i n ( GL11 . GL_QUADS ) ;
7 {
8 GL11 . g l T e x C o o r d 2 f ( 0 , 0 ) ; GL11 . g l V e r t e x 3 f ( 0 , 0 , 0 ) ;
9 GL11 . g l T e x C o o r d 2 f ( 0 , 1 ) ; GL11 . g l V e r t e x 3 f ( 0 , h e i g h t , 0 ) ;
10 GL11 . g l T e x C o o r d 2 f ( 1 , 1 ) ; GL11 . g l V e r t e x 3 f ( w i d t h , h e i g h t , 0 ) ;
11 GL11 . g l T e x C o o r d 2 f ( 1 , 0 ) ; GL11 . g l V e r t e x 3 f ( w i d t h , 0 , 0 ) ;
12 }
13 GL11 . g l E n d ( ) ;
14 GL11 . g l B i n d T e x t u r e ( GL11 . GL_TEXTURE_2D, 0 ) ;
15
16 }
PaintGL
RAZ du buffer
Définition d’une scène
1 private void g l s e t t i n g s (){
2 g l C l e a r (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ;
3 g l V i e w p o r t ( 0 , 0 , g e t W i d t h ( ) , g e t H e i g h t ( ) ) ; // 100% de l a f e n ê t r e
4 glClearColor (0. f , 0. f , 0. f , 1.0 f ) ;
5 g l M a t r i x M o d e (GL_PROJECTION ) ;
6
7 g l L o a d I d e n t i t y ( ) ; // RAZ t r a n s f o r m a t i o n s
8
9 // d e f i n i t i o n de l a t a i l l e de l a f e n e t r e 3D où l ’ on r e g a r d e
10
11 g l O r t h o ( ( f l o a t ) g e t W i d t h ( ) ⇤ zoom , ( f l o a t ) g e t W i d t h ( ) ⇤ zoom ,
12 ( float ) g e t H e i g h t ( ) ⇤ zoom ⇤ 0 . 5 , ( f l o a t ) g e t H e i g h t ( ) ⇤ zoom ⇤ 1 . 5 , 2
13
14 g l M a t r i x M o d e (GL_MODELVIEW ) ;
15 ...
PaintGL
Définition des lumières/matériaux
1 private void g l s e t t i n g s (){
2 [...]
3 i n i t L i g h t A r r a y s ( ) ; // c o d e f o u r n i s u r l e s i t e web
4
5 g l S h a d e M o d e l (GL_SMOOTH) ; // mode de r e n d u
6
7 g l M a t e r i a l (GL_FRONT, GL_SPECULAR , m a t S p e c u l a r ) ;
8 g l M a t e r i a l f (GL_FRONT, GL_SHININESS , 5 0 . 0 f ) ;
9
10 g l L i g h t ( GL_LIGHT0 , GL_POSITION , l i g h t P o s i t i o n ) ;
11 g l L i g h t ( GL_LIGHT0 , GL_SPECULAR , w h i t e L i g h t ) ;
12 g l L i g h t ( GL_LIGHT0 , GL_DIFFUSE , w h i t e L i g h t ) ;
13 g l L i g h t M o d e l (GL_LIGHT_MODEL_AMBIENT, l M o d e l A m b i e n t ) ;
14
15 g l E n a b l e ( GL_LIGHTING ) ; // a c t i v a t i o n
16 g l E n a b l e ( GL_LIGHT0 ) ;
17 g l E n a b l e (GL_COLOR_MATERIAL ) ;
18 g l C o l o r M a t e r i a l (GL_FRONT, GL_AMBIENT_AND_DIFFUSE ) ;
19
20 glLoadIdentity ();
Modes de rendu
3 2
Triangle-1
Triangle-0
0 1
GLuint bufferID;
//Generate the name and store it in bufferID
glGenBuffers(1, &bufferID);
// Do some initialization and rendering with the buffer
// Release the name
glDeleteBuffers(1, &bufferID);
glBindBuffer(GL_ARRAY_BUFFER, bufferID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,m_indexBuffer);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));
glDrawElements(
GL_TRIANGLES, // mode
m_indices_copy.size(), // count
GL_UNSIGNED_INT, // type
BUFFER_OFFSET(0) // element array buffer offset
);
glDisableClientState(GL_VERTEX_ARRAY);
+
P. Esling - JAVA - Cours 1. Fondamentaux 149
OpenGL
Et maintenant ?
+
P. Esling - JAVA - Cours 1. Fondamentaux 150
Qu’est-ce qu’un jeu?
§ Un vrai jeu
§ Nécessite au moins un
joueur (ou plus)
§ Contient des règles
§ A des conditions de
victoire et défaite
§ Très dur !
§ Et long !
§ Besoin de budget
§ (Nouveaux jeux
ont des budgets
équivalents aux
films
hollywoodiens)
§ Graphics
§ Math
§ Input handling
§ Audio
§ AI
§ Physics
§ Scripting
§ Level Editor
§ Network
Jeu (core)
Jeu (core)
Render-Interface
Jeu (core)
Render-Interface
Jeu (core)
Input
Render-Interface
Jeu (core)
Input-Interface
Input
Render-Interface
Jeu (core)
Input-Interface
Input
Render-Interface Audio-Interface
Jeu (core)
Input-Interface
Input
Render-Interface Audio-Interface
Jeu (core)
Input-Interface
Input Network
Render-Interface Audio-Interface
Jeu (core)
Input-Interface Network-Interface
Input Network
Render-Interface Audio-Interface
Intelligence
Jeu (core)
Input-Interface Network-Interface
Input Network
Render-Interface Audio-Interface
Intelligence
Physique
Jeu (core)
Input-Interface Network-Interface
Input Network
Game Loop
User Input
Network
Time = 25 ms
Application Specific
Game Logic
Sound
Rendering