Bladeren bron

Merge remote-tracking branch 'origin/master'

Daniel Salvadori 8 jaren geleden
bovenliggende
commit
7ecd67b47b
4 gewijzigde bestanden met toevoegingen van 43 en 24 verwijderingen
  1. 2 3
      geometry/geometry.go
  2. 17 6
      gls/vbo.go
  3. 20 13
      gui/panel.go
  4. 4 2
      gui/root.go

+ 2 - 3
geometry/geometry.go

@@ -151,7 +151,7 @@ func (g *Geometry) VBO(attrib string) *gls.VBO {
 
 // Returns the number of items in the first VBO
 // (The number of items should be same for all VBOs)
-// An item is a complete vertex position (3 floats) for example
+// An item is a complete group of attributes in the VBO buffer
 func (g *Geometry) Items() int {
 
 	if len(g.vbos) == 0 {
@@ -161,8 +161,7 @@ func (g *Geometry) Items() int {
 	if vbo.AttribCount() == 0 {
 		return 0
 	}
-	attrib := vbo.AttribAt(0)
-	return vbo.Buffer().Size() / int(attrib.ItemSize)
+	return vbo.Buffer().Bytes() / vbo.Stride()
 }
 
 // BoundingBox computes the bounding box of the geometry if necessary

+ 17 - 6
gls/vbo.go

@@ -103,6 +103,20 @@ func (vbo *VBO) Update() {
 	vbo.update = true
 }
 
+// Stride returns the stride of this VBO which is the number of bytes
+// used by one group attributes side by side in the buffer
+// Ex: x y z r g b x y z r g b ...x y z r g b
+// The stride will be: sizeof(float) * 6 = 24
+func (vbo *VBO) Stride() int {
+
+	stride := 0
+	elsize := int(unsafe.Sizeof(float32(0)))
+	for _, attrib := range vbo.attribs {
+		stride += elsize * int(attrib.ItemSize)
+	}
+	return stride
+}
+
 // Transfer is called internally and transfer the data in the VBO buffer to OpenGL if necessary
 func (vbo *VBO) Transfer(gs *GLS) {
 
@@ -116,14 +130,11 @@ func (vbo *VBO) Transfer(gs *GLS) {
 		vbo.handle = gs.GenBuffer()
 		gs.BindBuffer(ARRAY_BUFFER, vbo.handle)
 		// Calculates stride
-		elsize := int32(unsafe.Sizeof(float32(0)))
-		var stride int32 = 0
-		for _, attrib := range vbo.attribs {
-			stride += elsize * attrib.ItemSize
-		}
+		stride := vbo.Stride()
 		// For each attribute
 		var items uint32 = 0
 		var offset uint32 = 0
+		elsize := int32(unsafe.Sizeof(float32(0)))
 		for _, attrib := range vbo.attribs {
 			// Get attribute location in the current program
 			loc := gs.Prog.GetAttribLocation(attrib.Name)
@@ -132,7 +143,7 @@ func (vbo *VBO) Transfer(gs *GLS) {
 			}
 			// Enables attribute and sets its stride and offset in the buffer
 			gs.EnableVertexAttribArray(uint32(loc))
-			gs.VertexAttribPointer(uint32(loc), attrib.ItemSize, FLOAT, false, stride, offset)
+			gs.VertexAttribPointer(uint32(loc), attrib.ItemSize, FLOAT, false, int32(stride), offset)
 			items += uint32(attrib.ItemSize)
 			offset = uint32(elsize) * items
 		}

+ 20 - 13
gui/panel.go

@@ -768,6 +768,25 @@ func (p *Panel) resize(width, height float32) {
 // RenderSetup is called by the Engine before drawing the object
 func (p *Panel) RenderSetup(gl *gls.GLS, rinfo *core.RenderInfo) {
 
+	// Sets model matrix
+	var mm math32.Matrix4
+	p.SetModelMatrix(gl, &mm)
+	p.modelMatrixUni.SetMatrix4(&mm)
+
+	// Transfer uniforms
+	p.borderColorUni.Transfer(gl)
+	p.paddingColorUni.Transfer(gl)
+	p.contentColorUni.Transfer(gl)
+	p.boundsUni.Transfer(gl)
+	p.borderUni.Transfer(gl)
+	p.paddingUni.Transfer(gl)
+	p.contentUni.Transfer(gl)
+	p.modelMatrixUni.Transfer(gl)
+}
+
+// SetModelMatrix calculates and sets the specified matrix with the model matrix for this panel
+func (p *Panel) SetModelMatrix(gl *gls.GLS, mm *math32.Matrix4) {
+
 	// Get the current viewport width and height
 	_, _, width, height := gl.GetViewport()
 	fwidth := float32(width)
@@ -787,20 +806,8 @@ func (p *Panel) RenderSetup(gl *gls.GLS, rinfo *core.RenderInfo) {
 	posclip.Z = p.pospix.Z
 	//log.Debug("panel posclip:%v\n", posclip)
 
-	// Sets the model matrix uniform
-	var mm math32.Matrix4
+	// Calculates the model matrix
 	var quat math32.Quaternion
 	quat.SetIdentity()
 	mm.Compose(&posclip, &quat, &scale)
-	p.modelMatrixUni.SetMatrix4(&mm)
-
-	// Transfer uniforms
-	p.borderColorUni.Transfer(gl)
-	p.paddingColorUni.Transfer(gl)
-	p.contentColorUni.Transfer(gl)
-	p.boundsUni.Transfer(gl)
-	p.borderUni.Transfer(gl)
-	p.paddingUni.Transfer(gl)
-	p.contentUni.Transfer(gl)
-	p.modelMatrixUni.Transfer(gl)
 }

+ 4 - 2
gui/root.go

@@ -253,8 +253,10 @@ func (r *Root) sendPanels(x, y float32, evname string, ev interface{}) {
 		}
 		// Checks if any of its children also contains the position
 		for _, child := range pan.Children() {
-			ipan := child.(IPanel)
-			checkPanel(ipan)
+			ipan, ok := child.(IPanel)
+			if ok {
+				checkPanel(ipan)
+			}
 		}
 	}