Posts Tagged LablGl
OCaml et la 3D : CamlGL & LablGL
Posted by Alp Mestan in Langages fonctionnels on avril 29th, 2009
Bonsoir,
Comme demandé récemment par l’un des membres, il est tout à fait possible de faire de la 3D avec OpenGL en OCaml. Il y a principalement 2 bindings.
Inutile de commencer à donner un cours sur OpenGL, la plupart d’entre vous connaissant déjà, qui plus est il y a tout le nécessaire pour OpenGL en C & C++. Ces deux bibliothèques OCaml ne sont que des bindings, c’est à dire qu’elles appellent les fonctions C classiques en interfaçant par le biais de fonctions OCaml.
Juste pour vous donner une idée, voici un extrait du code correspondant au screenshot ci-dessus. Il s’agissait ici de montrer que ça existe, à quoi ça ressemble et où trouver le nécessaire pour s’y mettre.
let draw_gl_scene () = glClear (gl_color_buffer_bit lor gl_depth_buffer_bit) ; (* Clear The Screen And The Depth Buffer *) glLoadIdentity () ; (* Reset The View*) glTranslatef 0.0 0.0 !z ; (* move z units out from the screen. *) glRotatef !xrot 1.0 0.0 0.0 ; (* Rotate On The X Axis *) glRotatef !yrot 0.0 1.0 0.0 ; (* Rotate On The Y Axis *) glBindTexture gl_texture_2d texture.(!filter) ; (* choose the texture to use. *) glBegin gl_quads ; (* begin drawing a cube *) (* Front Face note that the texture's corners have to match the quad's corners *) glNormal3f 0.0 0.0 1.0 ; (* front face points out of the screen on z. *) glTexCoord2f 0.0 0.0 ; glVertex3f (-.1.0) (-.1.0) 1.0 ; (* Bottom Left Of The Texture and Quad *) glTexCoord2f 1.0 0.0 ; glVertex3f 1.0 (-.1.0) 1.0 ; (* Bottom Right Of The Texture and Quad *) glTexCoord2f 1.0 1.0 ; glVertex3f 1.0 1.0 1.0 ; (* Top Right Of The Texture and Quad *) glTexCoord2f 0.0 1.0 ; glVertex3f (-.1.0) 1.0 1.0 ; (* Top Left Of The Texture and Quad *) (* Back Face *) glNormal3f 0.0 0.0 (-.1.0) ; (* back face points into the screen on z. *) glTexCoord2f 1.0 0.0 ; glVertex3f (-.1.0) (-.1.0) (-.1.0) ; (* Bottom Right Of The Texture and Quad *) glTexCoord2f 1.0 1.0 ; glVertex3f (-.1.0) 1.0 (-.1.0) ; (* Top Right Of The Texture and Quad *) glTexCoord2f 0.0 1.0 ; glVertex3f 1.0 1.0 (-.1.0) ; (* Top Left Of The Texture and Quad *) glTexCoord2f 0.0 0.0 ; glVertex3f 1.0 (-.1.0) (-.1.0) ; (* Bottom Left Of The Texture and Quad *) (* Top Face *) glNormal3f 0.0 1.0 0.0 ; (* top face points up on y. *) glTexCoord2f 0.0 1.0 ; glVertex3f (-.1.0) 1.0 (-.1.0) ; (* Top Left Of The Texture and Quad *) glTexCoord2f 0.0 0.0 ; glVertex3f (-.1.0) 1.0 1.0 ; (* Bottom Left Of The Texture and Quad *) glTexCoord2f 1.0 0.0 ; glVertex3f 1.0 1.0 1.0 ; (* Bottom Right Of The Texture and Quad *) glTexCoord2f 1.0 1.0 ; glVertex3f 1.0 1.0 (-.1.0) ; (* Top Right Of The Texture and Quad *) (* Bottom Face *) glNormal3f 0.0 (-.1.0) 0.0 ; (* bottom face points down on y. *) glTexCoord2f 1.0 1.0 ; glVertex3f (-.1.0) (-.1.0) (-.1.0) ; (* Top Right Of The Texture and Quad *) glTexCoord2f 0.0 1.0 ; glVertex3f 1.0 (-.1.0) (-.1.0) ; (* Top Left Of The Texture and Quad *) glTexCoord2f 0.0 0.0 ; glVertex3f 1.0 (-.1.0) 1.0 ; (* Bottom Left Of The Texture and Quad *) glTexCoord2f 1.0 0.0 ; glVertex3f (-.1.0) (-.1.0) 1.0 ; (* Bottom Right Of The Texture and Quad *) (* Right face *) glNormal3f 1.0 0.0 0.0 ; (* right face points right on x. *) glTexCoord2f 1.0 0.0 ; glVertex3f 1.0 (-.1.0) (-.1.0) ; (* Bottom Right Of The Texture and Quad *) glTexCoord2f 1.0 1.0 ; glVertex3f 1.0 1.0 (-.1.0) ; (* Top Right Of The Texture and Quad *) glTexCoord2f 0.0 1.0 ; glVertex3f 1.0 1.0 1.0 ; (* Top Left Of The Texture and Quad *) glTexCoord2f 0.0 0.0 ; glVertex3f 1.0 (-.1.0) 1.0 ; (* Bottom Left Of The Texture and Quad *) (* Left Face *) glNormal3f (-.1.0) 0.0 0.0 ; (* left face points left on x. *) glTexCoord2f 0.0 0.0 ; glVertex3f (-.1.0) (-.1.0) (-.1.0) ; (* Bottom Left Of The Texture and Quad *) glTexCoord2f 1.0 0.0 ; glVertex3f (-.1.0) (-.1.0) 1.0 ; (* Bottom Right Of The Texture and Quad *) glTexCoord2f 1.0 1.0 ; glVertex3f (-.1.0) 1.0 1.0 ; (* Top Right Of The Texture and Quad *) glTexCoord2f 0.0 1.0 ; glVertex3f (-.1.0) 1.0 (-.1.0) ; (* Top Left Of The Texture and Quad *) glEnd (); (* done with the polygon. *) xrot := !xrot +. !xspeed; (* X Axis Rotation *) yrot := !yrot +. !yspeed (* Y Axis Rotation *) |
PS : aucun des deux ne semble être encore maintenu, toutefois GlCaml prend en charge les versions d’OpenGL jusqu’à 2.1 + extensions ARB et spécifiques aux vendeurs. GlCaml embarque un binding minimal de la SDL, moins complet que OCamlSDL que j’ai présenté dans un post précédent.



Commentaires récents