-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrenderer.go
280 lines (231 loc) · 6.65 KB
/
renderer.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package three
import (
"errors"
gl "github.com/go-gl/gl"
"github.com/go-gl/mathgl/mgl32"
glh "github.com/tobscher/glh"
)
// Renderer handles mesh rendering to the window.
type Renderer struct {
window *Window
vertexArray gl.VertexArray
}
// NewRenderer creates a new Renderer with the given window size and title.
func NewRenderer(window *Window) (*Renderer, error) {
logger.Debug("Initializing GLEW")
// Init glew
if gl.Init() != 0 {
return nil, errors.New("Could not initialise glew.")
}
gl.GetError()
if window.Settings.ClearColor != nil {
color := window.Settings.ClearColor
gl.ClearColor(
gl.GLclampf(color.R()),
gl.GLclampf(color.G()),
gl.GLclampf(color.B()),
0.,
)
}
gl.Enable(gl.DEPTH_TEST)
gl.DepthFunc(gl.LESS)
gl.Enable(gl.CULL_FACE)
// Vertex buffers
vertexArray := gl.GenVertexArray()
vertexArray.Bind()
renderer := Renderer{
vertexArray: vertexArray,
window: window,
}
return &renderer, nil
}
// Render renders the given scene with the given camera to the window.
func (r *Renderer) Render(scene *Scene, camera *PerspectiveCamera) {
gl.Viewport(0, 0, currentWindow.Width(), currentWindow.Height())
// Seems to be causing a strange memory leak
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
for _, element := range scene.objects {
handleElement(camera, element)
}
for _, text := range scene.texts {
handleText(camera, text)
}
r.window.Swap()
}
func handleText(camera *PerspectiveCamera, text *Text) {
// Material
material := text.Material()
program := material.Program()
if program == nil {
program = createTextProgram(text)
material.SetProgram(program)
}
program.Use()
defer program.Unuse()
if c, ok := material.(Colored); ok {
if c.Color() != nil {
program.uniforms["diffuse"].apply(c.Color())
}
}
if t, ok := material.(Textured); ok {
texture := t.Texture()
if texture != nil {
gl.ActiveTexture(gl.TEXTURE0)
texture.Bind()
defer texture.Unbind()
program.uniforms["texture"].apply(texture)
}
}
for _, attribute := range program.attributes {
attribute.enable()
defer attribute.disable()
attribute.bindBuffer()
defer attribute.unbindBuffer()
attribute.pointer()
attribute.bindBuffer()
}
vertexAttrib := gl.AttribLocation(0)
vertexAttrib.EnableArray()
defer vertexAttrib.DisableArray()
text.vertexBuffer.Bind(gl.ARRAY_BUFFER)
defer text.vertexBuffer.Unbind(gl.ARRAY_BUFFER)
vertexAttrib.AttribPointer(2, gl.FLOAT, false, 0, nil)
if t, ok := material.(Wireframed); ok {
if t.Wireframe() {
gl.PolygonMode(gl.FRONT_AND_BACK, gl.LINE)
} else {
gl.PolygonMode(gl.FRONT_AND_BACK, gl.FILL)
}
}
gl.Enable(gl.BLEND)
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
gl.DrawArrays(gl.TRIANGLES, 0, len(text.Geometry().Vertices))
}
func handleElement(camera *PerspectiveCamera, element SceneObject) {
// Material
material := element.Material()
program := material.Program()
if program == nil {
program = createProgram(element)
material.SetProgram(program)
}
program.Use()
defer program.Unuse()
view := camera.Transform.modelMatrix().Inv()
model := element.Transform().modelMatrix()
projection := camera.projectionMatrix
MVP := projection.Mul4(view).Mul4(model)
// Set model view projection matrix
program.uniforms["MVP"].apply(MVP)
program.uniforms["M"].apply(model)
program.uniforms["V"].apply(view)
// Light position
lightPos := mgl32.Vec3{4., 4., 4.}
program.uniforms["LightPosition_worldspace"].apply(lightPos)
if c, ok := material.(Colored); ok {
if c.Color() != nil {
program.uniforms["diffuse"].apply(c.Color())
}
}
if t, ok := material.(Textured); ok {
texture := t.Texture()
if texture != nil {
gl.ActiveTexture(gl.TEXTURE0)
texture.Bind()
defer texture.Unbind()
program.uniforms["texture"].apply(texture)
program.uniforms["repeat"].apply(texture.Repeat)
}
}
for _, attribute := range program.attributes {
attribute.enable()
defer attribute.disable()
attribute.bindBuffer()
defer attribute.unbindBuffer()
attribute.pointer()
attribute.bindBuffer()
}
vertexAttrib := gl.AttribLocation(0)
vertexAttrib.EnableArray()
defer vertexAttrib.DisableArray()
element.VertexBuffer().Bind(gl.ARRAY_BUFFER)
defer element.VertexBuffer().Unbind(gl.ARRAY_BUFFER)
vertexAttrib.AttribPointer(3, gl.FLOAT, false, 0, nil)
if t, ok := material.(Wireframed); ok {
if t.Wireframe() {
gl.PolygonMode(gl.FRONT_AND_BACK, gl.LINE)
} else {
gl.PolygonMode(gl.FRONT_AND_BACK, gl.FILL)
}
}
// If index available
index := element.Index()
if index != nil && index.count > 0 {
index.enable()
defer index.disable()
gl.DrawElements(gl.TRIANGLES, index.count, gl.UNSIGNED_SHORT, nil)
} else {
gl.DrawArrays(element.Mode(), 0, element.Geometry().ArrayCount())
}
}
func createProgram(sceneObject SceneObject) *Program {
program := NewProgram()
material := sceneObject.Material()
geometry := sceneObject.Geometry()
// Attributes
var feature ProgramFeature
if c, cOk := material.(Colored); cOk {
if c.Color() != nil {
feature = COLOR
}
}
// Let geometry return UVs
if t, tOk := material.(Textured); tOk {
if t.Texture() != nil {
program.attributes["texture"] = NewAttribute(1, 2, sceneObject.UVBuffer())
feature = TEXTURE
}
}
if len(geometry.Normals()) > 0 {
program.attributes["normals"] = NewAttribute(2, 3, sceneObject.NormalBuffer())
feature |= SHADING_BASIC
}
program.Load(MakeProgram(feature))
// Uniforms
program.uniforms["MVP"] = NewUniform(program, "MVP")
program.uniforms["V"] = NewUniform(program, "V")
program.uniforms["M"] = NewUniform(program, "M")
program.uniforms["LightPosition_worldspace"] = NewUniform(program, "LightPosition_worldspace")
program.uniforms["diffuse"] = NewUniform(program, "diffuse")
if t, tOk := material.(Textured); tOk {
if texture := t.Texture(); texture != nil {
program.uniforms["texture"] = NewUniform(program, "textureSampler")
program.uniforms["repeat"] = NewUniform(program, "repeat")
}
}
return program
}
func createTextProgram(text *Text) *Program {
program := NewProgram()
// uvs
program.attributes["texture"] = NewAttribute(1, 2, text.uvBuffer)
program.Load(MakeTextShader())
// Uniforms
program.uniforms["texture"] = NewUniform(program, "textureSampler")
program.uniforms["diffuse"] = NewUniform(program, "diffuse")
return program
}
// Unload deallocates the given scene and all its shader programs.
func (r *Renderer) Unload(s *Scene) {
logger.Info("Unload scene")
for _, element := range s.objects {
program := element.Material().Program()
program.unload()
}
r.vertexArray.Delete()
r.window.Unload()
}
// OpenGLSentinel reports any OpenGL related errors.
func (r *Renderer) OpenGLSentinel() {
glh.OpenGLSentinel()
}