Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java OpenGL
Hello,
 
I'm building a game based around the graphics style of minecraft to start with (simple shapes to learn the basics Big Grin | :-D ). But I find myself in a big deal that the code is using awfully much resources (cpu). I've already spend quite some time trying to optimize the coding of the rendering but its just not enough ): In a perfect situation on a 16x16x64 block field (only need to render outsides) I can push a 70 fps. (i5 2500, ATI HD 6870 1GB, 8GB DDR3 ram).
 
Is there a way I can improve the coding to increase the framerate? I'm really new into OpenGL so I don't know that much about it yet. I hope I'll recieve some help Smile | :)
And now for the coding:
 
For the rendering I use a couple of part of code.
To start the main rendering of the chunks of the world:
public void render() {
	GL11.glEnable(GL_TEXTURE_2D);
	GL11.glDisable(GL11.GL_CULL_FACE); //I use this so when I rotate the camera the textures will remain visible
	Launcher.getTextureManager().getTexture("terrain").bind();
	for(Entry<Long, Chunk> entry : chunkMap.entrySet()) {
		entry.getValue().render(); //See Code block 2
	}
	GL11.glDisable(GL_TEXTURE_2D);
	GL11.glEnable(GL11.GL_CULL_FACE);
}
For the chunk rendering:
public void render() {
	for(Entry<Short, Block> entry : blockMap.entrySet()) {
		entry.getValue().render();
	}
}
And for the blocks:
public abstract boolean isTransparant(); //Each block type will define this
	
public boolean shouldRenderSide(byte side) {
	try {
		if(side == RenderStatic.TOP)
			return Launcher.getWorld().getBlockAt(x, y + 1, z).isTransparant();
		else if(side == RenderStatic.BOTTOM)
			return Launcher.getWorld().getBlockAt(x, y - 1, z).isTransparant();
		else if(side == RenderStatic.LEFT)
			return Launcher.getWorld().getBlockAt(x - 1 , y, z).isTransparant();
		else if(side == RenderStatic.RIGHT)
			return Launcher.getWorld().getBlockAt(x + 1, y, z).isTransparant();
		else if(side == RenderStatic.FRONT)
			return Launcher.getWorld().getBlockAt(x, y, z + 1).isTransparant();
		else if(side == RenderStatic.BACK)
			return Launcher.getWorld().getBlockAt(x, y, z - 1).isTransparant();
		else
			throw new RuntimeException("Illegal Side: (byte)" + side );
	} catch (NullPointerException e) {//Will get thrown if the getBlockAt doesn't exist
		return true;
	}
}
	
public boolean isConsealed() {
	try {
		boolean left = Launcher.getWorld().getBlockAt(x - 1, y, z).isTransparant();
		boolean right = Launcher.getWorld().getBlockAt(x + 1, y, z).isTransparant();
		boolean up = Launcher.getWorld().getBlockAt(x, y + 1, z).isTransparant();
		boolean down = Launcher.getWorld().getBlockAt(x, y - 1, z).isTransparant();
		boolean forward = Launcher.getWorld().getBlockAt(x, y, z + 1).isTransparant();
		boolean backward = Launcher.getWorld().getBlockAt(x, y, z - 1).isTransparant();
		if(left || right || up || down || forward || backward)
			return false;
		return true;
	} catch (NullPointerException e) { //Will get thrown if the getBlockAt doesn't exist
		return false;
	}
}
	
public boolean shouldRenderBlock() {
	if(!isTransparant() && !isConsealed())
		return true;
	return false;
}
	
public void render() {
	if(shouldRenderBlock())
		super.render();
}
And now for the actual rendering of the block:
private void renderSide(byte side) {
	if(!shouldRenderSide(side))
		return;
	float indexInTexture = getTextureForSide(side);
	float x = 0.0625F * indexInTexture;
	float xMax = 0.0625F * (1 + indexInTexture);
	float y = 0.0625F * ((int)(indexInTexture / 16));
	float yMax = y + 0.0625F;
	
	if(side == TOP) {
		glTexCoord2f(x, y); glVertex3f(0, height, 0);
		glTexCoord2f(x, yMax); glVertex3f(0, height, depth);
		glTexCoord2f(xMax, yMax); glVertex3f(width, height, depth);			
		glTexCoord2f(xMax, y); glVertex3f(width, height, 0);
	} else if (side == BOTTOM) {
		glTexCoord2f(x, y); glVertex3f(0, 0, 0);
		glTexCoord2f(x, yMax); glVertex3f(0, 0, depth);
		glTexCoord2f(xMax, yMax); glVertex3f(width, 0, depth);			
		glTexCoord2f(xMax, y); glVertex3f(width, 0, 0);
	} else if (side == BACK) {
		glTexCoord2f(x, y); glVertex3f(0, height, 0); //Bot Left
		glTexCoord2f(xMax, y); glVertex3f(width, height, 0); //Bot Right
		glTexCoord2f(xMax, yMax); glVertex3f(width, 0, 0); //Top Right
		glTexCoord2f(x, yMax); glVertex3f(0, 0, 0); //Top Left
	} else if (side == FRONT) {
		glTexCoord2f(x, y); glVertex3f(0, height, depth); //Bot Left
		glTexCoord2f(xMax, y); glVertex3f(width, height, depth); //Bot Right
		glTexCoord2f(xMax, yMax); glVertex3f(width, 0, depth); //Top Right
		glTexCoord2f(x, yMax); glVertex3f(0, 0, depth); //Top Left
	}  else if (side == LEFT) {
		glTexCoord2f(x, yMax); glVertex3f(0, 0, depth); //Back Top
		glTexCoord2f(x, y); glVertex3f(0, height, depth); //Back Bot
		glTexCoord2f(xMax, y); glVertex3f(0, height, 0); //Front Bot
		glTexCoord2f(xMax, yMax); glVertex3f(0, 0, 0); //Front Top
	}  else if (side == RIGHT) {
		glTexCoord2f(x, yMax); glVertex3f(width, 0, depth); //Back Top
		glTexCoord2f(x, y); glVertex3f(width, height, depth); //Back Bot
		glTexCoord2f(xMax, y); glVertex3f(width, height, 0); //Front Bot
		glTexCoord2f(xMax, yMax); glVertex3f(width, 0, 0); //Front Top
	} else
		throw new RuntimeException("Invalid Side to Render on RenderSquare((byte)" + side + ");");
}
	
public void render() {
	glPushMatrix();
	glTranslatef(getX(), getY(), getZ());
	glBegin(GL_QUADS);
	{
		renderSide(LEFT);
		renderSide(RIGHT);
		renderSide(BOTTOM);
		renderSide(TOP);
		renderSide(FRONT);
		renderSide(BACK);			
	}
	glEnd();
	glPopMatrix();
}
 
Greetings,
Johnnei
Posted 14-Apr-12 0:02am
Johnnei231

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I'm not into OpenGL - but how about some Thread Handling[^]?
 
Multithreading Tutorial[^]
  Permalink  
Comments
Johnnei at 14-Apr-12 6:55am
   
I might have a look to seperate the updating from rendering, but I'm not confinced this will result in a much higher fps. But it's worth a try ^.^
Johnnei at 14-Apr-12 10:49am
   
Tried a couple of things. Result: If I make OpenGL calls from the update thread it just wont work. So I gotta have it single-threaded poorly ):
After some more trying I got everything working with 2 threads, result: no increase of fps ):

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 240
1 Kamal Rocks 184
2 BillWoodruff 173
3 PIEBALDconsult 160
4 CPallini 155
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 14 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100