Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
826 views
in Technique[技术] by (71.8m points)

opengl es - WebGL/Javascript: Object transformations with multiple objects

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

  • 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")).

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is the pseudo code for pretty much all WebGL programs

Pseudo code

// 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

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

a little more code...

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...