Ok not totally sure if this is a bug but it seems so.. anyway here is the code i'm struggling with..
import "assimplib3.1/assimp/assimp"
import "opengl-1.0/opengl"
var
data: seq[aiVector3D]
iterator mesh(s:ptr aiScene): ptr aiMesh =
for i in 0 .. s[].mNumMeshes.int -1:
yield s[].mMeshes[i]
iterator verts(s:ptr aiMesh): seq[aiVector3D] =
for i in 0 .. s.mNumVertices.int -1:
yield s.mVertices[]
proc loadTextMdl*(name: string) =
var s = aiImportFile(name, aiImporterFlags_Support_Text_Flavour)
echo(s.mMeshes[0].mNumVertices)
echo(aiGetErrorString())
for i in s.mesh:
for m in i.verts:
data.add(m)
aiReleaseImport(s)
and on the binding side...
type
aiScene* = object
mFlags*: cuint
mRootNode*: ptr aiNode
mNumMeshes*: cuint
mMeshes*: ptr ptr seq[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 seq[aiVector3D]
mNormals*: ptr seq[aiVector3D]
mTangents*: ptr seq[aiVector3D]
mBitTangents*: ptr seq[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 seq[aiFace]
nNumBones*: cuint
mBones*: ptr ptr aiBone
mMaterialIndex*: cuint
mName*: aiString
#mNumAnimMeshes*: cuint # not in use
#mAnimMeshs*: ptr ptr aiAnimMesh # not in use
the aiScene object points to the aiMesh object in a sequence but when i try this line...
echo(s.mMeshes[0].mNumVertices)
i believe i get the capacity of the binary type used... "3212836864" so since its a cuint i think i'm getting this error message because of that, i'm not really sure...
Hint: operation successful (29368 lines compiled; 2.266 sec total; 63.645MB; Debug Build) [SuccessX]
CC: model_loader
> Process terminated with exit code 0
3212836864
Traceback (most recent call last)
main.nim(68) main
main.nim(50) main
main.nim(14) init
vbo.nim(11) gen_vbo
model_loader.nim(13) loadTextMdl
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
> Process terminated with exit code 1
but when i change this line to this i get zero vertices like i expect..
echo(s.mMeshes[2].mNumVertices)
but still i'm not seeing the data i would expect but if it is like below.
echo(s.mMeshes[0].mNumVertices)
anyway help is appreciated if its not a bug.this
C_STRUCT aiMesh** mMeshes;
C_STRUCT aiVector3D* mVertices;
should become:
mMeshes*: ptr ptr aiMesh
mVertices*: ptr aiVector3D
now guess, what should this become?
C_STRUCT aiColor4D* mColors[AI_MAX_NUMBER_OF_COLOR_SETS];
bump...
never figured out how to make the binding work correctly still have no clue how to handle ptr to ptr, to handle the subscript.
the reason i had so many ptr to ptr things was i was shotgunning it and admittedly did'nt know how it worked so if anyone can clarify this is would be great.
anyway here my updated code for assimp...
type
aiScene* = object
mFlags*: cuint
mRootNode*: ptr 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[0 .. AI_MAX_NUMBER_COLOR_SETS,aiColor4D]
mTextureCoords*: ptr array[0 .. AI_MAX_NUMBER_OF_TEXTURECOORDS,aiVector3D]
mNumUVComponents*: array[0 .. AI_MAX_NUMBER_OF_TEXTURECOORDS,int]
mFaces*: ptr aiFace
nNumBones*: cuint
mBones*: ptr ptr aiBone
mMaterialIndex*: cuint
mName*: aiString
when i try to access the value in the aiScene ptr ptr to aiMesh i get correct values if i don't use seq[] but if i don't use seq[] i get no subscript operator and only the first aiVector3D object in aiMesh.
proc loadTextMdl*(name: string): seq[cfloat] =
var s = aiImportFile(name, aiImporterFlags_Support_Text_Flavour)
echo(s.mMeshes.mNumVertices) # works
for i in 0 .. s.mNumMeshes.int: # works
for v in 0 .. s.mMeshes.mNumVertices.int: # works
var d = s.mMeshes[i].mVertices[v] # doesnt work
result.add(d.x)
result.add(d.y)
result.add(d.z)
aiReleaseImport(s)
so i do have to overload the subscript operator?? remember this?
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
yes you have to overload the [] for ptr.
ptr, pointer, and their relatives are 'unsafe' stuff which the programmer(you) must know exactly their usage.
The need to write your own ptr helper is a means of compiler developer to warn you about their potential danger. pointer sometimes can be powerful and dangerous too.
power comes with responsibility?
for i in 0 .. s.mNumMeshes.int: # works #be careful, index out of bounds
for v in 0 .. s.mMeshes.mNumVertices.int: # works #be careful, index out of bounds
consider to use iterator too, they'll greatly improve code readability without performance penalty because iterators will be inlined by the compiler