Compare commits
2 Commits
d3dce20747
...
aecfb18d9d
Author | SHA1 | Date | |
---|---|---|---|
aecfb18d9d | |||
c7673e5cd0 |
BIN
res/skybox/back.png
Normal file
After Width: | Height: | Size: 1.1 MiB |
BIN
res/skybox/bottom.png
Normal file
After Width: | Height: | Size: 1.4 KiB |
BIN
res/skybox/front.png
Normal file
After Width: | Height: | Size: 991 KiB |
BIN
res/skybox/left.png
Normal file
After Width: | Height: | Size: 938 KiB |
BIN
res/skybox/right.png
Normal file
After Width: | Height: | Size: 1003 KiB |
BIN
res/skybox/top.png
Normal file
After Width: | Height: | Size: 342 KiB |
BIN
res/skybox2/back.png
Normal file
After Width: | Height: | Size: 126 KiB |
BIN
res/skybox2/bottom.png
Normal file
After Width: | Height: | Size: 28 KiB |
BIN
res/skybox2/front.png
Normal file
After Width: | Height: | Size: 38 KiB |
BIN
res/skybox2/left.png
Normal file
After Width: | Height: | Size: 38 KiB |
BIN
res/skybox2/right.png
Normal file
After Width: | Height: | Size: 57 KiB |
BIN
res/skybox2/top.png
Normal file
After Width: | Height: | Size: 24 KiB |
@ -154,7 +154,7 @@ public class MainGameLoop {
|
||||
guis.add(gui);
|
||||
|
||||
GuiRenderer guiRenderer = new GuiRenderer(loader);
|
||||
MasterRenderer renderer = new MasterRenderer();
|
||||
MasterRenderer renderer = new MasterRenderer(loader);
|
||||
|
||||
manager.setDrawer(new DisplayManagerDraw() {
|
||||
@Override
|
||||
|
@ -19,7 +19,7 @@ public class GuiRenderer {
|
||||
|
||||
public GuiRenderer(Loader loader) {
|
||||
float[] positions = {-1, 1, -1, -1, 1, 1, 1, -1};
|
||||
quad = loader.loadToVAO(positions);
|
||||
quad = loader.loadToVAO(positions, 2);
|
||||
shader = new GuiShader();
|
||||
}
|
||||
public void render(List<GuiTexture> guis) {
|
||||
|
@ -15,11 +15,11 @@ import org.lwjgl.opengl.GL13;
|
||||
import org.lwjgl.opengl.GL15;
|
||||
import org.lwjgl.opengl.GL20;
|
||||
import org.lwjgl.opengl.GL30;
|
||||
import org.lwjgl.openvr.Texture;
|
||||
|
||||
import de.matthiasmann.twl.utils.PNGDecoder;
|
||||
import de.matthiasmann.twl.utils.PNGDecoder.Format;
|
||||
import models.RawModel;
|
||||
import textures.TextureData;
|
||||
|
||||
|
||||
public class Loader {
|
||||
@ -49,12 +49,12 @@ public class Loader {
|
||||
unbindVAO();
|
||||
return new RawModel(vaoID, indices.length);
|
||||
}
|
||||
|
||||
public RawModel loadToVAO(float[] positions) {
|
||||
|
||||
public RawModel loadToVAO(float[] positions, int dimentions) {
|
||||
int vaoID = createVAO();
|
||||
this.storeDataInAttributeList(0, 2, positions);
|
||||
this.storeDataInAttributeList(0, dimentions, positions);
|
||||
unbindVAO();
|
||||
return new RawModel(vaoID, positions.length/2);
|
||||
return new RawModel(vaoID, positions.length/dimentions);
|
||||
}
|
||||
|
||||
public int loadTexture(String filename) {
|
||||
@ -65,7 +65,29 @@ public class Loader {
|
||||
return textureId;
|
||||
}
|
||||
|
||||
private int loadPNGTexture(String filename, int textureUnit) {
|
||||
public int loadCubeMap(String[] textureFiles) {
|
||||
int texId = GL11.glGenTextures();
|
||||
GL13.glActiveTexture(GL13.GL_TEXTURE0);
|
||||
GL11.glBindTexture(GL13.GL_TEXTURE_CUBE_MAP, texId);
|
||||
for (int i=0; i<textureFiles.length; i++) {
|
||||
TextureData data = decodeTextureFile("res/" + textureFiles[i] + ".png");
|
||||
GL13.glTexImage2D(GL13.GL_TEXTURE_CUBE_MAP_POSITIVE_X+i, 0,
|
||||
GL11.GL_RGBA, data.getWidth(), data.getHeight(), 0,
|
||||
GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE,
|
||||
data.getBuffer());
|
||||
}
|
||||
// Setup what to do when the texture has to be scaled
|
||||
GL11.glTexParameteri(GL13.GL_TEXTURE_CUBE_MAP, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
|
||||
GL11.glTexParameteri(GL13.GL_TEXTURE_CUBE_MAP, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
|
||||
|
||||
//this.exitOnGLError("loadCubeMap");
|
||||
textures.add(texId);
|
||||
return texId;
|
||||
|
||||
}
|
||||
|
||||
|
||||
private TextureData decodeTextureFile(String filename) {
|
||||
ByteBuffer buf = null;
|
||||
int tWidth = 0;
|
||||
int tHeight = 0;
|
||||
@ -85,9 +107,14 @@ public class Loader {
|
||||
in.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
System.err.println("try to load texture " + filename + ", didn't work");
|
||||
System.exit(-1);
|
||||
}
|
||||
|
||||
return new TextureData(buf, tWidth, tHeight);
|
||||
}
|
||||
|
||||
private int loadPNGTexture(String filename, int textureUnit) {
|
||||
TextureData data = decodeTextureFile(filename);
|
||||
// Create a new texture object in memory and bind it
|
||||
int texId = GL11.glGenTextures();
|
||||
GL13.glActiveTexture(textureUnit);
|
||||
@ -97,7 +124,7 @@ public class Loader {
|
||||
GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
|
||||
|
||||
// Upload the texture data and generate mip maps (for scaling)
|
||||
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, tWidth, tHeight, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buf);
|
||||
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, data.getWidth(), data.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, data.getBuffer());
|
||||
GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D);
|
||||
|
||||
// Setup the ST coordinate system
|
||||
@ -111,7 +138,7 @@ public class Loader {
|
||||
// GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
|
||||
|
||||
this.exitOnGLError("loadPNGTexture");
|
||||
|
||||
textures.add(texId);
|
||||
return texId;
|
||||
}
|
||||
/**
|
||||
|
@ -17,13 +17,14 @@ import entities.Light;
|
||||
import models.TexturedModel;
|
||||
import shaders.StaticShader;
|
||||
import shaders.TerrainShader;
|
||||
import skybox.SkyboxRenderer;
|
||||
import terrains.Terrain;
|
||||
|
||||
public class MasterRenderer {
|
||||
private static final float FOV = 70;
|
||||
private static final float NEAR_PLANE = 0.1f;
|
||||
private static final float FAR_PLANE = 10000;
|
||||
private static final Vector3f SKY_COLOUR = new Vector3f(0.5f, 0.5f, 0.5f);
|
||||
private static final Vector3f SKY_COLOUR = new Vector3f(0.5444f, 0.62f, 0.69f);
|
||||
|
||||
private Matrix4f projectionMatrix;
|
||||
|
||||
@ -36,13 +37,16 @@ public class MasterRenderer {
|
||||
private Map<TexturedModel, List<Entity>> entities = new HashMap<TexturedModel, List<Entity>>();
|
||||
private List<Terrain> terrains = new ArrayList<Terrain>();
|
||||
|
||||
public MasterRenderer() {
|
||||
private SkyboxRenderer skyboxRenderer;
|
||||
|
||||
public MasterRenderer(Loader loader) {
|
||||
enableCulling();
|
||||
GL11.glEnable(GL11.GL_BLEND);
|
||||
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
|
||||
createProjectionMatrix();
|
||||
renderer = new EntityRenderer(shader, projectionMatrix);
|
||||
terrainRenderer = new TerrainRenderer(terrainShader, projectionMatrix);
|
||||
skyboxRenderer = new SkyboxRenderer(loader, projectionMatrix);
|
||||
}
|
||||
|
||||
public static void enableCulling() {
|
||||
@ -56,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);
|
||||
|
124
src/skybox/SkyboxRenderer.java
Normal file
@ -0,0 +1,124 @@
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
71
src/skybox/SkyboxShader.java
Normal file
@ -0,0 +1,71 @@
|
||||
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;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public void loadProjectionMatrix(Matrix4f matrix){
|
||||
super.loadMatrix(location_projectionMatrix, matrix);
|
||||
}
|
||||
|
||||
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
|
||||
protected void bindAttributes() {
|
||||
super.bindAttribute(0, "position");
|
||||
}
|
||||
|
||||
}
|
21
src/skybox/skyboxFragmentShader.txt
Normal file
@ -0,0 +1,21 @@
|
||||
#version 400
|
||||
|
||||
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){
|
||||
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);
|
||||
}
|
14
src/skybox/skyboxVertexShader.txt
Normal file
@ -0,0 +1,14 @@
|
||||
#version 400
|
||||
|
||||
in vec3 position;
|
||||
out vec3 textureCoords;
|
||||
|
||||
uniform mat4 projectionMatrix;
|
||||
uniform mat4 viewMatrix;
|
||||
|
||||
void main(void){
|
||||
|
||||
gl_Position = projectionMatrix * viewMatrix * vec4(position, 1.0);
|
||||
textureCoords = position;
|
||||
|
||||
}
|
27
src/textures/TextureData.java
Normal file
@ -0,0 +1,27 @@
|
||||
package textures;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class TextureData {
|
||||
private int width;
|
||||
private int height;
|
||||
private ByteBuffer buffer;
|
||||
|
||||
public TextureData(ByteBuffer buffer, int width, int height){
|
||||
this.buffer = buffer;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
public int getWidth(){
|
||||
return width;
|
||||
}
|
||||
|
||||
public int getHeight(){
|
||||
return height;
|
||||
}
|
||||
|
||||
public ByteBuffer getBuffer(){
|
||||
return buffer;
|
||||
}
|
||||
}
|
@ -27,13 +27,17 @@ public class Maths {
|
||||
matrix.scale(scale);
|
||||
return matrix;
|
||||
}
|
||||
public static Matrix4f createViewMatrix(Camera camera) {
|
||||
public static Matrix4f createViewMatrixNoTranslate(Camera camera) {
|
||||
// Need to rework all of this this is really not optimum ...
|
||||
Matrix4f matrix = new Matrix4f();
|
||||
matrix.setIdentity();
|
||||
matrix.rotate(new Vector3f(1,0,0), camera.getPitch());
|
||||
matrix.rotate(new Vector3f(0,1,0), camera.getYaw());
|
||||
//matrix.rotate(new Vector3f(0,0,1), camera.getRoll());
|
||||
return matrix;
|
||||
}
|
||||
public static Matrix4f createViewMatrix(Camera camera) {
|
||||
// Need to rework all of this this is really not optimum ...
|
||||
Matrix4f matrix = createViewMatrixNoTranslate(camera);
|
||||
Vector3f camarePos = camera.getPosition();
|
||||
matrix.translate(new Vector3f(-camarePos.x,-camarePos.y,-camarePos.z));
|
||||
return matrix;
|
||||
@ -45,4 +49,5 @@ public class Maths {
|
||||
matrix.scale(new Vector3f(scale.x, scale.y, 1f));
|
||||
return matrix;
|
||||
}
|
||||
|
||||
}
|
||||
|