Use a fragment shader as well as a vertex shader for the skinned meshes.
I tracked the ATI slowdown down to using a vertex shader in combination with fixed function fog. Also cleaned up ShaderConfig a little. git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@244 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
@@ -0,0 +1,56 @@
|
|||||||
|
//
|
||||||
|
// $Id: ImageCache.java 158 2007-02-24 00:38:17Z mdb $
|
||||||
|
//
|
||||||
|
// Nenya library - tools for developing networked games
|
||||||
|
// Copyright (C) 2002-2007 Three Rings Design, Inc., All Rights Reserved
|
||||||
|
// http://www.threerings.net/code/nenya/
|
||||||
|
//
|
||||||
|
// This library is free software; you can redistribute it and/or modify it
|
||||||
|
// under the terms of the GNU Lesser General Public License as published
|
||||||
|
// by the Free Software Foundation; either version 2.1 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
// Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public
|
||||||
|
// License along with this library; if not, write to the Free Software
|
||||||
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
|
/** The diffuse texture map. */
|
||||||
|
uniform sampler2D diffuseMap;
|
||||||
|
|
||||||
|
/** The emissive texture map. */
|
||||||
|
#ifdef EMISSIVE_MAPPED
|
||||||
|
uniform sampler2D emissiveMap;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/** The amount of fog. */
|
||||||
|
#ifdef FOG
|
||||||
|
varying float fogAlpha;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fragment shader for skinned meshes.
|
||||||
|
*/
|
||||||
|
void main ()
|
||||||
|
{
|
||||||
|
// start with the diffuse color
|
||||||
|
vec4 fragColor = texture2D(diffuseMap, gl_TexCoord[0].st);
|
||||||
|
|
||||||
|
// modulate by the light color
|
||||||
|
#ifdef EMISSIVE_MAPPED
|
||||||
|
fragColor *= (gl_Color + vec4(texture2D(emissiveMap, gl_TexCoord[0].st).rgb, 0.0));
|
||||||
|
#else
|
||||||
|
fragColor *= gl_Color;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// blend between the computed color and the fog color
|
||||||
|
#ifdef FOG
|
||||||
|
gl_FragColor = mix(gl_Fog.color, fragColor, fogAlpha);
|
||||||
|
#else
|
||||||
|
gl_FragColor = fragColor;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
@@ -38,6 +38,11 @@ attribute ATTRIB_TYPE boneIndices;
|
|||||||
/** The bone weights. */
|
/** The bone weights. */
|
||||||
attribute ATTRIB_TYPE boneWeights;
|
attribute ATTRIB_TYPE boneWeights;
|
||||||
|
|
||||||
|
/** The amount of fog. */
|
||||||
|
#ifdef FOG
|
||||||
|
varying float fogAlpha;
|
||||||
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Vertex shader for skinned meshes.
|
* Vertex shader for skinned meshes.
|
||||||
*/
|
*/
|
||||||
@@ -65,12 +70,12 @@ void main ()
|
|||||||
vec3 eyeVertex = vec3(gl_ModelViewMatrix * modelVertex);
|
vec3 eyeVertex = vec3(gl_ModelViewMatrix * modelVertex);
|
||||||
vec3 eyeNormal = normalize(vec3(gl_ModelViewMatrixInverseTranspose * modelNormal));
|
vec3 eyeNormal = normalize(vec3(gl_ModelViewMatrixInverseTranspose * modelNormal));
|
||||||
|
|
||||||
// eye space 'z' is the standard fog coordinate
|
|
||||||
gl_FogFragCoord = -eyeVertex.z;
|
|
||||||
|
|
||||||
// set gl_FrontColor based on vertex, normal and light parameters
|
// set gl_FrontColor based on vertex, normal and light parameters
|
||||||
SET_FRONT_COLOR
|
SET_FRONT_COLOR
|
||||||
|
|
||||||
// set the varying texture coordinates
|
// set the varying texture coordinates
|
||||||
SET_TEX_COORDS
|
SET_TEX_COORDS
|
||||||
|
|
||||||
|
// set the fog alpha based on the eye space vertex
|
||||||
|
SET_FOG_ALPHA
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -408,7 +408,7 @@ public class SkinMesh extends ModelMesh
|
|||||||
if (bonesPerVertex > MAX_SHADER_BONES_PER_VERTEX) {
|
if (bonesPerVertex > MAX_SHADER_BONES_PER_VERTEX) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_sconfig = new SkinShaderConfig(scache, bonesPerVertex);
|
_sconfig = new SkinShaderConfig(scache, bonesPerVertex, _emissiveMap != null);
|
||||||
if (_sconfig.update(getBatch(0).states)) {
|
if (_sconfig.update(getBatch(0).states)) {
|
||||||
setShaderAttributes();
|
setShaderAttributes();
|
||||||
setRenderState(_sconfig.getState());
|
setRenderState(_sconfig.getState());
|
||||||
@@ -647,10 +647,19 @@ public class SkinMesh extends ModelMesh
|
|||||||
/** Tracks the configuration of a skin shader. */
|
/** Tracks the configuration of a skin shader. */
|
||||||
protected static class SkinShaderConfig extends ShaderConfig
|
protected static class SkinShaderConfig extends ShaderConfig
|
||||||
{
|
{
|
||||||
public SkinShaderConfig (ShaderCache scache, int bonesPerVertex)
|
public SkinShaderConfig (ShaderCache scache, int bonesPerVertex, boolean emissiveMapped)
|
||||||
{
|
{
|
||||||
super(scache);
|
super(scache);
|
||||||
_bonesPerVertex = bonesPerVertex;
|
_bonesPerVertex = bonesPerVertex;
|
||||||
|
_emissiveMapped = emissiveMapped;
|
||||||
|
|
||||||
|
// set bindings from texture units to samplers
|
||||||
|
if (emissiveMapped) {
|
||||||
|
_state.setUniform("diffuseMap", 1);
|
||||||
|
_state.setUniform("emissiveMap", 0);
|
||||||
|
} else {
|
||||||
|
_state.setUniform("diffuseMap", 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getBonesPerVertex ()
|
public int getBonesPerVertex ()
|
||||||
@@ -664,11 +673,20 @@ public class SkinMesh extends ModelMesh
|
|||||||
return "media/jme/skin.vert";
|
return "media/jme/skin.vert";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override // documentation inherited
|
||||||
|
protected String getFragmentShader ()
|
||||||
|
{
|
||||||
|
return "media/jme/skin.frag";
|
||||||
|
}
|
||||||
|
|
||||||
@Override // documentation inherited
|
@Override // documentation inherited
|
||||||
protected void getDefinitions (ArrayList<String> defs)
|
protected void getDefinitions (ArrayList<String> defs)
|
||||||
{
|
{
|
||||||
super.getDefinitions(defs);
|
super.getDefinitions(defs);
|
||||||
defs.add("BONES_PER_VERTEX " + _bonesPerVertex);
|
defs.add("BONES_PER_VERTEX " + _bonesPerVertex);
|
||||||
|
if (_emissiveMapped) {
|
||||||
|
defs.add("EMISSIVE_MAPPED");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override // documentation inherited
|
@Override // documentation inherited
|
||||||
@@ -679,6 +697,7 @@ public class SkinMesh extends ModelMesh
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected int _bonesPerVertex;
|
protected int _bonesPerVertex;
|
||||||
|
protected boolean _emissiveMapped;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** A stored frame used for linear blending. */
|
/** A stored frame used for linear blending. */
|
||||||
|
|||||||
@@ -25,11 +25,13 @@ import java.util.ArrayList;
|
|||||||
|
|
||||||
import com.jme.image.Texture;
|
import com.jme.image.Texture;
|
||||||
import com.jme.light.Light;
|
import com.jme.light.Light;
|
||||||
|
import com.jme.scene.state.FogState;
|
||||||
import com.jme.scene.state.GLSLShaderObjectsState;
|
import com.jme.scene.state.GLSLShaderObjectsState;
|
||||||
import com.jme.scene.state.LightState;
|
import com.jme.scene.state.LightState;
|
||||||
import com.jme.scene.state.RenderState;
|
import com.jme.scene.state.RenderState;
|
||||||
import com.jme.scene.state.TextureState;
|
import com.jme.scene.state.TextureState;
|
||||||
import com.jme.system.DisplaySystem;
|
import com.jme.system.DisplaySystem;
|
||||||
|
import com.jme.util.ShaderUniform;
|
||||||
|
|
||||||
import com.samskivert.util.StringUtil;
|
import com.samskivert.util.StringUtil;
|
||||||
|
|
||||||
@@ -93,6 +95,9 @@ public abstract class ShaderConfig
|
|||||||
DisplaySystem.getDisplaySystem().getRenderer().createGLSLShaderObjectsState();
|
DisplaySystem.getDisplaySystem().getRenderer().createGLSLShaderObjectsState();
|
||||||
other._state.setProgramID(_state.getProgramID());
|
other._state.setProgramID(_state.getProgramID());
|
||||||
other._state.attribs = _state.attribs;
|
other._state.attribs = _state.attribs;
|
||||||
|
for (ShaderUniform uniform : _state.uniforms.values()) {
|
||||||
|
other._state.uniforms.put(uniform.name, (ShaderUniform)uniform.clone());
|
||||||
|
}
|
||||||
if (_lights != null) {
|
if (_lights != null) {
|
||||||
other._lights = (LightConfig[])_lights.clone();
|
other._lights = (LightConfig[])_lights.clone();
|
||||||
for (int ii = 0; ii < _lights.length; ii++) {
|
for (int ii = 0; ii < _lights.length; ii++) {
|
||||||
@@ -120,7 +125,8 @@ public abstract class ShaderConfig
|
|||||||
// this is one place where we don't want short-circuit evaluation
|
// this is one place where we don't want short-circuit evaluation
|
||||||
boolean lchanged = updateLightConfigs((LightState)states[RenderState.RS_LIGHT]);
|
boolean lchanged = updateLightConfigs((LightState)states[RenderState.RS_LIGHT]);
|
||||||
boolean tchanged = updateTextureConfigs((TextureState)states[RenderState.RS_TEXTURE]);
|
boolean tchanged = updateTextureConfigs((TextureState)states[RenderState.RS_TEXTURE]);
|
||||||
return lchanged || tchanged;
|
boolean fchanged = updateFogConfig((FogState)states[RenderState.RS_FOG]);
|
||||||
|
return lchanged || tchanged || fchanged;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -133,7 +139,7 @@ public abstract class ShaderConfig
|
|||||||
_lights = null;
|
_lights = null;
|
||||||
return (olights != null);
|
return (olights != null);
|
||||||
}
|
}
|
||||||
int lcount = lstate.getQuantity();
|
int lcount = Math.min(lstate.getQuantity(), MAX_LIGHTS);
|
||||||
if (_lights == null || _lights.length != lcount) {
|
if (_lights == null || _lights.length != lcount) {
|
||||||
_lights = new LightConfig[lcount];
|
_lights = new LightConfig[lcount];
|
||||||
for (int ii = 0; ii < lcount; ii++) {
|
for (int ii = 0; ii < lcount; ii++) {
|
||||||
@@ -175,6 +181,17 @@ public abstract class ShaderConfig
|
|||||||
return changed;
|
return changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the fog configuration, returning <code>true</code> if it has changed.
|
||||||
|
*/
|
||||||
|
protected boolean updateFogConfig (FogState fstate)
|
||||||
|
{
|
||||||
|
int ofunc = _fogDensityFunc;
|
||||||
|
_fogDensityFunc = (fstate == null || !fstate.isEnabled()) ?
|
||||||
|
-1 : fstate.getDensityFunction();
|
||||||
|
return (ofunc != _fogDensityFunc);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the resource name of the vertex shader (or <code>null</code> for none).
|
* Returns the resource name of the vertex shader (or <code>null</code> for none).
|
||||||
*/
|
*/
|
||||||
@@ -204,6 +221,9 @@ public abstract class ShaderConfig
|
|||||||
if (_textures != null) {
|
if (_textures != null) {
|
||||||
defs.add("TEXTURES " + StringUtil.join(_textures, "/"));
|
defs.add("TEXTURES " + StringUtil.join(_textures, "/"));
|
||||||
}
|
}
|
||||||
|
if (_fogDensityFunc != -1) {
|
||||||
|
defs.add("FOG " + _fogDensityFunc);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -212,36 +232,30 @@ public abstract class ShaderConfig
|
|||||||
*/
|
*/
|
||||||
protected void getDerivedDefinitions (ArrayList<String> ddefs)
|
protected void getDerivedDefinitions (ArrayList<String> ddefs)
|
||||||
{
|
{
|
||||||
|
// add the def that sets the front color based on the light types
|
||||||
StringBuffer buf = new StringBuffer("SET_FRONT_COLOR ");
|
StringBuffer buf = new StringBuffer("SET_FRONT_COLOR ");
|
||||||
if (_lights != null) {
|
if (_lights != null) {
|
||||||
buf.append("gl_FrontColor.rgb = gl_FrontLightModelProduct.sceneColor.rgb; ");
|
// start with the "scene color," which combines scene ambient, emissivity, etc.
|
||||||
|
buf.append("vec3 frontColor = gl_FrontLightModelProduct.sceneColor.rgb; ");
|
||||||
|
|
||||||
|
// add snippets for each of the lights
|
||||||
for (int ii = 0; ii < _lights.length; ii++) {
|
for (int ii = 0; ii < _lights.length; ii++) {
|
||||||
LightConfig light = _lights[ii];
|
LightConfig light = _lights[ii];
|
||||||
if (light.type == -1) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (light.type == Light.LT_POINT) {
|
if (light.type == Light.LT_POINT) {
|
||||||
buf.append("vec3 lvec" + ii + " = gl_LightSource[" + ii +
|
buf.append(POINT_LIGHT_SNIPPET.replace("%", Integer.toString(ii)));
|
||||||
"].position.xyz - eyeVertex;");
|
} else if (light.type == Light.LT_DIRECTIONAL) {
|
||||||
buf.append("float ldist" + ii + " = length(lvec" + ii + ");");
|
buf.append(DIRECTIONAL_LIGHT_SNIPPET.replace("%", Integer.toString(ii)));
|
||||||
}
|
|
||||||
buf.append("gl_FrontColor.rgb += (gl_FrontLightProduct[" + ii + "].ambient.rgb + ");
|
|
||||||
buf.append("gl_FrontLightProduct[" + ii + "].diffuse.rgb * max(dot(eyeNormal, ");
|
|
||||||
if (light.type == Light.LT_POINT) {
|
|
||||||
buf.append("normalize(lvec" + ii + ")), 0.0)) ");
|
|
||||||
buf.append("/ (gl_LightSource[" + ii + "].constantAttenuation + " +
|
|
||||||
"ldist" + ii + " * gl_LightSource[" + ii + "].linearAttenuation + " +
|
|
||||||
"ldist" + ii + " * ldist" + ii + " * gl_LightSource[" + ii +
|
|
||||||
"].quadraticAttenuation);");
|
|
||||||
} else {
|
|
||||||
buf.append("gl_LightSource[" + ii + "].position.xyz), 0.0));");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
buf.append("gl_FrontColor.a = gl_FrontMaterial.diffuse.a;");
|
|
||||||
|
// the alpha value comes from the diffuse color in the material
|
||||||
|
buf.append("gl_FrontColor = vec4(frontColor, gl_FrontMaterial.diffuse.a);");
|
||||||
} else {
|
} else {
|
||||||
buf.append("gl_FrontColor = vec4(1.0, 1.0, 1.0, 1.0);");
|
buf.append("gl_FrontColor = vec4(1.0, 1.0, 1.0, 1.0);");
|
||||||
}
|
}
|
||||||
ddefs.add(buf.toString());
|
ddefs.add(buf.toString());
|
||||||
|
|
||||||
|
// add the def that sets the texture coordinates based on the env map modes
|
||||||
buf = new StringBuffer("SET_TEX_COORDS");
|
buf = new StringBuffer("SET_TEX_COORDS");
|
||||||
if (_textures != null) {
|
if (_textures != null) {
|
||||||
for (int ii = 0; ii < _textures.length; ii++) {
|
for (int ii = 0; ii < _textures.length; ii++) {
|
||||||
@@ -258,6 +272,13 @@ public abstract class ShaderConfig
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
ddefs.add(buf.toString());
|
ddefs.add(buf.toString());
|
||||||
|
|
||||||
|
// add the definition that sets the fog alpha based on the density function
|
||||||
|
buf = new StringBuffer("SET_FOG_ALPHA");
|
||||||
|
if (_fogDensityFunc == FogState.DF_EXP) {
|
||||||
|
buf.append(" fogAlpha = exp(gl_Fog.density * eyeVertex.z);");
|
||||||
|
}
|
||||||
|
ddefs.add(buf.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
/** The configuration of a single light in a {@link LightState}. */
|
/** The configuration of a single light in a {@link LightState}. */
|
||||||
@@ -335,4 +356,28 @@ public abstract class ShaderConfig
|
|||||||
|
|
||||||
/** The current texture configurations (or <code>null</code> if texturing is disabled). */
|
/** The current texture configurations (or <code>null</code> if texturing is disabled). */
|
||||||
protected TextureConfig[] _textures;
|
protected TextureConfig[] _textures;
|
||||||
|
|
||||||
|
/** The density function of the fog in the scene (or -1 for none). */
|
||||||
|
protected int _fogDensityFunc = -1;
|
||||||
|
|
||||||
|
/** To keep things sane, let's limit the total number of lights (OpenGL allows at least
|
||||||
|
* eight). */
|
||||||
|
protected static final int MAX_LIGHTS = 4;
|
||||||
|
|
||||||
|
/** A code snippet for adding the influence of a point light. */
|
||||||
|
protected static final String POINT_LIGHT_SNIPPET =
|
||||||
|
"vec3 lvec% = gl_LightSource[%].position.xyz - eyeVertex; " +
|
||||||
|
"float ldist% = length(lvec%); " +
|
||||||
|
"frontColor += (gl_FrontLightProduct[%].ambient.rgb + " +
|
||||||
|
"gl_FrontLightProduct[%].diffuse.rgb * " +
|
||||||
|
"max(dot(eyeNormal, normalize(lvec%)), 0.0)) / " +
|
||||||
|
"(gl_LightSource[%].constantAttenuation + " +
|
||||||
|
"ldist% * gl_LightSource[%].linearAttenuation + " +
|
||||||
|
"ldist% * ldist% * gl_LightSource[%].quadraticAttenuation);";
|
||||||
|
|
||||||
|
/** A code snippet for adding the influence of a directional light. */
|
||||||
|
protected static final String DIRECTIONAL_LIGHT_SNIPPET =
|
||||||
|
"frontColor += gl_FrontLightProduct[%].ambient.rgb + " +
|
||||||
|
"gl_FrontLightProduct[%].diffuse.rgb * " +
|
||||||
|
"max(dot(eyeNormal, gl_LightSource[%].position.xyz), 0.0);";
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user