leonsal 8 vuotta sitten
vanhempi
commit
1865723142
2 muutettua tiedostoa jossa 123 lisäystä ja 23 poistoa
  1. 113 15
      gui/chart.go
  2. 10 8
      gui/panel.go

+ 113 - 15
gui/chart.go

@@ -191,9 +191,24 @@ func (cl *ChartLine) AddGraph(color *math32.Color, data []float32) *LineGraph {
 	graph := newLineGraph(cl, color, data)
 	cl.graphs = append(cl.graphs, graph)
 	cl.Add(graph)
+	cl.recalc()
 	return graph
 }
 
+func (cl *ChartLine) RemoveGraph(g *LineGraph) {
+
+	cl.Remove(g)
+	g.Dispose()
+	for pos, current := range cl.graphs {
+		if current == g {
+			copy(cl.graphs[pos:], cl.graphs[pos+1:])
+			cl.graphs[len(cl.graphs)-1] = nil
+			cl.graphs = cl.graphs[:len(cl.graphs)-1]
+			break
+		}
+	}
+}
+
 func (cl *ChartLine) calcRangeY() {
 
 	if !cl.autoY {
@@ -256,7 +271,15 @@ func (cl *ChartLine) recalc() {
 
 	// Recalc graphs
 	for i := 0; i < len(cl.graphs); i++ {
-		cl.graphs[i].recalc()
+		g := cl.graphs[i]
+		g.recalc()
+		cl.SetTopChild(g)
+	}
+
+	// Sets the title at the top
+	if cl.title != nil {
+		log.Error("TITLE TOP")
+		cl.SetTopChild(cl.title)
 	}
 }
 
@@ -267,10 +290,11 @@ func (cl *ChartLine) recalc() {
 //
 //
 type ChartScaleX struct {
-	Panel               // Embedded panel
-	chart *ChartLine    // Container chart
-	lines int           // Number of vertical lines
-	mat   chartMaterial // Chart material
+	Panel                // Embedded panel
+	chart  *ChartLine    // Container chart
+	lines  int           // Number of vertical lines
+	bounds gls.Uniform4f // Bound uniform in OpenGL window coordinates
+	mat    chartMaterial // Chart material
 }
 
 // newChartScaleX creates and returns a pointer to a new ChartScaleX for the specified
@@ -280,6 +304,7 @@ func newChartScaleX(chart *ChartLine, lines int, color *math32.Color) *ChartScal
 	sx := new(ChartScaleX)
 	sx.chart = chart
 	sx.lines = lines
+	sx.bounds.Init("Bounds")
 
 	// Appends bottom horizontal line
 	positions := math32.NewArrayF32(0, 0)
@@ -322,6 +347,7 @@ func (sx *ChartScaleX) recalc() {
 }
 
 // RenderSetup is called by the renderer before drawing this graphic
+// It overrides the original panel RenderSetup
 // Calculates the model matrix and transfer to OpenGL.
 func (sx *ChartScaleX) RenderSetup(gs *gls.GLS, rinfo *core.RenderInfo) {
 
@@ -331,6 +357,11 @@ func (sx *ChartScaleX) RenderSetup(gs *gls.GLS, rinfo *core.RenderInfo) {
 	sx.SetModelMatrix(gs, &mm)
 	sx.modelMatrixUni.SetMatrix4(&mm)
 	sx.modelMatrixUni.Transfer(gs)
+
+	// Sets bounds in OpenGL window coordinates and transfer to shader
+	_, _, _, height := gs.GetViewport()
+	sx.bounds.Set(sx.pospix.X, float32(height)-sx.pospix.Y, sx.width, sx.height)
+	sx.bounds.Transfer(gs)
 }
 
 //
@@ -338,10 +369,11 @@ func (sx *ChartScaleX) RenderSetup(gs *gls.GLS, rinfo *core.RenderInfo) {
 // horizontal and labels.
 //
 type ChartScaleY struct {
-	Panel               // Embedded panel
-	chart *ChartLine    // Container chart
-	lines int           // Number of horizontal lines
-	mat   chartMaterial // Chart material
+	Panel                // Embedded panel
+	chart  *ChartLine    // Container chart
+	lines  int           // Number of horizontal lines
+	bounds gls.Uniform4f // Bound uniform in OpenGL window coordinates
+	mat    chartMaterial // Chart material
 }
 
 // newChartScaleY creates and returns a pointer to a new ChartScaleY for the specified
@@ -351,6 +383,7 @@ func newChartScaleY(chart *ChartLine, lines int, color *math32.Color) *ChartScal
 	sy := new(ChartScaleY)
 	sy.chart = chart
 	sy.lines = lines
+	sy.bounds.Init("Bounds")
 
 	// Appends left vertical line
 	positions := math32.NewArrayF32(0, 0)
@@ -388,23 +421,43 @@ func (sy *ChartScaleY) recalc() {
 	sy.SetSize(sy.chart.ContentWidth()-sy.chart.left, sy.chart.ContentHeight()-py-sy.chart.bottom)
 }
 
+// RenderSetup is called by the renderer before drawing this graphic
+// It overrides the original panel RenderSetup
+// Calculates the model matrix and transfer to OpenGL.
+func (sy *ChartScaleY) RenderSetup(gs *gls.GLS, rinfo *core.RenderInfo) {
+
+	//log.Error("ChartScaleY RenderSetup:%v", sy.pospix)
+	// Sets model matrix and transfer to shader
+	var mm math32.Matrix4
+	sy.SetModelMatrix(gs, &mm)
+	sy.modelMatrixUni.SetMatrix4(&mm)
+	sy.modelMatrixUni.Transfer(gs)
+
+	// Sets bounds in OpenGL window coordinates and transfer to shader
+	_, _, _, height := gs.GetViewport()
+	sy.bounds.Set(sy.pospix.X, float32(height)-sy.pospix.Y, sy.width, sy.height)
+	sy.bounds.Transfer(gs)
+}
+
 //
 //
 // LineGraph
 //
 //
 type LineGraph struct {
-	Panel               // Embedded panel
-	chart *ChartLine    // Container chart
-	color math32.Color  // Line color
-	y     []float32     // Data y
-	mat   chartMaterial // Chart material
+	Panel                // Embedded panel
+	chart  *ChartLine    // Container chart
+	color  math32.Color  // Line color
+	y      []float32     // Data y
+	bounds gls.Uniform4f // Bound uniform in OpenGL window coordinates
+	mat    chartMaterial // Chart material
 }
 
 func newLineGraph(chart *ChartLine, color *math32.Color, y []float32) *LineGraph {
 
 	log.Error("newLineGraph")
 	lg := new(LineGraph)
+	lg.bounds.Init("Bounds")
 	lg.chart = chart
 	lg.color = *color
 	lg.y = y
@@ -421,6 +474,11 @@ func (lg *LineGraph) SetData(x, y []float32) {
 	lg.y = y
 }
 
+func (lg *LineGraph) SetLineWidth(width float32) {
+
+	lg.mat.SetLineWidth(width)
+}
+
 func (lg *LineGraph) setGeometry() {
 
 	lg.chart.calcRangeY()
@@ -443,6 +501,9 @@ func (lg *LineGraph) setGeometry() {
 		}
 		vy := lg.y[x]
 		py := -1 + (vy / rangeY)
+		if py > 0 {
+			log.Error("PY:%v", py)
+		}
 		positions.Append(px, py, 0)
 	}
 
@@ -472,6 +533,24 @@ func (lg *LineGraph) recalc() {
 	lg.SetSize(w, h)
 }
 
+// RenderSetup is called by the renderer before drawing this graphic
+// It overrides the original panel RenderSetup
+// Calculates the model matrix and transfer to OpenGL.
+func (lg *LineGraph) RenderSetup(gs *gls.GLS, rinfo *core.RenderInfo) {
+
+	//log.Error("LineGraph RenderSetup:%v with/height: %v/%v", lg.posclip, lg.wclip, lg.hclip)
+	// Sets model matrix and transfer to shader
+	var mm math32.Matrix4
+	lg.SetModelMatrix(gs, &mm)
+	lg.modelMatrixUni.SetMatrix4(&mm)
+	lg.modelMatrixUni.Transfer(gs)
+
+	// Sets bounds in OpenGL window coordinates and transfer to shader
+	_, _, _, height := gs.GetViewport()
+	lg.bounds.Set(lg.pospix.X, float32(height)-lg.pospix.Y, lg.width, lg.height)
+	lg.bounds.Transfer(gs)
+}
+
 //
 //
 // Chart material (for lines)
@@ -522,7 +601,8 @@ void main() {
 
     // Set position
     vec4 pos = vec4(VertexPosition.xyz, 1);
-    gl_Position = ModelMatrix * pos;
+	vec4 posclip = ModelMatrix * pos;
+    gl_Position = posclip;
 }
 `
 
@@ -532,11 +612,29 @@ void main() {
 const shaderChartFrag = `
 #version {{.Version}}
 
+// Input uniforms from vertex shader
 in vec3 Color;
+
+// Input uniforms
+uniform vec4 Bounds;
+
+// Output
 out vec4 FragColor;
 
 void main() {
 
+    // Discard fragment outside of the received bounds in OpenGL window pixel coordinates
+    // Bounds[0] - x
+    // Bounds[1] - y
+    // Bounds[2] - width
+    // Bounds[3] - height
+    if (gl_FragCoord.x < Bounds[0] || gl_FragCoord.x > Bounds[0] + Bounds[2]) {
+        discard;
+    }
+    if (gl_FragCoord.y > Bounds[1] || gl_FragCoord.y < Bounds[1] - Bounds[3]) {
+        discard;
+    }
+
     FragColor = vec4(Color, 1.0);
 }
 `

+ 10 - 8
gui/panel.go

@@ -69,6 +69,9 @@ type Panel struct {
 	paddingUni       gls.Uniform4f       // pointer to padding uniform (texture coordinates)
 	contentUni       gls.Uniform4f       // pointer to content uniform (texture coordinates)
 	pospix           math32.Vector3      // absolute position in pixels
+	posclip          math32.Vector3      // position in clip (NDC) coordinates
+	wclip            float32             // width in clip coordinates
+	hclip            float32             // height in clip coordinates
 	xmin             float32             // minimum absolute x this panel can use
 	xmax             float32             // maximum absolute x this panel can use
 	ymin             float32             // minimum absolute y this panel can use
@@ -815,21 +818,20 @@ func (p *Panel) SetModelMatrix(gl *gls.GLS, mm *math32.Matrix4) {
 	fheight := float32(height)
 
 	// Scale the quad for the viewport so it has fixed dimensions in pixels.
-	fw := float32(p.width) / fwidth
-	fh := float32(p.height) / fheight
+	p.wclip = 2 * float32(p.width) / fwidth
+	p.hclip = 2 * float32(p.height) / fheight
 	var scale math32.Vector3
-	scale.Set(2*fw, 2*fh, 1)
+	scale.Set(p.wclip, p.hclip, 1)
 
 	// Convert absolute position in pixel coordinates from the top/left to
 	// standard OpenGL clip coordinates of the quad center
-	var posclip math32.Vector3
-	posclip.X = (p.pospix.X - fwidth/2) / (fwidth / 2)
-	posclip.Y = -(p.pospix.Y - fheight/2) / (fheight / 2)
-	posclip.Z = p.Position().Z
+	p.posclip.X = (p.pospix.X - fwidth/2) / (fwidth / 2)
+	p.posclip.Y = -(p.pospix.Y - fheight/2) / (fheight / 2)
+	p.posclip.Z = p.Position().Z
 	//log.Debug("panel posclip:%v\n", posclip)
 
 	// Calculates the model matrix
 	var quat math32.Quaternion
 	quat.SetIdentity()
-	mm.Compose(&posclip, &quat, &scale)
+	mm.Compose(&p.posclip, &quat, &scale)
 }