這是幾乎所有的WebGL程序
僞代碼
// At init time
for each shader program
create and compile vertex shader
create and compile fragment shader
create program and attach shaders
link program
record locations of attributes and uniforms
for each model/set of geometry/points/data
create buffer(s) for model
copy data into buffer(s) for model
for each texture
create texture
usually asynchronously load textures
// at draw time
clear
for each model
useProgram(program for model)
setup attributes for model
setup textures for model
set uniforms for model
draw
僞代碼這是不超過1個繪製模型1個着色器程序不同。只要做相同的設置。
多一點代碼...
有關設置屬性會看起來像
for each attribute used by model
gl.enableVertexAttribArray(attribLocation);
gl.bindBuffer(gl.ARRAY_BUFFER, bufferWithDataForAttribute);
gl.vertexAttribPointer(attribLocation, ...);
設置紋理(可能)看起來liek這
for each texture used by model
gl.activeTexture(gl.TEXTURE0 + ndx);
gl.bindTexture(gl.TEXTURE_2D, texture);
最後你」 d使用程序
gl.useProgram(programForModel);
for each uniform
gl.uniform???(uniformLocation, uniformValue);
gl.drawArrays(...)
or
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, bufferOfIndicesForModel);
gl.drawElements(...);
感謝gman,對於那真棒的回答! – 2015-05-08 09:05:30