Personal tools

GPipe

From HaskellWiki

(Difference between revisions)
Jump to: navigation, search
m (Made it a stub)
(Adding Vec, removing Vec-Transform)
 
(20 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{stub}}
+
== What is GPipe? ==
   
This is a wiki stub for the [http://hackage.haskell.org/package/GPipe GPipe package]. If you have any questions, feel free to [mailto:tobias_bexelius@hotmail.com mail] me.
+
[http://hackage.haskell.org/package/GPipe GPipe] is a library for programming the GPU (graphics processing unit). It is an alternative to using OpenGl, and has the advantage that it is purely functional, statically typed and operates on immutable data as opposed to OpenGl's inherently imperative style. Another important difference with OpenGl is that with GPipe you don't need to write shaders in a second shader language such as GLSL or Cg, but instead use regular Haskell functions on the GPU data types. GPipe uses the same conceptual model as OpenGl, and it is recommended that you have at least a basic understanding of how OpenGl works to be able to use GPipe.
   
== Example ==
+
== Examples and tutorials ==
This is a simple GPipe example that animates a spinning box. Besides GPipe, it uses the
 
[http://hackage.haskell.org/package/Vec-Transform Vec-Transform package] for the transformation matrices.
 
   
<haskell>
+
* Wiki [[/Tutorial/]] that explains the basic principles of GPipe.
module Main where
+
* [http://hackage.haskell.org/package/GPipe-Examples GPipe-Examples package], by Kree Cole-McLaughlin features a set of four examples with increasing complexity.
  +
* Csaba Hruska has made a Quake 3 map viewer using GPipe, sources on [https://github.com/csabahruska/GFXDemo GitHub].
   
import Graphics.GPipe
+
== Sources ==
import Data.Monoid
 
import Data.IORef
 
import qualified Data.Vec as Vec
 
import Data.Vec.Nat
 
import Data.Vec.LinAlg.Transform3D
 
import Graphics.UI.GLUT
 
(mainLoop,
 
postRedisplay,
 
idleCallback,
 
getArgsAndInitialize,
 
($=))
 
   
sidePosX = toGPUStream TriangleStrip $ map (flip (,) (1:.0:.0:.())) [1:.0:.0:.(), 1:.1:.0:.(), 1:.0:.1:.(), 1:.1:.1:.()]
+
All my GPipe related library sources are available on [http://github.com/tobbebex Github]. If you have something to contribute with, just send me a patch and I might merge it into the trunk.
sideNegX = toGPUStream TriangleStrip $ map (flip (,) ((-1):.0:.0:.())) [0:.0:.1:.(), 0:.1:.1:.(), 0:.0:.0:.(), 0:.1:.0:.()]
 
sidePosY = toGPUStream TriangleStrip $ map (flip (,) (0:.1:.0:.())) [0:.1:.1:.(), 1:.1:.1:.(), 0:.1:.0:.(), 1:.1:.0:.()]
 
sideNegY = toGPUStream TriangleStrip $ map (flip (,) (0:.(-1):.0:.())) [0:.0:.0:.(), 1:.0:.0:.(), 0:.0:.1:.(), 1:.0:.1:.()]
 
sidePosZ = toGPUStream TriangleStrip $ map (flip (,) (0:.0:.1:.())) [1:.0:.1:.(), 1:.1:.1:.(), 0:.0:.1:.(), 0:.1:.1:.()]
 
sideNegZ = toGPUStream TriangleStrip $ map (flip (,) (0:.0:.(-1):.())) [0:.0:.0:.(), 0:.1:.0:.(), 1:.0:.0:.(), 1:.1:.0:.()]
 
   
cube = mconcat [sidePosX, sideNegX, sidePosY, sideNegY, sidePosZ, sideNegZ]
+
== Other resources ==
   
transformedCube a = fmap (transform a) cube
+
* [http://hackage.haskell.org/package/GLUT GLUT] is used in GPipe for window management and the main loop.
transform :: Float -> (Vec3 (Vertex Float), Vec3 (Vertex Float)) -> (Vec4 (Vertex Float), Vec3 (Vertex Float))
+
* [http://hackage.haskell.org/package/Vec Vec package] is the vector math package used by GPipe.
transform a (pos, norm) = (transformedPos, transformedNorm)
+
* [http://hackage.haskell.org/package/GPipe-TextureLoad GPipe-TextureLoad package] helps loading textures from disc.
where
+
* [http://hackage.haskell.org/package/GPipe-Collada GPipe-Collada package] makes it possible to use Collada files with GPipe.
modelMat = rotationVec (normalize (1:.0.5:.0.3:.())) a `multmm` translation (-0.5)
 
viewMat = translation (-(0:.0:.2:.()))
 
projMat = perspective 1 100 (pi/3) (4/3)
 
viewProjMat = projMat `multmm` viewMat
 
transformedPos = toGPU (viewProjMat `multmm` modelMat) `multmv` homPoint pos
 
transformedNorm = toGPU (Vec.map (Vec.take n3) $ Vec.take n3 $ modelMat) `multmv` norm
 
   
coloredFragments a = fmap (RGB . Vec.vec . dot (toGPU (0:.0:.1:.()))) $ rasterizeFront $ transformedCube a
+
== Questions and feedback ==
   
paintSolid = paintColor NoBlending (RGB $ Vec.vec True)
+
If you have any questions or suggestions, feel free to [mailto:tobias_bexelius@hotmail.com mail] me. I'm also interested in seeing some use cases from the community, as complex or trivial they may be.
   
main = do getArgsAndInitialize
+
[[Category:3D]]
angleRef <- newIORef 0.0
+
[[Category:Graphics]]
newWindow "Spinning box" (100:.100:.()) (800:.600:.())
+
[[Category:Libraries]]
(do angle <- readIORef angleRef
+
[[Category:Packages]]
writeIORef angleRef ((angle + 0.01) `mod'` (2*pi))
 
return $ paintSolid (coloredFragments angle) (newFrameBufferColor (RGB 0))
 
)
 
(\ w -> idleCallback $= Just (postRedisplay (Just w)))
 
mainLoop
 
 
 
</haskell>
 
 
[[Image:box.jpg]]
 

Latest revision as of 16:52, 6 August 2012

Contents

[edit] 1 What is GPipe?

GPipe is a library for programming the GPU (graphics processing unit). It is an alternative to using OpenGl, and has the advantage that it is purely functional, statically typed and operates on immutable data as opposed to OpenGl's inherently imperative style. Another important difference with OpenGl is that with GPipe you don't need to write shaders in a second shader language such as GLSL or Cg, but instead use regular Haskell functions on the GPU data types. GPipe uses the same conceptual model as OpenGl, and it is recommended that you have at least a basic understanding of how OpenGl works to be able to use GPipe.

[edit] 2 Examples and tutorials

  • Wiki Tutorial that explains the basic principles of GPipe.
  • GPipe-Examples package, by Kree Cole-McLaughlin features a set of four examples with increasing complexity.
  • Csaba Hruska has made a Quake 3 map viewer using GPipe, sources on GitHub.

[edit] 3 Sources

All my GPipe related library sources are available on Github. If you have something to contribute with, just send me a patch and I might merge it into the trunk.

[edit] 4 Other resources

[edit] 5 Questions and feedback

If you have any questions or suggestions, feel free to mail me. I'm also interested in seeing some use cases from the community, as complex or trivial they may be.