function [V, F]=LoadVRML(filename)
% V = vertices
% T = triangles
% O = rendering options... ?
%
    V = {};
    F = {};

    if ~exist('filename')
        error('LoadVRML: Must supply a VRML file');
        return;
    end
    
    [fid, message] = fopen(filename);
    if fid == -1
        error(message)
    end
    has_Coordinates = 0;
    has_IndexedFaceSet = 0;
    vertices = [];
    faces = [];
    in = fscanf(fid, '%s', 1);
    while ~feof(fid)    
        if findstr(in, 'Coordinate')
            has_Coordinates = 1;
        elseif strcmp(in, 'IndexedFaceSet')
            has_IndexedFaceSet = 1;
        elseif strcmp(in, 'point')
            if has_Coordinates
               vertices = read_vertices(fid);
            end
        elseif strcmp(in, 'coordIndex')
            if has_IndexedFaceSet
               faces = read_faces(fid);
            end
        end
        in = fscanf(fid, '%s', 1);
    end  

    V{1} = vertices;
    F{1} = faces;

function V=read_vertices(fid)
    vertices = [];
    brake = 0;
    found = 0;

    if eat_til_word(fid, '[')
    % finally we can read coordinates... 
        while ~feof(fid) & ~brake
            line = fgetl(fid);
            line = deblank(line);
            if strcmp(line, ']')
               brake = 1;
            elseif findstr(line,']')
                
               [x, y, z] = strread(line, '%f%f%f', 'delimiter', ']');
               brake = 1;
               vertices = [vertices; x y z];
            else
               [x, y, z] = strread(line, '%f%f%f', 'delimiter', ',');
               vertices = [vertices; x y z];
            end
        end
    end
 
    V = vertices;

function F=read_faces(fid)
    faces = [];
    facecount = 0;
    brake = 0;
    found = 0;
    
    if eat_til_word(fid, '[')
       % finally we can read faces... 
       while ~feof(fid) & ~brake
           line = fgetl(fid);
           line = deblank(line);
           if strcmp(line, ']')
              brake = 1;
           elseif findstr(line,']')
              found = 1;
              face = strread(line(1:length(line)-1), '', -1, 'delimiter', ',');
           else
              face = strread(line(1:length(line)-1), '', -1', 'delimiter', ',');
           end
           if ~brake
              % add the face to the list of faces.... but we need to find -1
              if length(face) > 1
                 if face(length(face)) == -1
                    % Matlab is not zero based.
                    face = face(1:length(face)-1) + 1;
                    facecount = facecount + 1;
                    faces = [faces; face];
                 else % hmm... what to do here... 
                    face = face + 1;
                 end
              end
           end
           if found
              brake = 1;
           end
       end
    end

    F = faces;

function F=eat_til_word(fid, word)
    in = fscanf(fid, '%s', 1);
    found = 0;
    while ~feof(fid)
        if strcmp(in, word)
           F = 1;
           return
        end
        in = fscanf(fid, '%s', 1);
    end

    F = found;
