Thursday, August 18, 2022
HomeGame Developmentrendering - LWJGL eradicating voxel faces that aren't seen

rendering – LWJGL eradicating voxel faces that aren’t seen


I’m merely making an attempt to attain eradicating voxel faces that aren’t seen.What I’ve coded merely renders a piece of 16x16x16. I have already got again face culling enabled, which it does work. in order I’m making an attempt to determine this out in regards to the faces, I figured that its the faces that by default are usually not speculated to render and solely render when there may be air as neighbor.versus render all by default until there is not air. I’m pondering that these two methods of wording could make up completely different algorithms and subsequently coding it many alternative methods. I simply do not know which is greatest. Either manner, I’m unsure the best way to organize my code to make this occur. I imagine it could be as a consequence of my code being messy and that I’m a newbie that has alot to be taught.

package deal principal;

import org.lwjgl.glfw.GLFW;

import engine.graphics.Material;
import engine.graphics.Mesh;
import engine.graphics.Renderer;
import engine.graphics.Shader;
import engine.graphics.Vertex;
import engine.io.Input;
import engine.io.Window;
import engine.maths.Vector2f;
import engine.maths.Vector3f;
import engine.objects.Camera;
import engine.objects.GameObject;


public class Main implements Runnable {
    public ultimate static int CHUNK_SIZE= 16;
    public Thread sport;
    public Window window;
    public Renderer renderer;
    public Shader shader;
    public ultimate int WIDTH = 1280, HEIGHT = 760;    
    public 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[][][] objects;    
    public GameObject object = new GameObject(new Vector3f(0, 0, 0), new Vector3f(0, 0, 0), new Vector3f(1, 1, 1), mesh);
    public Camera digicam =new Camera(new Vector3f(0, 0, 1), new Vector3f(0, 0, 0));
    
    public void begin() {
        sport = new Thread(this, "sport");
        sport.begin();
    }
    public  void init() {

        window = new Window(WIDTH, HEIGHT, "GAME");
        shader = new Shader("/shaders/principalVertex.glsl", "/shaders/mainFragment.glsl");
        renderer = new Renderer(window, shader);
        window.setBackgroundColor(.49f , .73f, .91f);
        window.create();
        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), mesh);
                }
            }
        }
        
    }
    public void run() {
        init();
        whereas (!window.shouldClose() && !Input.iskeyDown(GLFW.GLFW_KEY_ESCAPE)) {
            replace();
            render();
            if (Input.iskeyDown(GLFW.GLFW_KEY_F11)) window.setFullscreen( !window.isFullscreen());
            if (Input.isButtonDown(GLFW.GLFW_MOUSE_BUTTON_LEFT)) window.mouseState(true);
                
        }
        shut();
    }
    non-public void replace() {
        window.replace();
        digicam.replace();
        }
    
    non-public 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], digicam); 
                
                    
                }
                }
                }
        //renderer.renderMesh(object, digicam);
        window.swapbuffers();
    }
    non-public void shut() {
        window.destroy();
        mesh.destroy();
        shader.destroy();

    }
    public static void principal(String[] args) {
        new Main().begin();

    }

}

You could possibly discover the dice in right here and the chunk
please let me know if there may be any extra code that you simply or one would wish to see to assist me any additional as I dont assume I must submit my whole code however I could also be unsuitable. please and thanks

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments