import assimp/assimp
import "opengl-1.0/opengl"
proc `items`(a:cuint): cuint =
result = a
proc `[]`(a:ptr aiScene,b:cuint): aiScene =
result = a[b]
proc `[]`(a:ptr aiMesh,b:cuint): aiMesh =
result = a[b]
proc `/`(a,b:cuint): cuint =
result = a / b
proc getData*(name: cstring): seq[aiVector3D] =
var s: ptr aiScene = aiImportFile(name, aiImporterFlags_Support_Text_Flavour)
var num : cuint = s[0].mNumMeshes
for i in num:
result = @[s[0].mMeshes[i]]
result = @[s[0].mMeshes[i]]
result = @[s[0].mMeshes[i]]
aiReleaseImport(s)
Well there is my code but i am trying to make a binding of assimp from github. but so far i got most of it i think to work but i'm having trouble trying to access a ptr to ptr array object in nim.
in the for loop i get an error message i don't know what to do about. `` Error: could not resolve: > Process terminated with exit code 1 ``
any help appreciated and i want to to eventually release this to you guys for some 3d type game stuff...
edit: forgot some extra code on binding side.
type
aiScene* = object
mFlags*: cuint
mRootNode*: aiNode
mNumMeshes*: cuint
mMeshes*: ptr ptr aiMesh
mNumMaterials*: cuint
mMaterials*: ptr ptr aiMaterial
mNumAnimations*: cuint
mAnimations*: ptr ptr aiAnimation
mNumTextures*: cuint
mTextures*: ptr ptr aiTexture
mNumLights*: cuint
mLight*: ptr ptr aiLight
mNumCamera*: cuint
mCamera*: ptr ptr aiCamera
mPrivate*: cstring
type
aiMesh* = object
mPrimitiveTypes*: cuint
mNumVertices*: cuint
mNumFaces*: cuint
mVertices*: ptr aiVector3D
mNormals*: ptr aiVector3D
mTangents*: ptr aiVector3D
mBitTangents*: ptr aiVector3D
mColors*: ptr array[AI_MAX_NUMBER_COLOR_SETS,aiColor4D]
mTextureCoords*: ptr array[AI_MAX_NUMBER_OF_TEXTURECOORDS,aiVector3D]
mNumUVComponents*: array[AI_MAX_NUMBER_OF_TEXTURECOORDS,cuint]
mFaces*: ptr aiFace
nNumBones*: cuint
mBones*: ptr ptr aiBone
mMaterialIndex*: cuint
mName*: aiString
#mNumAnimMeshes*: cuint # not in use
#mAnimMeshs*: ptr ptr aiAnimMesh # not in use
proc aiImportFile*(pFile :cstring,pFlags :cuint): ptr aiScene {.cdecl, importc: "aiImportFile", dynlib: AssimpLib.}
proc aiReleaseImport*(pScene: ptr aiScene) {.cdecl, importc: "aiReleaseImport", dynlib: AssimpLib.}
proc `[]`(a:ptr aiScene,b:cuint): aiScene =
result = a[b]
If a is a pointer to an object, then a[b] is not valid syntax in Nim. That works when a is an array or a sequence.
well still strugling to find out what i'm doing wrong i did change the wrapper a bit to this in aiScene object
ptr seq[aiMesh]
and in aiMesh object seq[aiVector3D]
among other things but i get an even stranger message from compiler stll dunno what to do.
but here is the code i am using
import "assimplib3.1/assimp/assimp"
import "opengl-1.0/opengl"
proc `items`(a:cuint): cuint =
result = a
var verts*: cuint
var data: ptr seq[float]
proc loadData*(name: cstring) =
var s: ptr aiScene = aiImportFile(name, aiImporterFlags_Support_Binary_Flavour)
var mesh : cuint = s.mNumMeshes
echo(mesh)
for m in mesh.cuint:
verts = s.mMeshes[m.int].mNumVertices
echo(verts)
for i in verts.cuint:
data[i.int * 3.int] = s.mMeshes[m.int].mVertices[i.int].x
data[i.int * 3.int + 1.int] = s.mMeshes[m.int].mVertices[i.int].y
data[i.int * 3.int + 2.int] = s.mMeshes[m.int].mVertices[i.int].z
echo data[0]
aiReleaseImport(s)
proc getData*(): ptr seq[float] =
return data
and i get this error
c:\users\kendall\desktop\nim_project\nimcache\model_loader.c: In function 'loaddata_730029':
c:\users\kendall\desktop\nim_project\nimcache\model_loader.c:351:28: error: request for member 'ClEnv' in something not a structure or union
if ((((NI*) items_730007.ClEnv)[0]) < 0) break;
^
c:\users\kendall\desktop\nim_project\nimcache\model_loader.c:370:30: error: request for member 'ClEnv' in something not a structure or union
if ((((NI*) items_730007.ClEnv)[0]) < 0) break;
^
Hint: [Link]
gcc.exe: error: c:\users\kendall\desktop\nim_project\nimcache\model_loader.o: No such file or directory
these are some helper to manipulate ptr:
template `+`[T](p: ptr T, off: int): ptr T =
cast[ptr type(p[])](cast[ByteAddress](p) +% off * sizeof(p[]))
template `+=`[T](p: ptr T, off: int) =
p = p + off
template `-`[T](p: ptr T, off: int): ptr T =
cast[ptr type(p[])](cast[ByteAddress](p) -% off * sizeof(p[]))
template `-=`[T](p: ptr T, off: int) =
p = p - off
template `mod`[T](p: ptr T, divisor: int): ptr T =
cast[ptr type(p[])](cast[ByteAddress](p) %% off)
template `[]`[T](p: ptr T, off: int): T =
(p + off)[]
template `[]=`[T](p: ptr T, off: int, val: T) =
(p + off)[] = val
and you can try something more idiomatic Nim code like this:
iterator meshes(s: ptr aiScene): ptr aiMesh =
for i in 0..s[].mNumMeshes.int-1:
yield s[].mMeshes[i] #this line need the above template
iterator vertices(m: ptr aiMesh): aiVector3D =
for i in 0..m[].mNumVertices.int-1:
yield m[].mVertices[i] #this line too
proc loadData*(name: cstring): seq[float] =
var s = aiImportFile(name, aiImporterFlags_Support_Binary_Flavour)
result = @[] #initialize the seq[float] with zero elem
for m in s.meshes: #unroll the iterator
for v in m.vertices: #again unroll the iterator
result.add v.x
result.add v.y
result.add v.z
aiReleaseImport(s)
if you want to know vertices number:
var verts = loadData("???.???")
var numVertices = verts.len div 3
or you could also put the aiVector3D right into the seq, why put float when you can put aiVector3D?
EDIT, use string is OK too here, the compiler will do automatic adaptation when you call aiImportFile inside loadData:
proc loadData*(name: string): seq[aiVector3D] =
let s = aiImportFile(name, aiImporterFlags_Support_Binary_Flavour)
result = @[] #initialize the seq[aiVector3D] with zero elem
for m in s.meshes: #unroll the iterator
for v in m.vertices: #again unroll the iterator
result.add v
aiReleaseImport(s)
let
verts = loadData("???.???")
numVertices = verts.len