Question

I'm working on a Haskell raytracer. I have the following Camera-type:

data Cameras = Ortographic | Pinhole {
    d :: Float,
    zoom :: Float,
    eye, lookAt, up :: Vector,
    cu, cv, cw :: Vector
} deriving (Show)

And the following Camera-typeclass:

class Camera a where
    renderPixel :: a -> (Float, Float) -> [Object] -> Float -> Vector
    rayDirection :: a -> Vector -> Vector

Now, when I try to make the type an instance of the typeclass, like this:

instance Camera Cameras where
    --Ortographic
    renderPixel (Ortographic) (x, y) scene numSamples = ...

    --Pinhole
    rayDirection (Pinhole d _ _ _ _ cu cv cw) (Vector2 u v) =
        normalize ((cu<*>u) <+> (cv<*>v) <-> (cw<*>d))

    renderPixel (Pinhole d _ _ _ _ cu cv cw) (x, y) scene numSamples = ...

I get an error saying "Conflicting definitions for `renderPixel'", pointing to the lines where the renderPixel-function starts for each of the cameras. What am I doing wrong?

Was it helpful?

Solution

I am quite sure the two renderPixel equations have to be right after each other. That is, the rayDirection function should be moved to before or after both renderPixel equations.

A function can have several equations (lines) due to pattern matching, but it is still a single function, and you can't shove another function definition in between the equations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top