memononen / nanovg

Antialiased 2D vector drawing library on top of OpenGL for UI and visualizations.
zlib License
5.15k stars 771 forks source link

Problem with glBlendFunc after nvgEndFrame #552

Open bruenor41 opened 4 years ago

bruenor41 commented 4 years ago

Hello,

first many thanks for awesome library, a lot of good work was put there.

I am working on small gui library and I use nanovg in two ways. First, at beginning of program I create with nanovg texture atlas. This works without issues. The second thing is runtime text. In the past I tried include font stash in my project, but I did not succeed. So, now I draw runtime text (like various labels) with nanovg. Yesterday, I needed draw half transparent square with standard opengl es 2.0 routines and I come across this issue

Shortly, I face opacity problem by drawing the square. Sometimes was square transparent like I needed, sometimes no, depending on picked color. More in the link above. First I draw text, then square. If I disabled drawing the text then square opacity was working, so I was searching what breaks it.

Looks like adding glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); after nvgEndFrame(vg); fixes the issue.

Here is my code for runtime text :

void Label::drawText(float x, float y, const char *text) {
    NVGcontext* vg = nvgWrapper->vg;

    nvgBeginFrame(vg, Screen::width, Screen::height, 1);

    nvgFontSize(vg, 58.0f);
    nvgFontFace(vg, "sans-bold");
    nvgTextAlign(vg, NVG_ALIGN_CENTER | NVG_ALIGN_MIDDLE);
    nvgText(vg, x, y, text, NULL);

    nvgEndFrame(vg);

    **glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);**
}

Do I something wrong in nanovg usage? Or is it bug in nanvg or my code?

Many thanks, Tony

memononen commented 4 years ago

Take a look at the readme, it lists the OpenGL state the renderer changed during rendering. https://github.com/memononen/nanovg#opengl-state-touched-by-the-backend

So yes, NanoVG may change the blend function.

On Tue, Dec 31, 2019 at 9:48 AM bruenor41 notifications@github.com wrote:

Hello,

first many thanks for awesome library, a lot of good work was put there.

I am working on small gui library and I use nanovg in two ways. First, at beginning of program I create with nanovg texture atlas. This works without issues. The second thing is runtime text. In the past I tried include font stash in my project, but I did not succeed. So, now I draw runtime text (like various labels) with nanovg. Yesterday, I needed draw half transparent square with standard opengl es 2.0 routines and I come across this issue https://stackoverflow.com/questions/59534830/opengl-es-2-0-problem-with-color-opacity-by-gldrawelementsgl-triangles

Shortly, I face opacity problem by drawing the square. Sometimes was square transparent like I needed, sometimes no, depending on picked color.

Looks like adding glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); after nvgEndFrame(vg); fixes the issue.

Here is my code for runtime text :

void Label::drawText(float x, float y, const char text) { NVGcontext vg = nvgWrapper->vg;

nvgBeginFrame(vg, Screen::width, Screen::height, 1);

nvgFontSize(vg, 58.0f);
nvgFontFace(vg, "sans-bold");
nvgTextAlign(vg, NVG_ALIGN_CENTER | NVG_ALIGN_MIDDLE);
nvgText(vg, x, y, text, NULL);

nvgEndFrame(vg);

**glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);**

}

Do I something wrong in nanovg usage? Or is it bug in nanvg or my code?

Many thanks, Tony

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/memononen/nanovg/issues/552?email_source=notifications&email_token=ABIBXSHAQ44IT7RVVQOHZ5DQ3L2OJA5CNFSM4KBTNMKKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4IDOSWXQ, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABIBXSDU4BW3LYOSWAW3JTDQ3L2OJANCNFSM4KBTNMKA .

bruenor41 commented 4 years ago

Ah,ok, so it is correct. Many thanks :)