Click here to Skip to main content
15,886,071 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I'm building a game based around the graphics style of minecraft to start with (simple shapes to learn the basics :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 :)
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:
Java
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:
Java
public void render() {
	for(Entry<Short, Block> entry : blockMap.entrySet()) {
		entry.getValue().render();
	}
}

And for the blocks:
Java
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:
Java
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

1 solution

I'm not into OpenGL - but how about some Thread Handling[^]?

Multithreading Tutorial[^]
 
Share this answer
 
Comments
Johnnei 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900