From 5b5d091d569813ce85a0c4513e77ca9d534d1b49 Mon Sep 17 00:00:00 2001 From: bloeys Date: Sat, 20 Nov 2021 00:37:59 +0400 Subject: [PATCH] Nodes implementation --- README.md | 2 +- asig/asig.go | 79 ++++++++++++++++++++++++++++++++++++++++++++++------ main.go | 2 +- 3 files changed, 73 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index ee12fde..c914618 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ The following features are already implemented: Unimplemented (yet) AssImp Scene objects: -* Nodes +* Animation * Lights * Camera * Metadata diff --git a/asig/asig.go b/asig/asig.go index 1e2ca93..2f36294 100755 --- a/asig/asig.go +++ b/asig/asig.go @@ -23,15 +23,31 @@ import ( ) type Node struct { + Name string + + //The transformation relative to the node's parent + Transformation *gglm.Mat4 + + //Parent node. NULL if this node is the root node + Parent *Node + + //The child nodes of this node. NULL if mNumChildren is 0 + Children []*Node + + //Each entry is an index into the mesh list of the scene + MeshIndicies []uint + + /** Metadata associated with this node or NULL if there is no metadata. + * Whether any metadata is generated depends on the source file format. See the + * @link importer_notes @endlink page for more information on every source file + * format. Importers that don't document any metadata don't write any. + */ + Metadata *Metadata } type Animation struct { } -type Texel struct { - R, G, B, A byte -} - type EmbeddedTexture struct { cTex *C.struct_aiTexture @@ -152,6 +168,7 @@ func parseScene(cs *C.struct_aiScene) *Scene { s := &Scene{cScene: cs} s.Flags = SceneFlag(cs.mFlags) + s.RootNode = parseRootNode(cs.mRootNode) s.Meshes = parseMeshes(cs.mMeshes, uint(cs.mNumMeshes)) s.Materials = parseMaterials(cs.mMaterials, uint(cs.mNumMaterials)) s.Textures = parseTextures(cs.mTextures, uint(s.cScene.mNumTextures)) @@ -159,6 +176,52 @@ func parseScene(cs *C.struct_aiScene) *Scene { return s } +func parseRootNode(cNodesIn *C.struct_aiNode) *Node { + + rn := &Node{ + Name: parseAiString(cNodesIn.mName), + Transformation: parseMat4(&cNodesIn.mTransformation), + Parent: nil, + MeshIndicies: parseUInts(cNodesIn.mMeshes, uint(cNodesIn.mNumMeshes)), + //TODO: Fill this + Metadata: &Metadata{}, + } + + rn.Children = parseNodes(cNodesIn.mChildren, rn) + return rn +} + +func parseNodes(cNodesIn **C.struct_aiNode, parent *Node) []*Node { + + if cNodesIn == nil { + return []*Node{} + } + + nodes := make([]*Node, len(parent.Children)) + cNodes := unsafe.Slice(cNodesIn, len(parent.Children)) + + for i := 0; i < len(nodes); i++ { + + n := cNodes[i] + + //Fill basic node info + nodes[i] = &Node{ + Name: parseAiString(n.mName), + Transformation: parseMat4(&n.mTransformation), + Parent: parent, + MeshIndicies: parseUInts(n.mMeshes, uint(n.mNumMeshes)), + + //TODO: Fill this + Metadata: &Metadata{}, + } + + //Parse node's children + nodes[i].Children = parseNodes(n.mChildren, nodes[i]) + } + + return nodes +} + func parseTextures(cTexIn **C.struct_aiTexture, count uint) []*EmbeddedTexture { if cTexIn == nil { @@ -367,20 +430,20 @@ func parseBones(cbs **C.struct_aiBone, count uint) []*Bone { bones[i] = &Bone{ Name: parseAiString(cBone.mName), Weights: parseVertexWeights(cBone.mWeights, uint(cBone.mNumWeights)), - OffsetMatrix: parseMat4(&cBone.mOffsetMatrix), + OffsetMatrix: *parseMat4(&cBone.mOffsetMatrix), } } return bones } -func parseMat4(cm4 *C.struct_aiMatrix4x4) gglm.Mat4 { +func parseMat4(cm4 *C.struct_aiMatrix4x4) *gglm.Mat4 { if cm4 == nil { - return gglm.Mat4{} + return &gglm.Mat4{} } - return gglm.Mat4{ + return &gglm.Mat4{ Data: [4][4]float32{ {float32(cm4.a1), float32(cm4.b1), float32(cm4.c1), float32(cm4.d1)}, {float32(cm4.a2), float32(cm4.b2), float32(cm4.c2), float32(cm4.d2)}, diff --git a/main.go b/main.go index cf37f18..20e7740 100755 --- a/main.go +++ b/main.go @@ -47,7 +47,7 @@ func main() { for i := 0; i < len(ts); i++ { t := ts[i] - fmt.Printf("T(%v): Name=%v, Hint=%v, Width=%v, Height=%v, NumTexels=%v", i, t.Filename, t.FormatHint, t.Width, t.Height, len(t.Data)) + fmt.Printf("T(%v): Name=%v, Hint=%v, Width=%v, Height=%v, NumTexels=%v\n", i, t.Filename, t.FormatHint, t.Width, t.Height, len(t.Data)) if t.FormatHint == "png" { decodePNG(t.Data)