GLCapabilities Enumeration Demo?

Hi. I have an enumeration demo for the jogl, but I don’t know how GLCapabilities will changed as it migrate to the jrs231. I can post it here for inclusion in the demos page, and also to iron out any bugs. Lemme know.

  • AK77

Thanks. How does it compare to the demos.printext.PrintExt demo? Could you attach it to a message here?

Here it is, Ken.


/*
 * GLCapsPaneDemo.java
 *
 * Created on August 15, 2005, 3:10 AM
 */

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import net.java.games.jogl.*;
import net.java.games.jogl.util.*;
/**
 * Cycle through your system's list of GLCapabilities with
 * the Prev and Next buttons, then Create Canvas to realise
 * the choosen caps. The chosen caps (curCaps) is an index number that
 * replaces the window system recommended choice in the 
 * chooseCapabilities function.
 *
 * @author Kiet Le
 */
public class GLCapsPaneDemo
{
  private static JFrame jframe = null;
  
  private GLCapabilities caps = new GLCapabilities();
  private GLCapsExt chooser = new GLCapsExt();
  private GLCapabilitiesPane capsPane = new GLCapabilitiesPane(caps);
  private GLCanvas canvas = null;
  private Gears gears = new Gears();
  private GLAnimator glanimator;// = new GLAnimator(canvas, 30);
  
  private JToolBar toolBar = new JToolBar();
  private JLabel preferred = new JLabel
    ("GLCapabilities index: " + chooser.getCurrentIndex());
  private JButton next = new JButton("Next");
  private JButton prev = new JButton("Previous");
  private JButton create = new JButton("Create Canvas");
  private JButton create2 = new JButton("Create GLJPanel");
  
  
  private GraphicsDevice device;
  /** Creates a new instance of GLCapsPaneDemo */
  public GLCapsPaneDemo()
  {
    device = GraphicsEnvironment.
      getLocalGraphicsEnvironment().
      getDefaultScreenDevice();
    jframe = new JFrame("GLCapsPaneDemo");
    
    toolBar.setFloatable(false);
    toolBar.add(preferred);
    toolBar.addSeparator();
    toolBar.add(prev);
    toolBar.add(next);
    toolBar.add(create);
    
    next.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        caps = chooser.getNext();
        capsPane.update(caps);
        preferred.setText("GLCapabilities index: "
          + chooser.getCurrentIndex());
      }
    });
    prev.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        caps = chooser.getPrevious();
        capsPane.update(caps);
        preferred.setText("GLCapabilities index: "
          + chooser.getCurrentIndex());
      }
    });
    
    create.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        if (canvas != null)
        {
          canvas.removeGLEventListener(gears);
          if (glanimator != null)
            glanimator.stop();
          glanimator = null;
          jframe.remove(canvas);
          canvas = null;
        }
        
        //caps = capsPane.getUserCaps();
        canvas = GLDrawableFactory.getFactory().
          createGLCanvas(caps, chooser, null, device);
        preferred.setText("Capabilities index: "
          + chooser.getCurrentIndex());
        capsPane.update(chooser.getGLCapabilities());

        canvas.addGLEventListener(gears);        
        if (!chooser.getSystemRecommendedCapabilities().
          getDoubleBuffered())
          canvas.setAutoSwapBufferMode(false);
        jframe.add(canvas);
        if (glanimator == null)
          new GLAnimator(canvas, 30).start();
        
        SwingUtilities.updateComponentTreeUI(canvas);
      }
    });
    
    create2.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        // TODO: create GLJPanel 
      }
    });
  }
  public void runExit()
  {
    new Thread(new Runnable()
    {
      public void run()
      {
        glanimator.stop();
      }
    }).start();
  }
  public void run(String [] args)
  {
    jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jframe.setSize(1024, 512);
    
    jframe.add(toolBar, BorderLayout.NORTH);
    jframe.add(capsPane, BorderLayout.LINE_START);
   
    jframe.setVisible(true);
  }
  public static void main(String [] args)
  {
    System.setProperty("JOGL_SINGLE_THREADED_WORKAROUND", "true");
    System.setProperty("Dsun.java2d.noddraw", "true");
    System.setProperty("sun.awt.noerasebackground","true");
    
    new GLCapsPaneDemo().run(args);
  }
  
  

/* GLCapabilities panel */
class GLCapabilitiesPane
extends JPanel
{
private JCheckBox hwAccelBuf = new JCheckBox(“Hardware Accelerated”);
private JCheckBox doubleBuf = new JCheckBox(“Double Buffered”);
private JCheckBox sampleBuf= new JCheckBox(“Sample Buffers”);
private JCheckBox stereoBuf = new JCheckBox(“Stereo Buffers”);

private JCheckBox pbufFloat = new JCheckBox("Floating-Point Buffers");
private JCheckBox pbufRTTex = new JCheckBox("Render-to-Texture");
private JCheckBox pbufRTTexRect = new JCheckBox("Render-to-Texture Rectangle");

private JSlider [] rgbaSliders = new JSlider[]
{
  new JSlider(), new JSlider(), new JSlider(), new JSlider()
};
private JSlider depthSlider = new JSlider();
private JSlider stencilSlider = new JSlider();
private final JSlider numSampleSlider = new JSlider();
private JSlider [] accumSliders = new JSlider[]
{
  new JSlider(), new JSlider(), new JSlider(), new JSlider()
};

private GLCapabilities chosenCaps = new GLCapabilities();
private GLCapabilities desiredCaps = new GLCapabilities();

public GLCapabilitiesPane(GLCapabilities caps)
{
  setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
  desiredCaps = caps;
  
  hwAccelBuf.setSelected(desiredCaps.getHardwareAccelerated());
  doubleBuf.setSelected(desiredCaps.getDoubleBuffered());
  sampleBuf.setSelected(desiredCaps.getSampleBuffers());
  stereoBuf.setSelected(desiredCaps.getStereo());
  pbufFloat.setSelected(desiredCaps.getOffscreenFloatingPointBuffers());
  pbufRTTex.setSelected(desiredCaps.getOffscreenRenderToTexture());
  pbufRTTexRect.setSelected(desiredCaps.getOffscreenRenderToTextureRectangle());
  
  /* Action listeners for buffer checkboxes */
  hwAccelBuf.addActionListener(new ActionListener()
  {
    public void actionPerformed(ActionEvent e)
    {
      desiredCaps.setHardwareAccelerated(hwAccelBuf.isSelected());
    }
  });
  doubleBuf.addActionListener(new ActionListener()
  {
    public void actionPerformed(ActionEvent e)
    {
      desiredCaps.setDoubleBuffered(doubleBuf.isSelected());
    }
  });
  sampleBuf.addActionListener(new ActionListener()
  {
    public void actionPerformed(ActionEvent e)
    {
      desiredCaps.setSampleBuffers(sampleBuf.isSelected());
      numSampleSlider.setEnabled(sampleBuf.isSelected());
    }
  });
  stereoBuf.addActionListener(new ActionListener()
  {
    public void actionPerformed(ActionEvent e)
    {
      desiredCaps.setStereo(stereoBuf.isSelected());
    }
  });
  
  /* color and accum buffers ticks settings */
  JPanel accumPane = new JPanel();
  JPanel colorPane = new JPanel();
  
  for (int c = 0; c < rgbaSliders.length; c++)
  {
    rgbaSliders[c].setOrientation(JSlider.VERTICAL);
    rgbaSliders[c].setPaintLabels(true);
    rgbaSliders[c].setMinorTickSpacing(1);
    rgbaSliders[c].setMajorTickSpacing(4);
    rgbaSliders[c].setSnapToTicks(true);
    rgbaSliders[c].setPaintTrack(true);
    rgbaSliders[c].setPaintTicks(true);
    colorPane.add(rgbaSliders[c]);  //add each slider to pane
    
    accumSliders[c].setOrientation(JSlider.VERTICAL);
    accumSliders[c].setPaintLabels(true);
    accumSliders[c].setMinorTickSpacing(1);
    accumSliders[c].setMajorTickSpacing(4);
    accumSliders[c].setSnapToTicks(true);
    accumSliders[c].setPaintTrack(true);
    accumSliders[c].setPaintTicks(true);
    accumPane.add(accumSliders[c]);
    
  }
  
  depthSlider.setOrientation(JSlider.VERTICAL);
  depthSlider.setPaintLabels(true);
  depthSlider.setMinorTickSpacing(1);
  depthSlider.setMajorTickSpacing(4);
  depthSlider.setSnapToTicks(true);
  depthSlider.setPaintTrack(true);
  depthSlider.setPaintTicks(true);
  
  stencilSlider.setOrientation(JSlider.VERTICAL);
  stencilSlider.setPaintLabels(true);
  stencilSlider.setMinorTickSpacing(1);
  stencilSlider.setMajorTickSpacing(4);
  stencilSlider.setSnapToTicks(true);
  stencilSlider.setPaintTrack(true);
  stencilSlider.setPaintTicks(true);
  
  numSampleSlider.setOrientation(JSlider.VERTICAL);
  numSampleSlider.setPaintLabels(true);
  numSampleSlider.setMinorTickSpacing(1);
  numSampleSlider.setMajorTickSpacing(2);
  numSampleSlider.setSnapToTicks(true);
  numSampleSlider.setPaintTrack(true);
  numSampleSlider.setPaintTicks(true);
  numSampleSlider.setEnabled(sampleBuf.isSelected());
        
  /* Color Sliders' listener */
  rgbaSliders[0].addChangeListener(new ChangeListener()
  {
    public void stateChanged(ChangeEvent e)
    {
      JSlider source = (JSlider)e.getSource();
      if (!source.getValueIsAdjusting())
      {
        desiredCaps.setRedBits((int)source.getValue());
      }
    }
  });
  rgbaSliders[1].addChangeListener(new ChangeListener()
  {
    public void stateChanged(ChangeEvent e)
    {
      JSlider source = (JSlider)e.getSource();
      if (!source.getValueIsAdjusting())
      {
        desiredCaps.setGreenBits((int)source.getValue());
      }
    }
  });
  rgbaSliders[2].addChangeListener(new ChangeListener()
  {
    public void stateChanged(ChangeEvent e)
    {
      JSlider source = (JSlider)e.getSource();
      if (!source.getValueIsAdjusting())
      {
        desiredCaps.setBlueBits((int)source.getValue());
      }
    }
  });
  rgbaSliders[3].addChangeListener(new ChangeListener()
  {
    public void stateChanged(ChangeEvent e)
    {
      JSlider source = (JSlider)e.getSource();
      if (!source.getValueIsAdjusting())
      {
        int value = (int)source.getValue();
        desiredCaps.setAlphaBits(value);
      }
    }
  });
  
  /* Accumulation Sliders' listener */
  accumSliders[0].addChangeListener(new ChangeListener()
  {
    public void stateChanged(ChangeEvent e)
    {
      JSlider source = (JSlider)e.getSource();
      if (!source.getValueIsAdjusting())
      {
        int value = (int)source.getValue();
        desiredCaps.setAccumRedBits(value);
      }
    }
  });
  accumSliders[1].addChangeListener(new ChangeListener()
  {
    public void stateChanged(ChangeEvent e)
    {
      JSlider source = (JSlider)e.getSource();
      if (!source.getValueIsAdjusting())
      {
        int value = (int)source.getValue();
        desiredCaps.setAccumGreenBits(value);
      }
    }
  });
  accumSliders[2].addChangeListener(new ChangeListener()
  {
    public void stateChanged(ChangeEvent e)
    {
      JSlider source = (JSlider)e.getSource();
      if (!source.getValueIsAdjusting())
      {
        int value = (int)source.getValue();
        desiredCaps.setAccumBlueBits(value);
      }
    }
  });
  accumSliders[3].addChangeListener(new ChangeListener()
  {
    public void stateChanged(ChangeEvent e)
    {
      JSlider source = (JSlider)e.getSource();
      if (!source.getValueIsAdjusting())
      {
        int value = (int)source.getValue();
        desiredCaps.setAccumAlphaBits(value);
      }
    }
  });
  
  depthSlider.addChangeListener(new ChangeListener()
  {
    public void stateChanged(ChangeEvent e)
    {
      JSlider source = (JSlider)e.getSource();
      if (!source.getValueIsAdjusting())
      {
        int value = (int)source.getValue();
        desiredCaps.setDepthBits(value);
      }
    }
  });
  
  stencilSlider.addChangeListener(new ChangeListener()
  {
    public void stateChanged(ChangeEvent e)
    {
      JSlider source = (JSlider)e.getSource();
      if (!source.getValueIsAdjusting())
      {
        int value = (int)source.getValue();
        desiredCaps.setStencilBits(value);
      }
    }
  });
  
  numSampleSlider.addChangeListener(new ChangeListener()
  {
    public void stateChanged(ChangeEvent e)
    {
      JSlider source = (JSlider)e.getSource();
      if (!source.getValueIsAdjusting())
      {
        int value = (int)source.getValue();
        desiredCaps.setNumSamples(value);
      }
    }
  });

JPanel hwBufPane = new JPanel();
      hwBufPane.setLayout(new BoxLayout(hwBufPane, BoxLayout.Y_AXIS));
      hwBufPane.setBorder(BorderFactory.
        createTitledBorder(null, "Buffer Hardware",
        TitledBorder.DEFAULT_JUSTIFICATION,
        TitledBorder.DEFAULT_POSITION) );
      hwBufPane.add(hwAccelBuf);
      hwBufPane.add(doubleBuf);
      hwBufPane.add(sampleBuf);
      hwBufPane.add(stereoBuf);
      
      JPanel pbufPane = new JPanel();
      pbufPane.setLayout(new BoxLayout(pbufPane, BoxLayout.Y_AXIS));
      pbufPane.setBorder(BorderFactory.
        createTitledBorder(null, "Off-Screen P Buffer",
        TitledBorder.DEFAULT_JUSTIFICATION,
        TitledBorder.DEFAULT_POSITION) );
      pbufPane.add(pbufFloat);
      pbufPane.add(pbufRTTex);
      pbufPane.add(pbufRTTexRect);
      
      //JPanel colorPane = new JPanel();
      colorPane.setLayout(new BoxLayout(colorPane, BoxLayout.X_AXIS));
      colorPane.setBorder(BorderFactory.
        createTitledBorder(null, "Color (RGBA)",
        TitledBorder.DEFAULT_JUSTIFICATION,
        TitledBorder.DEFAULT_POSITION) );
      
      accumPane.setLayout(new BoxLayout(accumPane, BoxLayout.X_AXIS));
      accumPane.setBorder(BorderFactory.
        createTitledBorder(null, "Accumulation (RGBA)",
        TitledBorder.DEFAULT_JUSTIFICATION,
        TitledBorder.DEFAULT_POSITION) );
      
      JPanel depthPane = new JPanel();
      depthPane.setLayout(new BoxLayout(depthPane, BoxLayout.X_AXIS));
      depthPane.setBorder(BorderFactory.
        createTitledBorder(null, "Depth",
        TitledBorder.DEFAULT_JUSTIFICATION,
        TitledBorder.DEFAULT_POSITION) );
      
      JPanel stencilPane = new JPanel();
      stencilPane.setLayout(new BoxLayout(stencilPane, BoxLayout.X_AXIS));
      stencilPane.setBorder(BorderFactory.
        createTitledBorder(null, "Stencil",
        TitledBorder.DEFAULT_JUSTIFICATION,
        TitledBorder.DEFAULT_POSITION) );
      
      JPanel numSamplePane = new JPanel();
      numSamplePane.setLayout(new BoxLayout(numSamplePane, BoxLayout.X_AXIS));
      numSamplePane.setBorder(BorderFactory.
        createTitledBorder(null, "Sample",
        TitledBorder.DEFAULT_JUSTIFICATION,
        TitledBorder.DEFAULT_POSITION) );
      
      JPanel dssPane = new JPanel();// depth-stencil-sample buffers panel
      dssPane.setLayout(new BoxLayout(dssPane, BoxLayout.X_AXIS));
      dssPane.setBorder(BorderFactory.
        createTitledBorder(null, "Depth, Stencil, Sample",
        TitledBorder.DEFAULT_JUSTIFICATION,
        TitledBorder.DEFAULT_POSITION) );
      dssPane.add(depthSlider);
      dssPane.add(stencilSlider);
      dssPane.add(numSampleSlider);
      
      JPanel buffersPane = new JPanel();
      //buffersPane.setLayout(new BoxLayout(buffersPane, BoxLayout.X_AXIS));
      buffersPane.setLayout(new BoxLayout(buffersPane, BoxLayout.X_AXIS));
      buffersPane.add(hwBufPane);
      buffersPane.add(pbufPane);
            
      add(buffersPane);
      add(colorPane);
      add(dssPane);
      add(accumPane);
      
      examineAvailableGLCapabilities();
    }
    public GLCapabilities getPreferredCaps()
    { return chosenCaps;  }
    public GLCapabilities getUserCaps()
    { return desiredCaps; };
    public void update(GLCapabilities caps)
    {
      hwAccelBuf.setSelected(caps.getHardwareAccelerated());
      doubleBuf.setSelected(caps.getDoubleBuffered());
      sampleBuf.setSelected(caps.getSampleBuffers());
      if (!sampleBuf.isSelected())
        numSampleSlider.setEnabled(false);
      stereoBuf.setSelected(caps.getStereo());
      
      pbufFloat.setSelected(caps.getOffscreenFloatingPointBuffers());
      pbufRTTex.setSelected(caps.getOffscreenRenderToTexture());
      pbufRTTexRect.setSelected(caps.getOffscreenRenderToTextureRectangle());
      
      rgbaSliders[0].setValue(caps.getRedBits());
      rgbaSliders[1].setValue(caps.getGreenBits());
      rgbaSliders[2].setValue(caps.getBlueBits());
      rgbaSliders[3].setValue(caps.getAlphaBits());
      
      accumSliders[0].setValue(caps.getAccumRedBits());
      accumSliders[1].setValue(caps.getAccumGreenBits());
      accumSliders[2].setValue(caps.getAccumBlueBits());
      accumSliders[3].setValue(caps.getAccumAlphaBits());
      
      depthSlider.setValue(caps.getDepthBits());
      stencilSlider.setValue(caps.getStencilBits());
      numSampleSlider.setValue(caps.getNumSamples());
      numSampleSlider.setMaximum(caps.getNumSamples());//FIXME: need to deter max num sample buffers
      
    }
    private void buildDummyWindow()
    {
      chooser = new GLCapsExt();
      GLCanvas canvas = GLDrawableFactory.getFactory().
        createGLCanvas(desiredCaps, chooser);
      JWindow jwin = new JWindow();
      jwin.add(canvas);
      jwin.setVisible(true);
    }
    private void examineAvailableGLCapabilities()
    {
      buildDummyWindow();
      
      for (int c = 0; c < rgbaSliders.length; c++)
      {
        rgbaSliders[c].setMaximum(chooser.getMaxColorBit());
      }
      for (int a = 0; a < accumSliders.length; a++)
      {
        accumSliders[a].setMaximum(chooser.getMaxAccumBit());
      }
      depthSlider.setMaximum(chooser.getMaxDepthBit());
      stencilSlider.setMaximum(chooser.getMaxStencilBit());
      numSampleSlider.setMaximum(chooser.getMaxNumSampleBuffers());
      
      chosenCaps = chooser.getSystemRecommendedCapabilities();
      update(chosenCaps);
    }
  }//
  
  /* A GLCapabilitiesChooser extension */
  class GLCapsExt
    extends DefaultGLCapabilitiesChooser
  {
    GLCapabilities caps = null;
    GLCapabilities [] allCaps = null;
    GLCapabilities chosenCaps = null;
    
    int maxColorBit = 0;
    int maxDepthBit = 0;
    int maxStencilBit = 0;
    int maxNumSample = 0;
    int maxAccumBit = 0;
    
    int curCaps = 0;
    
    public GLCapsExt()
    {
    }
    public GLCapabilities getGLCapabilities()
    { return getAllGLCapabilities()[curCaps];  }
    public GLCapabilities [] getAllGLCapabilities()
    { return allCaps;    }
    public GLCapabilities getSystemRecommendedCapabilities()
    { return chosenCaps;  }
    
    public final int getMaxColorBit()
    { return maxColorBit; }
    public final int getMaxDepthBit()
    { return maxDepthBit; }
    public final int getMaxStencilBit()
    { return maxStencilBit; }
    public final int getMaxSampleBufs()
    { return maxNumSample;  }
    public final int getMaxAccumBit()
    { return maxAccumBit; }
    public final int getMaxNumSampleBuffers()
    { return chosenCaps.getNumSamples();  }
    
    public GLCapabilities getNext()
    { curCaps++;
      if (curCaps >= allCaps.length-1)
        curCaps = allCaps.length-1;
      return allCaps[curCaps];
    }
    public GLCapabilities getPrevious()
    {
      curCaps--;
      if (curCaps <= 0)  curCaps = 0;
      return allCaps[curCaps];
    }
    public final int getCurrentIndex()
    { return curCaps; }
    
    public int chooseCapabilities(final GLCapabilities desired,
      final GLCapabilities[] available,
      final int windowSystemRecommendedChoice)
    {
      caps = desired;
      allCaps = available;
      
      for (int c = 0; c < available.length; c++)
      {
        //System.err.println("#" + c + " available " + available[c].toString());
        
        int col = Math.max(available[c].getRedBits(),
          available[c].getGreenBits());
        if (col > maxColorBit)  maxColorBit = col;
        col = Math.max(available[c].getBlueBits(),
          available[c].getAlphaBits());
        if (col > maxColorBit)  maxColorBit = col;
        
        // get the max value of capabilities
        maxDepthBit = Math.max(maxDepthBit,
          available[c].getDepthBits());
        maxStencilBit = Math.max(maxStencilBit,
          available[c].getStencilBits());
        maxNumSample = Math.max(maxNumSample,
          available[c].getNumSamples());
        
        int acc = Math.max(available[c].getAccumRedBits(),
          available[c].getAccumGreenBits());
        if (acc > maxAccumBit)  maxAccumBit = acc;
        acc = Math.max(available[c].getAccumBlueBits(),
          available[c].getAccumAlphaBits());
        if (acc > maxAccumBit)  maxAccumBit = acc;
      }
      int selection = super.chooseCapabilities
        (null, available, curCaps);
      curCaps = selection;
      chosenCaps = available[selection];
      System.err.println("index: " + selection + " | "
        + chosenCaps.toString());
      
      return selection;
    }//
    
  }//
  
}//

The dummy JWindow is used to get avialable caps ahead of time. But from that you can also get system recommended caps of all supported caps. Also, chooseCapabilities also determine the maximum value for all sliders.

Now, creating your own caps is not allow at the moment and since the system recommend choice is consistently the same number, there is not a need for it–so i passed null to the first param of the super class chooseCapabilities.

I create the renderer listener once and remove and add to the canvas on creation.

As far as layout is concern, I could do better when we debug this proggie. On me notebook, sometimes the canvas won’t display for certain GLCapabilities unless I resize the window or create the canvas again with the same GLCapabilities.

I intended this demo to show use of overriding DefaultGLCapababiltiesChooser and to preview all the available GLCapabilities. Anyway, this post is getting long winded. I hope you catch me drift. -AK77