Tiles & open gL?

Started by
5 comments, last by DevLiquidKnight 20 years, 10 months ago
I making a 2D tile based game and I am finding it is extremely slow when I load the tiles I dont know what im doing wrong ot load the tiles i use a for loop which creates a bunch of square planes using the glVertex3f and such. After its done and i load it the FPS drop to like really low. What am i doing wrong is there some other way of loading 2d tiles into open gl I dont understand what im doing wrong :/ lol any help would be nice! Thanks! coder requires 0xf00d before continue().
Killer Eagle Software
Advertisement
You say loading, but you mean rendering, right? Without any code to see, it will be hard to know what the problem is. How about posting the problem code so someone can try to help you?
This is an example of how im doing the tiles :/for(int x=0; x<=100; x++){for(int y=0; y<=100; y++){x1=x/x+5;y1=y/y+5;glBegin(GL_QUADS);glTexCoord2f(0.0f, 1.0f); glVertex3f(-x1, y1, 0.0f);glTexCoord2f(1.0f, 1.0f); glVertex3f( x1, y1, 0.0f);		glTexCoord2f(1.0f, 0.0f); glVertex3f( x1,-y1, 0.0f);glTexCoord2f(0.0f, 0.0f); glVertex3f(-x1,-y1, 0.0f);glEnd();}} 


coder requires 0xf00d before continue().

Killer Eagle Software
Without really changing anything, it''d be better like this:
glBegin(GL_QUADS)for(unsigned int x=0; x<=100; ++x) {  x1=x/x+5; // I''ll assume your x1 & y1 stuff is correct  for(unsigned int y=0; y<=100; ++y) {      y1=y/y+5;    glTexCoord2f(0.0f, 1.0f); glVertex2f(-x1, y1);    glTexCoord2f(1.0f, 1.0f); glVertex2f( x1, y1);		    glTexCoord2f(1.0f, 0.0f); glVertex2f( x1,-y1);    glTexCoord2f(0.0f, 0.0f); glVertex2f(-x1,-y1);  }}glEnd();

That should be enormously faster if you''re not fillrate limited. However, that still is far from optimized. Here it is with client side vertex arrays:
// NOTE: DO THIS PART ONLY ONCE, STORE IT SOMEWHEREconst unsigned int x_max = 100; // I dislike your <= stuff,const unsigned int y_max = 100; // but, whatever =P...GLfloat vert_array[(x_max + 1) * (y_max + 1) * 4][2];GLfloat texc_array[(x_max + 1) * (y_max + 1) * 4][2];for(int x=0; x<=x_max; ++x) {  const unsigned int offset = y_max * x;  x1=x/x+5; // Same assumption as above  for(int y=0; y<=y_max; ++y) {      y1=y/y+5;    // Hopefully I didn''t screw this part up;    // you''ll get the idea either way...    texc_array[(offset + y) * 4 + 0][0] = 0.0f;    texc_array[(offset + y) * 4 + 0][1] = 1.0f;    vert_array[(offset + y) * 4 + 0][0] = -x1;    vert_array[(offset + y) * 4 + 0][1] =  y1;    texc_array[(offset + y) * 4 + 1][0] = 1.0f;    texc_array[(offset + y) * 4 + 1][1] = 1.0f;    vert_array[(offset + y) * 4 + 1][0] = -x1;    vert_array[(offset + y) * 4 + 1][1] =  y1;    texc_array[(offset + y) * 4 + 2][0] = 1.0f;    texc_array[(offset + y) * 4 + 2][1] = 0.0f;    vert_array[(offset + y) * 4 + 2][0] = -x1;    vert_array[(offset + y) * 4 + 2][1] =  y1;    texc_array[(offset + y) * 4 + 3][0] = 0.0f;    texc_array[(offset + y) * 4 + 3][1] = 0.0f;    vert_array[(offset + y) * 4 + 3][0] = -x1;    vert_array[(offset + y) * 4 + 3][1] =  y1;  }}// NOTE: DO THIS TO RENDER THAT// (Push states if you want, or whatever)glVertexPointer(2, GL_FLOAT, 0, vert_array);glTexCoordPointer(2, GL_FLOAT, 0, texc_array);glEnableClientState(GL_VERTEX_ARRAY);glEnableClientState(GL_TEXTURE_COORD_ARRAY);glDrawArrays(GL_QUADS, 0, (x_max + 1) * (y_max + 1) * 4);

From there, if you''re geometry limited, you can continue to improve on this with VBO or something that enables server side caching of the data (or VAR or VAO if you want to be vendor specific: you don''t).

Thx that makes alot of sense now that i see it
heh your second optimization kinda bugs out on my compiler 0.0 because u cannot use a 0 in division

[edited by - DevLiquidKnight on June 3, 2003 2:04:14 PM]
quote:Original post by DevLiquidKnight
heh your second optimization kinda bugs out on my compiler 0.0 because u cannot use a 0 in division

I didn''t change your x1 and y1 related code, that''s your own fault. I was pretty sure it was incorrect, that''s why I put those comments in there.

This topic is closed to new replies.

Advertisement