Difference between revisions of "OpenGL"

From HaskellWiki
Jump to: navigation, search
m
m (Updated a link)
 
(77 intermediate revisions by 28 users not shown)
Line 1: Line 1:
This is a stub page for Haskell's OpenGL and GLUT bindings. It is meant as a starting point to replace the outdated and misleading documentation at the
 
  +
{{Stub}}
[http://www.haskell.org/HOpenGL/ old page] (which should have disappeared by now, so that url will no longer lead anywhere, but you might still meet it on the web).
 
   
First, note that the implementation is far more up-to-date than that old page suggested (originally, it was quite useful, but the page hasn't kept up with the implementation for a long time now). To find more recent information, try:
 
  +
== 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/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/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 [https://www.opengl.org/resources/libraries/glut/ OpenGL Utility Toolkit].
   
[http://www.haskell.org/ghc/docs/latest/html/libraries/OpenGL/Graphics-Rendering-OpenGL.html the API docs for the OpenGL binding]
 
  +
* The [http://www.haskell.org/mailman/listinfo/hopengl HOpenGL mailing list] can be used for all discussions regarding the use of OpenGL from Haskell.
   
[http://www.haskell.org/ghc/docs/latest/html/libraries/GLUT/Graphics-UI-GLUT.html the API docs for the GLUT binding]
 
  +
* [https://github.com/ GitHub] hosts the [https://github.com/haskell-opengl/ haskell-opengl] organization for Haskell OpenGL development.
   
[http://darcs.haskell.org/packages/OpenGL the darcs repo with the sources for the OpenGL binding]
 
  +
The [https://github.com/haskell-opengl/GLUT/tree/master/examples examples] directory in the GLUT package contains lots of examples, including translations of the [https://www.opengl.org/documentation/books/#book_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 [https://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 [https://www.opengl.org/wiki/Code_Resources Code Resources].
   
[http://darcs.haskell.org/packages/GLUT/ the darcs repo with the sources for the GLUT binding]
 
   
In particular, note that the [http://darcs.haskell.org/packages/GLUT/examples/ examples/] directory in the GLUT repo contains lots of examples, including translations of the red book examples.
 
  +
== Projects using the OpenGL bindings ==
   
Both the API documentation and the examples are best studied with the original specs and the original red book examples at hand. An index of the examples from v1.1 of the red book, with screen shots, can be found [http://www.opengl.org/resources/code/samples/redbook/ here]
 
  +
* [https://github.com/fiendfan1/Haskell-GLSL-eDSL Haskell-GLSL-eDSL], A GLSL code generating DSL embedded in Haskell
  +
* [https://github.com/mikeplus64/plissken plissken], a 3D version of snake
  +
* [http://www.increpare.com/2008/11/endless-cavern/ Endless Cavern], a 2D procedurally-generated exploration game.
  +
* [[Frag]], a 3D first-person shooter game.
  +
* [http://www.geocities.jp/takascience/haskell/monadius_en.html Monadius], a 2D scrolling arcade game.
  +
* [http://roguestar.downstairspeople.org/ Roguestar], a roguelike adventure game using 3D graphics.
  +
* [http://hackage.haskell.org/package/Shu-thing Shu-thing], a 2D scrolling arcade game.
  +
* [http://home.arcor.de/chr_bauer/topkata.html Topkata], a jumping ball puzzle game.
  +
* [http://www.comp.leeds.ac.uk/funvis/ PolyFunViz], a toolkit for scientific visualization (e.g. surfaces, flows, contours, volumes)
  +
* [http://bysusanlin.com/raincat/ Raincat], a 2d puzzle game
  +
* [http://trac.ouroborus.net/gloss Gloss], hides the pain of drawing simple vector graphics behind a nice data type and a few display functions
   
Projects using the OpenGL bindings:
 
  +
See also the [http://packdeps.haskellers.com/reverse/OpenGL reverse dependencies list on haskellers.com]
   
* [[Frag]], a 3D first-person shooter game
 
   
HOpenGL Resources:
+
== HOpenGL Resources ==
   
* http://www.cin.ufpe.br/~haskell/hopengl/ Andre Furtado's nice tutorial written in 2001
 
  +
* [[OpenGLTutorial1]] and [[OpenGLTutorial2]]
  +
* [http://renci.org/wp-content/pub/tutorials/BeautifulCode.pdf Beautiful Code, Compelling Evidence: Functional Programming for Information Visualization and Visual Analytics] - Writing visualizations using OpenGL or Cairo (PDF)
  +
* [http://www.cin.ufpe.br/~haskell/hopengl/ Andre Furtado's nice tutorial] written in 2001 (bitrotted)
   
OpenGL Resources:
 
   
* [http://www.opengl.org/resources/faq/technical/ OpenGL FAQ and Toubleshooting Guide] Assumes some knowledge of OpenGL. Good for those who have written something but want to avoid common pitfalls.
 
  +
== OpenGL Resources ==
  +
  +
* [https://www.opengl.org/archives/resources/faq/technical/ogl_faq.pdf OpenGL FAQ and Troubleshooting Guide] (PDF) Assumes some knowledge of OpenGL. Good for those who have written something but want to avoid common pitfalls.
  +
** [https://www.opengl.org/archives/resources/faq/technical/gettingstarted.htm#0050 2.100: What is the general form of an OpenGL program?]
  +
  +
  +
== Getting Started ==
  +
  +
* 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]
  +
* read the [https://github.com/madjestic/Haskell-OpenGL-Tutorial Haskell OpenGL Tutorial] on GitHub
  +
* 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 programmable pipeline
  +
* assuming you know Haskell, any OpenGL tutorial of your choice should get you going (browsing the [https://www.opengl.org OpenGL] site is also a good idea)
  +
* use the [https://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 [https://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
  +
  +
== Additional software ==
  +
  +
* [[FTGL]]: Portable TrueType font rendering for OpenGL using the Freetype2 library
  +
* [[GLFW]]: A binding for GLFW, An OpenGL Framework
  +
* [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/package/graphics-drawingcombinators graphics-drawingcombinators]: A functional interface to 2D drawing in OpenGL
  +
* [http://hackage.haskell.org/package/GPipe GPipe]: A functional graphics API for programmable GPUs
  +
* [https://lambdacube3d.wordpress.com LambdaCube 3D]: domain specific language and library that makes it possible to program GPUs in a purely functional style
  +
* [http://hackage.haskell.org/package/freetype2 The freetype2 package]; bindings to [http://freetype.org/ FreeType], a software font engine
  +
  +
Somewhat related is [http://www.libsdl.org/ SDL], which is based on OpenGL:
  +
* [http://hackage.haskell.org/package/SDL SDL]: Binding to libSDL
  +
* [http://hackage.haskell.org/package/SDL-gfx SDL-gfx]: Binding to libSDL_gfx
  +
* [http://hackage.haskell.org/package/SDL-image SDL-image]: Binding to libSDL_image
  +
* [http://hackage.haskell.org/package/SDL-mixer SDL-mixer]: Binding to libSDL_mixer
  +
* [http://hackage.haskell.org/package/SDL-mpeg SDL-mpeg]: Binding to the SMPEG library
  +
* [http://hackage.haskell.org/package/SDL-ttf SDL-ttf]: Binding to libSDL_ttf
  +
  +
To add sound to OpenGL applications:
  +
* [http://hackage.haskell.org/package/OpenAL OpenAL]: A binding to the [[OpenAL]] cross-platform 3D audio API
  +
* [http://hackage.haskell.org/package/ALUT ALUT]: A binding for the OpenAL Utility Toolkit
  +
  +
A fork of HOpenGL:
  +
* [[OGL]]
  +
  +
Experiments with raw bindings to GLFW/OpenGL produced with [[HSFFIG]]
  +
* [http://code.google.com/p/hs-ogl-misc/ hs-ogl-misc]
  +
  +
  +
== Troubleshooting ==
  +
  +
=== 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.
  +
<haskell>
  +
scale 0.001 0.001 (0.001∷GLfloat)
  +
renderString Roman "Test string"
  +
</haskell>
  +
  +
  +
=== 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:
  +
<haskell>
  +
db <- get doubleBuffered
  +
</haskell>
  +
and set DoubleBuffered mode (before creating your windows!) like this:
  +
<haskell>
  +
initialDisplayMode $= [DoubleBuffered]
  +
createWindow "My Window"
  +
</haskell>
  +
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)].
  +
  +
  +
=== 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:
  +
<haskell>depthFunc $= Just Less</haskell>
  +
  +
Furthermore, if you're using GLFW, the following var has to be greater than zero:
  +
<haskell>get (windowParam DepthBits)</haskell>
  +
  +
If DepthBits is 0, you probably forgot set it before the call to createWindow, like so:
  +
<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:
  +
<haskell>clear [ColorBuffer, DepthBuffer]</haskell>
  +
  +
See also: [https://www.opengl.org/archives/resources/faq/technical/depthbuffer.htm#0010 The OpenGL FAQ: 12.010 How do I make depth buffering work?]
  +
  +
  +
== Blog articles ==
  +
  +
* [http://wiki.zyghost.com/Haskell%20font%20rendering%20with%20freetype2%20and%20opengl Haskell font rendering with freetype2 and OpenGL]
  +
* [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]
  +
   
 
[[Category:Packages]]
 
[[Category:Packages]]
  +
[[Category:Libraries]]
  +
[[Category:Graphics]]
  +
[[Category:3D]]

Latest revision as of 13:10, 8 November 2015

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

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 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.


Projects using the OpenGL bindings

  • Haskell-GLSL-eDSL, A GLSL code generating DSL embedded in Haskell
  • plissken, a 3D version of snake
  • 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


HOpenGL Resources


OpenGL Resources


Getting Started

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
  • LambdaCube 3D: domain specific language and library that makes it possible to program GPUs in a purely functional style
  • 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


Troubleshooting

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.001GLfloat)
renderString Roman "Test string"


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).


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?


Blog articles