• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

README.mdH A D01-Aug-20215.5 KiB12187

fontstash.hH A D01-Aug-202147.4 KiB1,7691,362

nanovg.cH A D01-Aug-202174.8 KiB2,9072,390

nanovg.hH A D01-Aug-202127.4 KiB686264

nanovg_gl.hH A D01-Aug-202145.4 KiB1,6501,360

nanovg_gl_utils.hH A D01-Aug-20214.7 KiB155108

stb_image.hH A D01-Aug-2021222.3 KiB6,6254,808

stb_truetype.hH A D01-Aug-2021124.3 KiB3,2502,126

README.md

1NanoVG
2==========
3
4NanoVG is small antialiased vector graphics rendering library for OpenGL. It has lean API modeled after HTML5 canvas API. It is aimed to be a practical and fun toolset for building scalable user interfaces and visualizations.
5
6## Screenshot
7
8![screenshot of some text rendered witht the sample program](/example/screenshot-01.png?raw=true)
9
10Usage
11=====
12
13The NanoVG API is modeled loosely on HTML5 canvas API. If you know canvas, you're up to speed with NanoVG in no time.
14
15## Creating drawing context
16
17The drawing context is created using platform specific constructor function. If you're using the OpenGL 2.0 back-end the context is created as follows:
18```C
19#define NANOVG_GL2_IMPLEMENTATION	// Use GL2 implementation.
20#include "nanovg_gl.h"
21...
22struct NVGcontext* vg = nvgCreateGL2(NVG_ANTIALIAS | NVG_STENCIL_STROKES);
23```
24
25The first parameter defines flags for creating the renderer.
26
27- `NVG_ANTIALIAS` means that the renderer adjusts the geometry to include anti-aliasing. If you're using MSAA, you can omit this flags.
28- `NVG_STENCIL_STROKES` means that the render uses better quality rendering for (overlapping) strokes. The quality is mostly visible on wider strokes. If you want speed, you can omit this flag.
29
30Currently there is an OpenGL back-end for NanoVG: [nanovg_gl.h](/src/nanovg_gl.h) for OpenGL 2.0, OpenGL ES 2.0, OpenGL 3.2 core profile and OpenGL ES 3. The implementation can be chosen using a define as in above example. See the header file and examples for further info.
31
32*NOTE:* The render target you're rendering to must have stencil buffer.
33
34## Drawing shapes with NanoVG
35
36Drawing a simple shape using NanoVG consists of four steps: 1) begin a new shape, 2) define the path to draw, 3) set fill or stroke, 4) and finally fill or stroke the path.
37
38```C
39nvgBeginPath(vg);
40nvgRect(vg, 100,100, 120,30);
41nvgFillColor(vg, nvgRGBA(255,192,0,255));
42nvgFill(vg);
43```
44
45Calling `nvgBeginPath()` will clear any existing paths and start drawing from blank slate. There are number of number of functions to define the path to draw, such as rectangle, rounded rectangle and ellipse, or you can use the common moveTo, lineTo, bezierTo and arcTo API to compose the paths step by step.
46
47## Understanding Composite Paths
48
49Because of the way the rendering backend is build in NanoVG, drawing a composite path, that is path consisting from multiple paths defining holes and fills, is a bit more involved. NanoVG uses even-odd filling rule and by default the paths are wound in counter clockwise order. Keep that in mind when drawing using the low level draw API. In order to wind one of the predefined shapes as a hole, you should call `nvgPathWinding(vg, NVG_HOLE)`, or `nvgPathWinding(vg, NVG_CW)` _after_ defining the path.
50
51``` C
52nvgBeginPath(vg);
53nvgRect(vg, 100,100, 120,30);
54nvgCircle(vg, 120,120, 5);
55nvgPathWinding(vg, NVG_HOLE);	// Mark circle as a hole.
56nvgFillColor(vg, nvgRGBA(255,192,0,255));
57nvgFill(vg);
58```
59
60## Rendering is wrong, what to do?
61
62- make sure you have created NanoVG context using one of the `nvgCreatexxx()` calls
63- make sure you have initialised OpenGL with *stencil buffer*
64- make sure you have cleared stencil buffer
65- make sure all rendering calls happen between `nvgBeginFrame()` and `nvgEndFrame()`
66- to enable more checks for OpenGL errors, add `NVG_DEBUG` flag to `nvgCreatexxx()`
67- if the problem still persists, please report an issue!
68
69## OpenGL state touched by the backend
70
71The OpenGL back-end touches following states:
72
73When textures are uploaded or updated, the following pixel store is set to defaults: `GL_UNPACK_ALIGNMENT`, `GL_UNPACK_ROW_LENGTH`, `GL_UNPACK_SKIP_PIXELS`, `GL_UNPACK_SKIP_ROWS`. Texture binding is also affected. Texture updates can happen when the user loads images, or when new font glyphs are added. Glyphs are added as needed between calls to  `nvgBeginFrame()` and `nvgEndFrame()`.
74
75The data for the whole frame is buffered and flushed in `nvgEndFrame()`. The following code illustrates the OpenGL state touched by the rendering code:
76```C
77	glUseProgram(prog);
78	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
79	glEnable(GL_CULL_FACE);
80	glCullFace(GL_BACK);
81	glFrontFace(GL_CCW);
82	glEnable(GL_BLEND);
83	glDisable(GL_DEPTH_TEST);
84	glDisable(GL_SCISSOR_TEST);
85	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
86	glStencilMask(0xffffffff);
87	glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
88	glStencilFunc(GL_ALWAYS, 0, 0xffffffff);
89	glActiveTexture(GL_TEXTURE0);
90	glBindBuffer(GL_UNIFORM_BUFFER, buf);
91	glBindVertexArray(arr);
92	glBindBuffer(GL_ARRAY_BUFFER, buf);
93	glBindTexture(GL_TEXTURE_2D, tex);
94	glUniformBlockBinding(... , GLNVG_FRAG_BINDING);
95```
96
97## API Reference
98
99See the header file [nanovg.h](/src/nanovg.h) for API reference.
100
101## Ports
102
103- [DX11 port](https://github.com/cmaughan/nanovg) by [Chris Maughan](https://github.com/cmaughan)
104- [Metal port](https://github.com/ollix/MetalNanoVG) by [Olli Wang](https://github.com/olliwang)
105- [bgfx port](https://github.com/bkaradzic/bgfx/tree/master/examples/20-nanovg) by [Branimir Karadžić](https://github.com/bkaradzic)
106
107## Projects using NanoVG
108
109- [Processing API simulation by vinjn](https://github.com/vinjn/island/blob/master/examples/01-processing/sketch2d.h)
110- [NanoVG for .NET, C# P/Invoke binding](https://github.com/sbarisic/nanovg_dotnet)
111
112## License
113The library is licensed under [zlib license](LICENSE.txt)
114
115## Discussions
116[NanoVG mailing list](https://groups.google.com/forum/#!forum/nanovg)
117
118## Links
119Uses [stb_truetype](http://nothings.org) (or, optionally, [freetype](http://freetype.org)) for font rendering.
120Uses [stb_image](http://nothings.org) for image loading.
121