Results 1 to 10 of 10

Thread: Tiny 3DS loader

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Tiny 3DS loader

    hi
    a tiny 3ds warpper draw 1 object wih 1 texture
    in fact this is my translation of a c++ code has been found in www.codesampler.com

    Code:
    Unit Mini3ds;
    
    interface
    
    uses windows,opengl;
    
    
    const
       MAX_GEO = 10000;
    type
    
    // Vertex
       Vertex  = record
            x,
            y,
            z : single;
            end;
    
    // texture
       mapcoord = record
            u,
            v : single;
            end;
    
    // index
       polygon = record
            a,
            b,
            c : integer;
            end;
    
    // 3d object structure
       _object = record
            name : array[0..21] of char;
            numVerts : integer;
            numPolys : integer;
            v : array [0..MAX_GEO] of vertex;
            p : array [0..MAX_GEO] of polygon;
            m : array [0..MAX_GEO] of mapcoord;
            end;
    
    // 3ds object
      T3DS = object
       public
        procedure Load(_fn : string);
        procedure Draw();
        procedure free();
       public
        obj : _object;
       end;
    
    
    implementation
    
    { T3DS }
    
    procedure T3DS.Load(_fn: string);
    var
    _file : file of byte;
     i  : integer;
     chunkID : short;
     chunkLength  : integer;
     tmp       : char;
     useless  : short;
     from      : integer;
    _fsize     : integer;
     label begincase;
    begin
    
        AssignFile(_file,_fn);
        Reset(_file);
      _fsize := filesize(_file);
    
      while ( filepos(_file) < _fsize) do
      begin
         begincase:
         BlockRead(_file,chunkID,2);
         BlockRead(_file,chunkLength,4);
    
        case chunkID of
    
         $4d4d : begin end;
         $3d3d : begin end;
         $4000 :
      	for i:=0 to 20-1 do
    	 begin
    	       Blockread (_file,tmp, 1);
                           obj.name[i]:=tmp;
                    if pointer(tmp) = NIL then
                  goto begincase;
          end;
    
         $3f80 :begin end;
         $4100 :begin end;
         $4110 :
             begin
       	BlockRead(_file,obj.numVerts,2);
        	for i:=0 to obj.numVerts - 1 do
                        begin
    	         BlockRead (_file, obj.v[i].x,4);
                             BlockRead (_file, obj.v[i].y,4);
        	         BlockRead (_file, obj.v[i].z,4);
     	    end;
            end;
    
         $4120 :
          begin
    	BlockRead (_file,obj.numPolys, 2);
                      for i:=0 to obj.numPolys - 1 do
                         begin
    	        BlockRead (_file,obj.p[i].a, 2);
        	        BlockRead (_file,obj.p[i].b, 2);
    	        BlockRead (_file,obj.p[i].c, 2);
    	        BlockRead (_file,useless, 2);
    	     end;
          end;
    
         $4140 :
          begin
       	BlockRead (_file , useless, 2);
        	  for i:=0 to obj.numVerts - 1 do
    	     begin
    	         BlockRead (_file , obj.m[i].u , 4);
                             BlockRead (_file , obj.m[i].v , 4);
    	     end;
          end;
        else
        begin
             from := filePos(_file);
             seek(_file,from + chunkLength-6);
         end;
        end;
      end;
     closefile(_file);
    end;
    
    procedure T3DS.Draw();
    var i : integer;
    begin
    
       glBegin(GL_TRIANGLES);
         for i:=0 to obj.numPolys - 1 do
            begin
                 glTexCoord2f( obj.m[ obj.p[i].a ].u,obj.m[ obj.p[i].a ].v);
                 glVertex3f( obj.v[ obj.p[i].a ].x,
                                  obj.v[ obj.p[i].a ].y,
                                  obj.v[ obj.p[i].a ].z);
    
                 glTexCoord2f( obj.m[ obj.p[i].b ].u,obj.m[ obj.p[i].b ].v);
                 glVertex3f( obj.v[ obj.p[i].b ].x,
                                  obj.v[ obj.p[i].b ].y,
                                  obj.v[ obj.p[i].b ].z);
    
                 glTexCoord2f( obj.m[ obj.p[i].c ].u,obj.m[ obj.p[i].c ].v);
                 glVertex3f( obj.v[ obj.p[i].c ].x,
                                  obj.v[ obj.p[i].c ].y,
                                  obj.v[ obj.p[i].c ].z);
            end;
    
     glEnd();
    
    end;
    
    procedure T3DS.free;
    begin
     //
    end.
    virtual
    Last edited by virtual; 14-09-2010 at 07:36 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •