Chapter 4-The Render Class

Because having everything in the main class is mind-numbingly painful.

So, how are we all feeling about our understanding of LWJGL? We feeling good, feeling sassy? Alright, let's get ready to move a chunk of the stuff we wrote out of the Boot class. Why?

Because you never, ever, EVER keep all of your code in the main class. When you're sorting Legos for a big build, do you just dump everything into a pile? No, it's hard as hell. You sort things to some meaningful degree. Yellows with yellows, minifigs with minifigs, and with different piles for different parts of the build.

I've given you a starting point by using packages. By now you've noticed that anything that involves the render code goes into the root render package or one of it's children. Now, we're going to be making a new class in this package named Render. Can you guess what it's going to be used for?

Before we get started, you'll need to import the following:

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;

import tutorials.render.shader.ShaderTextured;

Now, right now this class is going to be very simple. Our engine is still in its infancy, and at the point where we can develop it into either a 2D or 3D game. To that end, we'll only need two methods: cleanup() and render(Mesh mesh). While we're at it, include a ShaderTextured in there too.

Render.java
ShaderTextured shader = new ShaderTextured();

public void cleanup(){
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT|GL11.GL_DEPTH_BUFFER_BIT);
}

public void render(){

}

We've already talked about the glClear(...) method before. As a reminder, every render cycle we need to erase everything that was drawn before, otherwise it just draws the objects over the previous frame. The result winds up being something a little like this:

Now, next we're going to be copying the rendering code from our main game loop and dropping it into the render(...) method.

Render.java
shader.start();
GL30.glBindVertexArray(mesh.getVaoID());
GL20.glEnableVertexAttribArray(0);
GL20.glEnableVertexAttribArray(1);
GL13.glActiveTexture(GL13.GL_TEXTURE0);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, mesh.getTexture());
GL11.glDrawElements(GL11.GL_TRIANGLES, mesh.getVertexCount(), GL11.GL_UNSIGNED_INT,0);
GL20.glDisableVertexAttribArray(0);
GL20.glDisableVertexAttribArray(1);
GL30.glBindVertexArray(0);
shader.stop();

Once again, we start our shader, bind the VAO for our mesh, enable the position and texture attributes, and set the active texture. Everything's pretty much the same with one change: our Mesh class has a new method, getTexture(). So, let's go back to the Mesh class and see what's changed:

Mesh.java
private int texture = 0;

//The rest of the Mesh class

public Mesh addTexture(String texture) {
		this.texture = Texture.loadTexture(texture);
		return this;
}

public int getTexture(){
		return this.texture;
}

So, we've added a new integer for the Mesh class that stores the texture of the mesh. The reason we have the addTexture(...) return a Mesh after setting the texture is so we can simple set the texture in the same line of code as our initialization for the Mesh. Since we already know how the getTexture() method works from its name and the other methods in the class, let's go back to the Boot class.

Mesh meshmeyek = MeshLoader.createMesh(vertices, UVs,indices).addTexture("coffee.png");
		
Render render = new Render();
		
while(!GLFW.glfwWindowShouldClose(window)) {
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT|GL11.GL_DEPTH_BUFFER_BIT);
			
		render.cleanup();
		render.render(meshmeyek);
			
		GLFW.glfwSwapBuffers(window);
		GLFW.glfwPollEvents();
}

That looks a LOT cleaner now, right? As I mentioned before, with the way the addTexture(...) method is set up, we can just add that to the end of our initialization for our mesh. With the Render class handling all of the render code now, we just need to tell it to cleanup() the last frame and render(...) the new one.

In the next chapter, we'll be further cleaning up our codebase by moving the GLFW code out of the Boot class.

Last updated