LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: jhovarie on April 05, 2018, 07:11:23

Title: How to save image without showing the window using lwjgl 2.4.9
Post by: jhovarie on April 05, 2018, 07:11:23
Hello I know I already ask this before but it is lwjgl version 3.. the link is http://forum.lwjgl.org/index.php?topic=6677.msg35296#msg35296
I want to use lwjgl 2.4.9

my code looks like this.

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;

import javax.imageio.ImageIO;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;

import models.RawModel;
import models.TexturedModel;
import renderEngine.DisplayManager;
import renderEngine.Loader;
import renderEngine.Renderer;
import shaders.StaticShader;
import texture.ModelTexture;

public class MainGameLoop {

public static void main(String[] args) throws IOException {
DisplayManager.createDisplay();
Loader loader = new Loader();
Renderer renderer = new Renderer();
StaticShader shader = new StaticShader();

float[]vertices = {
-1f, 1f, 0, //VO
-1f, -1f, 0, //V1
1f, -1f, 0, //V2
1f, 1f, 0 //V3
};

/*
float[]vertices = {
-0.5f, 0.5f, 0, //VO
-0.5f, -0.5f, 0, //V1
0.5f, -0.5f, 0, //V2
0.5f, 0.5f, 0 //V3
};
*/

int[]indices = {
0, 1, 3, //Top Left triangle (V0, V1, V3)
3, 1, 2  //Bottom right triangle (V3, V1, V2)
};

float[]textureCoords = {
0,0, //V0
0,1, //V1
1,1, //V2
1,0  //V3
};

//RawModel model = loader.loadToVAO(vertices, indices);
RawModel model = loader.loadToVAO(vertices, textureCoords, indices);
ModelTexture texture = new ModelTexture(loader.loadTexture("image.png"));
//ModelTexture texture = new ModelTexture(loader.loadTexture("pirate.png"));
TexturedModel texturedModel = new TexturedModel(model, texture);


int counter = 0;
while(!Display.isCloseRequested()){
renderer.prepare();
//game logic
//render
shader.start();
renderer.render(texturedModel);
//renderer.render(model);
shader.stop();
DisplayManager.updateDisplay();


if(counter == 5) {
ImageIO.write(glToBufferdImage(), "png", new File("C:/output.png"));
System.exit(0);
}
counter++;
}

shader.cleanUp();
loader.cleanUp();
DisplayManager.closeDisplay();

}

private static  BufferedImage glToBufferdImage() {
        //First off, we need to access the pixel data of the Display. Without this, we don't know what to save!
        GL11.glReadBuffer(GL11.GL_FRONT);
        int width = 1280;
        int height= 720;
        int bpp = 4; // Assuming a 32-bit display with a byte each for red, green, blue, and alpha.
        ByteBuffer buffer = BufferUtils.createByteBuffer(width * height * bpp);
        GL11.glReadPixels(0, 0, width, height, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer );
         
        //Save the screen image
       // File file = new File("output.png"); // The file to save to.
      //  String format = "png"; // Example: "PNG" or "JPG"
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
           
        for(int x = 0; x < width; x++)
        {
            for(int y = 0; y < height; y++)
            {
                int i = (x + (width * y)) * bpp;
                int r = buffer.get(i) & 0xFF;
                int g = buffer.get(i + 1) & 0xFF;
                int b = buffer.get(i + 2) & 0xFF;
                image.setRGB(x, height - (y + 1), (0xFF << 24) | (r << 16) | (g << 8) | b);
            }
        }
        return image;
    }
}


I want to save image with out showing the windows any help?
I did not include other class because it is not important.