This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mdesl.test; | |
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT; | |
import static org.lwjgl.opengl.GL11.GL_ONE; | |
import static org.lwjgl.opengl.GL11.GL_ONE_MINUS_SRC_ALPHA; | |
import static org.lwjgl.opengl.GL11.GL_RGBA; | |
import static org.lwjgl.opengl.GL11.GL_SRC_ALPHA; | |
import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; | |
import static org.lwjgl.opengl.GL11.GL_UNSIGNED_BYTE; | |
import static org.lwjgl.opengl.GL11.glBlendFunc; | |
import static org.lwjgl.opengl.GL11.glClear; | |
import static org.lwjgl.opengl.GL11.glClearColor; | |
import static org.lwjgl.opengl.GL11.glGetTexImage; | |
import java.awt.image.BufferedImage; | |
import java.awt.image.DataBufferInt; | |
import java.io.File; | |
import java.io.IOException; | |
import java.nio.ByteBuffer; | |
import javax.imageio.ImageIO; | |
import mdesl.graphics.SpriteBatch; | |
import mdesl.graphics.Texture; | |
import mdesl.graphics.glutils.FrameBuffer; | |
import org.lwjgl.BufferUtils; | |
import org.lwjgl.LWJGLException; | |
import org.lwjgl.Sys; | |
import org.lwjgl.input.Mouse; | |
import org.lwjgl.opengl.Display; | |
public class TestScreenshot extends SimpleGame { | |
SpriteBatch batch; | |
Texture tex0, tex1; | |
boolean firstRun = true; | |
FrameBuffer fbo; | |
public void create() throws LWJGLException { | |
super.create(); | |
batch = new SpriteBatch(); | |
fbo = new FrameBuffer(128, 128); | |
try { | |
tex0 = new Texture(Util.getResource("res/particle.png")); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
Sys.alert("Error", "Could not load images"); | |
System.exit(0); | |
} | |
} | |
public void render() throws LWJGLException { | |
glClearColor(0f, 0f, 0f, 0f); | |
glClear(GL_COLOR_BUFFER_BIT); | |
fbo.begin(); | |
//clear FBO | |
glClearColor(0f, 0f, 0f, 0f); | |
glClear(GL_COLOR_BUFFER_BIT); | |
boolean additive = Mouse.isButtonDown(0); | |
//setup FBO blend func | |
glBlendFunc(GL_ONE, additive ? GL_ONE : GL_ONE_MINUS_SRC_ALPHA); | |
//resize FBO | |
batch.resize(fbo.getWidth(), fbo.getHeight()); | |
//draw all our particles | |
batch.begin(); | |
batch.draw(tex0, 0, 0); | |
batch.draw(tex0, 21, 0); | |
batch.end(); | |
fbo.end(); | |
//apply regular blending | |
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | |
batch.resize(Display.getWidth(), Display.getHeight()); | |
batch.begin(); | |
batch.draw(fbo, 0, 0); | |
batch.end(); | |
//take a "snap shot" of the first frame | |
if (firstRun) { | |
firstRun = false; | |
try { | |
ImageIO.write(getFBOImage(), "PNG", new File("test.png")); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
//load image back in... | |
try { | |
tex1 = new Texture(new File("test.png").toURI().toURL()); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
Sys.alert("Error", "Could not load images"); | |
System.exit(0); | |
} | |
} | |
//rendering the image back with regular blending... | |
batch.begin(); | |
batch.draw(tex1, tex1.getWidth()+10, 0); | |
batch.end(); | |
} | |
public BufferedImage getFBOImage() { | |
int width = fbo.getWidth(); | |
int height = fbo.getHeight(); | |
ByteBuffer buffer = BufferUtils.createByteBuffer(width * height * 4); | |
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer); | |
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); | |
int[] px = ((DataBufferInt)image.getRaster().getDataBuffer()).getData(); | |
for (int x = 0; x < width; x++) { | |
for (int y = 0; y < height; y++) { | |
int i = (x + (width * y)) * 4; | |
int r = buffer.get(i) & 0xff; | |
int g = buffer.get(i + 1) & 0xff; | |
int b = buffer.get(i + 2) & 0xff; | |
int a = buffer.get(i + 3) & 0xff; | |
int argb = (a<<24) | (r<<16) | (g<<8) | b; | |
int off = (x + width * (height-y-1)); | |
px[off] = argb; | |
} | |
} | |
return image; | |
} | |
public static void main(String[] args) throws LWJGLException { | |
Game game = new TestScreenshot(); | |
game.setDisplayMode(800, 600, false); | |
game.start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment