diff --git a/res/fern_atlas.png b/res/fern_atlas.png new file mode 100644 index 0000000..ede2dd2 Binary files /dev/null and b/res/fern_atlas.png differ diff --git a/src/engineTester/MainGameLoop.java b/src/engineTester/MainGameLoop.java index 714e66c..405795a 100644 --- a/src/engineTester/MainGameLoop.java +++ b/src/engineTester/MainGameLoop.java @@ -66,8 +66,9 @@ public class MainGameLoop { flowerModel.getTexture().setUseFakeLighting(true); TexturedModel fernModel = new TexturedModel(OBJLoader.loadObjModel("fern", loader), - new ModelTexture(loader.loadTexture("fern"))); + new ModelTexture(loader.loadTexture("fern_atlas"))); fernModel.getTexture().setHasTransparency(true); + fernModel.getTexture().setNumberOfRows(2); Light light = new Light(new Vector3f(3000,2000,2000), new Vector3f(1,1,1)); @@ -93,11 +94,12 @@ public class MainGameLoop { new Vector3f(x, y, z), new Vector3f(0,0,0),3)); } - for (int iii=0; iii<500; iii++) { + for (int iii=0; iii<5000; iii++) { float x = random.nextFloat()*800 - 400; float z = random.nextFloat() * -600; float y = terrain.getHeightOfTerrain(x, z); entities.add(new Entity(fernModel, + random.nextInt(4), new Vector3f(x, y, z), new Vector3f(0,0,0),0.6f)); } diff --git a/src/entities/Entity.java b/src/entities/Entity.java index 406a803..2bc72f4 100644 --- a/src/entities/Entity.java +++ b/src/entities/Entity.java @@ -9,13 +9,31 @@ public class Entity { private Vector3f position; private Vector3f rotation; private float scale; + private int textureIndex = 0; public Entity(TexturedModel model, Vector3f position, Vector3f rotation, float scale) { this.model = model; this.position = position; this.rotation = rotation; this.scale = scale; } + public Entity(TexturedModel model, int textureIndex, Vector3f position, Vector3f rotation, float scale) { + this.model = model; + this.textureIndex = textureIndex; + this.position = position; + this.rotation = rotation; + this.scale = scale; + } + public float getTextureXOffset() { + int column = textureIndex%model.getTexture().getNumberOfRows(); + return (float)column/(float)model.getTexture().getNumberOfRows(); + } + public float getTextureYOffset() { + int row = textureIndex/model.getTexture().getNumberOfRows(); + return (float)row/(float)model.getTexture().getNumberOfRows(); + } + + public void increasePosition(float dx, float dy, float dz) { this.position = new Vector3f(position.x + dx, position.y + dy, position.z + dz); } diff --git a/src/models/TexturedModel.java b/src/models/TexturedModel.java index 53b03f4..923de46 100644 --- a/src/models/TexturedModel.java +++ b/src/models/TexturedModel.java @@ -28,4 +28,5 @@ public class TexturedModel { this.texture = texture; } + } diff --git a/src/renderEngine/EntityRenderer.java b/src/renderEngine/EntityRenderer.java index 9eb19ed..f9b3045 100644 --- a/src/renderEngine/EntityRenderer.java +++ b/src/renderEngine/EntityRenderer.java @@ -57,6 +57,7 @@ public class EntityRenderer { GL20.glEnableVertexAttribArray(1); GL20.glEnableVertexAttribArray(2); ModelTexture texture = model.getTexture(); + shader.loadNumberOfRows(texture.getNumberOfRows()); if (texture.isHasTransparency()) { MasterRenderer.disableCulling(); } @@ -78,6 +79,7 @@ public class EntityRenderer { private void prepareInstance(Entity entity) { Matrix4f transformationMatrix = Maths.createTransformationMatrix(entity.getPosition(), entity.getRotation(), entity.getScale()); shader.loadTransformationMatrix(transformationMatrix); + shader.loadOffset(entity.getTextureXOffset(), entity.getTextureYOffset()); } diff --git a/src/shaders/ShaderProgram.java b/src/shaders/ShaderProgram.java index 6b3bb78..d7a3abb 100644 --- a/src/shaders/ShaderProgram.java +++ b/src/shaders/ShaderProgram.java @@ -6,6 +6,7 @@ import java.io.IOException; import java.nio.FloatBuffer; import org.atriaSoft.etk.math.Matrix4f; +import org.atriaSoft.etk.math.Vector2f; import org.atriaSoft.etk.math.Vector3f; import org.lwjgl.BufferUtils; @@ -68,6 +69,9 @@ public abstract class ShaderProgram { protected void loadVector(int location, Vector3f value) { GL20.glUniform3f(location, value.x, value.y, value.z); } + protected void loadVector(int location, Vector2f value) { + GL20.glUniform2f(location, value.x, value.y); + } protected void loadBoolean(int location, boolean value) { //System.out.println("set value " + value + " " + (value==true?1.0f:0.0f)); diff --git a/src/shaders/StaticShader.java b/src/shaders/StaticShader.java index 09a7c77..70985bd 100644 --- a/src/shaders/StaticShader.java +++ b/src/shaders/StaticShader.java @@ -1,6 +1,7 @@ package shaders; import org.atriaSoft.etk.math.Matrix4f; +import org.atriaSoft.etk.math.Vector2f; import org.atriaSoft.etk.math.Vector3f; import entities.Camera; @@ -20,6 +21,8 @@ public class StaticShader extends ShaderProgram { private int location_shineDamper; private int location_useFakeLighting; private int location_skyColor; + private int location_numberOfRows; + private int location_offset; public StaticShader() { super(VERTEX_FILE, FRAGMENT_FILE); @@ -43,6 +46,17 @@ public class StaticShader extends ShaderProgram { location_shineDamper = super.getUniformLocation("shineDamper"); location_useFakeLighting = super.getUniformLocation("useFakeLighting"); location_skyColor = super.getUniformLocation("skyColor"); + location_numberOfRows = super.getUniformLocation("numberOfRows"); + location_offset = super.getUniformLocation("offset"); + } + + + public void loadNumberOfRows(int numberOfRows) { + super.loadFloat(location_numberOfRows, numberOfRows); + } + + public void loadOffset(float x, float y) { + super.loadVector(location_offset, new Vector2f(x,y)); } public void loadSkyColour(Vector3f color) { diff --git a/src/shaders/vertexShader.txt b/src/shaders/vertexShader.txt index db5739e..b853fe1 100644 --- a/src/shaders/vertexShader.txt +++ b/src/shaders/vertexShader.txt @@ -18,6 +18,9 @@ uniform vec3 lightPosition; uniform float useFakeLighting; +uniform float numberOfRows; +uniform vec2 offset; + const float density = 0.007; const float gradient = 1.5; @@ -26,7 +29,7 @@ void main(void) { vec4 worldPosition = transformationMatrix * vec4(position, 1.0); vec4 positionRelativeToCam = viewMatrix * worldPosition; gl_Position = projectionMatrix * positionRelativeToCam; - pass_textureCoordinates = textureCoords; + pass_textureCoordinates = (textureCoords/numberOfRows) + offset; vec3 actualNormal = normal; if (useFakeLighting > 0.5) { diff --git a/src/textures/ModelTexture.java b/src/textures/ModelTexture.java index 6c0b80f..295ed9e 100644 --- a/src/textures/ModelTexture.java +++ b/src/textures/ModelTexture.java @@ -10,6 +10,7 @@ public class ModelTexture { private float shineDamper = 1; private boolean hasTransparency = false; private boolean useFakeLighting = false; + private int numberOfRows = 1; public ModelTexture(int id) { this.textureID = id; @@ -51,5 +52,13 @@ public class ModelTexture { this.useFakeLighting = useFakeLighting; } + public int getNumberOfRows() { + return numberOfRows; + } + + public void setNumberOfRows(int numberOfRows) { + this.numberOfRows = numberOfRows; + } + }