Switching openGL context

edited May 2017 in Questions about Code

I recently had this discussion on stack overflow http://stackoverflow.com/questions/43829384/opengl-state-reset?noredirect=1#comment74705693_43829384

and a user pointed me out that maybe i am leaving the openGL context in a different state each run, and that maybe i can switch openGL context.

wich made me look into: https://forum.processing.org/two/discussion/2879/processing-2-1-1-how-to-get-the-gl-context

Of course almost anyone seems more in the know about openGL JOGL ecc... than me and im REALLY far from attempting a pull.

i was wondering if it was possible to do something like

"switching context"

    frame.addWindowListener( new WindowAdapter() {
            @ Override
            public void windowClosing(WindowEvent we) {

                "clearing or switching context"

                System.exit(0);
            }
        } );

and/or maybe if someone can advise me on renderers used by processing or possible combinations to obtain a clean state.

Beacause using awt in addition to processing seems usefull enough to me and its working 80% of the time. of course 20% random fail makes what im doing trash.

Answers

  • edited May 2017

    As a user on stackoverflow already mentioned. Its Most likely that you get a solution, when people can debug some code and contribute I know few things about OpenGL, i have no idea of ATW. I recomend to post a minimal example of ATW&Processing&JOGL you are using. Could look like this: https://www.javatpoint.com/java-awt

    Just to clearing the buffer(any content) JOGL could look like this: https://github.com/sgothel/jogl-demos/blob/master/src/demos/es2/RawGL2ES2demo.java#L557

    You don't have to you can run 10 fragmentShader and release only 2 of them and use the others by something else.

    I not sure what you are meaning with switching content. You have a single application with multiple windows? Basically you write something to a "buffer" and it will be their until you don't release it. So switching content you mean switching buffer, i guess.

    Good Luck.

  • edited May 2017

    the awt components are like this

    public class FormVM extends Panel implements ActionListener,Drawable{

    private ActionListener father;
    
    private String command;
    private String[] names;
    
    private ArrayList<JTextArea> fields;
    
    private int x,y;
    public FormVM(int x,int y,String command,String[] names,String[] initVals,ActionListener al) {
        fields=new ArrayList<JTextArea>();
        this.x=x;
        this.y=y;
        this.names=names;
        this.command=command;
        this.setLayout(null);
        JButton rem=new JButton("command");
        rem.addActionListener(this);
        this.setBounds(x,y,200,15*names.length);
        rem.setBounds(180,0,20,15*names.length);
        this.setSize(200,15*names.length);
        this.father=al;
        this.add(rem);
        for(int i=0;i<names.length;i++){
            JTextArea area = new JTextArea(x,y+i*15);
            area.setText(initVals[i]);
            this.add(area);
            area.setBounds(0,i*15,180,15);
            area.setBorder(BorderFactory.createLineBorder(Color.black));
            area.setEditable(true);
            fields.add(area);
            this.add(area);
        }
    
    }
    
    public void setup(){
        ((Container)father).add(this);
    }
    
    public ActionListener getFather() {
        return father;
    }
    public void setFather(ActionListener father) {
        this.father = father;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        String[] vals=new String[names.length];
        for(int i=0;i<names.length;i++){
            vals[i]=fields.get(i).getText();
        }
        ActionEvent remove=new ActionEvent(vals, 0, command);
        father.actionPerformed(remove); 
    }
    @Override
    public void draw() {
    
    
    }
    

    }

    in the setup() method they add themselves to the PApplet

    a custom component is like this

    public class RenderArea extends XYGroup{

    private ArrayList<XYEntity> childs=new ArrayList<XYEntity>();
    private float mW,mH;
    
    private PApplet father;
    
    private float 
            sliderX=0,
            sliderY=0;
    
    public RenderArea(float x, float y, int w, int h, float mW, float mH,
            PApplet father) {
        super();
        childs=new ArrayList<XYEntity>();
        this.posX = x;
        this.posY = y;
        this.w = w;
        this.h = h;
        this.mW = mW;
        this.mH = mH;       
        styleC=father.color(180,40,40);
        style=father.color(60,60,60);
        styleB=father.color(0,0,0);
        this.father=father;
    }
    
    public ArrayList<XYEntity> getChilds() {
        return childs;
    }
    
    public void setChilds(ArrayList<XYEntity> childs) {
        this.childs = childs;
    }
    
    public void addChild(Object child){
        this.childs.add((XYEntity) child);
    }
    
    public float getSliderX() {
        return sliderX;
    }
    
    public void setSliderX(float sliderX) {
        this.sliderX = sliderX;
    }
    
    
    
    public float getSliderY() {
        return sliderY;
    }
    
    public void setSliderY(float sliderY) {
        this.sliderY = sliderY;
    }
    
    
    
    public float getX() {
        return posX;
    }
    
    public void setX(float x) {
        this.posX = x;
    }
    
    public float getW() {
        return w;
    }
    
    
    
    public float getH() {
        return h;
    }
    
    
    
    public float getmW() {
        return mW;
    }
    
    public void setmW(float mW) {
        this.mW = mW;
    }
    
    public float getmH() {
        return mH;
    }
    
    public void setmH(float mH) {
        this.mH = mH;
    }
    
    
    float l=20;
    int styleC;
    int styleB;
    int style;
    
    public void draw(){
        this.render(father);
    }
    
    @Override
    public void render(Container view) { 
    
        boolean x1,x2,x3;
        boolean y1,y2,y3;
        boolean b1,b2,b3,b4; 
        x1=father.mouseX-posX>0&&father.mouseX-posX<l;
        x2=father.mouseX-(posX+w-l)>0&&father.mouseX-(posX+w-l)<l;
        x3=father.mouseX-(posX+w)>0&&father.mouseX-(posX+w)<l;
        y1=father.mouseY-posY>0&&father.mouseY-posY<l;
        y2=father.mouseY-(posY+h-l)>0&&father.mouseY-(posY+h-l)<l;
        y3=father.mouseY-(posY+h)>0&&father.mouseY-(posY+h)<l;
    
        boolean hit1,hit2,hit3,hit4,hitX,hitY;
        hit1=x1&&y3;
        hit2=x2&&y3;
        hit3=y1&&x3;
        hit4=y2&&x3;
        hitX=!hit1&&!hit2&&y3&&father.mouseX<posX+w-l&&father.mouseX>posX+l;
        hitY=!hit3&&!hit4&&x3&&father.mouseY<posY+h-l&&father.mouseY>posY+l;
        father.fill(styleB);
        father.stroke(25,25,200);
        father.rect(posX, posY, w, h);
        father.fill(hit1?styleC:style);
        father.rect(posX, posY+h, l, l);
        father.fill(hit3?styleC:style);
        father.rect(posX+w, posY, l, l);
        father.fill(hit2?styleC:style);
        father.rect(posX+w-l, posY+h, l, l);
        father.fill(hit4?styleC:style);
        father.rect(posX+w, posY+h-l, l, l);
        father.fill(hitX?styleC:style);
        father.rect(posX+l+(-sliderX*(w-2*l)/(mW-w))/*(w-2*l)*/, posY+h, l, l);
        father.fill(hitY?styleC:style);
        father.rect(posX+w, posY+l+(-sliderY*(h-2*l)/(mH-h))/**(h-2*l)*/, l, l);
        boolean down=father.mousePressed;
    
        father.clip(posX, posY, w, h);
    
        if(father.mouseX-posX>0&&father.mouseX<posX+w+l&&
           father.mouseY-posY>0&&father.mouseY<posY+h+l)
            sliderY-=father.mouseEvent.getCount()*7;
        father.mouseWheel();//consuma l'evento
        sliderX=sliderX>0?0:sliderX<-mW+w?-mW+w:sliderX;
        sliderY=sliderY>0?0:sliderY<-mH+h?-mH+h:sliderY;
        for(int i=0;i<childs.size();i++){
            XYEntity child=childs.get(i); 
            if(down){ 
                sliderX+=hit1?+1:hit2?-1:0;
                sliderY+=(hit3?+1:hit4?-1:0);
                sliderX=hitX?-(((mW-w)/(w-2*l))*(father.mouseX-posX-l)):sliderX;
                sliderY=hitY?-(((mH-h)/(h-2*l))*(father.mouseY-posY-l)):sliderY;
                sliderX=sliderX>0?0:sliderX<-mW+w?-mW+w:sliderX;
                sliderY=sliderY>0?0:sliderY<-mH+h?-mH+h:sliderY;
            }
            //PMatrix m=father.getMatrix().get();
            //father.resetMatrix();
            father.pushMatrix(); 
            try{
                father.translate(sliderX, sliderY); 
                child.xt=sliderX+this.xt;
                child.yt=sliderY+this.yt;
                child.render(father);
            }catch(Exception e){
                e.printStackTrace();
            }
            father.popMatrix();
            //father.getMatrix().set(m);
        }
        father.noClip();
    
    }
    
    @Override
    public void setDrawMode(int mode) {
        // TODO Auto-generated method stub
    
    }  
    

    }

    and it is rendered in the main PApplet draw() loop like this

    public void draw(){
        background(0,0);
        if(!endLoading)
            splash.render();
        else{
            if(cRoot!=null&&cRoot.getChilds()!=null){
                ToolTipVM.instance.text=null;
                for(int i=0;i<cRoot.getChilds().size();i++){
                    if(cRoot.getChilds().get(i)!=null&&cRoot.getChilds().get(i).getInstance()!=null&&cRoot.getChilds().get(i).getDraw()!=null){
                        try {
                            //default 
                            this.stroke(188);
                            this.fill(188);
    
                            cRoot.getChilds().get(i).getInstance().draw();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
    
                ToolTipVM.instance.render(this);
    
            }
        }
    }
    

    In the above example RenderArea renders the components within it after translating them with slidebars, it also clips the area so that it can contain an area larger than itself.

    This is only an example, but here the push/pop and the clip() functions break up at random at the very beginning of the run, else they dont at all.

    the loading is made by detecting all components, instantiating them, and calling their setup method.

    There is only one instance of PApplet per jvm, and multiple jvms can run without problems, only i occasionally get draw loop fails on push/pop clip() ecc...

    also i saw sometimes a pop failing right after a push, with too many pop exception suggesting that there are interactions with "something" other in graphics, i guess the awt thread but i dont know at all about graphics, i tought this was a rather simple thing to do and just wanted a clean start.

  • edited May 2017

    @JDev

    personal im out of time. what i can say: If you building jogl (with atw) from source the tests runnig about **1 hour ** various window creations (multiple windows, splitwindows, offscreen etc) tones of examples. hope you will find what you search

    https://jogamp.org/jogl/doc/HowToBuild.html

    https://github.com/sgothel/jogl/tree/master/src/test/com/jogamp/opengl

    try also to post the same thing into the jogl forum, the users their are more experience with atw.

    http://forum.jogamp.org/jogl-f782158.html

Sign In or Register to comment.