Has anyone started making some library functions?

BlitzMax Forums/OpenGL Module/Has anyone started making some library functions?

Ryan Burnside(Posted 2007) [#1]
Hello,

Does anyone have any useful functions that they would like to share for max OpenGL users? Mostly my interest lies in loading data from files. Currently I'm on a tight deadline and would appreciate some help in loading .3ds models for rendering. I don't really know what should be held in a mesh object and what I need. I would be grateful for some premade functionality.


Ryan Burnside(Posted 2007) [#2]
I found some code for loading .3ds data. The author is nowhere to be found and some good chap was able to patch most of it up for me. However some things are still missing. The original code was written in an outdated version of bmax. I wish sombody could get the color data and texture data working. The original author appears to speak French from what I can tell.


Last Codebox in discussion...
http://dbfinteractive.com/index.php?PHPSESSID=ea3183a30caa4c2f08e10cd040d98237&topic=2344.0

Here is a Utah Teapot for testing the following code with:
http://pascal.leynaud.free.fr/3ds/index.html

Here is where JumpMan doing the recent edits left off:

Strict
Global ScreenW=800,screenH=600
Global wireframe

init()

'    ------------------------------------------------------------------------------
'    Type Vertex, contient les positions des vertices.
'    ------------------------------------------------------------------------------
Type Vertex
    Field x:Float,y:Float,z:Float
    
    Method constructor(vx:Float,vy:Float,vz:Float)'assigne des coordonnees a un vertex cree
        x=vx; y=vy; z=vz
    End Method
End Type

'    ------------------------------------------------------------------------------
'    Type Polygone, contient les informations sur le vertices des ploygones(classe par ID)
'    ------------------------------------------------------------------------------
Type Polygone
    Field v1,v2,v3
    Field flag
    Field id_mat
    
    Method constructor(a,b,c)'assigne les vertices au polygone
        v1=a; v2=b; v3=c
    End Method
End Type

'    ------------------------------------------------------------------------------
'    Type MapCoord Contient les coordonnees des textures sur les polygones
'    ------------------------------------------------------------------------------
Type MapCoord
    Field u:Float,v:Float
    
    Method constructor(mu:Float,mv:Float)
        u=mu; v=mv
    End Method
End Type

'    ------------------------------------------------------------------------------
'    Type Objet Contient les polygones des sous objets d une scene
'    ------------------------------------------------------------------------------
Type Objet
    Field nom:String
    Field nombre_vertices,nombre_polygones,nombre_coordonnees
    Field n_vertex,n_polygone,n_mapCoord
    Field v:vertex[]
    Field p:polygone[]
    Field m:mapCoord[]
    Global texture:Int[],texture_name:String[],texture_path:String[],n_texture
    
    Method resizeV()
        v=v[..nombre_vertices]
    End Method
    Method resizeP()
        p=p[..nombre_polygones]
    End Method
    Method resizeM()
        m=m[..nombre_coordonnees]
    End Method
    Method assignTexture(tex,pathfile:String,texname:String)
        texture=texture[..n_texture+1] '????Comprend pas array out of bound
        texture_name=texture_name[..n_texture+1]
        texture_path=texture_path[..n_texture+1]
        DebugLog "n_texture "+n_texture
        
		texture_path[n_texture]=pathfile
        texture[n_texture]=tex
        texture_name[n_texture]=texname
        n_texture :+1
        Return n_texture-1
    End Method
    Method initTextureArray()
        texture=texture[..n_texture] '????Comprend pas array out of bound
        texture_name=texture_name[..n_texture]
        texture_path=texture_path[..n_texture]
    End Method
    
    Method NewVertex(vx:Float,vy:Float,vz:Float)
        'DebugLog n_vertex+"-"+vx+" "+vy+" "+vz
        v[n_vertex]=New vertex
        v[n_vertex].x=vx;v[n_vertex].y=vy;v[n_vertex].z=vz
        n_vertex:+1
    End Method
    
    Method newPolygon(a,b,c,flag:Short=0)
        'DebugLog n_polygone+"-"+a+" "+b+" "+c
        p[n_polygone]=New polygone
        p[n_polygone].v1=a;p[n_polygone].v2=b;p[n_polygone].v3=c
        p[n_polygone].flag=flag
        n_polygone:+1
    End Method
    
    Method newMapCoord(u:Float,v:Float)
        'DebugLog n_mapCoord+"-"+u+" "+v
        m[n_mapCoord]=New mapCoord
        m[n_mapCoord].u=u;m[n_mapCoord].v=v
        n_mapCoord:+1
    End Method
    
    Method draw()
        Local l
        'DebugLog texture
        
        Print nombre_polygones-1
        For l=0 To nombre_polygones-1
			
            'glColor3f Rnd(1),Rnd(1),Rnd(1)
            If n_texture glBindTexture (GL_TEXTURE_2D,texture[p[l].id_mat])
            'DebugLog p[l].id_mat
            glBegin GL_TRIANGLES
            'glTexCoord2f m[p[l].v1].u,m[p[l].v1].v
            glVertex3f v[p[l].v1].x, v[p[l].v1].y, v[p[l].v1].z
            'glTexCoord2f m[p[l].v2].u,m[p[l].v2].v
            glVertex3f v[p[l].v2].x, v[p[l].v2].y, v[p[l].v2].z
            'glTexCoord2f m[p[l].v3].u,m[p[l].v3].v
            glVertex3f v[p[l].v3].x, v[p[l].v3].y, v[p[l].v3].z
            glEnd
        Next
        
    End Method
    
    Method LoadTexture(filePath:String,texname:String,filter=2)
        Local img:TPixmap
        Local texture
        filepath=Lower$(filepath)
        If Right$(filepath,3)="png"
            img=LoadPixmapPNG(filePath)
            texture=GLTexFromPixmap(img,True)
        ElseIf Right$(filepath,3)="jpg"
            img=LoadPixmap(filePath)
            texture=GLTexFromPixmap(img,True)
        EndIf    
        If img=Null Or texture=Null Return 0
        glBindTexture (GL_TEXTURE_2D,texture)
        If filter=0 
            glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR)
            glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR)
        ElseIf filter=1
            glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST)
            glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST)
        ElseIf filter=2    
            glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR)
            glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST)
        EndIf
        Return assignTexture(texture,filepath,texname)
        
    End Method 
    
End Type

'    ------------------------------------------------------------------------------
'    Type Scene Contient les textures et les sous objets de .. ..
'    ------------------------------------------------------------------------------
Type Scene
    Field path:String
    Field directory:String
    Field o:Objet[],nombre_objets=-1
    Field listObjet:TList,obj:objet
    Field size
    
    Method New()
        If listObjet=Null Then listObjet=New TList
    End Method
    
    Method resizeO()
        nombre_objets:+1
        o=o[..nombre_objets]
    End Method
    
    Method load3ds(file:String)
        
        Local stream:TStream,id_noeud:String,taille_noeud,b:Byte,l,newobj:Objet,texture_name:String
        stream=ReadStream(file)
        If stream=Null Then Return 0
        size=FileSize(file)
        DebugLog "Open "+file+" | Size:"+size
        
        Local char:String
        Repeat 
            l:+1
            char=Mid$(file,Len(file)-l,1)
        Until char="/"
        directory=Left$(file,Len(file)-l)
        Print "Directory="+directory
        
        While Not Eof(stream)
            id_noeud=Right$(Upper$(Hex$(ReadShort(stream))),4)
            taille_noeud=ReadInt(stream)
            
            DebugLog "Id="+id_noeud+"  taille="+taille_noeud
            
            Select id_noeud
                Case "4D4D" 'Noeud Principal
                'Case "0002" 'Version
                    'version=readint(stream)
                Case "3D3D" 'Editeur
                Case "4000" 'nom de l objet
                    resizeO()
                    newobj=New objet
                    listObjet.AddLast newobj
                    DebugLog "Nouvel objet "+nombre_objets
                    Repeat '???????????????????? peut etre limite a 20 char?
                        b=ReadByte(stream)    
                        If b=0 Exit
                        newobj.nom:+Chr(b)                        
                    Until b = 0
                    DebugLog "Nom ="+newobj.nom
                Case "4100" 'regroupe les donnees/positions des points, faces
                Case "4110" 'liste des vertices    
                    newobj.nombre_vertices=ReadShort(stream)
                    newobj.resizeV()
                    DebugLog "    Vertices:"+newobj.nombre_vertices
                    For l = 0 To newobj.nombre_vertices-1
                        newobj.newVertex(ReadFloat(stream),ReadFloat(stream),ReadFloat(stream))
                    Next
                Case "4120"
                    Local a,b,c
                    newobj.nombre_polygones=ReadShort(stream)
                    newobj.resizeP()
                    DebugLog "    Polygones:"+newobj.nombre_polygones
                    For l=0 To newobj.nombre_polygones-1
                        newobj.newPolygon(ReadShort(stream),ReadShort(stream),ReadShort(stream))
                        ReadShort(stream)
                    Next
                Case "4130"
                    newobj.initTextureArray()
                    Local mat_name:String="",id_mat,face_n,id_poly
                    Repeat 
                        b=ReadByte(stream)
                        If b=0 Exit
                        mat_name:+Chr(b)
                    Until b=0
                    DebugLog "Assign "+mat_name +" as material. "+newobj.n_texture
                    If newobj.n_texture
					For l= 0 To newobj.n_texture 'donne l id du materiel en echange de son nom '(pour l utiliser dans les arrays)
                        If mat_name=newobj.texture_name[l]
                            id_mat=l
                            Exit
                        EndIf
                    Next
					EndIf
                    face_n=ReadShort(stream)
                    DebugLog "N FACE TO TEX TO "+mat_name+":"+face_n
                    For l=0 To face_n-1
                        id_poly=ReadShort(stream)
                        newobj.p[id_poly].id_mat=id_mat
                        'DebugLog "="newobj.p[id_poly].id_mat
                    Next
                Case "4140"
                    newobj.nombre_Coordonnees=ReadShort(stream)
                    newobj.resizeM()
                    DebugLog "    MapCoord"+newobj.nombre_Coordonnees
                    For l=0 To newobj.nombre_Coordonnees-1
                        newobj.newMapCoord(ReadFloat(stream),ReadFloat(stream))
                    Next                    
                
                Case "AFFF" 'textures
                    
                    Case "A000"
                        texture_name=""
                        Repeat 
                            b=ReadByte(stream)
                            If b=0 Exit
                            texture_name:+Chr(b)
                        Until b=0
                        DebugLog "Nom Texture:"+texture_name
                    Case "A200"'le fichier contient une texture
                        newobj=New objet
                    Case "A300"
                        Local texture_path:String
                        Repeat 
                            b=ReadByte(stream)
                            If b=0 Exit
                            texture_path:+Chr(b)
                        Until b=0
                        DebugLog "Path to  Texture:"+texture_path
                        newobj.loadtexture(directory+texture_path,texture_name)
                    
                    
                Default
                    SeekStream(stream,StreamPos(stream)-6+taille_noeud)
            End Select
        Wend
        CloseStream stream
        DebugLog "*** Fichier Ferme ***"
    End Method
    
    Method draw()
        For obj=EachIn ListObjet
            obj.draw()
        Next
    End Method
End Type

Function init()
    GLGraphics ScreenW,ScreenH
    glEnable GL_TEXTURE_2D
    glShadeModel(GL_SMOOTH)
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
    glEnable GL_DEPTH_TEST
    glMatrixMode GL_PROJECTION
    glLoadIdentity()
    glFrustum -0.1, 0.1,-0.1, 0.1, 0.5, 10000.0
    glMatrixMode GL_MODELVIEW
    glLoadIdentity()
End Function

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 s:scene=New scene
s.load3ds("news/teapot.3ds")
Local r:Float

While Not KeyHit(KEY_ESCAPE)
    key()
    glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT 'nettoye les buffer(tampon) de couleur et de profondeur 
    glLoadIdentity
    'gluLookAt(0,0,-500,    0,0,5,     0, 1, 0);
    glTranslatef 0,0,-200
    r:+.1
    If r>360 r:-360
    glRotatef r,1,.5,0
    
    s.draw()
    
	Flip()
Wend



JoshK(Posted 2007) [#3]
Why would you store all your mesh data in types when you can just store it in a VBO on the GPU? You don't even need client-side vertex arrays.


Ryan Burnside(Posted 2007) [#4]
I'm just trying to get the .3ds models read and loaded right now. This is not my code. It's frusterating trying to get a rendered .3ds object in my game. I'm jst looking for something that works well. :(


RobDaMo(Posted 2008) [#5]
I've just created a md2 file reader for BMax...


Dreamora(Posted 2008) [#6]
How about just using MiniB3D as base for your technology?


nawi(Posted 2008) [#7]

Why would you store all your mesh data in types when you can just store it in a VBO on the GPU? You don't even need client-side vertex arrays.


So you can modify the mesh.


klepto2(Posted 2008) [#8]
you can map vbos so you can modify them directly without the need of additional arrays.