Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package base;
- import static org.lwjgl.opengl.GL20.glUniformMatrix4;
- import static org.lwjgl.opengl.GL20.glUseProgram;
- import java.nio.FloatBuffer;
- import org.lwjgl.BufferUtils;
- import org.lwjgl.util.vector.Matrix4f;
- import org.lwjgl.util.vector.Vector3f;
- public class Matrices {
- public static Matrix4f perspectiveMatrix, viewMatrix, modelMatrix;
- public static int perspectiveMatrixLocation, viewMatrixLocation, modelMatrixLocation;
- public static void setupPerspective(float fieldOfView, float WIDTH, float HEIGHT, float zNear, float zFar){
- perspectiveMatrix = new Matrix4f();
- perspectiveMatrix.setIdentity();
- float aspectRatio = WIDTH / HEIGHT;
- float zRange = zNear - zFar;
- perspectiveMatrix.m00 = (float) (1.0f / (aspectRatio * Math.tan(degreesToRadians(fieldOfView / 2.0f))));
- perspectiveMatrix.m11 = (float) (1.0f / (Math.tan(degreesToRadians(fieldOfView / 2.0f))));
- perspectiveMatrix.m22 = (-zNear - zFar) / zRange;
- perspectiveMatrix.m23 = 1;
- perspectiveMatrix.m32 = (2 * zFar * zNear) / zRange;
- }
- public static void setupModel(){
- modelMatrix = new Matrix4f();
- modelMatrix.setIdentity();
- }
- public static void uploadMatrices(int shaderProgramId, int perspectiveMatrixLocation, int viewMatrixLocation, int modelMatrixLocation){
- FloatBuffer matrix44Buffer = BufferUtils.createFloatBuffer(16);
- glUseProgram(shaderProgramId);
- perspectiveMatrix.store(matrix44Buffer);
- matrix44Buffer.flip();
- glUniformMatrix4(perspectiveMatrixLocation, false, matrix44Buffer);
- viewMatrix.store(matrix44Buffer);
- matrix44Buffer.flip();
- glUniformMatrix4(viewMatrixLocation, false, matrix44Buffer);
- modelMatrix.store(matrix44Buffer);
- matrix44Buffer.flip();
- glUniformMatrix4(modelMatrixLocation, false, matrix44Buffer);
- glUseProgram(0);
- }
- public static float degreesToRadians(float angle){
- return angle * (float)(Math.PI / 180d);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment