wxGLCanvas [latest trunk] resize don't work with macOS 10.14.5

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
winnydows
In need of some credit
In need of some credit
Posts: 2
Joined: Sat May 25, 2019 7:36 am

wxGLCanvas [latest trunk] resize don't work with macOS 10.14.5

Post by winnydows » Sat May 25, 2019 8:22 am

If application build with 10.14 SDK wxGLCanvas wrongly resize texture on macOS 10.14.5 (10.14.4 work ok). Result will be cropped from right side if increase frame. Reduce from right side work fine. Increase and reduce from bottom side will be with wrong coordinates. Can be reproduced on any macOS 10.14.5 with sample builded with 10.14 SDK. Cube sample reproduce same problem. Same openGL code work fine if work with native NSOpenGLView without wxWidgets.
wxWidgets builded from latest trunk.

Minimal example:

Code: Select all

#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

#if !wxUSE_GLCANVAS
#error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild the library"
#endif

#include "wx/glcanvas.h"

class MyApp : public wxApp {
public:
    virtual bool OnInit() wxOVERRIDE;
};

wxIMPLEMENT_APP(MyApp);

class MyFrame : public wxFrame {
public:
    MyFrame();
private:
    ~MyFrame();
    void SetCurrent();
    wxGLCanvas *glCanvas;
    wxGLContext *glContext;
    GLuint texture;
};

bool MyApp::OnInit() {
    if (!wxApp::OnInit())
        return false;
    
    wxInitAllImageHandlers();
    new MyFrame();
    
    return true;
}

MyFrame::MyFrame() : wxFrame(NULL, wxID_ANY, "wxWidgets OpenGL Sample") {
    wxGLAttributes dispAttrs;
    dispAttrs.PlatformDefaults().MinRGBA(8, 8, 8, 8).DoubleBuffer().Depth(32).EndList();
    glCanvas = new wxGLCanvas(this, dispAttrs, wxID_ANY,
                              wxDefaultPosition, wxDefaultSize,
                              wxFULL_REPAINT_ON_RESIZE);
    glContext = new wxGLContext(glCanvas);
    
    SetCurrent();
    glGenTextures(1, &texture);
    
    wxString imagePath = wxString("/Users/winnydows/Dev/projects/OpenGL/1080p.png");
    if (wxFileExists(imagePath)) {
        wxImage img = wxImage(imagePath);

        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, texture);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.GetWidth(), img.GetHeight(),
                     0, GL_RGB, GL_UNSIGNED_BYTE, img.GetData());
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // Linear Filtering
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // Linear Filtering
    }
    
    glCanvas->Bind(wxEVT_PAINT, [=](wxPaintEvent& event) {
        // This is required even though dc is not used otherwise.
        wxPaintDC dc(glCanvas);
        SetCurrent();

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);      // Clear Screen And Depth Buffer
        glLoadIdentity();                                        // Reset The Current Matrix
        glBindTexture(GL_TEXTURE_2D, texture);
        glBegin(GL_QUADS);                                       // Start Drawing
        glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0f, -1.0f);      // Bottom Left Of The Texture and Quad
        glTexCoord2f(1.0f, 1.0f); glVertex2f( 1.0f, -1.0f);      // Bottom Right Of The Texture and Quad
        glTexCoord2f(1.0f, 0.0f); glVertex2f( 1.0f,  1.0f);      // Top Right Of The Texture and Quad
        glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f,  1.0f);      // Top Left Of The Texture and Quad
        glEnd();

        glCanvas->SwapBuffers();
    });

    glCanvas->Bind(wxEVT_SIZE, [=](wxSizeEvent& event) {
        event.Skip();
        SetCurrent();

        int w, h;
        glCanvas->GetClientSize(&w, &h);
        glViewport(0, 0, w, h);
        
        glCanvas->Refresh();
    });

    glCanvas->Bind(wxEVT_ERASE_BACKGROUND, [=](wxEraseEvent& event) {
        // Do nothing, to avoid flashing on MSW
    });
    
    SetClientSize(640, 480);
    Show();
}

MyFrame::~MyFrame() {
    delete glContext;
}

void MyFrame::SetCurrent() {
    glCanvas->SetCurrent(*glContext);
}
Attachments
resized.png
Resized window
Last edited by winnydows on Sun May 26, 2019 9:46 am, edited 1 time in total.

Manolo
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 690
Joined: Mon Apr 30, 2012 11:07 pm

Re: wxGLCanvas [latest trunk] resize don't work with macOS 10.14.5

Post by Manolo » Sat May 25, 2019 12:24 pm

Some comments.

Using the display attribute Depth(32) may not work for some old cards (where maximum was 24). You should first check if it's valid with wxGLCanvas::IsDisplaySupported(attrs).

When the window is resized you just call glViewport, but you don't force a redraw. If you think the style wxFULL_REPAINT_ON_RESIZE actually forces a redraw, no, it just tells what portion of the window is redraw. This has no sense in OpenGL, since ir redraws the full viewport part of the window .

So, just call Refresh() after glViewport.

Last but not least: Apple has some buggy OGL drivers and SDK since they decided not to continue supporting OpenGL (last version was 4.1).

winnydows
In need of some credit
In need of some credit
Posts: 2
Joined: Sat May 25, 2019 7:36 am

Re: wxGLCanvas [latest trunk] resize don't work with macOS 10.14.5

Post by winnydows » Sun May 26, 2019 5:21 am

Before try find solution was old

Code: Select all

int attributes[] = { WX_GL_RGBA, WX_GL_DOUBLEBUFFER, 0 };
and work fine on any macOS system. Now don't work on same systems.
Force Refresh need for windows only. But anyway tested with same result. Also tested glViewport in paint event.
I know what Apple start drop OpenGL support, but same gl code still work fine without wxWidgets/wxGLCanvas.

Manolo
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 690
Joined: Mon Apr 30, 2012 11:07 pm

Re: wxGLCanvas [latest trunk] resize don't work with macOS 10.14.5

Post by Manolo » Sun May 26, 2019 12:58 pm

I don't see anything else wrong in your code. And wxWidgets OpenGL stuff is quite simple. However, there have been some changes in the last two years that may be part of your issue.

If the opengl/cube sample (or any of the other opengl samples) doesn't work neither then please open a ticket at https://trac.wxwidgets.org/

Post Reply