WebGL/Javascript:具有多个对象的对象转换 [英] WebGL/Javascript: Object transformations with multiple objects

查看:25
本文介绍了WebGL/Javascript:具有多个对象的对象转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制几个对象,然后通过选择带有键盘索引的特定对象来转换它们.假设是 1-5.

I want to draw several objects and then transform them by selecting the specific one with a keyboard index. Let's say 1-5.

  • 我加载了画布.
  • 我初始化了 webgl-context.
  • 我定义了顶点/片段着色器并将它们绑定到我使用"的程序(gl.useProgram("program")).

然后我初始化了一个 VertexBuffer(它是一个自己的函数).在那里,我定义了立方体的顶点并绑定了该缓冲区.在同一个函数中,我定义了我的圆锥顶点并将其绑定到不同的缓冲区.

And then I initialized a VertexBuffer (it's an own function). There I defined the vertices for a cube and bound that buffer. In the same function I defined my cone vertices and I bound it to a different buffer.

问题是,我怎样才能制作不同的对象,我可以分别变换?我的意思是着色器从缓冲区获取数据.但是我上次尝试的时候,只画了一个对象.

The thing is, how can I make different objects, that I can transform separately? I mean the shader get's the data from the buffer. But when I tried it the last time, only one object was drawn.

推荐答案

这是几乎所有 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(...);

这篇关于WebGL/Javascript:具有多个对象的对象转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆