package javax.media.opengl.util.glsl; import javax.media.opengl.util.*; import javax.media.opengl.*; import java.nio.*; import java.io.*; import java.net.*; public class ShaderCode { public ShaderCode(int type, int number, int binFormat, Buffer binary, 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 = binFormat; shaderBinary = binary; shaderType = type; shader = BufferUtil.newIntBuffer(number); id = getNextID(); } public static ShaderCode create(int type, int number, Class context, int binFormat, String binaryFile, String[] sourceFiles) { String[][] shaderSources = null; ByteBuffer shaderBinary = null; if(null!=sourceFiles) { shaderSources = new String[sourceFiles.length][1]; for(int i=0; i= 0) { String tmpPath = className.substring(0, lastSlash + 1) + path; url = getResource(context.getClassLoader(), tmpPath); if (url != null) { path = tmpPath; } } } if (url == null) { return null; } StringBuffer result = new StringBuffer(); readShaderSource(context.getClassLoader(), path, url, result); return result.toString(); } public static ByteBuffer readShaderBinary(Class context, String path) { try { URL url = getResource(context.getClassLoader(), path); if (url == null) { // 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(context.getClassLoader(), tmpPath); if (url != null) { path = tmpPath; } } } if (url == null) { return null; } return readAll(new BufferedInputStream(url.openStream())); } catch (IOException e) { throw new RuntimeException(e); } } //---------------------------------------------------------------------- // Internals only below this point // protected static URL getResource(ClassLoader context, String path) { if (context != null) { return context.getResource(path); } else { return ClassLoader.getSystemResource(path); } } protected 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 = ByteBuffer.wrap(data); if (data.length != pos) { res.limit(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; }