Reputation: 43
I have to render the same textured triangle in different points of the screen. I notice a strong performance hit when rendering it more than 10 times. How could I render the same texture triangle several time before drawing.
Im trying to add new geometry to the vertex array before drawing with no success. I think the stride value is not right:
This is the vertices and texture coordinate. Im traying to draw 2 textured squares so I just kind of duplicate the data in the arrays.
In the DrawArray I change 4 to 8. The first square seems ok, the second one is definately not good 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);
}
Upvotes: 1
Views: 1598
Reputation: 3389
In every texture instead of using DrawArrays you need to save it's vertices and texture cordinates with the correct rotation and translation in to a big array and then use DrawArrays to draw only this array.(well... i cant explain it better :) )
Here is my code:
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);
}
};
How to use it?
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);
}
You can draw more than 500 textures in 60 fps with a mid device and you can have unique scale,rotation and color for each
Upvotes: 1