Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

554
Vistas
New to WebGl: Draw circle with triangle_fan, using a slider to determine the number of triangles to use to create the circle

I cannot manage to draw a circle using triangle fan. The code bellow is in a javascript file that would be called to create the circle. With help of a tutorial, this is what I was able to gather for a function to draw it.

function createCircleGeometry(radius, segments) {
    var pi2 = 2 * Math.PI;
    var step = pi2 / segments;
    var points = [];
    var colors = [];
    var tcolors = [
        vec3(1.0, 0.0, 0.0),
        vec3(0.0, 1.0, 0.0),
        vec3(0.0, 0.0, 1.0),
    ];
    var x;
    var y;
    var z;
    var noVer = segments + 2;
    var circleVerticesX = [];
    var circleVerticesY = [];
    var circleVerticesZ = [];

    circleVerticesX.length = noVer;
    circleVerticesY.length = noVer;
    circleVerticesZ.length = noVer;

    circleVerticesX[0] = x;
    circleVerticesY[0] = y;
    circleVerticesZ[0] = z;
    
    
    for ( let i = 1; i < noVer; i++ )
    {
        circleVerticesX[i] = x + ( radius * cos( i * step ) );
        circleVerticesY[i] = y + ( radius * sin( i * step) );
        circleVerticesZ[i] = z;
    }

    var allCircleVertices =[];
    allCircleVertices.length = noVer * 3;

    for ( let i = 0; i < noVer; i++ )
    {
        allCircleVertices[i * 3] = circleVerticesX[i];
        allCircleVertices[( i * 3 ) + 1] = circleVerticesY[i];
        allCircleVertices[( i * 3 ) + 2] = circleVerticesZ[i];

        points.push(circleVerticesZ[i],circleVerticesX[i],circleVerticesY[i]);
        colors.push(tcolors[0],tcolors[1],tcolors[2]);
    }
    

    return flattenArrays(points, colors);    
}

And this would be the function to render it.


function render() {
        gl.clear(gl.COLOR_BUFFER_BIT);
        gl.enableVertexAttribArray(colorAttribLocation);
        gl.vertexAttribPointer(positionAttribLocation, 3, gl.FLOAT, false, 0, 0);
        gl.vertexAttrib4f(colorAttribLocation, 1, 1 , 1, 1); 
        gl.drawArrays(gl.TRIANGLE_FAN, 0, noVer);
    }
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

So I took your code and step-debugged it. For that I:

  • defined vec3 as function vec3 (x,y,z) {return [x,y,z]}
  • created aliases for sin and cos by const sin = Math.sin, cos = Math.cos; (why alias those but not Math.PI?)
  • didn't know what flattenArrays was supposed to return but I didn't need to as the error happens before that

You declare x,y,z variables and use them without having assigned anything to them resulting in NaN values everywhere. Initializing those to 0 resulted in a points array that seemed reasonable on first glance.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda