View Matrix / Camera implemented
This commit is contained in:
@@ -8,9 +8,10 @@ out vec2 passUvs;
|
||||
|
||||
uniform mat4 transformationMatrix;
|
||||
uniform mat4 projectionMatrix;
|
||||
uniform mat4 viewMatrix;
|
||||
|
||||
void main(void)
|
||||
{
|
||||
gl_Position = projectionMatrix * transformationMatrix * vec4(position.x, position.y, position.z, 1.0);
|
||||
gl_Position = projectionMatrix * viewMatrix * transformationMatrix * vec4(position.x, position.y, position.z, 1.0);
|
||||
passUvs = uvs;
|
||||
}
|
||||
@@ -1,15 +1,17 @@
|
||||
package EngineTest;
|
||||
|
||||
import Entity.Entity;
|
||||
import Entity.*;
|
||||
import RenderEngine.DisplayManager;
|
||||
import RenderEngine.Loader;
|
||||
import Models.RawModel;
|
||||
import RenderEngine.Renderer;
|
||||
import Shaders.StaticShader;
|
||||
import Models.TexturedModel;
|
||||
import org.lwjgl.input.Keyboard;
|
||||
import org.lwjgl.opengl.Display;
|
||||
import Textures.ModelTexture;
|
||||
import org.lwjgl.util.vector.Vector3f;
|
||||
import sun.awt.AWTAccessor;
|
||||
|
||||
public class MainGameLoop
|
||||
{
|
||||
@@ -26,35 +28,115 @@ public class MainGameLoop
|
||||
StaticShader shader = new StaticShader();
|
||||
Renderer renderer = new Renderer(shader);
|
||||
|
||||
float[] vertices =
|
||||
{
|
||||
-.5f, .5f, 0,
|
||||
-.5f, -.5f, 0,
|
||||
.5f, -.5f, 0,
|
||||
.5f, .5f, 0
|
||||
};
|
||||
int[] indices =
|
||||
{
|
||||
0, 1, 3,
|
||||
3, 1, 2,
|
||||
};
|
||||
float[] uvs =
|
||||
{
|
||||
0, 0,
|
||||
0, 1,
|
||||
1, 1,
|
||||
1, 0
|
||||
};
|
||||
float[] vertices = {
|
||||
-0.5f,0.5f,-0.5f,
|
||||
-0.5f,-0.5f,-0.5f,
|
||||
0.5f,-0.5f,-0.5f,
|
||||
0.5f,0.5f,-0.5f,
|
||||
|
||||
-0.5f,0.5f,0.5f,
|
||||
-0.5f,-0.5f,0.5f,
|
||||
0.5f,-0.5f,0.5f,
|
||||
0.5f,0.5f,0.5f,
|
||||
|
||||
0.5f,0.5f,-0.5f,
|
||||
0.5f,-0.5f,-0.5f,
|
||||
0.5f,-0.5f,0.5f,
|
||||
0.5f,0.5f,0.5f,
|
||||
|
||||
-0.5f,0.5f,-0.5f,
|
||||
-0.5f,-0.5f,-0.5f,
|
||||
-0.5f,-0.5f,0.5f,
|
||||
-0.5f,0.5f,0.5f,
|
||||
|
||||
-0.5f,0.5f,0.5f,
|
||||
-0.5f,0.5f,-0.5f,
|
||||
0.5f,0.5f,-0.5f,
|
||||
0.5f,0.5f,0.5f,
|
||||
|
||||
-0.5f,-0.5f,0.5f,
|
||||
-0.5f,-0.5f,-0.5f,
|
||||
0.5f,-0.5f,-0.5f,
|
||||
0.5f,-0.5f,0.5f
|
||||
|
||||
};
|
||||
|
||||
float[] uvs = {
|
||||
|
||||
0,0,
|
||||
0,1,
|
||||
1,1,
|
||||
1,0,
|
||||
0,0,
|
||||
0,1,
|
||||
1,1,
|
||||
1,0,
|
||||
0,0,
|
||||
0,1,
|
||||
1,1,
|
||||
1,0,
|
||||
0,0,
|
||||
0,1,
|
||||
1,1,
|
||||
1,0,
|
||||
0,0,
|
||||
0,1,
|
||||
1,1,
|
||||
1,0,
|
||||
0,0,
|
||||
0,1,
|
||||
1,1,
|
||||
1,0
|
||||
|
||||
|
||||
};
|
||||
|
||||
int[] indices = {
|
||||
0,1,3,
|
||||
3,1,2,
|
||||
4,5,7,
|
||||
7,5,6,
|
||||
8,9,11,
|
||||
11,9,10,
|
||||
12,13,15,
|
||||
15,13,14,
|
||||
16,17,19,
|
||||
19,17,18,
|
||||
20,21,23,
|
||||
23,21,22
|
||||
|
||||
};
|
||||
|
||||
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);
|
||||
|
||||
Camera camera = new Camera();
|
||||
|
||||
while(!Display.isCloseRequested())
|
||||
{
|
||||
if(Keyboard.isKeyDown(Keyboard.KEY_W))
|
||||
{
|
||||
camera.translate(0, 0, -.02f);
|
||||
}
|
||||
if(Keyboard.isKeyDown(Keyboard.KEY_S))
|
||||
{
|
||||
camera.translate(0, 0, .02f);
|
||||
}
|
||||
if(Keyboard.isKeyDown(Keyboard.KEY_D))
|
||||
{
|
||||
camera.translate(.02f, 0, 0);
|
||||
}
|
||||
if(Keyboard.isKeyDown(Keyboard.KEY_A))
|
||||
{
|
||||
camera.translate(-.02f, 0, 0);
|
||||
}
|
||||
|
||||
entity.rotate(.5f, .5f, .5f);
|
||||
renderer.prepare(0, 0, 0);
|
||||
shader.start();
|
||||
shader.loadViewMatrix(camera);
|
||||
renderer.render(entity, shader);
|
||||
shader.stop();
|
||||
DisplayManager.updateDisplay();
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package Entity;
|
||||
|
||||
import org.lwjgl.util.vector.Vector3f;
|
||||
|
||||
public class Camera
|
||||
{
|
||||
private Vector3f position = new Vector3f(0, 0, 0);
|
||||
private float rotX, rotY, rotZ;
|
||||
|
||||
public Camera()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
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 Vector3f getPosition()
|
||||
{
|
||||
return position;
|
||||
}
|
||||
|
||||
public float getRotX()
|
||||
{
|
||||
return rotX;
|
||||
}
|
||||
|
||||
public float getRotY()
|
||||
{
|
||||
return rotY;
|
||||
}
|
||||
|
||||
public float getRotZ()
|
||||
{
|
||||
return rotZ;
|
||||
}
|
||||
}
|
||||
@@ -26,8 +26,10 @@ public class Renderer
|
||||
|
||||
public void prepare(float r, float g, float b)
|
||||
{
|
||||
GL11.glEnable(GL11.GL_DEPTH_TEST);
|
||||
GL11.glClearColor(r, g, b, 1);
|
||||
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
|
||||
GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT);
|
||||
}
|
||||
|
||||
public void render(Entity entity, StaticShader shader)
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package Shaders;
|
||||
|
||||
import Entity.Camera;
|
||||
import Tools.Maths;
|
||||
import org.lwjgl.util.vector.Matrix4f;
|
||||
|
||||
public class StaticShader extends ShaderProgram
|
||||
@@ -7,6 +9,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;
|
||||
private int locationView;
|
||||
|
||||
public StaticShader()
|
||||
{
|
||||
@@ -18,6 +21,7 @@ public class StaticShader extends ShaderProgram
|
||||
{
|
||||
locationTransform = getUniformLocation("transformationMatrix");
|
||||
locationProjection = getUniformLocation("projectionMatrix");
|
||||
locationView = getUniformLocation("viewMatrix");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -32,6 +36,12 @@ public class StaticShader extends ShaderProgram
|
||||
loadMatrix(locationTransform, matrix);
|
||||
}
|
||||
|
||||
public void loadViewMatrix(Camera camera)
|
||||
{
|
||||
Matrix4f matrix = Maths.createViewMatrix(camera);
|
||||
loadMatrix(locationView, matrix);
|
||||
}
|
||||
|
||||
public void loadProjectionMatrix(Matrix4f matrix)
|
||||
{
|
||||
loadMatrix(locationProjection, matrix);
|
||||
|
||||
@@ -8,9 +8,10 @@ out vec2 passUvs;
|
||||
|
||||
uniform mat4 transformationMatrix;
|
||||
uniform mat4 projectionMatrix;
|
||||
uniform mat4 viewMatrix;
|
||||
|
||||
void main(void)
|
||||
{
|
||||
gl_Position = projectionMatrix * transformationMatrix * vec4(position.x, position.y, position.z, 1.0);
|
||||
gl_Position = projectionMatrix * viewMatrix * transformationMatrix * vec4(position.x, position.y, position.z, 1.0);
|
||||
passUvs = uvs;
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package Tools;
|
||||
|
||||
import Entity.Camera;
|
||||
import org.lwjgl.util.vector.Matrix4f;
|
||||
import org.lwjgl.util.vector.Vector3f;
|
||||
|
||||
@@ -16,4 +17,16 @@ public class Maths
|
||||
Matrix4f.scale(new Vector3f(scale, scale, scale), matrix, matrix);
|
||||
return matrix;
|
||||
}
|
||||
|
||||
public static Matrix4f createViewMatrix(Camera camera)
|
||||
{
|
||||
Matrix4f viewMatrix = new Matrix4f();
|
||||
viewMatrix.setIdentity();
|
||||
Matrix4f.rotate((float) Math.toRadians(camera.getRotX()), new Vector3f(1, 0, 0), viewMatrix, viewMatrix);
|
||||
Matrix4f.rotate((float) Math.toRadians(camera.getRotY()), new Vector3f(0, 1, 0), viewMatrix, viewMatrix);
|
||||
Vector3f cameraPos = camera.getPosition();
|
||||
Vector3f negativeCameraPos = new Vector3f(-cameraPos.x, -cameraPos.y, -cameraPos.z);
|
||||
Matrix4f.translate(negativeCameraPos, viewMatrix, viewMatrix);
|
||||
return viewMatrix;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user