In the LWJGL, I have a program that renders a chunk of 16x16x16 voxels. I want to implement an optimization where the faces of the voxel only render if they are exposed to air. I know there must be 3 steps to this:
- assign the vertices to their appropriate face.
- make the voxel read whats around.
- remove face depending whether there's a voxel or not.
I dont know how I could implement this though.
Main class
package main;
import org.lwjgl.glfw.GLFW;
import engine.graphics.Block;
import engine.graphics.Renderer;
import engine.graphics.Shader;
import engine.io.Input;
import engine.io.Window;
import engine.maths.Vector3f;
import engine.objects.Camera;
import engine.objects.GameObject;
public class Main implements Runnable {
public final static int CHUNK_SIZE= 16;
public Thread game;
public Window window;
public Renderer renderer;
public Shader shader;
public Block block;
public final int WIDTH = 1280, HEIGHT = 760;
public GameObject [][][] objects;
public GameObject object = new GameObject(new Vector3f(0, 0, 0), new Vector3f(0, 0, 0), new Vector3f(1, 1, 1), Block.mesh);
public Camera camera =new Camera(new Vector3f(0, 0, 1), new Vector3f(0, 0, 0));
public void start() {
game = new Thread(this, "game");
game.start();
}
public void init() {
//System.out.println("Starting Game");
window = new Window(WIDTH, HEIGHT, "GAME");
shader = new Shader("/shaders/mainVertex.glsl", "/shaders/mainFragment.glsl");
renderer = new Renderer(window, shader);
window.setBackgroundColor(.49f , .73f, .91f);
window.create();
Block.mesh.create();
shader.create();
objects = new GameObject[CHUNK_SIZE][CHUNK_SIZE][CHUNK_SIZE];
for (int x = 0; x < CHUNK_SIZE; x++) {
for (int y = 0; y < CHUNK_SIZE; y++) {
for (int z = 0; z < CHUNK_SIZE; z++) {
objects[x][y][z] = new GameObject(new Vector3f(x, y, z), new Vector3f(0, 0, 0), new Vector3f(1, 1, 1), Block.mesh);
}
}
}
}
public void run() {
init();
while (!window.shouldClose() && !Input.iskeyDown(GLFW.GLFW_KEY_ESCAPE)) {
update();
render();
if (Input.iskeyDown(GLFW.GLFW_KEY_F11)) window.setFullscreen( !window.isFullscreen());
if (Input.isButtonDown(GLFW.GLFW_MOUSE_BUTTON_LEFT)) window.mouseState(true);
}
close();
}
private void update() {
window.update();
camera.update();
}
private void render() {
for (int x = 0; x < CHUNK_SIZE; x++) {
for (int y = 0; y < CHUNK_SIZE; y++) {
for (int z = 0; z < CHUNK_SIZE; z++) {
renderer.renderMesh(objects[x][y][z], camera);
}
}
}
renderer.renderMesh(object, camera);
window.swapbuffers();
}
private void close() {
window.destroy();
Block.mesh.destroy();
shader.destroy();
}
public static void main(String[] args) {
new Main().start();
}
}
Block class
package engine.graphics;
import engine.maths.Vector2f;
import engine.maths.Vector3f;
public class Block {
public static Vector3f[] PX_POS = {
new Vector3f(-0.5f, 0.5f, -0.5f),
new Vector3f(-0.5f, -0.5f, -0.5f),
new Vector3f( 0.5f, -0.5f, -0.5f),
new Vector3f( 0.5f, -0.5f, -0.5f),
new Vector3f( 0.5f, 0.5f, -0.5f),
new Vector3f(-0.5f, 0.5f, -0.5f)
};
public static Vector3f[] NX_POS = {
new Vector3f(-0.5f, 0.5f, 0.5f),
new Vector3f(-0.5f, -0.5f, 0.5f),
new Vector3f( 0.5f, -0.5f, 0.5f),
new Vector3f( 0.5f, -0.5f, 0.5f),
new Vector3f( 0.5f, 0.5f, 0.5f),
new Vector3f(-0.5f, 0.5f, 0.5f)
};
public static Vector3f[] PY_POS = {
new Vector3f( 0.5f, 0.5f, -0.5f),
new Vector3f( 0.5f, -0.5f, -0.5f),
new Vector3f( 0.5f, -0.5f, 0.5f),
new Vector3f( 0.5f, -0.5f, 0.5f),
new Vector3f( 0.5f, 0.5f, 0.5f),
new Vector3f( 0.5f, 0.5f, -0.5f)
};
public static Vector3f[] NY_POS = {
new Vector3f(-0.5f, 0.5f, -0.5f),
new Vector3f(-0.5f, -0.5f, -0.5f),
new Vector3f(-0.5f, -0.5f, 0.5f),
new Vector3f(-0.5f, -0.5f, 0.5f),
new Vector3f(-0.5f, 0.5f, 0.5f),
new Vector3f(-0.5f, 0.5f, -0.5f)
};
public static Vector3f[] PZ_POS = {
new Vector3f(-0.5f, 0.5f, 0.5f),
new Vector3f(-0.5f, 0.5f, -0.5f),
new Vector3f( 0.5f, 0.5f, -0.5f),
new Vector3f( 0.5f, 0.5f, -0.5f),
new Vector3f( 0.5f, 0.5f, 0.5f),
new Vector3f(-0.5f, 0.5f, 0.5f)
};
public static Vector3f[] NZ_POS = {
new Vector3f(-0.5f, -0.5f, 0.5f),
new Vector3f(-0.5f, -0.5f, -0.5f),
new Vector3f( 0.5f, -0.5f, -0.5f),
new Vector3f( 0.5f, -0.5f, -0.5f),
new Vector3f( 0.5f, -0.5f, 0.5f),
new Vector3f(-0.5f, -0.5f, 0.5f)
};
public static Vector2f[] UV = {
new Vector2f(0.f, 0.f),
new Vector2f(0.f, 1.f),
new Vector2f(1.f, 1.f),
new Vector2f(1.f, 1.f),
new Vector2f(1.f, 0.f),
new Vector2f(0.f, 0.f)
};
public static Vector3f[] NORMALS = {
new Vector3f(0.f, 0.f, 0.f),
new Vector3f(0.f, 0.f, 0.f),
new Vector3f(0.f, 0.f, 0.f),
new Vector3f(0.f, 0.f, 0.f),
new Vector3f(0.f, 0.f, 0.f),
new Vector3f(0.f, 0.f, 0.f)
};
public static Mesh mesh = new Mesh(new Vertex[] {
//Back face
new Vertex(new Vector3f(-0.5f, 0.5f, -0.5f), new Vector2f(0.0f, 0.0f)),
new Vertex(new Vector3f(-0.5f, -0.5f, -0.5f), new Vector2f(0.0f, 0.5f)),
new Vertex(new Vector3f( 0.5f, -0.5f, -0.5f), new Vector2f(0.5f, 0.5f)),
new Vertex(new Vector3f( 0.5f, 0.5f, -0.5f), new Vector2f(0.5f, 0.0f)),
//Front face
new Vertex(new Vector3f(-0.5f, 0.5f, 0.5f), new Vector2f(0.0f, 0.0f)),
new Vertex(new Vector3f(-0.5f, -0.5f, 0.5f), new Vector2f(0.0f, 0.5f)),
new Vertex(new Vector3f( 0.5f, -0.5f, 0.5f), new Vector2f(0.5f, 0.5f)),
new Vertex(new Vector3f( 0.5f, 0.5f, 0.5f), new Vector2f(0.5f, 0.0f)),
//Right face
new Vertex(new Vector3f( 0.5f, 0.5f, -0.5f), new Vector2f(0.0f, 0.0f)),
new Vertex(new Vector3f( 0.5f, -0.5f, -0.5f), new Vector2f(0.0f, 0.5f)),
new Vertex(new Vector3f( 0.5f, -0.5f, 0.5f), new Vector2f(0.5f, 0.5f)),
new Vertex(new Vector3f( 0.5f, 0.5f, 0.5f), new Vector2f(0.5f, 0.0f)),
//Left face
new Vertex(new Vector3f(-0.5f, 0.5f, -0.5f), new Vector2f(0.0f, 0.0f)),
new Vertex(new Vector3f(-0.5f, -0.5f, -0.5f), new Vector2f(0.0f, 0.5f)),
new Vertex(new Vector3f(-0.5f, -0.5f, 0.5f), new Vector2f(0.5f, 0.5f)),
new Vertex(new Vector3f(-0.5f, 0.5f, 0.5f), new Vector2f(0.5f, 0.0f)),
//Top face
new Vertex(new Vector3f(-0.5f, 0.5f, 0.5f), new Vector2f(0.5f, 0.0f)),
new Vertex(new Vector3f(-0.5f, 0.5f, -0.5f), new Vector2f(0.5f, 0.5f)),
new Vertex(new Vector3f( 0.5f, 0.5f, -0.5f), new Vector2f(1.0f, 0.5f)),
new Vertex(new Vector3f( 0.5f, 0.5f, 0.5f), new Vector2f(1.0f, 0.0f)),
//Bottom face
new Vertex(new Vector3f(-0.5f, -0.5f, 0.5f), new Vector2f(0.0f, 0.5f)),
new Vertex(new Vector3f(-0.5f, -0.5f, -0.5f), new Vector2f(0.0f, 1.0f)),
new Vertex(new Vector3f( 0.5f, -0.5f, -0.5f), new Vector2f(0.5f, 1.0f)),
new Vertex(new Vector3f( 0.5f, -0.5f, 0.5f), new Vector2f(0.5f, 0.5f)),
}, new int[] {
//Back face
0, 3, 1,
1, 3, 2,
//Front face
4, 5, 7,
7, 5, 6,
//Right face
8, 11, 9,
9, 11, 10,
//Left face
12, 13, 15,
15, 13, 14,
//Top face
16, 19, 17,
17, 19, 18,
//Bottom face
20, 21, 23,
23, 21, 22
}, new Material("/textures/Grass.png"));
//public GameObject object = new GameObject(new Vector3f(0, 0, 0), new Vector3f(0, 0, 0), new Vector3f(1, 1, 1), mesh);
public void create() {
}
public void destroy() {
}
}