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

..03-May-2022-

example/H03-May-2022-

obsolete/H13-Oct-2016-

src/H13-Oct-2016-

.gitignoreH A D13-Oct-2016258

README.mdH A D13-Oct-20165.3 KiB

premake4.luaH A D13-Oct-20166.4 KiB

README.md

1/work in progress.../
2
3
4NanoVG
5==========
6
7NanoVG 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.
8
9## Screenshot
10
11![screenshot of some text rendered witht the sample program](/example/screenshot-01.png?raw=true)
12
13Usage
14=====
15
16The NanoVG API is modeled loosely on HTML5 canvas API. If you know canvas, you're up to speed with NanoVG in no time.
17
18## Creating drawing context
19
20The 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:
21```C
22#define NANOVG_GL2_IMPLEMENTATION	// Use GL2 implementation.
23#include "nanovg_gl.h"
24...
25struct NVGcontext* vg = nvgCreateGL2(NVG_ANTIALIAS | NVG_STENCIL_STROKES);
26```
27
28The first parameter defines flags for creating the renderer.
29
30- `NVG_ANTIALIAS` means that the renderer adjusts the geometry to include anti-aliasing. If you're using MSAA, you can omit this flags.
31- `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.
32
33Currently 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.
34
35## Drawing shapes with NanoVG
36
37Drawing 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.
38
39```C
40nvgBeginPath(vg);
41nvgRect(vg, 100,100, 120,30);
42nvgFillColor(vg, nvgRGBA(255,192,0,255));
43nvgFill(vg);
44```
45
46Calling `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.
47
48## Understanding Composite Paths
49
50Because 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.
51
52``` C
53nvgBeginPath(vg);
54nvgRect(vg, 100,100, 120,30);
55nvgCircle(vg, 120,120, 5);
56nvgPathWinding(vg, NVG_HOLE);	// Mark circle as a hole.
57nvgFillColor(vg, nvgRGBA(255,192,0,255));
58nvgFill(vg);
59```
60
61## Rendering is wrong, what to do?
62
63- make sure you have created NanoVG context using one of the `nvgCreatexxx()` calls
64- make sure you have initialised OpenGL with stencil buffer
65- make sure you have cleared stencil buffer
66- make sure all rendering calls happen between `nvgBeginFrame()` and `nvgEndFrame()`
67- to enable more checks for OpenGL errors, add `NVG_DEBUG` flag to `nvgCreatexxx()`
68- if the problem still persists, please report an issue!
69
70## OpenGL state touched by the backend
71
72The OpenGL back-end touches following states:
73
74When 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()`.
75
76The data for the whole frame is buffered and flushed in `nvgEndFrame()`. The following code illustrates the OpenGL state touched by the rendering code:
77```C
78	glUseProgram(prog);
79	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
80	glEnable(GL_CULL_FACE);
81	glCullFace(GL_BACK);
82	glFrontFace(GL_CCW);
83	glEnable(GL_BLEND);
84	glDisable(GL_DEPTH_TEST);
85	glDisable(GL_SCISSOR_TEST);
86	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
87	glStencilMask(0xffffffff);
88	glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
89	glStencilFunc(GL_ALWAYS, 0, 0xffffffff);
90	glActiveTexture(GL_TEXTURE0);
91	glBindBuffer(GL_UNIFORM_BUFFER, buf);
92	glBindVertexArray(arr);
93	glBindBuffer(GL_ARRAY_BUFFER, buf);
94	glBindTexture(GL_TEXTURE_2D, tex);
95	glUniformBlockBinding(... , GLNVG_FRAG_BINDING);
96```
97
98## API Reference
99
100See the header file [nanovg.h](/src/nanovg.h) for API reference.
101
102## Ports
103
104- [DX11 port](https://github.com/cmaughan/nanovg) by [Chris Maughan](https://github.com/cmaughan)
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
111## License
112The library is licensed under [zlib license](LICENSE.txt)
113
114## Discussions
115[NanoVG mailing list](https://groups.google.com/forum/#!forum/nanovg)
116
117## Links
118Uses [stb_truetype](http://nothings.org) (or, optionally, [freetype](http://freetype.org)) for font rendering.
119Uses [stb_image](http://nothings.org) for image loading.
120