Created
February 3, 2012 00:22
-
-
Save usagi/1726705 to your computer and use it in GitHub Desktop.
how to use GPipe for starter from longgate; http://longgate.co.jp/blog/?p=21
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Graphics.GPipe | |
| import Graphics.GPipe.Texture.Load | |
| import Graphics.UI.GLUT( Window, | |
| mainLoop, | |
| postRedisplay, | |
| idleCallback, | |
| getArgsAndInitialize, | |
| ($=)) | |
| import qualified Data.Vec as Vec | |
| import Data.Vec.Nat | |
| import Data.Vec.LinAlg.Transform3D | |
| import Data.Monoid | |
| cube :: PrimitiveStream Triangle (Vec3 (Vertex Float), Vec3 (Vertex Float)) | |
| cube = mconcat [pos_x, neg_x, pos_y, neg_y, pos_z, neg_z] | |
| pos_x = toGPUStream TriangleStrip $ zip [1:.0:.0:.(), 1:.1:.0:.(), 1:.0:.1:.(), 1:.1:.1:.()] (repeat (1:.0:.0:.())) | |
| neg_x = toGPUStream TriangleStrip $ zip [0:.0:.1:.(), 0:.1:.1:.(), 0:.0:.0:.(), 0:.1:.0:.()] (repeat ((-1):.0:.0:.())) | |
| pos_y = toGPUStream TriangleStrip $ zip [0:.1:.1:.(), 1:.1:.1:.(), 0:.1:.0:.(), 1:.1:.0:.()] (repeat (0:.1:.0:.())) | |
| neg_y = toGPUStream TriangleStrip $ zip [0:.0:.0:.(), 1:.0:.0:.(), 0:.0:.1:.(), 1:.0:.1:.()] (repeat (0:.(-1):.0:.())) | |
| pos_z = toGPUStream TriangleStrip $ zip [1:.0:.1:.(), 1:.1:.1:.(), 0:.0:.1:.(), 0:.1:.1:.()] (repeat (0:.0:.1:.())) | |
| neg_z = toGPUStream TriangleStrip $ zip [0:.0:.0:.(), 0:.1:.0:.(), 1:.0:.0:.(), 1:.1:.0:.()] (repeat (0:.0:.(-1):.())) | |
| tex_coords = [ 0:.0:.(),0:.1:.(),1:.0:.(),1:.1:.()] | |
| plane :: PrimitiveStream Triangle (Vec3 (Vertex Float),Vec2 (Vertex Float)) | |
| plane = toGPUStream TriangleStrip $ zip [(-5):.0:.(-5):.(),(-5):.0:.5:.(),5:.0:.(-5):.(),5:.0:.5:.()] tex_coords | |
| -- vertex shader | |
| cube_proc_scene :: PrimitiveStream Triangle (Vec4 (Vertex Float), Vec3 (Vertex Float)) | |
| cube_proc_scene = fmap (cube_transform) $ cube | |
| cube_transform :: (Vec3 (Vertex Float),Vec3 (Vertex Float)) -> (Vec4 (Vertex Float), Vec3 (Vertex Float)) | |
| cube_transform (pos,norm) = (transformedPos,norm) | |
| where | |
| viewMat = (translation (0:.0:.(-10):.())) `multmm` (rotationX (pi/6)) `multmm` (rotationY (pi/4)) | |
| projMat = perspective 1 100 (pi/3) (800.0 / 600.0) | |
| viewProjMat = projMat `multmm` viewMat | |
| transformedPos = toGPU viewProjMat `multmv` (homPoint pos :: Vec4 (Vertex Float)) | |
| plane_proc_scene :: PrimitiveStream Triangle (Vec4 (Vertex Float), Vec2 (Vertex Float)) | |
| plane_proc_scene = fmap (plane_transform) $ plane | |
| plane_transform :: (Vec3 (Vertex Float),Vec2 (Vertex Float)) -> (Vec4 (Vertex Float),Vec2 (Vertex Float)) | |
| plane_transform (pos,texcoord) = (transformedPos,texcoord) | |
| where | |
| viewMat = (translation (0:.0:.(-10):.())) `multmm` (rotationX (pi/6)) `multmm` (rotationY (pi/4)) | |
| projMat = perspective 1 100 (pi/3) (800.0 / 600.0) | |
| viewProjMat = projMat `multmm` viewMat | |
| transformedPos = toGPU viewProjMat `multmv` (homPoint pos :: Vec4 (Vertex Float)) | |
| -- fragment shader | |
| cube_scene :: FragmentStream (Color RGBFormat (Fragment Float)) | |
| cube_scene = fmap (lit) $ rasterizeFront $ cube_proc_scene | |
| lit (norm) = color | |
| where | |
| li = norm `dot` toGPU (0.2:.0.5:.0.3:.()) | |
| color = RGB (li:.li:.li:.()) | |
| plane_scene :: Texture2D RGBFormat -> FragmentStream (Color RGBFormat (Fragment Float)) | |
| plane_scene tex = fmap (texed tex) $ rasterizeFront $ plane_proc_scene | |
| texed tex (texcoord) = sample (Sampler Linear Wrap) tex texcoord | |
| render_all :: Texture2D RGBFormat -> FrameBuffer RGBFormat () () | |
| render_all tex = draw (cube_scene) $ draw (plane_scene tex) clear | |
| where | |
| draw = paintColor NoBlending (RGB (True:.True:.True:.())) | |
| clear = newFrameBufferColor (RGB (0.1:.0.3:.0.6:.())) | |
| idle win = do postRedisplay (Just win) | |
| main :: IO () | |
| main = do | |
| getArgsAndInitialize tex <- loadTexture RGB8 "hoge.png" | |
| newWindow "GPipe" | |
| (100:.100:.()) | |
| (800:.600:.()) | |
| (render_scene tex) | |
| init_win | |
| mainLoop | |
| render_scene :: Texture2D RGBFormat -> Vec2 Int -> IO (FrameBuffer RGBFormat () ()) | |
| render_scene tex size = do | |
| return $ render_all tex | |
| init_win :: Window -> IO () | |
| init_win win = do | |
| idleCallback $= Just (idle win) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment