-
Notifications
You must be signed in to change notification settings - Fork 290
/
renderer.go
357 lines (312 loc) · 10.9 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Copyright 2016 The G3N Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package renderer implements the scene renderer.
package renderer
import (
"sort"
"github.com/g3n/engine/camera"
"github.com/g3n/engine/core"
"github.com/g3n/engine/gls"
"github.com/g3n/engine/graphic"
"github.com/g3n/engine/gui"
"github.com/g3n/engine/light"
"github.com/g3n/engine/material"
"github.com/g3n/engine/math32"
"github.com/g3n/engine/util/logger"
)
// Package logger
var log = logger.New("RENDERER", logger.Default)
// Renderer renders a scene containing 3D objects and/or 2D GUI elements.
type Renderer struct {
Shaman // Embedded shader manager
gs *gls.GLS // Reference to OpenGL state
rinfo core.RenderInfo // Preallocated Render info
specs ShaderSpecs // Preallocated Shader specs
sortObjects bool // Flag indicating whether objects should be sorted before rendering
stats Stats // Renderer statistics
// Populated each frame
ambLights []*light.Ambient // Ambient lights in the scene
dirLights []*light.Directional // Directional lights in the scene
pointLights []*light.Point // Point lights in the scene
spotLights []*light.Spot // Spot lights in the scene
others []core.INode // Other nodes (audio, players, etc)
graphics []*graphic.Graphic // Graphics to be rendered
grmatsOpaque []*graphic.GraphicMaterial // Opaque graphic materials to be rendered
grmatsTransp []*graphic.GraphicMaterial // Transparent graphic materials to be rendered
zLayers map[int][]gui.IPanel // All IPanels to be rendered organized by Z-layer
zLayerKeys []int // Z-layers being used (initially in no particular order, sorted later)
}
// Stats describes how many objects of each type are being rendered.
// It is cleared at the start of each render.
type Stats struct {
GraphicMats int // Number of graphic materials rendered
Lights int // Number of lights rendered
Panels int // Number of GUI panels rendered
Others int // Number of other objects rendered
}
// NewRenderer creates and returns a pointer to a new Renderer.
func NewRenderer(gs *gls.GLS) *Renderer {
r := new(Renderer)
r.gs = gs
r.Shaman.Init(gs)
r.sortObjects = true
r.ambLights = make([]*light.Ambient, 0)
r.dirLights = make([]*light.Directional, 0)
r.pointLights = make([]*light.Point, 0)
r.spotLights = make([]*light.Spot, 0)
r.others = make([]core.INode, 0)
r.graphics = make([]*graphic.Graphic, 0)
r.grmatsOpaque = make([]*graphic.GraphicMaterial, 0)
r.grmatsTransp = make([]*graphic.GraphicMaterial, 0)
r.zLayers = make(map[int][]gui.IPanel)
r.zLayers[0] = make([]gui.IPanel, 0)
r.zLayerKeys = append(r.zLayerKeys, 0)
return r
}
// Stats returns a copy of the statistics for the last frame.
// Should be called after the frame was rendered.
func (r *Renderer) Stats() Stats {
return r.stats
}
// SetObjectSorting sets whether objects will be sorted before rendering.
func (r *Renderer) SetObjectSorting(sort bool) {
r.sortObjects = sort
}
// ObjectSorting returns whether objects will be sorted before rendering.
func (r *Renderer) ObjectSorting() bool {
return r.sortObjects
}
// Render renders the specified scene using the specified camera. Returns an an error.
func (r *Renderer) Render(scene core.INode, cam camera.ICamera) error {
// Updates world matrices of all scene nodes
scene.UpdateMatrixWorld()
// Build RenderInfo
cam.ViewMatrix(&r.rinfo.ViewMatrix)
cam.ProjMatrix(&r.rinfo.ProjMatrix)
// Clear stats and scene arrays
r.stats = Stats{}
r.ambLights = r.ambLights[0:0]
r.dirLights = r.dirLights[0:0]
r.pointLights = r.pointLights[0:0]
r.spotLights = r.spotLights[0:0]
r.others = r.others[0:0]
r.graphics = r.graphics[0:0]
r.grmatsOpaque = r.grmatsOpaque[0:0]
r.grmatsTransp = r.grmatsTransp[0:0]
r.zLayers = make(map[int][]gui.IPanel)
r.zLayers[0] = make([]gui.IPanel, 0)
r.zLayerKeys = r.zLayerKeys[0:1]
r.zLayerKeys[0] = 0
// Prepare for frustum culling
var proj math32.Matrix4
proj.MultiplyMatrices(&r.rinfo.ProjMatrix, &r.rinfo.ViewMatrix)
frustum := math32.NewFrustumFromMatrix(&proj)
// Classify scene and all scene nodes, culling renderable IGraphics which are fully outside of the camera frustum
r.classifyAndCull(scene, frustum, 0)
// Set light counts in shader specs
r.specs.AmbientLightsMax = len(r.ambLights)
r.specs.DirLightsMax = len(r.dirLights)
r.specs.PointLightsMax = len(r.pointLights)
r.specs.SpotLightsMax = len(r.spotLights)
// Pre-calculate MV and MVP matrices and compile initial lists of opaque and transparent graphic materials
for _, gr := range r.graphics {
// Calculate MV and MVP matrices for all non-GUI graphics to be rendered
gr.CalculateMatrices(r.gs, &r.rinfo)
// Append all graphic materials of this graphic to lists of graphic materials to be rendered
materials := gr.Materials()
for i := range materials {
r.stats.GraphicMats++
if materials[i].IMaterial().GetMaterial().Transparent() {
r.grmatsTransp = append(r.grmatsTransp, &materials[i])
} else {
r.grmatsOpaque = append(r.grmatsOpaque, &materials[i])
}
}
}
// TODO: If both GraphicMaterials belong to same Graphic we might want to keep their relative order...
// Z-sort graphic materials back to front
if r.sortObjects {
zSort(r.grmatsOpaque)
zSort(r.grmatsTransp)
}
// Sort zLayers back to front
sort.Ints(r.zLayerKeys)
// Iterate over all panels from back to front, setting Z and adding graphic materials to grmatsTransp/grmatsOpaque
const deltaZ = 0.00001
panZ := float32(-1 + float32(r.stats.Panels)*deltaZ)
for _, k := range r.zLayerKeys {
for _, ipan := range r.zLayers[k] {
// Set panel Z
ipan.SetPositionZ(panZ)
panZ -= deltaZ
// Append the panel's graphic material to lists of graphic materials to be rendered
mat := ipan.GetGraphic().Materials()[0]
if mat.IMaterial().GetMaterial().Transparent() {
r.grmatsTransp = append(r.grmatsTransp, &mat)
} else {
r.grmatsOpaque = append(r.grmatsOpaque, &mat)
}
}
}
// Render opaque objects front to back
for i := len(r.grmatsOpaque) - 1; i >= 0; i-- {
err := r.renderGraphicMaterial(r.grmatsOpaque[i])
if err != nil {
return err
}
}
// Render transparent objects back to front
for _, grmat := range r.grmatsTransp {
err := r.renderGraphicMaterial(grmat)
if err != nil {
return err
}
}
// Render other nodes (audio players, etc)
for _, inode := range r.others {
inode.Render(r.gs)
}
// Enable depth mask so that clearing the depth buffer works
r.gs.DepthMask(true)
// TODO enable color mask, stencil mask?
// TODO clear the buffers for the user, and set the appropriate masks to true before clearing
return nil
}
// classifyAndCull classifies the provided INode and all of its descendents.
// It ignores (culls) renderable IGraphics which are fully outside of the specified frustum.
func (r *Renderer) classifyAndCull(inode core.INode, frustum *math32.Frustum, zLayer int) {
// Ignore invisible nodes and their descendants
if !inode.Visible() {
return
}
// If node is an IPanel append it to appropriate list
if ipan, ok := inode.(gui.IPanel); ok {
zLayer += ipan.ZLayerDelta()
if ipan.Renderable() {
// TODO cull panels
_, ok := r.zLayers[zLayer]
if !ok {
r.zLayerKeys = append(r.zLayerKeys, zLayer)
r.zLayers[zLayer] = make([]gui.IPanel, 0)
}
r.zLayers[zLayer] = append(r.zLayers[zLayer], ipan)
r.stats.Panels++
}
// Check if node is an IGraphic
} else if igr, ok := inode.(graphic.IGraphic); ok {
if igr.Renderable() {
gr := igr.GetGraphic()
// Frustum culling
if igr.Cullable() {
mw := gr.MatrixWorld()
bb := igr.GetGeometry().BoundingBox()
bb.ApplyMatrix4(&mw)
if frustum.IntersectsBox(&bb) {
// Append graphic to list of graphics to be rendered
r.graphics = append(r.graphics, gr)
}
} else {
// Append graphic to list of graphics to be rendered
r.graphics = append(r.graphics, gr)
}
}
// Node is not a Graphic
} else {
// Check if node is a Light
if il, ok := inode.(light.ILight); ok {
switch l := il.(type) {
case *light.Ambient:
r.ambLights = append(r.ambLights, l)
case *light.Directional:
r.dirLights = append(r.dirLights, l)
case *light.Point:
r.pointLights = append(r.pointLights, l)
case *light.Spot:
r.spotLights = append(r.spotLights, l)
default:
panic("Invalid light type")
}
// Other nodes
} else {
r.others = append(r.others, inode)
r.stats.Others++
}
}
// Classify children
for _, ichild := range inode.Children() {
r.classifyAndCull(ichild, frustum, zLayer)
}
}
// zSort sorts a list of graphic materials based on the user-specified render order
// then based on their Z position relative to the camera, back to front.
func zSort(grmats []*graphic.GraphicMaterial) {
sort.Slice(grmats, func(i, j int) bool {
gr1 := grmats[i].IGraphic().GetGraphic()
gr2 := grmats[j].IGraphic().GetGraphic()
// Check for user-supplied render order
rO1 := gr1.RenderOrder()
rO2 := gr2.RenderOrder()
if rO1 != rO2 {
return rO1 < rO2
}
mvm1 := gr1.ModelViewMatrix()
mvm2 := gr2.ModelViewMatrix()
g1pos := gr1.Position()
g2pos := gr2.Position()
g1pos.ApplyMatrix4(mvm1)
g2pos.ApplyMatrix4(mvm2)
return g1pos.Z < g2pos.Z
})
}
// renderGraphicMaterial renders the specified graphic material.
func (r *Renderer) renderGraphicMaterial(grmat *graphic.GraphicMaterial) error {
mat := grmat.IMaterial().GetMaterial()
geom := grmat.IGraphic().GetGeometry()
gr := grmat.IGraphic().GetGraphic()
// Add defines from material, geometry and graphic
r.specs.Defines = *gls.NewShaderDefines()
r.specs.Defines.Add(&mat.ShaderDefines)
r.specs.Defines.Add(&geom.ShaderDefines)
r.specs.Defines.Add(&gr.ShaderDefines)
// Set the shader specs for this material and set shader program
r.specs.Name = mat.Shader()
r.specs.ShaderUnique = mat.ShaderUnique()
r.specs.UseLights = mat.UseLights()
r.specs.MatTexturesMax = mat.TextureCount()
// Set active program and apply shader specs
_, err := r.Shaman.SetProgram(&r.specs)
if err != nil {
return err
}
// Set up lights (transfer lights' uniforms)
if r.specs.UseLights != material.UseLightNone {
if r.specs.UseLights&material.UseLightAmbient != 0 {
for idx, l := range r.ambLights {
l.RenderSetup(r.gs, &r.rinfo, idx)
r.stats.Lights++
}
}
if r.specs.UseLights&material.UseLightDirectional != 0 {
for idx, l := range r.dirLights {
l.RenderSetup(r.gs, &r.rinfo, idx)
r.stats.Lights++
}
}
if r.specs.UseLights&material.UseLightPoint != 0 {
for idx, l := range r.pointLights {
l.RenderSetup(r.gs, &r.rinfo, idx)
r.stats.Lights++
}
}
if r.specs.UseLights&material.UseLightSpot != 0 {
for idx, l := range r.spotLights {
l.RenderSetup(r.gs, &r.rinfo, idx)
r.stats.Lights++
}
}
}
// Render this graphic material
grmat.Render(r.gs, &r.rinfo)
return nil
}