Personal tools

OpenGL

From HaskellWiki

(Difference between revisions)
Jump to: navigation, search
(Updated links and removed the remark about the old page (this page is not linked anymore); added empty lines)
(update for recent GLFW-b)
(13 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
{{Stub}}
 
{{Stub}}
This is a stub page for Haskell's OpenGL and GLUT bindings.
 
 
   
 
== References ==
 
== References ==
   
* [http://www.haskell.org/mailman/listinfo/hopengl the HOpenGL mailing list]
+
* Starting with 2013.2.0.0, the OpenGL packages are part of the [http://www.haskell.org/platform/ Haskell platform], so you can find the [http://lambda.haskell.org/platform/doc/current/ API documentation] there.
+
* [http://hackage.haskell.org/ Hackage] hosts the individual OpenGL binding packages, including their latest API documentation:
* [http://hackage.haskell.org/packages/archive/OpenGL/latest/doc/html/ the API docs for the OpenGL binding]
+
** [http://hackage.haskell.org/package/OpenGLRaw OpenGLRaw] contains a low-level wrapper around OpenGL, which is basically a 1:1 mapping of the C API.
+
** [http://hackage.haskell.org/package/GLURaw GLURaw] is similar to OpenGLRaw, just for the GLU part of OpenGL.
* [http://hackage.haskell.org/packages/archive/GLUT/latest/doc/html/ the API docs for the GLUT binding]
+
** [http://hackage.haskell.org/package/OpenGL OpenGL] is the high-level view on OpenGL, wrapping OpenGLRaw into something more Haskell-like.
+
** [http://hackage.haskell.org/package/GLUT GLUT] is a binding for the [http://www.opengl.org/resources/libraries/glut/ OpenGL Utility Toolkit].
* [https://github.com/haskell-opengl/ The github organization for Haskell OpenGL development].
 
   
In particular, note that the [http://code.haskell.org/GLUT/examples/ examples/] directory in the GLUT repo contains lots of examples, including translations of the red book examples.
+
* The [http://www.haskell.org/mailman/listinfo/hopengl HOpenGL mailing list] can be used for all discussions regarding the use of OpenGL from Haskell.
   
(Note: at least some of these resources appear to be missing from [http://darcs.haskell.org/packages/ /packages], but there are copies at [http://darcs.haskell.org/ghc-6.8/packages/ /ghc-6.8/packages].)
+
* [https://github.com/ GitHub] hosts the [https://github.com/haskell-opengl/ haskell-opengl] organization for Haskell OpenGL development.
   
Both the API documentation and the examples are best studied with the [http://www.opengl.org/documentation/specs/ original specs] and the original [http://www.opengl.org/documentation/books/#the_opengl_programming_guide_the_official_guide_to_learning_opengl_version red book] examples at hand. An index of examples can be found at the OpenGL wiki page [http://www.opengl.org/wiki/Code_Resources Code Resources].
+
The [https://github.com/haskell-opengl/GLUT/tree/master/examples examples] directory in the GLUT package contains lots of examples, including translations of the [http://www.opengl.org/documentation/books/#the_opengl_programming_guide_the_official_guide_to_learning_opengl_version Red Book] examples. Both the API documentation and theses examples are best studied with the [http://www.opengl.org/documentation/specs/ original specs] and the original Red Book examples at hand. An index of examples can be found at the OpenGL wiki page [http://www.opengl.org/wiki/Code_Resources Code Resources].
   
   
Line 30: Line 28:
 
* [http://raincat.bysusanlin.com Raincat], a 2d puzzle game
 
* [http://raincat.bysusanlin.com Raincat], a 2d puzzle game
 
* [http://trac.haskell.org/gloss/ Gloss], hides the pain of drawing simple vector graphics behind a nice data type and a few display functions
 
* [http://trac.haskell.org/gloss/ Gloss], hides the pain of drawing simple vector graphics behind a nice data type and a few display functions
  +
  +
See also the [http://packdeps.haskellers.com/reverse/OpenGL reverse dependencies list on haskellers.com]
   
   
Line 47: Line 47:
 
== Getting Started ==
 
== Getting Started ==
   
* Windows users can read how to install OpenGL in the blog article [http://netsuperbrain.com/blog/posts/freeglut-windows-hopengl-hglut/ freeglut + Windows + HOpenGL + HGLUT]
 
 
* use the [http://hackage.haskell.org/package/nehe-tuts Haskell port] (Hackage package) of the [http://nehe.gamedev.net/tutorial/lessons_01__05/22004/ NeHe Tutorials]
 
* use the [http://hackage.haskell.org/package/nehe-tuts Haskell port] (Hackage package) of the [http://nehe.gamedev.net/tutorial/lessons_01__05/22004/ NeHe Tutorials]
  +
* use the (in progress) [https://github.com/bergey/haskell-OpenGL-examples Haskell port] of the [https://en.wikibooks.org/wiki/OpenGL_Programming OpenGL WikiBook], using the modern programable pipeline
 
* assuming you know Haskell, any OpenGL tutorial of your choice should get you going (browsing the [http://www.opengl.org OpenGL] site is also a good idea)
 
* assuming you know Haskell, any OpenGL tutorial of your choice should get you going (browsing the [http://www.opengl.org OpenGL] site is also a good idea)
* use the [http://www.opengl.org/documentation/red_book/ Red Book], and its example code translations, to understand the small differences between OpenGL and HOpenGL
+
* use the [http://www.opengl.org/documentation/books/#the_opengl_programming_guide_the_official_guide_to_learning_opengl_version Red Book], and its example code translations, to understand the small differences between OpenGL and HOpenGL
 
* use the [http://www.opengl.org/documentation/specs/ OpenGL and GLUT specs] to find your way around the [http://hackage.haskell.org/packages/archive/OpenGL/latest/doc/html/ HOpenGL Haddock documentation]
 
* use the [http://www.opengl.org/documentation/specs/ OpenGL and GLUT specs] to find your way around the [http://hackage.haskell.org/packages/archive/OpenGL/latest/doc/html/ HOpenGL Haddock documentation]
 
* use the [http://www.haskell.org/mailman/listinfo/hopengl HopenGL list] for questions and success stories
 
* use the [http://www.haskell.org/mailman/listinfo/hopengl HopenGL list] for questions and success stories
 
   
 
== Additional software ==
 
== Additional software ==
   
* [http://hackage.haskell.org/package/OpenGLRaw OpenGLRaw]: A 1:1 mapping of OpenGL's C API, intended as a basis for a nicer interface. ([http://aedion.de/haskell/SmoothRaw.hs Example code])
 
* [http://hackage.haskell.org/package/StateVar StateVar]: This package contains state variables, which are references in the IO monad, like IORefs or parts of the OpenGL state
 
* [http://hackage.haskell.org/package/ObjectName ObjectName]: Explicitly handled object names. This tiny package contains the class ObjectName, which corresponds to the general notion of explicitly handled identifiers for API objects, e.g. a texture object name in OpenGL or a buffer object name in OpenAL
 
* [http://hackage.haskell.org/package/GLURaw GLURaw]: A raw binding for the OpenGL graphics system. GLURaw is a raw Haskell binding for the GLU 1.3 OpenGL utility library. It is basically a 1:1 mapping of GLU's C API, intended as a basis for a nicer interface
 
 
* [[FTGL]]: Portable TrueType font rendering for OpenGL using the Freetype2 library
 
* [[FTGL]]: Portable TrueType font rendering for OpenGL using the Freetype2 library
* [http://hackage.haskell.org/cgi-bin/hackage-scripts/package/GLFW GLFW]: A binding for GLFW, An OpenGL Framework
+
* [[GLFW]]: A binding for GLFW, An OpenGL Framework
* [http://hackage.haskell.org/cgi-bin/hackage-scripts/package/GLUT GLUT]: A binding for the OpenGL Utility Toolkit
+
* [http://hackage.haskell.org/package/GLUtil GLUtil]: Miscellaneous OpenGL utilities
  +
* [http://hackage.haskell.org/package/vinyl-gl vinyl-gl]: Utilities for working with OpenGL's GLSL shading language and vinyl records
 
* [http://hackage.haskell.org/cgi-bin/hackage-scripts/package/graphics-drawingcombinators graphics-drawingcombinators]: A functional interface to 2D drawing in OpenGL
 
* [http://hackage.haskell.org/cgi-bin/hackage-scripts/package/graphics-drawingcombinators graphics-drawingcombinators]: A functional interface to 2D drawing in OpenGL
* [http://hackage.haskell.org/package/Tensor Tensor]: This package contains tensor data types and their instances for some basic type classes.
 
 
* [http://hackage.haskell.org/package/GPipe GPipe]: A functional graphics API for programmable GPUs
 
* [http://hackage.haskell.org/package/GPipe GPipe]: A functional graphics API for programmable GPUs
 
* [http://hackage.haskell.org/package/freetype2 The freetype2 package]; bindings to [http://www.freetype.org/freetype2/index.html FreeType], a software font engine
 
* [http://hackage.haskell.org/package/freetype2 The freetype2 package]; bindings to [http://www.freetype.org/freetype2/index.html FreeType], a software font engine
Line 86: Line 80:
 
Experiments with raw bindings to GLFW/OpenGL produced with [[HSFFIG]]
 
Experiments with raw bindings to GLFW/OpenGL produced with [[HSFFIG]]
 
* [http://code.google.com/p/hs-ogl-misc/ hs-ogl-misc]
 
* [http://code.google.com/p/hs-ogl-misc/ hs-ogl-misc]
 
   
 
== Troubleshooting ==
 
== Troubleshooting ==
Line 110: Line 103:
 
createWindow "My Window"
 
createWindow "My Window"
 
</haskell>
 
</haskell>
You will also need to call [http://hackage.haskell.org/packages/archive/GLUT/latest/doc/html/Graphics-UI-GLUT-Window.html#v%3AswapBuffers <haskell>swapBuffers</haskell>] at the end of your draw function.
+
You will also need to call <haskell> swapBuffers </haskell> at the end of your draw function [http://hackage.haskell.org/packages/archive/GLUT/latest/doc/html/Graphics-UI-GLUT-Window.html#v%3AswapBuffers (Read more about swapBuffers)].
   
   
Line 121: Line 114:
 
<haskell>get (windowParam DepthBits)</haskell>
 
<haskell>get (windowParam DepthBits)</haskell>
   
If DepthBits is 0, you probably forgot to initialize the window, like so:
+
If DepthBits is 0, you probably forgot set it before the call to createWindow, like so:
<haskell>openWindow size [DisplayDepthBits 16] Window</haskell>
+
<haskell>windowHint WindowHint'DepthBits 16</haskell>
   
 
Once you enable the depth buffer, you will need to clear it before each cycle of your drawing method:
 
Once you enable the depth buffer, you will need to clear it before each cycle of your drawing method:
Line 133: Line 126:
   
 
* [http://yannesposito.com/Scratch/en/blog/Haskell-OpenGL-Mandelbrot/ Haskell Progressive Example]
 
* [http://yannesposito.com/Scratch/en/blog/Haskell-OpenGL-Mandelbrot/ Haskell Progressive Example]
+
* [http://www.haskell.org/pipermail/haskell-cafe/2007-October/032568.html <nowiki>[Haskell-cafe]</nowiki> "with" and "preserving" for local state]
   
   

Revision as of 20:49, 12 January 2014

This article is a stub. You can help by expanding it.

Contents

1 References

  • Starting with 2013.2.0.0, the OpenGL packages are part of the Haskell platform, so you can find the API documentation there.
  • Hackage hosts the individual OpenGL binding packages, including their latest API documentation:
    • OpenGLRaw contains a low-level wrapper around OpenGL, which is basically a 1:1 mapping of the C API.
    • GLURaw is similar to OpenGLRaw, just for the GLU part of OpenGL.
    • OpenGL is the high-level view on OpenGL, wrapping OpenGLRaw into something more Haskell-like.
    • GLUT is a binding for the OpenGL Utility Toolkit.
  • The HOpenGL mailing list can be used for all discussions regarding the use of OpenGL from Haskell.

The examples directory in the GLUT package contains lots of examples, including translations of the Red Book examples. Both the API documentation and theses examples are best studied with the original specs and the original Red Book examples at hand. An index of examples can be found at the OpenGL wiki page Code Resources.


2 Projects using the OpenGL bindings

  • Endless Cavern, a 2D procedurally-generated exploration game.
  • Frag, a 3D first-person shooter game.
  • Monadius, a 2D scrolling arcade game.
  • Roguestar, a roguelike adventure game using 3D graphics.
  • Shu-thing, a 2D scrolling arcade game.
  • Topkata, a jumping ball puzzle game.
  • PolyFunViz, a toolkit for scientific visualization (e.g. surfaces, flows, contours, volumes)
  • Raincat, a 2d puzzle game
  • Gloss, hides the pain of drawing simple vector graphics behind a nice data type and a few display functions

See also the reverse dependencies list on haskellers.com


3 HOpenGL Resources


4 OpenGL Resources


5 Getting Started

6 Additional software

  • FTGL: Portable TrueType font rendering for OpenGL using the Freetype2 library
  • GLFW: A binding for GLFW, An OpenGL Framework
  • GLUtil: Miscellaneous OpenGL utilities
  • vinyl-gl: Utilities for working with OpenGL's GLSL shading language and vinyl records
  • graphics-drawingcombinators: A functional interface to 2D drawing in OpenGL
  • GPipe: A functional graphics API for programmable GPUs
  • The freetype2 package; bindings to FreeType, a software font engine

Somewhat related is SDL, which is based on OpenGL:

To add sound to OpenGL applications:

  • OpenAL: A binding to the OpenAL cross-platform 3D audio API
  • ALUT: A binding for the OpenAL Utility Toolkit

A fork of HOpenGL:

Experiments with raw bindings to GLFW/OpenGL produced with HSFFIG

7 Troubleshooting

7.1 I can't display text with renderString

It's probably because the text is displayed too big. Setting a much smaller scale factor before calling renderString should solve the problem.

scale 0.001 0.001 (0.001∷GLfloat)
renderString Roman "Test string"


7.2 Animations flicker

If you're not using DoubleBuffered display mode, turn that on. Also, you must set the display mode before creating the window you're going to be drawing in. To check if you've enabled double buffering use something like:

db <- get doubleBuffered

and set DoubleBuffered mode (before creating your windows!) like this:

initialDisplayMode $= [DoubleBuffered]
createWindow "My Window"
You will also need to call
 swapBuffers
at the end of your draw function (Read more about swapBuffers).


7.3 The depth buffer doesn't work (things that are closer to the camera are occluded by things that are farther from the camera)

Make sure that depthFunc is set:

depthFunc $= Just Less

Furthermore, if you're using GLFW, the following var has to be greater than zero:

get (windowParam DepthBits)

If DepthBits is 0, you probably forgot set it before the call to createWindow, like so:

windowHint WindowHint'DepthBits 16

Once you enable the depth buffer, you will need to clear it before each cycle of your drawing method:

clear [ColorBuffer, DepthBuffer]

See also: The OpenGL FAQ: 12.010 How do I make depth buffering work?


8 Blog articles