c# - Drawing 2D polygons in openGL -


i'm using duocode try , draw 2d polygons in webgl. wrote specific shaders purpose take 2d-vertices , z position other data:

<!-- fragment shader program --> <script id="shader-fs" type="x-shader/x-fragment">     varying highp vec2 vtexturecoord;     uniform highp vec3 ucolor;     uniform sampler2d usampler;     uniform int usamplercount;      void main(void) {     highp vec4 texcolor =vec4(ucolor, 1.0);     if(usamplercount > 0)         texcolor = texture2d(usampler, vtexturecoord);      gl_fragcolor = vec4(1.0, 1.0, 1.0, 1.0);     } </script>  <!-- vertex shader program --> <script id="shader-vs" type="x-shader/x-vertex">     attribute highp vec2 avertexposition;     attribute highp vec2 atexturecoord;      uniform highp vec2 uposition;     uniform highp float uzlayer;      varying highp vec2 vtexturecoord;      void main(void) {     gl_position = vec4(uposition + avertexposition, uzlayer, 1.0);     vtexturecoord = atexturecoord;     } </script> 

in case disabled textures , try draw white polygon. shaders compile without error.

i create vertex buffers this:

    public void updatebuffers()     {         system.console.writeline("updating buffers");         system.console.write("vertices: ");         (int = 0; < rotatedpoints.length; i++)             system.console.write(rotatedpoints[i] + ", ");         system.console.writeline(" ");         system.console.write("texcoords: ");         (int = 0; < texcoords.length; i++)             system.console.write(texcoords[i] + ", ");         system.console.writeline(" ");         system.console.write("triangles: ");         (int = 0; < triangles.length; i++)             system.console.write(triangles[i] + ", ");         system.console.writeline(" ");          gl.bindbuffer(gl.array_buffer, bvertexpositions);         gl.bufferdata(gl.array_buffer, rotatedpoints.as<arraybufferview>(), gl.static_draw);          gl.bindbuffer(gl.array_buffer, btexturecoords);         gl.bufferdata(gl.array_buffer, texcoords.as<arraybufferview>(), gl.static_draw);          gl.bindbuffer(gl.element_array_buffer, bindices);         gl.bufferdata(gl.element_array_buffer, triangles.as<arraybufferview>(), gl.static_draw);     } 

which again goes without errors , gives me following output:

updating buffers vertices: 0, 0, 10, 0, 10, 10, 0, 10,
texcoords: 0, 1, 1, 1, 1, 0, 0, 0,
triangles: 3, 0, 1, 3, 1, 2,

(vertices 2-component)

i'm trying draw single polygon this:

gl.viewport(0, 0, (int)canvas.width, (int)canvas.height); gl.clear(gl.color_buffer_bit | gl.depth_buffer_bit); testsprite.draw(avertexposition, atexturecoord, ucolor, uposition, uzlayer, usampler, usamplercount); 

a denotes attributes, u denotes uniforms locations here.

the drawing looks this:

public void draw(uint avertexposition, uint atexturecoord,         webgluniformlocation ucolor, webgluniformlocation uposition, webgluniformlocation uzlayer, webgluniformlocation usampler, webgluniformlocation usamplercount)     {          //position         gl.uniform2f(uposition, this.position.x, this.position.y);         gl.uniform1f(uzlayer, this.position.z);         gl.uniform3f(ucolor, 1f, 0.5f, 0.5f);         gl.uniform1i(usamplercount, 0);          //vertex data         gl.bindbuffer(gl.array_buffer, bvertexpositions);         gl.vertexattribpointer(avertexposition, 2, gl.float, false, 0, 0);          gl.bindbuffer(gl.array_buffer, btexturecoords);         gl.vertexattribpointer(atexturecoord, 2, gl.float, false, 0, 0);          gl.bindbuffer(gl.element_array_buffer, bindices);          //texture        // gl.activetexture(gl.texture0);        // gl.bindtexture(gl.texture_2d, texture);        // gl.uniform1i(usampler, 0);          //draw         gl.drawelements(gl.triangles, triangles.length, gl.unsigned_short, 0);     } 

again no errors in js console. unfortunately screen stays black. think there error might connected fact vertice array has 2 components, can't figure out. thought had understood basics of opengl apparently wrong, since should not hard of problem. yet can't drawn. if need can post remaining code, buffer creation think mistake must somewhere in code. hope not of "find bug somewhere in code" post, don't know go here, can't figure out mistake.

edit: found mistake, passing int element index list, marked them ushort

mistake here:

 gl.drawelements(gl.triangles, triangles.length, gl.unsigned_short, 0); 

triangles of type int, not ushort


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -