Add light from flame to smoke.

This commit is contained in:
bkuker 2013-12-04 12:33:35 -05:00
parent ba037a1b2c
commit e9a75b1111
3 changed files with 54 additions and 9 deletions

View File

@ -1,25 +1,36 @@
uniform sampler2D uNormal;
uniform sampler2D uSmoke;
uniform float z;
void main(void)
{
vec3 normal = 2.0 * texture2D (uNormal, gl_TexCoord[0].st).rgb - 1.0;
normal = normalize (gl_NormalMatrix * normal);
float lamberFactor = max (dot (gl_LightSource[1].position.xyz, normal), 0.0) ;
lamberFactor = lamberFactor * .6 + .4;
//Load only the Alpha from the smoke texture
vec4 diffuseMaterial = texture2D (uSmoke, gl_TexCoord[0].st);
diffuseMaterial.rgb = vec3(1);
vec4 diffuseLight = gl_LightSource[1].diffuse;
//Get Ambient light
vec4 ambientLight = gl_LightSource[1].ambient;
vec4 bump = vec4(lamberFactor,lamberFactor,lamberFactor,1);
//Calculate diffuse light from "sun"
vec3 diffuseNormal = normal;
diffuseNormal.b = diffuseNormal.b * 0.5;
diffuseNormal = normalize (gl_NormalMatrix * diffuseNormal);
vec4 diffuseLight = gl_LightSource[1].diffuse;
float diffuseFactor = max (dot (gl_LightSource[1].position.xyz, diffuseNormal), 0.0) ;
diffuseFactor = diffuseFactor * .6 + .4;
vec4 diffuseBump = vec4(diffuseFactor,diffuseFactor,diffuseFactor,1);
vec4 light = diffuseLight * bump + ambientLight;
gl_FragColor = gl_Color * diffuseMaterial * light;
vec4 flameColor = gl_LightSource[2].diffuse;
float flameFactor = max (dot (vec3(0,0,1), diffuseNormal), 0.0);
flameFactor = flameFactor * (1 - z / 1.0);
flameFactor = flameFactor * diffuseMaterial.a * gl_Color.a * 10;
flameFactor = clamp(flameFactor,0,1);
vec4 light = diffuseLight * diffuseBump + ambientLight;
gl_FragColor = gl_Color * diffuseMaterial * light + flameColor * flameFactor;
}

View File

@ -345,6 +345,7 @@ public class PhotoPanel extends JPanel implements GLEventListener {
gl.glEnable(GLLightingFunc.GL_LIGHT2);
} else {
gl.glDisable(GLLightingFunc.GL_LIGHT2);
gl.glLightfv(GLLightingFunc.GL_LIGHT2, GLLightingFunc.GL_DIFFUSE, new float[] { 0,0,0,1 }, 0);
}

View File

@ -168,6 +168,10 @@ public final class FlameRenderer {
}
public static void drawExhaust(GL2 gl, FlameSettings fs, Motor motor) {
//TODO REmove these
//dispose(gl);
//init(gl);
final float s = (float) Math.max(.5, Math.sqrt(motor.getAverageThrustEstimate()) / 4.0)
* (float) fs.getExhaustScale();
@ -307,6 +311,7 @@ public final class FlameRenderer {
public static void init(GL2 gl) {
try {
log.debug("Loading Textures");
TextureData data = TextureIO.newTextureData(GLProfile.getDefault(),
FlameRenderer.class.getResourceAsStream("/datafiles/flame/c-color.png"), GL.GL_RGBA, GL.GL_RGBA,
true, null);
@ -320,6 +325,7 @@ public final class FlameRenderer {
true, null);
flameT = TextureIO.newTexture(data);
log.debug("Loading Shader");
String line;
shaderprogram = gl.glCreateProgram();
@ -365,6 +371,19 @@ public final class FlameRenderer {
e1.printStackTrace();
}
}
public static void dispose(GL2 gl) {
log.debug("Destroying Textures");
smokeT.destroy(gl);
smokeN.destroy(gl);
flameT.destroy(gl);
smokeT = null;
smokeN = null;
flameT = null;
log.debug("Deleting Shader {}", shaderprogram);
//gl.glDeleteShader(shaderprogram); TODO Why is this broken?
shaderprogram = 0;
}
private static void trail(GL2 gl, Func radius, Func dZ, Func alpha, float LEN, int P, Color color, float scale) {
float[] c = new float[4];
@ -416,6 +435,11 @@ public final class FlameRenderer {
c[3] = alpha.f(z);
gl.glColor4fv(c, 0);
int[] ii = {0};
gl.glGetIntegerv(GL2.GL_CURRENT_PROGRAM, ii, 0);
if ( ii[0] == shaderprogram )
setUniform1f(gl, shaderprogram, "z", z);
for (int i = 0; i < P; i++) {
gl.glPushMatrix();
@ -460,6 +484,15 @@ public final class FlameRenderer {
log.warn("UNIFORM COULD NOT BE FOUND! NAME={}", inName);
}
}
private static void setUniform1f(GL2 inGL, int inProgramID, String inName, float inValue) {
int tUniformLocation = inGL.glGetUniformLocation(inProgramID, inName);
if (tUniformLocation != -1) {
inGL.glUniform1f(tUniformLocation, inValue);
} else {
log.warn("UNIFORM COULD NOT BE FOUND! NAME={}", inName);
}
}
private static void sparks(GL2 gl, FlameSettings fs) {
// Use the same seed every time