# Drawing text behind the OpenGL context

**URL:** <https://forum.juce.com/t/drawing-text-behind-the-opengl-context/10406>\
**Category:** Audio Plugins\
**Created:** [May 6, 2013, 12:17pm UTC](https://forum.juce.com/t/drawing-text-behind-the-opengl-context/10406 "2013-05-06T12:17:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anokta](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/anokta/32/296_2.png) [@anokta](https://forum.juce.com/u/anokta)\
**Post date:** [May 6, 2013, 12:17pm UTC](https://forum.juce.com/t/drawing-text-behind-the-opengl-context/10406/1 "2013-05-06T12:17:12Z")

</div>

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):

```auto
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. 🙂 Thank you.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [May 6, 2013, 12:21pm UTC](https://forum.juce.com/t/drawing-text-behind-the-opengl-context/10406/2 "2013-05-06T12:21:43Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![anokta](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/anokta/32/296_2.png) [@anokta](https://forum.juce.com/u/anokta)\
**Post date:** [May 6, 2013, 1:01pm UTC](https://forum.juce.com/t/drawing-text-behind-the-opengl-context/10406/3 "2013-05-06T13:01:37Z")

</div>

> [@jules](#):
>
> 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)

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):

```auto
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. 🙂

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [May 6, 2013, 1:16pm UTC](https://forum.juce.com/t/drawing-text-behind-the-opengl-context/10406/4 "2013-05-06T13:16:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anokta](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/anokta/32/296_2.png) [@anokta](https://forum.juce.com/u/anokta)\
**Post date:** [May 6, 2013, 1:37pm UTC](https://forum.juce.com/t/drawing-text-behind-the-opengl-context/10406/5 "2013-05-06T13:37:10Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![anokta](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/anokta/32/296_2.png) [@anokta](https://forum.juce.com/u/anokta)\
**Post date:** [May 13, 2013, 6:14pm UTC](https://forum.juce.com/t/drawing-text-behind-the-opengl-context/10406/6 "2013-05-13T18:14:43Z")

</div>

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.
