func (buf *IndexBuffer) Unpack(res *resource.Container) error { buf.Stride = 3 * 2 // 3*uint16 /* is this stored anywhere? */ res.Parse(&buf.IndexHeader) buf.Index = make([]*types.Tri, buf.Count/3) for i := range buf.Index { buf.Index[i] = new(types.Tri) } if err := res.Detour(buf.Buffer, func() error { buffer := make([]byte, buf.Stride) reader := bytes.NewReader(buffer) for _, idx := range buf.Index { /* Read the index into our local buffer */ if size, err := res.Read(buffer); size != buf.Stride || err != nil { return err } /* Parse out the info we can */ if err := binary.Read(reader, binary.BigEndian, idx); err != nil { return err } reader.Seek(0, 0) } return nil }); err != nil { return err } return nil }
func (shader *Shader) Unpack(res *resource.Container) error { res.Parse(&shader.Header) shader.Parameters = make([]*Parameter, shader.ParameterCount) for i := range shader.Parameters { shader.Parameters[i] = new(Parameter) } if err := res.Detour(shader.ParameterList, func() error { for _, param := range shader.Parameters { if err := param.Unpack(res); err != nil { return err } if param.parameter != nil { var err error switch param.Type { case ParamDiffuseBitmap: shader.DiffusePath, err = param.parameter.(*BitmapParameter).Get(res) case ParamNormalBitmap: shader.NormalPath, err = param.parameter.(*BitmapParameter).Get(res) } if err != nil { return err } } } return nil }); err != nil { return err } return nil }
func (buf *VertexBuffer) Unpack(res *resource.Container) error { res.Parse(&buf.VertexHeader) if err := res.Detour(buf.Info, func() error { res.Parse(&buf.VertexInfo) return nil }); err != nil { return err } buf.Vertex = make([]*Vertex, buf.Count) for i := range buf.Vertex { buf.Vertex[i] = new(Vertex) } if err := res.Detour(buf.Buffer, func() error { for _, vert := range buf.Vertex { if err := vert.Unpack(res, buf); err != nil { return err } } return nil }); err != nil { return err } return nil }
func (param *Parameter) Unpack(res *resource.Container) error { res.Parse(¶m.ParameterHeader) if !param.Offset.Valid() { return nil } switch param.Type { case ParamDiffuseBitmap, ParamNormalBitmap: bitmap := new(BitmapParameter) if err := res.Detour(param.Offset, func() error { res.Parse(bitmap) param.parameter = bitmap return nil }); err != nil { return err } default: /* unsupported parameter. don't bother giving an error for now */ param.parameter = nil } return nil }
func (group *Group) Unpack(res *resource.Container) error { res.Parse(&group.GroupHeader) /* Read any texture dictionary */ if group.TexturePtr.Valid() { if err := res.Detour(group.TexturePtr, func() error { group.Texture = new(texture.Texture) return group.Texture.Unpack(res) }); err != nil { return err } } /* Read our shader headers */ group.Shaders = make([]*Shader, group.Count) for i := range group.Shaders { group.Shaders[i] = new(Shader) } /* Read the shaders */ for i, shader := range group.Shaders { if err := group.Detour(res, i, func() error { if err := shader.Unpack(res); err != nil { return err } return nil }); err != nil { log.Printf("Error reading shader %v\n", i) return err } } return nil }
func (b *Bitmap) Unpack(res *resource.Container) error { res.Parse(&b.Header) if b.Header.Title.Valid() { res.Detour(b.Header.Title, func() error { res.Parse(&b.Title) return nil }) } return nil }
func (frag *FragType) Unpack(res *resource.Container) error { res.Parse(&frag.Header) if err := res.Detour(frag.Header.Drawable, func() error { frag.Drawable.Unpack(res) return nil }); err != nil { return err } return nil }
func (bmp *BitmapParameter) Get(res *resource.Container) (string, error) { if !bmp.Path.Valid() { return "", nil } var path string if err := res.Detour(bmp.Path, func() error { res.Parse(&path) return nil }); err != nil { return "", err } return path, nil }
func (vol *Volume) Unpack(res *resource.Container) error { res.Parse(&vol.VolumeHeader) vol.Mesh = export.NewMesh() res.Detour(vol.VerticesAddr, func() error { return vol.unpackVertices(res) }) res.Detour(vol.IndicesAddr, func() error { return vol.unpackFaces(res) }) return nil }
func (geom *Geometry) Unpack(res *resource.Container) error { res.Parse(&geom.GeometryHeader) if err := res.Detour(geom.VertexBuffer, func() error { return geom.Vertices.Unpack(res) }); err != nil { log.Printf("error parsing vertex buffer") } if err := res.Detour(geom.IndexBuffer, func() error { return geom.Indices.Unpack(res) }); err != nil { log.Printf("error parsing index buffer") } return nil }
func (drawable *Drawable) Unpack(res *resource.Container) error { res.Parse(&drawable.Header) drawable.Model = export.NewModel() /* unpack */ if drawable.Header.ShaderTable.Valid() { if err := res.Detour(drawable.Header.ShaderTable, func() error { return drawable.Shaders.Unpack(res) }); err != nil { return err } } if err := res.Detour(drawable.Header.ModelCollection, func() error { return drawable.Models.Unpack(res) }); err != nil { return err } if drawable.Header.Title.Valid() { if err := res.Detour(drawable.Header.Title, func() error { res.Parse(&drawable.Title) drawable.Title = drawable.Title[:strings.LastIndex(drawable.Title, ".")] return nil }); err != nil { return err } } else { drawable.Title = fmt.Sprintf("unnamed_%v", NextUnnamedIndex) NextUnnamedIndex++ } /* Load everything into our exportable */ drawable.Model.Name = drawable.Title for _, shader := range drawable.Shaders.Shaders { material := export.NewMaterial() if shader.DiffusePath != "" { material.DiffBitmap = fmt.Sprintf("%v.dds", shader.DiffusePath) } drawable.Model.AddMaterial(material) } for _, model := range drawable.Models.Models { for _, geom := range model.Geometry { mesh := export.NewMesh() mesh.Material = int(geom.Shader) for _, vert := range geom.Vertices.Vertex { /* Even if a feature isn't supported, the nil value should be fine */ newVert := export.Vertex{ Pos: mathgl.Vec4f{ vert.WorldCoord[0], vert.WorldCoord[1], vert.WorldCoord[2], 1.0, }, UV: mathgl.Vec2f{ vert.UV0.U.Value(), (-vert.UV0.V.Value()) + 1, }, Colour: vert.Colour, } mesh.AddVert(newVert) } mesh.Format = geom.Vertices.Format for _, face := range geom.Indices.Index { mesh.AddFace(*face) } drawable.Model.AddMesh(mesh) } } return nil }