Sample code used to work in JOGL 1. How do I make it work in JOG 2?

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

Sample code used to work in JOGL 1. How do I make it work in JOG 2?

s3a
Could someone please help me get the following JOGL 1 code to work in JOGL 2? The code is nothing mission-critical but I just want to see a before and after when converting to JOGL 2 so I can start to grasp the differences between the versions. (Ignore the questions in the actual code itself).

import java.nio.FloatBuffer;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
//import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.JFrame;

public class MyClass implements GLEventListener
{

    public void init(GLAutoDrawable glad) {
        // code
    }

    public void display(GLAutoDrawable drawable) {


        GL gl = drawable.getGL();

        gl.glClearColor(0.1f, 0.5f, 1.0f, 0.7f);
        gl.glClear(GL.GL_COLOR_BUFFER_BIT);
        gl.glViewport(0, 0, 300, 300);
//        gl.glColor3f(1.0f, 1.0f, 1.0f);

        // An array of 3 vectors which represents 3 vertices
        /*static final */float[] g_vertex_buffer_data = {
           -1.0f, -1.0f, 0.0f,
           1.0f, -1.0f, 0.0f,
           0.0f,  1.0f, 0.0f,
        };

        // This will identify our vertex buffer
        int[] vertexbuffer = new int[1];

        // Generate 1 buffer, put the resulting identifier in vertexbuffer
        gl.glGenBuffers(1, vertexbuffer, 0);

        // The following commands will talk about our 'vertexbuffer' buffer
        gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vertexbuffer[0]);

        // Give our vertices to OpenGL.
//        gl.glBufferData(GL.GL_ARRAY_BUFFER, 3/*sizeof(g_vertex_buffer_data)*/, null/*g_vertex_buffer_data*/, GL.GL_STATIC_DRAW); // WHAT DO I PUT AS THE SECOND PARAMETER?
        gl.glBufferData(GL.GL_ARRAY_BUFFER, 12, FloatBuffer.wrap(g_vertex_buffer_data), GL.GL_STATIC_DRAW);

        // 1rst attribute buffer : vertices
        gl.glEnableVertexAttribArray(0);
        gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vertexbuffer[0]);
        gl.glVertexAttribPointer(
           0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
           3,                  // size
           GL.GL_FLOAT,           // type
           false,           // normalized?
           0,                  // stride
           /*(void*)*/0            // array buffer offset
        );

        gl.glColor3f(0.0f, 0.0f, 0.0f);
        // Draw the triangle !
        gl.glDrawArrays(GL.GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle

        gl.glDisableVertexAttribArray(0);
        gl.glFlush();

    }

    public void reshape(GLAutoDrawable glad, int i, int i1, int i2, int i3) {
        // code
    }

    public void displayChanged(GLAutoDrawable glad, boolean bln, boolean bln1) {
        // code
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("TheFrame");
        GLCanvas canvas = new GLCanvas();
        canvas.addGLEventListener(new MyClass());
        frame.add(canvas);
        frame.setSize(300,300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}


Any input would be greatly appreciated!
Thanks in advance!
Reply | Threaded
Open this post in threaded view
|

Re: Sample code used to work in JOGL 1. How do I make it work in JOG 2?

gouessej
Administrator
please look at the example i put on wikipedia. i'm on my htc desire z, it is not easy...
Julien Gouesse | Personal blog | Website
s3a
Reply | Threaded
Open this post in threaded view
|

Re: Sample code used to work in JOGL 1. How do I make it work in JOG 2?

s3a
This might sound weird but being told where the answer is helps me find the answer more easily when I look there (in this case Wikipedia).

Basically, I told Netbeans to implement all abstract methods and I changed the GL gl = drawable.getGL(); line to GL2 gl = drawable.getGL().getGL2(); and then it worked.

Thanks!