JOGL Current release for Windows 64b - problem with VBO?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

JOGL Current release for Windows 64b - problem with VBO?

robcavin
I apologize in advance for this huge post, but this has been driving me nuts for a day.  I have certainly looked for solutions but with the number of GL versions and JOGL versions out there, it's tough to isolate the problem.

I recently started using JOGL and quickly got things up and running using immediate mode calls (glVertex2f, etc.).  However my goal was to use only forward compatible vertex array/buffer objects to pass the geometry to the shaders.  This is where things started breaking down.  I simply can not get any geometry to display when passed via VAO.  I reverted back to GL2 to allow glVertex2f calls and with these, I can see the geometry display and see the influence of the shaders.  This leads me to believe the VAO structure is at fault.  There is confusion regarding how to properly use FloatBuffer to pass in the data to the Vertex Buffer, but I've tried every permutation I can think of.  I've read the buffer back from GL and verified the vertices are there.  Not sure how to "read" back data from a VAO.  

I'm not an opengl expert but I did port this from a working example (admittedly opengl ES2).  I also tried the SimpleJOGL app for OpenGL3.2 at http://www.java-gaming.org/index.php?topic=21485.0.  Experienced the same problem - no geometry drawn at all.

I've attached the entire test app in one file in the hopes that someone spots where I'm being dumb.  

Thanks in advance for any help!

// jogl_test.java
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.charset.Charset;

import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.JFrame;

import com.jogamp.opengl.util.GLBuffers;

public class jogl_test implements GLEventListener  {

        private static final jogl_test instance = new jogl_test();
        private int shader_program, vertex_shader, fragment_shader;
        private int vertex_array, vertex_buffer;
       
        private float[] vertices;
       
        /**
         * @param args
         */
        public static void main(String[] args) {
        GLProfile glp = GLProfile.get(GLProfile.GL2);
        GLCapabilities caps = new GLCapabilities(glp);
        GLCanvas canvas = new GLCanvas(caps);
        canvas.addGLEventListener(instance);
       
        JFrame frame = new JFrame("AWT Window Test");
        frame.setSize(300, 300);
        frame.add(canvas);
        frame.setVisible(true);
       
        // by default, an AWT Frame doesn't do anything when you click
        // the close button; this bit of code will terminate the program when
        // the window is asked to close
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        }

        @SuppressWarnings("static-access")
        private int compileShader(String shaderPath, GL2 gl, int shader_type) throws Exception, IOException {
           
                BufferedReader brv = new BufferedReader(new FileReader(shaderPath));
            StringBuffer vsrc = new StringBuffer(256);
            String line;
            while ((line=brv.readLine()) != null) {
              vsrc.append(line + "\n");
            }
            brv.close();
            String[] vsrc_a = new String[] {vsrc.toString()};
            System.out.println(vsrc_a[0]);
           
            int v = gl.glCreateShader(shader_type);
            gl.glShaderSource(v, 1, vsrc_a, null);
            gl.glCompileShader(v);
           
            IntBuffer logLength = IntBuffer.allocate(1);
            ByteBuffer result = ByteBuffer.allocate(256);
            gl.glGetShaderiv(v, gl.GL_INFO_LOG_LENGTH, logLength);
            if (logLength.get(0) > 0) {
                gl.glGetShaderInfoLog(v, logLength.get(0), logLength, result);
                System.out.println("Shader compile log:\n" + Charset.defaultCharset().decode(result).toString());
            }
           
            IntBuffer status =  IntBuffer.allocate(1);
            gl.glGetShaderiv(v, gl.GL_COMPILE_STATUS, status);
            if (status.get(0) == 0) {
                gl.glDeleteShader(v);
                throw new Exception("Shader could not be compiled");
            }
           
            return v;
        }

        @SuppressWarnings("static-access")
        @Override
        public void init(GLAutoDrawable drawable) {
                // TODO Auto-generated method stub
            //drawable.setGL(new DebugGL2(drawable.getGL().getGL2()));
            GL2 gl = drawable.getGL().getGL2();
            //gl.glViewport(0, 0, drawable.getWidth(),drawable.getHeight());
           
            this.shader_program = gl.glCreateProgram();
           
            try {
            this.vertex_shader = compileShader("shaders/vertex_shader.glsl",gl,gl.GL_VERTEX_SHADER);
            this.fragment_shader = compileShader("shaders/fragment_shader.glsl",gl,gl.GL_FRAGMENT_SHADER);
            }
            catch (Exception e) {
            System.err.println("Error compiling shader: " + e.getMessage());
            }
           
            gl.glAttachShader(this.shader_program, this.vertex_shader);
            gl.glAttachShader(this.shader_program, this.fragment_shader);
           
            gl.glBindAttribLocation(this.shader_program, 0, "position");  // 0 = position always
            gl.glBindFragDataLocation(this.shader_program,0,"fragColor");

            gl.glLinkProgram(this.shader_program);
            IntBuffer status = IntBuffer.allocate(1);
           
            IntBuffer logLength = IntBuffer.allocate(1);
            ByteBuffer result = ByteBuffer.allocate(256);
            gl.glGetProgramiv(this.shader_program, gl.GL_INFO_LOG_LENGTH, logLength);
            if (logLength.get(0) > 0) {
                gl.glGetProgramInfoLog(this.shader_program, logLength.get(0), logLength, result);
                System.out.println("Shader compile log:\n" + Charset.defaultCharset().decode(result).toString());
            }

           
            gl.glGetProgramiv(this.shader_program, gl.GL_LINK_STATUS, status);
            if (status.get(0) == 0) {
                System.err.println("Could not link program");
            }
           
            System.out.println("Position id = " + gl.glGetAttribLocation(this.shader_program, "position"));
            System.out.println("Color id = " + gl.glGetFragDataLocation(this.shader_program, "fragColor"));
           
            IntBuffer vertexArrayIds = IntBuffer.allocate(1);
            gl.glGenVertexArrays(1,vertexArrayIds);
            this.vertex_array = vertexArrayIds.get(0);
            gl.glBindVertexArray(this.vertex_array);

            IntBuffer vertexBufferIds = IntBuffer.allocate(1);
            gl.glGenBuffers(1, vertexBufferIds);
            this.vertex_buffer = vertexBufferIds.get(0);
            gl.glBindBuffer(gl.GL_ARRAY_BUFFER, this.vertex_buffer);
           
            vertices = new float[] {
            -1f, -1f, 0f,
            0f, -1f, 0f,
            1f, -1f, 0f
            };
            FloatBuffer buffer = GLBuffers.newDirectFloatBuffer(vertices,0);
            //buffer.put(vertices);
            //buffer.rewind();
            gl.glBufferData(gl.GL_ARRAY_BUFFER, buffer.capacity()*4, buffer, gl.GL_STATIC_DRAW);
           
            gl.glEnableVertexAttribArray(0); // Position
            gl.glVertexAttribPointer(0, 3, gl.GL_FLOAT, false, 0, 0);
            ByteBuffer testBuffer = GLBuffers.newDirectByteBuffer(buffer.capacity()*4);
            gl.glGetBufferSubData(gl.GL_ARRAY_BUFFER, 0, buffer.capacity()*4, testBuffer);
            testBuffer.rewind();    
            for (int i = 0; i < 36; i++)  System.out.println(testBuffer.get(i) + ",");
            FloatBuffer foob = testBuffer.asFloatBuffer();
            for (int i = 0; i < 9; i++)  System.out.println(foob.get(i) + ",");
           
            gl.glBindBuffer(gl.GL_ARRAY_BUFFER, 0);
            gl.glBindVertexArray(0);  // Reset binding
           
            System.out.println("GL Error code : " + gl.glGetError());

        }

        @Override
        public void dispose(GLAutoDrawable drawable) {
                // TODO Auto-generated method stub
               
        }

        @Override
        public void display(GLAutoDrawable drawable) {
                // TODO Auto-generated method stub
            GL2 gl = drawable.getGL().getGL2();
            gl.glClearColor(0.65f, 0.65f, 0.65f, 1.0f);
            gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT);

            gl.glBindVertexArray(this.vertex_array);
            gl.glBindBuffer(gl.GL_ARRAY_BUFFER, this.vertex_buffer);
            gl.glEnableVertexAttribArray(0);
            gl.glVertexAttribPointer(0, 3, gl.GL_FLOAT, false, 3 * Float.SIZE, 0);

            gl.glUseProgram(this.shader_program);
            gl.glDrawArrays(gl.GL_TRIANGLES, 0, 3);
            System.out.println("GL Error code : " + gl.glGetError());

            /*gl.glBegin(gl.GL_TRIANGLES);
            gl.glVertex2f(-1, -1);
            gl.glVertex2f(0, 1);
            gl.glVertex2f(1, -1);
            gl.glEnd();*/

        }

        @Override
        public void reshape(GLAutoDrawable drawable, int x, int y, int width,
                        int height) {
                // TODO Auto-generated method stub
                GL2 gl = drawable.getGL().getGL2();
            gl.glViewport(0, 0, width, height);
        }

}


//vertex_shader.glsl
#version 150

in vec4 position;
void main()
{
    gl_Position = position;
}

//fragment_shader.glsl
#version 150
out vec4 fragColor;
void main()
{
        fragColor = vec4(1.0,0.0,1.0,1.0);
}
Reply | Threaded
Open this post in threaded view
|

Re: JOGL Current release for Windows 64b - problem with VBO?

gouessej
Administrator
Hi

Please can you try to use VBOs but no VAOs as a first step? It would be helpful to know whether your problem comes from the latter.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: JOGL Current release for Windows 64b - problem with VBO?

robcavin
I tried the following - still the same.  No geometry displays.  I think this covers your suggestion, but please let me know if you see an obvious issue here.  Again, pretty new to opengl.  Thanks!

In the init function :

            IntBuffer vertexBufferIds = IntBuffer.allocate(1);
            gl.glGenBuffers(1, vertexBufferIds);
            this.vertex_buffer = vertexBufferIds.get(0);
            gl.glBindBuffer(gl.GL_ARRAY_BUFFER, this.vertex_buffer);
           
            vertices = new float[] {
            -1f, -1f, 0f,
            0f, -1f, 0f,
            1f, -1f, 0f
            };
            FloatBuffer buffer = GLBuffers.newDirectFloatBuffer(vertices,0);
            gl.glBufferData(gl.GL_ARRAY_BUFFER, buffer.capacity()*4, buffer, gl.GL_STATIC_DRAW);
            gl.glBindBuffer(gl.GL_ARRAY_BUFFER, 0);


In the display function :

            gl.glBindBuffer(gl.GL_ARRAY_BUFFER, this.vertex_buffer);
            gl.glVertexPointer(3, gl.GL_FLOAT, 0, 0);
            gl.glBindBuffer(gl.GL_ARRAY_BUFFER, this.vertex_buffer);  // Redundant just in case the state was updated by the glVertexPointer call
            gl.glEnableClientState(gl.GL_VERTEX_ARRAY);
            gl.glUseProgram(this.shader_program);
            gl.glDrawArrays(gl.GL_TRIANGLES, 0, 3);
            gl.glFlush();  // Just in case



Reply | Threaded
Open this post in threaded view
|

Re: JOGL Current release for Windows 64b - problem with VBO?

gouessej
Administrator
I know it is not very important but please use Buffers.newDirectFloatBuffer(int) to create your direct NIO buffers containing floats.

Try to run our existing examples to check if there is something wrong in your configurations.

Edit.: Does the following one work?
https://github.com/sgothel/jogl/blob/master/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/RedSquareES2.java

Which OpenGL driver do you use?
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: JOGL Current release for Windows 64b - problem with VBO?

robcavin
I had tried several combinations of the floatbuffer definition, including Buffers.newDirectFloatBuffer(int).  Tried again and still no dice.

The example code did work, but unfortunately it is hiding a lot of the underlying GL calls.

I used the TraceGL2 implementations to trace both the working sample you pointed me too and the code above.   After some edits the only difference I can see boils down to the way glVertexAttribPointer is called.

Mine :
glBindBuffer(<int> 0x8892, <int> 0x1)
glBufferData(<int> 0x8892, <long> 36, <java.nio.Buffer> java.nio.DirectFloatBufferU[pos=0 lim=9 cap=9], <int> 0x88E4)
glEnableVertexAttribArray(<int> 0x0)
glVertexAttribPointer(<int> 0x0, <int> 0x3, <int> 0x1406, <boolean> false, <int> 0x0, <long> 0)

Example:
glBindBuffer(<int> 0x8892, <int> 0x1)
glBufferData(<int> 0x8892, <long> 48, <java.nio.Buffer> java.nio.DirectFloatBufferU[pos=0 lim=12 cap=12], <int> 0x88E4)
glGetAttribLocation(<int> 0x1, <java.lang.String> mgl_Vertex) = 0

glVertexAttribPointer(<javax.media.opengl.GLArrayData> GLArrayDataServer[mgl_Vertex, index -1, location 0, isVertexAttribute true, dataType 0x1406, bufferClazz class java.nio.FloatBuffer, elements 4, components 3, stride 12b 3c, initialElementCount 4, vboEnabled true, vboName 1, vboUsage 0x88e4, vboTarget 0x8892, vboOffset 0, sealed true, bufferEnabled false, bufferWritten true, buffer java.nio.DirectFloatBufferU[pos=0 lim=12 cap=12], alive true])

The glVertexAttribPointer call used in the sample does not seem to be a normal mapping on top of the GL spec.  Not really sure how to tell what is going on there.  Is there any documentation on this that I'm missing?


Reply | Threaded
Open this post in threaded view
|

Re: JOGL Current release for Windows 64b - problem with VBO?

Sven Gothel
Administrator
On 09/11/2012 09:32 PM, robcavin [via jogamp] wrote:

> I had tried several combinations of the floatbuffer definition, including
> Buffers.newDirectFloatBuffer(int).  Tried again and still no dice.
>
> The example code did work, but unfortunately it is hiding a lot of the
> underlying GL calls.
>
> I used the TraceGL2 implementations to trace both the working sample you
> pointed me too and the code above.   After some edits the only difference I
> can see boils down to the way glVertexAttribPointer is called.
>
> Mine :
> glBindBuffer(<int> 0x8892, <int> 0x1)
> glBufferData(<int> 0x8892, <long> 36,
> <java.nio.Buffer> java.nio.DirectFloatBufferU[pos=0 lim=9 cap=9], <int> 0x88E4)
> glEnableVertexAttribArray(<int> 0x0)
> glVertexAttribPointer(<int> 0x0, <int> 0x3, <int> 0x1406, <boolean> false,
> <int> 0x0, <long> 0)
>
> Example:
> glBindBuffer(<int> 0x8892, <int> 0x1)
> glBufferData(<int> 0x8892, <long> 48,
> <java.nio.Buffer> java.nio.DirectFloatBufferU[pos=0 lim=12 cap=12], <int> 0x88E4)
> glGetAttribLocation(<int> 0x1, <java.lang.String> mgl_Vertex) = 0
>
> glVertexAttribPointer(<javax.media.opengl.GLArrayData> GLArrayDataServer[mgl_Vertex,
> index -1, location 0, isVertexAttribute true, dataType 0x1406, bufferClazz
> class java.nio.FloatBuffer, elements 4, components 3, stride 12b 3c,
> initialElementCount 4, vboEnabled true, vboName 1, vboUsage 0x88e4, vboTarget
> 0x8892, vboOffset 0, sealed true, bufferEnabled false, bufferWritten true,
> buffer java.nio.DirectFloatBufferU[pos=0 lim=12 cap=12], alive true])
>
> The glVertexAttribPointer call used in the sample does not seem to be a normal
> mapping on top of the GL spec.  Not really sure how to tell what is going on
> there.  Is there any documentation on this that I'm missing?
>
I will have a look at this these days,
can't tell right now - not seeing an obvious mistake.

Thanks for providing a test case.

~Sven



signature.asc (907 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: JOGL Current release for Windows 64b - problem with VBO?

robcavin
Thanks.

I did figure out the problem by comparing with the sample code GL trace.  I'm sure I had several small problems that were addressed along the way, but the final issue was too dumb to mention. :D

In the end I was able to reproduce the same behavior as the sample without using that special form of GLVertexAttribPointer.