diff --git a/res/skybox2/back.png b/res/skybox2/back.png new file mode 100644 index 0000000..1657c3a Binary files /dev/null and b/res/skybox2/back.png differ diff --git a/res/skybox2/bottom.png b/res/skybox2/bottom.png new file mode 100644 index 0000000..5ff68d9 Binary files /dev/null and b/res/skybox2/bottom.png differ diff --git a/res/skybox2/front.png b/res/skybox2/front.png new file mode 100644 index 0000000..34d393e Binary files /dev/null and b/res/skybox2/front.png differ diff --git a/res/skybox2/left.png b/res/skybox2/left.png new file mode 100644 index 0000000..43955f3 Binary files /dev/null and b/res/skybox2/left.png differ diff --git a/res/skybox2/right.png b/res/skybox2/right.png new file mode 100644 index 0000000..fe03402 Binary files /dev/null and b/res/skybox2/right.png differ diff --git a/res/skybox2/top.png b/res/skybox2/top.png new file mode 100644 index 0000000..5a4bea2 Binary files /dev/null and b/res/skybox2/top.png differ diff --git a/src/renderEngine/MasterRenderer.java b/src/renderEngine/MasterRenderer.java index c14672f..96cdb43 100644 --- a/src/renderEngine/MasterRenderer.java +++ b/src/renderEngine/MasterRenderer.java @@ -60,6 +60,7 @@ public class MasterRenderer { public void render(List lights, Camera camera) { prepare(); + skyboxRenderer.render(camera, SKY_COLOUR); shader.start(); shader.loadSkyColour(SKY_COLOUR); shader.loadLights(lights); @@ -73,7 +74,6 @@ public class MasterRenderer { terrainShader.loadViewMatrix(camera); terrainRenderer.render(terrains); terrainShader.stop(); - skyboxRenderer.render(camera); terrains.clear(); } diff --git a/src/skybox/SkyboxRenderer.java b/src/skybox/SkyboxRenderer.java index be073c8..6e0d84e 100644 --- a/src/skybox/SkyboxRenderer.java +++ b/src/skybox/SkyboxRenderer.java @@ -1,6 +1,7 @@ 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; @@ -8,6 +9,7 @@ import org.lwjgl.opengl.GL30; import entities.Camera; import models.RawModel; +import renderEngine.DisplayManager; import renderEngine.Loader; public class SkyboxRenderer { @@ -57,31 +59,66 @@ public class SkyboxRenderer { }; 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) { + public void render(Camera camera, Vector3f colour) { shader.start(); shader.loadViewMatrix(camera); + shader.loadFogColour(colour); GL30.glBindVertexArray(cube.getVaoID()); GL20.glEnableVertexAttribArray(0); - GL13.glActiveTexture(GL13.GL_TEXTURE0); - GL11.glBindTexture(GL13.GL_TEXTURE_CUBE_MAP, texture); + 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); + } + } diff --git a/src/skybox/SkyboxShader.java b/src/skybox/SkyboxShader.java index 64e3cac..78d320d 100644 --- a/src/skybox/SkyboxShader.java +++ b/src/skybox/SkyboxShader.java @@ -1,8 +1,10 @@ package skybox; import org.atriaSoft.etk.math.Matrix4f; +import org.atriaSoft.etk.math.Vector3f; import entities.Camera; +import renderEngine.DisplayManager; import shaders.ShaderProgram; import toolbox.Maths; @@ -11,8 +13,16 @@ public class SkyboxShader extends ShaderProgram { private static final String VERTEX_FILE = "src/skybox/skyboxVertexShader.txt"; private static final String FRAGMENT_FILE = "src/skybox/skyboxFragmentShader.txt"; + private static final float ROTATE_SPEED = 0.02f; + private int location_projectionMatrix; private int location_viewMatrix; + private int location_fogColour; + private int location_cubeMap; + private int location_cubeMap2; + private int location_blendFactor; + + private float rotation = 0; public SkyboxShader() { super(VERTEX_FILE, FRAGMENT_FILE); @@ -24,13 +34,33 @@ public class SkyboxShader extends ShaderProgram { public void loadViewMatrix(Camera camera){ Matrix4f matrix = Maths.createViewMatrixNoTranslate(camera); + rotation += ROTATE_SPEED * DisplayManager.getFrameTimeSecconds(); + matrix.rotate(new Vector3f(0,1,0), rotation); super.loadMatrix(location_viewMatrix, matrix); } + public void loadFogColour(Vector3f colour) { + super.loadVector(location_fogColour, colour); + } + + public void connectTextureUnits() { + super.loadInt(location_cubeMap, 0); + super.loadInt(location_cubeMap2, 1); + } + + public void loadBlendFactor(float factor) { + super.loadFloat(location_blendFactor, factor); + } + + @Override protected void getAllUniformLocations() { location_projectionMatrix = super.getUniformLocation("projectionMatrix"); location_viewMatrix = super.getUniformLocation("viewMatrix"); + location_fogColour = super.getUniformLocation("fogColour"); + location_cubeMap = super.getUniformLocation("cubeMap"); + location_cubeMap2 = super.getUniformLocation("cubeMap2"); + location_blendFactor = super.getUniformLocation("blendFactor"); } @Override diff --git a/src/skybox/skyboxFragmentShader.txt b/src/skybox/skyboxFragmentShader.txt index 8b64fa7..4fbc621 100644 --- a/src/skybox/skyboxFragmentShader.txt +++ b/src/skybox/skyboxFragmentShader.txt @@ -4,7 +4,18 @@ in vec3 textureCoords; out vec4 out_Color; uniform samplerCube cubeMap; +uniform samplerCube cubeMap2; +uniform float blendFactor; +uniform vec3 fogColour; + +const float lowerLimit = 0.0; +const float upperLimit = 30.0; void main(void){ - out_Color = texture(cubeMap, textureCoords); + vec4 texture1 = texture(cubeMap, textureCoords); + vec4 texture2 = texture(cubeMap2, textureCoords); + vec4 finalColour = mix(texture1, texture2, blendFactor); + float factor = (textureCoords.y - lowerLimit) / (upperLimit - lowerLimit); + factor = clamp(factor, 0.0, 1.0); + out_Color = mix(vec4(fogColour,1.0), finalColour, factor); } \ No newline at end of file