Skip to content

Commit ff61409

Browse files
committed
Cleaned up mesh renderer
1 parent f97283f commit ff61409

File tree

2 files changed

+11
-38
lines changed

2 files changed

+11
-38
lines changed

TODO

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,4 @@ front to back render
5656
occlusion culling
5757
texture atlas/array?
5858
PURE depth pass, do ambient colors on first light pass?
59+
reenable light significance test

render/mesh.go

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ type MeshShaderProgram struct {
3838
AlphaMap *graphics.UniformSampler
3939
BumpMap *graphics.UniformSampler
4040

41-
LightType *graphics.UniformInteger
4241
LightPos *graphics.UniformVector3
4342
LightDir *graphics.UniformVector3
4443
AmbientLight *graphics.UniformVector3
@@ -51,7 +50,6 @@ type MeshShaderProgram struct {
5150
DirShadowMap *graphics.UniformSampler
5251
ShadowFar *graphics.UniformFloat
5352
LightAttQuad *graphics.UniformFloat
54-
CastShadow *graphics.UniformBool
5553
}
5654

5755
// TODO: redesign attr/uniform access system?
@@ -177,7 +175,6 @@ func NewMeshShaderProgram(defines []string) *MeshShaderProgram {
177175
sp.AlphaMap = sp.UniformSampler("material.alphaMap")
178176
sp.BumpMap = sp.UniformSampler("material.bumpMap")
179177

180-
sp.LightType = sp.UniformInteger("light.type")
181178
sp.LightPos = sp.UniformVector3("light.position")
182179
sp.LightDir = sp.UniformVector3("light.direction")
183180
sp.AmbientLight = sp.UniformVector3("light.ambient")
@@ -190,7 +187,6 @@ func NewMeshShaderProgram(defines []string) *MeshShaderProgram {
190187
sp.DirShadowMap = sp.UniformSampler("dirShadowMap")
191188
sp.ShadowFar = sp.UniformFloat("light.far")
192189
sp.LightAttQuad = sp.UniformFloat("light.attenuationQuadratic")
193-
sp.CastShadow = sp.UniformBool("light.castshadow")
194190

195191
sp.Position.SetFormat(gl.FLOAT, false)
196192
sp.Normal.SetFormat(gl.FLOAT, false)
@@ -237,12 +233,14 @@ func (r *MeshRenderer) renderMesh(sp *MeshShaderProgram, m *object.Mesh, c camer
237233
r.SetCamera(sp, c)
238234

239235
// TODO: cache per mesh/camera?
240-
r.normalMatrix.Identity()
241-
r.normalMatrix.Mult(c.ViewMatrix())
242-
r.normalMatrix.Mult(m.WorldMatrix())
243-
r.normalMatrix.Invert()
244-
r.normalMatrix.Transpose()
245-
sp.NormalMatrix.Set(&r.normalMatrix)
236+
if sp.NormalMatrix != nil {
237+
r.normalMatrix.Identity()
238+
r.normalMatrix.Mult(c.ViewMatrix())
239+
r.normalMatrix.Mult(m.WorldMatrix())
240+
r.normalMatrix.Invert()
241+
r.normalMatrix.Transpose()
242+
sp.NormalMatrix.Set(&r.normalMatrix)
243+
}
246244

247245
for _, subMesh := range m.SubMeshes {
248246
if !c.Cull(subMesh) {
@@ -257,8 +255,6 @@ func (r *MeshRenderer) AmbientPass(s *scene.Scene, c camera.Camera) {
257255
r.renderState.DepthTest = graphics.LessDepthTest
258256
r.renderState.Program = r.sp1.ShaderProgram
259257

260-
// TODO: WHY MUST THIS BE SET FOR AMBIENT LIGHT?!?! GRAPHICS DRIVER BUG?
261-
// TODO: FIX: AVOID BRANCHING IN SHADERS!!!!!!!!!!!!!
262258
for _, m := range s.Meshes {
263259
r.renderMesh(r.sp1, m, c)
264260
}
@@ -268,7 +264,7 @@ func (r *MeshRenderer) LightPass(s *scene.Scene, c camera.Camera) {
268264
r.renderState.DepthTest = graphics.EqualDepthTest
269265
r.renderState.BlendSourceFactor = graphics.OneBlendFactor
270266
r.renderState.BlendDestinationFactor = graphics.OneBlendFactor // add to framebuffer contents
271-
r.renderState.Program = r.sp2.ShaderProgram
267+
272268
r.PointLightPass(s, c)
273269
r.SpotLightPass(s, c)
274270
r.DirectionalLightPass(s, c)
@@ -280,24 +276,7 @@ func (r *MeshRenderer) PointLightPass(s *scene.Scene, c camera.Camera) {
280276
for _, l := range s.PointLights {
281277
r.SetPointLight(r.sp2, l)
282278
for _, m := range s.Meshes {
283-
sp := r.sp2
284-
r.SetMesh(sp, m)
285-
r.SetCamera(sp, c)
286-
287-
// TODO: cache per mesh/camera?
288-
r.normalMatrix.Identity()
289-
r.normalMatrix.Mult(c.ViewMatrix())
290-
r.normalMatrix.Mult(m.WorldMatrix())
291-
r.normalMatrix.Invert()
292-
r.normalMatrix.Transpose()
293-
sp.NormalMatrix.Set(&r.normalMatrix)
294-
295-
for _, subMesh := range m.SubMeshes {
296-
if !c.Cull(subMesh) {
297-
r.SetSubMesh(sp, subMesh)
298-
graphics.NewRenderCommand(graphics.Triangle, subMesh.Geo.Inds, 0, r.renderState).Execute()
299-
}
300-
}
279+
r.renderMesh(r.sp2, m, c)
301280
}
302281
}
303282
}
@@ -421,17 +400,14 @@ func (r *MeshRenderer) SetSubMesh(sp *MeshShaderProgram, sm *object.SubMesh) {
421400
}
422401

423402
func (r *MeshRenderer) SetAmbientLight(sp *MeshShaderProgram, l *light.AmbientLight) {
424-
sp.LightType.Set(0)
425403
sp.AmbientLight.Set(l.Color)
426404
sp.LightAttQuad.Set(0)
427405
}
428406

429407
func (r *MeshRenderer) SetPointLight(sp *MeshShaderProgram, l *light.PointLight) {
430-
sp.LightType.Set(1)
431408
sp.LightPos.Set(l.Position)
432409
sp.DiffuseLight.Set(l.Diffuse)
433410
sp.SpecularLight.Set(l.Specular)
434-
sp.CastShadow.Set(l.CastShadows)
435411
if l.CastShadows {
436412
sp.ShadowFar.Set(l.ShadowFar)
437413
smap, found := r.pointLightShadowMaps[l.ID]
@@ -444,13 +420,11 @@ func (r *MeshRenderer) SetPointLight(sp *MeshShaderProgram, l *light.PointLight)
444420
}
445421

446422
func (r *MeshRenderer) SetSpotLight(sp *MeshShaderProgram, l *light.SpotLight) {
447-
sp.LightType.Set(2)
448423
sp.LightPos.Set(l.Position)
449424
sp.LightDir.Set(l.Forward())
450425
sp.DiffuseLight.Set(l.Diffuse)
451426
sp.SpecularLight.Set(l.Specular)
452427
sp.LightAttQuad.Set(l.AttenuationQuadratic)
453-
sp.CastShadow.Set(l.CastShadows)
454428

455429
if l.CastShadows {
456430
sp.ShadowViewMatrix.Set(l.ViewMatrix())
@@ -465,12 +439,10 @@ func (r *MeshRenderer) SetSpotLight(sp *MeshShaderProgram, l *light.SpotLight) {
465439
}
466440

467441
func (r *MeshRenderer) SetDirectionalLight(sp *MeshShaderProgram, l *light.DirectionalLight) {
468-
sp.LightType.Set(3)
469442
sp.LightDir.Set(l.Forward())
470443
sp.DiffuseLight.Set(l.Diffuse)
471444
sp.SpecularLight.Set(l.Specular)
472445
sp.LightAttQuad.Set(0)
473-
sp.CastShadow.Set(l.CastShadows)
474446

475447
if l.CastShadows {
476448
sp.ShadowViewMatrix.Set(l.ViewMatrix())

0 commit comments

Comments
 (0)