Opengl。如何在同一个绘图调用中多次渲染相同的纹理三角形?

时间:2012-08-26 00:27:06

标签: opengl-es

我必须在屏幕的不同点渲染相同的纹理三角形。我发现它渲染超过10次时会有很强的性能损失。如何在绘制之前多次渲染相同的纹理三角形。

我试图在绘制之前向顶点数组添加新几何图形但没有成功。我认为步幅值不对:

这是顶点和纹理坐标。我正在绘制2个纹理正方形,所以我只是复制数组中的数据。

在DrawArray中,我将4改为8.第一个方块似乎没问题,第二个方格肯定不好 GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP,0,8);

private float[] mVertexData2 = 
    {
            -0.3f, -0.3f,  0.0f,        // V1 - bottom left

            -0.3f,  0.3f,  0.0f,        // V2 - top left

             0.3f, -0.3f,  0.0f,        // V3 - bottom right

             0.3f,  0.3f,  0.0f         // V4 - top right


        //Seconde square     
             -1f, -1f,  0.0f,        // V1 - bottom left

            -1f,  1f,  0.0f,        // V2 - top left

             1f, -1f,  0.0f,        // V3 - bottom right

             1f,  1f,  0.0f         // V4 - top right

    };

    private float[] mTextureData2 =
    {
            0.0f, 1.0f,     // top left     (V2)
            0.0f, 0.0f,     // bottom left  (V1)
            1.0f, 1.0f,     // top right    (V4)
            1.0f, 0.0f,     // bottom right (V3)

        //Texture for seconde square    
            0.0f, 1.0f,     // top left     (V2)
            0.0f, 0.0f,     // bottom left  (V1)
            1.0f, 1.0f,     // top right    (V4)
            1.0f, 0.0f,     // bottom right (V3)


    };

public void draw()
{





    // Load the vertex position
    mVertices.position(0);
    GLES20.glVertexAttribPointer ( mSpritePositionLoc, 3, GLES20.GL_FLOAT, 
                                   false, 
                                   0, mVertices );


    // Load the texture coordinate
    mTexVertices.position(0);
    GLES20.glVertexAttribPointer ( mSpriteTexCoordLoc, 2, GLES20.GL_FLOAT,
                                   false, 
                                   0, 
                                   mTexVertices );






    GLES20.glEnableVertexAttribArray ( mSpritePositionLoc );
    //GLES20.glEnableVertexAttribArray ( mSpriteTexCoordLoc );

    GLES20.glActiveTexture ( GLES20.GL_TEXTURE0 );
    GLES20.glBindTexture ( GLES20.GL_TEXTURE_2D, mSpriteTextureId);
    // Set the base map sampler to texture unit to 1
    GLES20.glUniform1i ( mSpriteTextureIdLoc, 1 );




    //GLES20.glDrawElements ( GLES20.GL_TRIANGLES, 6, GLES20.GL_UNSIGNED_SHORT, mIndices );
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP    , 0, 8);



}

1 个答案:

答案 0 :(得分:1)

在每个纹理中,而不是使用DrawArrays,你需要保存它的顶点和纹理坐标,正确的旋转和平移到一个大数组,然后使用DrawArrays只绘制这个数组。(嗯...我不能更好地解释它:))

这是我的代码:

    float squarevData[12]={
            -1,1,
            1,1,
            -1,-1,
            1,1,
            1,-1,
            -1,-1,
        };
    float squarevData2[12]={
            -1,1,
            1,1,
            -1,-1,
            1,1,
            1,-1,
            -1,-1,
        };
    class BatchRenderer
    {
    public:
        float* partVdata;
        float* partCdata;
        float* partTdata;

    int counter1,counter2,counter3;
    int count;
    bool isz;
    BatchRenderer(int maxTextures,bool iszi)
    {
        isz=iszi;
        if(isz)partVdata=(float*)malloc(maxTextures*18*4);
        else partVdata=(float*)malloc(maxTextures*12*4);

        partCdata=(float*)malloc(maxTextures*24*4);
        partTdata=(float*)malloc(maxTextures*12*4);
    }

    void Draw(float x,float y,float z,float scalex,float scaley,float angle,float r,float g,float b,float a)
    {
        angle*=0.017453f;
        for(int c2=0;c2<12;c2+=2)
        {
                float x=squarevData[c2]*scalex;
                float y=squarevData[c2+1]*scaley;
                float cos1=cos(angle);
                float sin1=sin(angle);
                squarevData2[c2] = (cos1*x) - ( sin1*y);
                squarevData2[c2+1] = (sin1*x) + ( cos1*y);
        }

        partVdata[counter1++]=x+squarevData2[0];
        partVdata[counter1++]=y+squarevData2[1];
        if(isz)partVdata[counter1++]=z;
        partCdata[counter2++]=r;
        partCdata[counter2++]=g;
        partCdata[counter2++]=b;
        partCdata[counter2++]=a;
        partTdata[counter3++]=0;
        partTdata[counter3++]=1;


        partVdata[counter1++]=x+squarevData2[2];
        partVdata[counter1++]=y+squarevData2[3];
        if(isz)partVdata[counter1++]=z;
        partCdata[counter2++]=r;
        partCdata[counter2++]=g;
        partCdata[counter2++]=b;
        partCdata[counter2++]=a;
        partTdata[counter3++]=1;
        partTdata[counter3++]=1;

        partVdata[counter1++]=x+squarevData2[4];
        partVdata[counter1++]=y+squarevData2[5];
        if(isz)partVdata[counter1++]=z;
        partCdata[counter2++]=r;
        partCdata[counter2++]=g;
        partCdata[counter2++]=b;
        partCdata[counter2++]=a;
        partTdata[counter3++]=0;
        partTdata[counter3++]=0;

        partVdata[counter1++]=x+squarevData2[6];
        partVdata[counter1++]=y+squarevData2[7];
        if(isz)partVdata[counter1++]=z;
        partCdata[counter2++]=r;
        partCdata[counter2++]=g;
        partCdata[counter2++]=b;
        partCdata[counter2++]=a;
        partTdata[counter3++]=1;
        partTdata[counter3++]=1;

        partVdata[counter1++]=x+squarevData2[8];
        partVdata[counter1++]=y+squarevData2[9];
        if(isz)partVdata[counter1++]=z;
        partCdata[counter2++]=r;
        partCdata[counter2++]=g;
        partCdata[counter2++]=b;
        partCdata[counter2++]=a;
        partTdata[counter3++]=1;
        partTdata[counter3++]=0;

        partVdata[counter1++]=x+squarevData2[10];
        partVdata[counter1++]=y+squarevData2[11];
        if(isz)partVdata[counter1++]=z;
        partCdata[counter2++]=r;
        partCdata[counter2++]=g;
        partCdata[counter2++]=b;
        partCdata[counter2++]=a;
        partTdata[counter3++]=0;
        partTdata[counter3++]=0;

        count++;

    }
    void RenderStart()
    {
        counter1=counter2=count=counter3=0;

    }
    void RenderStop(int textureid)
    {
        glEnable(GL_TEXTURE_2D);
        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE);
        glEnableClientState(GL_COLOR_ARRAY);
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
        glEnableClientState(GL_VERTEX_ARRAY);
        glBindTexture(GL_TEXTURE_2D, textureid);
        glTexCoordPointer(2, GL_FLOAT, 0, partTdata);
        glColorPointer(4, GL_FLOAT, 0,partCdata );
        if(isz)glVertexPointer(3, GL_FLOAT, 0, partVdata);
        else glVertexPointer(2, GL_FLOAT, 0, partVdata);
        glDrawArrays(GL_TRIANGLES, 0, count*6);
        glDisableClientState(GL_COLOR_ARRAY);
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);
        glDisableClientState(GL_VERTEX_ARRAY);

    }
};

如何使用它?

BatchRenderer* br=new BatchRenderer(500,false)//the max number of textures that can be drawn , and if you want z axis

void Render()
{
      br->RenderStart();
      for(int c=0;c<POINTS;c++)
      {                          
    br->Draw(p[c].x,p[c].y,0,p[c].scalex,p[c].scaly,p[c].angle,p[c].r,p[c].g,p[c].b,p[c].a);
      }
      br->RenderStop(yourtextureid);

}

您可以使用mid设备以60 fps绘制超过500个纹理,并且每个

可以具有独特的比例,旋转和颜色