Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TerrainChunkMesh is initialized incorrectly #4

Open
AntonioNoack opened this issue Aug 12, 2022 · 2 comments
Open

TerrainChunkMesh is initialized incorrectly #4

AntonioNoack opened this issue Aug 12, 2022 · 2 comments

Comments

@AntonioNoack
Copy link

Existing code, producing IndexOutOfBoundsException:

        val vertices = ArrayList<Vec2f>()
        var index = 0
        vertices[index++] = Vec2f(0f, 0f)
        vertices[index++] = Vec2f(0.333f, 0f)
        vertices[index++] = Vec2f(0.666f, 0f)
        vertices[index++] = Vec2f(1f, 0f)
        vertices[index++] = Vec2f(0f, 0.333f)
        vertices[index++] = Vec2f(0.333f, 0.333f)
        vertices[index++] = Vec2f(0.666f, 0.333f)
        vertices[index++] = Vec2f(1f, 0.333f)
        vertices[index++] = Vec2f(0f, 0.666f)
        vertices[index++] = Vec2f(0.333f, 0.666f)
        vertices[index++] = Vec2f(0.666f, 0.666f)
        vertices[index++] = Vec2f(1f, 0.666f)
        vertices[index++] = Vec2f(0f, 1f)
        vertices[index++] = Vec2f(0.333f, 1f)
        vertices[index++] = Vec2f(0.666f, 1f)
        vertices[index++] = Vec2f(1f, 1f)
        return vertices.toTypedArray()

Correct code, first possibility:

        val vertices = ArrayList<Vec2f>(16)
        vertices.add(Vec2f(0f, 0f))
        vertices.add(Vec2f(0.333f, 0f))
        vertices.add(Vec2f(0.666f, 0f))
        vertices.add(Vec2f(1f, 0f))
        vertices.add(Vec2f(0f, 0.333f))
        vertices.add(Vec2f(0.333f, 0.333f))
        vertices.add(Vec2f(0.666f, 0.333f))
        vertices.add(Vec2f(1f, 0.333f))
        vertices.add(Vec2f(0f, 0.666f))
        vertices.add(Vec2f(0.333f, 0.666f))
        vertices.add(Vec2f(0.666f, 0.666f))
        vertices.add(Vec2f(1f, 0.666f))
        vertices.add(Vec2f(0f, 1f))
        vertices.add(Vec2f(0.333f, 1f))
        vertices.add(Vec2f(0.666f, 1f))
        vertices.add(Vec2f(1f, 1f))
        return vertices.toTypedArray()

Correct code, second possibility:

return arrayOf(
            Vec2f(0f, 0f),
            Vec2f(0.333f, 0f),
            Vec2f(0.666f, 0f),
            Vec2f(1f, 0f),
            Vec2f(0f, 0.333f),
            Vec2f(0.333f, 0.333f),
            Vec2f(0.666f, 0.333f),
            Vec2f(1f, 0.333f),
            Vec2f(0f, 0.666f),
            Vec2f(0.333f, 0.666f),
            Vec2f(0.666f, 0.666f),
            Vec2f(1f, 0.666f),
            Vec2f(0f, 1f),
            Vec2f(0.333f, 1f),
            Vec2f(0.666f, 1f),
            Vec2f(1f, 1f)
        )

The function Cube() and NDCQuad2Drot180() in that class have the same issue.
Why? Indices, that are not part of the ArrayList, must not be accessed, before there is a value at that index in the list.

@AntonioNoack
Copy link
Author

Corrected Cube() function:

    fun Cube(): Mesh {
        val indices = intArrayOf(
            0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11, 12, 13,
            14, 12, 14, 15, 16, 17, 18, 16, 18, 19, 20, 21, 22, 20, 22, 23
        )
        val vertices = arrayOf(
            Vertex(Vec3f(-1f, -1f, -1f), Vec2f(0f, 1f)),
            Vertex(Vec3f(-1f, 1f, -1f), Vec2f(0f, 0f)),
            Vertex(Vec3f(1f, 1f, -1f), Vec2f(1f, 0f)),
            Vertex(Vec3f(1f, -1f, -1f), Vec2f(1f, 1f)),
            Vertex(Vec3f(-1f, -1f, 1f), Vec2f(0f, 1f)),
            Vertex(Vec3f(-1f, 1f, 1f), Vec2f(0f, 0f)),
            Vertex(Vec3f(-1f, 1f, -1f), Vec2f(1f, 0f)),
            Vertex(Vec3f(-1f, -1f, -1f), Vec2f(1f, 1f)),
            Vertex(Vec3f(1f, -1f, 1f), Vec2f(0f, 1f)),
            Vertex(Vec3f(1f, 1f, 1f), Vec2f(0f, 0f)),
            Vertex(Vec3f(-1f, 1f, 1f), Vec2f(1f, 0f)),
            Vertex(Vec3f(-1f, -1f, 1f), Vec2f(1f, 1f)),
            Vertex(Vec3f(1f, -1f, -1f), Vec2f(0f, 1f)),
            Vertex(Vec3f(1f, 1f, -1f), Vec2f(0f, 0f)),
            Vertex(Vec3f(1f, 1f, 1f), Vec2f(1f, 0f)),
            Vertex(Vec3f(1f, -1f, 1f), Vec2f(1f, 1f)),
            Vertex(Vec3f(-1f, 1f, -1f), Vec2f(0f, 1f)),
            Vertex(Vec3f(-1f, 1f, 1f), Vec2f(0f, 0f)),
            Vertex(Vec3f(1f, 1f, 1f), Vec2f(1f, 0f)),
            Vertex(Vec3f(1f, 1f, -1f), Vec2f(1f, 1f)),
            Vertex(Vec3f(1f, -1f, -1f), Vec2f(0f, 1f)),
            Vertex(Vec3f(1f, -1f, 1f), Vec2f(0f, 0f)),
            Vertex(Vec3f(-1f, -1f, 1f), Vec2f(1f, 0f)),
            Vertex(Vec3f(-1f, -1f, -1f), Vec2f(1f, 1f))
        )
        return Mesh(vertices, indices)
    }

@AntonioNoack
Copy link
Author

AntonioNoack commented Aug 12, 2022

But oh well, the terrain doesn't work anyway: a few configuration values are missing, and the program crashes when it happens (it would be nice if there was defaults), and after that is fixed, it doesn't find addComponent(NodeComponentType.WIREFRAME_RENDERINFO, components[NodeComponentType.WIREFRAME_RENDERINFO]!!.clone()) and crashes by a NullPtrException

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant