Lucas
Lucas

Reputation: 3500

Weird line appears on 3D cube attempt

First things first

I'm very new to OpenGL and game development at all. Perhaps I'll miss some important code or did something terrible in my code. Please feel free to ask for more information in comments.

Problem

I've tried to draw a 3D cube made out of single lines. It works as expected but a weird line appears.
The weird line moves from 0,0,1 to 1,1,0

I colored my lines with different (solid) colors. The weird line has a gradient.

wild line appears apparently

Okay, time for some code

Since this is a lot of code, I'll try to post the minimum. As I said before, feel free to ask for more.

The method that draws my cube

private void drawBlock(Position pos) {
    int x = pos.x, y = pos.y, z = pos.z;

    GL11.glBegin(GL11.GL_LINE_STRIP);

    GL11.glColor4f(1, 1, 1, 1);
    // 0,0,0 -> 5,0,0
    GL11.glVertex3i(x, y, z);
    GL11.glVertex3i(x + blockSize, y, z);

    // 0,0,0 -> 0,5,0
    GL11.glVertex3i(x, y, z);
    GL11.glVertex3i(x, y + blockSize, z);

    // 0,0,0 -> 0,0,5
    GL11.glVertex3i(x, y, z);
    GL11.glVertex3i(x, y, z + blockSize);


    GL11.glColor4f(0.1f, 0.5f, 0.5f, 1);
    // 5,0,5 -> 0,0,5
    GL11.glVertex3i(x + blockSize, y, z + blockSize);
    GL11.glVertex3i(x, y, z + blockSize);

    // 5,0,5 -> 5,5,5
    GL11.glVertex3i(x + blockSize, y, z + blockSize);
    GL11.glVertex3i(x + blockSize, y + blockSize, z + blockSize);

    // 5,0,5 -> 5,0,0
    GL11.glVertex3i(x + blockSize, y, z + blockSize);
    GL11.glVertex3i(x + blockSize, y, z);


    GL11.glColor4f(0.9f, 0.25f, 0.25f, 1);
    // 0,5,5 -> 0,0,5
    GL11.glVertex3i(x, y + blockSize, z + blockSize);
    GL11.glVertex3i(x, y, z + blockSize);

    // 0,5,5 -> 5,5,5
    GL11.glVertex3i(x, y + blockSize, z + blockSize);
    GL11.glVertex3i(x + blockSize, y + blockSize, z + blockSize);

    // 0,5,5 -> 0,5,0
    GL11.glVertex3i(x, y + blockSize, z + blockSize);
    GL11.glVertex3i(x, y + blockSize, z);

    GL11.glColor4f(1, 0.75f, 0, 1);
    // 5,5,0 -> 5,0,0
    GL11.glVertex3i(x + blockSize, y + blockSize, z);
    GL11.glVertex3i(x + blockSize, y, z);

    // 5,5,0 -> 0,5,0
    GL11.glVertex3i(x + blockSize, y + blockSize, z);
    GL11.glVertex3i(x, y + blockSize, z);

    // 5,5,0 -> 5,5,5
    GL11.glVertex3i(x + blockSize, y + blockSize, z);
    GL11.glVertex3i(x + blockSize, y + blockSize, z + blockSize);

    GL11.glEnd();
}

OpenGL Initialization

public void init3D() {
    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();

    GLU.gluPerspective((float) 100, width / height, 0.001f, 1000);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);

    GL11.glEnable(GL11.GL_TEXTURE_2D);
    GL11.glShadeModel(GL11.GL_SMOOTH);
    GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
    GL11.glClearDepth(1.0f);
}

Upvotes: 1

Views: 120

Answers (1)

ratchet freak
ratchet freak

Reputation: 48216

you should use GL11.glBegin(GL11.GL_LINES); instead of line strip,

line_strip is a sequence of connected line where the sequence P1, P2, P3, P4 gives the lines P1-P2, P2-P3, P3-P4 and so on while GL_LINES would give only P1-P2 and P3-P4

Upvotes: 1

Related Questions