Преглед на файлове

Merge branch 'master' into gltf

Daniel Salvadori преди 7 години
родител
ревизия
2122b3dbf6
променени са 3 файла, в които са добавени 67 реда и са изтрити 6 реда
  1. 1 1
      README.md
  2. 49 0
      animation/channel.go
  3. 17 5
      gls/vbo.go

+ 1 - 1
README.md

@@ -75,7 +75,7 @@ install the packages. Make sure your GOPATH is set correctly.
 * Users' applications can use their own vertex and fragment shaders.
 
 <p align="center">
-  <img style="float: right;" src="https://github.com/g3n/g3n.github.io/blob/master/g3n_banner_small.png" alt="G3N Banner"/>
+  <img style="float: right;" src="https://github.com/g3n/g3n.github.io/raw/master/img/g3n_banner_small.png" alt="G3N Banner"/>
 </p>
 
 ## Basic application

+ 49 - 0
animation/channel.go

@@ -7,6 +7,7 @@ package animation
 import (
 	"github.com/g3n/engine/core"
 	"github.com/g3n/engine/math32"
+	"github.com/g3n/engine/geometry"
 )
 
 // A Channel associates an animation parameter channel to an interpolation sampler
@@ -243,6 +244,54 @@ func NewScaleChannel(node core.INode) *ScaleChannel {
 	return sc
 }
 
+// MorphChannel is the IChannel for morph geometries.
+type MorphChannel struct {
+	Channel
+	target *geometry.MorphGeometry
+}
+
+func NewMorphChannel(mg *geometry.MorphGeometry) *MorphChannel {
+
+	mc := new(MorphChannel)
+	mc.target = mg
+	numWeights := len(mg.Weights())
+	mc.updateInterpAction = func() {
+		// Update interpolation function
+		switch mc.interpType {
+		case STEP:
+			mc.interpAction = func(idx int, k float32) {
+				start := idx*numWeights
+				weights := mc.values[start:start+numWeights]
+				mg.SetWeights(weights)
+			}
+		case LINEAR:
+			mc.interpAction = func(idx int, k float32) {
+				start1 := idx*numWeights
+				start2 := (idx+1)*numWeights
+				weights1 := mc.values[start1:start1+numWeights]
+				weights2 := mc.values[start2:start2+numWeights]
+				for i := range weights1 {
+					weights1[i] = weights1[i] + (weights2[i]-weights1[i])*k
+				}
+				mg.SetWeights(weights1)
+			}
+		case CUBICSPLINE: // TODO
+			mc.interpAction = func(idx int, k float32) {
+				start1 := idx*numWeights
+				start2 := (idx+1)*numWeights
+				weights1 := mc.values[start1:start1+numWeights]
+				weights2 := mc.values[start2:start2+numWeights]
+				for i := range weights1 {
+					weights1[i] = weights1[i] + (weights2[i]-weights1[i])*k
+				}
+				mg.SetWeights(weights1)
+			}
+		}
+	}
+	mc.SetInterpolationType(LINEAR)
+	return mc
+}
+
 // InterpolationType specifies the interpolation type.
 type InterpolationType string
 

+ 17 - 5
gls/vbo.go

@@ -6,7 +6,6 @@ package gls
 
 import (
 	"github.com/g3n/engine/math32"
-	"unsafe"
 )
 
 // VBO abstracts an OpenGL Vertex Buffer Object.
@@ -35,6 +34,7 @@ const (
 	Undefined = AttribType(iota)
 	VertexPosition
 	VertexNormal
+	VertexTangent
 	VertexColor
 	VertexTexcoord
 )
@@ -43,6 +43,7 @@ const (
 var attribTypeNameMap = map[AttribType]string{
 	VertexPosition: "VertexPosition",
 	VertexNormal:   "VertexNormal",
+	VertexTangent:  "VertexTangent",
 	VertexColor:    "VertexColor",
 	VertexTexcoord: "VertexTexcoord",
 }
@@ -55,7 +56,16 @@ var attribTypeSizeMap = map[AttribType]int32{
 	VertexTexcoord: 2,
 }
 
-const floatSize = uint32(unsafe.Sizeof(float32(0)))
+// Map from element type to element size (in bytes).
+var elementTypeSizeMap = map[uint32]int{
+	BYTE:           1,
+	UNSIGNED_BYTE:  1,
+	SHORT:          2,
+	UNSIGNED_SHORT: 2,
+	INT:            4,
+	UNSIGNED_INT:   4,
+	FLOAT:          4,
+}
 
 // NewVBO creates and returns a pointer to a new OpenGL Vertex Buffer Object.
 func NewVBO(buffer math32.ArrayF32) *VBO {
@@ -257,9 +267,11 @@ func (vbo *VBO) Stride() int {
 // and the stride size would be: sizeof(float)*stride = 4*5 = 20
 func (vbo *VBO) StrideSize() int {
 
-	stride := vbo.Stride()
-	elsize := int(unsafe.Sizeof(float32(0)))
-	return stride * elsize
+	strideSize := 0
+	for _, attrib := range vbo.attribs {
+		strideSize += int(attrib.NumElements) * elementTypeSizeMap[attrib.ElementType]
+	}
+	return strideSize
 }
 
 // Transfer (called internally) transfers the data from the VBO buffer to OpenGL if necessary.