glVertexPointer Stride Problem

I’ve done a post search and can’t find anyone having same issue as me. I’m using the current release binaries (1.1.0).

Please try this code:


import java.applet.Applet;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;

import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;

import com.sun.opengl.util.Animator;
import com.sun.opengl.util.BufferUtil;

public class VertexArrayTest extends Applet implements GLEventListener
{
	Animator anim;
	GLCanvas canvas;
    
    public void start()
    {
    	this.setSize(640, 480);

        GLCapabilities glCaps = new GLCapabilities();

        canvas = new GLCanvas( glCaps );
        canvas.setSize(this.getSize());
        canvas.setLocation(0, 0);
        this.add( canvas );

        canvas.addGLEventListener(this);
    }
    
    public void init(GLAutoDrawable drawable)
    {
    	GL gl = drawable.getGL();
        
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

        int[] v = new int[] {25, 25, 0,
            100, 325, 0,
            175, 25, 0,
            175, 325, 0,
            250, 25, 0,
            325, 325,  0};
        
        byte[] c = new byte[]{
        	(byte)255, (byte)0, (byte)0,
        	(byte)0, (byte)255, (byte)0,
        	(byte)0, (byte)0, (byte)255,
        	(byte)255, (byte)0, (byte)0,
        	(byte)0, (byte)255, (byte)0,
        	(byte)0, (byte)0, (byte)255,
        };

        IntBuffer vertices = BufferUtil.newIntBuffer(v.length);
        ByteBuffer colors = BufferUtil.newByteBuffer(c.length);
        
        vertices.put(v);
        colors.put(c);
        colors.rewind();
        vertices.rewind();
        
        gl.glEnableClientState (GL.GL_COLOR_ARRAY);
        gl.glEnableClientState (GL.GL_VERTEX_ARRAY);
        
        gl.glColorPointer (3, GL.GL_UNSIGNED_BYTE, 0, colors);
        gl.glVertexPointer (3, GL.GL_INT, 0, vertices);       
        
        anim = new Animator(canvas);
        anim.start();
    }

    public void display(GLAutoDrawable drawable)
    {
    	GL gl = drawable.getGL();
         
        gl.glClear(GL.GL_COLOR_BUFFER_BIT );
        
        IntBuffer buffer = BufferUtil.newIntBuffer(6);
        buffer.rewind();
        buffer.put(new int[]{0, 1, 2, 3, 4, 5}).rewind();
        gl.glDrawElements( GL.GL_TRIANGLES, 
				6,
				GL.GL_UNSIGNED_INT,
				buffer );
    }

    
    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
    {
    	GL gl = drawable.getGL();
        gl.glViewport (0, 0, drawable.getWidth(),  drawable.getHeight());
        gl.glMatrixMode (GL.GL_PROJECTION);
        gl.glLoadIdentity ();
        gl.glOrtho(0, drawable.getWidth(), 0, drawable.getHeight(), -1, 1);
    }

    public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { }

}

It runs fine. Two triangles adjacent to each other (one upside down). Now try running this code:


import java.applet.Applet;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;

import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;

import com.sun.opengl.util.Animator;
import com.sun.opengl.util.BufferUtil;

public class VertexArrayTest extends Applet implements GLEventListener
{
	Animator anim;
	GLCanvas canvas;
    
    public void start()
    {
    	this.setSize(640, 480);

        GLCapabilities glCaps = new GLCapabilities();

        canvas = new GLCanvas( glCaps );
        canvas.setSize(this.getSize());
        canvas.setLocation(0, 0);
        this.add( canvas );

        canvas.addGLEventListener(this);
    }
    
    public void init(GLAutoDrawable drawable)
    {
    	GL gl = drawable.getGL();
        
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

        int[] v = new int[] {25, 25, 0, 0,
            100, 325, 0, 0,
            175, 25, 0, 0,
            175, 325, 0, 0,
            250, 25, 0, 0,
            325, 325,  0,  0};
        
        byte[] c = new byte[]{
        	(byte)255, (byte)0, (byte)0,
        	(byte)0, (byte)255, (byte)0,
        	(byte)0, (byte)0, (byte)255,
        	(byte)255, (byte)0, (byte)0,
        	(byte)0, (byte)255, (byte)0,
        	(byte)0, (byte)0, (byte)255,
        };

        IntBuffer vertices = BufferUtil.newIntBuffer(v.length);
        ByteBuffer colors = BufferUtil.newByteBuffer(c.length);
        
        vertices.put(v);
        colors.put(c);
        colors.rewind();
        vertices.rewind();
        
        gl.glEnableClientState (GL.GL_COLOR_ARRAY);
        gl.glEnableClientState (GL.GL_VERTEX_ARRAY);
        
        gl.glColorPointer (3, GL.GL_UNSIGNED_BYTE, 0, colors);
        gl.glVertexPointer (3, GL.GL_INT, 4, vertices);       
        
        anim = new Animator(canvas);
        anim.start();
    }

    public void display(GLAutoDrawable drawable)
    {
    	GL gl = drawable.getGL();
         
        gl.glClear(GL.GL_COLOR_BUFFER_BIT );
        
        IntBuffer buffer = BufferUtil.newIntBuffer(6);
        buffer.rewind();
        buffer.put(new int[]{0, 1, 2, 3, 4, 5}).rewind();
        gl.glDrawElements( GL.GL_TRIANGLES, 
				6,
				GL.GL_UNSIGNED_INT,
				buffer );
    }

    
    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
    {
    	GL gl = drawable.getGL();
        gl.glViewport (0, 0, drawable.getWidth(),  drawable.getHeight());
        gl.glMatrixMode (GL.GL_PROJECTION);
        gl.glLoadIdentity ();
        gl.glOrtho(0, drawable.getWidth(), 0, drawable.getHeight(), -1, 1);
    }

    public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { }

}

I changed the vertex array to have an extraneous int in each row, and changed the glVertexPointer to stride over it (an int is 4 bytes, I hope I’m not that stupid :frowning: ). The triangles are no longer rendering correctly, it looks like the stride is not working correctly. Am I correct in assuming stride is the byte offset between vertexes, and a vertex in this case is made up of 3 ints? So shouldn’t a stride of 4 bytes be skipping over every 4th int?

Please let me know if I have it all wrong or if this is indeed a bug in JOGL 1.1.0 :slight_smile:

The stride is the byte offset between the start of one vertex and the start of the next vertex. So in your case it would be 4 bytes per int * 4 ints = 16.

Ah. My reading comprehension skills fail :frowning:

Code works perfectly now. Thank you for the swift reply Ken, and sorry to burden you with my newbness, I’m sure you get it quite frequently :open_mouth:

It confused me too a few years ago, as a stride of 0 is the default stride, which doesn’t make much sense.

The drivers convert that into the real stride for you behind the scenes.