Reputation: 2045
I've just started learning LWJGL (Java OpenGL), and i'm encountering an enormous amount of lag on my program.
Basically, I'm trying to make a 2.5D Minecraft clone, so what I've done for the world code is create a multidimensional array of the "Block" class i made Its 150 By 50 . Here's the code for my world class so far:
And here is my render loop:
Some extra info, all the draw method for the "Block" class does is draw all the sides of a cube, except for the back.
So, what is happening is that it is rendering a big wall of cubes (What i wanted), except that the FPS is VERY bad.
I'm not quite sure what I'm doing wrong. And remember that I need an array of cubes because since it's going to be like Minecraft, i'm going to have to be able to easily organize the blocks, and be able to destroy/place blocks depending on what the user does.
Thanks!
Edit: Draw Method For The Block Class:
Texture stone = Util.createTexture("Tiles/Stone.png");
stone.bind();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBegin(GL_QUADS);
//FRONT FACE
glTexCoord3f(0,1,z);
glVertex3f(x,y,z);
glTexCoord3f(1,1,z);
glVertex3f(x + width,y,z);
glTexCoord3f(1,0,z);
glVertex3f(x + width,y + height,z);
glTexCoord3f(0,0,z);
glVertex3f(x,y + height, z);
//RIGHT FACE
glTexCoord3f(0,1,z);
glVertex3f(x + width,y,z);
glTexCoord3f(1,1,z - length);
glVertex3f(x + width,y,z - length);
glTexCoord3f(1,0,z - length);
glVertex3f(x + width,y + height,z - length);
glTexCoord3f(0,0,z);
glVertex3f(x + width,y + height,z);
//LEFT FACE
glTexCoord3f(0,1,z);
glVertex3f(x,y,z);
glTexCoord3f(1,1,z - length);
glVertex3f(x,y,z - length);
glTexCoord3f(1,0,z - length);
glVertex3f(x,y + height,z - length);
glTexCoord3f(0,0,z);
glVertex3f(x,y + height,z);
//TOP FACE
glTexCoord3f(0,1,z - length);
glVertex3f(x,y + height,z - length);
glTexCoord3f(1,1,z - length);
glVertex3f(x + width,y + height,z - length);
glTexCoord3f(1,0,z);
glVertex3f(x + width,y + height,z);
glTexCoord3f(0,0,z);
glVertex3f(x,y + height,z);
//BOTTOM FACE
glTexCoord3f(0,1,z - length);
glVertex3f(x,y,z - length);
glTexCoord3f(1,1,z - length);
glVertex3f(x + width,y,z - length);
glTexCoord3f(1,0,z);
glVertex3f(x + width,y,z);
glTexCoord3f(0,0,z);
glVertex3f(x,y,z);
glEnd();
Upvotes: 1
Views: 2007
Reputation: 420
You shouldn't create the texture every time block.draw() is called. Call it once, before you do any rendering.
Texture stone = Util.createTexture("Tiles/Stone.png");
It would also improve your performance to use vertex arrays instead of immediate mode drawing.
Upvotes: 5