console
/**
* Created by hebo on 2017/12/26.
* webgl base template
*/
main();
function main() {
const canvas = document.querySelector("#glcanvas");
// Initialize the GL context
const gl = canvas.getContext("webgl");
// Only continue if WebGL is available and working
if (!gl) {
alert("Unable to initialize WebGL. Your browser or machine may not support it.");
return;
}
// 顶点着色器 glsl-es语言编写
let vertexSource = `
attribute vec4 a_Position;
attribute float a_PointSize;
void main(){
gl_Position = a_Position;
gl_PointSize = a_PointSize;
}`;
// 片元着色器 glsl-es语言编写
let fragmentSource = `
void main(){
gl_FragColor = vec4(1.0,0.0,0.0,1.0);
}`;
// return;
// 创建着色器程序
let shaderProgram = initShaderProgram(gl, vertexSource, fragmentSource);
initBuffers(gl,shaderProgram)
// 绘制
draw(gl, shaderProgram)
}
function initBuffers(gl, shaderProgram){
var vertices = new Float32Array([
0.0, 0.5, -0.5, -0.5, 0.5, -0.5
]);
var n = 3;
// 多个顶点size数据
var sizes = new Float32Array([
10.0, 20.0, 30.0 // Point sizes
]);
// Create a buffer object
var vertexBuffer = gl.createBuffer();
var sizeBuffer = gl.createBuffer();
if (!vertexBuffer || !sizeBuffer) {
console.log('Failed to create the buffer object');
return -1;
}
// Write vertex coordinates to the buffer object and enable it
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
var a_Position = gl.getAttribLocation(shaderProgram, 'a_Position');
if(a_Position < 0) {
console.log('Failed to get the storage location of a_Position');
return -1;
}
gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(a_Position);
// Bind the point size buffer object to target
gl.bindBuffer(gl.ARRAY_BUFFER, sizeBuffer);
gl.bufferData(gl.ARRAY_BUFFER, sizes, gl.STATIC_DRAW);
var a_PointSize = gl.getAttribLocation(shaderProgram, 'a_PointSize');
if(a_PointSize < 0) {
console.log('Failed to get the storage location of a_PointSize');
return -1;
}
gl.vertexAttribPointer(a_PointSize, 1, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(a_PointSize);
// Unbind the buffer object
gl.bindBuffer(gl.ARRAY_BUFFER, null);
}
function draw(gl,program){
gl.useProgram(program);
// Set clear color to black, fully opaque
// 指定绘图区背景色 arg: r,g,b,a
gl.clearColor(0.0, 0.0, 0.0, 1.0);
// Clear the color buffer with specified clear color
// 清空绘图区域,用设定的背景色填充绘图区域
gl.clear(gl.COLOR_BUFFER_BIT);
// 执行顶点着色器,
//args: mode 绘制模式,接受的是gl常量 比如 gl.LINES
// first 指定从哪个顶点开始绘制
// count 指定绘制需要用多少个顶点
gl.drawArrays(gl.POINTS, 0, 3);
}
function initShaderProgram(gl, vsSource, fsSource) {
let vertexShader = loadShader(gl, gl.VERTEX_SHADER, vsSource);
let fragmentShader = loadShader(gl, gl.FRAGMENT_SHADER, fsSource);
const shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram);
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
alert('Unable to initialize the shader program: ' + gl.getProgramInfoLog(shaderProgram));
return null;
}
return shaderProgram;
}
function loadShader(gl, type, source) {
// 依据type创建着色器
const shader = gl.createShader(type);
// Send the source to the shader object
gl.shaderSource(shader, source);
// Compile the shader program
gl.compileShader(shader);
// See if it compiled successfully
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert('An error occurred compiling the shaders: ' + gl.getShaderInfoLog(shader));
gl.deleteShader(shader);
return null;
}
return shader;
}
<h3>WebGL Demo</h3>
<canvas id="glcanvas" width="640" height="480"></canvas>