From ae8f12e8bac8c79f3218036b295acaf6e17ff26e Mon Sep 17 00:00:00 2001 From: Erwin Coumans Date: Wed, 20 Sep 2017 13:09:18 -0700 Subject: [PATCH] more fixes for retina + glfw + opengl2 fallback --- data/multibody.bullet | Bin 14844 -> 14844 bytes .../ExampleBrowser/OpenGLExampleBrowser.cpp | 16 ++++++--- examples/OpenGLWindow/GLFWOpenGLWindow.cpp | 16 ++++----- examples/OpenGLWindow/SimpleOpenGL2App.cpp | 17 +++++++--- .../Gwen/Renderers/OpenGL_DebugFont.cpp | 31 ++++++++++-------- .../Gwen/Renderers/OpenGL_DebugFont.h | 6 ++-- examples/ThirdPartyLibs/glad/glad.c | 3 +- test/GwenOpenGLTest/OpenGLSample.cpp | 8 ++--- 8 files changed, 59 insertions(+), 38 deletions(-) diff --git a/data/multibody.bullet b/data/multibody.bullet index bb56ec2891c1b4ab03b3c006a73efd35cae11a27..e57061f7ae941a64931368b8fc823d88f3592bad 100644 GIT binary patch delta 358 zcmexU{HJ)rmdPTVHWNS8_zO6MZQ54P00RL~Mu0E_M4SgI-~dy>1Y>~dfNWXS?BDDR z7g!kzfG}tBZbow<4u*(AzJ>phOr4y_C^1=vNkbinF$@S}f*3*>T!HFkCwDOA;8Krf zZ81>2xsfVEgCoSz6HqoZ83N*=SvJVRfGY13f=5Us!rT~`#M-2b~ delta 87 zcmexU{HJ)r7Dl1T3t5yWZ(xy{_^E2*1GdQ%Sp6nHU}Twmgi&v@0*lsU877U%y{s;i qJD74Ncd~|pm_renderer = new SimpleOpenGL2Renderer(width,height); } @@ -932,7 +938,7 @@ bool OpenGLExampleBrowser::init(int argc, char* argv[]) else { char title[1024]; - sprintf(title,"%s using OpenGL3+. %s", appTitle,optMode); + sprintf(title,"%s using OpenGL3+ %s %s", appTitle,glContext, optMode); simpleApp = new SimpleOpenGL3App(title,width,height, gAllowRetina); s_app = simpleApp; } @@ -1001,7 +1007,7 @@ bool OpenGLExampleBrowser::init(int argc, char* argv[]) if (sUseOpenGL2) { - m_internalData->m_gwenRenderer = new Gwen::Renderer::OpenGL_DebugFont(); + m_internalData->m_gwenRenderer = new Gwen::Renderer::OpenGL_DebugFont(s_window->getRetinaScale()); } #ifndef NO_OPENGL3 else @@ -1341,7 +1347,7 @@ void OpenGLExampleBrowser::update(float deltaTime) if (sUseOpenGL2) { - saveOpenGLState(s_instancingRenderer->getScreenWidth(), s_instancingRenderer->getScreenHeight()); + saveOpenGLState(s_instancingRenderer->getScreenWidth()*s_window->getRetinaScale(), s_instancingRenderer->getScreenHeight()*s_window->getRetinaScale()); } if (m_internalData->m_gui) diff --git a/examples/OpenGLWindow/GLFWOpenGLWindow.cpp b/examples/OpenGLWindow/GLFWOpenGLWindow.cpp index dbc6434bf..832e6d7cf 100644 --- a/examples/OpenGLWindow/GLFWOpenGLWindow.cpp +++ b/examples/OpenGLWindow/GLFWOpenGLWindow.cpp @@ -241,12 +241,14 @@ void GLFWOpenGLWindow::mouseCursorCallbackInternal(double xPos, double yPos) void GLFWOpenGLWindow::resizeInternal(int width,int height) { - if (getResizeCallback()) - { - getResizeCallback()(width,height); - } + glfwGetFramebufferSize(m_data->m_glfwWindow, &m_data->m_width, &m_data->m_height); glViewport (0,0,m_data->m_width,m_data->m_height); + + if (getResizeCallback()) + { + getResizeCallback()(m_data->m_width/m_data->m_retinaScaleFactor,m_data->m_height / m_data->m_retinaScaleFactor); + } } void GLFWOpenGLWindow::createDefaultWindow(int width, int height, const char* title) @@ -272,12 +274,10 @@ void GLFWOpenGLWindow::createWindow(const b3gWindowConstructionInfo& ci) if (ci.m_openglVersion==2) { - printf("V2\n"); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); } else { - printf("V3\n"); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); @@ -309,8 +309,8 @@ void GLFWOpenGLWindow::createWindow(const b3gWindowConstructionInfo& ci) glfwGetFramebufferSize(m_data->m_glfwWindow, &m_data->m_width, &m_data->m_height); int windowWidth, windowHeight; glfwGetWindowSize(m_data->m_glfwWindow, &windowWidth, &windowHeight); - m_data->m_retinaScaleFactor = float(m_data->m_width)/float(windowWidth); - glViewport(0,0,m_data->m_width, m_data->m_height); + m_data->m_retinaScaleFactor = float(m_data->m_width)/float(windowWidth); + glViewport(0,0,m_data->m_width, m_data->m_height); } } diff --git a/examples/OpenGLWindow/SimpleOpenGL2App.cpp b/examples/OpenGLWindow/SimpleOpenGL2App.cpp index e25f76b8d..3cc45e9e4 100644 --- a/examples/OpenGLWindow/SimpleOpenGL2App.cpp +++ b/examples/OpenGLWindow/SimpleOpenGL2App.cpp @@ -46,9 +46,9 @@ static void Simple2ResizeCallback( float widthf, float heightf) { int width = (int)widthf; int height = (int)heightf; - if (gApp2->m_renderer) - gApp2->m_renderer->resize(width,height); - //gApp2->m_renderer->setScreenSize(width,height); + if (gApp2->m_renderer && gApp2->m_window) + gApp2->m_renderer->resize(width,height);//*gApp2->m_window->getRetinaScale(),height*gApp2->m_window->getRetinaScale()); + } @@ -65,11 +65,18 @@ static void Simple2KeyboardCallback(int key, int state) void Simple2MouseButtonCallback( int button, int state, float x, float y) { - gApp2->defaultMouseButtonCallback(button,state,x,y); + if (gApp2 && gApp2->m_window) + { + gApp2->defaultMouseButtonCallback(button,state,x,y); + } } void Simple2MouseMoveCallback( float x, float y) { - gApp2->defaultMouseMoveCallback(x,y); + + if (gApp2 && gApp2->m_window) + { + gApp2->defaultMouseMoveCallback(x,y); + } } void Simple2WheelCallback( float deltax, float deltay) diff --git a/examples/ThirdPartyLibs/Gwen/Renderers/OpenGL_DebugFont.cpp b/examples/ThirdPartyLibs/Gwen/Renderers/OpenGL_DebugFont.cpp index 71f223b6e..e69ff813d 100644 --- a/examples/ThirdPartyLibs/Gwen/Renderers/OpenGL_DebugFont.cpp +++ b/examples/ThirdPartyLibs/Gwen/Renderers/OpenGL_DebugFont.cpp @@ -153,7 +153,9 @@ namespace Gwen { namespace Renderer { - OpenGL_DebugFont::OpenGL_DebugFont() + + OpenGL_DebugFont::OpenGL_DebugFont(float retinaScale) + :m_retinaScale(retinaScale) { m_iVertNum = 0; @@ -260,8 +262,8 @@ namespace Gwen Flush(); } - m_Vertices[ m_iVertNum ].x = (float)x; - m_Vertices[ m_iVertNum ].y = (float)y; + m_Vertices[ m_iVertNum ].x = (float)x*m_retinaScale; + m_Vertices[ m_iVertNum ].y = (float)y*m_retinaScale; m_Vertices[ m_iVertNum ].u = u; m_Vertices[ m_iVertNum ].v = v; @@ -306,16 +308,19 @@ namespace Gwen Flush(); Gwen::Rect rect = ClipRegion(); - // OpenGL's coords are from the bottom left - // so we need to translate them here. - { - GLint view[4]; - glGetIntegerv( GL_VIEWPORT, &view[0] ); - rect.y = view[3] - (rect.y + rect.h); - } - - glScissor( rect.x * Scale(), rect.y * Scale(), rect.w * Scale(), rect.h * Scale() ); - glEnable( GL_SCISSOR_TEST ); + float retinaScale = m_retinaScale; + // OpenGL's coords are from the bottom left + // so we need to translate them here. + { + GLint view[4]; + glGetIntegerv( GL_VIEWPORT, &view[0] ); + rect.y = view[3]/retinaScale - (rect.y + rect.h); + } + + glScissor( retinaScale * rect.x * Scale(), retinaScale * rect.y * Scale(), retinaScale * rect.w * Scale(), retinaScale * rect.h * Scale() ); + glEnable( GL_SCISSOR_TEST ); + //glDisable( GL_SCISSOR_TEST ); + }; void OpenGL_DebugFont::EndClip() diff --git a/examples/ThirdPartyLibs/Gwen/Renderers/OpenGL_DebugFont.h b/examples/ThirdPartyLibs/Gwen/Renderers/OpenGL_DebugFont.h index 021a71443..00ea814df 100644 --- a/examples/ThirdPartyLibs/Gwen/Renderers/OpenGL_DebugFont.h +++ b/examples/ThirdPartyLibs/Gwen/Renderers/OpenGL_DebugFont.h @@ -21,6 +21,8 @@ namespace Gwen class OpenGL_DebugFont : public Gwen::Renderer::Base { + float m_retinaScale; + public: struct Vertex @@ -32,8 +34,8 @@ namespace Gwen static const int MaxVerts = 1024; - - OpenGL_DebugFont(); + + OpenGL_DebugFont(float retinaScale); ~OpenGL_DebugFont(); void RenderText( Gwen::Font* pFont, Gwen::Point pos, const Gwen::UnicodeString& text ); diff --git a/examples/ThirdPartyLibs/glad/glad.c b/examples/ThirdPartyLibs/glad/glad.c index b0e82f840..81af8bb5e 100644 --- a/examples/ThirdPartyLibs/glad/glad.c +++ b/examples/ThirdPartyLibs/glad/glad.c @@ -1852,6 +1852,7 @@ static void load_GL_VERSION_2_0(GLADloadproc load) { glad_glVertexAttrib4uiv = (PFNGLVERTEXATTRIB4UIVPROC)load("glVertexAttrib4uiv"); glad_glVertexAttrib4usv = (PFNGLVERTEXATTRIB4USVPROC)load("glVertexAttrib4usv"); glad_glVertexAttribPointer = (PFNGLVERTEXATTRIBPOINTERPROC)load("glVertexAttribPointer"); + glad_glGenerateMipmap = (PFNGLGENERATEMIPMAPPROC)load("glGenerateMipmap"); } static void load_GL_VERSION_2_1(GLADloadproc load) { if(!GLAD_GL_VERSION_2_1) return; @@ -1938,7 +1939,7 @@ static void load_GL_VERSION_3_0(GLADloadproc load) { glad_glFramebufferTexture3D = (PFNGLFRAMEBUFFERTEXTURE3DPROC)load("glFramebufferTexture3D"); glad_glFramebufferRenderbuffer = (PFNGLFRAMEBUFFERRENDERBUFFERPROC)load("glFramebufferRenderbuffer"); glad_glGetFramebufferAttachmentParameteriv = (PFNGLGETFRAMEBUFFERATTACHMENTPARAMETERIVPROC)load("glGetFramebufferAttachmentParameteriv"); - glad_glGenerateMipmap = (PFNGLGENERATEMIPMAPPROC)load("glGenerateMipmap"); + glad_glBlitFramebuffer = (PFNGLBLITFRAMEBUFFERPROC)load("glBlitFramebuffer"); glad_glRenderbufferStorageMultisample = (PFNGLRENDERBUFFERSTORAGEMULTISAMPLEPROC)load("glRenderbufferStorageMultisample"); glad_glFramebufferTextureLayer = (PFNGLFRAMEBUFFERTEXTURELAYERPROC)load("glFramebufferTextureLayer"); diff --git a/test/GwenOpenGLTest/OpenGLSample.cpp b/test/GwenOpenGLTest/OpenGLSample.cpp index 442af7574..89a4d908c 100644 --- a/test/GwenOpenGLTest/OpenGLSample.cpp +++ b/test/GwenOpenGLTest/OpenGLSample.cpp @@ -345,10 +345,12 @@ int main() sprintf(title,"Gwen with OpenGL %d\n",wci.m_openglVersion); } window->setWindowTitle(title); + + float retinaScale = window->getRetinaScale(); + #ifndef NO_OPENGL3 if (majorGlVersion>=3 && wci.m_openglVersion>=3) { - float retinaScale = 1.f; #ifndef B3_USE_GLFW #ifndef __APPLE__ #ifndef _WIN32 @@ -365,8 +367,6 @@ int main() assert(err==GL_NO_ERROR); - retinaScale = window->getRetinaScale(); - primRenderer = new GLPrimitiveRenderer(sWidth,sHeight); sth_stash* font = initFont(primRenderer ); @@ -378,7 +378,7 @@ int main() #endif { //OpenGL 2.x - gwenRenderer = new Gwen::Renderer::OpenGL_DebugFont(); + gwenRenderer = new Gwen::Renderer::OpenGL_DebugFont(retinaScale); skin.SetRender( gwenRenderer );