06-fun/scripts/06-02-modelloader.js
/**
 * Reads in a model .data file and initializes all buffers with 
 * the contained data.
 * @param fileText String source of the .data file
 */
Reads in a model .data file and initializes all buffers with
the contained data.
@param fileText String source of the .data file
Reads in a model . data file and initializes all buffers with the contained data . @param fileText String source of the . data file
function readDataFile(fileText) {
    var lines = fileText.split(/\r\n|\r|\n/);
    var vertices = [];
    var linds = [];
    var trinds = [];
    for( var i = 0; i < lines.length; ++i ) {
        var tokens = lines[i].split(" ");
        if( tokens.length !== 3 ) {
            continue;
        }
        linds.push( linds.length );
        if( ((vertices.length / 3) + 1) % 4 !== 0 ) {
            trinds.push( vertices.length / 3 );
        }
        vertices.push( tokens[0], tokens[1], tokens[2] );
    }
    gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
    vbo.length = vertices.length;
    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, lineIndices);
    gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(linds), gl.STATIC_DRAW);
    lineIndices.length = linds.length;
    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, triIndices);
    gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(trinds), gl.STATIC_DRAW);
    triIndices.length = trinds.length;
    display();
}
function readDataFile ( fileText ) { var lines = fileText . split ( / \r\n|\r|\n / ) ; var vertices = [ ] ; var linds = [ ] ; var trinds = [ ] ; for ( var i = 0 ; i < lines . length ; ++ i ) { var tokens = lines [ i ] . split ( " " ) ; if ( tokens . length !== 3 ) { continue ; } linds . push ( linds . length ) ; if ( ( ( vertices . length / 3 ) + 1 ) % 4 !== 0 ) { trinds . push ( vertices . length / 3 ) ; } vertices . push ( tokens [ 0 ] , tokens [ 1 ] , tokens [ 2 ] ) ; } gl . bindBuffer ( gl . ARRAY_BUFFER , vbo ) ; gl . bufferData ( gl . ARRAY_BUFFER , new Float32Array ( vertices ) , gl . STATIC_DRAW ) ; vbo . length = vertices . length ; gl . bindBuffer ( gl . ELEMENT_ARRAY_BUFFER , lineIndices ) ; gl . bufferData ( gl . ELEMENT_ARRAY_BUFFER , new Uint16Array ( linds ) , gl . STATIC_DRAW ) ; lineIndices . length = linds . length ; gl . bindBuffer ( gl . ELEMENT_ARRAY_BUFFER , triIndices ) ; gl . bufferData ( gl . ELEMENT_ARRAY_BUFFER , new Uint16Array ( trinds ) , gl . STATIC_DRAW ) ; triIndices . length = trinds . length ; display ( ) ; }
Reads in a model .data file and initializes all buffers with
 the contained data.
Reads in a model . data file and initializes all buffers with the contained data .
// Split the file text into individual lines // Initialize some arrays to keep track of vertices, line  // indices, and triangle indices // Loop through each line // Split the line by whitespace // If there weren't 3 tokens on the line, we don't care about it // If there were, we have a new vertex, update all arrays // Bind and set the data of the vbo // Bind and set the data of the lineIndices buffer // Bind and set the data of the triangle Indices buffer // Finally, display the results of the computation