Search code examples
javaopengllwjglchunksprocedural-generation

What could be causing meshes to generate rotated? LWGL


I am loading chunk values from a height map to create infinite terrain, but I'm having issues with the rendering of each mesh chunk. When I load a single 500x500 unit mesh, it is a smooth mesh. When I load a 5x5 of 100x100 meshes, it is a jumbled up mess.

I made the two different types of meshes save an image of the height of each mesh, and they both give a smooth height map with gradual value changes.

...But when I render them, this is what I see: 500x500 mesh 5x5 100x100 mesh

As you can see, the 5x5 mesh isn't at all aligned. The first three chunks 0:0, 0:1, and 1:0 seem to look correct, but the others are all different. All rotations in their transformation matrix is vec3(0,0,0), but they generate like this. Here is the code used to render them:

    public void render() {
        shader.start();
        shader.loadViewMatrix(Maths.createViewMatrix(engine.getPlayer()));
        shader.loadLight(engine.getManagers().getLightManager().getLights().get(0));

        for(Chunk chunk : engine.getManagers().getTerrainManager().getLoadedChunks().values()) {
            RawModel model = chunk.getModel();

            bindChunk(chunk);
            loadShaderUniforms(chunk);
            GL11.glDrawElements(GL11.GL_TRIANGLES, model.getVertexCount(), GL11.GL_UNSIGNED_INT, 0);

            unbindChunk();
        }
        shader.stop();
    }

    private void bindChunk(Chunk chunk) {
        RawModel rawModel = chunk.getModel();
        GL30.glBindVertexArray(rawModel.getVaoID());
        GL20.glEnableVertexAttribArray(0); // position
        GL20.glEnableVertexAttribArray(1); // textureCoordinates
        GL20.glEnableVertexAttribArray(2); // normal

        ModelTexture texture = chunk.getTexture();
        GL13.glActiveTexture(GL13.GL_TEXTURE0);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getTextureID());
    }

    private void unbindChunk() {
        GL20.glDisableVertexAttribArray(0);
        GL20.glDisableVertexAttribArray(1);
        GL20.glDisableVertexAttribArray(2);
    }

    private void loadShaderUniforms(Chunk chunk) {
        Matrix4f transformation = Maths.createTransformationMatrix(chunk.getLocation().getPosition(),
                chunk.getLocation().getRotation(), 1, true);
        System.out.println(chunk.getLocation().getRotation());
        shader.loadTransformationMatrix(transformation);
        shader.setShowHeightMap(chunk.shouldShowHeightMap());
    }

I'm not familiar with the details of how LWJGL uses VAOs, so it might be a simple issue I'm forgetting. Any help would be appreciated as I've spent several days just narrowing down the issue to a rendering problem instead of a problem with the perlin noise/mesh generation.


Solution

  • I discovered the issue! There was a flaw in how I created the mesh which caused it to render the triangles flipped over. I discovered this by turning off culling and observing it from both sides and moving the three vertex coordinates around. Fixing the vertex face (e.g. swapping it from a clockwise to counter-clockwise and then reversing the X and Z coordinates) fixed it.