From 3fd6f27768f45ff26dd518b654b992ae0ee76d1b Mon Sep 17 00:00:00 2001 From: bjorn Date: Thu, 8 Feb 2018 21:26:53 -0800 Subject: [PATCH] Allow custom values when clearing color, depth, and stencil buffers; --- src/api/graphics.c | 45 ++++++++++++++++++++++++++++++++++++----- src/graphics/canvas.c | 2 +- src/graphics/graphics.c | 16 ++++++++++++--- src/graphics/graphics.h | 2 +- src/headset/fake.c | 2 +- src/headset/openvr.c | 2 +- 6 files changed, 57 insertions(+), 12 deletions(-) diff --git a/src/api/graphics.c b/src/api/graphics.c index f4fb6f9d..d334fc46 100644 --- a/src/api/graphics.c +++ b/src/api/graphics.c @@ -290,10 +290,44 @@ int l_lovrGraphicsReset(lua_State* L) { } int l_lovrGraphicsClear(lua_State* L) { - bool color = lua_gettop(L) < 1 || lua_toboolean(L, 1); - bool depth = lua_gettop(L) < 2 || lua_toboolean(L, 2); - bool stencil = lua_gettop(L) < 3 || lua_toboolean(L, 3); - lovrGraphicsClear(color, depth, stencil); + int index = 1; + int top = lua_gettop(L); + + bool clearColor = true; + bool clearDepth = true; + bool clearStencil = true; + Color color = lovrGraphicsGetBackgroundColor(); + float depth = 1.0; + int stencil = 0; + + if (top >= index) { + if (lua_type(L, index) == LUA_TNUMBER) { + color.r = luaL_checknumber(L, index++); + color.g = luaL_checknumber(L, index++); + color.b = luaL_checknumber(L, index++); + color.a = luaL_optnumber(L, index++, 1.); + } else { + clearColor = lua_toboolean(L, index++); + } + } + + if (top >= index) { + if (lua_type(L, index) == LUA_TNUMBER) { + depth = luaL_checknumber(L, index++); + } else { + clearDepth = lua_toboolean(L, index++); + } + } + + if (top >= index) { + if (lua_type(L, index) == LUA_TNUMBER) { + stencil = luaL_checkinteger(L, index++); + } else { + clearStencil = lua_toboolean(L, index++); + } + } + + lovrGraphicsClear(clearColor, clearDepth, clearStencil, color, depth, stencil); return 0; } @@ -787,7 +821,8 @@ int l_lovrGraphicsStencil(lua_State* L) { int replaceValue = luaL_optinteger(L, 3, 1); bool keepValues = lua_toboolean(L, 4); if (!keepValues) { - lovrGraphicsClear(false, false, true); + int clearTo = lua_isnumber(L, 4) ? lua_tonumber(L, 4) : 0; + lovrGraphicsClear(false, false, true, (Color) { 0, 0, 0, 0 }, 0, clearTo); } lua_settop(L, 1); lovrGraphicsStencil(action, replaceValue, stencilCallback, L); diff --git a/src/graphics/canvas.c b/src/graphics/canvas.c index e9db0013..521dc74b 100644 --- a/src/graphics/canvas.c +++ b/src/graphics/canvas.c @@ -78,7 +78,7 @@ Canvas* lovrCanvasCreate(int width, int height, TextureFormat format, CanvasType } lovrAssert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE, "Error creating Canvas"); - lovrGraphicsClear(true, true, true); + lovrGraphicsClear(true, true, true, (Color) { 0, 0, 0, 0 }, 1., 0); glBindFramebuffer(GL_FRAMEBUFFER, 0); return canvas; diff --git a/src/graphics/graphics.c b/src/graphics/graphics.c index f122276a..708355a5 100644 --- a/src/graphics/graphics.c +++ b/src/graphics/graphics.c @@ -81,9 +81,19 @@ void lovrGraphicsReset() { lovrGraphicsOrigin(); } -void lovrGraphicsClear(bool color, bool depth, bool stencil) { - if (!color && !depth && !stencil) return; - glClear((color ? GL_COLOR_BUFFER_BIT : 0) | (depth ? GL_DEPTH_BUFFER_BIT : 0) | (stencil ? GL_STENCIL_BUFFER_BIT : 0)); +void lovrGraphicsClear(bool clearColor, bool clearDepth, bool clearStencil, Color color, float depth, int stencil) { + if (clearColor) { + float c[4] = { color.r, color.g, color.b, color.a }; + glClearBufferfv(GL_COLOR, 0, c); + } + + if (clearDepth) { + glClearBufferfv(GL_DEPTH, 0, &depth); + } + + if (clearStencil) { + glClearBufferiv(GL_STENCIL, 0, &stencil); + } } void lovrGraphicsPresent() { diff --git a/src/graphics/graphics.h b/src/graphics/graphics.h index 2c885af9..5d2aef89 100644 --- a/src/graphics/graphics.h +++ b/src/graphics/graphics.h @@ -139,7 +139,7 @@ typedef struct { void lovrGraphicsInit(); void lovrGraphicsDestroy(); void lovrGraphicsReset(); -void lovrGraphicsClear(bool color, bool depth, bool stencil); +void lovrGraphicsClear(bool clearColor, bool clearDepth, bool clearStencil, Color color, float depth, int stencil); void lovrGraphicsPresent(); void lovrGraphicsPrepare(Material* material, float* pose); void lovrGraphicsCreateWindow(int w, int h, bool fullscreen, int msaa, const char* title, const char* icon); diff --git a/src/headset/fake.c b/src/headset/fake.c index 1a70f76c..6f495a22 100644 --- a/src/headset/fake.c +++ b/src/headset/fake.c @@ -348,7 +348,7 @@ static void fakeRenderTo(headsetRenderCallback callback, void* userdata) { lovrGraphicsMatrixTransform(MATRIX_VIEW, inv); lovrGraphicsSetProjection(state.projection); - lovrGraphicsClear(true, true, true); + lovrGraphicsClear(true, true, true, lovrGraphicsGetBackgroundColor(), 1., 0); callback(EYE_LEFT, userdata); lovrGraphicsPop(); } diff --git a/src/headset/openvr.c b/src/headset/openvr.c index a4e8a0cd..4089537a 100644 --- a/src/headset/openvr.c +++ b/src/headset/openvr.c @@ -756,7 +756,7 @@ static void openvrRenderTo(headsetRenderCallback callback, void* userdata) { lovrGraphicsPush(); lovrGraphicsMatrixTransform(MATRIX_VIEW, transform); lovrGraphicsSetProjection(projection); - lovrGraphicsClear(true, true, false); + lovrGraphicsClear(true, true, false, (Color) { 0, 0, 0, 0 }, 1., 0); callback(eye, userdata); lovrGraphicsPop(); lovrCanvasResolveMSAA(state.canvas);