31
Support / Re: Reflections in water ?
« on: January 25, 2010, 05:09:07 am »
that looks really nice good job.
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
/** FilmGrain
This api is designed to give an old time cinematic feel to images or animated
scenes. Also useful for in games effects like poor reception TV .
@author Jason T. Jarvis
@version %I%, %G%
*/
package jpctfx;
import java.util.Random;
import java.awt.image.BufferedImage;
import com.threed.jpct.Texture;
import com.threed.jpct.TextureManager;
import com.threed.jpct.FrameBuffer;
public class JPCTFilmGrain
{
public static final int QUALITY_HIGH = 256;
public static final int QUALITY_GOOD = 128;
public static final int QUALITY_MEDIUM = 64;
public static final int QUALITY_LOW = 32;
public static final int QUALITY_VERYLOW = 16;
private int _alpha = 20;
private int _quality = QUALITY_HIGH;
private int _frames = 3;
private int _currentFrame = 0;
private int _duration = 3;
private int _durationCount = 0;
private boolean _colourGrain = false;
private int[][] _ptexture;
private Texture[] _texture;
public JPCTFilmGrain(boolean initialize)
{
if(initialize) init();
}
public void setAlpha(int alpha)
{
alpha = alpha < 0 ? 0 : alpha;
alpha = alpha > 255? 255: alpha;
_alpha = alpha;
}
public void setQuality(int quality)
{
quality = quality < 1 ? 1 : quality;
quality = quality > 256? 256: quality;
_quality = quality;
}
public void setFrames(int frames)
{
_frames = frames < 1 ? 1 : frames;
}
public void setDuration(int duration)
{
_duration = duration < 1? 1 : duration;
}
public void colourGrain(boolean on)
{
_colourGrain = on;
}
public void setTexture(int index, Texture tex)
{
if(index >= _texture.length)
index = index % _texture.length;
TextureManager.getInstance().removeTexture("FilmGrain"+index);
TextureManager.getInstance().addTexture("FilmGrain"+index, tex);
_texture[index] = tex;
}
public int getAlpha(){ return _alpha; }
public int getQuality(){return _quality;}
public int getNumFrames(){return _frames;}
public int getDuratoin(){return _duration;}
public int getCurrentFrame(){return _currentFrame;}
public int getDurationCount(){return _durationCount;}
public boolean isColour(){return _colourGrain;}
public int[][] getPixelTextureArray(){return _ptexture;}
public int[] getPixelTexture(int index){return _ptexture[index];}
public Texture getTexture(int index){return _texture[index];}
public Texture[] getTextureArray(){return _texture;}
public void init()
{
_texture = new Texture[_frames];
_ptexture= new int[_frames][_quality * _quality];
BufferedImage src;
for(int i = 0; i < _frames; i++)
{
src = new BufferedImage(_quality, _quality, BufferedImage.TYPE_INT_ARGB);
_ptexture[i] = createGrain(src);
_texture[i] = new Texture(src, true);
TextureManager.getInstance().removeTexture("FilmGrain"+i);
TextureManager.getInstance().addTexture("FilmGrain"+i, _texture[i]);
}
}
public void apply(FrameBuffer buffer)
{
if(_durationCount >= _duration)
{
_currentFrame++;
if(_currentFrame >= _frames)
{
_currentFrame = 0;
}
}
if(_texture[_currentFrame] == null)
{
return;
}
buffer.blit(_texture[_currentFrame],
0, 0, 0, 0,
_quality, _quality,
buffer.getOutputWidth(),
buffer.getOutputHeight(),
0, false, null);
_durationCount++;
}
private int[] createGrain(BufferedImage image)
{
int argb;
int r, g, b;
int pixels[] = new int[_quality * _quality];
Random random = new Random();
for(int i = 0, y = 0; y < _quality; y++)
{
for(int x = 0; x < _quality; x++)
{
r = random.nextInt(256);
g = _colourGrain ? random.nextInt(256) : r;
b = _colourGrain ? random.nextInt(256) : r;
argb = ( (_alpha<<24) | (r<<16) | (g<<8) | b );
pixels[i++] = argb;
image.setRGB(x, y, argb);
}
}
return pixels;
}
}
filmgrain = new JPCTFilmGrain(true);
filmgrain = new JPCTFilmGrain(false);
filmgrain.setFrames(5);
filmgrain.setDuration(3);
filmgrain.setAlpha(20);
filmgrain.setQuality(JPCTFilmGrain.QUALITY_HIGH);
filmgrain.colourGrain(true);
filmgrain.init();
First start by not allowing the default initialization. Pass a false value to the constructor. Everything is optional as all of them have default values. Only the last line init() is required. The default vales are within the parameter. buffer.clear();
world.renderScene(buffer);
world.draw(buffer);
buffer.update();
filmgrain.apply(buffer); // <==== HERE
buffer.display(frame.getGraphics());
yep, just one line before the display is drawn to the screen. That is right. Minimally it only takes three lines of code to get FilmGrain into your projects(don't forget to import). Have fun downgrading
src = new BufferedImage(256, 256, BufferedImage.TYPE_INT_RGB);
createGrain(src);
_texture[i] = new Texture(src, true);
TextureManager.getInstance().addTexture("FilmGrain"+i, _texture[i]);
Removing the setAlpha() now let's me see it. setting the alpha to anything would show nothing. I also using RGB or ARGB with no difference in effect. buffer.blit(_texture[_currentFrame],
0, 0, 0, 0,
_quality, _quality,
_width, _height,
_alpha, false, null);
Alpha can be anything and it will have no effect.src = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);
createGrain(src);
_texture[i] = new Texture(src, true);
_texture[i].setAlpha(_alpha);
TextureManager.getInstance().addTexture("FilmGrain"+i, _texture[i]);
buffer.blit(_texture[_currentFrame],
0, 0, 0, 0,
_quality, _quality, true);
buffer.blit(_texture[_currentFrame],
0, 0, 0, 0,
_quality, _quality,
_width, _height,
_alpha, true, new Color(0,0,0)); // color is created else where, but I thought I would just show that's it's set to 0 0 0
buffer.clear(java.awt.Color.BLUE);
world.renderScene(buffer);
world.draw(buffer);
buffer.update();
filmgrain.apply(buffer.getGraphics(), frame);
dynamicWorld.debugDrawWorld();
buffer.display(frame.getGraphics());
/** FilmGrain
This api is designed to give an old time cinematic feel to images or animated
scenes.
@author Jason T. Jarvis
*/
import java.util.Random;
import java.awt.image.BufferedImage;
import java.awt.Graphics;
import java.awt.geom.*;
import java.awt.*;
import java.awt.image.*;
public class FilmGrain
{
public static final int QUALITY_HIGH = 1;
public static final int QUALITY_GOOD = 2;
public static final int QUALITY_MEDIUM = 4;
public static final int QUALITY_LOW = 8;
private int _width = 1;
private int _height = 1;
private int _sWidth;
private int _sHeight;
private int _alpha = 50;
private int _quality = 1;
private int _frames = 1;
private int _currentFrame = 0;
private int _duration = 1;
private int _durationCount = 0;
private BufferedImage[] _texture;
public FilmGrain(int width, int height)
{
_width = width;
_height = height;
init();
}
public FilmGrain(int frames, int width, int height)
{
_frames = frames;
_width = width;
_height = height;
init();
}
public void setAlpha(int alpha)
{
if(alpha < 0 || alpha > 255)
alpha = _alpha;
_alpha = alpha;
}
public void setQuality(int quality)
{
if(quality < 1 || quality > 64)
quality = _quality;
_quality = quality;
}
public void setGrainFrames(int frames)
{
if(frames < 1 || frames > 10)
frames = _frames;
_frames = frames;
}
public void setDuration(int duration)
{
if(duration < 1)
duration = _duration;
_duration = duration;
}
public void init()
{
_texture = new BufferedImage[_frames];
_sWidth = _width / _quality;
_sHeight = _height / _quality;
for(int i = 0; i < _frames; i++)
{
_texture[i] = new BufferedImage(_sWidth, _sHeight, BufferedImage.TYPE_INT_ARGB);
createGrain(_texture[i]);
}
}
public void apply(Component c)
{
Graphics g = c.getGraphics();
if(_durationCount >= _duration)
{
_currentFrame++;
if(_currentFrame >= _frames)
{
_currentFrame = 0;
}
}
g.drawImage(_texture[_currentFrame],
0, 0, _width, _height,
0, 0, _sWidth, _sHeight,
c);
_durationCount++;
}
public void apply(Graphics g, Component c)
{
if(_durationCount >= _duration)
{
_currentFrame++;
if(_currentFrame >= _frames)
{
_currentFrame = 0;
}
}
g.drawImage(_texture[_currentFrame],
0, 0, _width, _height,
0, 0, _sWidth, _sHeight,
c);
_durationCount++;
}
public static BufferedImage scale(BufferedImage bsrc, int width, int height){
BufferedImage bdest =
new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bdest.createGraphics();
AffineTransform at =
AffineTransform.getScaleInstance((double)width/bsrc.getWidth(),
(double)height/bsrc.getHeight());
g.drawRenderedImage(bsrc,at);
return bdest;
}
private void createGrain(BufferedImage image)
{
int argb;
int r, g, b;
int size = width * height;
int pixels[] = new int[size];
Random random = new Random();
for(int i = 0, y = 0; y < _sHeight; y++)
{
for(int x = 0; x < _sWidth; x++)
{
r = random.nextInt(256);
g = random.nextInt(256);
b = random.nextInt(256);
argb = ( (_alpha<<24) | (r<<16) | (r<<8) | r );
//argb = ( (_alpha<<24) | (r<<16) | (g<<8) | b );
//pixels[i++] = argb;
image.setRGB(x, y, argb);
}
}
}
}
* If you want to have coloured grain use the implemented version of rgb bit shift not the rrrfilmgrain = new FilmGrain(800, 600);
filmgrain.apply([frame/applet/Component]);
This will create a single frame grain on a 1:1 size. Then apply will affect the graphics of the object. filmgrain = new FilmGrain(800, 600);
filmgrain.setGrainFrames(5);
filmgrain.setDuration(3);
filmgrain.setAlpha(50);
filmgrain.setQuality(FilmGrain.QUALITY_MEDIUM);
filmgrain.init();
Create a new grain. world.renderScene(buffer);
world.draw(buffer);
filmgrain.apply(buffer.getGraphics(), [Component|Frame|Applet]);
buffer.update();
buffer.display(frame.getGraphics());
/** FilmGrain
This api is designed to give an old time cinematic feel to images or animated
scenes.
@author Jason T. Jarvis
*/
package sre.gfx;
import java.util.Random;
import java.awt.image.BufferedImage;
import java.awt.Graphics;
import java.awt.*;
import java.awt.image.*;
public class FilmGrain
{
private int _width = 1;
private int _height = 1;
private int _alpha = 50;
private int _quality = 32;
private int _frames = 1;
private int _currentFrame = 0;
private int _duration = 1;
private int _durationCount = 0;
private BufferedImage[] _texture;
public FilmGrain(int width, int height)
{
_width = width;
_height = height;
init();
}
public FilmGrain(int frames, int width, int height)
{
_frames = frames;
_width = width;
_height = height;
init();
}
public void setAlpha(int alpha)
{
if(alpha < 0 || alpha > 255)
alpha = _alpha;
_alpha = alpha;
}
public void setQuality(int quality)
{
if(quality < 1 || quality > 64)
quality = _quality;
_quality = quality;
}
public void setGrainFrames(int frames)
{
if(frames < 1 || frames > 10)
frames = _frames;
_frames = frames;
}
public void setDuration(int duration)
{
if(duration < 1)
duration = _duration;
_duration = duration;
}
public void init()
{
_texture = new BufferedImage[_frames];
for(int i = 0; i < _frames; i++)
{
_texture[i] = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_ARGB);
createGrain(_texture[i]);
}
}
public void apply(Component c)
{
Graphics g = c.getGraphics();
if(_durationCount >= _duration)
{
_currentFrame++;
if(_currentFrame >= _frames)
{
_currentFrame = 0;
}
}
g.drawImage(_texture[_currentFrame], 0, 0, _width, _height, c);
_durationCount++;
}
public void apply(Graphics g, Component c)
{
if(_durationCount >= _duration)
{
_currentFrame++;
if(_currentFrame >= _frames)
{
_currentFrame = 0;
}
}
g.drawImage(_texture[_currentFrame], 0, 0, _width, _height, c);
_durationCount++;
}
private void createGrain(BufferedImage image)
{
int argb = 0x000000;// 0x 00 00 00 00
int r, g, b;
int width = (int)_width / _quality;
int height = (int)_height / _quality;
int size = width * height;
int pixels[] = new int[size];
Random random = new Random();
for(int i = 0, y = 0; y < height; y++)
{
for(int x = 0; x < width; x++)
{
r = random.nextInt(256);
//g = random.nextInt(256);
//b = random.nextInt(256);
argb = ( (_alpha<<24) | (r<<16) | (r<<8) | r );
pixels[i++] = argb;
image.setRGB(x, y, argb);
//System.out.println(argb);
}
}
//image.setRGB(0, 0, _width, _height, pixels, offset, scanline);
}
}
--- initilization
filmgrain = new FilmGrain(800, 600);
filmgrain.setGrainFrames(3);
filmgrain.setDuration(3);
filmgrain.setAlpha(200);
filmgrain.setQuality(32);
filmgrain.init();
--- render loop
dynamicWorld.debugDrawWorld();
filmgrain.apply(buffer.getGraphics(), frame);
buffer.display(frame.getGraphics());
vertex = to - from // i'm hoping that this will treat the new vertix around a point 0/0/0
vertex.y/z = -vertex.y/z //i'm reversing yz
To = vertex + from