Strange jogl problem. Perhaps it's glDrawElements...

Hello!
I am using jogl and java to rewrite my c++ code to java. The problem is with drawing model read from file. I am using glDrawElements like this:

gl.glDrawElements(GL.GL_TRIANGLES,ibuf.capacity()/3,GL.GL_UNSIGNED_INT,ibuf); //ibuf - Index buffer.

In the above code I have to use GL_UNSIGNED_INT though in java we only have signed int, but when I change it to GL_INT nothing is drawn. So this is how I do “drawing”. Of course I am calling rewind() for each buffer. Each buffer is created like this:

ibuf = BufferUtil.newIntBuffer(fnum*3);

Now is the strange part. When I ran my program I saw this result:

http://iis.pwsz.elblag.pl/~iis7043/tiny.jpg

Then when I ran it again after after changing something small in code, like zFar in gluPerspective from 2000 to 3000 (I does not affect the drawing) the result was:

http://iis.pwsz.elblag.pl/~iis7043/tiny1.jpg

I changed nothing in the drawing part of code! Each time program is rad, the result is different. though everything is the same!

Please help!

I am using:
jogl-1.1.0
java version “1.6.0_04”
Linux, openSUSE 10.3

PS: I am writing this post, because I’ve been “googling” for few hours with no results.

Hi woytah,

yes I know this this part is often very confusing because you always have to keep in mind if the method requires bytes or elements as input.

here my code to init texture and index buffers

here the code to draw the stuff (vertex and normal buffers are updated in this part to…)


    gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL.GL_NORMAL_ARRAY);
    gl.glEnableClientState(GL.GL_TEXTURE_COORD_ARRAY);
        
        // upload changed data
        gl.glBindBuffer(GL.GL_ARRAY_BUFFER, buffer[VERTICES]);
        synchronized(vb) {
            gl.glBufferData(GL.GL_ARRAY_BUFFER, vb.capacity()*4, vb, GL.GL_STREAM_DRAW);
        }
        gl.glVertexPointer(3, GL.GL_FLOAT, 0, 0);

        gl.glBindBuffer(GL.GL_ARRAY_BUFFER, buffer[NORMALES]);
        synchronized(nb) {
            gl.glBufferData(GL.GL_ARRAY_BUFFER, nb.capacity()*4, nb, GL.GL_STREAM_DRAW);
        }
        gl.glNormalPointer(GL.GL_FLOAT, 0, 0);
        
        // bind fixed tex coords
        gl.glBindBuffer(GL.GL_ARRAY_BUFFER, buffer[TEXCOORDS]);
        gl.glTexCoordPointer(2, GL.GL_FLOAT, 0, 0);
        
        // bind indices and render all elements
        gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, buffer[INDICES]);
        gl.glDrawElements(GL.GL_TRIANGLES, ib.capacity(), GL.GL_UNSIGNED_INT, 0);

    gl.glDisableClientState(GL.GL_NORMAL_ARRAY);
    gl.glDisableClientState(GL.GL_VERTEX_ARRAY);
    gl.glDisableClientState(GL.GL_TEXTURE_COORD_ARRAY);
    gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);// disable VBOs

(all methods with buffer.capacity()*4 require the buffer size in bytes)

hope that helps,

I will probably upload this demo to the NB OpenGL pack soon.

Thank you! Thank you very much for such fast reply!

After a reboot and some time spent on experimenting with your code I found that in glDrawElements you have: ib.capacity(), and I had: capacity()/3. Of course yours version is correct and everything works fine now. I found that first screen is a result of passing capacity()/3 to glDrawArrays. Without division the result is correct:

http://iis.pwsz.elblag.pl/~iis7043/tiny2.jpg

But I can’t figure out what made the result look like in the second screen… Maybe it has something to do with JVM, because eclipse started to act crazy (it never happened before).

So once again thank you! :slight_smile: