Skip to content

Commit

Permalink
Added a ground plane grid visualization.
Browse files Browse the repository at this point in the history
  • Loading branch information
acowley committed Nov 7, 2012
1 parent 851cb14 commit ff4225d
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 5 deletions.
15 changes: 11 additions & 4 deletions src/Main.hs
Expand Up @@ -18,6 +18,7 @@ import Linear.V4
import Linear.Vector
import qualified PCD.Data as PCD
import PointsGL
import VizMarkers
import MyPaths
import HeatPalette
import FrameGrabber
Expand Down Expand Up @@ -75,7 +76,8 @@ bool _ f False = f

data ShaderArgs = ShaderArgs { camMat :: UniformLocation
, heatTex :: UniformLocation
, vertexPos :: AttribLocation }
, vertexPos :: AttribLocation
, cloudProg :: Program }

initShader :: IO ShaderArgs
initShader = do vs <- loadShader =<< getDataFileName "etc/cloud.vert"
Expand All @@ -85,6 +87,7 @@ initShader = do vs <- loadShader =<< getDataFileName "etc/cloud.vert"
ShaderArgs <$> get (uniformLocation p "cam")
<*> get (uniformLocation p "heat")
<*> get (attribLocation p "vertexCoord")
<*> pure p

buildMat :: Float -> Float -> Float -> M44 Float
buildMat s near far = V4 (set _x s 0)
Expand All @@ -98,13 +101,14 @@ setup scale ptFile = do clearColor $= Color4 1 1 1 0
depthFunc $= Just Lequal
vertexProgramPointSize $= Enabled
pointSmooth $= Enabled
textureFunction $= Decal
--textureFunction $= Decal
lighting $= Disabled
s <- initShader
activeTexture $= TextureUnit 0
uniform (heatTex s) $= Index1 (0::GLuint)
uniform (heatTex s) $= Index1 (0::GLint)
(heatVec, t) <- heatTexture 1024
let ext = takeExtension ptFile
gp <- groundPlane 5 0.1
v <- case () of
_ | ext == ".pcd" ->
PCD.loadXyz ptFile >>= \v' ->
Expand All @@ -120,7 +124,10 @@ setup scale ptFile = do clearColor $= Color4 1 1 1 0
let m = uniformMat (camMat s)
proj = buildMat scale 0.01 100.0
drawPoints <- prepPoints v (vertexPos s)
let draw c = do m $= (toList . fmap (toList . fmap realToFrac) $
let draw c = do gp Z (V3 1 0 0) (fmap (fmap realToFrac) $
proj !*! toMatrix c)
currentProgram $= Just (cloudProg s)
m $= (toList . fmap (toList . fmap realToFrac) $
proj !*! toMatrix c)
activeTexture $= TextureUnit 0
uniform (heatTex s) $= Index1 (0::GLuint)
Expand Down
6 changes: 5 additions & 1 deletion src/PointsGL.hs
Expand Up @@ -12,9 +12,13 @@ prepPoints v vertexPos =
let iv = V.enumFromN 0 (V.length v) :: V.Vector Word32
ib <- fromVector ArrayBuffer iv
bindBuffer ArrayBuffer $= Just vb
bindBuffer ElementArrayBuffer $= Just ib
vertexAttribArray vertexPos $= Enabled
vertexAttribPointer vertexPos $=
(ToFloat, VertexArrayDescriptor 3 Float 0 offset0)
return $ do bindBuffer ElementArrayBuffer $= Just ib
return $ do bindBuffer ArrayBuffer $= Just vb
--bindBuffer ElementArrayBuffer $= Just ib
vertexAttribPointer vertexPos $=
(ToFloat, VertexArrayDescriptor 3 Float 0 offset0)
drawElements Points (fromIntegral $ V.length v)
UnsignedInt offset0
86 changes: 86 additions & 0 deletions src/VizMarkers.hs
@@ -0,0 +1,86 @@
{-# OPTIONS_GHC -Wall #-}
module VizMarkers where
import Control.Exception (SomeException, catch)
import Data.Foldable (toList)
import Graphics.GLUtil
import Graphics.Rendering.OpenGL
import Linear.V2
import Linear.V3
import Linear.Matrix
import MyPaths
import System.Exit (exitWith, ExitCode(..))

data EuclideanGround = X | Y | Z deriving (Eq,Ord,Show,Enum)

enumToGL :: Enum a => a -> GLint
enumToGL = fromIntegral . fromEnum

toGLMat :: Real a => M44 a -> [[GLfloat]]
toGLMat = toList . fmap (toList . fmap realToFrac)

initGroundShader :: IO (EuclideanGround -> V3 GLfloat -> M44 GLfloat -> IO ())
initGroundShader = do let giveUp :: SomeException -> IO a
giveUp _ = {-shutdown >>-} exitWith (ExitFailure 1)
bindBuffer ArrayBuffer $= Nothing
currentProgram $= Nothing
vs <- (loadShader =<< getDataFileName "etc/GroundPlane.vert")
`catch` giveUp
fs <- (loadShader =<< getDataFileName "etc/GroundPlane.frag")
`catch` giveUp
p <- linkShaderProgram [vs] [fs] `catch` giveUp
pos <- get $ attribLocation p "vertexCoord"
cam <- get $ uniformLocation p "cam"
plane <- get $ uniformLocation p "euclideanGround"
wireColor <- get $ uniformLocation p "wireColor"
let mat = uniformMat cam
vec = uniformVec wireColor
vad = VertexArrayDescriptor 2 Float 0 offset0
vertexAttribArray pos $= Enabled
vertexAttribPointer pos $= (ToFloat, vad)
return $ \whichPlane col proj ->
do currentProgram $= Just p
vertexAttribPointer pos $= (ToFloat, vad)
uniform plane $= Index1 (enumToGL whichPlane)
mat $= toList (fmap toList proj)
vec $= toList col

checkErrors :: IO ()
checkErrors = get errors >>= aux
where aux [] = return ()
aux x = print x >> exitWith (ExitFailure 1)

-- |@groundPlane gridSize cellSize@ prepares an action for drawing a
-- wireframe ground plane, centered at the origin, consisting of
-- @gridSize@ squares with side length @cellSize@ extending off in
-- each direction.
groundPlane :: Int -> GLfloat ->
IO (EuclideanGround -> V3 GLfloat -> M44 GLfloat -> IO ())
groundPlane n sz = do vb <- makeBuffer ArrayBuffer (horizontals ++ verts)
putStrLn $ (show (length (horizontals++verts))) ++ " vs "++
(show (8*(n+1)))++" vs "++show (length indices)
print (horizontals++verts)
bindBuffer ArrayBuffer $= Just vb
shader <- initGroundShader
eb <- makeBuffer ElementArrayBuffer indices
bindBuffer ElementArrayBuffer $= Just eb
let go whichPlane col proj =
do bindBuffer ArrayBuffer $= Just vb
--bindBuffer ElementArrayBuffer $= Just eb
shader whichPlane col proj
drawElements Lines (8*(fromIntegral n+1))
UnsignedInt offset0
return go
where extent = fromIntegral n * sz
horizontals = concatMap (\z -> let z' = fromIntegral z * sz
in [ V2 (-extent) z'
, V2 extent z'
, V2 (-extent) (-z')
, V2 extent (-z') ])
[0 .. n]
verts = concatMap (\x -> let x' = fromIntegral x * sz
in [ V2 x' (-extent)
, V2 x' extent
, V2 (-x') (-extent)
, V2 (-x') extent ])
[0 .. n]
indices = [(0::GLuint)..8 * (fromIntegral n + 1)-1]

0 comments on commit ff4225d

Please sign in to comment.