From de511dc4ed37f71a5cca31e16b0401394f57d5d4 Mon Sep 17 00:00:00 2001 From: benikabocha Date: Sat, 24 Jul 2021 23:58:13 +0900 Subject: [PATCH] #21 fix transparent window problem --- example/simple_mmd_viewer_glfw.cpp | 44 ++++++++++++++++++-- viewer/Saba/Viewer/resource/shader/copy.frag | 12 ++++++ 2 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 viewer/Saba/Viewer/resource/shader/copy.frag diff --git a/example/simple_mmd_viewer_glfw.cpp b/example/simple_mmd_viewer_glfw.cpp index 9ba5b108..be4a18e5 100644 --- a/example/simple_mmd_viewer_glfw.cpp +++ b/example/simple_mmd_viewer_glfw.cpp @@ -1,5 +1,9 @@ #include #include +#if _WIN32 +#define GLFW_EXPOSE_NATIVE_WIN32 +#include +#endif // _WIN32 #include #include @@ -292,7 +296,9 @@ struct AppContext GLuint m_transparentMSAAFbo = 0; GLuint m_copyTransparentWindowShader = 0; GLint m_copyTransparentWindowShaderTex = -1; - GLuint m_copyTransparentWindowVAO = 0; + GLuint m_copyShader = 0; + GLint m_copyShaderTex = -1; + GLuint m_copyVAO = 0; float m_elapsed = 0.0f; float m_animTime = 0.0f; @@ -385,6 +391,7 @@ bool AppContext::Setup() glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, 1, 1, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr); glBindTexture(GL_TEXTURE_2D, 0); + // Create Copy Transparent Window Shader (only windows) m_copyTransparentWindowShader = CreateShaderProgram( saba::PathUtil::Combine(m_shaderDir, "quad.vert"), saba::PathUtil::Combine(m_shaderDir, "copy_transparent_window.frag") @@ -392,7 +399,15 @@ bool AppContext::Setup() m_copyTransparentWindowShaderTex = glGetUniformLocation(m_copyTransparentWindowShader, "u_Tex"); - glGenVertexArrays(1, &m_copyTransparentWindowVAO); + // Copy Shader + m_copyShader = CreateShaderProgram( + saba::PathUtil::Combine(m_shaderDir, "quad.vert"), + saba::PathUtil::Combine(m_shaderDir, "copy.frag") + ); + + m_copyShaderTex = glGetUniformLocation(m_copyShader, "u_Tex"); + + glGenVertexArrays(1, &m_copyVAO); return true; } @@ -421,7 +436,8 @@ void AppContext::Clear() if (m_transparentFboMSAAColorRB != 0) { glDeleteRenderbuffers(1, &m_transparentFboMSAAColorRB); } if (m_transparentFboMSAADepthRB != 0) { glDeleteRenderbuffers(1, &m_transparentFboMSAADepthRB); } if (m_copyTransparentWindowShader != 0) { glDeleteProgram(m_copyTransparentWindowShader); } - if (m_copyTransparentWindowVAO != 0) { glDeleteVertexArrays(1, &m_copyTransparentWindowVAO); } + if (m_copyShader != 0) { glDeleteProgram(m_copyShader); } + if (m_copyVAO != 0) { glDeleteVertexArrays(1, &m_copyVAO); } m_vmdCameraAnim.reset(); } @@ -1253,6 +1269,18 @@ bool SampleMain(std::vector& args) return false; } +#if _WIN32 && (GLFW_VERSION_MAJOR >= 3) && (GLFW_VERSION_MINOR >= 3) && (GLFW_VERSION_REVISION >= 3) + // The color key was removed from glfw3.3.3. (Windows) + if (enableTransparentWindow) + { + HWND hwnd = glfwGetWin32Window(window); + LONG exStyle = GetWindowLongW(hwnd, GWL_EXSTYLE); + exStyle |= WS_EX_LAYERED; + SetWindowLongW(hwnd, GWL_EXSTYLE, exStyle); + SetLayeredWindowAttributes(hwnd, RGB(255, 0, 255), 255, LWA_COLORKEY); + } +#endif // _WIN32 + glfwMakeContextCurrent(window); if (gl3wInit() != 0) @@ -1410,6 +1438,8 @@ bool SampleMain(std::vector& args) { glDisable(GL_MULTISAMPLE); glBindFramebuffer(GL_FRAMEBUFFER, 0); + glClearColor(0, 0, 0, 1); + glClear(GL_COLOR_BUFFER_BIT); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, appContext.m_transparentFbo); glBindFramebuffer(GL_READ_FRAMEBUFFER, appContext.m_transparentMSAAFbo); @@ -1419,8 +1449,14 @@ bool SampleMain(std::vector& args) glBindFramebuffer(GL_READ_FRAMEBUFFER, 0); glDisable(GL_DEPTH_TEST); - glBindVertexArray(appContext.m_copyTransparentWindowVAO); + glBindVertexArray(appContext.m_copyVAO); +#if _WIN32 glUseProgram(appContext.m_copyTransparentWindowShader); +#else // !_WIN32 + glUseProgram(appContext.m_copyShader); + glEnable(GL_BLEND); + glBlendFuncSeparate(GL_ONE, GL_ZERO, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA); +#endif glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, appContext.m_transparentFboColorTex); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); diff --git a/viewer/Saba/Viewer/resource/shader/copy.frag b/viewer/Saba/Viewer/resource/shader/copy.frag new file mode 100644 index 00000000..01926737 --- /dev/null +++ b/viewer/Saba/Viewer/resource/shader/copy.frag @@ -0,0 +1,12 @@ +#version 140 + +in vec2 vs_UV; + +uniform sampler2D u_Tex; + +out vec4 fs_Color; + +void main() +{ + fs_Color = texture(u_Tex, vs_UV); +}