Bug when trying to draw sub section of OpenGLImageType


#1

I'm trying to use the OpenGLImageType class but it won't display when I try to draw just a subsection of the image.

Here's a small MainComponent that reproduces the problem.

 

class MainContentComponent   : public Component, public OpenGLRenderer

{

public:

    MainContentComponent();


    void paint (Graphics&) override;

    void newOpenGLContextCreated() override;

    void renderOpenGL() override;

    void openGLContextClosing() override;    

private:

    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainContentComponent)

    OpenGLContext _context;    

    Image _image;

};

 

 

 

.cpp

 

MainContentComponent::MainContentComponent()

{

    setSize (600, 400);    

    _context.setRenderer(this);

    _context.attachTo(*this);

}


void MainContentComponent::paint (Graphics& g)

{

    g.fillAll(Colours::black);

    jassert(_context.isActive());

    g.drawImage(_image,

                0, 0, getWidth(), getHeight(),

                0, 0, _image.getWidth() / 2, _image.getHeight() / 2);

    

}



void MainContentComponent::newOpenGLContextCreated()

{

    auto cpuImage = ImageCache::getFromMemory(BinaryData::canvas1_png, BinaryData::canvas1_pngSize);

    _image = OpenGLImageType().convert(cpuImage);

}


void MainContentComponent::renderOpenGL()

{

}


void MainContentComponent::openGLContextClosing()

{

}

 

This code works as expected when using an image created directly from ImageCache::getFromMemory(...) but the image doesn't display when using OpenGLImageType.  This has been tested using the latest juce on OSX


#2

After a bit more testing, I found this also fails when using sub images got from Image::getClippedImage()


#3

Like I replied on the other thread: You can't call any openGL functions (even indirectly via the OpenGLImageType) in the paint callback, because doing so will mess-up the GL context state which the renderer is using. You need to do this kind of thing in renderOpenGL() or newOpenGLContextCreated()


#4

I read your reply and have been trying to move everything to standard juce code so I could keep using paint().  I read here: http://www.juce.com/forum/topic/opengl-performance-windows?page=1 (post #19) that you recommended drawing an OpenGLImageType image in the paint method - is this no longer recommended?

Everything else now seems to work as expected except drawing only part of the image.


#5

To be honest since we recently added GL texture caching, there's not a lot of point in using an OpenGLImageType unless you actually need to draw into it using GL. If you just use a normal image then the first time you draw it, it'll get automatically cached as a texture, so will be very fast to draw it subsequently.