mirror of
https://github.com/bloeys/assimp-go.git
synced 2025-12-29 08:28:20 +00:00
Compare commits
20 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 25eafaab12 | |||
| 04a2f5b8f2 | |||
| 41ab98ff76 | |||
| bf172f60cd | |||
| 6b65d26014 | |||
| 4e56ab3c9b | |||
| 38aa4923bc | |||
| c8ec2b30b5 | |||
| 41995b60bb | |||
| e6f5dca6da | |||
| f50b60e8eb | |||
| 0bc81ac5ba | |||
| 6bb760fc0a | |||
| 362558c877 | |||
| 7e82370f34 | |||
| 58e13d1c5a | |||
| bfa7357ea2 | |||
| dd346de9de | |||
| babf9c3926 | |||
| 0fdd5d479c |
21
.github/workflows/run-assimp-go.yml
vendored
Executable file
21
.github/workflows/run-assimp-go.yml
vendored
Executable file
@ -0,0 +1,21 @@
|
||||
name: Build-AssImp
|
||||
on:
|
||||
workflow_dispatch:
|
||||
create:
|
||||
|
||||
jobs:
|
||||
Run-assimp-go-macos:
|
||||
runs-on: macos-11
|
||||
steps:
|
||||
- name: Install golang 1.17
|
||||
uses: actions/setup-go@v2
|
||||
with:
|
||||
go-version: '^1.17'
|
||||
- name: Clone assimp-go
|
||||
run: git clone https://github.com/bloeys/assimp-go
|
||||
- name: Copy dylib
|
||||
working-directory: assimp-go
|
||||
run: sudo mkdir -p /usr/local/lib && sudo cp asig/libs/libassimp_darwin_amd64.dylib /usr/local/lib/libassimp.5.dylib
|
||||
- name: Run assimp-go
|
||||
working-directory: assimp-go
|
||||
run: go run .
|
||||
3
.gitignore
vendored
3
.gitignore
vendored
@ -18,4 +18,5 @@ vendor/
|
||||
!obj.obj
|
||||
*.fbx
|
||||
*.glb
|
||||
!libassimp_darwin_arm64.dylib
|
||||
!libassimp_darwin_arm64.dylib
|
||||
!libassimp_darwin_amd64.dylib
|
||||
63
README.md
63
README.md
@ -1,5 +1,7 @@
|
||||
# assimp-go
|
||||
|
||||
[](https://github.com/bloeys/assimp-go/actions/workflows/run-assimp-go.yml)
|
||||
|
||||
A Handcrafted Open Asset Import Library (AssImp) wrapper for Go.
|
||||
|
||||
## Features
|
||||
@ -25,19 +27,53 @@ Unimplemented (yet) AssImp Scene objects:
|
||||
|
||||
To run the project you need:
|
||||
|
||||
* A 64-bit machine (32-bit machines should be supportable if we get help adding the needed libs)
|
||||
* A recent version of [Go](https://golang.org/) installed (1.17+)
|
||||
* A C/C++ compiler installed and in your path
|
||||
* Windows: [MingW](https://www.mingw-w64.org/downloads/#mingw-builds) or similar
|
||||
* Mac/Linux: Should be installed by default, but if not try [GCC](https://gcc.gnu.org/) or [Clang](https://releases.llvm.org/download.html)
|
||||
* Download the appropriate DLL for your version and put it in the root of your Go project
|
||||
* **Windows**: [MingW](https://www.mingw-w64.org/downloads/#mingw-builds) or similar
|
||||
* **Mac/Linux**: Should be installed by default, but if not try [GCC](https://gcc.gnu.org/) or [Clang](https://releases.llvm.org/download.html)
|
||||
|
||||
Then simply clone and use `go run .`
|
||||
Now to be able to run assimp-go you will need the AssImp shared libraries (DLLs/DyLibs), which you can
|
||||
download from the GitHub releases page.
|
||||
|
||||
### Installing on Windows
|
||||
|
||||
Download the **.dll** of the [release you want](https://github.com/bloeys/assimp-go/releases), and place it in the **root** of your Go project.
|
||||
|
||||
### Installing on MacOS
|
||||
|
||||
First, download the appropriate **.dylib** for your device (`_amd64` for Intel CPUs and `_arm64` for Apple CPUs).
|
||||
Next you will need to rename the lib to `libassimp.5.dylib` and move it to `/usr/local/lib` or `/usr/lib`.
|
||||
|
||||
You can use this command to do it: `sudo mkdir -p /usr/local/lib && sudo cp libassimp_darwin*.dylib /usr/local/lib/libassimp.5.dylib`
|
||||
|
||||
### Installing on Linux
|
||||
|
||||
Download the the AssImp package for your distro, or build from source and add it to your path.
|
||||
> NOTE: Insatall assimp >= 3.1 for bindings to work as expected.
|
||||
> Though getting the latest version is always recommended.
|
||||
|
||||
#### Installing on Ubuntu
|
||||
You can install the Asset-Importer-Lib via apt:
|
||||
```
|
||||
sudo apt-get update
|
||||
sudo apt-get install libassimp-dev
|
||||
```
|
||||
|
||||
#### Installing on Arch
|
||||
You can install the Asset-Importer-Lib via pacman
|
||||
```
|
||||
sudo pacman -S assimp
|
||||
```
|
||||
#### Building From Source
|
||||
To build the Asset Importer Package from sorce read the [sorce build guide](https://github.com/assimp/assimp/blob/master/Build.md)
|
||||
|
||||
### Running assimp-go
|
||||
|
||||
Use `go run .` to run the simple example in `main.go` ;)
|
||||
|
||||
> Note: that it might take a while to run the first time because of downloading/compiling dependencies.
|
||||
|
||||
`assimp-go` dynamically links (e.g. through a DLL) AssImp using platform dependent libraries, which are made available with the GitHub releases.
|
||||
Currently only `Windows` libraries are available, but more should be easy to add by compiling AssImp on the wanted platform. (Make a PR if you can help us get those binaries!)
|
||||
|
||||
### Getting Started
|
||||
|
||||
```Go
|
||||
@ -85,14 +121,13 @@ While `asig` functions should NOT be called on a Scene (or its objects) after th
|
||||
|
||||
## Developing assimp-go
|
||||
|
||||
We link against assimp libraries that are built for each platform and the `*.a` files are added to the `asig/libs` package.
|
||||
Depending on the platform we select one of them and link against it when doing `go build`.
|
||||
We link against assimp libraries that are built for each platform and the `.a`/`.dylib` files are added to the `asig/libs` package.
|
||||
At build time, the `#cgo` directive choose the appropriate libs and links against them.
|
||||
|
||||
The general steps are:
|
||||
|
||||
* Copy assimp includes into `asig/assimp`
|
||||
* Copy `zlib.h`, `zconf.h` and `irrXML.h` into `asig/zlib` and `asig/irrxml` respectively.
|
||||
* Copy static libraries and DLL import libraries into `asig/libs`
|
||||
* Copy libraries and DLL import libraries into `asig/libs`
|
||||
|
||||
> Note: When dealing with libraries the compiler will probably (e.g. MinGW does this) ignore `lib` prefixes and `.a`/`.lib` suffixes.
|
||||
So if your lib name is `libassimp.a` you need to pass it to CGO as `-l assimp`, otherwise you will get an error about library not found.
|
||||
@ -111,3 +146,9 @@ Now assuming you are using MinGW on windows:
|
||||
* Run `cmake --build . --parallel 6`
|
||||
* Copy the generated `*.lib` (or `*.a`) files from the `lib` folder and into `asig/libs`, and copy the generated dll from AssImp `bin` folder into the root of `assimp-go`.
|
||||
* Copy the generated `libzlibstatic.a` file from `contrib/zlib` and into the `asig/libs` folder.
|
||||
|
||||
**MacOS**:
|
||||
|
||||
* Clone wanted release of assimp and run `cmake CMakeLists.txt -D ASSIMP_BUILD_ZLIB=ON -D ASSIMP_BUILD_ASSIMP_TOOLS=OFF` in the root folder
|
||||
* Run `cmake --build . --parallel 6`
|
||||
* Copy the generated `*.dylib` files from the `bin` folder and into both `asig/libs` and `/usr/local/lib`
|
||||
|
||||
140
asig/asig.go
140
asig/asig.go
@ -1,17 +1,24 @@
|
||||
package asig
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -I .
|
||||
#cgo LDFLAGS: -L libs
|
||||
#cgo windows,amd64 LDFLAGS: -l assimp_windows_amd64
|
||||
#cgo darwin,arm64 LDFLAGS: -l assimp_darwin_arm64
|
||||
#cgo linux CFLAGS:
|
||||
#cgo windows,amd64 CFLAGS: -I .
|
||||
#cgo darwin,amd64 CFLAGS: -I .
|
||||
#cgo darwin,arm64 CFLAGS: -I .
|
||||
|
||||
#include "wrap.c"
|
||||
#include <stdlib.h>
|
||||
#cgo linux LDFLAGS: -lassimp
|
||||
#cgo windows,amd64 LDFLAGS: -L libs -l assimp_windows_amd64
|
||||
#cgo darwin,amd64 LDFLAGS: -L libs -l assimp_darwin_amd64
|
||||
#cgo darwin,arm64 LDFLAGS: -Wl,-rpath,/usr/local/lib
|
||||
|
||||
#include "wrap.h"
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"io/fs"
|
||||
"runtime"
|
||||
"unsafe"
|
||||
|
||||
"github.com/bloeys/gglm/gglm"
|
||||
@ -157,6 +164,121 @@ func ImportFile(file string, postProcessFlags PostProcess) (s *Scene, release fu
|
||||
return s, func() { s.releaseCResources() }, nil
|
||||
}
|
||||
|
||||
//export go_aiFileOpenProc
|
||||
func go_aiFileOpenProc(io *C.struct_aiFileIO, name *C.char, flags *C.char) *C.struct_aiFile {
|
||||
fsWrapper := (*fsWrapper)(unsafe.Pointer(io.UserData))
|
||||
|
||||
file, err := fsWrapper.fsys.Open(C.GoString(name))
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
fsWrapper.Pin(&file)
|
||||
|
||||
ret := &C.struct_aiFile{
|
||||
ReadProc: (C.aiFileReadProc)(C.cgo_aiFileReadProc),
|
||||
WriteProc: (C.aiFileWriteProc)(C.cgo_aiFileWriteProc),
|
||||
TellProc: (C.aiFileTellProc)(C.cgo_aiFileTellProc),
|
||||
FileSizeProc: (C.aiFileTellProc)(C.cgo_aiFileFileSizeProc),
|
||||
SeekProc: (C.aiFileSeek)(C.cgo_aiFileSeekProc),
|
||||
FlushProc: (C.aiFileFlushProc)(C.cgo_aiFileFlushProc),
|
||||
UserData: (*C.char)(unsafe.Pointer(&file)),
|
||||
}
|
||||
fsWrapper.Pin(ret)
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
//export go_aiFileCloseProc
|
||||
func go_aiFileCloseProc(io *C.struct_aiFileIO, file *C.struct_aiFile) {
|
||||
f := *(*fs.File)(unsafe.Pointer(file.UserData))
|
||||
f.Close()
|
||||
}
|
||||
|
||||
//export go_aiFileReadProc
|
||||
func go_aiFileReadProc(file *C.struct_aiFile, buffer *C.char, size C.size_t, count C.size_t) C.size_t {
|
||||
f := *(*fs.File)(unsafe.Pointer(file.UserData))
|
||||
slice := unsafe.Slice((*byte)(unsafe.Pointer(buffer)), size*count)
|
||||
|
||||
n, err := f.Read(slice)
|
||||
if err != nil {
|
||||
return (C.size_t)(0)
|
||||
}
|
||||
return (C.size_t)(n)
|
||||
}
|
||||
|
||||
//export go_aiFileWriteProc
|
||||
func go_aiFileWriteProc(file *C.struct_aiFile, buffer *C.char, size C.size_t, count C.size_t) C.size_t {
|
||||
panic("aiFileWrite not supported")
|
||||
}
|
||||
|
||||
//export go_aiFileFileSizeProc
|
||||
func go_aiFileFileSizeProc(file *C.struct_aiFile) C.size_t {
|
||||
f := *(*fs.File)(unsafe.Pointer(file.UserData))
|
||||
info, err := f.Stat()
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return (C.size_t)(info.Size())
|
||||
}
|
||||
|
||||
//export go_aiFileFlushProc
|
||||
func go_aiFileFlushProc(file *C.struct_aiFile) {
|
||||
panic("aiFileFlush not supported")
|
||||
}
|
||||
|
||||
//export go_aiFileTellProc
|
||||
func go_aiFileTellProc(file *C.struct_aiFile) C.size_t {
|
||||
panic("aiFileTell not supported")
|
||||
}
|
||||
|
||||
//export go_aiFileSeekProc
|
||||
func go_aiFileSeekProc(file *C.struct_aiFile, offset C.size_t, origin C.enum_aiOrigin) C.enum_aiReturn {
|
||||
f := *(*fs.File)(unsafe.Pointer(file.UserData))
|
||||
if s, ok := f.(io.Seeker); ok {
|
||||
_, err := s.Seek(int64(offset), int(origin))
|
||||
if err == nil {
|
||||
return aiReturnSuccess
|
||||
}
|
||||
}
|
||||
return aiReturnFailure
|
||||
}
|
||||
|
||||
type fsWrapper struct {
|
||||
pinnedMemory runtime.Pinner
|
||||
fsys fs.FS
|
||||
}
|
||||
|
||||
func (fws *fsWrapper) Pin(pointer interface{}) {
|
||||
fws.pinnedMemory.Pin(pointer)
|
||||
}
|
||||
|
||||
func ImportFileEx(file string, postProcessFlags PostProcess, fsys fs.FS) (s *Scene, release func(), err error) {
|
||||
cstr := C.CString(file)
|
||||
defer C.free(unsafe.Pointer(cstr))
|
||||
|
||||
wrapper := &fsWrapper{
|
||||
pinnedMemory: runtime.Pinner{},
|
||||
fsys: fsys,
|
||||
}
|
||||
wrapper.Pin(wrapper)
|
||||
defer wrapper.pinnedMemory.Unpin()
|
||||
|
||||
cio := &C.struct_aiFileIO{
|
||||
OpenProc: (C.aiFileOpenProc)(C.cgo_aiFileOpenProc),
|
||||
CloseProc: (C.aiFileCloseProc)(C.cgo_aiFileCloseProc),
|
||||
UserData: (*C.char)(unsafe.Pointer(wrapper)),
|
||||
}
|
||||
wrapper.Pin(cio)
|
||||
|
||||
cs := C.aiImportFileEx(cstr, C.uint(postProcessFlags), cio)
|
||||
if cs == nil {
|
||||
return nil, func() {}, getAiErr()
|
||||
}
|
||||
|
||||
s = parseScene(cs)
|
||||
return s, func() { s.releaseCResources() }, nil
|
||||
}
|
||||
|
||||
func getAiErr() error {
|
||||
return errors.New("asig error: " + C.GoString(C.aiGetErrorString()))
|
||||
}
|
||||
@ -214,7 +336,7 @@ func parseNodes(cNodesIn **C.struct_aiNode, parent *Node, parentChildrenCount ui
|
||||
}
|
||||
|
||||
//Parse node's children
|
||||
nodes[i].Children = parseNodes(n.mChildren, nodes[i], parentChildrenCount)
|
||||
nodes[i].Children = parseNodes(n.mChildren, nodes[i], uint(n.mNumChildren))
|
||||
}
|
||||
|
||||
return nodes
|
||||
@ -518,6 +640,10 @@ func parseVertexWeights(cWeights *C.struct_aiVertexWeight, count uint) []VertexW
|
||||
}
|
||||
|
||||
func parseAiString(aiString C.struct_aiString) string {
|
||||
if aiString.length == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
return C.GoStringN(&aiString.data[0], C.int(aiString.length))
|
||||
}
|
||||
|
||||
|
||||
BIN
asig/libs/libassimp_darwin_amd64.dylib
Executable file
BIN
asig/libs/libassimp_darwin_amd64.dylib
Executable file
Binary file not shown.
@ -1,13 +1,17 @@
|
||||
package asig
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -I .
|
||||
#cgo LDFLAGS: -L libs
|
||||
#cgo windows,amd64 LDFLAGS: -l assimp_windows_amd64
|
||||
#cgo darwin,arm64 LDFLAGS: -l assimp_darwin_arm64
|
||||
#cgo linux CFLAGS:
|
||||
#cgo windows,amd64 CFLAGS: -I .
|
||||
#cgo darwin,amd64 CFLAGS: -I .
|
||||
#cgo darwin,arm64 CFLAGS: -I .
|
||||
|
||||
#include "wrap.c"
|
||||
#include <stdlib.h>
|
||||
#cgo linux LDFLAGS: -lassimp
|
||||
#cgo windows,amd64 LDFLAGS: -L libs -l assimp_windows_amd64
|
||||
#cgo darwin,amd64 LDFLAGS: -L libs -l assimp_darwin_amd64
|
||||
#cgo darwin,arm64 LDFLAGS: -L libs -l assimp_darwin_arm64
|
||||
|
||||
#include "wrap.h"
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
|
||||
53
asig/wrap.c
53
asig/wrap.c
@ -1,3 +1,50 @@
|
||||
#include <assimp/cimport.h> // Plain-C interface
|
||||
#include <assimp/scene.h> // Output data structure
|
||||
#include <assimp/postprocess.h>
|
||||
#include "wrap.h"
|
||||
#include <stdio.h>
|
||||
|
||||
extern C_STRUCT aiFile *go_aiFileOpenProc(C_STRUCT aiFileIO *io, char *name, char *flags);
|
||||
C_STRUCT aiFile *cgo_aiFileOpenProc(C_STRUCT aiFileIO *io, const char *name, const char *flags)
|
||||
{
|
||||
return go_aiFileOpenProc(io, (char *)name, (char *)flags);
|
||||
}
|
||||
|
||||
extern void go_aiFileCloseProc(C_STRUCT aiFileIO *io, C_STRUCT aiFile *file);
|
||||
void cgo_aiFileCloseProc(C_STRUCT aiFileIO *io, C_STRUCT aiFile *file)
|
||||
{
|
||||
go_aiFileCloseProc(io, file);
|
||||
}
|
||||
|
||||
extern size_t go_aiFileWriteProc(C_STRUCT aiFile *file, const char *buffer, size_t size, size_t count);
|
||||
size_t cgo_aiFileWriteProc(C_STRUCT aiFile *file, const char *buffer, size_t size, size_t count)
|
||||
{
|
||||
return go_aiFileWriteProc(file, (char *)buffer, size, count);
|
||||
}
|
||||
|
||||
extern size_t go_aiFileReadProc(C_STRUCT aiFile *, char *, size_t, size_t);
|
||||
size_t cgo_aiFileReadProc(C_STRUCT aiFile *file, char *buffer, size_t size, size_t count)
|
||||
{
|
||||
return go_aiFileReadProc(file, buffer, size, count);
|
||||
}
|
||||
|
||||
extern size_t go_aiFileTellProc(C_STRUCT aiFile *file);
|
||||
size_t cgo_aiFileTellProc(C_STRUCT aiFile *file)
|
||||
{
|
||||
return go_aiFileTellProc(file);
|
||||
}
|
||||
|
||||
extern size_t go_aiFileFileSizeProc(C_STRUCT aiFile *file);
|
||||
size_t cgo_aiFileFileSizeProc(C_STRUCT aiFile *file)
|
||||
{
|
||||
return go_aiFileFileSizeProc(file);
|
||||
}
|
||||
|
||||
extern void go_aiFileFlushProc(C_STRUCT aiFile *file);
|
||||
void cgo_aiFileFlushProc(C_STRUCT aiFile *file)
|
||||
{
|
||||
go_aiFileFlushProc(file);
|
||||
}
|
||||
|
||||
extern C_ENUM aiReturn go_aiFileSeekProc(C_STRUCT aiFile *file, size_t offset, C_ENUM aiOrigin origin);
|
||||
C_ENUM aiReturn cgo_aiFileSeekProc(C_STRUCT aiFile *file, size_t offset, C_ENUM aiOrigin origin)
|
||||
{
|
||||
return go_aiFileSeekProc(file, offset, origin);
|
||||
}
|
||||
|
||||
20
asig/wrap.h
Normal file
20
asig/wrap.h
Normal file
@ -0,0 +1,20 @@
|
||||
#ifndef WRAP_H
|
||||
#define WRAP_H
|
||||
|
||||
#include <assimp/cimport.h> // Plain-C interface
|
||||
#include <assimp/cfileio.h> // Plain-C interface
|
||||
#include <assimp/scene.h> // Output data structure
|
||||
#include <assimp/postprocess.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
C_STRUCT aiFile *cgo_aiFileOpenProc(C_STRUCT aiFileIO *io, const char *name, const char *flags);
|
||||
void cgo_aiFileCloseProc(C_STRUCT aiFileIO *io, C_STRUCT aiFile *file);
|
||||
|
||||
size_t cgo_aiFileWriteProc(C_STRUCT aiFile *, const char *, size_t, size_t);
|
||||
size_t cgo_aiFileReadProc(C_STRUCT aiFile *, char *, size_t, size_t);
|
||||
size_t cgo_aiFileTellProc(C_STRUCT aiFile *);
|
||||
size_t cgo_aiFileFileSizeProc(C_STRUCT aiFile *);
|
||||
void cgo_aiFileFlushProc(C_STRUCT aiFile *);
|
||||
C_ENUM aiReturn cgo_aiFileSeekProc(C_STRUCT aiFile *, size_t, C_ENUM aiOrigin);
|
||||
|
||||
#endif
|
||||
5
main.go
5
main.go
@ -4,13 +4,16 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"image/png"
|
||||
"os"
|
||||
|
||||
"github.com/bloeys/assimp-go/asig"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
scene, release, err := asig.ImportFile("obj.obj", asig.PostProcessTriangulate|asig.PostProcessJoinIdenticalVertices)
|
||||
//scene, release, err := asig.ImportFile("obj.obj", asig.PostProcessTriangulate|asig.PostProcessJoinIdenticalVertices)
|
||||
fsys := os.DirFS(".")
|
||||
scene, release, err := asig.ImportFileEx("obj.obj", asig.PostProcessTriangulate|asig.PostProcessJoinIdenticalVertices, fsys)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user