[DEV] tutorial 28 'day-night' implementation

This commit is contained in:
Edouard DUPIN 2020-04-25 13:49:50 +02:00
parent c7673e5cd0
commit aecfb18d9d
10 changed files with 83 additions and 5 deletions

BIN
res/skybox2/back.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 126 KiB

BIN
res/skybox2/bottom.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

BIN
res/skybox2/front.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

BIN
res/skybox2/left.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

BIN
res/skybox2/right.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 57 KiB

BIN
res/skybox2/top.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

View File

@ -60,6 +60,7 @@ public class MasterRenderer {
public void render(List<Light> 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();
}

View File

@ -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);
}
}

View File

@ -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

View File

@ -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);
}