Drawing text behind the OpenGL context


#1

Hello all,

I am using 2D & 3D graphics combined in my audio plugin by using OpenGLRenderer and Graphics components together. In my draw routine, I firstly draw my GL components, and then call this code block to draw 2D graphics (basically for drawing texts):

ScopedPointer<LowLevelGraphicsContext> glRenderer (createOpenGLGraphicsContext (openGLContext, getWidth(), getHeight()));

	if(glRenderer)
	{
		Graphics g(glRenderer);

		for(int i=0; i<drawables.size(); ++i)
		{
			drawables[i]->draw2D(g);
		}
	}

However, as expected, all the 2D graphics are drawn onto the GL components, which is undesirable, i.e. it fails when I try to mask some part of the canvas with a transparent colour so on and so forth, since this code block creates another gl context over my entire gl routine - if I did not misunderstand. So, my question is; is there a way to somehow blend or combine the two contexts in order to put the 2D graphics behind the 3D graphics? Otherwise, as I am using Graphics for almost only for drawing texts, I would also appreciate if anyone suggests a convenient way to draw Strings in OpenGL draw routine. I am not really familiar with how this LowLevelGraphicsContext structure works, sorry if it does not makes sense at all. :slight_smile: Thank you.


#2

Look at the juce GL demo - that draws some 2D in the background, then draws 3D over it, and then more 2D over the top… There’s no need to use your own graphics context directly (and I’d strongly recommend that you let the framework handle all the GL context creation - don’t do it yourself)


#3

yeah I actually took this code snippet from there, but changing the order of the two drawing calls just does not work for me somehow. When I try to run this code, it draws 2D calls and nothing else (GL draws does not appear both 2d and 3d):

void OpenGLCanvas::renderOpenGL()
{
	OpenGLHelpers::clear(Colours::black);

	// draw non-gl elements
	ScopedPointer<LowLevelGraphicsContext> glRenderer (createOpenGLGraphicsContext (openGLContext, getWidth(), getHeight()));

	if(glRenderer)
	{
		Graphics g(glRenderer);

		for(int i=0; i<drawables.size(); ++i)
		{
			drawables[i]->draw2D(g);
		}
	}
	
	glEnable (GL_DEPTH_TEST);
   glDepthFunc (GL_LESS);
		
	OpenGLHelpers::prepareFor2D(getWidth(), getHeight());
	glTranslatef (0.0f, 0.0f, -1.0f);

	// draw components
	for(int i=0; i<drawables.size(); ++i)
	{
		drawables[i]->drawGL(*this);
	}
}

Am I missing something? By the way, what do you mean by using the context directly? I honestly just followed the code in the demo. :slight_smile:


#4

Don’t create a context yourself - just draw your 2D stuff in paint(), and just draw 3D stuff in renderOpenGL()


#5

My first approach was indeed to draw 2D by overloading paint() functions, but I couldn’t manage them to work in AudioPluginEditor and AudioPluginProcessor, since I believe when using OpenGLContext, they need to be children of that Component to be drawn, which does not seem a good idea for those classes. What should be the best approach for this case? Besides, would that solve the “painting over” issue, as I am not sure about the order of those calls?


#6

Okay I have worked it out. For anyone who might encounter a similar issue, it was a simple mistake that I have overlooked. LowLevelGraphicsContext should be released before doing any standard GL stuff, as it was scoped in the same function in my case, I was trying to call GL functions after creating that one which results the malfunction. So declaring it outside and deleting afterwards did the trick for me.