<commit_before>-----------------------------------------------------------------------------
-- |
-- Copyright   : (C) 2015 <NAME>
-- License     : BSD3
--
-- Maintainer  : <NAME> <<EMAIL>>
-- Stability   : experimental
-- Portability : portable
--
----------------------------------------------------------------------------

module Quaazar.Render.Lit where

import Control.Lens
import Control.Monad.State ( evalState )
import Data.Bits ( (.|.) )
import Data.Monoid ( Monoid(..) )
import Data.Foldable ( for_ )
import Graphics.Rendering.OpenGL.Raw
import Quaazar.Core.Light
import Quaazar.Core.Transform
import Quaazar.Render.Lighting
import Quaazar.Render.Rendered ( Rendered(..) )
import Quaazar.Render.GL.Buffer ( Buffer )
import Quaazar.Render.GL.Framebuffer ( Framebuffer, Target(..)
                                     , bindFramebuffer )
import Quaazar.Render.GL.Shader ( (@=) )
import Quaazar.Render.Light

newtype Lit mat = Lit {
    unLit :: Framebuffer                -- ^ lighting framebuffer
          -> Buffer                     -- ^ omni light buffer
          -> Maybe (ShadowConf,Shadows) -- ^ shadows configuration
          -> (mat -> IO ())             -- ^ material sink
          -> IO ()
  }

lighten :: Ambient -> [(Omni,Transform)] -> Rendered mat -> Lit mat
lighten (Ambient ligAmbCol ligAmbPow) omnis shd = Lit lighten_
  where
    lighten_ lightingFB omniBuffer shadowsConf sinkMat = do
      omnisWithShadows <- case shadowsConf of
        Just (conf,_) -> do 
          let
            omnisWithShadows = flip evalState (0,0,0) $ mapM (addShadowInfo_ lmax mmax hmax) omnis
            lmax = conf^.lowShadowMaxNb
            mmax = conf^.mediumShadowMaxNb
            hmax = conf^.highShadowMaxNb
          -- TODO: create shadowmaps
          return omnisWithShadows
        Nothing -> return $ map addNoShadows omnis
      purgeLightingFramebuffer lightingFB
      ligAmbColUniform @= ligAmbCol
      ligAmbPowUniform @= ligAmbPow
      pushOmnis omnisWithShadows omniBuffer
      unRendered shd modelUniform sinkMat
    addShadowInfo_ lmax mmax hmax (omni,transform) = do
      (omni',lod,index) <- addShadowInfo lmax mmax hmax omni
      return (omni',lod,index,transform)
    addNoShadows (omni,transform) = (omni,0,0,transform)

purgeLightingFramebuffer :: Framebuffer -> IO ()
purgeLightingFramebuffer fb = do
  bindFramebuffer fb ReadWrite
  glClearColor 0 0 0 0
  glClear $ gl_DEPTH_BUFFER_BIT .|. gl_COLOR_BUFFER_BIT
<commit_msg>Names.<commit_after>-----------------------------------------------------------------------------
-- |
-- Copyright   : (C) 2015 <NAME>
-- License     : BSD3
--
-- Maintainer  : <NAME> <<EMAIL>>
-- Stability   : experimental
-- Portability : portable
--
----------------------------------------------------------------------------

module Quaazar.Render.Lit where

import Control.Lens
import Control.Monad.State ( evalState )
import Data.Bits ( (.|.) )
import Data.Monoid ( Monoid(..) )
import Data.Foldable ( for_ )
import Graphics.Rendering.OpenGL.Raw
import Quaazar.Core.Light
import Quaazar.Core.Transform
import Quaazar.Render.Lighting
import Quaazar.Render.Rendered ( Rendered(..) )
import Quaazar.Render.GL.Buffer ( Buffer )
import Quaazar.Render.GL.Framebuffer ( Framebuffer, Target(..)
                                     , bindFramebuffer )
import Quaazar.Render.GL.Shader ( (@=) )
import Quaazar.Render.Light

newtype Lit mat = Lit {
    unLit :: Framebuffer                -- ^ framebuffer
          -> Buffer                     -- ^ omni light buffer
          -> Maybe (ShadowConf,Shadows) -- ^ shadows configuration
          -> (mat -> IO ())             -- ^ material sink
          -> IO ()
  }

lighten :: Ambient -> [(Omni,Transform)] -> Rendered mat -> Lit mat
lighten (Ambient ligAmbCol ligAmbPow) omnis shd = Lit lighten_
  where
    lighten_ fb omniBuffer shadowsConf sinkMat = do
      omnisWithShadows <- case shadowsConf of
        Just (conf,_) -> do 
          let
            omnisWithShadows = flip evalState (0,0,0) $ mapM (addShadowInfo_ lmax mmax hmax) omnis
            lmax = conf^.lowShadowMaxNb
            mmax = conf^.mediumShadowMaxNb
            hmax = conf^.highShadowMaxNb
          -- TODO: create shadowmaps
          return omnisWithShadows
        Nothing -> return $ map addNoShadows omnis
      purgeLightingFramebuffer lightingFB
      ligAmbColUniform @= ligAmbCol
      ligAmbPowUniform @= ligAmbPow
      pushOmnis omnisWithShadows omniBuffer
      unRendered shd modelUniform sinkMat
    addShadowInfo_ lmax mmax hmax (omni,transform) = do
      (omni',lod,index) <- addShadowInfo lmax mmax hmax omni
      return (omni',lod,index,transform)
    addNoShadows (omni,transform) = (omni,0,0,transform)

purgeLightingFramebuffer :: Framebuffer -> IO ()
purgeLightingFramebuffer fb = do
  bindFramebuffer fb ReadWrite
  glClearColor 0 0 0 0
  glClear $ gl_DEPTH_BUFFER_BIT .|. gl_COLOR_BUFFER_BIT

{'fraction_non_alphanumeric': 0.1550182852498984, 'fraction_numerical': 0.005688744412840309, 'mean_word_length': 2.078799249530957, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '326257', 'n_tokens_mistral': 1488, 'n_tokens_neox': 1410, 'n_words': 353}