如何在Haskell和GPipe中的单独文件中定义着色器

时间:2016-12-18 13:22:29

标签: haskell

我对Haskell很新(我在C ++和Java方面经验丰富),所以我的第一次尝试是用GPipe编写3D程序。

目前,我在与#34; main"相同的文件中定义了着色器。程序:

{-# LANGUAGE ScopedTypeVariables, PackageImports, TypeFamilies #-}
module Drawy (drawyRun) where

import Graphics.GPipe
import qualified "GPipe-GLFW" Graphics.GPipe.Context.GLFW as GLFW
import Control.Monad (unless)

mainShader = do
      primitiveStream <- toPrimitiveStream id
      let tPrimStr = fmap (\(pos,col) -> (pos - V4 1 0 0 0, col  - V3 1 0 0)) primitiveStream
      fragmentStream <- rasterize (const (FrontAndBack, ViewPort (V2 0 0) (V2 500 500), DepthRange 0 1)) tPrimStr
      drawContextColor (const (ContextColorOption NoBlending (V3 True True True))) fragmentStream

drawyRun :: IO ()
drawyRun = do
  runContextT GLFW.newContext (ContextFormatColor RGB8) $ do
    vertexBuffer :: Buffer os (B4 Float, B3 Float) <- newBuffer 3
    writeBuffer vertexBuffer 0 [ (V4 (-1) 1 0 1, V3 1 0 0)
                               , (V4 0 (-1) 0 1, V3 0 1 0)
                               , (V4 1 1 0 1,  V3 0 0 1)
                               ]

    shader <- compileShader mainShader

    loop vertexBuffer shader 0.0

loop vertexBuffer shader i = do

  writeBuffer vertexBuffer 0 [ (V4 (-1) 1 0 1, V3 1 0 1)
                             , (V4 0 (-1) 0 1, V3 0 1 0)
                             , (V4 1 0 0 1,  V3 0 0 1)
                             ]

  render $ do
    clearContextColor (V3 0.0 0.0 0.0)
    vertexArray <- newVertexArray vertexBuffer
    let primitiveArray = toPrimitiveArray TriangleList vertexArray
    shader primitiveArray
  swapContextBuffers

  closeRequested <- GLFW.windowShouldClose
  unless closeRequested $
    loop vertexBuffer shader (i+0.1)

但是,我想将着色器放在不同的文件中:

module Shaders(mainShader) where

import Graphics.GPipe
import Control.Monad (unless)

mainShader :: Shader
mainShader = do
      primitiveStream <- toPrimitiveStream id
      let tPrimStr = fmap (\(pos,col) -> (pos - V4 1 0 0 0, col  - V3 1 0 0)) primitiveStream
      fragmentStream <- rasterize (const (FrontAndBack, ViewPort (V2 0 0) (V2 500 500), DepthRange 0 1)) tPrimStr
      drawContextColor (const (ContextColorOption NoBlending (V3 True True True))) fragmentStream

然后,编译器抱怨:

  

Shaders.hs:6:15:错误:       •期待“Shader”的四个论点         预计有一种类型,但'Shader'有种'* - &gt; * - &gt; * - &gt; * - &gt; *”       •在类型签名中:           mainShader :: Shader

在单独的文件中定义着色器的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

实际上,结果是定义需要GHC能够从单个文件中收集的一些额外信息,但是当着色器位于单独的文件中时无法检索。

这是应该如何定义的:

mainShader :: Shader os (ContextFormat RGBFloat ds) (PrimitiveArray p (B4 Float, B3 Float)) ()
mainShader = do
      primitiveStream <- toPrimitiveStream id
      let tPrimStr = fmap (\(pos,col) -> (pos - V4 1 0 0 0, col  - V3 1 0 0)) primitiveStream
      fragmentStream <- rasterize (const (FrontAndBack, ViewPort (V2 0 0) (V2 500 500), DepthRange 0 1)) tPrimStr
      drawContextColor (const (ContextColorOption NoBlending (V3 True True True))) fragmentStream