简体   繁体   English

在openGL中绘制2D多边形

[英]Drawing 2D polygons in openGL

I'm using DuoCode to try and draw 2D polygons in webGL. 我正在使用DuoCode尝试在webGL中绘制2D多边形。 I wrote specific shaders for this purpose that take 2D-vertices and z position as well as some other data: 为此,我编写了特定的着色器,这些着色器采用2D顶点和z位置以及一些其他数据:

<!-- 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 this case I disabled textures and just try to draw a white polygon. 在这种情况下,我禁用了纹理,只是尝试绘制白色多边形。 The shaders compile without error. 着色器编译没有错误。

I create my vertex buffers like this: 我创建这样的顶点缓冲区:

    public void UpdateBuffers()
    {
        System.Console.WriteLine("Updating buffers");
        System.Console.Write("Vertices: ");
        for (int i = 0; i < rotatedPoints.length; i++)
            System.Console.Write(rotatedPoints[i] + ", ");
        System.Console.WriteLine(" ");
        System.Console.Write("texCoords: ");
        for (int i = 0; i < texCoords.length; i++)
            System.Console.Write(texCoords[i] + ", ");
        System.Console.WriteLine(" ");
        System.Console.Write("Triangles: ");
        for (int i = 0; i < 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 and gives me the following output: 再次没有错误,并提供以下输出:

Updating buffers Vertices: 0, 0, 10, 0, 10, 10, 0, 10, 更新缓冲区顶点:0、0、10、0、10、10、0、10,
texCoords: 0, 1, 1, 1, 1, 0, 0, 0, texCoords:0、1、1、1、1、0、0、0,
Triangles: 3, 0, 1, 3, 1, 2, 三角形:3、0、1、3、1、2

(Vertices are 2-component) (顶点是2分量)

I'm trying to draw a single polygon like 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. a表示属性,u表示此处的制服位置。

The drawing itself looks like 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 the JS console. 再次在JS控制台中没有错误。 Unfortunately the screen just stays black. 不幸的是,屏幕只是保持黑色。 I think there error might be connected to the fact that the vertice array has 2 components, but I can't figure it out. 我认为可能与顶点数组具有2个成分的事实有关,但我无法弄清楚。 I thought I had understood the basics of openGL but apparently I was wrong, since this should really not be that hard of a problem. 我以为我已经了解了openGL的基础知识,但是显然我错了,因为这实际上应该不是那么难解决的问题。 Yet I can't get anything drawn. 但是我什么也画不出来。 If you need it I can post the remaining code, like the buffer creation but I think my mistake must be somewhere in this code. 如果需要,我可以发布其余代码,例如创建缓冲区,但是我认为我的错误一定在此代码中。 I hope this is not too much of a "find the bug somewhere in my code" post, but I really don't know where to go from here, I just can't figure out my mistake. 我希望这不是“在我的代码中的某个地方找到错误”的帖子,但我真的不知道从这里去哪里,我只是无法弄清楚我的错误。

EDIT: Found my mistake, I was passing int to the element index list, but I marked them as ushort 编辑:发现我的错误,我将int传递到元素索引列表,但是我将它们标记为ushort

Mistake was here: 错误在这里:

 gl.drawElements(GL.TRIANGLES, triangles.length, GL.UNSIGNED_SHORT, 0);

triangles was actually of type int, not ushort 三角形实际上是int类型,而不是ushort类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM