Hi, I am new to WEBGL and facing problem while rendering circle using TRIANGLE_FAN
I have written the complete code but don’t know what’s going wrong while rendering semi circle
It is making one unnecessary triangle every time at any position
Here is the code given, How can this be solved this problem?
const canvas_1 = document.getElementById("my_canvas_1");
var gl = canvas_1.getContext('webgl');
// shader Program
function ourShader(){
var vertCode ='attribute vec2 coordinates;' + 'void main(void) {' + ' gl_Position = vec4(coordinates,0.0, 1.0);' + 'gl_PointSize = 1.0;'+'}';
var vertShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vertShader, vertCode);
gl.compileShader(vertShader);
var fragCode = 'void main(void) {' + 'gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);' + '}';
var fragShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fragShader, fragCode);
gl.compileShader(fragShader);
var shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, vertShader);
gl.attachShader(shaderProgram, fragShader);
gl.linkProgram(shaderProgram);
gl.useProgram(shaderProgram);
return shaderProgram;
}
// Clear background
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.enable(gl.DEPTH_TEST);
gl.clear(gl.COLOR_BUFFER_BIT);
var vertices = new Array();
console.log("vertices: "+vertices);
let r = 0.2;
let prec = 0.0001;
pos = [0.0,-0.5];
let i=0;
vertices.push(pos[0],pos[1]);
let x = r+pos[0];
let x_ = -r+pos[0];
function drawSmallDrop(shadersProg)
{
if(x >= x_)
{
y = Math.sqrt( (r*r) - ((x-pos[0])*(x-pos[0])) ) + pos[1];
vertices.push(x,y);
console.log(x,y,i);
i++;
x = x-prec;
}
if(vertices.length>=3){
var vertex_buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer);
var coord = gl.getAttribLocation(shadersProg, "coordinates");
gl.vertexAttribPointer(coord, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(coord);
gl.drawArrays(gl.TRIANGLE_FAN, 0, vertices.length);
}
console.log(gl.getError());// for error check
}
shadProg = ourShader();
setInterval(drawSmallDrop, 1, shadProg) ;
gl.viewport(0,0,canvas_1.height,canvas_1.height);