mirror of
https://github.com/bloeys/assimp-go.git
synced 2025-12-29 08:28:20 +00:00
447 lines
10 KiB
Go
Executable File
447 lines
10 KiB
Go
Executable File
package asig
|
|
|
|
/*
|
|
#cgo CFLAGS: -I .
|
|
#cgo LDFLAGS: -L ./libs -l assimp_windows_amd64 -l IrrXML_windows_amd64 -l zlib_windows_amd64
|
|
|
|
#include <stdlib.h> //Needed for C.free
|
|
|
|
#include <assimp/scene.h>
|
|
|
|
//Functions
|
|
struct aiScene* aiImportFile(const char* pFile, unsigned int pFlags);
|
|
void aiReleaseImport(const struct aiScene* pScene);
|
|
const char* aiGetErrorString();
|
|
unsigned int aiGetMaterialTextureCount(const struct aiMaterial* pMat, enum aiTextureType type);
|
|
*/
|
|
import "C"
|
|
import (
|
|
"errors"
|
|
"unsafe"
|
|
|
|
"github.com/bloeys/gglm/gglm"
|
|
)
|
|
|
|
type Node struct {
|
|
}
|
|
|
|
type Animation struct {
|
|
}
|
|
|
|
type Texel struct {
|
|
R, G, B, A byte
|
|
}
|
|
|
|
type Texture struct {
|
|
/** Width of the texture, in pixels
|
|
*
|
|
* If mHeight is zero the texture is compressed in a format
|
|
* like JPEG. In this case mWidth specifies the size of the
|
|
* memory area pcData is pointing to, in bytes.
|
|
*/
|
|
Width uint
|
|
|
|
/** Height of the texture, in pixels
|
|
*
|
|
* If this value is zero, pcData points to an compressed texture
|
|
* in any format (e.g. JPEG).
|
|
*/
|
|
Height uint
|
|
|
|
/** A hint from the loader to make it easier for applications
|
|
* to determine the type of embedded textures.
|
|
*
|
|
* If mHeight != 0 this member is show how data is packed. Hint will consist of
|
|
* two parts: channel order and channel bitness (count of the bits for every
|
|
* color channel). For simple parsing by the viewer it's better to not omit
|
|
* absent color channel and just use 0 for bitness. For example:
|
|
* 1. Image contain RGBA and 8 bit per channel, achFormatHint == "rgba8888";
|
|
* 2. Image contain ARGB and 8 bit per channel, achFormatHint == "argb8888";
|
|
* 3. Image contain RGB and 5 bit for R and B channels and 6 bit for G channel, achFormatHint == "rgba5650";
|
|
* 4. One color image with B channel and 1 bit for it, achFormatHint == "rgba0010";
|
|
* If mHeight == 0 then achFormatHint is set set to '\\0\\0\\0\\0' if the loader has no additional
|
|
* information about the texture file format used OR the
|
|
* file extension of the format without a trailing dot. If there
|
|
* are multiple file extensions for a format, the shortest
|
|
* extension is chosen (JPEG maps to 'jpg', not to 'jpeg').
|
|
* E.g. 'dds\\0', 'pcx\\0', 'jpg\\0'. All characters are lower-case.
|
|
* The fourth character will always be '\\0'.
|
|
*/
|
|
FormatHint string
|
|
|
|
/** Data of the texture.
|
|
*
|
|
* Points to an array of mWidth * mHeight aiTexel's.
|
|
* The format of the texture data is always ARGB8888 to
|
|
* make the implementation for user of the library as easy
|
|
* as possible. If mHeight = 0 this is a pointer to a memory
|
|
* buffer of size mWidth containing the compressed texture
|
|
* data. Good luck, have fun!
|
|
*/
|
|
Texels []Texel
|
|
|
|
Filename string
|
|
}
|
|
|
|
type Light struct {
|
|
}
|
|
|
|
type Camera struct {
|
|
}
|
|
|
|
type Metadata struct {
|
|
}
|
|
|
|
type Scene struct {
|
|
cScene *C.struct_aiScene
|
|
Flags SceneFlag
|
|
|
|
RootNode *Node
|
|
Meshes []*Mesh
|
|
Materials []*Material
|
|
Animations []*Animation
|
|
Textures []*Texture
|
|
Lights []*Light
|
|
Cameras []*Camera
|
|
}
|
|
|
|
func (s *Scene) releaseCResources() {
|
|
C.aiReleaseImport(s.cScene)
|
|
}
|
|
|
|
//
|
|
// Assimp API
|
|
//
|
|
|
|
func ImportFile(file string, postProcessFlags PostProcess) (s *Scene, release func(), err error) {
|
|
|
|
cstr := C.CString(file)
|
|
defer C.free(unsafe.Pointer(cstr))
|
|
|
|
cs := C.aiImportFile(cstr, C.uint(postProcessFlags))
|
|
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()))
|
|
}
|
|
|
|
//
|
|
// Parsers
|
|
//
|
|
|
|
func parseScene(cs *C.struct_aiScene) *Scene {
|
|
|
|
s := &Scene{cScene: cs}
|
|
s.Flags = SceneFlag(cs.mFlags)
|
|
s.Meshes = parseMeshes(cs.mMeshes, uint(cs.mNumMeshes))
|
|
s.Materials = parseMaterials(cs.mMaterials, uint(cs.mNumMaterials))
|
|
|
|
return s
|
|
}
|
|
|
|
func parseMeshes(cm **C.struct_aiMesh, count uint) []*Mesh {
|
|
|
|
if cm == nil {
|
|
return []*Mesh{}
|
|
}
|
|
|
|
meshes := make([]*Mesh, count)
|
|
cmeshes := unsafe.Slice(cm, count)
|
|
|
|
for i := 0; i < int(count); i++ {
|
|
|
|
m := &Mesh{}
|
|
|
|
cmesh := cmeshes[i]
|
|
vertCount := uint(cmesh.mNumVertices)
|
|
|
|
m.Vertices = parseVec3s(cmesh.mVertices, vertCount)
|
|
m.Normals = parseVec3s(cmesh.mNormals, vertCount)
|
|
m.Tangents = parseVec3s(cmesh.mTangents, vertCount)
|
|
m.BitTangents = parseVec3s(cmesh.mBitangents, vertCount)
|
|
|
|
//Color sets
|
|
m.ColorSets = parseColorSet(cmesh.mColors, vertCount)
|
|
|
|
//Tex coords
|
|
m.TexCoords = parseTexCoords(cmesh.mTextureCoords, vertCount)
|
|
m.TexCoordChannelCount = [8]uint{}
|
|
for j := 0; j < len(cmesh.mTextureCoords); j++ {
|
|
|
|
//If a color set isn't available then it is nil
|
|
if cmesh.mTextureCoords[j] == nil {
|
|
continue
|
|
}
|
|
|
|
m.TexCoordChannelCount[j] = uint(cmeshes[j].mNumUVComponents[j])
|
|
}
|
|
|
|
//Faces
|
|
cFaces := unsafe.Slice(cmesh.mFaces, cmesh.mNumFaces)
|
|
m.Faces = make([]Face, cmesh.mNumFaces)
|
|
for j := 0; j < len(m.Faces); j++ {
|
|
|
|
m.Faces[j] = Face{
|
|
Indices: parseUInts(cFaces[j].mIndices, uint(cFaces[j].mNumIndices)),
|
|
}
|
|
}
|
|
|
|
//Other
|
|
m.Bones = parseBones(cmesh.mBones, uint(cmesh.mNumBones))
|
|
m.AnimMeshes = parseAnimMeshes(cmesh.mAnimMeshes, uint(cmesh.mNumAnimMeshes))
|
|
m.AABB = AABB{
|
|
Min: parseVec3(&cmesh.mAABB.mMin),
|
|
Max: parseVec3(&cmesh.mAABB.mMax),
|
|
}
|
|
|
|
m.MorphMethod = MorphMethod(cmesh.mMethod)
|
|
m.MaterialIndex = uint(cmesh.mMaterialIndex)
|
|
m.Name = parseAiString(cmesh.mName)
|
|
|
|
meshes[i] = m
|
|
}
|
|
|
|
return meshes
|
|
}
|
|
|
|
func parseVec3(cv *C.struct_aiVector3D) gglm.Vec3 {
|
|
|
|
if cv == nil {
|
|
return gglm.Vec3{}
|
|
}
|
|
|
|
return gglm.Vec3{
|
|
Data: [3]float32{
|
|
float32(cv.x),
|
|
float32(cv.y),
|
|
float32(cv.z),
|
|
},
|
|
}
|
|
}
|
|
|
|
func parseAnimMeshes(cam **C.struct_aiAnimMesh, count uint) []*AnimMesh {
|
|
|
|
if cam == nil {
|
|
return []*AnimMesh{}
|
|
}
|
|
|
|
animMeshes := make([]*AnimMesh, count)
|
|
cAnimMeshes := unsafe.Slice(cam, count)
|
|
|
|
for i := 0; i < int(count); i++ {
|
|
|
|
m := cAnimMeshes[i]
|
|
animMeshes[i] = &AnimMesh{
|
|
Name: parseAiString(m.mName),
|
|
Vertices: parseVec3s(m.mVertices, uint(m.mNumVertices)),
|
|
Normals: parseVec3s(m.mNormals, uint(m.mNumVertices)),
|
|
Tangents: parseVec3s(m.mTangents, uint(m.mNumVertices)),
|
|
BitTangents: parseVec3s(m.mBitangents, uint(m.mNumVertices)),
|
|
Colors: parseColorSet(m.mColors, uint(m.mNumVertices)),
|
|
TexCoords: parseTexCoords(m.mTextureCoords, uint(m.mNumVertices)),
|
|
Weight: float32(m.mWeight),
|
|
}
|
|
}
|
|
|
|
return animMeshes
|
|
}
|
|
|
|
func parseTexCoords(ctc [MaxTexCoords]*C.struct_aiVector3D, vertCount uint) [MaxTexCoords][]gglm.Vec3 {
|
|
|
|
texCoords := [MaxTexCoords][]gglm.Vec3{}
|
|
|
|
for j := 0; j < len(ctc); j++ {
|
|
|
|
//If a color set isn't available then it is nil
|
|
if ctc[j] == nil {
|
|
continue
|
|
}
|
|
|
|
texCoords[j] = parseVec3s(ctc[j], vertCount)
|
|
}
|
|
|
|
return texCoords
|
|
}
|
|
|
|
func parseColorSet(cc [MaxColorSets]*C.struct_aiColor4D, vertCount uint) [MaxColorSets][]gglm.Vec4 {
|
|
|
|
colorSet := [MaxColorSets][]gglm.Vec4{}
|
|
for j := 0; j < len(cc); j++ {
|
|
|
|
//If a color set isn't available then it is nil
|
|
if cc[j] == nil {
|
|
continue
|
|
}
|
|
|
|
colorSet[j] = parseColors(cc[j], vertCount)
|
|
}
|
|
|
|
return colorSet
|
|
}
|
|
|
|
func parseBones(cbs **C.struct_aiBone, count uint) []*Bone {
|
|
|
|
if cbs == nil {
|
|
return []*Bone{}
|
|
}
|
|
|
|
bones := make([]*Bone, count)
|
|
cbones := unsafe.Slice(cbs, count)
|
|
|
|
for i := 0; i < int(count); i++ {
|
|
|
|
cBone := cbones[i]
|
|
bones[i] = &Bone{
|
|
Name: parseAiString(cBone.mName),
|
|
Weights: parseVertexWeights(cBone.mWeights, uint(cBone.mNumWeights)),
|
|
OffsetMatrix: parseMat4(&cBone.mOffsetMatrix),
|
|
}
|
|
}
|
|
|
|
return bones
|
|
}
|
|
|
|
func parseMat4(cm4 *C.struct_aiMatrix4x4) gglm.Mat4 {
|
|
|
|
if cm4 == nil {
|
|
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)},
|
|
{float32(cm4.a3), float32(cm4.b3), float32(cm4.c3), float32(cm4.d3)},
|
|
{float32(cm4.a4), float32(cm4.b4), float32(cm4.c4), float32(cm4.d4)},
|
|
},
|
|
}
|
|
}
|
|
|
|
func parseVertexWeights(cWeights *C.struct_aiVertexWeight, count uint) []VertexWeight {
|
|
|
|
if cWeights == nil {
|
|
return []VertexWeight{}
|
|
}
|
|
|
|
vw := make([]VertexWeight, count)
|
|
cvw := unsafe.Slice(cWeights, count)
|
|
|
|
for i := 0; i < int(count); i++ {
|
|
|
|
vw[i] = VertexWeight{
|
|
VertIndex: uint(cvw[i].mVertexId),
|
|
Weight: float32(cvw[i].mWeight),
|
|
}
|
|
}
|
|
|
|
return vw
|
|
}
|
|
|
|
func parseAiString(aiString C.struct_aiString) string {
|
|
return C.GoStringN(&aiString.data[0], C.int(aiString.length))
|
|
}
|
|
|
|
func parseUInts(cui *C.uint, count uint) []uint {
|
|
|
|
if cui == nil {
|
|
return []uint{}
|
|
}
|
|
|
|
uints := make([]uint, count)
|
|
cUInts := unsafe.Slice(cui, count)
|
|
for i := 0; i < len(cUInts); i++ {
|
|
uints[i] = uint(cUInts[i])
|
|
}
|
|
|
|
return uints
|
|
}
|
|
|
|
func parseVec3s(cv *C.struct_aiVector3D, count uint) []gglm.Vec3 {
|
|
|
|
if cv == nil {
|
|
return []gglm.Vec3{}
|
|
}
|
|
|
|
carr := unsafe.Slice(cv, count)
|
|
verts := make([]gglm.Vec3, count)
|
|
|
|
for i := 0; i < int(count); i++ {
|
|
verts[i] = gglm.Vec3{
|
|
Data: [3]float32{
|
|
float32(carr[i].x),
|
|
float32(carr[i].y),
|
|
float32(carr[i].z),
|
|
},
|
|
}
|
|
}
|
|
|
|
return verts
|
|
}
|
|
|
|
func parseColors(cv *C.struct_aiColor4D, count uint) []gglm.Vec4 {
|
|
|
|
if cv == nil {
|
|
return []gglm.Vec4{}
|
|
}
|
|
|
|
carr := unsafe.Slice(cv, count)
|
|
verts := make([]gglm.Vec4, count)
|
|
|
|
for i := 0; i < int(count); i++ {
|
|
verts[i] = gglm.Vec4{
|
|
Data: [4]float32{
|
|
float32(carr[i].r),
|
|
float32(carr[i].g),
|
|
float32(carr[i].b),
|
|
float32(carr[i].a),
|
|
},
|
|
}
|
|
}
|
|
|
|
return verts
|
|
}
|
|
|
|
func parseMaterials(cMatsIn **C.struct_aiMaterial, count uint) []*Material {
|
|
|
|
mats := make([]*Material, count)
|
|
cMats := unsafe.Slice(cMatsIn, count)
|
|
|
|
for i := 0; i < int(count); i++ {
|
|
|
|
mats[i] = &Material{
|
|
cMat: cMats[i],
|
|
Properties: parseMatProperties(cMats[i].mProperties, uint(cMats[i].mNumProperties)),
|
|
AllocatedStorage: uint(cMats[i].mNumAllocated),
|
|
}
|
|
}
|
|
|
|
return mats
|
|
}
|
|
|
|
func parseMatProperties(cMatPropsIn **C.struct_aiMaterialProperty, count uint) []*MaterialProperty {
|
|
|
|
matProps := make([]*MaterialProperty, count)
|
|
cMatProps := unsafe.Slice(cMatPropsIn, count)
|
|
|
|
for i := 0; i < int(count); i++ {
|
|
|
|
cmp := cMatProps[i]
|
|
|
|
matProps[i] = &MaterialProperty{
|
|
name: parseAiString(cmp.mKey),
|
|
Semantic: TextureType(cmp.mSemantic),
|
|
Index: uint(cmp.mIndex),
|
|
TypeInfo: MatPropertyTypeInfo(cmp.mType),
|
|
Data: C.GoBytes(unsafe.Pointer(cmp.mData), C.int(cmp.mDataLength)),
|
|
}
|
|
}
|
|
|
|
return matProps
|
|
}
|