Which way is faster?

Started by
14 comments, last by markr 18 years, 10 months ago
Hiya, Which is faster? I think the 2nd one would be, but my FPS doesn't show much difference.
//Draw players
	glLoadIdentity();
	glBegin(GL_QUADS);
		//player 1
		glTexCoord2f(ent1->GetImage().left, ent1->GetImage().bottom);	glVertex2f((float)(p1_x * TILE_SIZE), (float)(p1_y * TILE_SIZE));							// Bottom Left Of The Texture and Quad
		glTexCoord2f(ent1->GetImage().right, ent1->GetImage().bottom);	glVertex2f((float)(p1_x * TILE_SIZE + TILE_SIZE), (float)(p1_y * TILE_SIZE));				// Bottom Right Of The Texture and Quad
		glTexCoord2f(ent1->GetImage().right, ent1->GetImage().top);		glVertex2f((float)(p1_x * TILE_SIZE + TILE_SIZE), (float)(p1_y * TILE_SIZE - TILE_SIZE));	// Top Right Of The Texture and Quad
		glTexCoord2f(ent1->GetImage().left, ent1->GetImage().top);		glVertex2f((float)(p1_x * TILE_SIZE), (float)(p1_y * TILE_SIZE - TILE_SIZE));				// Top left of the texture and quad

		//player 2
		glTexCoord2f(ent2->GetImage().left, ent2->GetImage().bottom);	glVertex2f((float)(p2_x * TILE_SIZE), (float)(p2_y * TILE_SIZE));							// Bottom Left Of The Texture and Quad
		glTexCoord2f(ent2->GetImage().right, ent2->GetImage().bottom);	glVertex2f((float)(p2_x * TILE_SIZE + TILE_SIZE), (float)(p2_y * TILE_SIZE));				// Bottom Right Of The Texture and Quad
		glTexCoord2f(ent2->GetImage().right, ent2->GetImage().top);		glVertex2f((float)(p2_x * TILE_SIZE + TILE_SIZE), (float)(p2_y * TILE_SIZE - TILE_SIZE));	// Top Right Of The Texture and Quad
		glTexCoord2f(ent2->GetImage().left, ent2->GetImage().top);		glVertex2f((float)(p2_x * TILE_SIZE), (float)(p2_y * TILE_SIZE - TILE_SIZE));				// Top left of the texture and quad
	glEnd();
OR
	//Draw players
	Sprite ent1Spr = ent1->GetImage();
	Sprite ent2Spr = ent2->GetImage();

	glLoadIdentity();
	glBegin(GL_QUADS);
		//player 1
		glTexCoord2f(ent1Spr.left, ent1Spr.bottom);		glVertex2f((float)(p1_x * TILE_SIZE), (float)(p1_y * TILE_SIZE));							// Bottom Left Of The Texture and Quad
		glTexCoord2f(ent1Spr.right, ent1Spr.bottom);	glVertex2f((float)(p1_x * TILE_SIZE + TILE_SIZE), (float)(p1_y * TILE_SIZE));				// Bottom Right Of The Texture and Quad
		glTexCoord2f(ent1Spr.right, ent1Spr.top);		glVertex2f((float)(p1_x * TILE_SIZE + TILE_SIZE), (float)(p1_y * TILE_SIZE - TILE_SIZE));	// Top Right Of The Texture and Quad
		glTexCoord2f(ent1Spr.left, ent1Spr.top);		glVertex2f((float)(p1_x * TILE_SIZE), (float)(p1_y * TILE_SIZE - TILE_SIZE));				// Top left of the texture and quad

		//player 2
		glTexCoord2f(ent2Spr.left, ent2Spr.bottom);		glVertex2f((float)(p2_x * TILE_SIZE), (float)(p2_y * TILE_SIZE));							// Bottom Left Of The Texture and Quad
		glTexCoord2f(ent2Spr.right, ent2Spr.bottom);	glVertex2f((float)(p2_x * TILE_SIZE + TILE_SIZE), (float)(p2_y * TILE_SIZE));				// Bottom Right Of The Texture and Quad
		glTexCoord2f(ent2Spr.right, ent2Spr.top);		glVertex2f((float)(p2_x * TILE_SIZE + TILE_SIZE), (float)(p2_y * TILE_SIZE - TILE_SIZE));	// Top Right Of The Texture and Quad
		glTexCoord2f(ent2Spr.left, ent2Spr.top);		glVertex2f((float)(p2_x * TILE_SIZE), (float)(p2_y * TILE_SIZE - TILE_SIZE));				// Top left of the texture and quad
	glEnd();
Advertisement
Maybe you dont see a difference because your compiler already made that optimization.
Go on an Intense Rampage
And even if the compiler didn't already make it, that is such a minor optimization, it won't really affect performance at all.
Not to mention that optimization is completely useless in the face of the fact that you're using glBegin/End/Vertex/etc.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
It's pointless to optimize Immediate OpenGL. Step into Vertex Arrays. (Do a search on Vertex Arrays on that page and read up on it).

Actually, I take that back, it's not pointless to optimize any OpenGL, but optimizing OpenGL is more about sorting data to minimize state changes rather than finding superfast commands.

Whenever you glEnable, glDisable, glBind, glBegin and glEnd, you take a performance hit. Getting the OpenGL state machine in that sweet spot where you put the most out on the screen while call those commands the least is your optimizing dream.
Correct me if I am wrong, but all you did is create temporary variables to replace all those references you were using?

That seems like less of a performance issue and more of a readability issue.
Thnaks for the replies.
I only asked because it was beat into my throughout college that optimization is key. I figured 10 calls to a class method was more costly than creating two temp variables once.

I will do my best to optimize the gl calls however.
If I am drawing quads throughout the Render method, I should only ever need one glBegin/glEnd call right?
It was beat into you that optimization is the key?

I recommend a long period of recuperation, possibly involving self flagelation, to beat it back out again...

"Premature optimization is the root of all evil."

"More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity."

"There are three rules of optimization:
1) Don't optimize.
2) Don't optimize yet.
3) Don't optimize unless a profiler specifically tells you to do so."

"80% of your code is run 20% of the time" (and optimizing that 80% is therefore pointless).

And many more quotes... and threads... both in gamedev and elsewhere... I was thinking all this even as I read your subject...



Perfectly understood, and I wish you were there in class as they taught us to code in this fashion. We could have used another person questioning their optimization lessons.
Quote:
"There are three rules of optimization:
1) Don't optimize.
2) Don't optimize yet.
3) Don't optimize unless a profiler specifically tells you to do so."


The two rules of optimization:
1) Don't optimize
2) ( * For Experts Only * ) Don't optimize yet

This topic is closed to new replies.

Advertisement