如何使用 OpenGL ES 绘制多个三角形,每个三角形都有不同的变换矩阵?

How to draw multiple triangles each with a different transformation matrix with OpenGL ES?

因为我没有成功地为每个三角形绘制具有不同矩阵的多个三角形,所以现在我坚持在 CPU 上变换顶点并使用没有矩阵变换的着色器..

不胜感激!

这是我当前的着色器:

    attribute vec2 vertices;
    attribute vec2 textureUvs;
    varying vec2 textureUv;
    void main()
    {
      gl_Position = vec4(vertices,0.0,1.0);
      textureUv = textureUvs;
    };

除了在调用 OpenGL drawArray() 之前所有顶点都由 CPU 变换外,它工作得很好,我想如果我可以发送每个三角形矩阵并让 OpenGL 计算顶点,我会获得更好的性能。

这是绘图调用:

public final static void drawTexture(FloatBuffer vertices, FloatBuffer textureUvs, int textureHandle, int count, boolean triangleFan)
{
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle);
    GLES20.glUseProgram(progTexture);
    GLES20.glEnableVertexAttribArray(progTextureVertices);
    GLES20.glVertexAttribPointer(progTextureVertices, 2, GLES20.GL_FLOAT, false,  2*Float.BYTES, vertices);
    GLES20.glEnableVertexAttribArray(progTextureUvs);
    GLES20.glVertexAttribPointer(progTextureUvs, 2, GLES20.GL_FLOAT, false, 2*Float.BYTES, textureUvs);
  
    if(triangleFan)
    {
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 4 * count); //Faster 10%
    }
    else
    {
        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 6 * count);
    }
    GLES20.glDisableVertexAttribArray(progTextureVertices);
    GLES20.glDisableVertexAttribArray(progTextureUvs);

}

请注意,这是一个 Sprite 渲染器,这就是我只使用 2d 顶点的原因。

我终于回答了我自己的问题,是的,可以使用 OpenGLES2 绘制具有不同矩阵的多个三角形,这是值得的!

答案与此有关How to include model matrix to a VBO?和@httpdigest 评论。

基本上对于精灵来说,它只需要两个 vec3 作为着色器的属性,你用矩阵 3x3 的第一行和第二行填充。

这是我正在使用的着色器:

    attribute vec3 xTransform;
    attribute vec3 yTransform;
    attribute vec2 vertices;
    attribute vec2 textureUvs;
    varying vec2 textureUv;
    void main()
    {
      gl_Position = vec4(dot(vec3(vertices,1.0), xTransform), dot(vec3(vertices,1.0), yTransform), 0.0,1.0) ;
      textureUv = textureUvs;
    }

首先你得到两个属性指针:

int progTextureXTransform = GLES20.glGetAttribLocation(progTexture, "xTransform");
int progTextureYTransform = GLES20.glGetAttribLocation(progTexture, "yTransform");

对于绘图,您为每个顶点传递一个向量:

    GLES20.glEnableVertexAttribArray(progTextureXTransform);
    GLES20.glVertexAttribPointer(progTextureXTransform, 3, GLES20.GL_FLOAT, false, 3*Float.BYTES, xTransforms);
    GLES20.glEnableVertexAttribArray(progTextureYTransform);
    GLES20.glVertexAttribPointer(progTextureYTransform, 3, GLES20.GL_FLOAT, false, 3*Float.BYTES, yTransforms);

在星系 Tab 2 上,这比使用 CPU 计算顶点快两倍。

xTransform 是 3x3 矩阵的第一行

yTransform 是 3x3 矩阵的第二行

当然,这可以通过添加 zTransform + switch to vec4 扩展到 3d 渲染