Search code examples
openglhaskellshadervertices

How do I send a uniform matrix to a shader in Haskell?


Trying to do essentially do matrix multiplication on vertices in shaders for transformations and it's uh, not having it.

At the moment, I've got this:

curMatrix <- get ((matrix $ Just $ Modelview 0)::StateVar(GLmatrix GLfloat))
       mc <- getMatrixComponents ColumnMajor curMatrix
setUniform p "uModelViewMatrix" mc 

setUniform being a function in the GLUtil package. Do I need to convert my array to a Vec4 (Vec4), or is there another way?

Thanks!

Edit:

I've ended up doing this:

getMat :: [GLfloat] -> [V4 GLfloat]
getMat (a11:a12:a13:a14: 
        a21:a22:a23:a24:
        a31:a32:a33:a34:
        a41:a42:a43:a44:_) = [(V4 a11 a12 a13 a14),
                              (V4 a21 a22 a23 a24),
                              (V4 a31 a32 a33 a34),
                              (V4 a41 a42 a43 a44)]

And uh, well that doesn't seem to have worked!

Edit 2: What I mean by it didn't work is, that I got it to set it as a uniform variable, but now my object has disappeared!


Solution

  • Got it working! Main reason it wasn't working is because I wasn't using the projection matrix as well.

    Please don't beat me with sticks for my bad/confusing variable names.

        mvMatrix <- get ((matrix $ Just $ Modelview 0)::StateVar(GLmatrix GLfloat))
        pMatrix <- get ((matrix $ Just Projection)::StateVar(GLmatrix GLfloat))
        mv <- getMatrixComponents ColumnMajor mvMatrix
        pm <-   getMatrixComponents ColumnMajor pMatrix
        let mvm = getMat mv
        let pM = getMat pm 
        let mvmp = mvm !*! pM
        setUniform p "uModelViewMatrix" mvmp
    

    My getMat function is as:

    getMat :: [GLfloat] -> V4 (V4 GLfloat)
    getMat (a11:a12:a13:a14: 
                a21:a22:a23:a24:
                a31:a32:a33:a34:
                a41:a42:a43:a44:_) = V4 (V4 a11 a12 a13 a14)
                 (V4 a21 a22 a23 a24)
                 (V4 a31 a32 a33 a34)
                 (V4 a41 a42 a43 a44)
    

    I'm using the Linear package. The !*! infix function is a matrix multiplication.

    This works exactly as I wanted it to, so yay! Hope it helps anyone else!