zhiayang
zhiayang

Reputation: 571

OpenGL Texture Glitch with faces

I'm using LWJGL to create a simple voxel thingy as some practice, as a moderately simple project to familiarise myself with Kotlin.

So i've got the voxel rendering down, except for rendering a voxel with different textures per face.

It looks alright, until I start moving around... then: what

If anybody needs the entire repo, it's here

As for the relevant classes, I'm not too sure what is relevant, but here's the thing that generates glVertex3f and glTexture2f:

public class Shape
{
    class object
    {
        public fun CreateCube(x: Float, y: Float, z: Float, colour: ColourRGBA, tex: MutableList<TextureCoords>, size: Float)
        {
            val sheet: Spritesheet = Main.Instance!!.blocksprites

            var textures: MutableList<TextureCoords> = arrayListOf()
            if (tex.size == 1)
            {

                for (i in 0 .. 5)
                    textures.add(tex[0])
            }
            else
                textures.addAll(tex)







            // bottom face
            // bottom face (0, 1)
            GL11.glColor4f(colour.red.toFloat(), colour.green.toFloat(), colour.blue.toFloat(), colour.alpha.toFloat());
            GL11.glTexCoord2f(textures[0].x, textures[0].y + sheet.GetUniformSize());
            GL11.glVertex3f(x, y, z + size);
            GL11.glTexCoord2f(textures[0].x + sheet.GetUniformSize(), textures[0].y + sheet.GetUniformSize());
            GL11.glVertex3f(x + size, y, z + size);
            GL11.glTexCoord2f(textures[0].x + sheet.GetUniformSize(), textures[0].y);
            GL11.glVertex3f(x + size, y, z);
            GL11.glTexCoord2f(textures[0].x, textures[0].y);
            GL11.glVertex3f(x, y, z);

            // top face (2, 3)
            GL11.glColor4f(colour.red.toFloat(), colour.green.toFloat(), colour.blue.toFloat(), colour.alpha.toFloat());
            GL11.glTexCoord2f(textures[1].x, textures[1].y + sheet.GetUniformSize());
            GL11.glVertex3f(x, y + size, z);
            GL11.glTexCoord2f(textures[1].x + sheet.GetUniformSize(), textures[1].y + sheet.GetUniformSize());
            GL11.glVertex3f(x + size, y + size, z);
            GL11.glTexCoord2f(textures[1].x + sheet.GetUniformSize(), textures[1].y);
            GL11.glVertex3f(x + size, y + size, z + size);
            GL11.glTexCoord2f(textures[1].x, textures[1].y);
            GL11.glVertex3f(x, y + size, z + size);

            // front face (4, 5)
            GL11.glColor4f(colour.red.toFloat(), colour.green.toFloat(), colour.blue.toFloat(), colour.alpha.toFloat());
            GL11.glTexCoord2f(textures[2].x, textures[2].y + sheet.GetUniformSize());
            GL11.glVertex3f(x, y, z);
            GL11.glTexCoord2f(textures[2].x + sheet.GetUniformSize(), textures[2].y + sheet.GetUniformSize());
            GL11.glVertex3f(x + size, y, z);
            GL11.glTexCoord2f(textures[2].x + sheet.GetUniformSize(), textures[2].y);
            GL11.glVertex3f(x + size, y + size, z);
            GL11.glTexCoord2f(textures[2].x, textures[2].y);
            GL11.glVertex3f(x, y + size, z);

            // back face (6, 7)
            GL11.glColor4f(colour.red.toFloat(), colour.green.toFloat(), colour.blue.toFloat(), colour.alpha.toFloat());
            GL11.glTexCoord2f(textures[3].x, textures[3].y);
            GL11.glVertex3f(x, y + size, z + size);
            GL11.glTexCoord2f(textures[3].x + sheet.GetUniformSize(), textures[3].y);
            GL11.glVertex3f(x + size, y + size, z + size);
            GL11.glTexCoord2f(textures[3].x + sheet.GetUniformSize(), textures[3].y + sheet.GetUniformSize());
            GL11.glVertex3f(x + size, y, z + size);
            GL11.glTexCoord2f(textures[3].x, textures[3].y + sheet.GetUniformSize());
            GL11.glVertex3f(x, y, z + size);

            // left face (8, 9)
            GL11.glColor4f(colour.red.toFloat(), colour.green.toFloat(), colour.blue.toFloat(), colour.alpha.toFloat());
            GL11.glTexCoord2f(textures[4].x, textures[4].y + sheet.GetUniformSize());
            GL11.glVertex3f(x + size, y, z);
            GL11.glTexCoord2f(textures[4].x + sheet.GetUniformSize(), textures[4].y + sheet.GetUniformSize());
            GL11.glVertex3f(x + size, y, z + size);
            GL11.glTexCoord2f(textures[4].x + sheet.GetUniformSize(), textures[4].y);
            GL11.glVertex3f(x + size, y + size, z + size);
            GL11.glTexCoord2f(textures[4].x, textures[4].y);
            GL11.glVertex3f(x + size, y + size, z);

            // right face (10, 11)
            GL11.glColor4f(colour.red.toFloat(), colour.green.toFloat(), colour.blue.toFloat(), colour.alpha.toFloat());
            GL11.glTexCoord2f(textures[5].x, textures[5].y + sheet.GetUniformSize());
            GL11.glVertex3f(x, y, z + size);
            GL11.glTexCoord2f(textures[5].x + sheet.GetUniformSize(), textures[5].y + sheet.GetUniformSize());
            GL11.glVertex3f(x, y, z);
            GL11.glTexCoord2f(textures[5].x + sheet.GetUniformSize(), textures[5].y);
            GL11.glVertex3f(x, y + size, z);
            GL11.glTexCoord2f(textures[5].x, textures[5].y);
            GL11.glVertex3f(x, y + size, z + size);
        }
    }
}

EDIT: after a bit more testing, I realise that I only get the weird glitch when the camera is above the voxel in question. I similar blocks placed elsewhere with only the side faces exposed, and when viewed from the bottom they're fine:

Camera slightly above block (problem escalates the further away the camera is): enter image description here Camera below block: enter image description here

EDIT 2: after some more testing, it turns out the issue is rather with any two different kinds of blocks with different textures interacting... So it's probably not a per-face thing, but rather a face-face interaction thing? I dunno.

EDIT 3: I decided to try and pinpoint the issue, so I removed the glTexCoords2f() calls from my code. Unfortunately, the issue persists:enter image description here

Upvotes: 0

Views: 667

Answers (1)

zhiayang
zhiayang

Reputation: 571

So.

GLU.gluPerspective(67.0f, (width / height).toFloat(), 0.0001f, 1000.0f)

How about GLU.gluPerspective(67.0f, (width / height).toFloat(), 0.5f, 1000.0f)

Basically, reduce the min view distance thingy, solves the issue.

Upvotes: 1

Related Questions