125 lines
3.4 KiB
Java
125 lines
3.4 KiB
Java
package skybox;
|
|
|
|
import org.atriaSoft.etk.math.Matrix4f;
|
|
import org.atriaSoft.etk.math.Vector3f;
|
|
import org.lwjgl.opengl.GL11;
|
|
import org.lwjgl.opengl.GL13;
|
|
import org.lwjgl.opengl.GL20;
|
|
import org.lwjgl.opengl.GL30;
|
|
|
|
import entities.Camera;
|
|
import models.RawModel;
|
|
import renderEngine.DisplayManager;
|
|
import renderEngine.Loader;
|
|
|
|
public class SkyboxRenderer {
|
|
private static final float SIZE = 500f;
|
|
private static final float[] VERTICES = {
|
|
-SIZE, SIZE, -SIZE,
|
|
-SIZE, -SIZE, -SIZE,
|
|
SIZE, -SIZE, -SIZE,
|
|
SIZE, -SIZE, -SIZE,
|
|
SIZE, SIZE, -SIZE,
|
|
-SIZE, SIZE, -SIZE,
|
|
|
|
-SIZE, -SIZE, SIZE,
|
|
-SIZE, -SIZE, -SIZE,
|
|
-SIZE, SIZE, -SIZE,
|
|
-SIZE, SIZE, -SIZE,
|
|
-SIZE, SIZE, SIZE,
|
|
-SIZE, -SIZE, SIZE,
|
|
|
|
SIZE, -SIZE, -SIZE,
|
|
SIZE, -SIZE, SIZE,
|
|
SIZE, SIZE, SIZE,
|
|
SIZE, SIZE, SIZE,
|
|
SIZE, SIZE, -SIZE,
|
|
SIZE, -SIZE, -SIZE,
|
|
|
|
-SIZE, -SIZE, SIZE,
|
|
-SIZE, SIZE, SIZE,
|
|
SIZE, SIZE, SIZE,
|
|
SIZE, SIZE, SIZE,
|
|
SIZE, -SIZE, SIZE,
|
|
-SIZE, -SIZE, SIZE,
|
|
|
|
-SIZE, SIZE, -SIZE,
|
|
SIZE, SIZE, -SIZE,
|
|
SIZE, SIZE, SIZE,
|
|
SIZE, SIZE, SIZE,
|
|
-SIZE, SIZE, SIZE,
|
|
-SIZE, SIZE, -SIZE,
|
|
|
|
-SIZE, -SIZE, -SIZE,
|
|
-SIZE, -SIZE, SIZE,
|
|
SIZE, -SIZE, -SIZE,
|
|
SIZE, -SIZE, -SIZE,
|
|
-SIZE, -SIZE, SIZE,
|
|
SIZE, -SIZE, SIZE
|
|
};
|
|
|
|
private static String[] TEXTURE_FILES = { "skybox/right", "skybox/left", "skybox/top", "skybox/bottom", "skybox/back", "skybox/front"};
|
|
private static String[] TEXTURE_FILES2 = { "skybox2/right", "skybox2/left", "skybox2/top", "skybox2/bottom", "skybox2/back", "skybox2/front"};
|
|
|
|
private RawModel cube;
|
|
private int texture;
|
|
private int texture2;
|
|
private SkyboxShader shader;
|
|
private float time;
|
|
|
|
public SkyboxRenderer(Loader loader, Matrix4f projectionMatrix) {
|
|
cube = loader.loadToVAO(VERTICES, 3);
|
|
texture = loader.loadCubeMap(TEXTURE_FILES);
|
|
texture2 = loader.loadCubeMap(TEXTURE_FILES2);
|
|
shader = new SkyboxShader();
|
|
shader.start();
|
|
shader.connectTextureUnits();
|
|
shader.loadProjectionMatrix(projectionMatrix);
|
|
shader.stop();
|
|
}
|
|
|
|
public void render(Camera camera, Vector3f colour) {
|
|
shader.start();
|
|
shader.loadViewMatrix(camera);
|
|
shader.loadFogColour(colour);
|
|
GL30.glBindVertexArray(cube.getVaoID());
|
|
GL20.glEnableVertexAttribArray(0);
|
|
bindTextures();
|
|
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, cube.getVertexCount());
|
|
GL20.glDisableVertexAttribArray(0);
|
|
GL30.glBindVertexArray(0);
|
|
shader.stop();
|
|
}
|
|
|
|
private void bindTextures(){
|
|
time += DisplayManager.getFrameTimeSecconds() * 1000;
|
|
time %= 24000;
|
|
int texture1;
|
|
int texture2;
|
|
float blendFactor;
|
|
if(time >= 0 && time < 5000){
|
|
texture1 = this.texture2;
|
|
texture2 = this.texture2;
|
|
blendFactor = (time - 0)/(5000 - 0);
|
|
}else if(time >= 5000 && time < 8000){
|
|
texture1 = this.texture2;
|
|
texture2 = this.texture;
|
|
blendFactor = (time - 5000)/(8000 - 5000);
|
|
}else if(time >= 8000 && time < 21000){
|
|
texture1 = this.texture;
|
|
texture2 = this.texture;
|
|
blendFactor = (time - 8000)/(21000 - 8000);
|
|
}else{
|
|
texture1 = this.texture;
|
|
texture2 = this.texture2;
|
|
blendFactor = (time - 21000)/(24000 - 21000);
|
|
}
|
|
GL13.glActiveTexture(GL13.GL_TEXTURE0);
|
|
GL11.glBindTexture(GL13.GL_TEXTURE_CUBE_MAP, texture1);
|
|
GL13.glActiveTexture(GL13.GL_TEXTURE1);
|
|
GL11.glBindTexture(GL13.GL_TEXTURE_CUBE_MAP, texture2);
|
|
shader.loadBlendFactor(blendFactor);
|
|
}
|
|
|
|
}
|