San Mor
San Mor

Reputation: 133

JOGL display using Buffer

I have 3 FloatBuffers - vertices, normals and colors which contain what the name suggests. I also have an IntBuffer to keep track of the indexing.

The data looks correct, but I'm having trouble displaying it. I just see a blank canvas. I'm not sure what I am doing wrong. I'm guessing something is being overlooked in init(), display(), reshape() and dispose(). Can anyone tell me if you find something glaringly wrong in the code below, and why you think nothing is being displayed?

  @Override
  public void init(GLAutoDrawable drawable) {
    GL2 gl = drawable.getGL().getGL2();
    gl.glClearColor(.0f, .0f, .2f, 0.9f);
    gl.glEnable(GL2.GL_DEPTH_TEST);
    gl.glDepthFunc(GL2.GL_LESS);
    gl.glEnable(GL2.GL_CULL_FACE);
    gl.glEnable(GL2.GL_LIGHTING);
    gl.glEnable(GL2.GL_LIGHT0);
    gl.glEnable(GL2.GL_AUTO_NORMAL);
    gl.glEnable(GL2.GL_NORMALIZE);
    gl.glFrontFace(GL2.GL_CCW);
    gl.glCullFace(GL2.GL_BACK);
    gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);
    gl.glShadeModel(GL2.GL_SMOOTH);
    if (viewMesh) {
        gl.glPolygonMode(GL2.GL_FRONT_AND_BACK, GL2.GL_LINE);
    } else {
        gl.glPolygonMode(GL2.GL_FRONT_AND_BACK, GL2.GL_FILL);
    }

    glu = new GLU();

    // Build the VBOs
    VBO = IntBuffer.allocate(4);
    gl.glGenBuffers(4, VBO);
    vertVBOID = VBO.get(0);
    normalVBOID = VBO.get(1);
    colorVBOID = VBO.get(2);
    indexVBOID = VBO.get(3);

    // vertices
    int vsize = sd.verts.capacity() * BufferUtil.SIZEOF_FLOAT;
    gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vertVBOID); // get a valid name
    gl.glBufferData(GL2.GL_ARRAY_BUFFER, vsize, sd.verts, GL2.GL_STATIC_DRAW);
    gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, 0); // reset

    // normals
    int nsize = sd.normals.capacity() * BufferUtil.SIZEOF_FLOAT;
    gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, normalVBOID);
    gl.glBufferData(GL2.GL_ARRAY_BUFFER, nsize, sd.normals, GL2.GL_STATIC_DRAW);
    gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, 0);

    // colors
    int csize = sd.colors.capacity() * BufferUtil.SIZEOF_FLOAT;
    gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, colorVBOID);
    gl.glBufferData(GL2.GL_ARRAY_BUFFER, csize, sd.colors, GL2.GL_STATIC_DRAW);
    gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, 0);

    int isize = sd.indices.capacity() * BufferUtil.SIZEOF_INT;
    gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, indexVBOID);
    gl.glBufferData(GL2.GL_ELEMENT_ARRAY_BUFFER, isize, sd.indices, GL2.GL_STATIC_DRAW);
    gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, 0);

    // sd.verts = null; // copy of data is no longer necessary, it is in the graphics card now.
    // sd.colors = null;
    // sd.normals = null;
    // sd.indices = null;
} 

@Override
public void display(GLAutoDrawable drawable) {
    GL2 gl = drawable.getGL().getGL2();
    gl.glMatrixMode(GL2.GL_MODELVIEW);
    gl.glLoadIdentity();
    glu.gluLookAt(45, 0, 0, 0, 0, 0, 0.0, 1.0, 0.0);

    gl.glScalef(scale, scale, scale);
    gl.glRotatef(rot, 0, 1, 0);
    gl.glTranslatef(-sd.tr_x, -sd.tr_y, -sd.tr_z);

        gl.glEnableClientState(GLPointerFunc.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GLPointerFunc.GL_NORMAL_ARRAY);
    gl.glEnableClientState(GLPointerFunc.GL_COLOR_ARRAY);

    gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, normalVBOID);
    gl.glNormalPointer(GL2.GL_FLOAT, 0, 0);
    gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, colorVBOID);
    gl.glColorPointer(3, GL2.GL_FLOAT, 0, 0);
    gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vertVBOID);
    gl.glVertexPointer(3, GL2.GL_FLOAT, 0, 0);

    gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, indexVBOID);
    gl.glDrawElements(GL2.GL_TRIANGLES, sd.indices.capacity(), GL2.GL_INT, 0);
    gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, 0); // unbind it

    gl.glDisableClientState(GLPointerFunc.GL_VERTEX_ARRAY);
    gl.glDisableClientState(GLPointerFunc.GL_NORMAL_ARRAY);
    gl.glDisableClientState(GLPointerFunc.GL_COLOR_ARRAY);

}   

    @Override
public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
    GL2 gl = drawable.getGL().getGL2();
    double fov = (Math.PI / 4.0);
    double zmm = Math.abs(sd.min_z - sd.max_z);
    camdist = (zmm / 2.0) / Math.tan(fov / 2.0);

    h = (h == 0) ? 1 : h;

    gl.glViewport(0, 0, w, h);
    gl.glMatrixMode(GL2.GL_PROJECTION);
    gl.glLoadIdentity();
    glu.gluPerspective(camdist, w / (float) h, 0.1f, 1000.0);
}




    /**
 * Initilaize graphics
 */
public void initOGL() {
    profile = GLProfile.get(GLProfile.GL2);
    caps = new GLCapabilities(profile);
    caps.setHardwareAccelerated(true);
    canvas = new GLCanvas(caps);
    canvas.addGLEventListener(this);
    canvas.requestFocusInWindow();
    getContentPane().add(canvas); // add to the frame
}


    @Override
public void dispose(GLAutoDrawable drawable) {
}

Upvotes: 1

Views: 475

Answers (1)

Reto Koradi
Reto Koradi

Reputation: 54642

One clear problem is in the draw call:

gl.glDrawElements(GL2.GL_TRIANGLES, sd.indices.capacity(), GL2.GL_INT, 0);

GL_INT is not valid for the type argument of glDrawElements(). The only valid values are GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT and GL_UNSIGNED_INT. So the call needs to be:

gl.glDrawElements(GL2.GL_TRIANGLES, sd.indices.capacity(), GL2.GL_UNSIGNED_INT, 0);

Anytime you have a problem with OpenGL code not working as expected, make sure that you call glGetError(). With the call from your code, you should immediately get a GL_INVALID_ENUM error.

Also, your perspective setup code looks somewhat suspicious. I don't fully understand what you're trying to do there, but even just the naming suggests a possible misunderstanding:

glu.gluPerspective(camdist, w / (float) h, 0.1f, 1000.0);

The first argument of gluPerspective() is the field-of-view angle (in degrees), not a distance.

Upvotes: 3

Related Questions