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

查看:67
本文介绍了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))。

  • I loaded the canvas.
  • I initialized the webgl-context.
  • I defined vertex/fragment shaders and bound them to a program, which I "used" (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模式没什么不同l带有1个着色器程序。只需进行相同的设置。

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

多一点代码...

设置属性会看起来像什么喜欢

For setting up attributes would look something like

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

设置纹理(可能)看起来有点谎言

Setting up textures (might) look something liek this

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

最后你要使用的程序

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天全站免登陆