Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

547
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda