gl and OOP

Im sure im gonna sound dumb…but here i go. So I’ve read about the restriction about multi thread and gl context in that I cant create objects and then pass GL to my methods…I don’t like that lol, is there a way around it? for example
if i have a cube class and I want to be able to construct the cube from my main class and then have cube take care of itself as usual in OOD
and I want to put a method inside of the cube class called drawCube which contains the instructions to draw the figure.
I know why this won’t work the way Im trying it but is there a way to do this? will virtual methods work?

Ok, so I tryed something else instead, it works but im not sure how stable it is or if it is good practice
here is a sample class im working with



public class Cube {
    
    int color;
    int[] sides;
    
    double[] accumTransMatrix = new double[16];
    
    Cube(){
        
        color = 1;
        sides = new int[6];
        
    }
    
    public void drawCube(GLAutoDrawable drawable){
        GL gl = drawable.getGL();
        
        
        //draw and transform cube
        gl.glColor3f(1.0f, 1.0f, 1.0f);
        gl.glBegin(GL.GL_LINE_LOOP);
        gl.glVertex3d(1.0, 2.0, 1.0);
        gl.glVertex3d(2.0, 1.0, 1.0);
        gl.glVertex3d(0.0, 0.0, 0.0);
        gl.glEnd();
                    
    }
    
}

nothing fancy. At first I was trying to pass in my GL object, instead now im passing in my GLAutoDrawable and using the drawable.getGL()
is this a proper way to do this? it works but is it right?

You can have a one time made reference to the gl context and use this whenever you want as long as you are within the drawing thread and the gl context. The FPSAnimator or a class provided by you calls the display or repaint function of the drawable. The drawable itself will call a display method, which has to be implemented by you. From within this method you are save to draw everything you want.

You might want to provide some interface “Renderable” with a function “public void render(GL gl)” and pass the gl reference when calling your objectsto render themselves from within the display method. You grab the reference when “init(GLAutoDrawable drawable)” is called by “gl = drawable.getGL()”.
Another way is to give all your renderable objects the gl reference in advance.

Storing the gl ogject itself is dangerous. Just use the “Renderable” interface with a “public void render(GL gl)” method. Collect all renderables and iterate over them in the display() callback. If you advance further with your app, you might want to do state/depth sorting on your Renderables anyway.