| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- // 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
- import (
- "github.com/g3n/engine/camera"
- "github.com/g3n/engine/core"
- "github.com/g3n/engine/gls"
- "github.com/g3n/engine/graphic"
- "github.com/g3n/engine/light"
- )
- type Renderer struct {
- gs *gls.GLS
- shaman Shaman // Internal shader manager
- ambLights []*light.Ambient // Array of ambient lights for last scene
- dirLights []*light.Directional // Array of directional lights for last scene
- pointLights []*light.Point // Array of point
- spotLights []*light.Spot // Array of spot lights for the scene
- others []core.INode // Other nodes (audio, players, etc)
- grmats []*graphic.GraphicMaterial // Array of all graphic materials for scene
- rinfo core.RenderInfo // Preallocated Render info
- specs ShaderSpecs // Preallocated Shader specs
- }
- func NewRenderer(gs *gls.GLS) *Renderer {
- r := new(Renderer)
- r.gs = gs
- r.shaman.Init(gs)
- 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.grmats = make([]*graphic.GraphicMaterial, 0)
- return r
- }
- func (r *Renderer) AddDefaultShaders() error {
- return r.shaman.AddDefaultShaders()
- }
- func (r *Renderer) AddChunk(name, source string) {
- r.shaman.AddChunk(name, source)
- }
- func (r *Renderer) AddShader(name, source string) {
- r.shaman.AddShader(name, source)
- }
- func (r *Renderer) AddProgram(name, vertex, frag string, others ...string) {
- r.shaman.AddProgram(name, vertex, frag, others...)
- }
- //// SetProgramShader sets the shader type and name for a previously specified program name.
- //// Returns error if the specified program or shader name not found or
- //// if an invalid shader type was specified.
- //func (r *Renderer) SetProgramShader(pname string, stype int, sname string) error {
- //
- // return r.shaman.SetProgramShader(pname, stype, sname)
- //}
- func (r *Renderer) Render(iscene core.INode, icam camera.ICamera) error {
- // Updates world matrices of all scene nodes
- iscene.UpdateMatrixWorld()
- scene := iscene.GetNode()
- // Builds RenderInfo calls RenderSetup for all visible nodes
- icam.ViewMatrix(&r.rinfo.ViewMatrix)
- icam.ProjMatrix(&r.rinfo.ProjMatrix)
- // Clear scene arrays
- 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.grmats = r.grmats[0:0]
- // Internal function to classify a node and its children
- var classifyNode func(inode core.INode)
- classifyNode = func(inode core.INode) {
- // If node not visible, ignore
- node := inode.GetNode()
- if !node.Visible() {
- return
- }
- // Checks if node is a Graphic
- igr, ok := inode.(graphic.IGraphic)
- if ok {
- if igr.Renderable() {
- // Appends to list each graphic material for this graphic
- gr := igr.GetGraphic()
- materials := gr.Materials()
- for i := 0; i < len(materials); i++ {
- r.grmats = append(r.grmats, &materials[i])
- }
- }
- // Node is not a Graphic
- } else {
- // Checks if node is a Light
- il, ok := inode.(light.ILight)
- if 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)
- }
- }
- // Classify node children
- for _, ichild := range node.Children() {
- classifyNode(ichild)
- }
- }
- // Classify all scene nodes
- classifyNode(scene)
- // Sets lights count 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)
- // Render other nodes (audio players, etc)
- for i := 0; i < len(r.others); i++ {
- inode := r.others[i]
- if !inode.GetNode().Visible() {
- continue
- }
- r.others[i].Render(r.gs)
- }
- // For each *GraphicMaterial
- for _, grmat := range r.grmats {
- //log.Debug("grmat:%v", grmat)
- mat := grmat.GetMaterial().GetMaterial()
- // Sets the shader specs for this material and sets shader program
- r.specs.Name = mat.Shader()
- r.specs.ShaderUnique = mat.ShaderUnique()
- r.specs.UseLights = mat.UseLights()
- r.specs.MatTexturesMax = mat.TextureCount()
- _, err := r.shaman.SetProgram(&r.specs)
- if err != nil {
- return err
- }
- // Setup lights (transfer lights uniforms)
- for idx, l := range r.ambLights {
- l.RenderSetup(r.gs, &r.rinfo, idx)
- }
- for idx, l := range r.dirLights {
- l.RenderSetup(r.gs, &r.rinfo, idx)
- }
- for idx, l := range r.pointLights {
- l.RenderSetup(r.gs, &r.rinfo, idx)
- }
- for idx, l := range r.spotLights {
- l.RenderSetup(r.gs, &r.rinfo, idx)
- }
- // Render this graphic material
- grmat.Render(r.gs, &r.rinfo)
- }
- return nil
- }
|