import initShaders from './initShaders.js'
let canvas = document.getElementById('webgl')
let gl = canvas.getContext('webgl')
let vertexShader = `
attribute vec2 a_position;
uniform mat4 u_matrix;
void main() {
gl_Position = u_matrix * vec4(a_position, 0.0, 1.0);
gl_PointSize = 10.0;
}
`
let fragmentShader = `
precision mediump float;
void main() {
gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
}
`
initShaders(gl, vertexShader, fragmentShader)
initVertexBuffers(gl)
function initVertexBuffers(gl) {
let vertices = [ // x, y -0.5, -0.5, 0.5, -0.5, 0.0, 0.5, ]
vertices = new Float32Array(vertices)
let FSIZE = vertices.BYTES_PER_ELEMENT
let buffer = gl.createBuffer()
gl.bindBuffer(gl.ARRAY_BUFFER, buffer)
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW)
let a_position = gl.getAttribLocation(gl.program, 'a_position')
gl.vertexAttribPointer(
a_position, //location: vertex Shader里面attribute变量的location
2, //size: attribute变量的长度(vec2)
gl.FLOAT,
false,
2 * FSIZE,
0
)
gl.enableVertexAttribArray(a_position)
}
function draw(gl) {
gl.clearColor(0.0, 0.0, 0.0, 1.0)
gl.clear(gl.COLOR_BUFFER_BIT)
let n = 3
gl.drawArrays(gl.TRIANGLE_FAN, 0, n)
}
let Sx = 1, Sy = 0.5, Sz = 1
let scale_matrix = [ Sx, 0, 0, 0, 0, Sy, 0, 0, 0, 0, Sz, 0, 0, 0, 0, 1,]
let Tx = 1, Ty = 0, Tz = 0
let translate_matrix = [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, Tx, Ty, Tz, 1]
let deg = 0
let cos = Math.cos(deg / 180 * Math.PI), sin = Math.sin(deg / 180 * Math.PI)
let rotate_matrix = [ cos, sin, 0, 0, -sin, cos, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1,]
tick()
function tick() {
let u_matrix = gl.getUniformLocation(gl.program, 'u_matrix')
gl.uniformMatrix4fv(u_matrix, false, new Float32Array(scale_matrix))
draw(gl)
requestAnimationFrame(tick)
}