且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

WebGL/Javascript:具有多个对象的对象转换

更新时间:2023-02-02 17:27:50

这是几乎所有 WebGL 程序的伪代码

This is the pseudo code for pretty much all WebGL programs

伪代码

// 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 个模型没有什么不同.只需进行相同的设置即可.

This is no different than drawing 1 model with 1 shader program. Just do the same setup.

更多代码...

设置属性看起来像

for each attribute used by model
   gl.enableVertexAttribArray(attribLocation);
   gl.bindBuffer(gl.ARRAY_BUFFER, bufferWithDataForAttribute);
   gl.vertexAttribPointer(attribLocation, ...);

设置纹理(可能)看起来像这样

Setting up textures (might) look something like this

for each texture used by model
   gl.activeTexture(gl.TEXTURE0 + ndx);
   gl.bindTexture(gl.TEXTURE_2D, texture);

你终于可以使用这个程序了

Finally you'd use the program

gl.useProgram(programForModel);
for each uniform
   gl.uniform???(uniformLocation, uniformValue);

gl.drawArrays(...) 
or 
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, bufferOfIndicesForModel);
gl.drawElements(...);