Added a method to create a shader state using Files and preprocessor
definitions. git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@159 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
@@ -21,8 +21,15 @@
|
||||
|
||||
package com.threerings.jme.util;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import com.jme.math.Vector3f;
|
||||
import com.jme.scene.Controller;
|
||||
import com.jme.scene.state.GLSLShaderObjectsState;
|
||||
import com.jme.system.DisplaySystem;
|
||||
|
||||
import com.samskivert.util.StringUtil;
|
||||
|
||||
@@ -156,4 +163,44 @@ public class JmeUtil
|
||||
}
|
||||
return defaultType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the specified shaders (prepending the supplied preprocessor definitions)
|
||||
* and returns a GLSL shader state. One of the supplied files may be <code>null</code>
|
||||
* in order to use the fixed-function pipeline for that part.
|
||||
*
|
||||
* @param defs a number of preprocessor definitions to be #defined in both shaders
|
||||
* (e.g., "ENABLE_FOG", "NUM_LIGHTS 2").
|
||||
*/
|
||||
public static GLSLShaderObjectsState loadShaders (File vert, File frag, String... defs)
|
||||
{
|
||||
GLSLShaderObjectsState sstate =
|
||||
DisplaySystem.getDisplaySystem().getRenderer().createGLSLShaderObjectsState();
|
||||
sstate.load(
|
||||
(vert == null) ? null : readSource(vert, defs),
|
||||
(frag == null) ? null : readSource(frag, defs));
|
||||
return sstate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads an entire source file as a string, prepending the supplied preprocessor definitions.
|
||||
*/
|
||||
protected static String readSource (File file, String[] defs)
|
||||
{
|
||||
StringBuffer buf = new StringBuffer();
|
||||
String ln = System.getProperty("line.separator");
|
||||
for (String def : defs) {
|
||||
buf.append("#define ").append(def).append(ln);
|
||||
}
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(file));
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
buf.append(line).append(ln);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user