In the previous two articles, we introduced how to use WebGL for simple drawing. Today, we will briefly discuss how to use buffers.
Additionally, I have placed some resources used during the learning process on the GitHub Yes-webGL project https://github.com/zmofei/yes-webgl. If anyone has good suggestions or is willing to write tutorials together, feel free to reach out to me.
Through the previous examples, we should be very familiar with the rendering process of WebGL: Create Project -> Bind Shader -> Clear Canvas -> Draw Canvas
. Before each drawing, we clear the contents of the canvas and perform new drawings. However, what if we need to draw multiple points or shapes at once? Drawing one by one may not be satisfactory in terms of performance, especially when we have tens of thousands of points to draw.
At this time, buffers
start to make their presence felt. We can store our data in a buffer and then draw the data from the buffer all at once.
In this DEMO, we encapsulated complex steps like getting WebGL context
and binding shaders
. If you want to understand how these two steps work, please refer to the previous two articles.
Now let's get to the point and see how we handle these details in our DEMO.
Using a buffer is very simple. Here, we complete the creation, binding, and data input of the buffer through the following steps:
var vertexColorBuffer = gl.createBuffer();
Create a buffer; this is quite understandable and requires no further explanation.
gl.bindBuffer(gl.ARRAY_BUFFER, vertexColorBuffer);
Bind the created buffer to the WebGL object. Here, we used two parameters: the first is the type. Since we are using array point data, we use the system-provided gl.ARRAY_BUFFER
variable.
The second parameter is the buffer object we need to bind, which is the buffer object we created in the first step.
var verticesColors = new Float32Array([
0.5, 0.5, 0,
-0.5, -0.5, 0,
-0.5, 0.5, 0,
0.5, -0.5, 0]);
//...
gl.bufferData(gl.ARRAY_BUFFER, verticesColors, gl.STATIC_DRAW);
In this step, we pass our point position array into the buffer object. The method accepts three parameters:
gl.ARRAY_BUFFER
or gl.ELEMENT_ARRAY_BUFFER
. In this example, we use gl.ARRAY_BUFFER
.GL_STREAM_DRAW
, GL_STATIC_DRAW
, GL_DYNAMIC_DRAW
.In this example, we pass some vertex coordinates to the vertex shader through JS. So how are these coordinates assigned?
var VSHADER_SOURCE =
'attribute vec4 a_Position;
' +
'void main() {
' +
' gl_Position = a_Position;
' +
' gl_PointSize = 10.0;
' +
'}
';
First, look at the vertex shader. We declare the variable a_Position, and then we assign values using the following method.
var a_Position = gl.getAttribLocation(gl.program, 'a_Position');
gl.vertexAttribPointer(a_Position, 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(a_Position);
In JS, we first get the variable a_Position
address in the renderer using getAttribLocation(). This method accepts two variables:
Next, we specify how to handle the value in the renderer using the vertexAttribPointer() method.
This is a somewhat complex method.
Next, enable enableVertexAttribArray()
.
## Summary
OK, so far we have learned very basic knowledge of WebGL. Next, we will try to take a real step into 3D learning.