VBO and interleaved data problem with JOGL

Started by
5 comments, last by LizardCPP 13 years, 5 months ago
It's been a while since I have done any OpenGL coding and now I'm trying to get back into the game using JOGL, OpenGL bindings for Java. I got a simple program up and running that draws two triangles as a quad using the old fixed-function pipeline (i.e. glBegin(GL_TRIANGLES) / glEnd()). Now I'm trying to get it to work with VBO:s, indexed interleaved vertex data as i used to do it 5 years ago. Then after that I'm thinking of working my way up to use the 3.2 profile.

Update: I created a simpler test program that displays my problem in one file, see post 6 in this thread.

Update 2: This has now been solved, see post 7.

The sample program requests a OpenGL 2.0 profile and then tries to render two triangles as a quad with one color per vertex. The vertex data is interleaved with first 3 floats for position and then 4 floats for color. 4 vertex is set in a VBO and then 6 indices are set in a index buffer. Result is a black window, nothing is rendered,glGetError returns 0 if called after glDrawRangeElements().

Geometry class:
import java.nio.Buffer;public class Geometry{    /**     *     */    private VertexFormat vertexFormat;    /**     *     */    private Buffer vertexBuffer;    /**     *     */    private Buffer indexBuffer;    /**     * @param vertexFormat     * @param vertexBuffer     * @param indexBuffer     */    public Geometry(VertexFormat vertexFormat, Buffer vertexBuffer, Buffer indexBuffer)    {        this.vertexFormat = vertexFormat;        this.vertexBuffer = vertexBuffer;        this.indexBuffer = indexBuffer;    }    /**     * @return     */    public int getStride()    {        return vertexFormat.getStride();    }    public Buffer getVertexData()    {        return vertexBuffer;    }    public Buffer getIndexData()    {        return indexBuffer;    }}


JOGLBufferObject class:
import javax.media.opengl.GL2;import javax.media.opengl.GLContext;import java.nio.Buffer;class JOGLBufferObject implements BufferObject{    /**     * The OpenGL api object     */    private GL2 gl;    /**     * This will either be set to GL_ARRAY_BUFFER or GL_ELEMENT_ARRAY_BUFFER     */    private int type;    /**     * Size of the buffer in bytes     */    private int size;    /**     * The buffers id     */    private int[] bufferId;    /**     *     * @param type the type of the buffer, will either be GL_ARRAY_BUFFER or GL_ELEMENT_ARRAY_BUFFER     * @param size size of the buffer in bytes     */    private JOGLBufferObject(int type, int size)    {        this.gl = GLContext.getCurrentGL().getGL2();        this.type = type;        this.bufferId = new int[1];        this.size = size;        gl.glGenBuffers(1, bufferId, 0);        gl.glBindBuffer(type, bufferId[0]);        gl.glBufferData(type, size, null, GL2.GL_DYNAMIC_DRAW);    }    /**     *     * @param size     * @return     */    public static JOGLBufferObject createVertexBuffer(int size)    {        return new JOGLBufferObject(GL2.GL_ARRAY_BUFFER, size);    }    /**     *     * @param size     * @return     */    public static JOGLBufferObject createIndexBuffer(int size)    {       return new JOGLBufferObject(GL2.GL_ELEMENT_ARRAY_BUFFER, size);    }    /**     * Retrieve the size of the buffer     *     * @return size of the buffer     */    public int getSize()    {        return size;    }    /**     *     */    public void destroy()    {       gl.glDeleteBuffers(1, bufferId, 0);    }    /**     *     */    public void bind()    {        gl.glBindBuffer(type, bufferId[0]);    }    /**     *     */    public void unbind()    {        gl.glBindBuffer(type, 0);    }    /**     *     * @param offset     * @param size     * @param buffer     */    public void update(int offset, int size, Buffer buffer)    {        gl.glBufferSubData(type, offset, size, buffer);    }}


Setup of geometry:
float[] vertexData = new float[]{    0.75f, 0.25f, -2.0f, 1.0f, 0.0f, 0.0f, 1.0f,    0.75f, 0.75f, -2.0f, 0.0f, 1.0f, 0.0f, 1.0f,    0.25f, 0.25f, -2.0f, 0.0f, 0.0f, 1.0f, 1.0f,    0.25f, 0.75f, -2.0f, 0.5f, 0.5f, 0.5f, 1.0f};ByteBuffer vertexByteBuffer = ByteBuffer.allocateDirect(7 * 4 * 4);FloatBuffer vertexBuffer = vertexByteBuffer.asFloatBuffer();vertexBuffer.put(vertexData);int[] indexData = new int[]{    0, 1, 2, 2, 1, 3};ByteBuffer indexByteBuffer = ByteBuffer.allocateDirect(6 * 4);IntBuffer indexBuffer = indexByteBuffer.asIntBuffer();indexBuffer.put(indexData);VertexFormat vertexFormat = new VertexFormat(7 * 4);geometry = new Geometry(vertexFormat, vertexByteBuffer, indexByteBuffer);


JOGLRenderer, it's the render(Geometry geometry) function that draws nothing:
import javax.media.opengl.GL;import javax.media.opengl.GL2;import javax.media.opengl.GLContext;import javax.media.opengl.glu.GLU;import java.nio.Buffer;public class JOGLRenderer implements Renderer{    /**     *     */    private GL2 gl;    /**     *     */    private GLU glu;    /**     *     */    private BufferObject vertexBuffer;    /**     *     */    private BufferObject indexBuffer;    // TEMP    private boolean setupDone = false;    /**     */    public JOGLRenderer()    {        this.gl = GLContext.getCurrentGL().getGL2();        this.glu = new GLU();        this.vertexBuffer = JOGLBufferObject.createVertexBuffer(1024);        this.indexBuffer = JOGLBufferObject.createIndexBuffer(1024);        gl.glShadeModel(GL2.GL_SMOOTH);        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);        gl.glClearDepth(1.0f);        gl.glEnable(GL.GL_DEPTH_TEST);        gl.glDepthFunc(GL.GL_LEQUAL);        gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);    }    @Override    public void reshape(int x, int y, int width, int height)    {        gl.glMatrixMode(GL2.GL_MODELVIEW);        gl.glLoadIdentity();        gl.glViewport(0, 0, width, height);        gl.glMatrixMode(GL2.GL_PROJECTION);        gl.glLoadIdentity();        glu.gluPerspective(45.0f, (float) width / (float) height, 0.1f, 100.0f);        gl.glMatrixMode(GL2.GL_MODELVIEW);    }    @Override    public void prepare()    {        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);        gl.glMatrixMode(GL2.GL_MODELVIEW);        gl.glLoadIdentity();    }    @Override    public void render(OLDGeometry oldGeometry)    {        gl.glBegin(GL.GL_TRIANGLES);        gl.glColor3f(oldGeometry.color.getX(), oldGeometry.color.getY(), oldGeometry.color.getZ());        gl.glVertex3f(oldGeometry.topLeft.getX(), oldGeometry.topLeft.getY(), oldGeometry.topLeft.getZ());        gl.glVertex3f(oldGeometry.topRight.getX(), oldGeometry.topRight.getY(), oldGeometry.topRight.getZ());        gl.glVertex3f(oldGeometry.bottomLeft.getX(), oldGeometry.bottomLeft.getY(), oldGeometry.bottomLeft.getZ());        gl.glVertex3f(oldGeometry.bottomLeft.getX(), oldGeometry.bottomLeft.getY(), oldGeometry.bottomLeft.getZ());        gl.glVertex3f(oldGeometry.topRight.getX(), oldGeometry.topRight.getY(), oldGeometry.topRight.getZ());        gl.glVertex3f(oldGeometry.bottomRight.getX(), oldGeometry.bottomRight.getY(), oldGeometry.bottomRight.getZ());        gl.glEnd();    }    @Override    public void render(Geometry geometry)    {        Buffer vertexBufferData = geometry.getVertexData();        Buffer indexBufferData = geometry.getIndexData();        int stride = geometry.getStride();   // the stride is here 28 i.e. 7*4        gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);        gl.glEnableClientState(GL2.GL_COLOR_ARRAY);        if (!setupDone)        {            vertexBuffer.bind();            indexBuffer.bind();            vertexBuffer.update(0, vertexBufferData.capacity(), vertexBufferData);  // capacity returns 112 i.e. 7*4*4            indexBuffer.update(0, indexBufferData.capacity(), indexBufferData);    // capacity returns 24 i.e. 6*4            setupDone = true;        }        gl.glVertexPointer(3, GL2.GL_FLOAT, stride, 0);        gl.glColorPointer(4, GL2.GL_FLOAT, stride, 3 * 4);        gl.glDrawRangeElements(GL2.GL_TRIANGLES, 0, 5, 2, GL2.GL_UNSIGNED_INT, 0);        gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);        gl.glDisableClientState(GL2.GL_COLOR_ARRAY);    }    @Override    public void present()    {        // No need to swap buffers, jogl does that for me    }}


Render loop:
    renderer.prepare();    renderer.render(geometry);    renderer.present();



Can anyone see what I'm doing wrong?

[Edited by - LizardCPP on November 13, 2010 8:00:52 AM]
Advertisement
  gl.glDrawRangeElements(GL2.GL_TRIANGLES, 0, 5, 2, GL2.GL_UNSIGNED_INT, 0);


I think you've got the wrong value for 'count' here. It should be the number of indices, not the number of primitives. I believe this code only draws two vertices and quits, so you're not completing the first triangle.

[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
Quote:Original post by karwosts
*** Source Snippet Removed ***

I think you've got the wrong value for 'count' here. It should be the number of indices, not the number of primitives. I believe this code only draws two vertices and quits, so you're not completing the first triangle.



Thanks for your replay, but I have now tried a count between 2-6 nothing is rendered in any case.


I realize this is a bit of work for you, but can you redo your code into a nice little linear flow without so many function calls? I found myself rolling up and down the screen about 50 times trying to follow the flow of your program and it's difficult to gather in my head in what order things are happening.

maybe just make a new quickie render hack function that is like the following:

glGenBuffers()glBindBuffer()glBufferData()glBindBuffer()glBufferData()glVertexPointer()glColorPointer()glDrawElements()


It doesn't have to be clean and you can break on the second frame so you don't have to worry about initializing outside of your render loop, but that's probably the best way to get the basics nailed down, and once you have it working, you can OO'ize your code into classes. But just for me trying to read it's very difficult to follow.

You can certainly choose not to do this, but it's much easier to debug that way.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
Quote:Original post by karwosts
I realize this is a bit of work for you, but can you redo your code into a nice little linear flow without so many function calls? I found myself rolling up and down the screen about 50 times trying to follow the flow of your program and it's difficult to gather in my head in what order things are happening.

maybe just make a new quickie render hack function that is like the following:

*** Source Snippet Removed ***

It doesn't have to be clean and you can break on the second frame so you don't have to worry about initializing outside of your render loop, but that's probably the best way to get the basics nailed down, and once you have it working, you can OO'ize your code into classes. But just for me trying to read it's very difficult to follow.

You can certainly choose not to do this, but it's much easier to debug that way.


Yeah, I'll think I have to do that. Create a single class that does this without the OO. I'll get back to this post tomorrow if I can't get that to work.

Thanks for trying to help me.
Ok I have created a single file that displays my problem, it can be compiled and run if one has a java SDK and JOGL.

The problem is still that nothing is rendered. glGetError returns 0 after glDrawRangelElements.

import com.jogamp.opengl.util.AnimatorBase;import com.jogamp.opengl.util.FPSAnimator;import javax.media.opengl.*;import javax.media.opengl.awt.GLCanvas;import javax.media.opengl.glu.GLU;import javax.swing.*;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.nio.ByteBuffer;import java.nio.FloatBuffer;import java.nio.IntBuffer;/** * @version 2010-nov-13 */public class JOGLTest{    public static void main(String[] args)    {        JFrame frame = new JFrame("JOGL test");        frame.setSize(640, 480);        GLProfile glProfile = GLProfile.get(GLProfile.GL2);        GLCapabilities glCapabilities = new GLCapabilities(glProfile);        GLCanvas canvas = new GLCanvas(glCapabilities);        frame.add(canvas);        canvas.addGLEventListener(new JOGLRenderer());        AnimatorBase animator = new FPSAnimator(canvas, 60);        animator.add(canvas);        animator.start();        frame.addWindowListener(new WindowAdapter()        {            public void windowClosing(WindowEvent e)            {                System.exit(0);            }        });        frame.setVisible(true);    }    private static class JOGLRenderer implements GLEventListener    {        @Override        public void init(GLAutoDrawable drawable)        {            GL2 gl = drawable.getGL().getGL2();            gl.glShadeModel(GL2.GL_SMOOTH);            gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);            gl.glClearDepth(1.0f);            gl.glEnable(GL.GL_DEPTH_TEST);            gl.glDepthFunc(GL.GL_LEQUAL);            gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);            // Create vertex data            float[] vertexData = new float[]            {                0.75f, 0.25f, -2.0f, 1.0f, 0.0f, 0.0f, 1.0f,                0.75f, 0.75f, -2.0f, 0.0f, 1.0f, 0.0f, 1.0f,                0.25f, 0.25f, -2.0f, 0.0f, 0.0f, 1.0f, 1.0f,                0.25f, 0.75f, -2.0f, 0.5f, 0.5f, 0.5f, 1.0f            };            ByteBuffer vertexByteBuffer = ByteBuffer.allocateDirect(7 * 4 * 4);            FloatBuffer vertexBuffer = vertexByteBuffer.asFloatBuffer();            vertexBuffer.put(vertexData);            // Create vertex buffer            int[] vertexBufferId = new int[1];            gl.glGenBuffers(1, vertexBufferId, 0);            gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vertexBufferId[0]);            gl.glBufferData(GL2.GL_ARRAY_BUFFER, 1024, null, GL2.GL_DYNAMIC_DRAW);            // Load vertex data into vertex buffer            gl.glBufferSubData(GL2.GL_ARRAY_BUFFER, 0, vertexByteBuffer.capacity(), vertexByteBuffer);            // Create index data            int[] indexData = new int[]            {                0, 1, 2, 2, 1, 3            };            ByteBuffer indexByteBuffer = ByteBuffer.allocateDirect(6 * 4);            IntBuffer indexBuffer = indexByteBuffer.asIntBuffer();            indexBuffer.put(indexData);            // Create index buffer            int[] indexBufferId = new int[1];            gl.glGenBuffers(1, indexBufferId, 0);            gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, indexBufferId[0]);            gl.glBufferData(GL2.GL_ELEMENT_ARRAY_BUFFER, 1024, null, GL2.GL_DYNAMIC_DRAW);            // Load index data into index buffer            gl.glBufferSubData(GL2.GL_ELEMENT_ARRAY_BUFFER, 0, indexByteBuffer.capacity(), indexByteBuffer);        }        @Override        public void dispose(GLAutoDrawable drawable)        {        }        @Override        public void display(GLAutoDrawable drawable)        {            GL2 gl = drawable.getGL().getGL2();            gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);            gl.glMatrixMode(GL2.GL_MODELVIEW);            gl.glLoadIdentity();            /*               This rendered a purple quad            gl.glBegin(GL2.GL_TRIANGLES);            gl.glColor3f(0.5f ,0.0f, 0.5f);            gl.glVertex3f(0.75f, 0.25f, -2);            gl.glVertex3f(0.75f, 0.75f, -2);            gl.glVertex3f(0.25f, 0.25f, -2);            gl.glVertex3f(0.25f, 0.25f, -2);            gl.glVertex3f(0.75f, 0.75f, -2);            gl.glVertex3f(0.25f, 0.75f, -2);            gl.glEnd();             */             // this below renders nothing             int stride = 7 * 4;            gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);            gl.glEnableClientState(GL2.GL_COLOR_ARRAY);            gl.glVertexPointer(3, GL2.GL_FLOAT, stride, 0);            gl.glColorPointer(4, GL2.GL_FLOAT, stride, 3 * 4);            gl.glDrawRangeElements(GL2.GL_TRIANGLES, 0, 5, 6, GL2.GL_UNSIGNED_INT, 0);            // gl.glDrawElements(GL2.GL_TRIANGLES, 6, GL2.GL_UNSIGNED_INT, 0);            // glGetError returns 0            int error = gl.glGetError();            gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);            gl.glDisableClientState(GL2.GL_COLOR_ARRAY);        }        @Override        public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)        {            GL2 gl = drawable.getGL().getGL2();            GLU glu = new GLU();            gl.glMatrixMode(GL2.GL_MODELVIEW);            gl.glLoadIdentity();            gl.glViewport(0, 0, width, height);            gl.glMatrixMode(GL2.GL_PROJECTION);            gl.glLoadIdentity();            glu.gluPerspective(45.0f, (float) width / (float) height, 0.1f, 100.0f);            gl.glMatrixMode(GL2.GL_MODELVIEW);        }    }}
Ok I solved it myself and the problem lies in how one should work with java.nio.Buffer. I have to call the order() method on the ByteBuffer with ByteOrder.nativeOrder(). Then it works!

Full working source:
import com.jogamp.opengl.util.AnimatorBase;import com.jogamp.opengl.util.FPSAnimator;import javax.media.opengl.*;import javax.media.opengl.awt.GLCanvas;import javax.media.opengl.glu.GLU;import javax.swing.*;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.nio.ByteBuffer;import java.nio.ByteOrder;import java.nio.FloatBuffer;import java.nio.IntBuffer;/** * @version 2010-nov-13 */public class JOGLTest{    public static void main(String[] args)    {        JFrame frame = new JFrame("JOGL test");        frame.setSize(640, 480);        GLProfile glProfile = GLProfile.get(GLProfile.GL2);        GLCapabilities glCapabilities = new GLCapabilities(glProfile);        GLCanvas canvas = new GLCanvas(glCapabilities);        frame.add(canvas);        canvas.addGLEventListener(new JOGLRenderer());        AnimatorBase animator = new FPSAnimator(canvas, 60);        animator.add(canvas);        animator.start();        frame.addWindowListener(new WindowAdapter()        {            public void windowClosing(WindowEvent e)            {                System.exit(0);            }        });        frame.setVisible(true);    }    private static class JOGLRenderer implements GLEventListener    {        @Override        public void init(GLAutoDrawable drawable)        {            GL2 gl = drawable.getGL().getGL2();            gl.glShadeModel(GL2.GL_SMOOTH);            gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);            gl.glClearDepth(1.0f);            gl.glEnable(GL.GL_DEPTH_TEST);            gl.glDepthFunc(GL.GL_LEQUAL);            gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);            // Create vertex data            float[] vertexData = new float[]            {                0.75f, 0.25f, -2.0f, 1.0f, 0.0f, 0.0f, 1.0f,                0.75f, 0.75f, -2.0f, 0.0f, 1.0f, 0.0f, 1.0f,                0.25f, 0.25f, -2.0f, 0.0f, 0.0f, 1.0f, 1.0f,                0.25f, 0.75f, -2.0f, 0.5f, 0.5f, 0.5f, 1.0f            };            ByteBuffer vertexByteBuffer = ByteBuffer.allocateDirect(7 * 4 * 4);            vertexByteBuffer.order(ByteOrder.nativeOrder());  // <-- this line was missing            FloatBuffer vertexBuffer = vertexByteBuffer.asFloatBuffer();            vertexBuffer.put(vertexData);            // Create vertex buffer            int[] vertexBufferId = new int[1];            gl.glGenBuffers(1, vertexBufferId, 0);            gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vertexBufferId[0]);            gl.glBufferData(GL2.GL_ARRAY_BUFFER, 1024, null, GL2.GL_DYNAMIC_DRAW);            // Load vertex data into vertex buffer            gl.glBufferSubData(GL2.GL_ARRAY_BUFFER, 0, vertexByteBuffer.capacity(), vertexByteBuffer);            // Create index data            int[] indexData = new int[]            {                0, 1, 2, 2, 1, 3            };            ByteBuffer indexByteBuffer = ByteBuffer.allocateDirect(6 * 4);            indexByteBuffer.order(ByteOrder.nativeOrder());  // <- this line was missing            IntBuffer indexBuffer = indexByteBuffer.asIntBuffer();            indexBuffer.put(indexData);            // Create index buffer            int[] indexBufferId = new int[1];            gl.glGenBuffers(1, indexBufferId, 0);            gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, indexBufferId[0]);            gl.glBufferData(GL2.GL_ELEMENT_ARRAY_BUFFER, 1024, null, GL2.GL_DYNAMIC_DRAW);            // Load index data into index buffer            gl.glBufferSubData(GL2.GL_ELEMENT_ARRAY_BUFFER, 0, indexByteBuffer.capacity(), indexByteBuffer);        }        @Override        public void dispose(GLAutoDrawable drawable)        {        }        @Override        public void display(GLAutoDrawable drawable)        {            GL2 gl = drawable.getGL().getGL2();            gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);            gl.glMatrixMode(GL2.GL_MODELVIEW);            gl.glLoadIdentity();            /*               This rendered a purple quad            gl.glBegin(GL2.GL_TRIANGLES);            gl.glColor3f(0.5f ,0.0f, 0.5f);            gl.glVertex3f(0.75f, 0.25f, -2);            gl.glVertex3f(0.75f, 0.75f, -2);            gl.glVertex3f(0.25f, 0.25f, -2);            gl.glVertex3f(0.25f, 0.25f, -2);            gl.glVertex3f(0.75f, 0.75f, -2);            gl.glVertex3f(0.25f, 0.75f, -2);            gl.glEnd();             */            int stride = 7 * 4;            gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);            gl.glEnableClientState(GL2.GL_COLOR_ARRAY);            gl.glVertexPointer(3, GL2.GL_FLOAT, stride, 0);            gl.glColorPointer(4, GL2.GL_FLOAT, stride, 3 * 4);            gl.glDrawRangeElements(GL2.GL_TRIANGLES, 0, 5, 6, GL2.GL_UNSIGNED_INT, 0);            // gl.glDrawElements(GL2.GL_TRIANGLES, 6, GL2.GL_UNSIGNED_INT, 0);            // glGetError returns 0            int error = gl.glGetError();            gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);            gl.glDisableClientState(GL2.GL_COLOR_ARRAY);        }        @Override        public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)        {            GL2 gl = drawable.getGL().getGL2();            GLU glu = new GLU();            gl.glMatrixMode(GL2.GL_MODELVIEW);            gl.glLoadIdentity();            gl.glViewport(0, 0, width, height);            gl.glMatrixMode(GL2.GL_PROJECTION);            gl.glLoadIdentity();            glu.gluPerspective(45.0f, (float) width / (float) height, 0.1f, 100.0f);            gl.glMatrixMode(GL2.GL_MODELVIEW);        }    }}

This topic is closed to new replies.

Advertisement