opengl es - WebGL - which API to use? -


i want draw multiple polygon shapes (where each shape has it's own set of vertices). want able position these shapes independently of each other.

which api can use set a_position vertex shader?

  • a) gl.vertexattrib3f
  • b) gl.vertexattribpointer + gl.enablevertexattribarray

thanks.

your question makes sound you're new webgl? maybe should read tutorials? in answer question:

gl.vertexattrib3f lets supply single constant value glsl attribute you'll need use gl.vertexattribpointer , gl.enablevertexattribarray. you'll need set buffers vertex data.

gl.vertexattrib3f point arguably let pass in constant in case have shader uses multiple attributes don't have data of them. example lets have shader uses both textures , needs texture coordinates , has vertex colors. this

vertex shader

attribute vec4 a_position; attribute vec2 a_texcoord; attribute vec4 a_color;  varying vec2 v_texcoord; varying vec4 v_color;  uniform mat4 u_matrix;  void main() {   gl_position = u_matrix * a_position;    // pass texcoord , vertex colors fragment shader   v_texcoord = a_texcoord;   v_color = v_color; } 

fragment shader

precision mediump float;  varying vec2 v_texcoord; varying vec4 v_color;  uniform sampler2d u_texture;  void main() {    vec4 texturecolor = texture2d(u_texture, v_texcoord);     // multiply texture color vertex color    gl_fragcolor = texturecolor * v_color; } 

this shader requires vertex colors. if geometry doesn't have vertex colors have 2 options (1) use different shader (2) turn off attribute vertex colors , set constant color, white.

gl.disablevertexattribarray(acolorlocation); gl.vertexattrib4f(acolorlocation, 1, 1, 1, 1); 

now can use same shader though have no vertex color data.

similarly if have no texture coordinates pass in white 1 pixel shader , set texture coordinates constant.

gl.displayvertexattribarray(atexcoordlocation); gl.vertexattrib2f(atexcoordlocation, 0, 0);   gl.bindtexture(gl.texture_2d, some1x1pixelwhitetexture); 

in case decide color draw setting vertex color attribute.

gl.vertexattrib4f(acolorlocation, 1, 0, 1, 1);  // draw in magenta 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -