From 376375008c8044ba6e5cbc25906cf5f5f8a068dc Mon Sep 17 00:00:00 2001 From: Stefan Dresselhaus Date: Wed, 7 May 2014 09:51:35 +0200 Subject: [PATCH] tessellation-factor is now adaptive - tessellation gets reduced in distance - tess-factor is now adaptive --- shaders/map/tessControl.shader | 31 ++++++++++++++++++++++++++----- src/Main.hs | 12 +++++++----- src/Types.hs | 1 + 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/shaders/map/tessControl.shader b/shaders/map/tessControl.shader index e7a5d25..a6f81b1 100644 --- a/shaders/map/tessControl.shader +++ b/shaders/map/tessControl.shader @@ -10,6 +10,9 @@ out vec4 tcColor[]; out vec3 tcNormal[]; uniform float TessLevelInner = 1.0; // controlled by keyboard buttons uniform float TessLevelOuter = 1.0; // controlled by keyboard buttons +uniform mat4 ProjectionMatrix; +uniform mat4 ViewMatrix; +uniform mat3 NormalMatrix; #define ID gl_InvocationID @@ -18,10 +21,28 @@ void main() tcPosition[ID] = vPosition[ID]; tcColor[ID] = vColor[ID]; tcNormal[ID] = vNormal[ID]; + float dist = (ProjectionMatrix * ViewMatrix * vec4(vPosition[ID], 1)).z; if (ID == 0) { - gl_TessLevelInner[0] = TessLevelInner; - gl_TessLevelOuter[0] = TessLevelOuter; - gl_TessLevelOuter[1] = TessLevelOuter; - gl_TessLevelOuter[2] = TessLevelOuter; + if (dist < 30) { + gl_TessLevelInner[0] = TessLevelInner; + gl_TessLevelOuter[0] = TessLevelOuter; + gl_TessLevelOuter[1] = TessLevelOuter; + gl_TessLevelOuter[2] = TessLevelOuter; + } else if (dist < 50) { + gl_TessLevelInner[0] = max(TessLevelInner-1.0,1.0); + gl_TessLevelOuter[0] = max(TessLevelOuter-1.0,1.0); + gl_TessLevelOuter[1] = max(TessLevelOuter-1.0,1.0); + gl_TessLevelOuter[2] = max(TessLevelOuter-1.0,1.0); + } else if (dist < 100) { + gl_TessLevelInner[0] = max(TessLevelInner-2.0,1.0); + gl_TessLevelOuter[0] = max(TessLevelOuter-2.0,1.0); + gl_TessLevelOuter[1] = max(TessLevelOuter-2.0,1.0); + gl_TessLevelOuter[2] = max(TessLevelOuter-2.0,1.0); + } else { + gl_TessLevelInner[0] = 1.0; + gl_TessLevelOuter[0] = 1.0; + gl_TessLevelOuter[1] = 1.0; + gl_TessLevelOuter[2] = 1.0; + } } -} \ No newline at end of file +} diff --git a/src/Main.hs b/src/Main.hs index 209ae84..f033a77 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -132,6 +132,7 @@ main = } , _io = IOState { _clock = now + , _tessClockFactor = 0 } , _mouse = MouseState { _isDown = False @@ -234,23 +235,24 @@ run = do targetFrametime = 1.0/targetFramerate targetFrametimeμs = targetFrametime * 1000000.0 now <- getCurrentTime - diff <- return $ diffUTCTime now (state ^. io.clock) -- get time-diffs - title <- return $ unwords ["Pioneers @ ",show ((round . double $ 1.0/diff)::Int),"fps"] + let diff = diffUTCTime now (state ^. io.clock) -- get time-diffs + title = unwords ["Pioneers @ ",show ((round . double $ 1.0/diff)::Int),"fps"] setWindowTitle (env ^. windowObject) title let sleepAmount = floor ((targetFrametime - double diff)*1000000) :: Int -- get time until next frame in microseconds + clockFactor = (state ^. io.tessClockFactor) tessChange - | (sleepAmount > (floor $0.1*targetFrametimeμs)) && (state ^. gl.glMap.stateTessellationFactor < 5) = ((+)1 :: Int -> Int) + | (clockFactor > (2*targetFrametimeμs)) && (state ^. gl.glMap.stateTessellationFactor < 5) = ((+)1 :: Int -> Int) -- > factor < 5 & 10% of frame idle -> increase graphics | sleepAmount < 0 && (state ^. gl.glMap.stateTessellationFactor > 1) = (flip (-) 1 :: Int -> Int) -- frame used up completely -> decrease | otherwise = ((+)0 :: Int -> Int) -- 0ms > x > 10% -> keep settings when (sleepAmount > 0) $ threadDelay sleepAmount - print targetFrametimeμs return (now,tessChange,sleepAmount) -- set state with new clock-time modify $ (io.clock .~ mt) . (gl.glMap.stateTessellationFactor %~ tc) - liftIO $ putStrLn $ concat $ ["TessFactor at: ",show (state ^. gl.glMap.stateTessellationFactor), " - slept for ",show sleepAmount, "μs."] + . (io.tessClockFactor %~ (((+) (fromIntegral sleepAmount)).((*) 0.99))) + -- liftIO $ putStrLn $ concat $ ["TessFactor at: ",show (state ^. gl.glMap.stateTessellationFactor), " - slept for ",show sleepAmount, "μs."] shouldClose' <- return $ state ^. window.shouldClose unless shouldClose' run diff --git a/src/Types.hs b/src/Types.hs index 8f6aae6..af45b18 100644 --- a/src/Types.hs +++ b/src/Types.hs @@ -51,6 +51,7 @@ data CameraState = CameraState data IOState = IOState { _clock :: !UTCTime + , _tessClockFactor :: !Double } data GameState = GameState