<commit_before>package net.mgsx.game.examples.openworld.systems;

import com.badlogic.ashley.core.Engine;
import com.badlogic.ashley.core.EntitySystem;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Cubemap;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.glutils.FrameBufferCubemap;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.GdxRuntimeException;

import net.mgsx.game.core.GamePipeline;
import net.mgsx.game.core.GameScreen;
import net.mgsx.game.core.annotations.Editable;
import net.mgsx.game.core.annotations.EditableSystem;
import net.mgsx.game.core.annotations.Inject;

@EditableSystem
public class OpenWorldSkySystem extends EntitySystem
{
	@Inject OpenWorldLandRenderSystem landRenderer;
	@Inject OpenWorldEnvSystem environment;
	
	@Editable public boolean debugFaces = false;
	
	private final int cubeMapSize = 1024;
	private boolean cubeMapDirty;
	private FrameBufferCubemap fboCubeMap;
	private ShaderProgram bgShader;
	private PerspectiveCamera fboCam;
	
	private ShaderProgram skyShader;
	private ShapeRenderer skyRenderer;
	
	private ShapeRenderer renderer;
	private GameScreen screen;
	
	public OpenWorldSkySystem(GameScreen screen) {
		super(GamePipeline.RENDER);
		this.screen = screen;
	}
	
	public Cubemap getCubeMap() {
		return fboCubeMap.getColorBufferTexture();
	}
	
	@Editable
	public void genEnv(){
		cubeMapDirty = true;
		
		// XXX
		if(skyShader != null) skyShader.dispose();
		skyShader = new ShaderProgram(
				Gdx.files.internal("shaders/sky.vert"),
				Gdx.files.internal("shaders/sky.frag"));
		
		if(!skyShader.isCompiled()){
			throw new GdxRuntimeException(skyShader.getLog());
		}
		
		if(skyRenderer != null) skyRenderer.dispose();
		skyRenderer = new ShapeRenderer(36, skyShader);
	}
	
	@Override
	public void addedToEngine(Engine engine) {
		super.addedToEngine(engine);
		
		fboCam = new PerspectiveCamera(90, cubeMapSize, cubeMapSize);
		
		fboCubeMap = new FrameBufferCubemap(Format.RGBA8888, cubeMapSize, cubeMapSize, true);

		cubeMapDirty = true;
//		for(int i=0 ; i<6 ; i++)
//			Gdx.gl30.glFramebufferTextureLayer(GL30.GL_DRAW_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0 + i, cubeMap.glTarget, 0, 1);
	
		bgShader = new ShaderProgram(
			Gdx.files.internal("shaders/sky-bg.vert"),
			Gdx.files.internal("shaders/sky-bg.frag"));
		<commit_msg>editable cubemap size<commit_after>package net.mgsx.game.examples.openworld.systems;

import java.nio.IntBuffer;

import com.badlogic.ashley.core.Engine;
import com.badlogic.ashley.core.EntitySystem;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Cubemap;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.glutils.FrameBufferCubemap;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.BufferUtils;
import com.badlogic.gdx.utils.GdxRuntimeException;

import net.mgsx.game.core.GamePipeline;
import net.mgsx.game.core.GameScreen;
import net.mgsx.game.core.annotations.Editable;
import net.mgsx.game.core.annotations.EditableSystem;
import net.mgsx.game.core.annotations.Inject;

@EditableSystem
public class OpenWorldSkySystem extends EntitySystem
{
	@Inject OpenWorldLandRenderSystem landRenderer;
	@Inject OpenWorldEnvSystem environment;
	
	@Editable public boolean debugFaces = false;
	
	@Editable public int cubeMapSize = 2048;
	
	private boolean cubeMapDirty;
	private FrameBufferCubemap fboCubeMap;
	private ShaderProgram bgShader;
	private PerspectiveCamera fboCam;
	
	private ShaderProgram skyShader;
	private ShapeRenderer skyRenderer;
	
	private ShapeRenderer renderer;
	private GameScreen screen;
	
	public OpenWorldSkySystem(GameScreen screen) {
		super(GamePipeline.RENDER);
		this.screen = screen;
	}
	
	public Cubemap getCubeMap() {
		return fboCubeMap.getColorBufferTexture();
	}
	
	@Editable
	public void genEnv(){
		cubeMapDirty = true;
		
		// XXX
		if(skyShader != null) skyShader.dispose();
		skyShader = new ShaderProgram(
				Gdx.files.internal("shaders/sky.vert"),
				Gdx.files.internal("shaders/sky.frag"));
		
		if(!skyShader.isCompiled()){
			throw new GdxRuntimeException(skyShader.getLog());
		}
		
		if(skyRenderer != null) skyRenderer.dispose();
		skyRenderer = new ShapeRenderer(36, skyShader);
		
		// TODO utils in libGDX ?
		IntBuffer params = BufferUtils.newIntBuffer(16);
		Gdx.gl.glGetIntegerv(GL20.GL_MAX_CUBE_MAP_TEXTURE_SIZE, params);
		int maxCubeMapSize = params.get() / 4; // TODO why : because it is the max size for 1 byte format, we use 4 bytes format ... ?
		
		if(cubeMapSize > maxCubeMapSize) {
			cubeMapSize = maxCubeMapSize;
			Gdx.app.log("WARNING", "cubemap size too big : max " + maxCubeMapSize);
		}
		
		if(fboCubeMap == null || fboCubeMap.getWidth() != cubeMapSize || fboCubeMap.getHeight() != cubeMapSize){
			if(fboCubeMap != null) fboCubeMap.dispose();
			fboCubeMap = new FrameBufferCubemap(Format.RGBA8888, cubeMapSize, cubeMapSize, true);
			fboCam = new PerspectiveCamera(90, cubeMapSize, cubeMapSize);
		}
	}
	
	@Override
	public void addedToEngine(Engine engine) {
		super.addedToEngine(engine);
		
		cubeMapDirty = true;
//		for(int i=0 ; i<6 ; i++)
//			Gdx.gl30.glFramebufferTextureLayer(GL30.GL_DRAW_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0 + i, cubeMap.glTarget, 0, 1);
	
		bgShader = new ShaderProgram(
			Gdx.files.internal("shaders/sky-bg.vert"),
			Gdx.files.internal("shaders/sky-bg.frag"));
		
{'fraction_non_alphanumeric': 0.10996055226824458, 'fraction_numerical': 0.010026298487836949, 'mean_word_length': 5.115577889447236, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6746599', 'n_tokens_mistral': 2318, 'n_tokens_neox': 2076, 'n_words': 374}