Add PPM image exporting
This commit is contained in:
parent
8660bde83f
commit
ef52a8f702
6
.gitignore
vendored
6
.gitignore
vendored
|
@ -5,3 +5,9 @@ build/
|
|||
*.*~
|
||||
|
||||
**/.DS_Store
|
||||
|
||||
# Ignore output images
|
||||
*.ppm
|
||||
*.png
|
||||
*.jpg
|
||||
*.gif
|
||||
|
|
|
@ -3,15 +3,11 @@ module Render.Object
|
|||
import Data.Vect
|
||||
import Render.Camera
|
||||
import Render.Color
|
||||
import public Render.Object.Interface
|
||||
import public Render.Object.Point
|
||||
|
||||
%default total
|
||||
|
||||
|
||||
public export
|
||||
interface Object' obj where
|
||||
draw : obj -> Camera -> List (Integer, Integer, ColorAlpha)
|
||||
|
||||
|
||||
public export
|
||||
data Object : Type where
|
||||
MkObject : Object' obj => obj -> Object
|
||||
MkObject : IsObject obj => obj -> Object
|
||||
|
|
11
src/Render/Object/Interface.idr
Normal file
11
src/Render/Object/Interface.idr
Normal file
|
@ -0,0 +1,11 @@
|
|||
module Render.Object.Interface
|
||||
|
||||
import Data.Vect
|
||||
import Render.Color
|
||||
import Render.Camera
|
||||
|
||||
%default total
|
||||
|
||||
public export
|
||||
interface IsObject obj where
|
||||
draw : obj -> Camera -> List (Integer, Integer, ColorAlpha)
|
|
@ -4,12 +4,11 @@ import Data.Vect
|
|||
import Render.Color
|
||||
import Render.Camera
|
||||
import Render.Picture
|
||||
import Render.Object
|
||||
import Render.Object.Interface
|
||||
|
||||
%default total
|
||||
|
||||
|
||||
|
||||
public export
|
||||
record Point where
|
||||
constructor MkPoint
|
||||
|
@ -18,7 +17,7 @@ record Point where
|
|||
|
||||
|
||||
export
|
||||
Object' Point where
|
||||
IsObject Point where
|
||||
draw (MkPoint pos col) cam =
|
||||
let (px,py) = pointToPix cam pos
|
||||
in [(px,py,col)]
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
module Render.Scene
|
||||
|
||||
import Data.DPair
|
||||
import Data.Vect
|
||||
import Data.Buffer
|
||||
import System.File
|
||||
import Render.Color
|
||||
import Render.Camera
|
||||
import Render.Object
|
||||
|
@ -15,7 +18,7 @@ record Scene where
|
|||
bgcolor : Color
|
||||
|
||||
|
||||
public export
|
||||
export
|
||||
render : (cam : Camera) -> Scene -> PictureType cam
|
||||
render cam sc =
|
||||
let blank : PictureType cam = replicate _ (replicate _ sc.bgcolor)
|
||||
|
@ -31,3 +34,26 @@ render cam sc =
|
|||
drawObject pic (MkObject obj) =
|
||||
let pixs = draw obj cam
|
||||
in foldr drawPixel pic pixs
|
||||
|
||||
|
||||
export
|
||||
renderToPPM : HasIO io => (dest : String) -> Camera -> Scene -> io (Either FileError ())
|
||||
renderToPPM dest cam sc = do
|
||||
let bufsize = cast cam.pixw * cast cam.pixh * 3
|
||||
Just buf <- newBuffer bufsize
|
||||
| Nothing => pure $ Right ()
|
||||
|
||||
let pic = render cam sc
|
||||
for_ (zip (tabulate fst) (concat pic)) $ \(i,[r,g,b]) => do
|
||||
setByte buf (cast i * 3) (cast $ r * 255)
|
||||
setByte buf (cast i * 3 + 1) (cast $ g * 255)
|
||||
setByte buf (cast i * 3 + 2) (cast $ b * 255)
|
||||
|
||||
_ <- if !(exists dest) then removeFile {io} dest else pure $ Right ()
|
||||
Right h <- openFile dest Append
|
||||
| Left err => pure $ Left err
|
||||
Right () <- fPutStrLn h "P6\n\{show cam.pixw} \{show cam.pixh}\n255"
|
||||
| Left err => pure $ Left err
|
||||
Right () <- writeBufferData h buf 0 bufsize
|
||||
| Left (err,_) => pure $ Left err
|
||||
pure $ Right ()
|
||||
|
|
Loading…
Reference in a new issue