Bezier curve

Hello everyone

I want to draw a Bezier curve by using JOGL. In the class showing bellow, I display my 8 control points (that’s okay for them) in red and my Bezier curve in blue. The problem is that my curve starts from the first point (nice !) but doesn’t finish at the last point. Moreover, the result of the curve is not really what I have expected.
Here’s a part of the class :


class Renderer implements GLEventListener, KeyListener {
   
    private float [][]ctrlpts = new float [8][3];
   
    public void display(GLDrawable gLDrawable) {
        final GL gl = gLDrawable.getGL();
        gl.glClear(GL.GL_COLOR_BUFFER_BIT);
        gl.glColor3f((float)0, (float)0, (float)1);
        gl.glBegin(gl.GL_LINE_STRIP);
        for (int i=0; i<=100; i++)
            gl.glEvalCoord1f((float)(i/100));
        gl.glEnd();
        gl.glPointSize((float)5);
        gl.glColor3f((float)1, (float)0, (float)0);
        gl.glBegin(gl.GL_POINTS);
        for (int i=0; i<8; i++)
            gl.glVertex3fv(ctrlpts[i]);
        gl.glEnd();
        gl.glFlush();
    }
   
    public void displayChanged(GLDrawable gLDrawable, boolean modeChanged, boolean deviceChanged) {
    }
   
    public void init(GLDrawable gLDrawable) {
        final GL gl = gLDrawable.getGL();
        ctrlpts = buildPts();
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        gl.glShadeModel(GL.GL_SMOOTH);
        gl.glMap1f(GL.GL_MAP1_VERTEX_3, (float)0, (float)1, 3, ctrlpts.length, ctrlpts[0]);
        gl.glEnable(GL.GL_MAP1_VERTEX_3);
    }
   
    public void reshape(GLDrawable gLDrawable, int x, int y, int width, int height) {
        final GL gl = gLDrawable.getGL();
        final GLU glu = gLDrawable.getGLU();
        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(gl.GL_PROJECTION);
        gl.glLoadIdentity();
        glu.gluOrtho2D(-50, width, -50, height);
        gl.glMatrixMode(gl.GL_MODELVIEW);
    } 

Thank you very much for your answers ! :slight_smile:

Bye bye

You are lucky, I just finished that one ! :slight_smile:

The problem is that Java doesn’t handle multidimensional arrays like C. So you must declare your control points in a 1D array :

private float ctrlpts = new float [8 * 3]; 

and then call glMap1f as follow :

gl.glMap1f(GL.GL_MAP1_VERTEX_3, 0.0f, 1.0f, 3, 8, ctrlpts); 

Thank you, you’re right. That’s strange that JOGL doesn’t accept multidimensionnal matrices just like C :o

In C, when you declare a multidimensional array, it is stored in memory as if it were 1D therefore you can cast a type[][] to a type[] with no error.

AFAIK this is not the case in Java. I think the VM is free to do some memory management with nD arrays (such as storing them in non contiguous memory areas).

It’s because Java does not have true multi-dimensional arrays, an array like int[][] is actually an array of arrays and therefore almost certainly not contigious.

So don’t blame JOGL, blame Sun ;D