0
votes

Using LWJGL I tried to render to render a simple Mesh on screen, but OpenGL decided to instead do nothing. :(

So I have a mesh class which creates a VBO. I can add some vertices which then are supposed to be drawn on screen.

public class Mesh {

    private int vbo;
    private int size = 0;

    public Mesh() {
        vbo = glGenBuffers();
    }

    public void addVertices(Vertex[] vertices) {
        size = vertices.length;

        glBindBuffer(GL_ARRAY_BUFFER, vbo);
        glBufferData(GL_ARRAY_BUFFER, Util.createFlippedBuffer(vertices), GL_STATIC_DRAW);
    }

    public void draw() {
        glEnableVertexAttribArray(0);

        glBindBuffer(GL_ARRAY_BUFFER, vbo);
        glVertexAttribPointer(0, 3, GL_FLOAT, false, Vertex.SIZE * 4, 0);

        glDrawArrays(GL_TRIANGLES, 0, size);

        glDisableVertexAttribArray(0);
    }

}

Here is how I add vertices to my mesh:

mesh = new Mesh();

Vertex[] vertices = new Vertex[] { new Vertex(new Vector3f(-1, -1, 0)),
                                   new Vertex(new Vector3f(-1, 1, 0)),
                                   new Vertex(new Vector3f(0, 1, 0)) };

mesh.addVertices(vertices);

I am pretty sure I added them in the correct (clock-wise) order.

And my OpenGL setup:

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

glFrontFace(GL_CW);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);

Calling glGetError() returns no error (0).

EDIT:

Well I found out that macs are little weird when it comes to OpenGL. I needed to use a VAO along with the VBO. Now it works fine. Thanks anyway!

2
addVertices is a misnomer, it replaces the existing vertices, setVertices would be a more accurate name. - ratchet freak
I know, but thanks for pointing it out. I only add them once. - user1734282
Are you using shaders or the fixed pipeline? - Reto Koradi

2 Answers

0
votes

I don't see anywhere you're specifying either a shader for output or a color, or a vertex array. Depending on which profile you're using you need to be doing one or more of these.

I would suggest checking / setting the following

  • Disable face culling to ensure that regardless of the winding you should see something
  • If you're requesting a core profile, you'll need a shader and quite possibly a vertex array object
  • If you're instead using a compatibility profile you should call glColor3f(1, 1, 1) in your draw call to ensure you're not drawing a black triangle
  • Are you clearing the color and depth framebuffers before your render?
0
votes

You might not be drawing that object within the viewing frustum, also call glCheckError to make sure you aren't making any mistakes.

It is also important to understand the difference between fixed pipeline and programmable pipeline OpenGL. If you are using a version with a programmable pipeline you will need to write shaders, otherwise you will need to set modelview and projection matrices.