forked from shibukawa/nanovgo
/
example_fbo.go
129 lines (97 loc) · 2.78 KB
/
example_fbo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"github.com/goxjs/gl"
"github.com/goxjs/glfw"
"nanovgo"
)
func main() {
var winWidth, winHeight int
var fbWidth, fbHeight int
var pixelRatio float32
var fb *nanovgo.FrameBuffer
err := glfw.Init(gl.ContextWatcher)
if err != nil {
panic(err)
}
defer glfw.Terminate()
// demo MSAA
glfw.WindowHint(glfw.Samples, 4)
window, err := glfw.CreateWindow(1000, 600, "NanoVGo", nil, nil)
if err != nil {
panic(err)
}
window.MakeContextCurrent()
ctx, err := nanovgo.NewContext(nanovgo.AntiAlias | nanovgo.StencilStrokes | nanovgo.Debug)
defer ctx.Delete()
if err != nil {
panic(err)
}
fbWidth, fbHeight = window.GetFramebufferSize()
winWidth, winHeight = window.GetSize()
pixelRatio = float32(fbWidth) / float32(winWidth)
fb = ctx.CreateFramebuffer(fbWidth, fbHeight, 0)
if fb == nil {
panic("framebufferobject should be somewhere around there")
return
}
glfw.SwapInterval(0)
for !window.ShouldClose() {
renderPattern(ctx, fb, pixelRatio)
gl.Viewport(0, 0, fbWidth, fbHeight)
gl.ClearColor(0, 0, 0, 0)
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT)
fbWidth, fbHeight = window.GetFramebufferSize()
winWidth, winHeight = window.GetSize()
pixelRatio = float32(fbWidth) / float32(winWidth)
ctx.BeginFrame(winWidth, winHeight, pixelRatio)
if fb != nil {
ctx.Save()
ctx.BeginPath()
ctx.RoundedRect(300, 200, 200,200,10)
ctx.Circle(200, 400, 50)
ctx.SetFillColor(nanovgo.RGBA(255, 0, 0, 255))
ctx.Fill()
img := nanovgo.ImagePattern(0, 0, float32(fbWidth), float32(fbHeight), 0, fb.Image(), 1.0)
ctx.BeginPath()
ctx.Circle(200, 200, 150)
ctx.SetFillPaint(img)
ctx.Fill()
ctx.Stroke()
ctx.SetStrokeColor(nanovgo.RGBA(0, 255, 0, 255))
ctx.SetStrokeWidth(3)
ctx.Stroke()
ctx.Restore()
}
ctx.EndFrame()
gl.Enable(gl.DEPTH_TEST)
window.SwapBuffers()
glfw.PollEvents()
}
nanovgo.NvgluDeleteFramebuffer(fb)
}
func renderPattern(ctx *nanovgo.Context, fb *nanovgo.FrameBuffer, pxRatio float32) {
var winWidth, winHeight int
var fboWidth, fboHeight int
//s := 20.0
if fb == nil {
return
}
fboWidth, fboHeight, _ = ctx.ImageSize(fb.Image())
winWidth = int(float32(fboWidth) / pxRatio)
winHeight = int(float32(fboHeight) / pxRatio)
// Draw some stuff to an FBO as a test
nanovgo.NvgluBindFramebuffer(fb)
gl.Viewport(0, 0, fboWidth, fboHeight)
gl.ClearColor(0, 0, 0, 0)
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT)
ctx.BeginFrame(winWidth, winHeight, pxRatio)
for i := 0; i < 20; i++ {
ctx.BeginPath()
ctx.Rect(float32(10+i*30), 10, 5, float32(winHeight-20))
ctx.Rotate(nanovgo.DegToRad(1))
ctx.SetFillColor(nanovgo.HSLA(float32(i/19.0), 0.5, 0.5, 255))
ctx.Fill()
}
ctx.EndFrame()
nanovgo.NvgluBindFramebuffer(nil)
}