gl constants <-> string mappings

Does LWJGL have a mapping from gl constants to string representations of those constants? E.g. I need to convert “GL_TEXTURE” to GL_TEXTURE and so on, and vice-versa. I don’t see anything like this but I thought I’d check.

Nope, it’s beyond the remit of LWJGL, but fortunately I have a thingy to do it for me in SPGL :slight_smile:

Cas :slight_smile:

Using reflection it’s a piece of cake.

If you have to do it often, you can cache the results in a HashMap.

But you shouldn’t do it often for obvious reasons… :slight_smile:


	/** A map of constant names to values */	
	private static final HashMap glConstantsMap = new HashMap(513, 1.0f);
	
	static {
		loadGLConstants();
	}

	/**
	 * Decode a gl string constant
	 */
	public static int decode(String glstring) throws OpenGLException {
		Integer i = (Integer) glConstantsMap.get(glstring.toUpperCase());
		if (i == null)
			throw new OpenGLException(glstring+" is not a recognised GL constant");
		else
			return i.intValue();		
	}
	
	/**
	 * Recode a gl constant back into a string
	 */
	public static String recode(int code) {
		for (Iterator i = glConstantsMap.keySet().iterator(); i.hasNext(); ) {
			String s = (String) i.next();
			Integer n = (Integer) glConstantsMap.get(s);
			if (n.intValue() == code)
				return s;
		}
		throw new OpenGLException(code+" is not a known GL code");
	}
	private static void loadGLConstants(Class intf) {
		Field[] field = intf.getFields();
		for (int i = 0; i < field.length; i ++) {
			try {
				if (Modifier.isStatic(field[i].getModifiers()) && Modifier.isPublic(field[i].getModifiers()) && Modifier.isFinal(field[i].getModifiers()) && field[i].getType().equals(int.class))
					glConstantsMap.put(field[i].getName(), new Integer(field[i].getInt(null)));
			} catch (Exception e) {
			}
		}
	}

	/**
	 * Reads all the constant enumerations from this class and stores them
	 * so we can decode them from strings.
	 * @see #decode()
	 * @see #recode()
	 */
	private static void loadGLConstants() {
		Class[] classes = new Class[] {
			GL11.class,
			GL12.class,
			GL13.class,
			GL14.class,
			GL15.class,
			ARBMultitexture.class,
			ARBTextureCubeMap.class,
			ARBDepthTexture.class,
			ARBFragmentProgram.class,
			ARBMatrixPalette.class,
			ARBMultisample.class,
			ARBPointParameters.class,
			ARBShadow.class,
			ARBShadowAmbient.class,
			ARBTextureBorderClamp.class,
			ARBTextureCompression.class,
			ARBTextureEnvCombine.class,
			ARBTextureEnvDot3.class,
			ARBTextureMirroredRepeat.class,
			ARBTransposeMatrix.class,
			ARBVertexBlend.class,
			ARBVertexBufferObject.class,
			ARBVertexProgram.class,
			ARBWindowPos.class,
			EXTDrawRangeElements.class,
			EXTAbgr.class,
			EXTBgra.class,
			EXTBlendFuncSeparate.class,
			EXTBlendSubtract.class,
			EXTCompiledVertexArray.class,
			EXTFogCoord.class,
			EXTMultiDrawArrays.class,
			EXTPackedPixels.class,
			EXTPointParameters.class,
			EXTRescaleNormal.class,
			EXTSecondaryColor.class,
			EXTSeparateSpecularColor.class,
			EXTSharedTexturePalette.class,
			EXTStencilTwoSide.class,
			EXTStencilWrap.class,
			EXTTextureCompressionS3TC.class,
			EXTTextureEnvCombine.class,
			EXTTextureEnvDot3.class,
			EXTTextureFilterAnisotropic.class,
			EXTTextureLODBias.class,
			EXTVertexShader.class,
			EXTVertexWeighting.class,
			ATIElementArray.class,
			ATIEnvmapBumpmap.class,
			ATIFragmentShader.class,
			ATIPnTriangles.class,
			ATISeparateStencil.class,
			ATITextureMirrorOnce.class,
			ATIVertexArrayObject.class,
			ATIVertexStreams.class,
			NVCopyDepthToColor.class,
			NVDepthClamp.class,
			NVEvaluators.class,
			NVFence.class,
			NVFogDistance.class,
			NVLightMaxExponent.class,
			NVOcclusionQuery.class,
			NVPackedDepthStencil.class,
			NVPointSprite.class,
			NVRegisterCombiners.class,
			NVRegisterCombiners2.class,
			NVTexgenReflection.class,
			NVTextureEnvCombine4.class,
			NVTextureRectangle.class,
			NVTextureShader.class,
			NVTextureShader2.class,
			NVTextureShader3.class,
			NVVertexArrayRange.class,
			NVVertexArrayRange2.class,
			NVVertexProgram.class
		};
		for (int i = 0; i < classes.length; i ++) {
			loadGLConstants(classes[i]);
		}
	}

Cas :slight_smile:

recode(…) would be much faster if it used a HashMap with the keys/values of the glConstantsMap flipped. :slight_smile:

Well, it might be, except that I only use that method during development to read XML files in…

Cas :slight_smile: