Skip to content Skip to sidebar Skip to footer

Webgl Not Rendering My Circle

I am trying to learn how to use Webgl and have already learned how to draw triangles,squares,and lines. I am having problems with creating my circle in webgl. I am not receiving

Solution 1:

Your mistake is not a mistake in the OGL part, but an error when setting up your vertex arrays. You have generated a 2-dimensional array for your vertex buffer instead of a linear 1-dimensional array.

You have to use vertices.push(origin[0], origin[1]); instead of vertices.push(origin); (same for vert).

Your code should look somehow like this:

var vertices = [];
var origin = [0,0];
vertices.push( origin[0], origin[1] );     <-------------------------
for(var i = 0; i <= 360; i+=1){
    var j = i*Math.PI/180;
    var vert = [Math.sin(j),Math.cos(j)];
    vertices.push( vert[0], vert[1] );     <-------------------------
}

The corrected WebGL examples :

varInitDemo = function(){

    	var canvas = document.getElementById('circle-surface');
    	var gl = canvas.getContext('webgl');
    
    	//define geomtryvar vertices = [];
    	var origin = [0,0];
      vertices.push( origin[0], origin[1] );  
   
      for(var i = 0; i <= 360; i+=1){
          var j = i*Math.PI/180;
          var vert = [Math.sin(j),Math.cos(j)];
          vertices.push( vert[0], vert[1] );    
      }
      
    	console.log('the vertices are: '+vertices);
    	var vertBuffer = gl.createBuffer();
    	gl.bindBuffer(gl.ARRAY_BUFFER, vertBuffer);
    	gl.bufferData(gl.ARRAY_BUFFER, newFloat32Array(vertices), gl.STATIC_DRAW);
    	//shadersvar vertSrc = 'attribute vec2 coordinates;'+
    				'void main(){' +
    				'	gl_Position = vec4(coordinates,0.0,1.0);'+
    				'}';
    	var vertShader = gl.createShader(gl.VERTEX_SHADER);
    	gl.shaderSource(vertShader, vertSrc);
    	gl.compileShader(vertShader);
    	if (!gl.getShaderParameter(vertShader, gl.COMPILE_STATUS)) {
    		console.error('ERROR compiling vertex shader!', gl.getShaderInfoLog(vertShader));
    		return;
    	}
    
    	var fragSrc = 'void main(){'+
    					'	gl_FragColor = vec4(1.0,0.0,1.0,1.0);'+
    					'}';
    	var fragShader = gl.createShader(gl.FRAGMENT_SHADER);
    	gl.shaderSource(fragShader, fragSrc);
    	gl.compileShader(fragShader);
    	if (!gl.getShaderParameter(fragShader, gl.COMPILE_STATUS)) {
    		console.error('ERROR compiling fragment shader!', gl.getShaderInfoLog(fragShader));
    		return;
    	}
    
    	var shaderProg = gl.createProgram();
    	gl.attachShader(shaderProg, vertShader);
    	gl.attachShader(shaderProg, fragShader);
    	gl.linkProgram(shaderProg);
    	gl.useProgram(shaderProg);
      
    	if (!gl.getProgramParameter(shaderProg, gl.LINK_STATUS)) {
    		console.error('ERROR linking program!', gl.getProgramInfoLog(shaderProg));
    		return;
    	}
    	gl.validateProgram(shaderProg);
    	if (!gl.getProgramParameter(shaderProg, gl.VALIDATE_STATUS)) {
    		console.error('ERROR validating program!', gl.getProgramInfoLog(shaderProg));
    		return;
    	}
    
    	//associate shader and buffervar coord = gl.getAttribLocation(shaderProg, 'coordinates');
    	gl.vertexAttribPointer(coord, 2, gl.FLOAT, gl.FALSE, 2*Float32Array.BYTES_PER_ELEMENT, 0);
    	gl.enableVertexAttribArray(coord);
    	//Draw
    	gl.clearColor(0.0, 0.0, 0.2, 1.0);
    	gl.clear(gl.COLOR_BUFFER_BIT);
    	gl.drawArrays(gl.TRIANGLE_FAN, 0, vertices.length/2);    
}

InitDemo();
<canvasid = "circle-surface"width="800px"height="600px"></canvas>

Post a Comment for "Webgl Not Rendering My Circle"