diff --git a/res/skybox/back.png b/res/skybox/back.png new file mode 100644 index 0000000..6dea976 Binary files /dev/null and b/res/skybox/back.png differ diff --git a/res/skybox/bottom.png b/res/skybox/bottom.png new file mode 100644 index 0000000..ffd848e Binary files /dev/null and b/res/skybox/bottom.png differ diff --git a/res/skybox/front.png b/res/skybox/front.png new file mode 100644 index 0000000..b4f5fb7 Binary files /dev/null and b/res/skybox/front.png differ diff --git a/res/skybox/left.png b/res/skybox/left.png new file mode 100644 index 0000000..23a1de2 Binary files /dev/null and b/res/skybox/left.png differ diff --git a/res/skybox/right.png b/res/skybox/right.png new file mode 100644 index 0000000..8ce890b Binary files /dev/null and b/res/skybox/right.png differ diff --git a/res/skybox/top.png b/res/skybox/top.png new file mode 100644 index 0000000..dce761f Binary files /dev/null and b/res/skybox/top.png differ diff --git a/src/engineTester/MainGameLoop.java b/src/engineTester/MainGameLoop.java index 56a86e3..3aa9eac 100644 --- a/src/engineTester/MainGameLoop.java +++ b/src/engineTester/MainGameLoop.java @@ -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 diff --git a/src/guis/GuiRenderer.java b/src/guis/GuiRenderer.java index 9d001a1..e866244 100644 --- a/src/guis/GuiRenderer.java +++ b/src/guis/GuiRenderer.java @@ -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 guis) { diff --git a/src/renderEngine/Loader.java b/src/renderEngine/Loader.java index 5db0609..d8770dd 100644 --- a/src/renderEngine/Loader.java +++ b/src/renderEngine/Loader.java @@ -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> entities = new HashMap>(); private List terrains = new ArrayList(); - 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() { @@ -69,6 +73,7 @@ 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 new file mode 100644 index 0000000..be073c8 --- /dev/null +++ b/src/skybox/SkyboxRenderer.java @@ -0,0 +1,87 @@ +package skybox; + +import org.atriaSoft.etk.math.Matrix4f; +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.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 RawModel cube; + private int texture; + private SkyboxShader shader; + + public SkyboxRenderer(Loader loader, Matrix4f projectionMatrix) { + cube = loader.loadToVAO(VERTICES, 3); + texture = loader.loadCubeMap(TEXTURE_FILES); + shader = new SkyboxShader(); + shader.start(); + shader.loadProjectionMatrix(projectionMatrix); + shader.stop(); + } + + public void render(Camera camera) { + shader.start(); + shader.loadViewMatrix(camera); + GL30.glBindVertexArray(cube.getVaoID()); + GL20.glEnableVertexAttribArray(0); + GL13.glActiveTexture(GL13.GL_TEXTURE0); + GL11.glBindTexture(GL13.GL_TEXTURE_CUBE_MAP, texture); + GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, cube.getVertexCount()); + GL20.glDisableVertexAttribArray(0); + GL30.glBindVertexArray(0); + shader.stop(); + } + +} diff --git a/src/skybox/SkyboxShader.java b/src/skybox/SkyboxShader.java new file mode 100644 index 0000000..64e3cac --- /dev/null +++ b/src/skybox/SkyboxShader.java @@ -0,0 +1,41 @@ +package skybox; + +import org.atriaSoft.etk.math.Matrix4f; + +import entities.Camera; +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 int location_projectionMatrix; + private int location_viewMatrix; + + 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); + super.loadMatrix(location_viewMatrix, matrix); + } + + @Override + protected void getAllUniformLocations() { + location_projectionMatrix = super.getUniformLocation("projectionMatrix"); + location_viewMatrix = super.getUniformLocation("viewMatrix"); + } + + @Override + protected void bindAttributes() { + super.bindAttribute(0, "position"); + } + +} diff --git a/src/skybox/skyboxFragmentShader.txt b/src/skybox/skyboxFragmentShader.txt new file mode 100644 index 0000000..8b64fa7 --- /dev/null +++ b/src/skybox/skyboxFragmentShader.txt @@ -0,0 +1,10 @@ +#version 400 + +in vec3 textureCoords; +out vec4 out_Color; + +uniform samplerCube cubeMap; + +void main(void){ + out_Color = texture(cubeMap, textureCoords); +} \ No newline at end of file diff --git a/src/skybox/skyboxVertexShader.txt b/src/skybox/skyboxVertexShader.txt new file mode 100644 index 0000000..d05a2e7 --- /dev/null +++ b/src/skybox/skyboxVertexShader.txt @@ -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; + +} \ No newline at end of file diff --git a/src/textures/TextureData.java b/src/textures/TextureData.java new file mode 100644 index 0000000..78a9f7c --- /dev/null +++ b/src/textures/TextureData.java @@ -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; + } +} diff --git a/src/toolbox/Maths.java b/src/toolbox/Maths.java index a9e4295..c7752da 100644 --- a/src/toolbox/Maths.java +++ b/src/toolbox/Maths.java @@ -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; } + }