1
General discussions / I think SFML can be faster if it use gl buffers
« on: October 27, 2007, 04:02:07 pm »
Hi ,I am new here and I am very interested in SFML . and I think it could run faster if it use gl buffers . In my D engine.I use vertex buffer to accumulate the vertex and redner all the buffer when texture or render state changes. You can see the HGE for example . I think it will make SFML run faster.
first I check the gl versions : if it support buffer object ,enable buffer rendering .else if it support vertex array . enable vertex array .else use the begin end method.
second my video driver has a function " void drawQuadList(Quad * list,int length)" and draw the quad list .
third I use a 2D render to render the quads
hope my code helps. And you can see HGE as a example for quad accumulate method .
first I check the gl versions : if it support buffer object ,enable buffer rendering .else if it support vertex array . enable vertex array .else use the begin end method.
second my video driver has a function " void drawQuadList(Quad * list,int length)" and draw the quad list .
Code: [Select]
// create the buffer
if (rm == RenderMethod.RM_BUFFER)
{
glGenBuffers(1,&buffer);
glBindBuffer(GL_ARRAY_BUFFER,buffer);
// pre-alloc the buffer
bufferSize = RM_BUFFER_SIZE;
glBufferData(GL_ARRAY_BUFFER,bufferSize,null,GL_STREAM_DRAW);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
Vertex v;
glVertexPointer(3,GL_FLOAT,Vertex.sizeof,cast(void *) (0 ));
glColorPointer(4,GL_FLOAT,Vertex.sizeof,cast(void *)( cast(ubyte *)(&(v.r)) - cast(ubyte *)(&(v.x)) ) );
glTexCoordPointer(2,GL_FLOAT,Vertex.sizeof,cast(void *) ( cast(ubyte*)(&(v.tx)) - cast(ubyte *)(&(v.x)) ) );
}
else if (rm == RenderMethod.RM_VERTEXARRAY)
{
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
}
else if (rm == RenderMethod.RM_BEGINEND)
{
}
//videoDriver's draw
void drawQuadList(Quad * list,int length)
{
if (rm == RenderMethod.RM_BUFFER)
{
//glBufferData(GL_ARRAY_BUFFER,length * Quad.sizeof,cast(void*)list,GL_STREAM_DRAW);
if (length * Quad.sizeof > bufferSize)
{
bufferSize = length * Quad.sizeof;
glBufferData(GL_ARRAY_BUFFER,bufferSize,null,GL_STREAM_DRAW);
}
glBufferSubData(GL_ARRAY_BUFFER,0,length * Quad.sizeof,cast(void *)list);
glDrawArrays(GL_QUADS,0,length * 4);
}
else if (rm == RenderMethod.RM_VERTEXARRAY)
{
glVertexPointer(3,GL_FLOAT,Vertex.sizeof,cast(void *)(&(list[0].points[0].x)));
glColorPointer(4,GL_FLOAT,Vertex.sizeof,cast(void *)(&(list[0].points[0].r)));
glTexCoordPointer(2,GL_FLOAT,Vertex.sizeof,cast(void *)(&(list[0].points[0].tx)));
glDrawArrays(GL_QUADS,0,length * 4);
}
else if (rm == RenderMethod.RM_BEGINEND)
{
glBegin (GL_QUADS);
int i;
for (i = 0; i < length; i++)
{
int j;
for (j = 0; j < 4; j ++ )
{
glTexCoord2f(list[i].points[j].tx,list[i].points[j].ty);
glColor4f(list[i].points[j].r,list[i].points[j].g,list[i].points[j].b,list[i].points[j].a);
glVertex3f(list[i].points[j].x,list[i].points[j].y,list[i].points[j].z);
}
}
glEnd ();
}
}
third I use a 2D render to render the quads
Code: [Select]
// quad's render
void renderEx(float x,float y,float angle,float scale)
{
Quad q = quad;
for (int i = 0; i < 4; i ++)
{
// scale
q.points[i].x *= scale;
q.points[i].y *= scale;
// rotate
float tempx = q.points[i].x;
float tempy = q.points[i].y;
q.points[i].x = cos(angle) * tempx - sin(angle) * tempy;
q.points[i].y = sin(angle) * tempx + cos(angle) * tempy;
// translate
q.points[i].x += x;
q.points[i].y += y;
}
SceneRenderer.getInstance().renderQuad(q,texture);
}
// scenerenderer's renderQuad
void renderQuad(in Quad q,Texture t)
{
if ((currentTexture !is t) || bufferSize >= BUFFER_LENGTH)
{
currentTexture = t;
renderBuffer();
videoDriver.applyTexture(t);
}
buffer[bufferSize] = q;
bufferSize ++;
}
void beginRender()
{
currentTexture = null;
}
void endRender()
{
renderBuffer();
}
// render buffer
void renderBuffer()
{
if (bufferSize > 0)
{
videoDriver.drawQuadList(cast(Quad *)buffer,bufferSize);
}
bufferSize = 0;
}
hope my code helps. And you can see HGE as a example for quad accumulate method .