001/**
002 * 
003 */
004package icy.util;
005
006import javax.media.opengl.GLProfile;
007
008/**
009 * Utilities class for OpenGL.
010 * 
011 * @author Stephane
012 */
013public class OpenGLUtil
014{
015    /**
016     * Returns <code>true</code> is the specified version of OpenGL is supported by the graphics card (or by its
017     * driver).<br/>
018     * Ex: if (isOpenGLSupported(2)) // test for OpenGL 2 compliance
019     * 
020     * @param version
021     *        the version of OpenGL we want to test for (1 to 4)
022     */
023    public static boolean isOpenGLSupported(int version)
024    {
025        return isOpenGLSupported(version, false);
026    }
027    
028    /**
029     * Returns <code>true</code> is the specified version of OpenGL is supported by the graphics card
030     * (or by its driver).<br/>
031     * Ex: if (isOpenGLSupported(2, true))   // test if GPU supports OpenGL 2 (hardware support)<br/>
032     *     if (isOpenGLSupported(3, false))  // test if driver support OpenGL 3 (hardware or software implementation)<br/>
033     * 
034     * @param version
035     *        the version of OpenGL we want to test for (1 to 4)
036     * @param hard
037     *        specify if we query about hardware support (GPU) or not
038     */
039    public static boolean isOpenGLSupported(int version, boolean hard)
040    {
041        try
042        {
043            // get maximum supported GL profile
044            final GLProfile glp = GLProfile.getMaximum(hard);
045            boolean result = false;
046            
047            switch (version)
048            {
049                    case 2:
050                        result |= glp.isGL2();
051                    case 3:
052                        result |= glp.isGL3();
053                    case 4:
054                        result |= glp.isGL4();
055            }
056            
057            if ((version > 0) && (version <= 4)) return result;
058        }
059        catch (Exception e)
060        {
061            // OpenGL throwing error --> just report as not supported
062        }
063        
064        return false;
065    }
066}