浏览代码

gui renderer dev...

leonsal 8 年之前
父节点
当前提交
eafae80dc5
共有 2 个文件被更改,包括 112 次插入8 次删除
  1. 22 0
      gui/panel.go
  2. 90 8
      renderer/renderer.go

+ 22 - 0
gui/panel.go

@@ -74,6 +74,7 @@ type Panel struct {
 	bounded          bool               // panel is bounded by its parent
 	bounded          bool               // panel is bounded by its parent
 	enabled          bool               // enable event processing
 	enabled          bool               // enable event processing
 	cursorEnter      bool               // mouse enter dispatched
 	cursorEnter      bool               // mouse enter dispatched
+	changed          bool               // changed flag
 	layout           ILayout            // current layout for children
 	layout           ILayout            // current layout for children
 	layoutParams     interface{}        // current layout parameters used by container panel
 	layoutParams     interface{}        // current layout parameters used by container panel
 	uniMatrix        gls.Uniform        // model matrix uniform location cache
 	uniMatrix        gls.Uniform        // model matrix uniform location cache
@@ -175,6 +176,7 @@ func (p *Panel) InitializeGraphic(width, height float32, gr *graphic.Graphic) {
 	p.udata.bordersColor = math32.Color4{0, 0, 0, 1}
 	p.udata.bordersColor = math32.Color4{0, 0, 0, 1}
 	p.bounded = true
 	p.bounded = true
 	p.enabled = true
 	p.enabled = true
+	p.changed = true
 	p.resize(width, height, true)
 	p.resize(width, height, true)
 }
 }
 
 
@@ -231,6 +233,7 @@ func (p *Panel) SetTopChild(ipan IPanel) {
 	found := p.Remove(ipan)
 	found := p.Remove(ipan)
 	if found {
 	if found {
 		p.Add(ipan)
 		p.Add(ipan)
+		p.changed = true
 	}
 	}
 }
 }
 
 
@@ -240,6 +243,7 @@ func (p *Panel) SetPosition(x, y float32) {
 
 
 	p.Node.SetPositionX(math32.Round(x))
 	p.Node.SetPositionX(math32.Round(x))
 	p.Node.SetPositionY(math32.Round(y))
 	p.Node.SetPositionY(math32.Round(y))
+	p.changed = true
 }
 }
 
 
 // SetSize sets this panel external width and height in pixels.
 // SetSize sets this panel external width and height in pixels.
@@ -388,12 +392,14 @@ func (p *Panel) Paddings() BorderSizes {
 func (p *Panel) SetBordersColor(color *math32.Color) {
 func (p *Panel) SetBordersColor(color *math32.Color) {
 
 
 	p.udata.bordersColor = math32.Color4{color.R, color.G, color.B, 1}
 	p.udata.bordersColor = math32.Color4{color.R, color.G, color.B, 1}
+	p.changed = true
 }
 }
 
 
 // SetBordersColor4 sets the color and opacity of this panel borders
 // SetBordersColor4 sets the color and opacity of this panel borders
 func (p *Panel) SetBordersColor4(color *math32.Color4) {
 func (p *Panel) SetBordersColor4(color *math32.Color4) {
 
 
 	p.udata.bordersColor = *color
 	p.udata.bordersColor = *color
+	p.changed = true
 }
 }
 
 
 // BorderColor4 returns current border color
 // BorderColor4 returns current border color
@@ -406,6 +412,7 @@ func (p *Panel) BordersColor4() math32.Color4 {
 func (p *Panel) SetPaddingsColor(color *math32.Color) {
 func (p *Panel) SetPaddingsColor(color *math32.Color) {
 
 
 	p.udata.paddingsColor = math32.Color4{color.R, color.G, color.B, 1}
 	p.udata.paddingsColor = math32.Color4{color.R, color.G, color.B, 1}
+	p.changed = true
 }
 }
 
 
 // SetColor sets the color of the panel paddings and content area
 // SetColor sets the color of the panel paddings and content area
@@ -413,6 +420,7 @@ func (p *Panel) SetColor(color *math32.Color) *Panel {
 
 
 	p.udata.paddingsColor = math32.Color4{color.R, color.G, color.B, 1}
 	p.udata.paddingsColor = math32.Color4{color.R, color.G, color.B, 1}
 	p.udata.contentColor = p.udata.paddingsColor
 	p.udata.contentColor = p.udata.paddingsColor
+	p.changed = true
 	return p
 	return p
 }
 }
 
 
@@ -421,6 +429,7 @@ func (p *Panel) SetColor4(color *math32.Color4) *Panel {
 
 
 	p.udata.paddingsColor = *color
 	p.udata.paddingsColor = *color
 	p.udata.contentColor = *color
 	p.udata.contentColor = *color
+	p.changed = true
 	return p
 	return p
 }
 }
 
 
@@ -562,6 +571,18 @@ func (p *Panel) InsideBorders(x, y float32) bool {
 	return true
 	return true
 }
 }
 
 
+// SetChanged sets this panel changed flag
+func (p *Panel) SetChanged(changed bool) {
+
+	p.changed = changed
+}
+
+// Changed returns this panel changed flag
+func (p *Panel) Changed() bool {
+
+	return p.changed
+}
+
 // SetEnabled sets the panel enabled state
 // SetEnabled sets the panel enabled state
 // A disabled panel do not process key or mouse events.
 // A disabled panel do not process key or mouse events.
 func (p *Panel) SetEnabled(state bool) {
 func (p *Panel) SetEnabled(state bool) {
@@ -862,6 +883,7 @@ func (p *Panel) resize(width, height float32, dispatch bool) {
 		float32(p.content.Width) / float32(p.width),
 		float32(p.content.Width) / float32(p.width),
 		float32(p.content.Height) / float32(p.height),
 		float32(p.content.Height) / float32(p.height),
 	}
 	}
+	p.changed = true
 
 
 	// Update layout and dispatch event
 	// Update layout and dispatch event
 	if !dispatch {
 	if !dispatch {

+ 90 - 8
renderer/renderer.go

@@ -9,12 +9,15 @@ import (
 	"github.com/g3n/engine/core"
 	"github.com/g3n/engine/core"
 	"github.com/g3n/engine/gls"
 	"github.com/g3n/engine/gls"
 	"github.com/g3n/engine/graphic"
 	"github.com/g3n/engine/graphic"
+	"github.com/g3n/engine/gui"
 	"github.com/g3n/engine/light"
 	"github.com/g3n/engine/light"
 )
 )
 
 
 type Renderer struct {
 type Renderer struct {
 	gs          *gls.GLS
 	gs          *gls.GLS
 	shaman      Shaman                     // Internal shader manager
 	shaman      Shaman                     // Internal shader manager
+	scene       core.INode                 // Node containing 3D scene to render
+	gui         gui.IPanel                 // Panel containing GUI to render
 	ambLights   []*light.Ambient           // Array of ambient lights for last scene
 	ambLights   []*light.Ambient           // Array of ambient lights for last scene
 	dirLights   []*light.Directional       // Array of directional lights for last scene
 	dirLights   []*light.Directional       // Array of directional lights for last scene
 	pointLights []*light.Point             // Array of point
 	pointLights []*light.Point             // Array of point
@@ -61,15 +64,46 @@ func (r *Renderer) AddProgram(name, vertex, frag string, others ...string) {
 	r.shaman.AddProgram(name, vertex, frag, others...)
 	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)
-//}
+// SetGui sets the gui panel which contains the Gui to render
+// over the optional 3D scene.
+// If set to nil, no Gui will be rendered
+func (r *Renderer) SetGui(gui gui.IPanel) {
 
 
-func (r *Renderer) Render(iscene core.INode, icam camera.ICamera) error {
+	r.gui = gui
+}
+
+// SetScene sets the Node which contains the scene to render
+// If set to nil, no scene will be rendered
+func (r *Renderer) SetScene(scene core.INode) {
+
+	r.scene = scene
+}
+
+// Render renders the previously set Scene and Gui using the specified camera
+func (r *Renderer) Render(icam camera.ICamera) error {
+
+	// Renders the 3D scene
+	if r.scene != nil {
+		r.gs.Clear(gls.DEPTH_BUFFER_BIT | gls.STENCIL_BUFFER_BIT | gls.COLOR_BUFFER_BIT)
+		err := r.renderScene(r.scene, icam)
+		if err != nil {
+			return err
+		}
+	}
+	// Renders the Gui over the 3D scene
+	if r.gui != nil {
+		r.gs.Clear(gls.DEPTH_BUFFER_BIT)
+		err := r.renderGui(icam)
+		//err := r.renderScene(r.gui, icam)
+		if err != nil {
+			return err
+		}
+	}
+	return nil
+}
+
+// renderScene renders the 3D scene using the specified camera
+func (r *Renderer) renderScene(iscene core.INode, icam camera.ICamera) error {
 
 
 	// Updates world matrices of all scene nodes
 	// Updates world matrices of all scene nodes
 	iscene.UpdateMatrixWorld()
 	iscene.UpdateMatrixWorld()
@@ -189,3 +223,51 @@ func (r *Renderer) Render(iscene core.INode, icam camera.ICamera) error {
 	}
 	}
 	return nil
 	return nil
 }
 }
+
+// renderGui renders the Gui
+func (r *Renderer) renderGui(icam camera.ICamera) error {
+
+	// Updates panels bounds and relative positions
+	parent := r.gui.GetPanel()
+	parent.UpdateMatrixWorld()
+
+	// Builds RenderInfo calls RenderSetup for all visible nodes
+	icam.ViewMatrix(&r.rinfo.ViewMatrix)
+	icam.ProjMatrix(&r.rinfo.ProjMatrix)
+
+	var buildRenderList func(ipan gui.IPanel)
+	buildRenderList = func(ipan gui.IPanel) {
+		pan := ipan.GetPanel()
+		if !pan.Visible() {
+			return
+		}
+		gr := pan.GetGraphic()
+		materials := gr.Materials()
+		for i := 0; i < len(materials); i++ {
+			r.grmats = append(r.grmats, &materials[i])
+		}
+		for _, ichild := range pan.Children() {
+			buildRenderList(ichild.(gui.IPanel))
+		}
+	}
+
+	r.grmats = r.grmats[0:0]
+	buildRenderList(parent)
+
+	// For each *GraphicMaterial
+	for _, grmat := range r.grmats {
+		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()
+		_, err := r.shaman.SetProgram(&r.specs)
+		if err != nil {
+			return err
+		}
+		// Render this graphic material
+		grmat.Render(r.gs, &r.rinfo)
+	}
+
+	return nil
+}