【问题标题】:WebGL/Javascript: Object transformations with multiple objectsWebGL/Javascript:具有多个对象的对象转换
【发布时间】:2014-10-18 17:05:11
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: javascript opengl-es webgl


    【解决方案1】:

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

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

    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(...);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 2019-04-06
      相关资源
      最近更新 更多