/** Company: Shout3D LLC Project: Shout3D 2.0 Sample Code Class: FogEffect Date: March 23, 2000 Description: A simple example of a PostRenderEffect that adds a fog to the scene. (C) Copyright Shout3D LLC. - 1997-2001 - All rights reserved */ package custom_nodes; import shout3d.core.*; import shout3d.*; import java.awt.Graphics; /** * FogEffect * * @author Paul Isaacs */ public class FogEffect extends PostRenderEffect { // fogColor is the color that the scene fades to. // Objects closer than fogNear will render fully as their own color // Objects between fogNear and fogFar will fade linearly from their own color to fogColor // Objects farther than fogFar will be rendered fully as fogFar final public FloatArrayField fogColor = new FloatArrayField(this, "fogColor", Field.COLOR, new float[]{0,0,0}); final public FloatField fogNear = new FloatField( this, "fogNear", Field.ANY, 0); final public FloatField fogFar = new FloatField( this, "fogFar", Field.ANY, 50); /** * Constructs a default FogEffect node. */ public FogEffect() {} public void filter(Graphics offScreenGraphics, int surface_pixel_bits[], float z_buffer[], int deviceWidth, int deviceHeight) { if (surface_pixel_bits == null || z_buffer == null) return; // Get the fog color components into byte form. int fogR = (int)(fogColor.getValue()[0]*255) &0xff; int fogG = (int)(fogColor.getValue()[1]*255) &0xff; int fogB = (int)(fogColor.getValue()[2]*255) &0xff; // Put the fog color components into a single aggregate int, suitable // for plopping into the array of pixels. int fogColorInt = 0xff000000 + (fogR<<16) + (fogG<<8) + fogB; // Handy variables. float f_far = fogFar.getValue(); float f_near = fogNear.getValue(); float farMinusNear = f_far - f_near; float zDist, normZDist; float objRatio; int imgR, imgG, imgB; // Iterate over all the pixels. for (int i=0;i= f_far) normZDist = 1f; else if (zDist <= f_near) normZDist = 0f; else normZDist = (float)((zDist - f_near)/farMinusNear); // The pixel gets a contribution of normZDist times the fog color, // plus (1-normZDist) times the color in the image. objRatio = (float)(1.0 - normZDist); imgR = (surface_pixel_bits[i]>>16)&0xff; imgG = (surface_pixel_bits[i]>>8)&0xff; imgB = surface_pixel_bits[i] &0xff; surface_pixel_bits[i] = 0xff000000 + (((int)(objRatio * imgR + normZDist * fogR)&0xff)<<16) + (((int)(objRatio * imgG + normZDist * fogG)&0xff)<<8) + ((int)(objRatio * imgB + normZDist * fogB)&0xff); } } } }