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

module Quaazar.Render.Forward.Renderer where

import Control.Applicative
import Control.Lens
import Control.Monad.Error.Class ( MonadError )
import Control.Monad.Trans ( MonadIO(..) )
import Data.Bits ( (.|.) )
import Graphics.Rendering.OpenGL.Raw
import Graphics.UI.GLFW ( Window )
import Numeric.Natural ( Natural )
import Quaazar.Render.Compositing ( Compositor, copyVS, copyFS )
import Quaazar.Render.Forward.Accumulation ( Accumulation, getAccumulation )
import Quaazar.Render.Forward.Lighting ( Lighting, getLighting )
import Quaazar.Render.GL.Framebuffer ( Target(ReadWrite), unbindFramebuffer )
import Quaazar.Render.GL.Shader ( Program, buildProgram, useProgram )
import Quaazar.Render.GL.VertexArray ( VertexArray, bindVertexArray
                                     , genAttributelessVertexArray )
import Quaazar.Render.Texture ( GPUTexture )
import Quaazar.Utils.Log
import Quaazar.Utils.Scoped

data ForwardRenderer = ForwardRenderer {
    _frCopyProgram :: Program
  , _frLighting    :: Lighting
  , _frAccumulation :: Accumulation
  , _frVA          :: VertexArray
  , _frWindow      :: Window
  }

makeLenses ''ForwardRenderer

getRenderer :: (Applicative m,MonadScoped IO m,MonadIO m,MonadLogger m,MonadError Log m)
            => Natural
            -> Natural
            -> Natural
            -> Natural
            -> Window
            -> m ForwardRenderer
getRenderer w h shadowDef lightNb window =
  ForwardRenderer
    <$> getCopyProgram
    <*> getLighting w h lightNb
    <*> getAccumulation w h
    <*> genAttributelessVertexArray
    <*> pure window

getCopyProgram :: (MonadIO m,MonadScoped IO m,MonadLogger m,MonadError Log m)
               => m Program
getCopyProgram = buildProgram copyVS Nothing Nothing copyFS

display :: (MonadIO m) => ForwardRenderer -> Compositor () GPUTexture -> m ()
display rdr compt = liftIO $ do
  useProgram (rdr^.frCopyProgram)
  unbindFramebuffer ReadWrite
  glClearColor 0 0 0 0
  glClear $ gl_COLOR_BUFFER_BIT .|. gl_DEPTH_BUFFER_BIT
  bindVertexArray (rdr^.frVA)
  glDrawArrays gl_TRIANGLE_STRIP 0 4
<commit_msg>Added swapBuffers in display.<commit_after>-----------------------------------------------------------------------------
-- |
-- Copyright   : (C) 2015 <NAME>
-- License     : BSD3
--
-- Maintainer  : <NAME> <<EMAIL>>
-- Stability   : experimental
-- Portability : portable
--
----------------------------------------------------------------------------

module Quaazar.Render.Forward.Renderer where

import Control.Applicative
import Control.Lens
import Control.Monad.Error.Class ( MonadError )
import Control.Monad.Trans ( MonadIO(..) )
import Data.Bits ( (.|.) )
import Graphics.Rendering.OpenGL.Raw
import Graphics.UI.GLFW ( Window, swapBuffers )
import Numeric.Natural ( Natural )
import Quaazar.Render.Compositing ( Compositor, copyVS, copyFS )
import Quaazar.Render.Forward.Accumulation ( Accumulation, getAccumulation )
import Quaazar.Render.Forward.Lighting ( Lighting, getLighting )
import Quaazar.Render.GL.Framebuffer ( Target(ReadWrite), unbindFramebuffer )
import Quaazar.Render.GL.Shader ( Program, buildProgram, useProgram )
import Quaazar.Render.GL.VertexArray ( VertexArray, bindVertexArray
                                     , genAttributelessVertexArray )
import Quaazar.Render.Texture ( GPUTexture )
import Quaazar.Utils.Log
import Quaazar.Utils.Scoped

data ForwardRenderer = ForwardRenderer {
    _frCopyProgram :: Program
  , _frLighting    :: Lighting
  , _frAccumulation :: Accumulation
  , _frVA          :: VertexArray
  , _frWindow      :: Window
  }

makeLenses ''ForwardRenderer

getRenderer :: (Applicative m,MonadScoped IO m,MonadIO m,MonadLogger m,MonadError Log m)
            => Natural
            -> Natural
            -> Natural
            -> Natural
            -> Window
            -> m ForwardRenderer
getRenderer w h shadowDef lightNb window =
  ForwardRenderer
    <$> getCopyProgram
    <*> getLighting w h lightNb
    <*> getAccumulation w h
    <*> genAttributelessVertexArray
    <*> pure window

getCopyProgram :: (MonadIO m,MonadScoped IO m,MonadLogger m,MonadError Log m)
               => m Program
getCopyProgram = buildProgram copyVS Nothing Nothing copyFS

display :: (MonadIO m) => ForwardRenderer -> Compositor () GPUTexture -> m ()
display rdr compt = liftIO $ do
  useProgram (rdr^.frCopyProgram)
  unbindFramebuffer ReadWrite
  glClearColor 0 0 0 0
  glClear $ gl_COLOR_BUFFER_BIT .|. gl_DEPTH_BUFFER_BIT
  bindVertexArray (rdr^.frVA)
  glDrawArrays gl_TRIANGLE_STRIP 0 4
  liftIO $ swapBuffers (rdr^.frWindow)

{'fraction_non_alphanumeric': 0.15090090090090091, 'fraction_numerical': 0.0045045045045045045, 'mean_word_length': 2.589272593681117, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 41, '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': '5922191', 'n_tokens_mistral': 1458, 'n_tokens_neox': 1414, 'n_words': 339}