Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

249
Views
Approximating sphere recursively using WebGL, but getting some errors

I'm super new to WebGL and trying to run the below portion of my code, but it's not working. I'm following an exercise in my textbook for approximating a sphere object using subdivision. Trying to follow the same basic concept that I used for the Sierpinski Gasket.

    //Specifiy four vertices for a tetrahedron
    var va = vec4(0.0, 0.0, -1.0, 1);
    var vb = vec4(0.0, 0.942809, 0.333333, 1);
    var vc = vec4(-0.816497, -0.471405, 0.333333, 1);
    var vd = vec4(0.816497, -0.471405, 0.333333, 1);
    
    var pointsArray = [];
    var numTimesToSubdivide = 8;
    
    //Adding vertex positions to a vertex array
    function triangle(a, b, c)
    {
        pointsArray.push(a);
        pointsArray.push(a);
        pointsArray.push(a);
        index += 3;
    }
    
    //Tetrahedron midpoint subdivision
    function divideTriangle(a, b, c, count)
    {
        if(count > 0) {
            var ab = normalize(mix(a, b, 0.5), true);
            var ac = normalize(mix(a, c, 0.5), true);
            var bc = normalize(mix(b, c, 0.5), true);
            divideTriangle(a, ab, ac, count - 1);
            divideTriangle(ab, b, bc, count - 1);
            divideTriangle(bc, c, ac, count - 1);
            divideTriangle(ab, bc, ac, count - 1);
        }
        else {
            triangle(a, b, c);
        }
    }
    
    //Dividing four sides of the tetrahedron
    function tetrahedron(a, b, c, d, n)
    {
        divideTriangle(a, b, c, n);
        divideTriangle(d, c, b, n);
        divideTriangle(a, d, b, n);
        divideTriangle(a, c, d, n);
    }

    //Initiate recursion
    tetrahedron(va, vb, vc, vd, numTimesToSubdivide);
    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(pointsArray), gl.STATIC_DRAW);
    gl.drawArrays(gl.TRIANGLES, 0, pointsArray.length / 2);
}

Getting this error

Uncaught ReferenceError: index is not defined

Appreciate any help.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!