Projection Matrix
This commit is contained in:
@@ -7,9 +7,10 @@ out vec3 color;
|
|||||||
out vec2 passUvs;
|
out vec2 passUvs;
|
||||||
|
|
||||||
uniform mat4 transformationMatrix;
|
uniform mat4 transformationMatrix;
|
||||||
|
uniform mat4 projectionMatrix;
|
||||||
|
|
||||||
void main(void)
|
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;
|
passUvs = uvs;
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package EngineTest;
|
package EngineTest;
|
||||||
|
|
||||||
|
import Entity.Entity;
|
||||||
import RenderEngine.DisplayManager;
|
import RenderEngine.DisplayManager;
|
||||||
import RenderEngine.Loader;
|
import RenderEngine.Loader;
|
||||||
import Models.RawModel;
|
import Models.RawModel;
|
||||||
@@ -8,6 +9,7 @@ import Shaders.StaticShader;
|
|||||||
import Models.TexturedModel;
|
import Models.TexturedModel;
|
||||||
import org.lwjgl.opengl.Display;
|
import org.lwjgl.opengl.Display;
|
||||||
import Textures.ModelTexture;
|
import Textures.ModelTexture;
|
||||||
|
import org.lwjgl.util.vector.Vector3f;
|
||||||
|
|
||||||
public class MainGameLoop
|
public class MainGameLoop
|
||||||
{
|
{
|
||||||
@@ -21,8 +23,8 @@ public class MainGameLoop
|
|||||||
DisplayManager.createDisplay("Rendr");
|
DisplayManager.createDisplay("Rendr");
|
||||||
|
|
||||||
Loader loader = new Loader();
|
Loader loader = new Loader();
|
||||||
Renderer renderer = new Renderer();
|
|
||||||
StaticShader shader = new StaticShader();
|
StaticShader shader = new StaticShader();
|
||||||
|
Renderer renderer = new Renderer(shader);
|
||||||
|
|
||||||
float[] vertices =
|
float[] vertices =
|
||||||
{
|
{
|
||||||
@@ -47,12 +49,13 @@ public class MainGameLoop
|
|||||||
RawModel model = loader.loadToVAO(vertices, indices, uvs);
|
RawModel model = loader.loadToVAO(vertices, indices, uvs);
|
||||||
ModelTexture texture = new ModelTexture(loader.loadTexture("mossyBricks"));
|
ModelTexture texture = new ModelTexture(loader.loadTexture("mossyBricks"));
|
||||||
TexturedModel texturedModel = new TexturedModel(model, texture);
|
TexturedModel texturedModel = new TexturedModel(model, texture);
|
||||||
|
Entity entity = new Entity(texturedModel, new Vector3f(0, 0, -5), 0, 0, 0, 1);
|
||||||
|
|
||||||
while(!Display.isCloseRequested())
|
while(!Display.isCloseRequested())
|
||||||
{
|
{
|
||||||
renderer.prepare(0, 0, 0);
|
renderer.prepare(0, 0, 0);
|
||||||
shader.start();
|
shader.start();
|
||||||
renderer.render(texturedModel);
|
renderer.render(entity, shader);
|
||||||
shader.stop();
|
shader.stop();
|
||||||
DisplayManager.updateDisplay();
|
DisplayManager.updateDisplay();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,28 +1,47 @@
|
|||||||
package RenderEngine;
|
package RenderEngine;
|
||||||
|
|
||||||
|
import Entity.Entity;
|
||||||
import Models.RawModel;
|
import Models.RawModel;
|
||||||
import Models.TexturedModel;
|
import Models.TexturedModel;
|
||||||
import org.lwjgl.opengl.GL11;
|
import Shaders.StaticShader;
|
||||||
import org.lwjgl.opengl.GL13;
|
import Tools.Maths;
|
||||||
import org.lwjgl.opengl.GL20;
|
import org.lwjgl.opengl.*;
|
||||||
import org.lwjgl.opengl.GL30;
|
import org.lwjgl.util.vector.Matrix4f;
|
||||||
|
|
||||||
public class Renderer
|
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)
|
public void prepare(float r, float g, float b)
|
||||||
{
|
{
|
||||||
GL11.glClearColor(r, g, b, 1);
|
GL11.glClearColor(r, g, b, 1);
|
||||||
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
|
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();
|
RawModel model = texturedModel.getRawModel();
|
||||||
//bind the models vao
|
//bind the models vao
|
||||||
GL30.glBindVertexArray(model.getVaoID());
|
GL30.glBindVertexArray(model.getVaoID());
|
||||||
//vertex data is in list 0
|
//vertex data is in list 0
|
||||||
GL20.glEnableVertexAttribArray(0);
|
GL20.glEnableVertexAttribArray(0);
|
||||||
GL20.glEnableVertexAttribArray(1);
|
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);
|
GL13.glActiveTexture(GL13.GL_TEXTURE0);
|
||||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texturedModel.getTexture().getID());
|
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texturedModel.getTexture().getID());
|
||||||
@@ -34,4 +53,20 @@ public class Renderer
|
|||||||
GL20.glDisableVertexAttribArray(1);
|
GL20.glDisableVertexAttribArray(1);
|
||||||
GL30.glBindVertexArray(0);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ public class StaticShader extends ShaderProgram
|
|||||||
{
|
{
|
||||||
private static final String vertexFile = "src/shaders/vertexShader.glsl", fragmentFile = "src/shaders/fragmentShader.glsl";
|
private static final String vertexFile = "src/shaders/vertexShader.glsl", fragmentFile = "src/shaders/fragmentShader.glsl";
|
||||||
private int locationTransform;
|
private int locationTransform;
|
||||||
|
private int locationProjection;
|
||||||
|
|
||||||
public StaticShader()
|
public StaticShader()
|
||||||
{
|
{
|
||||||
@@ -16,6 +17,7 @@ public class StaticShader extends ShaderProgram
|
|||||||
protected void getAllUniformLocations()
|
protected void getAllUniformLocations()
|
||||||
{
|
{
|
||||||
locationTransform = getUniformLocation("transformationMatrix");
|
locationTransform = getUniformLocation("transformationMatrix");
|
||||||
|
locationProjection = getUniformLocation("projectionMatrix");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -29,4 +31,9 @@ public class StaticShader extends ShaderProgram
|
|||||||
{
|
{
|
||||||
loadMatrix(locationTransform, matrix);
|
loadMatrix(locationTransform, matrix);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void loadProjectionMatrix(Matrix4f matrix)
|
||||||
|
{
|
||||||
|
loadMatrix(locationProjection, matrix);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,9 +7,10 @@ out vec3 color;
|
|||||||
out vec2 passUvs;
|
out vec2 passUvs;
|
||||||
|
|
||||||
uniform mat4 transformationMatrix;
|
uniform mat4 transformationMatrix;
|
||||||
|
uniform mat4 projectionMatrix;
|
||||||
|
|
||||||
void main(void)
|
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;
|
passUvs = uvs;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user