[DEV] tutorial 28 'day-night' implementation
This commit is contained in:
parent
c7673e5cd0
commit
aecfb18d9d
BIN
res/skybox2/back.png
Normal file
BIN
res/skybox2/back.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 126 KiB |
BIN
res/skybox2/bottom.png
Normal file
BIN
res/skybox2/bottom.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 28 KiB |
BIN
res/skybox2/front.png
Normal file
BIN
res/skybox2/front.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 38 KiB |
BIN
res/skybox2/left.png
Normal file
BIN
res/skybox2/left.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 38 KiB |
BIN
res/skybox2/right.png
Normal file
BIN
res/skybox2/right.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 57 KiB |
BIN
res/skybox2/top.png
Normal file
BIN
res/skybox2/top.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 24 KiB |
@ -60,6 +60,7 @@ public class MasterRenderer {
|
|||||||
|
|
||||||
public void render(List<Light> lights, Camera camera) {
|
public void render(List<Light> lights, Camera camera) {
|
||||||
prepare();
|
prepare();
|
||||||
|
skyboxRenderer.render(camera, SKY_COLOUR);
|
||||||
shader.start();
|
shader.start();
|
||||||
shader.loadSkyColour(SKY_COLOUR);
|
shader.loadSkyColour(SKY_COLOUR);
|
||||||
shader.loadLights(lights);
|
shader.loadLights(lights);
|
||||||
@ -73,7 +74,6 @@ public class MasterRenderer {
|
|||||||
terrainShader.loadViewMatrix(camera);
|
terrainShader.loadViewMatrix(camera);
|
||||||
terrainRenderer.render(terrains);
|
terrainRenderer.render(terrains);
|
||||||
terrainShader.stop();
|
terrainShader.stop();
|
||||||
skyboxRenderer.render(camera);
|
|
||||||
terrains.clear();
|
terrains.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package skybox;
|
package skybox;
|
||||||
|
|
||||||
import org.atriaSoft.etk.math.Matrix4f;
|
import org.atriaSoft.etk.math.Matrix4f;
|
||||||
|
import org.atriaSoft.etk.math.Vector3f;
|
||||||
import org.lwjgl.opengl.GL11;
|
import org.lwjgl.opengl.GL11;
|
||||||
import org.lwjgl.opengl.GL13;
|
import org.lwjgl.opengl.GL13;
|
||||||
import org.lwjgl.opengl.GL20;
|
import org.lwjgl.opengl.GL20;
|
||||||
@ -8,6 +9,7 @@ import org.lwjgl.opengl.GL30;
|
|||||||
|
|
||||||
import entities.Camera;
|
import entities.Camera;
|
||||||
import models.RawModel;
|
import models.RawModel;
|
||||||
|
import renderEngine.DisplayManager;
|
||||||
import renderEngine.Loader;
|
import renderEngine.Loader;
|
||||||
|
|
||||||
public class SkyboxRenderer {
|
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_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 RawModel cube;
|
||||||
private int texture;
|
private int texture;
|
||||||
|
private int texture2;
|
||||||
private SkyboxShader shader;
|
private SkyboxShader shader;
|
||||||
|
private float time;
|
||||||
|
|
||||||
public SkyboxRenderer(Loader loader, Matrix4f projectionMatrix) {
|
public SkyboxRenderer(Loader loader, Matrix4f projectionMatrix) {
|
||||||
cube = loader.loadToVAO(VERTICES, 3);
|
cube = loader.loadToVAO(VERTICES, 3);
|
||||||
texture = loader.loadCubeMap(TEXTURE_FILES);
|
texture = loader.loadCubeMap(TEXTURE_FILES);
|
||||||
|
texture2 = loader.loadCubeMap(TEXTURE_FILES2);
|
||||||
shader = new SkyboxShader();
|
shader = new SkyboxShader();
|
||||||
shader.start();
|
shader.start();
|
||||||
|
shader.connectTextureUnits();
|
||||||
shader.loadProjectionMatrix(projectionMatrix);
|
shader.loadProjectionMatrix(projectionMatrix);
|
||||||
shader.stop();
|
shader.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void render(Camera camera) {
|
public void render(Camera camera, Vector3f colour) {
|
||||||
shader.start();
|
shader.start();
|
||||||
shader.loadViewMatrix(camera);
|
shader.loadViewMatrix(camera);
|
||||||
|
shader.loadFogColour(colour);
|
||||||
GL30.glBindVertexArray(cube.getVaoID());
|
GL30.glBindVertexArray(cube.getVaoID());
|
||||||
GL20.glEnableVertexAttribArray(0);
|
GL20.glEnableVertexAttribArray(0);
|
||||||
GL13.glActiveTexture(GL13.GL_TEXTURE0);
|
bindTextures();
|
||||||
GL11.glBindTexture(GL13.GL_TEXTURE_CUBE_MAP, texture);
|
|
||||||
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, cube.getVertexCount());
|
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, cube.getVertexCount());
|
||||||
GL20.glDisableVertexAttribArray(0);
|
GL20.glDisableVertexAttribArray(0);
|
||||||
GL30.glBindVertexArray(0);
|
GL30.glBindVertexArray(0);
|
||||||
shader.stop();
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
package skybox;
|
package skybox;
|
||||||
|
|
||||||
import org.atriaSoft.etk.math.Matrix4f;
|
import org.atriaSoft.etk.math.Matrix4f;
|
||||||
|
import org.atriaSoft.etk.math.Vector3f;
|
||||||
|
|
||||||
import entities.Camera;
|
import entities.Camera;
|
||||||
|
import renderEngine.DisplayManager;
|
||||||
import shaders.ShaderProgram;
|
import shaders.ShaderProgram;
|
||||||
import toolbox.Maths;
|
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 VERTEX_FILE = "src/skybox/skyboxVertexShader.txt";
|
||||||
private static final String FRAGMENT_FILE = "src/skybox/skyboxFragmentShader.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_projectionMatrix;
|
||||||
private int location_viewMatrix;
|
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() {
|
public SkyboxShader() {
|
||||||
super(VERTEX_FILE, FRAGMENT_FILE);
|
super(VERTEX_FILE, FRAGMENT_FILE);
|
||||||
@ -24,13 +34,33 @@ public class SkyboxShader extends ShaderProgram {
|
|||||||
|
|
||||||
public void loadViewMatrix(Camera camera){
|
public void loadViewMatrix(Camera camera){
|
||||||
Matrix4f matrix = Maths.createViewMatrixNoTranslate(camera);
|
Matrix4f matrix = Maths.createViewMatrixNoTranslate(camera);
|
||||||
|
rotation += ROTATE_SPEED * DisplayManager.getFrameTimeSecconds();
|
||||||
|
matrix.rotate(new Vector3f(0,1,0), rotation);
|
||||||
super.loadMatrix(location_viewMatrix, matrix);
|
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
|
@Override
|
||||||
protected void getAllUniformLocations() {
|
protected void getAllUniformLocations() {
|
||||||
location_projectionMatrix = super.getUniformLocation("projectionMatrix");
|
location_projectionMatrix = super.getUniformLocation("projectionMatrix");
|
||||||
location_viewMatrix = super.getUniformLocation("viewMatrix");
|
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
|
@Override
|
||||||
|
@ -4,7 +4,18 @@ in vec3 textureCoords;
|
|||||||
out vec4 out_Color;
|
out vec4 out_Color;
|
||||||
|
|
||||||
uniform samplerCube cubeMap;
|
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){
|
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);
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user