assimp.3.1.1 3D model and scene im/exporter library (32/64-bit).

Headers, Bindings, Libraries for use with FreeBASIC, Please include example of use to help ensure they are tested and usable.
Post Reply
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

assimp.3.1.1 3D model and scene im/exporter library (32/64-bit).

Post by D.J.Peters »

Open Asset Import Library

For advanced users only assimp..
You have to learn the C API self.

Some importers are complete and stable some are not stable nor complete..

Supported 3d model / scene / game and motion capture formats:
Collada ( .dae )
Blender 3D ( .blend )
3ds Max 3DS ( .3ds )
3ds Max ASE ( .ase )
Wavefront Object ( .obj )
Industry Foundation Classes (IFC/Step) ( .ifc )
XGL ( .xgl,.zgl )
Stanford Polygon Library ( .ply )
AutoCAD DXF ( .dxf )
LightWave ( .lwo )
LightWave Scene ( .lws )
Modo ( .lxo )
Stereolithography ( .stl )
DirectX X ( .x )
AC3D ( .ac )
Milkshape 3D ( .ms3d )
TrueSpace ( .cob,.scn )
Biovision BVH ( .bvh )
CharacterStudio Motion ( .csm )
Ogre XML ( .xml )
Irrlicht Mesh ( .irrmesh )
Irrlicht Scene ( .irr )
Quake I ( .mdl )
Quake II ( .md2 )
Quake III Mesh ( .md3 )
Quake III Map/BSP ( .pk3 )
Return to Castle Wolfenstein ( .mdc )
Doom 3 ( .md5* )
Valve Model ( .smd,.vta )
Starcraft II M3 ( .m3 )
Unreal ( .3d )
BlitzBasic 3D ( .b3d )
Quick3D ( .q3d,.q3s )
Neutral File Format ( .nff )
Sense8 WorldToolKit ( .nff )
Object File Format ( .off )
PovRAY Raw ( .raw )
Terragen Terrain ( .ter )
3D GameStudio (3DGS) ( .mdl )
3D GameStudio (3DGS) Terrain ( .hmp )
Izware Nendo ( .ndo )

Windows and Linux (32/64-bit).
download: assimp-3.1.1.zip
Source code with Code::Blocks project file.
download: assimp-3.1.1-src.zip

Joshy

file: test01.bas

Code: Select all

#include "inc/assimp-c.bi"

'const as string file = "media/test_cube_compressed.x"
'const as string file = "media/jeep1.ms3d"
'const as string file = "media/test.obj"
const as string file = "media/shadow.obj"
'const as string file = "media/test.3ds"
'const as string file = "media/test.dae"


sub processMesh(byval mesh as aiMesh ptr,byval scene as const aiScene ptr)
  dim as aiColor4D col
  if mesh->mName.length>0 then
    print "Mesh name: " & mesh->mName.data
    print
  end if  

  
  dim as aiMaterial ptr mat=scene->mMaterials[mesh->mMaterialIndex]
  if mat then
    if aiGetMaterialColor(mat,AI_MATKEY_COLOR_DIFFUSE,@col)=aiReturn_SUCCESS then
      print "material diffuse :" & col.r & "," & col.g & "," & col.b & "," & col.a
    end if  
    if aiGetMaterialColor(mat,AI_MATKEY_COLOR_AMBIENT,@col)=aiReturn_SUCCESS then
      print "material ambient  :" & col.r & "," & col.g & "," & col.b & "," & col.a
    end if
    if aiGetMaterialColor(mat,AI_MATKEY_COLOR_SPECULAR,@col)=aiReturn_SUCCESS then
      print "material specular :" & col.r & "," & col.g & "," & col.b & "," & col.a
    end if
    print
  end if
  
  if mesh->mNumVertices then
    print "mNumVertices: " & mesh->mNumVertices
    for i as long=0 to mesh->mNumVertices-1
      if i<10 then print "position: [" & i & "] " & mesh->mVertices[i].x & "," & mesh->mVertices[i].y & "," & mesh->mVertices[i].y
      if (mesh->mNormals) then
        if i<10 then print "normal  : [" & i & "] " & mesh->mNormals[i].x  & "," & mesh->mNormals[i].y  & "," & mesh->mNormals[i].y
      end if
      if (mesh->mTangents) then
        if i<10 then print "tangent : [" & i & "] " & mesh->mTangents[i].x  & "," & mesh->mTangents[i].y  & "," & mesh->mTangents[i].z
      end if
      if (mesh->mBitangents) then
        if i<10 then print "bitangent [" & i & "] " & mesh->mBitangents[i].x  & "," & mesh->mBitangents[i].y  & "," & mesh->mBitangents[i].z
      end if
      
      
      for c as long = 0 to AI_MAX_NUMBER_OF_COLOR_SETS-1
         dim as aiColor4D ptr col=mesh->mColors(c)
         if col=0 then exit for
         if i<10 then print "color   : [" & i & "] " & col[i].r & "," & col[i].g & "," & col[i].b & "," & col[i].a
      next
      for t as long=0 to AI_MAX_NUMBER_OF_TEXTURECOORDS-1
         dim as aiVector3D ptr v=mesh->mTextureCoords(t)
         if v=0 then exit for
         if i<10 then print "texture : [" & i & "] " & v[i].x & "," & v[i].y
      next
    next
    if mesh->mNumVertices>9 then  print "<CUT>"
    print
  end if

  if mesh->mNumFaces then
    print "mNumFaces: " & mesh->mNumFaces
    for i as long=0 to mesh->mNumFaces-1
      if i<10 then 
        dim as aiFace face=mesh->mFaces[i]
        print "face[" & i & "].mNumIndices: " & face.mNumIndices & "  < ";
        for j as long = 0 to face.mNumIndices-1
          print face.mIndices[j] & " ";
        next
        print ">"
      end if  
    next
    if mesh->mNumFaces>9 then  print "<CUT>"
    print
  end if

  for typ as long=0 to AI_TEXTURE_TYPE_MAX-1
    dim as long tc = aiGetMaterialTextureCount(mat,typ) ' aiTextureType_DIFFUSE)
    if tc>0 then
      print "texturecount: " & tc
      for i as long = 0 to tc-1
        dim as aiString path
        if aiGetMaterialTexture(mat,aiTextureType_DIFFUSE,i,@path)=aiReturn_SUCCESS then
          print "loadTexture('" & left(path.data,path.length) & "')"
        end if
      next
      print
    end if
  next  
  print
end sub

dim shared as long nMeshes,nChilds

sub recursiveProcess(byval node as aiNode ptr,byval scene as const aiScene ptr)
  if node->mNumMeshes>0 then
    print "Transformation:"
    print node->mTransformation
    for i as long=0 to node->mNumMeshes-1
      dim as aiMesh ptr mesh=scene->mMeshes[node->mMeshes[i]]
      processMesh(mesh,scene)
    next
  end if
  if node->mNumChildren>0 then
    for child as long=0 to node->mNumChildren-1
      ' !!! recursion !!!
      recursiveProcess(node->mChildren[child],scene)
    next
  end if
end sub

chdir exepath

' optional
dim as aiLogStream stream
stream = aiGetPredefinedLogStream(aiDefaultLogStream_STDOUT,0)
aiAttachLogStream(@stream)

dim as const aiScene ptr scene = aiImportFile(file,aiProcessPreset_TargetRealtime_MaxQuality)
if scene=0 then
  print "error: " & *aiGetErrorString()
  beep : sleep : end
end if

nMeshes=0
nChilds=0
recursiveProcess(scene->mRootNode,scene)
sleep
Last edited by D.J.Peters on Oct 12, 2022 18:23, edited 4 times in total.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: assimp-c 3d model and scene im/exporter library.

Post by D.J.Peters »

file: "cmaterial.bi" is complete now.

Joshy
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: assimp-c 3d model and scene im/exporter library.

Post by D.J.Peters »

How you process a aiScene recursively to get vertex data, face indices and material properties from the aiNode tree.

Joshy

The 3d model "jeep1.ms3" for testing is in the download.

Code: Select all

#include "inc/assimp-c.bi"

'const as string file = "media/test_cube_compressed.x"
const as string file = "media/jeep1.ms3d"

sub processMesh(mesh as aiMesh ptr,scene as const aiScene ptr)
  dim as aiColor4D col
  if mesh->mName.length>0 then
    print "Mesh name: " & mesh->mName.data
  end if

  
  dim as aiMaterial ptr mat=scene->mMaterials[mesh->mMaterialIndex]
  if mat then
    aiGetMaterialColor(mat,AI_MATKEY_COLOR_DIFFUSE,@col)
    print "material diffuse :" & col.r & "," & col.g & "," & col.b
  end if
  
  if mesh->mNumVertices then
    print "mNumVertices: " & mesh->mNumVertices
    for i as integer=0 to mesh->mNumVertices-1
      print "position: [" & i & "] " & mesh->mVertices[i].x & "," & mesh->mVertices[i].y & "," & mesh->mVertices[i].y
      print "normal  : [" & i & "] " & mesh->mNormals[i].x  & "," & mesh->mNormals[i].y  & "," & mesh->mNormals[i].y
      if (mesh->mTangents) then
      print "tangent : [" & i & "] " & mesh->mTangents[i].x  & "," & mesh->mTangents[i].y  & "," & mesh->mTangents[i].z
      end if
      for c as integer = 0 to AI_MAX_NUMBER_OF_COLOR_SETS-1
         dim as aiColor4D ptr col=mesh->mColors(c)
         if col=0 then exit for
         print "color   : [" & i & "] " & col[i].r & "," & col[i].g & "," & col[i].b & "," & col[i].a
      next
      for t as integer=0 to AI_MAX_NUMBER_OF_TEXTURECOORDS-1
         dim as aiVector3D ptr v=mesh->mTextureCoords(t)
         if v=0 then exit for
         print "texture : [" & i & "] " & v[i].x & "," & v[i].y
      next
    next
  end if

  if mesh->mNumFaces then
    print "mNumFaces: " & mesh->mNumFaces
    for i as integer=0 to mesh->mNumFaces-1
      dim as aiFace face=mesh->mFaces[i]
      print "face[" & i & "].mNumIndices: " & face.mNumIndices & "  < ";
      for j as integer = 0 to face.mNumIndices-1
        print face.mIndices[j] & " ";
      next
      print ">"
    next
    print
  end if

  dim as integer tc = aiGetMaterialTextureCount(mat,aiTextureType_DIFFUSE)
  if tc then
     print "texturecount: " & tc
    for i as integer = 0 to tc-1
      dim as aiString path
      if aiGetMaterialTexture(mat,aiTextureType_DIFFUSE,i,@path)=aiReturn_SUCCESS then
        print "loadTexture('" & path.data & "')"
      end if
    next
    print
  end if
end sub

sub recursiveProcess(node as aiNode ptr,scene as const aiScene ptr)
  for i as integer=0 to node->mNumMeshes-1
    dim as aiMesh ptr mesh=scene->mMeshes[node->mMeshes[i]]
    processMesh(mesh,scene)
  next
  ' recursion
  for child as integer=0 to node->mNumChildren-1
    recursiveProcess(node->mChildren[child],scene)
  next
end sub

chdir exepath

' optional
dim as aiLogStream stream
stream = aiGetPredefinedLogStream(aiDefaultLogStream_STDOUT,0)
aiAttachLogStream(@stream)

dim as const aiScene ptr scene = aiImportFile(file,aiProcessPreset_TargetRealtime_MaxQuality)
if scene=0 then
  print "error: " & *aiGetErrorString()
  beep : sleep : end
end if

recursiveProcess(scene->mRootNode,scene)
sleep
angros47
Posts: 2323
Joined: Jun 21, 2005 19:04

Re: assimp-c 3d model and scene im/exporter library.

Post by angros47 »

You suggested me to use it with OpenB3D (I don't know why your post has been deleted), so I tried:

Code: Select all

'const as string file = "./test_cube_compressed.x"
'const as string file = "./duck.dae"

#include "inc/assimp-c.bi"

#include "openb3d.bi"

screen 18,  32, , &h10002 

	
Graphics3d 640,480,32,1,1

dim shared as any ptr loadedmesh

loadedmesh=createmesh


var camera=createcamera

moveentity camera,0,0,-20




const as string file = "./jeep1.ms3d"

sub processMesh(mesh as aiMesh ptr,scene as const aiScene ptr)
  dim as aiColor4D col
  if mesh->mName.length>0 then
    'print "Mesh name: " & mesh->mName.data
    NameEntity loadedmesh, mesh->mName.data
  end if

  
  dim as aiMaterial ptr mat=scene->mMaterials[mesh->mMaterialIndex]
  if mat then
    aiGetMaterialColor(mat,AI_MATKEY_COLOR_DIFFUSE,@col)
    'print "material diffuse :" & col.r & "," & col.g & "," & col.b
    'entitycolor loadedmesh, col.r, col.g, col.b
  end if
  
  var surf= createsurface(loadedmesh)
  if mesh->mNumVertices then
    'print "mNumVertices: " & mesh->mNumVertices
    for i as integer=0 to mesh->mNumVertices-1
      'print "position: [" & i & "] " & mesh->mVertices[i].x & "," & mesh->mVertices[i].y & "," & mesh->mVertices[i].y
      'print "normal  : [" & i & "] " & mesh->mNormals[i].x  & "," & mesh->mNormals[i].y  & "," & mesh->mNormals[i].y
      addvertex surf,mesh->mVertices[i].x,mesh->mVertices[i].y, mesh->mVertices[i].z

      if (mesh->mTangents) then
         'print "tangent : [" & i & "] " & mesh->mTangents[i].x  & "," & mesh->mTangents[i].y  & "," & mesh->mTangents[i].z
      end if
      for c as integer = 0 to AI_MAX_NUMBER_OF_COLOR_SETS-1
         dim as aiColor4D ptr col=mesh->mColors(c)
         if col=0 then exit for
         'print "color   : [" & i & "] " & col[i].r & "," & col[i].g & "," & col[i].b & "," & col[i].a
         Vertexcolor surf,i,col[i].r,col[i].g, col[i].b, col[i].a
      next
      for t as integer=0 to AI_MAX_NUMBER_OF_TEXTURECOORDS-1
         dim as aiVector3D ptr v=mesh->mTextureCoords(t)
         if v=0 then exit for
         'print "texture : [" & i & "] " & v[i].x & "," & v[i].y
         vertextexcoords surf,i,v[i].x, v[i].y
      next
    next
  end if

  if mesh->mNumFaces then
    print "mNumFaces: " & mesh->mNumFaces
    for i as integer=0 to mesh->mNumFaces-1
      dim as aiFace face=mesh->mFaces[i]
      'print "face[" & i & "].mNumIndices: " & face.mNumIndices & "  < ";
      'for j as integer = 0 to face.mNumIndices-1
      '  print face.mIndices[j] & " ";
      'next
      'print ">"
      addtriangle surf,face.mIndices[0],face.mIndices[1],face.mIndices[2]
    next
  end if

  dim as integer tc = aiGetMaterialTextureCount(mat,aiTextureType_DIFFUSE)
  if tc then
    var brush=createbrush
    'print "texturecount: " & tc
    for i as integer = 0 to tc-1
      dim as aiString path
      if aiGetMaterialTexture(mat,aiTextureType_DIFFUSE,i,@path)=aiReturn_SUCCESS then
        'print "loadTexture('" & path.data & "')"
        brushtexture brush,loadtexture(path.data)
      end if
    next
    paintsurface surf,brush
    print
  end if
end sub

sub recursiveProcess(node as aiNode ptr,scene as const aiScene ptr)
  for i as integer=0 to node->mNumMeshes-1
    dim as aiMesh ptr mesh=scene->mMeshes[node->mMeshes[i]]
    processMesh(mesh,scene)
  next
  ' recursion
  for child as integer=0 to node->mNumChildren-1
    recursiveProcess(node->mChildren[child],scene)
  next
end sub

chdir "media"'exepath

' optional
dim as aiLogStream stream
stream = aiGetPredefinedLogStream(aiDefaultLogStream_STDOUT,0)
aiAttachLogStream(@stream)

dim as const aiScene ptr scene = aiImportFile(file,aiProcessPreset_TargetRealtime_MaxQuality)
if scene=0 then
  print "error: " & *aiGetErrorString()
  beep : sleep : end
end if

recursiveProcess(scene->mRootNode,scene)


cameraclscolor camera,0,255,0
updatenormals loadedmesh



renderworld
flip

sleep
For some reason, OpenB3D doesn't like the texture "jeep1.jpg" (saved in a non-standard format?); just open it with GIMP and save it again, and it will work; also, the line "entitycolor loadedmesh" makes the mesh black, so I commented it (the color of the mesh should be used only if there are no textures, I suppose). Well, looks like you found a way to load a lot of graphic formats. Thank you!
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: assimp-c 3d model and scene im/exporter library.

Post by D.J.Peters »

Hello angros47,
I self never used the included texture I wrote only the recursive scene parser as an starting point for other FreeBASIC assimp users.

On youtube I saw assimp can load blender scenes with all it's complex material properties.

If you take a look in "cmaterial.bi" you can imagine how complex it can be.

Of course such complex blender materials works only with modern shaders not the fixed render pipeline.
(so far I know)

Joshy
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: assimp.3.1.1 3D model and scene im/exporter library (32/64-bit).

Post by D.J.Peters »

Moved to my server shiny3d.de added source code with Code::Block IDE project (Windows and Linux).

Joshy
jepalza
Posts: 149
Joined: Feb 24, 2010 10:08
Location: Spain (Bilbao)

Re: assimp.3.1.1 3D model and scene im/exporter library (32/64-bit).

Post by jepalza »

File "assimp-3.1.1.zip" is broken. Download is good, but file is corrupt.
Post Reply