package javax.media.opengl.glsl; import javax.media.opengl.*; import javax.media.opengl.util.*; import java.util.*; import java.nio.*; import java.io.*; import java.net.*; public class ShaderCode { public static final String SUFFIX_VERTEX_SOURCE = "vp" ; public static final String SUFFIX_VERTEX_BINARY = "bvp" ; public static final String SUFFIX_FRAGMENT_SOURCE = "fp" ; public static final String SUFFIX_FRAGMENT_BINARY = "bfp" ; public static final String SUB_PATH_NVIDIA = "nvidia" ; public ShaderCode(int type, int number, String[][] source) { switch (type) { case GL2ES2.GL_VERTEX_SHADER: case GL2ES2.GL_FRAGMENT_SHADER: break; default: throw new GLException("Unknown shader type: "+type); } shaderSource = source; shaderBinaryFormat = -1; shaderBinary = null; shaderType = type; shader = BufferUtil.newIntBuffer(number); id = getNextID(); } public ShaderCode(int type, int number, int binFormat, Buffer binary) { switch (type) { case GL2ES2.GL_VERTEX_SHADER: case GL2ES2.GL_FRAGMENT_SHADER: break; default: throw new GLException("Unknown shader type: "+type); } shaderSource = null; shaderBinaryFormat = binFormat; shaderBinary = binary; shaderType = type; shader = BufferUtil.newIntBuffer(number); id = getNextID(); } public static ShaderCode create(int type, int number, Class context, String[] sourceFiles) { String[][] shaderSources = null; if(null!=sourceFiles) { shaderSources = new String[sourceFiles.length][1]; for(int i=0; null!=shaderSources && i= 0) { String tmpPath = className.substring(0, lastSlash + 1) + path; url = getResource(tmpPath, contextCL); if (url != null) { path = tmpPath; } } } if (url == null) { return null; } StringBuffer result = new StringBuffer(); readShaderSource(contextCL, path, url, result); return result.toString(); } public static ByteBuffer readShaderBinary(Class context, String path) { try { URL url = getResource(context, path); if (url == null) { return null; } return readAll(new BufferedInputStream(url.openStream())); } catch (IOException e) { throw new RuntimeException(e); } } //---------------------------------------------------------------------- // Internals only below this point // public static URL getResource(Class context, String path) { ClassLoader contextCL = (null!=context)?context.getClassLoader():null; URL url = getResource(path, contextCL); if (url == null && null!=context) { // Try again by scoping the path within the class's package String className = context.getName().replace('.', '/'); int lastSlash = className.lastIndexOf('/'); if (lastSlash >= 0) { String tmpPath = className.substring(0, lastSlash + 1) + path; url = getResource(tmpPath, contextCL); } } return url; } public static URL getResource(String path, ClassLoader context) { URL url = null; if (context != null) { url = context.getResource(path); } else { url = ClassLoader.getSystemResource(path); } if(!urlExists(url)) { url = null; try { url = new URL(path); } catch (MalformedURLException e) { } } if(!urlExists(url)) { url = null; try { File file = new File(path); if(file.exists()) { url = file.toURL(); } } catch (MalformedURLException e) {} } return url; } private static boolean urlExists(URL url) { boolean v = false; if(null!=url) { try { URLConnection uc = url.openConnection(); v = true; } catch (IOException ioe) { } } return v; } private static String makeRelative(String context, String includeFile) { File file = new File(context); file = file.getParentFile(); while (file != null && includeFile.startsWith("../")) { file = file.getParentFile(); includeFile = includeFile.substring(3); } if (file != null) { String res = new File(file, includeFile).getPath(); // Handle things on Windows return res.replace('\\', '/'); } else { return includeFile; } } private static ByteBuffer readAll(InputStream stream) throws IOException { byte[] data = new byte[1024]; int numRead = 0; int pos = 0; do { int avail = data.length - pos; if (avail == 0) { int newSize = 2 * data.length; byte[] newData = new byte[newSize]; System.arraycopy(data, 0, newData, 0, data.length); data = newData; avail = data.length - pos; } numRead = stream.read(data, pos, avail); if (numRead > 0) { pos += numRead; } } while (numRead >= 0); ByteBuffer res = BufferUtil.newByteBuffer(data, 0, pos); return res; } protected String[][] shaderSource = null; protected Buffer shaderBinary = null; protected int shaderBinaryFormat = -1; protected IntBuffer shader = null; protected int shaderType = -1; protected Integer id = null; protected boolean valid=false; private static synchronized Integer getNextID() { return new Integer(nextID++); } protected static int nextID = 1; }