diff --git a/res/blendMap.png b/res/blendMap.png new file mode 100644 index 0000000..b0d2cb7 Binary files /dev/null and b/res/blendMap.png differ diff --git a/res/dirt.png b/res/dirt.png new file mode 100644 index 0000000..4e139ae Binary files /dev/null and b/res/dirt.png differ diff --git a/res/grassFlowers.png b/res/grassFlowers.png new file mode 100644 index 0000000..65c2e9c Binary files /dev/null and b/res/grassFlowers.png differ diff --git a/res/grassy.png b/res/grassy.png new file mode 100644 index 0000000..249ced7 Binary files /dev/null and b/res/grassy.png differ diff --git a/res/mud.png b/res/mud.png new file mode 100644 index 0000000..dc1632f Binary files /dev/null and b/res/mud.png differ diff --git a/res/path.png b/res/path.png new file mode 100644 index 0000000..84b3bac Binary files /dev/null and b/res/path.png differ diff --git a/src/engineTester/MainGameLoop.java b/src/engineTester/MainGameLoop.java index f3cce6b..1631586 100644 --- a/src/engineTester/MainGameLoop.java +++ b/src/engineTester/MainGameLoop.java @@ -22,6 +22,8 @@ import renderEngine.OBJLoader; import renderEngine.EntityRenderer; import shaders.StaticShader; import terrains.Terrain; +import terrains.TerrainTexture; +import terrains.TerrainTexturePack; import textures.ModelTexture; /** @@ -57,14 +59,28 @@ public class MainGameLoop { new ModelTexture(loader.loadTexture("grassTexture"))); grassModel.getTexture().setHasTransparency(true); grassModel.getTexture().setUseFakeLighting(true); + + TexturedModel flowerModel = new TexturedModel(OBJLoader.loadObjModel("grassModel", loader), + new ModelTexture(loader.loadTexture("flower"))); + flowerModel.getTexture().setHasTransparency(true); + flowerModel.getTexture().setUseFakeLighting(true); + TexturedModel fernModel = new TexturedModel(OBJLoader.loadObjModel("fern", loader), new ModelTexture(loader.loadTexture("fern"))); fernModel.getTexture().setHasTransparency(true); Light light = new Light(new Vector3f(3000,2000,2000), new Vector3f(1,1,1)); + + TerrainTexture backgroundTexture = new TerrainTexture(loader.loadTexture("grass")); + TerrainTexture rTexture = new TerrainTexture(loader.loadTexture("dirt")); + TerrainTexture gTexture = new TerrainTexture(loader.loadTexture("grassFlowers")); + TerrainTexture bTexture = new TerrainTexture(loader.loadTexture("path")); + TerrainTexturePack texturePack = new TerrainTexturePack(backgroundTexture, rTexture, gTexture, bTexture); + + TerrainTexture blendMap = new TerrainTexture(loader.loadTexture("blendMap")); - Terrain terrain = new Terrain(0,-1,loader, new ModelTexture(loader.loadTexture("grass"))); - Terrain terrain2 = new Terrain(-1,-1,loader, new ModelTexture(loader.loadTexture("grass"))); + Terrain terrain = new Terrain(0,-1,loader, texturePack, blendMap); + Terrain terrain2 = new Terrain(-1,-1,loader, texturePack, blendMap); Camera camera = new Camera(); @@ -81,6 +97,9 @@ public class MainGameLoop { entities.add(new Entity(fernModel, new Vector3f(random.nextFloat()*800 - 400, 0, random.nextFloat() * -600), new Vector3f(0,0,0),0.6f)); + entities.add(new Entity(flowerModel, + new Vector3f(random.nextFloat()*800 - 400, 0, random.nextFloat() * -600), + new Vector3f(0,0,0),1.5f)); } MasterRenderer renderer = new MasterRenderer(); diff --git a/src/entities/Camera.java b/src/entities/Camera.java index 6131fc8..ef198f5 100644 --- a/src/entities/Camera.java +++ b/src/entities/Camera.java @@ -26,6 +26,12 @@ public class Camera { if (DisplayManager.isKeyDown('a')) { position.x -= 0.8f; } + if (DisplayManager.isKeyDown('q')) { + position.y += 0.8f; + } + if (DisplayManager.isKeyDown('z')) { + position.y -= 0.8f; + } } public Vector3f getPosition() { diff --git a/src/renderEngine/Loader.java b/src/renderEngine/Loader.java index 717acdb..687d1f5 100644 --- a/src/renderEngine/Loader.java +++ b/src/renderEngine/Loader.java @@ -57,7 +57,6 @@ public class Loader { } private int loadPNGTexture(String filename, int textureUnit) { - ByteBuffer bufIn = null; ByteBuffer buf = null; int tWidth = 0; int tHeight = 0; @@ -71,7 +70,8 @@ public class Loader { tHeight = decoder.getHeight(); // Decode the PNG file in a ByteBuffer buf = ByteBuffer.allocateDirect(4 * decoder.getWidth() * decoder.getHeight()); - decoder.decodeFlipped(buf, decoder.getWidth() * 4, Format.RGBA); + //decoder.decodeFlipped(buf, decoder.getWidth() * 4, Format.RGBA); + decoder.decode(buf, decoder.getWidth() * 4, Format.RGBA); buf.flip(); in.close(); } catch (IOException e) { @@ -96,10 +96,10 @@ public class Loader { GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT); // Setup what to do when the texture has to be scaled -// GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST); -// GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR_MIPMAP_LINEAR); - GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR); - GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR); + GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST); + GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR_MIPMAP_LINEAR); +// GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR); +// GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR); this.exitOnGLError("loadPNGTexture"); diff --git a/src/renderEngine/OBJLoader.java b/src/renderEngine/OBJLoader.java index 07b0258..6e66b8e 100644 --- a/src/renderEngine/OBJLoader.java +++ b/src/renderEngine/OBJLoader.java @@ -6,7 +6,8 @@ import objConverter.OBJFileLoader; public class OBJLoader { public static RawModel loadObjModel(String fileName, Loader loader) { - ModelData data = OBJFileLoader.loadOBJ("tree"); + System.out.println("Load file " + fileName); + ModelData data = OBJFileLoader.loadOBJ(fileName); return loader.loadToVAO(data.getVertices(), data.getTextureCoords(), data.getNormals(), data.getIndices()); } } diff --git a/src/renderEngine/TerrainRenderer.java b/src/renderEngine/TerrainRenderer.java index 22b956d..9ad39ae 100644 --- a/src/renderEngine/TerrainRenderer.java +++ b/src/renderEngine/TerrainRenderer.java @@ -13,6 +13,7 @@ import org.lwjgl.opengl.GL30; import models.RawModel; import shaders.TerrainShader; import terrains.Terrain; +import terrains.TerrainTexturePack; import textures.ModelTexture; import toolbox.Maths; @@ -24,6 +25,7 @@ public class TerrainRenderer { this.shader = shader; shader.start(); shader.loadProjectionMatrix(projectionMatrix); + shader.connectTextureUnits(); shader.stop(); } @@ -43,12 +45,24 @@ public class TerrainRenderer { GL20.glEnableVertexAttribArray(0); GL20.glEnableVertexAttribArray(1); GL20.glEnableVertexAttribArray(2); - ModelTexture texture = terrain.getTexture(); - shader.loadShineVariable(texture.getShineDamper(), texture.getReflectivity()); - GL13.glActiveTexture(GL13.GL_TEXTURE0); - GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getTexturedID()); + bindTextures(terrain); + shader.loadShineVariable(1, 0); } + private void bindTextures(Terrain terrain) { + TerrainTexturePack texturePack = terrain.getTexturePack(); + GL13.glActiveTexture(GL13.GL_TEXTURE0); + GL11.glBindTexture(GL11.GL_TEXTURE_2D, texturePack.getBackgroundTexture().getTextureId()); + GL13.glActiveTexture(GL13.GL_TEXTURE1); + GL11.glBindTexture(GL11.GL_TEXTURE_2D, texturePack.getrTexture().getTextureId()); + GL13.glActiveTexture(GL13.GL_TEXTURE2); + GL11.glBindTexture(GL11.GL_TEXTURE_2D, texturePack.getgTexture().getTextureId()); + GL13.glActiveTexture(GL13.GL_TEXTURE3); + GL11.glBindTexture(GL11.GL_TEXTURE_2D, texturePack.getbTexture().getTextureId()); + GL13.glActiveTexture(GL13.GL_TEXTURE4); + GL11.glBindTexture(GL11.GL_TEXTURE_2D, terrain.getBlendMap().getTextureId()); + } + private void unbindTexturedModel() { GL20.glDisableVertexAttribArray(0); GL20.glDisableVertexAttribArray(1); diff --git a/src/shaders/ShaderProgram.java b/src/shaders/ShaderProgram.java index dd9f6ad..835ea18 100644 --- a/src/shaders/ShaderProgram.java +++ b/src/shaders/ShaderProgram.java @@ -60,10 +60,15 @@ public abstract class ShaderProgram { protected void loadFloat(int location, float value) { GL20.glUniform1f(location, value); } + + protected void loadInt(int location, int value) { + GL20.glUniform1i(location, value); + } protected void loadVector(int location, Vector3f value) { GL20.glUniform3f(location, value.x, value.y, value.z); } + protected void loadBoolean(int location, boolean value) { //System.out.println("set value " + value + " " + (value==true?1.0f:0.0f)); GL20.glUniform1f(location, value==true?1.0f:0.0f); diff --git a/src/shaders/TerrainShader.java b/src/shaders/TerrainShader.java index 3407325..785361e 100644 --- a/src/shaders/TerrainShader.java +++ b/src/shaders/TerrainShader.java @@ -19,6 +19,11 @@ public class TerrainShader extends ShaderProgram { private int location_reflectivity; private int location_shineDamper; private int location_skyColor; + private int location_backgroundTexture; + private int location_rTexture; + private int location_gTexture; + private int location_bTexture; + private int location_blendMap; public TerrainShader() { super(VERTEX_FILE, FRAGMENT_FILE); @@ -41,6 +46,19 @@ public class TerrainShader extends ShaderProgram { location_reflectivity = super.getUniformLocation("reflectivity"); location_shineDamper = super.getUniformLocation("shineDamper"); location_skyColor = super.getUniformLocation("skyColor"); + location_backgroundTexture = super.getUniformLocation("backgroundTexture"); + location_rTexture = super.getUniformLocation("rTexture"); + location_gTexture = super.getUniformLocation("gTexture"); + location_bTexture = super.getUniformLocation("bTexture"); + location_blendMap = super.getUniformLocation("blendMap"); + } + + public void connectTextureUnits() { + super.loadInt(location_backgroundTexture, 0); + super.loadInt(location_rTexture, 1); + super.loadInt(location_gTexture, 2); + super.loadInt(location_bTexture, 3); + super.loadInt(location_blendMap, 4); } public void loadSkyColour(Vector3f color) { diff --git a/src/shaders/terrainFragmentShader.txt b/src/shaders/terrainFragmentShader.txt index 5c8bae6..5f863f2 100644 --- a/src/shaders/terrainFragmentShader.txt +++ b/src/shaders/terrainFragmentShader.txt @@ -9,7 +9,12 @@ in float visibility; out vec4 out_Color; -uniform sampler2D textureSampler; +uniform sampler2D backgroundTexture; +uniform sampler2D rTexture; +uniform sampler2D gTexture; +uniform sampler2D bTexture; +uniform sampler2D blendMap; + uniform vec3 lightColour; uniform float reflectivity; uniform float shineDamper; @@ -18,6 +23,17 @@ uniform vec3 skyColor; void main(void) { + vec4 blendMapColour = texture(blendMap, pass_textureCoordinates); + + float backTextureAmount = 1 - (blendMapColour.r + blendMapColour.g + blendMapColour.b); + vec2 tiledCoords = pass_textureCoordinates * 40.0; + vec4 backgroundTextureColour = texture(backgroundTexture, tiledCoords) * backTextureAmount; + vec4 rTextureColour = texture(rTexture, tiledCoords) * blendMapColour.r; + vec4 gTextureColour = texture(gTexture, tiledCoords) * blendMapColour.g; + vec4 bTextureColour = texture(bTexture, tiledCoords) * blendMapColour.b; + + vec4 totalColour = backgroundTextureColour + rTextureColour + gTextureColour + bTextureColour; + vec3 unitNormal = normalize(surfaceNormal); vec3 unitLightVector = normalize(toLightVector); @@ -35,7 +51,7 @@ void main(void) { float damperFactor = pow(specularFactor, shineDamper); vec3 finalSpecular = damperFactor * reflectivity * lightColour; - out_Color = vec4(diffuse,1.0) * texture(textureSampler,pass_textureCoordinates) + vec4(finalSpecular, 1.0); + out_Color = vec4(diffuse,1.0) * totalColour + vec4(finalSpecular, 1.0); out_Color = mix(vec4(skyColor,1.0), out_Color, visibility); } diff --git a/src/terrains/Terrain.java b/src/terrains/Terrain.java index 3f6bb37..6224c9c 100644 --- a/src/terrains/Terrain.java +++ b/src/terrains/Terrain.java @@ -11,10 +11,12 @@ public class Terrain { private float x; private float z; private RawModel model; - private ModelTexture texture; + private TerrainTexturePack texturePack; + private TerrainTexture blendMap; - public Terrain(int girdX, int girdZ, Loader loader, ModelTexture texture) { - this.texture = texture; + public Terrain(int girdX, int girdZ, Loader loader, TerrainTexturePack texturePack, TerrainTexture blendMap) { + this.texturePack = texturePack; + this.blendMap = blendMap; this.x = girdX * SIZE; this.z = girdZ * SIZE; this.model = generateTerrain(loader); @@ -35,8 +37,8 @@ public class Terrain { normals[vertexPointer*3] = 0; normals[vertexPointer*3+1] = 1; normals[vertexPointer*3+2] = 0; - textureCoords[vertexPointer*2] = (float)j/((float)VERTEX_COUNT - 1)*100; - textureCoords[vertexPointer*2+1] = (float)i/((float)VERTEX_COUNT - 1)*100; + textureCoords[vertexPointer*2] = (float)j/((float)VERTEX_COUNT - 1); + textureCoords[vertexPointer*2+1] = (float)i/((float)VERTEX_COUNT - 1); vertexPointer++; } } @@ -82,11 +84,11 @@ public class Terrain { this.model = model; } - public ModelTexture getTexture() { - return texture; + public TerrainTexture getBlendMap() { + return blendMap; } - public void setTexture(ModelTexture texture) { - this.texture = texture; + public TerrainTexturePack getTexturePack() { + return texturePack; } } diff --git a/src/terrains/TerrainTexture.java b/src/terrains/TerrainTexture.java new file mode 100644 index 0000000..085a2d3 --- /dev/null +++ b/src/terrains/TerrainTexture.java @@ -0,0 +1,18 @@ +package terrains; + +public class TerrainTexture { + private int textureId; + + public TerrainTexture(int textureId) { + this.textureId = textureId; + } + + public int getTextureId() { + return textureId; + } + + public void setTextureId(int textureId) { + this.textureId = textureId; + } + +} diff --git a/src/terrains/TerrainTexturePack.java b/src/terrains/TerrainTexturePack.java new file mode 100644 index 0000000..d2059ba --- /dev/null +++ b/src/terrains/TerrainTexturePack.java @@ -0,0 +1,32 @@ +package terrains; + +public class TerrainTexturePack { + private TerrainTexture backgroundTexture; + private TerrainTexture rTexture; + private TerrainTexture gTexture; + private TerrainTexture bTexture; + + public TerrainTexturePack(TerrainTexture backgroundTexture, TerrainTexture rTexture, TerrainTexture gTexture, + TerrainTexture bTexture) { + this.backgroundTexture = backgroundTexture; + this.rTexture = rTexture; + this.gTexture = gTexture; + this.bTexture = bTexture; + } + + public TerrainTexture getBackgroundTexture() { + return backgroundTexture; + } + + public TerrainTexture getrTexture() { + return rTexture; + } + + public TerrainTexture getgTexture() { + return gTexture; + } + + public TerrainTexture getbTexture() { + return bTexture; + } +}