Skip to main content
GameDev.net gamedev.net
🔒 Locked

2D OpenGL rendering slowly

Started by Blackstream Oct 4, 2007 at 12:55 PM 4 replies 1.7k views
Original Post
Blackstream
Blackstream
Hi all, I'm not sure what exactly I'm doing wrong. I'm trying to make a 2d engine in opengl right now. My video card is kinda crappy (a pci gforce fx 5200), but still, I don't think it should be going this slow. Bascially, I'm just rendering the same image and covering the screen with it, and to try and stress it, do that several times per frame. As it stands now, I can only layer on 1-10 times depending on the sprite size before the program slows down. If it's a big image, I can layer about 10 times, and if it's a small image (I went down to 5x5), even 1 full layer slows down a lot. Per second wise, I get about 30k pictures per second for the big pictures (64x64) and about 200k pictures per second for the small pictures (10x10 and 5x5). I've tried display lists, I've tried to limit the number of calls to bind texture, and they make literally no difference on my fps. Here is my code to draw a sprite

void bstSprite::drawSprite(float x, float y, float layer, 
			   float scale_x, float scale_y, float turnDegree,
			   float *hotspot_x, float *hotspot_y,
			   rgbaIntensity *rgba, txtCoords *tc) {
	//Get our draw coordinates
	float drawWidth, drawHeight;
	float left, top, right, bottom;

	if(tc != NULL) {
		//If we have texture coordinates, get from there
		drawWidth = (tc->right-tc->left)*trueWidth;
		drawHeight = (tc->bottom-tc->top)*trueHeight;
		left   = tc->left;
		top    = tc->top;
		right  = tc->right;
		bottom = tc->bottom;
	} else {
		//Otherwise assume they want the full image.
		txtCoords *tcFull = makeTxtCoords(0, 0, width-1, height-1);
		drawWidth=width;
		drawHeight=height;
		left   = tcFull->left;
		top    = tcFull->top;
		right  = tcFull->right;
		bottom = tcFull->bottom;
		delete tcFull;
	}
	
	//Save our current state
	glPushMatrix();

	//Set color, if passed
	if(rgba != NULL) {
		glColor4f(rgba->redI, rgba->greenI, rgba->blueI, rgba->alphaI);
	}

	//Move to position
	glTranslatef(x, y, layer);

	//Translate to the center of the picture
	if(hotspot_x != NULL && hotspot_y != NULL) {
		glTranslatef((*hotspot_x)*scale_x, (*hotspot_y)*scale_y, 0);
	} else {
		glTranslatef(drawWidth/2*scale_x, drawHeight/2*scale_y, 0);
	}

	//Rotate
	glRotatef(turnDegree, 0.0f, 0.0f, 1.0f);

	//Go up to our new upperleft corner so that we can draw
	if(hotspot_x != NULL && hotspot_y != NULL) {
		glTranslatef(-(*hotspot_x)*scale_x, -(*hotspot_y)*scale_y, 0);
	} else {
		glTranslatef(-drawWidth/2*scale_x, -drawHeight/2*scale_y, 0);
	}  
	//Prepare the texture
	glBindTexture(GL_TEXTURE_2D, texSprite);

	//Now draw!!1
	glBegin(GL_QUADS);
		glTexCoord2f(left, top);
		glVertex2d(0, 0);

		glTexCoord2f(left, bottom);
		glVertex2d(0, drawHeight * scale_y);

		glTexCoord2f(right, bottom);
		glVertex2d(drawWidth * scale_x, drawHeight * scale_y);

		glTexCoord2f(right, top);
		glVertex2d(drawWidth * scale_x, 0);
	glEnd();

	//Go back to our original state
	glPopMatrix();
}


And here is my code to layer them on the screen (just quick test code, but it might be important).

for(r = 0; r < 10; r++) {
 for(x = 0; x < 640; x = x+s->getWidth()) {
  for(y = 0; y < 480; y = y+s->getHeight()) {
    s->drawSprite(x,y,1,1,1,a,NULL,NULL,&rgbaWhite,NULL);
  }
 }
}


Any help would be appreciated.
-Blackstream Will you, won't you, will you, won't you, won't you take my virus?-The Mad HackerBlackstream's Webpage
Blackstream
Blackstream
Well it's just one image (and I do plan on using sprite sheets, I have texture coordinate functionality built in). But regardless, I don't think it's that because I can move the outer part of that loop

[source="cpp"]for(r = 0; r < 10; r++) {}


surround it around the part of my code that draws the sprite

[source="cpp"]//Now draw!!1for(r = 0; r < 10; r++) {glBegin(GL_QUADS);	glTexCoord2f(left, top);	glVertex2d(0, 0);	glTexCoord2f(left, bottom);	glVertex2d(0, drawHeight * scale_y);	glTexCoord2f(right, bottom);	glVertex2d(drawWidth * scale_x, drawHeight * scale_y);	glTexCoord2f(right, top);	glVertex2d(drawWidth * scale_x, 0);glEnd();}


And there is literally no change in my draw speed, even though I'm now calling the bindtexture 10 times less.

EDIT:
The guy whose post I was replying to disappeared o_O
-Blackstream Will you, won't you, will you, won't you, won't you take my virus?-The Mad HackerBlackstream's Webpage
Cranky
Cranky
Quote:
Per second wise, I get about 30k pictures per second for the big pictures (64x64) and about 200k pictures per second for the small pictures (10x10 and 5x5).

You have an fps of 30000-200000 and talk about slow downs?
Or to what is that "k" referring to? Or do you mean that you render that amount of sprites to the screen in one second?

If you fps are really that high try to render a lot of triangles at once (30k triangles?) until yout fps really drops, then you can try to optimize your code, as then you'll be able to tell better whether your program performs better.

Also try using vertex buffer object's. glVertex* functions are pretty slow, although it should be fine for any 2d rendering...
Blackstream
Blackstream
Quote:
Original post by Cranky
Quote:
Per second wise, I get about 30k pictures per second for the big pictures (64x64) and about 200k pictures per second for the small pictures (10x10 and 5x5).

You have an fps of 30000-200000 and talk about slow downs?
Or to what is that "k" referring to? Or do you mean that you render that amount of sprites to the screen in one second?

Yeah, it's number of sprites rendering to my screen. My fps is usually somewhere in the teens at the limits I mentioned. If I was getting 200k fps, I'd be patting myself on the back right about now :p

More specifically, say I've got a 5x5 sprite, and I'm laying it over a 640x480 window (fullscreen vs windowed makes no difference either btw). I can draw 12,228 sprites per frame, and about 214k sprite per second, which is about 17.6 fps. Since with that setup I'm only covering the screen once, it feels like I'm probably doing something wrong.

Quote:
Also try using vertex buffer object's. glVertex* functions are pretty slow, although it should be fine for any 2d rendering...

I'll look into that, but I have a feeling I'm missing something more fundamental here that's causing this.

[Edited by - Blackstream on October 4, 2007 2:44:27 PM]
-Blackstream Will you, won't you, will you, won't you, won't you take my virus?-The Mad HackerBlackstream's Webpage
dpadam450
dpadam450
There's no problem here. The data bus to the GPU isnt that fast. Your sending:

30,000 (sprites) x 4(vertices) x 4(texcoords)....... = shitload of data sent per-frame to the GPU.

Use VBO's, but either way, 30,000 sprites is pretty ridiculous seriously. If its for particles, if you study current games when u play them, you will notice they dont use many 2d sprites at all.

And p.s. 5200FX is a piece of shit. seriously terrible.
NBA2K, Madden, Maneater, Killing Floor, Sims 
Blackstream
Blackstream
Quote:

And p.s. 5200FX is a piece of shit. seriously terrible.

Believe me, I know. I used to have a 7900 GT, but it died on me and I had to return it and put back in my old fx 5200, and I haven't been in a good place to get a new card since then (I will in about a week though). I just thought that I should be able to do better, but it's good to know that it's good as is.

Anyways, I'll look into VBOs and see if I can make them work.
-Blackstream Will you, won't you, will you, won't you, won't you take my virus?-The Mad HackerBlackstream's Webpage

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.