着色器分为
顶点着色器
跟片元着色器
// HelloPoint1.js (c) 2012 matsuda
// Vertex shader program
var VSHADER_SOURCE =
'void main() {\n' +
' gl_Position = vec4(0.0, 0.0, 0.0, 1.0);\n' + // Set the vertex coordinates of the point
' gl_PointSize = 10.0;\n' + // Set the point size
'}\n';
// Fragment shader program
var FSHADER_SOURCE =
'void main() {\n' +
' gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n' + // Set the point color
'}\n';
// Initialize shaders
if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
console.log('Failed to intialize shaders.');
return;
}
// Draw a point
gl.drawArrays(gl.POINTS, 0, 1);
/**
* Create a program object and make current
* @param gl GL context
* @param vshader a vertex shader program (string)
* @param fshader a fragment shader program (string)
* @return true, if the program object was created and successfully made current
*/
function initShaders(gl, vshader, fshader) {
var program = createProgram(gl, vshader, fshader);
if (!program) {
console.log('Failed to create program');
return false;
}
gl.useProgram(program);
gl.program = program;
return true;
}
homogeneous coordinate
顶点相关的数据
顶点无关的数据
所有 attribute 变量都以 a_前缀开始; 所有 uniform 变量都以u_ 前缀开始
var a_Position = gl.getAttribLocation(gl.program, 'a_Position');
gl.vertexAttrib1f(location, v0)
gl.vertexAttrib2f(location, v0, v1)
gl.vertexAttrib3f(location, v0, v1, v2)
gl.vertexAttrib4f(location, v0, v1, v2, v3)
gl.getUniformLocation(program, name)
gl.uniform1f(location, v0)
gl.uniform2f(location, v0, v1)
gl.uniform3f(location, v0, v1, v2)
gl.uniform4f(location, v0, v1, v2, v3)
1. 创建缓冲区对象:Create a buffer object ( gl.createBuffer() ).
2. 将缓冲区对象绑定到目标:Bind the buffer object to a target ( gl.bindBuffer() ).
3. 向缓冲区对象中写入数据:Write data into the buffer object ( gl.bufferData() ).
4. 将缓冲区对象分配给a_Position变量:Assign the buffer object to an attribute variable ( gl.vertexAttribPointer() ).
5. 连接a_Position变量与分配给它的缓冲区对象:Enable assignment ( gl.enableVertexAttribArray() ).
x' = x cos β – y sin β
y' = x sin β + y cos β
z' = z
texture mapping
纹理映射过程主要包括五个部分