diff --git a/Rendr/out/production/Rendr/Shaders/vertexShader.glsl b/Rendr/out/production/Rendr/Shaders/vertexShader.glsl index 341d39f..c2b439f 100644 --- a/Rendr/out/production/Rendr/Shaders/vertexShader.glsl +++ b/Rendr/out/production/Rendr/Shaders/vertexShader.glsl @@ -7,9 +7,10 @@ out vec3 color; out vec2 passUvs; uniform mat4 transformationMatrix; +uniform mat4 projectionMatrix; void main(void) { - gl_Position = vec4(position.x, position.y, position.z, 1.0); + gl_Position = projectionMatrix * transformationMatrix * vec4(position.x, position.y, position.z, 1.0); passUvs = uvs; } \ No newline at end of file diff --git a/Rendr/src/EngineTest/MainGameLoop.java b/Rendr/src/EngineTest/MainGameLoop.java index d7f550b..a580085 100644 --- a/Rendr/src/EngineTest/MainGameLoop.java +++ b/Rendr/src/EngineTest/MainGameLoop.java @@ -1,5 +1,6 @@ package EngineTest; +import Entity.Entity; import RenderEngine.DisplayManager; import RenderEngine.Loader; import Models.RawModel; @@ -8,6 +9,7 @@ import Shaders.StaticShader; import Models.TexturedModel; import org.lwjgl.opengl.Display; import Textures.ModelTexture; +import org.lwjgl.util.vector.Vector3f; public class MainGameLoop { @@ -21,8 +23,8 @@ public class MainGameLoop DisplayManager.createDisplay("Rendr"); Loader loader = new Loader(); - Renderer renderer = new Renderer(); StaticShader shader = new StaticShader(); + Renderer renderer = new Renderer(shader); float[] vertices = { @@ -47,12 +49,13 @@ public class MainGameLoop RawModel model = loader.loadToVAO(vertices, indices, uvs); ModelTexture texture = new ModelTexture(loader.loadTexture("mossyBricks")); TexturedModel texturedModel = new TexturedModel(model, texture); + Entity entity = new Entity(texturedModel, new Vector3f(0, 0, -5), 0, 0, 0, 1); while(!Display.isCloseRequested()) { renderer.prepare(0, 0, 0); shader.start(); - renderer.render(texturedModel); + renderer.render(entity, shader); shader.stop(); DisplayManager.updateDisplay(); } diff --git a/Rendr/src/Entity/Entity.java b/Rendr/src/Entity/Entity.java new file mode 100644 index 0000000..84a0291 --- /dev/null +++ b/Rendr/src/Entity/Entity.java @@ -0,0 +1,103 @@ +package Entity; + +import Models.TexturedModel; +import org.lwjgl.util.vector.Vector3f; + +public class Entity +{ + private TexturedModel model; + private Vector3f position; + private float rotX, rotY, rotZ; + private float scale; + + public Entity(TexturedModel model, Vector3f position, float rotX, float rotY, float rotZ, float scale) + { + this.model = model; + this.position = position; + this.rotX = rotX; + this.rotY = rotY; + this.rotZ = rotZ; + this.scale = scale; + } + + public void translate(Vector3f value) + { + position.x += value.x; + position.y += value.y; + position.z += value.z; + } + + public void translate(float x, float y, float z) + { + position.x += x; + position.y += y; + position.z += z; + } + + public void rotate(float x, float y, float z) + { + rotX += x; + rotY += y; + rotZ += z; + } + + public TexturedModel getModel() + { + return model; + } + + public void setModel(TexturedModel model) + { + this.model = model; + } + + public Vector3f getPosition() + { + return position; + } + + public void setPosition(Vector3f position) + { + this.position = position; + } + + public float getRotX() + { + return rotX; + } + + public void setRotX(float rotX) + { + this.rotX = rotX; + } + + public float getRotY() + { + return rotY; + } + + public void setRotY(float rotY) + { + this.rotY = rotY; + } + + public float getRotZ() + { + return rotZ; + } + + public void setRotZ(float rotZ) + { + this.rotZ = rotZ; + } + + public float getScale() + { + return scale; + } + + public void setScale(float scale) + { + this.scale = scale; + } +} diff --git a/Rendr/src/RenderEngine/Renderer.java b/Rendr/src/RenderEngine/Renderer.java index d5d14da..8a00edd 100644 --- a/Rendr/src/RenderEngine/Renderer.java +++ b/Rendr/src/RenderEngine/Renderer.java @@ -1,28 +1,47 @@ package RenderEngine; +import Entity.Entity; import Models.RawModel; import Models.TexturedModel; -import org.lwjgl.opengl.GL11; -import org.lwjgl.opengl.GL13; -import org.lwjgl.opengl.GL20; -import org.lwjgl.opengl.GL30; +import Shaders.StaticShader; +import Tools.Maths; +import org.lwjgl.opengl.*; +import org.lwjgl.util.vector.Matrix4f; public class Renderer { + private static final float fov = 70; + private static final float nearPlane = .1f; + private static final float farPlane = 1000; + + private Matrix4f projectionMatrix; + + public Renderer(StaticShader shader) + { + createProjectionMatrix(); + shader.start(); + shader.loadProjectionMatrix(projectionMatrix); + shader.stop(); + } + public void prepare(float r, float g, float b) { GL11.glClearColor(r, g, b, 1); GL11.glClear(GL11.GL_COLOR_BUFFER_BIT); } - public void render(TexturedModel texturedModel) + public void render(Entity entity, StaticShader shader) { + TexturedModel texturedModel = entity.getModel(); RawModel model = texturedModel.getRawModel(); //bind the models vao GL30.glBindVertexArray(model.getVaoID()); //vertex data is in list 0 GL20.glEnableVertexAttribArray(0); GL20.glEnableVertexAttribArray(1); + Matrix4f transform = Maths.createTransformMatrix(entity.getPosition(), entity.getRotX(), entity.getRotY(), entity.getRotZ(), entity.getScale()); + + shader.loadTransformMatrix(transform); GL13.glActiveTexture(GL13.GL_TEXTURE0); GL11.glBindTexture(GL11.GL_TEXTURE_2D, texturedModel.getTexture().getID()); @@ -34,4 +53,20 @@ public class Renderer GL20.glDisableVertexAttribArray(1); GL30.glBindVertexArray(0); } + + private void createProjectionMatrix() + { + float aspectRatio = (float) Display.getWidth() / (float) Display.getHeight(); + float yScale = (float) ((1f / Math.tan(Math.toRadians(fov / 2f))) * aspectRatio); + float xScale = yScale / aspectRatio; + float frustumLength = farPlane - nearPlane; + + projectionMatrix = new Matrix4f(); + projectionMatrix.m00 = xScale; + projectionMatrix.m11 = yScale; + projectionMatrix.m22 = -((farPlane + nearPlane) / frustumLength); + projectionMatrix.m23 = -1; + projectionMatrix.m32 = -((2 * nearPlane * farPlane) / frustumLength); + projectionMatrix.m33 = 0; + } } diff --git a/Rendr/src/Shaders/StaticShader.java b/Rendr/src/Shaders/StaticShader.java index dec2e6b..f0eddb2 100644 --- a/Rendr/src/Shaders/StaticShader.java +++ b/Rendr/src/Shaders/StaticShader.java @@ -6,6 +6,7 @@ public class StaticShader extends ShaderProgram { private static final String vertexFile = "src/shaders/vertexShader.glsl", fragmentFile = "src/shaders/fragmentShader.glsl"; private int locationTransform; + private int locationProjection; public StaticShader() { @@ -16,6 +17,7 @@ public class StaticShader extends ShaderProgram protected void getAllUniformLocations() { locationTransform = getUniformLocation("transformationMatrix"); + locationProjection = getUniformLocation("projectionMatrix"); } @Override @@ -29,4 +31,9 @@ public class StaticShader extends ShaderProgram { loadMatrix(locationTransform, matrix); } + + public void loadProjectionMatrix(Matrix4f matrix) + { + loadMatrix(locationProjection, matrix); + } } diff --git a/Rendr/src/Shaders/vertexShader.glsl b/Rendr/src/Shaders/vertexShader.glsl index 27c0c1b..c2b439f 100644 --- a/Rendr/src/Shaders/vertexShader.glsl +++ b/Rendr/src/Shaders/vertexShader.glsl @@ -7,9 +7,10 @@ out vec3 color; out vec2 passUvs; uniform mat4 transformationMatrix; +uniform mat4 projectionMatrix; void main(void) { - gl_Position = vec4(position.x, position.y, position.z, 1.0) * transformationMatrix; + gl_Position = projectionMatrix * transformationMatrix * vec4(position.x, position.y, position.z, 1.0); passUvs = uvs; } \ No newline at end of file