Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View TestScreenshot.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
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
Something went wrong with that request. Please try again.