3ds Loader, help to resize arrays needed

BlitzMax Forums/BlitzMax Programming/3ds Loader, help to resize arrays needed

Skurcey(Posted 2005) [#1]
I have this code and i can t use multi dimensionnal arrays beacause there s an error during the compilation who say "Incorrect Number of array dimension"...
'Strict
load3ds ("obj/mesh.3ds")

Type object3ds
	Field vertex:Float[][]
	Field polygon:Int[][]
	Field mapCoord:Float[][]
	
	Method resizeVertex(quantity)
		vertex=vertex[..2]
		For Local l=0 To 2 
			vertex[l]=vertex[l][..quantity]
		Next
	End Method

	Method resizepolygon(quantity)
		polygon=polygon[..2]
		For Local l=0 To 2 
			polygon[l]=[l][..quantity]
		Next
	End Method
	Method resizemapCoord(quantity)
		mapCoord=mapCoord[..1]
		For Local l=0 To 1 
			mapCoord[l]=mapCoord[l][..quantity]
		Next
	End Method
		
End Type
'---------------------------------------------------------
Function load3ds (path:String)
'---------------------------------------------------------
	Local file=OpenFile(path)
	If file=0 Then Return 0
	
	o:object3ds=New object3ds
	
	Local chunk_id:String,chunk_length:String,quantity:Short
	Local b:Byte
		'-----------------------------------------------------------
		While Not Eof(file)
			chunk_id:String=Hex(ReadShort(file))
			chunk_length:String=Hex(Readint(file))
			'DebugLog chunk_id
			'DebugLog chunk_length
			
			Select chunk_id
				Case "00004D4D"'no data
			
				Case "00003D3D"'no data
				
				Case "00004000"'object's name
					While b<>0
						b=ReadByte(file)
						name:String=name+Chr(b)
					Wend
					
				Case "00004100"'empty too
				
				Case "00004110"'list od the vertices
					quantity=ReadShort(file)
					DebugLog "N vertices= "+quantity
					o.resizevertex(quantity)
					For l=1 To quantity
						For l2=0 To 2
							o.vertex[l2,l]=ReadShort(file)
						Next
					Next
					
				Case "00004120"'faces description
					quantity=ReadShort(file)
					DebugLog "N Faces= "+quantity
					o:object3ds=New object3ds
					o.resizepolygon(quantity)
					For l=1 To quantity
						For l2=0 To 2
							o.polygon[l2,l]=ReadShort(file)
						Next
						face_flag=ReadShort(file)
					Next
				
				Case "00004140"'coordinates list
					quantity=ReadShort(file)
					DebugLog "N Coordinates= "+quantity
					o.resizemapCoord(quantity)
					For l=1 To quantity
						For l2=0 To 1
							o.mapCoord[l2,l]=ReadFloat(file)
						Next
					Next
				
				Default
					DebugLog "Default Case"
					For l=1 To 6
						ReadByte (file)
					Next
			End Select
		Wend
		cloasefile(file)
End Function
'---------------------------------------------------------


thanks.


Vertex(Posted 2005) [#2]
Hmm cool, I have worked yet on an 3ds viewer to code further a 3DS importer for my 3d engine. Here the 3DS viever for BlitzBasic. (bad code I know!)

If its help:
Global Stream, Chunk, Size, Number, Name$, Char

Stream = ReadFile("mickey.3ds")

While Not Eof(Stream)
   Chunk = ReadShort(Stream)
   Size  = ReadInt(Stream)
   
   Select Chunk
      Case $4D4D:
         DebugLog "MAIN"
      
      Case $3D3D
         DebugLog " 3D Editor"

      Case $4000
         DebugLog "  Object Block"
         Name$ = ""
         While Not Eof(Stream)
            Char = ReadByte(Stream)
            If Char = 0 Then Exit
            Name$ = Name$+Chr(Char)
         Wend
         DebugLog "   Name: "+Name$
         
      Case $4100
         DebugLog "   Triangular Object"
         
      Case $4110
         DebugLog "    Vertex List"
         Number = ReadShort(Stream)
         DebugLog "     Number Of Vertices: "+Number
         For I = 1 To Number
            DebugLog "      "+ReadFloat(Stream)+", "+ReadFloat(Stream)+", "+ReadFloat(Stream)
         Next
         
      Case $4120
         DebugLog "    Face List"
         Number = ReadShort(Stream)
         DebugLog "     Number Of Faces: "+Number
         For I = 1 To Number
            DebugLog "     "+ReadShort(Stream)+", "+ReadShort(Stream)+", "+ReadShort(Stream)
         Next
         ReadShort(Stream)
         
      Case $4130
         DebugLog "     Faces material List"
         Name$ = ""
         While Not Eof(Stream)
            Char = ReadByte(Stream)
            If Char = 0 Then Exit
            Name$ = Name$+Chr(Char)
         Wend
         DebugLog "      Name: "+Name$
         DebugLog "      Number of entries: "+ReadShort(Stream)
         For I = 1 To Number
            DebugLog "      "+ReadShort(Stream)
         Next
      
      Case $4140
         DebugLog "    Mapping coordinates list for each vertex"
         Number = ReadShort(Stream)
         DebugLog "     Number Of Vertices: "+Number
         For I = 1 To Number
            DebugLog "     "+ReadFloat(Stream)+", "+ReadFloat(Stream)
         Next
         
      Case $AFFF
         DebugLog "  Material block"
      
      Case $A000
         DebugLog "   Material Name"
         Name$ = ""
         While Not Eof(Stream)
            Char = ReadByte(Stream)
            If Char = 0 Then Exit
            Name$ = Name$+Chr(Char)
         Wend
         DebugLog "    Name: "+Name$
      
      Case $A200
         DebugLog "   Map"
      
      Case $A300
         DebugLog "    Map Filename"
         Name$ = ""
         While Not Eof(Stream)
            Char = ReadByte(Stream)
            If Char = 0 Then Exit
            Name$ = Name$+Chr(Char)
         Wend
         DebugLog "     Filename: "+Name$
         
      Case $A354
         DebugLog "    V Scale"
         DebugLog "     Scale: "+ReadFloat(Stream)
         
      Case $A356
         DebugLog "    U Scale"
         DebugLog "     Scale: "+ReadFloat(Stream)
         
      Case $A358
         DebugLog "    U Offset"
         DebugLog "     Offset: "+ReadFloat(Stream)
         
      Case $A35A
         DebugLog "    V Offset"
         DebugLog "     Offset: "+ReadFloat(Stream)
         
      Case $A35A
         DebugLog "    Rotation angle"
         DebugLog "     Angle: "+ReadFloat(Stream)
         
      Default
         SeekFile Stream, FilePos(Stream)+Size-6
   End Select
Wend

CloseFile Stream


Just use Banks! It is better for handling in the 3d engine, becouse you can update for example VBOs faster.

mfg olli

Edit: One question: Have the 3ds format information about vertexnormals?


Skurcey(Posted 2005) [#3]
When i ll get it i will repost... :)

I tried with arrays idependant of the types and it s working, now i need to copy them


Skurcey(Posted 2005) [#4]
i've found another way but i have an error, i can t read the face description
[CODE]
Strict
Global ScreenW=800
Global ScreenH=600
init()
'********************************************************************************************************************************
'* Moteur 3d en BlitzMax et Raw OpenGl *
'* interprete par Skurcey (tiré de http://www.spacesimulator.net/tut1_3dengine_fre.html ) *
'*******************************************************************************************************************************
Type vertex
Field id:Int,x:Float,y:Float,z:Float
Method constructor(n:Int,vx:Float,vy:Float,vz:Float)
id=n;x=vx;y=vy;z=vz
End Method
End Type

Type polygon
Field id,a,b,c

Method constructor(n:Int,v1:Float,v2:Float,v3:Float)
id=n;a=v1;b=v2;c=v3
End Method
End Type

Type mapCoord
Field id,u:Float,v:Float
Method constructor(n:Int,mu:Float,mv:Float)
id=n;u=mu;v=mv
End Method
End Type

Type obj
Field n_v,n_p
Field max_vertices,max_polygons
Field v:vertex[0]
Field p:polygon[0]
Field m:mapCoord[0]
Field name:String

Method newvertex(vx,vy,vz)
v[n_v]=New vertex
v[n_v].constructor(n_v,vx,vy,vz)
n_v:+1
End Method

Method newpoly(id1,id2,id3)
p[n_p]=New polygon
p[n_p].constructor(n_p,id1,id2,id3)
n_p:+1
End Method

Method newmapCoord(id,u,v)
DebugLog id
m[id]=New mapCoord
m[id].constructor(id,u,v)
End Method

Method draw()
Local l:Int
glBegin GL_TRIANGLES
For l=0 To n_p-1
glVertex3f v[p[l].a].x,v[p[l].a].y,v[p[l].a].z
glVertex3f v[p[l].b].x,v[p[l].b].y,v[p[l].b].z
glVertex3f v[p[l].c].x,v[p[l].c].y,v[p[l].c].z
Next
glEnd
End Method

Method load3ds(path:String)
Local file=ReadFile(path)
Local size=FileSize(path)
DebugLog "Sizez :"+size
Local chunk_id:String
Local chunk_length
Local l,tmp
Local b:Byte
If file=0 Then Return 0
DebugLog "Has open "+path

While Not Eof(file)
chunk_id=Hex(ReadShort(file))
chunk_length=convertToInt(Hex(Readint(file)))

'DebugLog chunk_id
Select chunk_id
Case "00004d4d"
Case "00003d3d"
Case "00004000" 'name of the object
For l=0 To 20
b=ReadByte(file)
If b=0 Then Exit
name:+Chr(b)
Next
DebugLog "Name: "+name

Case "00004100"
Case "00004110" 'vertices list+ quantity
max_vertices=ReadShort(file)
DebugLog "Vertices :"+Max_vertices
resizevertices()
For l=0 To max_vertices-1
newvertex(ReadFloat(file),ReadFloat(file),ReadFloat(file))
Next
Case "00004120" 'quantity + list of poly
max_polygons=ReadShort(file)
DebugLog "Polygons: "+Max_polygons
resizepolygons()
For l=0 To max_polygons-1
newpoly(ReadShort(file),ReadShort(file),ReadShort(file))
Next
Case "00004140"'quantity + map coordinates
max_polygons=ReadShort(file)
DebugLog "Map Coord: "+max_polygons
resizeMapCoord(max_polygons)
For l=0 To max_polygons-1
newMapCoord(l,ReadFloat(file),ReadFloat(file))
Next

Default
tmp=SeekStream (file,StreamPos(file)+chunk_length-1)
If tmp=-1 Or (StreamPos(file)+chunk_length)>=size-1 Then Exit

DebugLog StreamPos(file)+chunk_length

'For l= 1 To tmp
' ReadByte file
'Next
End Select
Wend
CloseFile file
DebugLog path +" is now close"
End Method

Method convertToInt(s$)
Local m:String,l,resultat

For Local l=1 To Len(s)
m$=Mid$(s,l,1)
If m>=0 Or m<=9
resultat:+Int(m)
ElseIf m="a"
resultat:+11
ElseIf m="b"
resultat:+12
ElseIf m="c"
resultat:+13
ElseIf m="d"
resultat:+14
ElseIf m="e"
resultat:+15
ElseIf m="f"
resultat:+16
EndIf
Next
Return resultat
End Method

Method resizeMapCoord(newSize)
m=m[..newSize]
End Method
Method resizeVertices()
v=v[..max_vertices]
End Method
Method resizePolygons()
p=p[..max_polygons]
End Method


End Type

Function init()
bglCreateContext ScreenW,ScreenH
glMatrixMode GL_PROJECTION 'permet de fixer la position et lorientation de la camera
glLoadIdentity() 'initialise ou reinitialise la matrice dans les operations courantes
glFrustum -0.1, 0.1,-0.1, 0.1, 0.1, 10000.0 'transformation de la perspective et affichage des points contenu entre les 2 dernieres variables
' gauche,droite,bas,haut, visionMin, visionMax

glMatrixMode GL_MODELVIEW 'objet qui composent la scene(id,orientation,position) + camera
glLoadIdentity()
End Function

Global wireframe
Function key()
If KeyHit(KEY_F1)
If wireframe=0
wireframe=1
glPolygonMode (GL_FRONT_AND_BACK,GL_POINT ) 'dessinne les poly comme des points
'le 1er parametre peut etre GL_FRONT ou GL_BACK aussi
ElseIf wireframe=1
wireframe=2
glPolygonMode (GL_FRONT_AND_BACK,GL_LINE ) 'comme des ligne (wireframe)
ElseIf wireframe=2
wireframe=0
glPolygonMode (GL_FRONT_AND_BACK, GL_FILL ) 'dessinne les poly remplits
EndIf

EndIf
End Function


'----------------------------------------------------------------------------------------------------------------------------------


Local o:obj=New obj
o.load3ds("geosphere.3ds")

While Not KeyHit(KEY_ESCAPE)
glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT 'nettoye les buffer(tampon) de couleur et de profondeur
glLoadIdentity
gluLookAt(0,0,-100, 0,0,5, 0, 1, 0);
o.draw()



bglSwapBuffers
Wend
[/CODE]