m a t h
parent
a7af118ab6
commit
619e7200f1
|
@ -10,6 +10,7 @@ import org.apache.commons.cli.Options;
|
|||
import org.apache.commons.cli.ParseException;
|
||||
import org.apache.log4j.Level;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.joml.Vector3f;
|
||||
import org.lwjgl.Version;
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
import org.lwjgl.glfw.GLFWErrorCallback;
|
||||
|
@ -24,7 +25,6 @@ import com.github.halotroop.litecraft.render.RenderWrapper;
|
|||
|
||||
import io.github.hydos.ginger.engine.elements.objects.RenderPlayer;
|
||||
import io.github.hydos.ginger.engine.io.Window;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
import io.github.hydos.ginger.engine.obj.ModelLoader;
|
||||
import io.github.hydos.ginger.engine.render.models.TexturedModel;
|
||||
|
||||
|
@ -35,6 +35,7 @@ public class LiteCraftMain implements Runnable
|
|||
public static int width = 640, height = 480, maxFPS = 60; // Don't change these values. They just initialize it in case we forget to set them later.
|
||||
public static boolean spamLog = false, debug = false, limitFPS = false;
|
||||
public static String splashText = "";
|
||||
public static boolean fullscreen = false;
|
||||
private int fps, ups, tps;
|
||||
private long frameTimer;
|
||||
protected Timer timer;
|
||||
|
|
|
@ -3,6 +3,9 @@ package com.github.halotroop.litecraft.render;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.joml.Vector3f;
|
||||
import org.joml.Vector4f;
|
||||
|
||||
import com.github.halotroop.litecraft.LiteCraftMain;
|
||||
|
||||
import io.github.hydos.ginger.engine.cameras.ThirdPersonCamera;
|
||||
|
@ -12,8 +15,6 @@ import io.github.hydos.ginger.engine.elements.objects.Light;
|
|||
import io.github.hydos.ginger.engine.elements.objects.RenderPlayer;
|
||||
import io.github.hydos.ginger.engine.font.TextMaster;
|
||||
import io.github.hydos.ginger.engine.io.Window;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector4f;
|
||||
import io.github.hydos.ginger.engine.particle.ParticleMaster;
|
||||
import io.github.hydos.ginger.engine.postprocessing.PostProcessing;
|
||||
import io.github.hydos.ginger.engine.render.MasterRenderer;
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package io.github.hydos.ginger.engine.cameras;
|
||||
|
||||
import org.joml.Vector3f;
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
|
||||
import io.github.hydos.ginger.engine.io.Window;
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
package io.github.hydos.ginger.engine.cameras;
|
||||
|
||||
import org.joml.Vector3f;
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
import org.lwjgl.glfw.GLFWScrollCallback;
|
||||
|
||||
import io.github.hydos.ginger.engine.elements.objects.RenderPlayer;
|
||||
import io.github.hydos.ginger.engine.io.Window;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
|
||||
public class ThirdPersonCamera
|
||||
{
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package io.github.hydos.ginger.engine.elements;
|
||||
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector2f;
|
||||
import org.joml.Vector2f;
|
||||
|
||||
public class GuiTexture
|
||||
{
|
||||
|
|
|
@ -2,11 +2,11 @@ package io.github.hydos.ginger.engine.elements.buttons;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
import org.joml.Vector2f;
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
|
||||
import io.github.hydos.ginger.engine.elements.GuiTexture;
|
||||
import io.github.hydos.ginger.engine.io.Window;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector2f;
|
||||
import io.github.hydos.ginger.engine.utils.Loader;
|
||||
|
||||
public class TextureButton
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package io.github.hydos.ginger.engine.elements.objects;
|
||||
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
import io.github.hydos.ginger.engine.render.models.TexturedModel;
|
||||
|
||||
public class Entity
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package io.github.hydos.ginger.engine.elements.objects;
|
||||
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class Light
|
||||
{
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
package io.github.hydos.ginger.engine.elements.objects;
|
||||
|
||||
import org.joml.Vector3f;
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
|
||||
import io.github.hydos.ginger.engine.io.Window;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
import io.github.hydos.ginger.engine.render.models.TexturedModel;
|
||||
import io.github.hydos.ginger.engine.terrain.Terrain;
|
||||
import io.github.hydos.ginger.main.settings.Constants;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package io.github.hydos.ginger.engine.font;
|
||||
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector2f;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
/** Represents a piece of text in the game. */
|
||||
public class GUIText
|
||||
|
|
|
@ -6,7 +6,7 @@ import java.io.InputStreamReader;
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import io.github.hydos.ginger.engine.io.Window;
|
||||
import com.github.halotroop.litecraft.LiteCraftMain;
|
||||
|
||||
/** Provides functionality for getting the values from a font file. */
|
||||
public class MetaFile
|
||||
|
@ -35,7 +35,7 @@ public class MetaFile
|
|||
* - the font file. */
|
||||
protected MetaFile(String file)
|
||||
{
|
||||
this.aspectRatio = (double) Window.width / (double) Window.height;
|
||||
this.aspectRatio = (double) LiteCraftMain.width / (double) LiteCraftMain.height;
|
||||
openFile(file);
|
||||
loadPaddingData();
|
||||
loadLineSizes();
|
||||
|
|
|
@ -3,6 +3,8 @@ package io.github.hydos.ginger.engine.io;
|
|||
import java.nio.DoubleBuffer;
|
||||
import java.nio.IntBuffer;
|
||||
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector3f;
|
||||
import org.lwjgl.BufferUtils;
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
import org.lwjgl.glfw.GLFWImage;
|
||||
|
@ -11,22 +13,19 @@ import org.lwjgl.opengl.GL;
|
|||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.GLCapabilities;
|
||||
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector2f;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
import com.github.halotroop.litecraft.LiteCraftMain;
|
||||
|
||||
import io.github.hydos.ginger.engine.render.texture.Image;
|
||||
|
||||
public class Window
|
||||
{
|
||||
public static int width;
|
||||
public static int height;
|
||||
private static String title;
|
||||
public static long window;
|
||||
private static Vector3f backgroundColour = new Vector3f(0.2f, 0.2f, 0.2f);
|
||||
private static Vector3f backgroundColour = new Vector3f(0.2f, 0.2f, 0.6f);
|
||||
private static boolean[] keys = new boolean[GLFW.GLFW_KEY_LAST];
|
||||
private static boolean[] mouseButtons = new boolean[GLFW.GLFW_MOUSE_BUTTON_LAST];
|
||||
private static GLFWImage.Buffer iconBuffer = null;
|
||||
private static double fpsCap, time, processedTime = 0;
|
||||
private static boolean isFullscreen = false;
|
||||
public static double dy = 0;
|
||||
public static double dx = 0;
|
||||
static double oldX = 0;
|
||||
|
@ -38,8 +37,6 @@ public class Window
|
|||
|
||||
public static void create(int width, int height, String title, int fps)
|
||||
{
|
||||
Window.width = width / 2;
|
||||
Window.height = height / 2;
|
||||
Window.actuallHeight = height;
|
||||
Window.actuallWidth = width;
|
||||
Window.title = title;
|
||||
|
@ -59,9 +56,9 @@ public class Window
|
|||
GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_PROFILE, GLFW.GLFW_OPENGL_CORE_PROFILE);
|
||||
GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_FORWARD_COMPAT, GL11.GL_TRUE);
|
||||
GLFW.glfwWindowHint(GLFW.GLFW_RESIZABLE, GLFW.GLFW_FALSE);
|
||||
GLFW.glfwWindowHint(GLFW.GLFW_RESIZABLE, GLFW.GLFW_TRUE);
|
||||
GLFWVidMode vidmode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
|
||||
window = GLFW.glfwCreateWindow(actuallWidth, actuallHeight, title, (isFullscreen) ? GLFW.glfwGetPrimaryMonitor() : 0, window);
|
||||
window = GLFW.glfwCreateWindow(actuallWidth, actuallHeight, title, (LiteCraftMain.fullscreen) ? GLFW.glfwGetPrimaryMonitor() : 0, window);
|
||||
if (window == 0)
|
||||
{
|
||||
System.err.println("Error: Couldnt initilize window");
|
||||
|
@ -70,7 +67,7 @@ public class Window
|
|||
GLFW.glfwMakeContextCurrent(window);
|
||||
glContext = GL.createCapabilities();
|
||||
GL11.glEnable(GL11.GL_DEPTH_TEST);
|
||||
GLFW.glfwSetWindowPos(window, (vidmode.width() - width) / 2, (vidmode.height() - height) / 2);
|
||||
GLFW.glfwSetWindowPos(window, (vidmode.width() - LiteCraftMain.width) / 2, (vidmode.height() - LiteCraftMain.height) / 2);
|
||||
GLFW.glfwShowWindow(window);
|
||||
time = getTime();
|
||||
getCurrentTime();
|
||||
|
@ -84,9 +81,7 @@ public class Window
|
|||
IntBuffer widthBuffer = BufferUtils.createIntBuffer(1);
|
||||
IntBuffer heightBuffer = BufferUtils.createIntBuffer(1);
|
||||
GLFW.glfwGetWindowSize(window, widthBuffer, heightBuffer);
|
||||
width = widthBuffer.get(0);
|
||||
height = heightBuffer.get(0);
|
||||
GL11.glViewport(0, 0, width, height);
|
||||
GL11.glViewport(0, 0, LiteCraftMain.width, LiteCraftMain.height);
|
||||
GL11.glClearColor(backgroundColour.x, backgroundColour.y, backgroundColour.z, 1.0f);
|
||||
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
|
||||
GLFW.glfwPollEvents();
|
||||
|
@ -180,9 +175,6 @@ public class Window
|
|||
{ GLFW.glfwSetWindowIcon(window, iconBuffer); }
|
||||
}
|
||||
|
||||
public void setFullscreen(boolean t)
|
||||
{ Window.isFullscreen = t; }
|
||||
|
||||
public static void lockMouse()
|
||||
{ GLFW.glfwSetInputMode(window, GLFW.GLFW_CURSOR, GLFW.GLFW_CURSOR_DISABLED); }
|
||||
|
||||
|
@ -197,8 +189,8 @@ public class Window
|
|||
|
||||
public static Vector2f getNormalizedMouseCoordinates()
|
||||
{
|
||||
float normalX = -1.0f + 2.0f * (float) getMouseX() / width;
|
||||
float normalY = 1.0f - 2.0f * (float) getMouseY() / height;
|
||||
float normalX = -1.0f + 2.0f * (float) getMouseX() / LiteCraftMain.width;
|
||||
float normalY = 1.0f - 2.0f * (float) getMouseY() / LiteCraftMain.height;
|
||||
return new Vector2f(normalX, normalY);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,34 +1,36 @@
|
|||
package io.github.hydos.ginger.engine.math;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector3f;
|
||||
import org.joml.Vector4f;
|
||||
|
||||
import io.github.hydos.ginger.engine.cameras.ThirdPersonCamera;
|
||||
import io.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector2f;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
|
||||
public class Maths
|
||||
{
|
||||
public static Matrix4f createTransformationMatrix(Vector3f translation, float rx, float ry, float rz, Vector3f scale)
|
||||
{
|
||||
Matrix4f matrix = new Matrix4f();
|
||||
matrix.setIdentity();
|
||||
Matrix4f.translate(translation, matrix, matrix);
|
||||
Matrix4f.rotate((float) Math.toRadians(rx), new Vector3f(1, 0, 0), matrix, matrix);
|
||||
Matrix4f.rotate((float) Math.toRadians(ry), new Vector3f(0, 1, 0), matrix, matrix);
|
||||
Matrix4f.rotate((float) Math.toRadians(rz), new Vector3f(0, 0, 1), matrix, matrix);
|
||||
Matrix4f.scale(scale, matrix, matrix);
|
||||
matrix.identity();
|
||||
matrix.translate(translation, matrix);
|
||||
matrix.rotate((float) Math.toRadians(rx), new Vector3f(1, 0, 0), matrix);
|
||||
matrix.rotate((float) Math.toRadians(ry), new Vector3f(0, 1, 0), matrix);
|
||||
matrix.rotate((float) Math.toRadians(rz), new Vector3f(0, 0, 1), matrix);
|
||||
matrix.scale(scale, matrix);
|
||||
return matrix;
|
||||
}
|
||||
|
||||
public static Matrix4f createViewMatrix(ThirdPersonCamera camera)
|
||||
{
|
||||
Matrix4f viewMatrix = new Matrix4f();
|
||||
viewMatrix.setIdentity();
|
||||
Matrix4f.rotate((float) Math.toRadians(camera.getPitch()), new Vector3f(1, 0, 0), viewMatrix, viewMatrix);
|
||||
Matrix4f.rotate((float) Math.toRadians(camera.getYaw()), new Vector3f(0, 1, 0), viewMatrix, viewMatrix);
|
||||
Matrix4f.rotate((float) Math.toRadians(camera.getRoll()), new Vector3f(0, 0, 1), viewMatrix, viewMatrix);
|
||||
viewMatrix.identity();
|
||||
viewMatrix.rotate((float) Math.toRadians(camera.getPitch()), new Vector3f(1, 0, 0), viewMatrix);
|
||||
viewMatrix.rotate((float) Math.toRadians(camera.getYaw()), new Vector3f(0, 1, 0), viewMatrix);
|
||||
viewMatrix.rotate((float) Math.toRadians(camera.getRoll()), new Vector3f(0, 0, 1), viewMatrix);
|
||||
Vector3f cameraPos = camera.getPosition();
|
||||
Vector3f negativeCameraPos = new Vector3f(-cameraPos.x, -cameraPos.y, -cameraPos.z);
|
||||
Matrix4f.translate(negativeCameraPos, viewMatrix, viewMatrix);
|
||||
viewMatrix.translate(negativeCameraPos, viewMatrix);
|
||||
return viewMatrix;
|
||||
}
|
||||
|
||||
|
@ -44,22 +46,37 @@ public class Maths
|
|||
public static Matrix4f createTransformationMatrix(Vector2f translation, Vector2f scale)
|
||||
{
|
||||
Matrix4f matrix = new Matrix4f();
|
||||
matrix.setIdentity();
|
||||
Matrix4f.translate(translation, matrix, matrix);
|
||||
Matrix4f.scale(new Vector3f(scale.x, scale.y, 1f), matrix, matrix);
|
||||
matrix.identity();
|
||||
matrix.translate(new Vector3f(translation.x, translation.y, 1), matrix);
|
||||
matrix.scale(new Vector3f(scale.x, scale.y, 1f), matrix);
|
||||
return matrix;
|
||||
}
|
||||
|
||||
public static Matrix4f createTransformationMatrix(Vector3f translation, float rx, float ry, float rz, float scale)
|
||||
{
|
||||
Matrix4f matrix = new Matrix4f();
|
||||
matrix.setIdentity();
|
||||
Matrix4f.translate(translation, matrix, matrix);
|
||||
Matrix4f.rotate((float) Math.toRadians(rx), new Vector3f(1, 0, 0), matrix, matrix);
|
||||
Matrix4f.rotate((float) Math.toRadians(ry), new Vector3f(0, 1, 0), matrix, matrix);
|
||||
Matrix4f.rotate((float) Math.toRadians(rz), new Vector3f(0, 0, 1), matrix, matrix);
|
||||
matrix.identity();
|
||||
matrix.translate(translation, matrix);
|
||||
matrix.rotate((float) Math.toRadians(rx), new Vector3f(1, 0, 0), matrix);
|
||||
matrix.rotate((float) Math.toRadians(ry), new Vector3f(0, 1, 0), matrix);
|
||||
matrix.rotate((float) Math.toRadians(rz), new Vector3f(0, 0, 1), matrix);
|
||||
Vector3f newScale = new Vector3f(scale, scale, scale);
|
||||
Matrix4f.scale(newScale, matrix, matrix);
|
||||
matrix.scale(newScale, matrix);
|
||||
return matrix;
|
||||
}
|
||||
|
||||
public static Vector3f Vec4ToVec3(Vector4f eyeSpacePos) {
|
||||
Vector3f vec3 = new Vector3f();
|
||||
vec3.x = eyeSpacePos.x;
|
||||
vec3.y = eyeSpacePos.y;
|
||||
vec3.z = eyeSpacePos.z;
|
||||
|
||||
return vec3;
|
||||
}
|
||||
|
||||
public static void scale(Vector3f change, float time) {
|
||||
change.x = change.x*time;
|
||||
change.y = change.y*time;
|
||||
change.x = change.x*time;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,488 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2002-2008 LWJGL Project
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of 'LWJGL' nor the names of
|
||||
* its contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package io.github.hydos.ginger.engine.math;
|
||||
|
||||
/**
|
||||
*
|
||||
* Quaternions for LWJGL!
|
||||
*
|
||||
* @author fbi
|
||||
* @version $Revision$
|
||||
* $Id$
|
||||
*/
|
||||
import java.nio.FloatBuffer;
|
||||
|
||||
import io.github.hydos.ginger.engine.math.matrixes.Matrix3f;
|
||||
import io.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import io.github.hydos.ginger.engine.math.vectors.ReadableVector4f;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector4f;
|
||||
|
||||
public class Quaternion extends Vector implements ReadableVector4f
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
public float x, y, z, w;
|
||||
|
||||
/** C'tor. The quaternion will be initialized to the identity. */
|
||||
public Quaternion()
|
||||
{
|
||||
super();
|
||||
setIdentity();
|
||||
}
|
||||
|
||||
/** C'tor
|
||||
*
|
||||
* @param src */
|
||||
public Quaternion(ReadableVector4f src)
|
||||
{ set(src); }
|
||||
|
||||
/** C'tor */
|
||||
public Quaternion(float x, float y, float z, float w)
|
||||
{ set(x, y, z, w); }
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.lwjgl.util.vector.WritableVector2f#set(float, float)
|
||||
*/
|
||||
public void set(float x, float y)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.lwjgl.util.vector.WritableVector3f#set(float, float, float)
|
||||
*/
|
||||
public void set(float x, float y, float z)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.lwjgl.util.vector.WritableVector4f#set(float, float, float,
|
||||
* float)
|
||||
*/
|
||||
public void set(float x, float y, float z, float w)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
this.w = w;
|
||||
}
|
||||
|
||||
/** Load from another Vector4f
|
||||
*
|
||||
* @param src
|
||||
* The source vector
|
||||
* @return this */
|
||||
public Quaternion set(ReadableVector4f src)
|
||||
{
|
||||
x = src.getX();
|
||||
y = src.getY();
|
||||
z = src.getZ();
|
||||
w = src.getW();
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Set this quaternion to the multiplication identity.
|
||||
*
|
||||
* @return this */
|
||||
public Quaternion setIdentity()
|
||||
{ return setIdentity(this); }
|
||||
|
||||
/** Set the given quaternion to the multiplication identity.
|
||||
*
|
||||
* @param q The quaternion
|
||||
* @return q */
|
||||
public static Quaternion setIdentity(Quaternion q)
|
||||
{
|
||||
q.x = 0;
|
||||
q.y = 0;
|
||||
q.z = 0;
|
||||
q.w = 1;
|
||||
return q;
|
||||
}
|
||||
|
||||
/** @return the length squared of the quaternion */
|
||||
@Override
|
||||
public float lengthSquared()
|
||||
{ return x * x + y * y + z * z + w * w; }
|
||||
|
||||
/** Normalise the source quaternion and place the result in another quaternion.
|
||||
*
|
||||
* @param src
|
||||
* The source quaternion
|
||||
* @param dest
|
||||
* The destination quaternion, or null if a new quaternion is to be
|
||||
* created
|
||||
* @return The normalised quaternion */
|
||||
public static Quaternion normalise(Quaternion src, Quaternion dest)
|
||||
{
|
||||
float inv_l = 1f / src.length();
|
||||
if (dest == null)
|
||||
dest = new Quaternion();
|
||||
dest.set(src.x * inv_l, src.y * inv_l, src.z * inv_l, src.w * inv_l);
|
||||
return dest;
|
||||
}
|
||||
|
||||
/** Normalise this quaternion and place the result in another quaternion.
|
||||
*
|
||||
* @param dest
|
||||
* The destination quaternion, or null if a new quaternion is to be
|
||||
* created
|
||||
* @return the normalised quaternion */
|
||||
public Quaternion normalise(Quaternion dest)
|
||||
{ return normalise(this, dest); }
|
||||
|
||||
/** The dot product of two quaternions
|
||||
*
|
||||
* @param left
|
||||
* The LHS quat
|
||||
* @param right
|
||||
* The RHS quat
|
||||
* @return left dot right */
|
||||
public static float dot(Quaternion left, Quaternion right)
|
||||
{ return left.x * right.x + left.y * right.y + left.z * right.z + left.w
|
||||
* right.w; }
|
||||
|
||||
/** Calculate the conjugate of this quaternion and put it into the given one
|
||||
*
|
||||
* @param dest
|
||||
* The quaternion which should be set to the conjugate of this
|
||||
* quaternion */
|
||||
public Quaternion negate(Quaternion dest)
|
||||
{ return negate(this, dest); }
|
||||
|
||||
/** Calculate the conjugate of this quaternion and put it into the given one
|
||||
*
|
||||
* @param src
|
||||
* The source quaternion
|
||||
* @param dest
|
||||
* The quaternion which should be set to the conjugate of this
|
||||
* quaternion */
|
||||
public static Quaternion negate(Quaternion src, Quaternion dest)
|
||||
{
|
||||
if (dest == null)
|
||||
dest = new Quaternion();
|
||||
dest.x = -src.x;
|
||||
dest.y = -src.y;
|
||||
dest.z = -src.z;
|
||||
dest.w = src.w;
|
||||
return dest;
|
||||
}
|
||||
|
||||
/** Calculate the conjugate of this quaternion */
|
||||
@Override
|
||||
public Vector negate()
|
||||
{ return negate(this, this); }
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.lwjgl.util.vector.Vector#load(java.nio.FloatBuffer)
|
||||
*/
|
||||
@Override
|
||||
public Vector load(FloatBuffer buf)
|
||||
{
|
||||
x = buf.get();
|
||||
y = buf.get();
|
||||
z = buf.get();
|
||||
w = buf.get();
|
||||
return this;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.lwjgl.vector.Vector#scale(float)
|
||||
*/
|
||||
@Override
|
||||
public Vector scale(float scale)
|
||||
{ return scale(scale, this, this); }
|
||||
|
||||
/** Scale the source quaternion by scale and put the result in the destination
|
||||
*
|
||||
* @param scale The amount to scale by
|
||||
* @param src The source quaternion
|
||||
* @param dest The destination quaternion, or null if a new quaternion is to be created
|
||||
* @return The scaled quaternion */
|
||||
public static Quaternion scale(float scale, Quaternion src, Quaternion dest)
|
||||
{
|
||||
if (dest == null)
|
||||
dest = new Quaternion();
|
||||
dest.x = src.x * scale;
|
||||
dest.y = src.y * scale;
|
||||
dest.z = src.z * scale;
|
||||
dest.w = src.w * scale;
|
||||
return dest;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.lwjgl.util.vector.ReadableVector#store(java.nio.FloatBuffer)
|
||||
*/
|
||||
@Override
|
||||
public Vector store(FloatBuffer buf)
|
||||
{
|
||||
buf.put(x);
|
||||
buf.put(y);
|
||||
buf.put(z);
|
||||
buf.put(w);
|
||||
return this;
|
||||
}
|
||||
|
||||
/** @return x */
|
||||
@Override
|
||||
public final float getX()
|
||||
{ return x; }
|
||||
|
||||
/** @return y */
|
||||
@Override
|
||||
public final float getY()
|
||||
{ return y; }
|
||||
|
||||
/** Set X
|
||||
*
|
||||
* @param x */
|
||||
public final void setX(float x)
|
||||
{ this.x = x; }
|
||||
|
||||
/** Set Y
|
||||
*
|
||||
* @param y */
|
||||
public final void setY(float y)
|
||||
{ this.y = y; }
|
||||
|
||||
/** Set Z
|
||||
*
|
||||
* @param z */
|
||||
public void setZ(float z)
|
||||
{ this.z = z; }
|
||||
|
||||
/*
|
||||
* (Overrides)
|
||||
*
|
||||
* @see org.lwjgl.vector.ReadableVector3f#getZ()
|
||||
*/
|
||||
@Override
|
||||
public float getZ()
|
||||
{ return z; }
|
||||
|
||||
/** Set W
|
||||
*
|
||||
* @param w */
|
||||
public void setW(float w)
|
||||
{ this.w = w; }
|
||||
|
||||
/*
|
||||
* (Overrides)
|
||||
*
|
||||
* @see org.lwjgl.vector.ReadableVector3f#getW()
|
||||
*/
|
||||
@Override
|
||||
public float getW()
|
||||
{ return w; }
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{ return "Quaternion: " + x + " " + y + " " + z + " " + w; }
|
||||
|
||||
/** Sets the value of this quaternion to the quaternion product of
|
||||
* quaternions left and right (this = left * right). Note that this is safe
|
||||
* for aliasing (e.g. this can be left or right).
|
||||
*
|
||||
* @param left
|
||||
* the first quaternion
|
||||
* @param right
|
||||
* the second quaternion */
|
||||
public static Quaternion mul(Quaternion left, Quaternion right,
|
||||
Quaternion dest)
|
||||
{
|
||||
if (dest == null)
|
||||
dest = new Quaternion();
|
||||
dest.set(left.x * right.w + left.w * right.x + left.y * right.z
|
||||
- left.z * right.y,
|
||||
left.y * right.w + left.w * right.y
|
||||
+ left.z * right.x - left.x * right.z,
|
||||
left.z * right.w
|
||||
+ left.w * right.z + left.x * right.y - left.y * right.x,
|
||||
left.w * right.w - left.x * right.x - left.y * right.y
|
||||
- left.z * right.z);
|
||||
return dest;
|
||||
}
|
||||
|
||||
/** Multiplies quaternion left by the inverse of quaternion right and places
|
||||
* the value into this quaternion. The value of both argument quaternions is
|
||||
* preservered (this = left * right^-1).
|
||||
*
|
||||
* @param left
|
||||
* the left quaternion
|
||||
* @param right
|
||||
* the right quaternion */
|
||||
public static Quaternion mulInverse(Quaternion left, Quaternion right,
|
||||
Quaternion dest)
|
||||
{
|
||||
float n = right.lengthSquared();
|
||||
// zero-div may occur.
|
||||
n = (n == 0.0 ? n : 1 / n);
|
||||
// store on stack once for aliasing-safty
|
||||
if (dest == null)
|
||||
dest = new Quaternion();
|
||||
dest
|
||||
.set((left.x * right.w - left.w * right.x - left.y
|
||||
* right.z + left.z * right.y)
|
||||
* n,
|
||||
(left.y * right.w - left.w * right.y - left.z
|
||||
* right.x + left.x * right.z)
|
||||
* n,
|
||||
(left.z * right.w - left.w * right.z - left.x
|
||||
* right.y + left.y * right.x)
|
||||
* n,
|
||||
(left.w * right.w + left.x * right.x + left.y
|
||||
* right.y + left.z * right.z)
|
||||
* n);
|
||||
return dest;
|
||||
}
|
||||
|
||||
/** Sets the value of this quaternion to the equivalent rotation of the
|
||||
* Axis-Angle argument.
|
||||
*
|
||||
* @param a1
|
||||
* the axis-angle: (x,y,z) is the axis and w is the angle */
|
||||
public final void setFromAxisAngle(Vector4f a1)
|
||||
{
|
||||
x = a1.x;
|
||||
y = a1.y;
|
||||
z = a1.z;
|
||||
float n = (float) Math.sqrt(x * x + y * y + z * z);
|
||||
// zero-div may occur.
|
||||
float s = (float) (Math.sin(0.5 * a1.w) / n);
|
||||
x *= s;
|
||||
y *= s;
|
||||
z *= s;
|
||||
w = (float) Math.cos(0.5 * a1.w);
|
||||
}
|
||||
|
||||
/** Sets the value of this quaternion using the rotational component of the
|
||||
* passed matrix.
|
||||
*
|
||||
* @param m
|
||||
* The matrix
|
||||
* @return this */
|
||||
public final Quaternion setFromMatrix(Matrix4f m)
|
||||
{ return setFromMatrix(m, this); }
|
||||
|
||||
/** Sets the value of the source quaternion using the rotational component of the
|
||||
* passed matrix.
|
||||
*
|
||||
* @param m
|
||||
* The source matrix
|
||||
* @param q
|
||||
* The destination quaternion, or null if a new quaternion is to be created
|
||||
* @return q */
|
||||
public static Quaternion setFromMatrix(Matrix4f m, Quaternion q)
|
||||
{ return q.setFromMat(m.m00, m.m01, m.m02, m.m10, m.m11, m.m12, m.m20,
|
||||
m.m21, m.m22); }
|
||||
|
||||
/** Sets the value of this quaternion using the rotational component of the
|
||||
* passed matrix.
|
||||
*
|
||||
* @param m
|
||||
* The source matrix */
|
||||
public final Quaternion setFromMatrix(Matrix3f m)
|
||||
{ return setFromMatrix(m, this); }
|
||||
|
||||
/** Sets the value of the source quaternion using the rotational component of the
|
||||
* passed matrix.
|
||||
*
|
||||
* @param m
|
||||
* The source matrix
|
||||
* @param q
|
||||
* The destination quaternion, or null if a new quaternion is to be created
|
||||
* @return q */
|
||||
public static Quaternion setFromMatrix(Matrix3f m, Quaternion q)
|
||||
{ return q.setFromMat(m.m00, m.m01, m.m02, m.m10, m.m11, m.m12, m.m20,
|
||||
m.m21, m.m22); }
|
||||
|
||||
/** Private method to perform the matrix-to-quaternion conversion */
|
||||
private Quaternion setFromMat(float m00, float m01, float m02, float m10,
|
||||
float m11, float m12, float m20, float m21, float m22)
|
||||
{
|
||||
float s;
|
||||
float tr = m00 + m11 + m22;
|
||||
if (tr >= 0.0)
|
||||
{
|
||||
s = (float) Math.sqrt(tr + 1.0);
|
||||
w = s * 0.5f;
|
||||
s = 0.5f / s;
|
||||
x = (m21 - m12) * s;
|
||||
y = (m02 - m20) * s;
|
||||
z = (m10 - m01) * s;
|
||||
}
|
||||
else
|
||||
{
|
||||
float max = Math.max(Math.max(m00, m11), m22);
|
||||
if (max == m00)
|
||||
{
|
||||
s = (float) Math.sqrt(m00 - (m11 + m22) + 1.0);
|
||||
x = s * 0.5f;
|
||||
s = 0.5f / s;
|
||||
y = (m01 + m10) * s;
|
||||
z = (m20 + m02) * s;
|
||||
w = (m21 - m12) * s;
|
||||
}
|
||||
else if (max == m11)
|
||||
{
|
||||
s = (float) Math.sqrt(m11 - (m22 + m00) + 1.0);
|
||||
y = s * 0.5f;
|
||||
s = 0.5f / s;
|
||||
z = (m12 + m21) * s;
|
||||
x = (m01 + m10) * s;
|
||||
w = (m02 - m20) * s;
|
||||
}
|
||||
else
|
||||
{
|
||||
s = (float) Math.sqrt(m22 - (m00 + m11) + 1.0);
|
||||
z = s * 0.5f;
|
||||
s = 0.5f / s;
|
||||
x = (m20 + m02) * s;
|
||||
y = (m12 + m21) * s;
|
||||
w = (m10 - m01) * s;
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -1,106 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2002-2008 LWJGL Project
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of 'LWJGL' nor the names of
|
||||
* its contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package io.github.hydos.ginger.engine.math.matrixes;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.nio.FloatBuffer;
|
||||
|
||||
/** Base class for matrices. When a matrix is constructed it will be the identity
|
||||
* matrix unless otherwise stated.
|
||||
*
|
||||
* @author cix_foo <cix_foo@users.sourceforge.net>
|
||||
* @version $Revision$
|
||||
* $Id$ */
|
||||
public abstract class Matrix implements Serializable
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/** Constructor for Matrix. */
|
||||
protected Matrix()
|
||||
{ super(); }
|
||||
|
||||
/** Set this matrix to be the identity matrix.
|
||||
*
|
||||
* @return this */
|
||||
public abstract Matrix setIdentity();
|
||||
|
||||
/** Invert this matrix
|
||||
*
|
||||
* @return this */
|
||||
public abstract Matrix invert();
|
||||
|
||||
/** Load from a float buffer. The buffer stores the matrix in column major
|
||||
* (OpenGL) order.
|
||||
*
|
||||
* @param buf A float buffer to read from
|
||||
* @return this */
|
||||
public abstract Matrix load(FloatBuffer buf);
|
||||
|
||||
/** Load from a float buffer. The buffer stores the matrix in row major
|
||||
* (mathematical) order.
|
||||
*
|
||||
* @param buf A float buffer to read from
|
||||
* @return this */
|
||||
public abstract Matrix loadTranspose(FloatBuffer buf);
|
||||
|
||||
/** Negate this matrix
|
||||
*
|
||||
* @return this */
|
||||
public abstract Matrix negate();
|
||||
|
||||
/** Store this matrix in a float buffer. The matrix is stored in column
|
||||
* major (openGL) order.
|
||||
*
|
||||
* @param buf The buffer to store this matrix in
|
||||
* @return this */
|
||||
public abstract Matrix store(FloatBuffer buf);
|
||||
|
||||
/** Store this matrix in a float buffer. The matrix is stored in row
|
||||
* major (maths) order.
|
||||
*
|
||||
* @param buf The buffer to store this matrix in
|
||||
* @return this */
|
||||
public abstract Matrix storeTranspose(FloatBuffer buf);
|
||||
|
||||
/** Transpose this matrix
|
||||
*
|
||||
* @return this */
|
||||
public abstract Matrix transpose();
|
||||
|
||||
/** Set this matrix to 0.
|
||||
*
|
||||
* @return this */
|
||||
public abstract Matrix setZero();
|
||||
|
||||
/** @return the determinant of the matrix */
|
||||
public abstract float determinant();
|
||||
}
|
|
@ -1,360 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2002-2008 LWJGL Project
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of 'LWJGL' nor the names of
|
||||
* its contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package io.github.hydos.ginger.engine.math.matrixes;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.nio.FloatBuffer;
|
||||
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector2f;
|
||||
|
||||
/** Holds a 2x2 matrix
|
||||
*
|
||||
* @author cix_foo <cix_foo@users.sourceforge.net>
|
||||
* @version $Revision$
|
||||
* $Id$ */
|
||||
public class Matrix2f extends Matrix implements Serializable
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
public float m00, m01, m10, m11;
|
||||
|
||||
/** Constructor for Matrix2f. The matrix is initialised to the identity. */
|
||||
public Matrix2f()
|
||||
{ setIdentity(); }
|
||||
|
||||
/** Constructor */
|
||||
public Matrix2f(Matrix2f src)
|
||||
{ load(src); }
|
||||
|
||||
/** Load from another matrix
|
||||
*
|
||||
* @param src The source matrix
|
||||
* @return this */
|
||||
public Matrix2f load(Matrix2f src)
|
||||
{ return load(src, this); }
|
||||
|
||||
/** Copy the source matrix to the destination matrix.
|
||||
*
|
||||
* @param src The source matrix
|
||||
* @param dest The destination matrix, or null if a new one should be created.
|
||||
* @return The copied matrix */
|
||||
public static Matrix2f load(Matrix2f src, Matrix2f dest)
|
||||
{
|
||||
if (dest == null)
|
||||
dest = new Matrix2f();
|
||||
dest.m00 = src.m00;
|
||||
dest.m01 = src.m01;
|
||||
dest.m10 = src.m10;
|
||||
dest.m11 = src.m11;
|
||||
return dest;
|
||||
}
|
||||
|
||||
/** Load from a float buffer. The buffer stores the matrix in column major
|
||||
* (OpenGL) order.
|
||||
*
|
||||
* @param buf A float buffer to read from
|
||||
* @return this */
|
||||
@Override
|
||||
public Matrix load(FloatBuffer buf)
|
||||
{
|
||||
m00 = buf.get();
|
||||
m01 = buf.get();
|
||||
m10 = buf.get();
|
||||
m11 = buf.get();
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Load from a float buffer. The buffer stores the matrix in row major
|
||||
* (mathematical) order.
|
||||
*
|
||||
* @param buf A float buffer to read from
|
||||
* @return this */
|
||||
@Override
|
||||
public Matrix loadTranspose(FloatBuffer buf)
|
||||
{
|
||||
m00 = buf.get();
|
||||
m10 = buf.get();
|
||||
m01 = buf.get();
|
||||
m11 = buf.get();
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Store this matrix in a float buffer. The matrix is stored in column
|
||||
* major (openGL) order.
|
||||
*
|
||||
* @param buf The buffer to store this matrix in */
|
||||
@Override
|
||||
public Matrix store(FloatBuffer buf)
|
||||
{
|
||||
buf.put(m00);
|
||||
buf.put(m01);
|
||||
buf.put(m10);
|
||||
buf.put(m11);
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Store this matrix in a float buffer. The matrix is stored in row
|
||||
* major (maths) order.
|
||||
*
|
||||
* @param buf The buffer to store this matrix in */
|
||||
@Override
|
||||
public Matrix storeTranspose(FloatBuffer buf)
|
||||
{
|
||||
buf.put(m00);
|
||||
buf.put(m10);
|
||||
buf.put(m01);
|
||||
buf.put(m11);
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Add two matrices together and place the result in a third matrix.
|
||||
*
|
||||
* @param left The left source matrix
|
||||
* @param right The right source matrix
|
||||
* @param dest The destination matrix, or null if a new one is to be created
|
||||
* @return the destination matrix */
|
||||
public static Matrix2f add(Matrix2f left, Matrix2f right, Matrix2f dest)
|
||||
{
|
||||
if (dest == null)
|
||||
dest = new Matrix2f();
|
||||
dest.m00 = left.m00 + right.m00;
|
||||
dest.m01 = left.m01 + right.m01;
|
||||
dest.m10 = left.m10 + right.m10;
|
||||
dest.m11 = left.m11 + right.m11;
|
||||
return dest;
|
||||
}
|
||||
|
||||
/** Subtract the right matrix from the left and place the result in a third matrix.
|
||||
*
|
||||
* @param left The left source matrix
|
||||
* @param right The right source matrix
|
||||
* @param dest The destination matrix, or null if a new one is to be created
|
||||
* @return the destination matrix */
|
||||
public static Matrix2f sub(Matrix2f left, Matrix2f right, Matrix2f dest)
|
||||
{
|
||||
if (dest == null)
|
||||
dest = new Matrix2f();
|
||||
dest.m00 = left.m00 - right.m00;
|
||||
dest.m01 = left.m01 - right.m01;
|
||||
dest.m10 = left.m10 - right.m10;
|
||||
dest.m11 = left.m11 - right.m11;
|
||||
return dest;
|
||||
}
|
||||
|
||||
/** Multiply the right matrix by the left and place the result in a third matrix.
|
||||
*
|
||||
* @param left The left source matrix
|
||||
* @param right The right source matrix
|
||||
* @param dest The destination matrix, or null if a new one is to be created
|
||||
* @return the destination matrix */
|
||||
public static Matrix2f mul(Matrix2f left, Matrix2f right, Matrix2f dest)
|
||||
{
|
||||
if (dest == null)
|
||||
dest = new Matrix2f();
|
||||
float m00 = left.m00 * right.m00 + left.m10 * right.m01;
|
||||
float m01 = left.m01 * right.m00 + left.m11 * right.m01;
|
||||
float m10 = left.m00 * right.m10 + left.m10 * right.m11;
|
||||
float m11 = left.m01 * right.m10 + left.m11 * right.m11;
|
||||
dest.m00 = m00;
|
||||
dest.m01 = m01;
|
||||
dest.m10 = m10;
|
||||
dest.m11 = m11;
|
||||
return dest;
|
||||
}
|
||||
|
||||
/** Transform a Vector by a matrix and return the result in a destination
|
||||
* vector.
|
||||
*
|
||||
* @param left The left matrix
|
||||
* @param right The right vector
|
||||
* @param dest The destination vector, or null if a new one is to be created
|
||||
* @return the destination vector */
|
||||
public static Vector2f transform(Matrix2f left, Vector2f right, Vector2f dest)
|
||||
{
|
||||
if (dest == null)
|
||||
dest = new Vector2f();
|
||||
float x = left.m00 * right.x + left.m10 * right.y;
|
||||
float y = left.m01 * right.x + left.m11 * right.y;
|
||||
dest.x = x;
|
||||
dest.y = y;
|
||||
return dest;
|
||||
}
|
||||
|
||||
/** Transpose this matrix
|
||||
*
|
||||
* @return this */
|
||||
@Override
|
||||
public Matrix transpose()
|
||||
{ return transpose(this); }
|
||||
|
||||
/** Transpose this matrix and place the result in another matrix.
|
||||
*
|
||||
* @param dest The destination matrix or null if a new matrix is to be created
|
||||
* @return the transposed matrix */
|
||||
public Matrix2f transpose(Matrix2f dest)
|
||||
{ return transpose(this, dest); }
|
||||
|
||||
/** Transpose the source matrix and place the result in the destination matrix.
|
||||
*
|
||||
* @param src The source matrix or null if a new matrix is to be created
|
||||
* @param dest The destination matrix or null if a new matrix is to be created
|
||||
* @return the transposed matrix */
|
||||
public static Matrix2f transpose(Matrix2f src, Matrix2f dest)
|
||||
{
|
||||
if (dest == null)
|
||||
dest = new Matrix2f();
|
||||
float m01 = src.m10;
|
||||
float m10 = src.m01;
|
||||
dest.m01 = m01;
|
||||
dest.m10 = m10;
|
||||
return dest;
|
||||
}
|
||||
|
||||
/** Invert this matrix
|
||||
*
|
||||
* @return this if successful, null otherwise */
|
||||
@Override
|
||||
public Matrix invert()
|
||||
{ return invert(this, this); }
|
||||
|
||||
/** Invert the source matrix and place the result in the destination matrix.
|
||||
*
|
||||
* @param src The source matrix to be inverted
|
||||
* @param dest The destination matrix or null if a new matrix is to be created
|
||||
* @return The inverted matrix, or null if source can't be reverted. */
|
||||
public static Matrix2f invert(Matrix2f src, Matrix2f dest)
|
||||
{
|
||||
/*
|
||||
*inv(A) = 1/det(A) * adj(A);
|
||||
*/
|
||||
float determinant = src.determinant();
|
||||
if (determinant != 0)
|
||||
{
|
||||
if (dest == null)
|
||||
dest = new Matrix2f();
|
||||
float determinant_inv = 1f / determinant;
|
||||
float t00 = src.m11 * determinant_inv;
|
||||
float t01 = -src.m01 * determinant_inv;
|
||||
float t11 = src.m00 * determinant_inv;
|
||||
float t10 = -src.m10 * determinant_inv;
|
||||
dest.m00 = t00;
|
||||
dest.m01 = t01;
|
||||
dest.m10 = t10;
|
||||
dest.m11 = t11;
|
||||
return dest;
|
||||
}
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Returns a string representation of this matrix */
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append(m00).append(' ').append(m10).append(' ').append('\n');
|
||||
buf.append(m01).append(' ').append(m11).append(' ').append('\n');
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
/** Negate this matrix
|
||||
*
|
||||
* @return this */
|
||||
@Override
|
||||
public Matrix negate()
|
||||
{ return negate(this); }
|
||||
|
||||
/** Negate this matrix and stash the result in another matrix.
|
||||
*
|
||||
* @param dest The destination matrix, or null if a new matrix is to be created
|
||||
* @return the negated matrix */
|
||||
public Matrix2f negate(Matrix2f dest)
|
||||
{ return negate(this, dest); }
|
||||
|
||||
/** Negate the source matrix and stash the result in the destination matrix.
|
||||
*
|
||||
* @param src The source matrix to be negated
|
||||
* @param dest The destination matrix, or null if a new matrix is to be created
|
||||
* @return the negated matrix */
|
||||
public static Matrix2f negate(Matrix2f src, Matrix2f dest)
|
||||
{
|
||||
if (dest == null)
|
||||
dest = new Matrix2f();
|
||||
dest.m00 = -src.m00;
|
||||
dest.m01 = -src.m01;
|
||||
dest.m10 = -src.m10;
|
||||
dest.m11 = -src.m11;
|
||||
return dest;
|
||||
}
|
||||
|
||||
/** Set this matrix to be the identity matrix.
|
||||
*
|
||||
* @return this */
|
||||
@Override
|
||||
public Matrix setIdentity()
|
||||
{ return setIdentity(this); }
|
||||
|
||||
/** Set the source matrix to be the identity matrix.
|
||||
*
|
||||
* @param src The matrix to set to the identity.
|
||||
* @return The source matrix */
|
||||
public static Matrix2f setIdentity(Matrix2f src)
|
||||
{
|
||||
src.m00 = 1.0f;
|
||||
src.m01 = 0.0f;
|
||||
src.m10 = 0.0f;
|
||||
src.m11 = 1.0f;
|
||||
return src;
|
||||
}
|
||||
|
||||
/** Set this matrix to 0.
|
||||
*
|
||||
* @return this */
|
||||
@Override
|
||||
public Matrix setZero()
|
||||
{ return setZero(this); }
|
||||
|
||||
public static Matrix2f setZero(Matrix2f src)
|
||||
{
|
||||
src.m00 = 0.0f;
|
||||
src.m01 = 0.0f;
|
||||
src.m10 = 0.0f;
|
||||
src.m11 = 0.0f;
|
||||
return src;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.lwjgl.vector.Matrix#determinant()
|
||||
*/
|
||||
@Override
|
||||
public float determinant()
|
||||
{ return m00 * m11 - m01 * m10; }
|
||||
}
|
|
@ -1,467 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2002-2008 LWJGL Project
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of 'LWJGL' nor the names of
|
||||
* its contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package io.github.hydos.ginger.engine.math.matrixes;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.nio.FloatBuffer;
|
||||
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
|
||||
/** Holds a 3x3 matrix.
|
||||
*
|
||||
* @author cix_foo <cix_foo@users.sourceforge.net>
|
||||
* @version $Revision$
|
||||
* $Id$ */
|
||||
public class Matrix3f extends Matrix implements Serializable
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
public float m00,
|
||||
m01,
|
||||
m02,
|
||||
m10,
|
||||
m11,
|
||||
m12,
|
||||
m20,
|
||||
m21,
|
||||
m22;
|
||||
|
||||
/** Constructor for Matrix3f. Matrix is initialised to the identity. */
|
||||
public Matrix3f()
|
||||
{
|
||||
super();
|
||||
setIdentity();
|
||||
}
|
||||
|
||||
/** Load from another matrix
|
||||
*
|
||||
* @param src The source matrix
|
||||
* @return this */
|
||||
public Matrix3f load(Matrix3f src)
|
||||
{ return load(src, this); }
|
||||
|
||||
/** Copy source matrix to destination matrix
|
||||
*
|
||||
* @param src The source matrix
|
||||
* @param dest The destination matrix, or null of a new matrix is to be created
|
||||
* @return The copied matrix */
|
||||
public static Matrix3f load(Matrix3f src, Matrix3f dest)
|
||||
{
|
||||
if (dest == null)
|
||||
dest = new Matrix3f();
|
||||
dest.m00 = src.m00;
|
||||
dest.m10 = src.m10;
|
||||
dest.m20 = src.m20;
|
||||
dest.m01 = src.m01;
|
||||
dest.m11 = src.m11;
|
||||
dest.m21 = src.m21;
|
||||
dest.m02 = src.m02;
|
||||
dest.m12 = src.m12;
|
||||
dest.m22 = src.m22;
|
||||
return dest;
|
||||
}
|
||||
|
||||
/** Load from a float buffer. The buffer stores the matrix in column major
|
||||
* (OpenGL) order.
|
||||
*
|
||||
* @param buf A float buffer to read from
|
||||
* @return this */
|
||||
@Override
|
||||
public Matrix load(FloatBuffer buf)
|
||||
{
|
||||
m00 = buf.get();
|
||||
m01 = buf.get();
|
||||
m02 = buf.get();
|
||||
m10 = buf.get();
|
||||
m11 = buf.get();
|
||||
m12 = buf.get();
|
||||
m20 = buf.get();
|
||||
m21 = buf.get();
|
||||
m22 = buf.get();
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Load from a float buffer. The buffer stores the matrix in row major
|
||||
* (maths) order.
|
||||
*
|
||||
* @param buf A float buffer to read from
|
||||
* @return this */
|
||||
@Override
|
||||
public Matrix loadTranspose(FloatBuffer buf)
|
||||
{
|
||||
m00 = buf.get();
|
||||
m10 = buf.get();
|
||||
m20 = buf.get();
|
||||
m01 = buf.get();
|
||||
m11 = buf.get();
|
||||
m21 = buf.get();
|
||||
m02 = buf.get();
|
||||
m12 = buf.get();
|
||||
m22 = buf.get();
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Store this matrix in a float buffer. The matrix is stored in column
|
||||
* major (openGL) order.
|
||||
*
|
||||
* @param buf The buffer to store this matrix in */
|
||||
@Override
|
||||
public Matrix store(FloatBuffer buf)
|
||||
{
|
||||
buf.put(m00);
|
||||
buf.put(m01);
|
||||
buf.put(m02);
|
||||
buf.put(m10);
|
||||
buf.put(m11);
|
||||
buf.put(m12);
|
||||
buf.put(m20);
|
||||
buf.put(m21);
|
||||
buf.put(m22);
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Store this matrix in a float buffer. The matrix is stored in row
|
||||
* major (maths) order.
|
||||
*
|
||||
* @param buf The buffer to store this matrix in */
|
||||
@Override
|
||||
public Matrix storeTranspose(FloatBuffer buf)
|
||||
{
|
||||
buf.put(m00);
|
||||
buf.put(m10);
|
||||
buf.put(m20);
|
||||
buf.put(m01);
|
||||
buf.put(m11);
|
||||
buf.put(m21);
|
||||
buf.put(m02);
|
||||
buf.put(m12);
|
||||
buf.put(m22);
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Add two matrices together and place the result in a third matrix.
|
||||
*
|
||||
* @param left The left source matrix
|
||||
* @param right The right source matrix
|
||||
* @param dest The destination matrix, or null if a new one is to be created
|
||||
* @return the destination matrix */
|
||||
public static Matrix3f add(Matrix3f left, Matrix3f right, Matrix3f dest)
|
||||
{
|
||||
if (dest == null)
|
||||
dest = new Matrix3f();
|
||||
dest.m00 = left.m00 + right.m00;
|
||||
dest.m01 = left.m01 + right.m01;
|
||||
dest.m02 = left.m02 + right.m02;
|
||||
dest.m10 = left.m10 + right.m10;
|
||||
dest.m11 = left.m11 + right.m11;
|
||||
dest.m12 = left.m12 + right.m12;
|
||||
dest.m20 = left.m20 + right.m20;
|
||||
dest.m21 = left.m21 + right.m21;
|
||||
dest.m22 = left.m22 + right.m22;
|
||||
return dest;
|
||||
}
|
||||
|
||||
/** Subtract the right matrix from the left and place the result in a third matrix.
|
||||
*
|
||||
* @param left The left source matrix
|
||||
* @param right The right source matrix
|
||||
* @param dest The destination matrix, or null if a new one is to be created
|
||||
* @return the destination matrix */
|
||||
public static Matrix3f sub(Matrix3f left, Matrix3f right, Matrix3f dest)
|
||||
{
|
||||
if (dest == null)
|
||||
dest = new Matrix3f();
|
||||
dest.m00 = left.m00 - right.m00;
|
||||
dest.m01 = left.m01 - right.m01;
|
||||
dest.m02 = left.m02 - right.m02;
|
||||
dest.m10 = left.m10 - right.m10;
|
||||
dest.m11 = left.m11 - right.m11;
|
||||
dest.m12 = left.m12 - right.m12;
|
||||
dest.m20 = left.m20 - right.m20;
|
||||
dest.m21 = left.m21 - right.m21;
|
||||
dest.m22 = left.m22 - right.m22;
|
||||
return dest;
|
||||
}
|
||||
|
||||
/** Multiply the right matrix by the left and place the result in a third matrix.
|
||||
*
|
||||
* @param left The left source matrix
|
||||
* @param right The right source matrix
|
||||
* @param dest The destination matrix, or null if a new one is to be created
|
||||
* @return the destination matrix */
|
||||
public static Matrix3f mul(Matrix3f left, Matrix3f right, Matrix3f dest)
|
||||
{
|
||||
if (dest == null)
|
||||
dest = new Matrix3f();
|
||||
float m00 = left.m00 * right.m00 + left.m10 * right.m01 + left.m20 * right.m02;
|
||||
float m01 = left.m01 * right.m00 + left.m11 * right.m01 + left.m21 * right.m02;
|
||||
float m02 = left.m02 * right.m00 + left.m12 * right.m01 + left.m22 * right.m02;
|
||||
float m10 = left.m00 * right.m10 + left.m10 * right.m11 + left.m20 * right.m12;
|
||||
float m11 = left.m01 * right.m10 + left.m11 * right.m11 + left.m21 * right.m12;
|
||||
float m12 = left.m02 * right.m10 + left.m12 * right.m11 + left.m22 * right.m12;
|
||||
float m20 = left.m00 * right.m20 + left.m10 * right.m21 + left.m20 * right.m22;
|
||||
float m21 = left.m01 * right.m20 + left.m11 * right.m21 + left.m21 * right.m22;
|
||||
float m22 = left.m02 * right.m20 + left.m12 * right.m21 + left.m22 * right.m22;
|
||||
dest.m00 = m00;
|
||||
dest.m01 = m01;
|
||||
dest.m02 = m02;
|
||||
dest.m10 = m10;
|
||||
dest.m11 = m11;
|
||||
dest.m12 = m12;
|
||||
dest.m20 = m20;
|
||||
dest.m21 = m21;
|
||||
dest.m22 = m22;
|
||||
return dest;
|
||||
}
|
||||
|
||||
/** Transform a Vector by a matrix and return the result in a destination
|
||||
* vector.
|
||||
*
|
||||
* @param left The left matrix
|
||||
* @param right The right vector
|
||||
* @param dest The destination vector, or null if a new one is to be created
|
||||
* @return the destination vector */
|
||||
public static Vector3f transform(Matrix3f left, Vector3f right, Vector3f dest)
|
||||
{
|
||||
if (dest == null)
|
||||
dest = new Vector3f();
|
||||
float x = left.m00 * right.x + left.m10 * right.y + left.m20 * right.z;
|
||||
float y = left.m01 * right.x + left.m11 * right.y + left.m21 * right.z;
|
||||
float z = left.m02 * right.x + left.m12 * right.y + left.m22 * right.z;
|
||||
dest.x = x;
|
||||
dest.y = y;
|
||||
dest.z = z;
|
||||
return dest;
|
||||
}
|
||||
|
||||
/** Transpose this matrix
|
||||
*
|
||||
* @return this */
|
||||
@Override
|
||||
public Matrix transpose()
|
||||
{ return transpose(this, this); }
|
||||
|
||||
/** Transpose this matrix and place the result in another matrix
|
||||
*
|
||||
* @param dest The destination matrix or null if a new matrix is to be created
|
||||
* @return the transposed matrix */
|
||||
public Matrix3f transpose(Matrix3f dest)
|
||||
{ return transpose(this, dest); }
|
||||
|
||||
/** Transpose the source matrix and place the result into the destination matrix
|
||||
*
|
||||
* @param src The source matrix to be transposed
|
||||
* @param dest The destination matrix or null if a new matrix is to be created
|
||||
* @return the transposed matrix */
|
||||
public static Matrix3f transpose(Matrix3f src, Matrix3f dest)
|
||||
{
|
||||
if (dest == null)
|
||||
dest = new Matrix3f();
|
||||
float m00 = src.m00;
|
||||
float m01 = src.m10;
|
||||
float m02 = src.m20;
|
||||
float m10 = src.m01;
|
||||
float m11 = src.m11;
|
||||
float m12 = src.m21;
|
||||
float m20 = src.m02;
|
||||
float m21 = src.m12;
|
||||
float m22 = src.m22;
|
||||
dest.m00 = m00;
|
||||
dest.m01 = m01;
|
||||
dest.m02 = m02;
|
||||
dest.m10 = m10;
|
||||
dest.m11 = m11;
|
||||
dest.m12 = m12;
|
||||
dest.m20 = m20;
|
||||
dest.m21 = m21;
|
||||
dest.m22 = m22;
|
||||
return dest;
|
||||
}
|
||||
|
||||
/** @return the determinant of the matrix */
|
||||
@Override
|
||||
public float determinant()
|
||||
{
|
||||
float f = m00 * (m11 * m22 - m12 * m21)
|
||||
+ m01 * (m12 * m20 - m10 * m22)
|
||||
+ m02 * (m10 * m21 - m11 * m20);
|
||||
return f;
|
||||
}
|
||||
|
||||
/** Returns a string representation of this matrix */
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append(m00).append(' ').append(m10).append(' ').append(m20).append(' ').append('\n');
|
||||
buf.append(m01).append(' ').append(m11).append(' ').append(m21).append(' ').append('\n');
|
||||
buf.append(m02).append(' ').append(m12).append(' ').append(m22).append(' ').append('\n');
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
/** Invert this matrix
|
||||
*
|
||||
* @return this if successful, null otherwise */
|
||||
@Override
|
||||
public Matrix invert()
|
||||
{ return invert(this, this); }
|
||||
|
||||
/** Invert the source matrix and put the result into the destination matrix
|
||||
*
|
||||
* @param src The source matrix to be inverted
|
||||
* @param dest The destination matrix, or null if a new one is to be created
|
||||
* @return The inverted matrix if successful, null otherwise */
|
||||
public static Matrix3f invert(Matrix3f src, Matrix3f dest)
|
||||
{
|
||||
float determinant = src.determinant();
|
||||
if (determinant != 0)
|
||||
{
|
||||
if (dest == null)
|
||||
dest = new Matrix3f();
|
||||
/* do it the ordinary way
|
||||
*
|
||||
* inv(A) = 1/det(A) * adj(T), where adj(T) = transpose(Conjugate Matrix)
|
||||
*
|
||||
* m00 m01 m02
|
||||
* m10 m11 m12
|
||||
* m20 m21 m22
|
||||
*/
|
||||
float determinant_inv = 1f / determinant;
|
||||
// get the conjugate matrix
|
||||
float t00 = src.m11 * src.m22 - src.m12 * src.m21;
|
||||
float t01 = -src.m10 * src.m22 + src.m12 * src.m20;
|
||||
float t02 = src.m10 * src.m21 - src.m11 * src.m20;
|
||||
float t10 = -src.m01 * src.m22 + src.m02 * src.m21;
|
||||
float t11 = src.m00 * src.m22 - src.m02 * src.m20;
|
||||
float t12 = -src.m00 * src.m21 + src.m01 * src.m20;
|
||||
float t20 = src.m01 * src.m12 - src.m02 * src.m11;
|
||||
float t21 = -src.m00 * src.m12 + src.m02 * src.m10;
|
||||
float t22 = src.m00 * src.m11 - src.m01 * src.m10;
|
||||
dest.m00 = t00 * determinant_inv;
|
||||
dest.m11 = t11 * determinant_inv;
|
||||
dest.m22 = t22 * determinant_inv;
|
||||
dest.m01 = t10 * determinant_inv;
|
||||
dest.m10 = t01 * determinant_inv;
|
||||
dest.m20 = t02 * determinant_inv;
|
||||
dest.m02 = t20 * determinant_inv;
|
||||
dest.m12 = t21 * determinant_inv;
|
||||
dest.m21 = t12 * determinant_inv;
|
||||
return dest;
|
||||
}
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Negate this matrix
|
||||
*
|
||||
* @return this */
|
||||
@Override
|
||||
public Matrix negate()
|
||||
{ return negate(this); }
|
||||
|
||||
/** Negate this matrix and place the result in a destination matrix.
|
||||
*
|
||||
* @param dest The destination matrix, or null if a new matrix is to be created
|
||||
* @return the negated matrix */
|
||||
public Matrix3f negate(Matrix3f dest)
|
||||
{ return negate(this, dest); }
|
||||
|
||||
/** Negate the source matrix and place the result in the destination matrix.
|
||||
*
|
||||
* @param src The source matrix
|
||||
* @param dest The destination matrix, or null if a new matrix is to be created
|
||||
* @return the negated matrix */
|
||||
public static Matrix3f negate(Matrix3f src, Matrix3f dest)
|
||||
{
|
||||
if (dest == null)
|
||||
dest = new Matrix3f();
|
||||
dest.m00 = -src.m00;
|
||||
dest.m01 = -src.m02;
|
||||
dest.m02 = -src.m01;
|
||||
dest.m10 = -src.m10;
|
||||
dest.m11 = -src.m12;
|
||||
dest.m12 = -src.m11;
|
||||
dest.m20 = -src.m20;
|
||||
dest.m21 = -src.m22;
|
||||
dest.m22 = -src.m21;
|
||||
return dest;
|
||||
}
|
||||
|
||||
/** Set this matrix to be the identity matrix.
|
||||
*
|
||||
* @return this */
|
||||
@Override
|
||||
public Matrix setIdentity()
|
||||
{ return setIdentity(this); }
|
||||
|
||||
/** Set the matrix to be the identity matrix.
|
||||
*
|
||||
* @param m The matrix to be set to the identity
|
||||
* @return m */
|
||||
public static Matrix3f setIdentity(Matrix3f m)
|
||||
{
|
||||
m.m00 = 1.0f;
|
||||
m.m01 = 0.0f;
|
||||
m.m02 = 0.0f;
|
||||
m.m10 = 0.0f;
|
||||
m.m11 = 1.0f;
|
||||
m.m12 = 0.0f;
|
||||
m.m20 = 0.0f;
|
||||
m.m21 = 0.0f;
|
||||
m.m22 = 1.0f;
|
||||
return m;
|
||||
}
|
||||
|
||||
/** Set this matrix to 0.
|
||||
*
|
||||
* @return this */
|
||||
@Override
|
||||
public Matrix setZero()
|
||||
{ return setZero(this); }
|
||||
|
||||
/** Set the matrix matrix to 0.
|
||||
*
|
||||
* @param m The matrix to be set to 0
|
||||
* @return m */
|
||||
public static Matrix3f setZero(Matrix3f m)
|
||||
{
|
||||
m.m00 = 0.0f;
|
||||
m.m01 = 0.0f;
|
||||
m.m02 = 0.0f;
|
||||
m.m10 = 0.0f;
|
||||
m.m11 = 0.0f;
|
||||
m.m12 = 0.0f;
|
||||
m.m20 = 0.0f;
|
||||
m.m21 = 0.0f;
|
||||
m.m22 = 0.0f;
|
||||
return m;
|
||||
}
|
||||
}
|
|
@ -1,798 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2002-2008 LWJGL Project
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of 'LWJGL' nor the names of
|
||||
* its contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package io.github.hydos.ginger.engine.math.matrixes;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.nio.FloatBuffer;
|
||||
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector2f;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector4f;
|
||||
|
||||
/** Holds a 4x4 float matrix.
|
||||
*
|
||||
* @author foo */
|
||||
public class Matrix4f extends Matrix implements Serializable
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
public float m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33;
|
||||
|
||||
/** Construct a new matrix, initialized to the identity. */
|
||||
public Matrix4f()
|
||||
{
|
||||
super();
|
||||
setIdentity();
|
||||
}
|
||||
|
||||
public Matrix4f(final Matrix4f src)
|
||||
{
|
||||
super();
|
||||
load(src);
|
||||
}
|
||||
|
||||
/** Returns a string representation of this matrix */
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append(m00).append(' ').append(m10).append(' ').append(m20).append(' ').append(m30).append('\n');
|
||||
buf.append(m01).append(' ').append(m11).append(' ').append(m21).append(' ').append(m31).append('\n');
|
||||
buf.append(m02).append(' ').append(m12).append(' ').append(m22).append(' ').append(m32).append('\n');
|
||||
buf.append(m03).append(' ').append(m13).append(' ').append(m23).append(' ').append(m33).append('\n');
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
/** Set this matrix to be the identity matrix.
|
||||
*
|
||||
* @return this */
|
||||
@Override
|
||||
public Matrix setIdentity()
|
||||
{ return setIdentity(this); }
|
||||
|
||||
/** Set the given matrix to be the identity matrix.
|
||||
*
|
||||
* @param m The matrix to set to the identity
|
||||
* @return m */
|
||||
public static Matrix4f setIdentity(Matrix4f m)
|
||||
{
|
||||
m.m00 = 1.0f;
|
||||
m.m01 = 0.0f;
|
||||
m.m02 = 0.0f;
|
||||
m.m03 = 0.0f;
|
||||
m.m10 = 0.0f;
|
||||
m.m11 = 1.0f;
|
||||
m.m12 = 0.0f;
|
||||
m.m13 = 0.0f;
|
||||
m.m20 = 0.0f;
|
||||
m.m21 = 0.0f;
|
||||
m.m22 = 1.0f;
|
||||
m.m23 = 0.0f;
|
||||
m.m30 = 0.0f;
|
||||
m.m31 = 0.0f;
|
||||
m.m32 = 0.0f;
|
||||
m.m33 = 1.0f;
|
||||
return m;
|
||||
}
|
||||
|
||||
/** Set this matrix to 0.
|
||||
*
|
||||
* @return this */
|
||||
@Override
|
||||
public Matrix setZero()
|
||||
{ return setZero(this); }
|
||||
|
||||
/** Set the given matrix to 0.
|
||||
*
|
||||
* @param m The matrix to set to 0
|
||||
* @return m */
|
||||
public static Matrix4f setZero(Matrix4f m)
|
||||
{
|
||||
m.m00 = 0.0f;
|
||||
m.m01 = 0.0f;
|
||||
m.m02 = 0.0f;
|
||||
m.m03 = 0.0f;
|
||||
m.m10 = 0.0f;
|
||||
m.m11 = 0.0f;
|
||||
m.m12 = 0.0f;
|
||||
m.m13 = 0.0f;
|
||||
m.m20 = 0.0f;
|
||||
m.m21 = 0.0f;
|
||||
m.m22 = 0.0f;
|
||||
m.m23 = 0.0f;
|
||||
m.m30 = 0.0f;
|
||||
m.m31 = 0.0f;
|
||||
m.m32 = 0.0f;
|
||||
m.m33 = 0.0f;
|
||||
return m;
|
||||
}
|
||||
|
||||
/** Load from another matrix4f
|
||||
*
|
||||
* @param src The source matrix
|
||||
* @return this */
|
||||
public Matrix4f load(Matrix4f src)
|
||||
{ return load(src, this); }
|
||||
|
||||
/** Copy the source matrix to the destination matrix
|
||||
*
|
||||
* @param src The source matrix
|
||||
* @param dest The destination matrix, or null of a new one is to be created
|
||||
* @return The copied matrix */
|
||||
public static Matrix4f load(Matrix4f src, Matrix4f dest)
|
||||
{
|
||||
if (dest == null)
|
||||
dest = new Matrix4f();
|
||||
dest.m00 = src.m00;
|
||||
dest.m01 = src.m01;
|
||||
dest.m02 = src.m02;
|
||||
dest.m03 = src.m03;
|
||||
dest.m10 = src.m10;
|
||||
dest.m11 = src.m11;
|
||||
dest.m12 = src.m12;
|
||||
dest.m13 = src.m13;
|
||||
dest.m20 = src.m20;
|
||||
dest.m21 = src.m21;
|
||||
dest.m22 = src.m22;
|
||||
dest.m23 = src.m23;
|
||||
dest.m30 = src.m30;
|
||||
dest.m31 = src.m31;
|
||||
dest.m32 = src.m32;
|
||||
dest.m33 = src.m33;
|
||||
return dest;
|
||||
}
|
||||
|
||||
/** Load from a float buffer. The buffer stores the matrix in column major
|
||||
* (OpenGL) order.
|
||||
*
|
||||
* @param buf A float buffer to read from
|
||||
* @return this */
|
||||
@Override
|
||||
public Matrix load(FloatBuffer buf)
|
||||
{
|
||||
m00 = buf.get();
|
||||
m01 = buf.get();
|
||||
m02 = buf.get();
|
||||
m03 = buf.get();
|
||||
m10 = buf.get();
|
||||
m11 = buf.get();
|
||||
m12 = buf.get();
|
||||
m13 = buf.get();
|
||||
m20 = buf.get();
|
||||
m21 = buf.get();
|
||||
m22 = buf.get();
|
||||
m23 = buf.get();
|
||||
m30 = buf.get();
|
||||
m31 = buf.get();
|
||||
m32 = buf.get();
|
||||
m33 = buf.get();
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Load from a float buffer. The buffer stores the matrix in row major
|
||||
* (maths) order.
|
||||
*
|
||||
* @param buf A float buffer to read from
|
||||
* @return this */
|
||||
@Override
|
||||
public Matrix loadTranspose(FloatBuffer buf)
|
||||
{
|
||||
m00 = buf.get();
|
||||
m10 = buf.get();
|
||||
m20 = buf.get();
|
||||
m30 = buf.get();
|
||||
m01 = buf.get();
|
||||
m11 = buf.get();
|
||||
m21 = buf.get();
|
||||
m31 = buf.get();
|
||||
m02 = buf.get();
|
||||
m12 = buf.get();
|
||||
m22 = buf.get();
|
||||
m32 = buf.get();
|
||||
m03 = buf.get();
|
||||
m13 = buf.get();
|
||||
m23 = buf.get();
|
||||
m33 = buf.get();
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Store this matrix in a float buffer. The matrix is stored in column
|
||||
* major (openGL) order.
|
||||
*
|
||||
* @param buf The buffer to store this matrix in */
|
||||
@Override
|
||||
public Matrix store(FloatBuffer buf)
|
||||
{
|
||||
buf.put(m00);
|
||||
buf.put(m01);
|
||||
buf.put(m02);
|
||||
buf.put(m03);
|
||||
buf.put(m10);
|
||||
buf.put(m11);
|
||||
buf.put(m12);
|
||||
buf.put(m13);
|
||||
buf.put(m20);
|
||||
buf.put(m21);
|
||||
buf.put(m22);
|
||||
buf.put(m23);
|
||||
buf.put(m30);
|
||||
buf.put(m31);
|
||||
buf.put(m32);
|
||||
buf.put(m33);
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Store this matrix in a float buffer. The matrix is stored in row
|
||||
* major (maths) order.
|
||||
*
|
||||
* @param buf The buffer to store this matrix in */
|
||||
@Override
|
||||
public Matrix storeTranspose(FloatBuffer buf)
|
||||
{
|
||||
buf.put(m00);
|
||||
buf.put(m10);
|
||||
buf.put(m20);
|
||||
buf.put(m30);
|
||||
buf.put(m01);
|
||||
buf.put(m11);
|
||||
buf.put(m21);
|
||||
buf.put(m31);
|
||||
buf.put(m02);
|
||||
buf.put(m12);
|
||||
buf.put(m22);
|
||||
buf.put(m32);
|
||||
buf.put(m03);
|
||||
buf.put(m13);
|
||||
buf.put(m23);
|
||||
buf.put(m33);
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Store the rotation portion of this matrix in a float buffer. The matrix is stored in column
|
||||
* major (openGL) order.
|
||||
*
|
||||
* @param buf The buffer to store this matrix in */
|
||||
public Matrix store3f(FloatBuffer buf)
|
||||
{
|
||||
buf.put(m00);
|
||||
buf.put(m01);
|
||||
buf.put(m02);
|
||||
buf.put(m10);
|
||||
buf.put(m11);
|
||||
buf.put(m12);
|
||||
buf.put(m20);
|
||||
buf.put(m21);
|
||||
buf.put(m22);
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Add two matrices together and place the result in a third matrix.
|
||||
*
|
||||
* @param left The left source matrix
|
||||
* @param right The right source matrix
|
||||
* @param dest The destination matrix, or null if a new one is to be created
|
||||
* @return the destination matrix */
|
||||
public static Matrix4f add(Matrix4f left, Matrix4f right, Matrix4f dest)
|
||||
{
|
||||
if (dest == null)
|
||||
dest = new Matrix4f();
|
||||
dest.m00 = left.m00 + right.m00;
|
||||
dest.m01 = left.m01 + right.m01;
|
||||
dest.m02 = left.m02 + right.m02;
|
||||
dest.m03 = left.m03 + right.m03;
|
||||
dest.m10 = left.m10 + right.m10;
|
||||
dest.m11 = left.m11 + right.m11;
|
||||
dest.m12 = left.m12 + right.m12;
|
||||
dest.m13 = left.m13 + right.m13;
|
||||
dest.m20 = left.m20 + right.m20;
|
||||
dest.m21 = left.m21 + right.m21;
|
||||
dest.m22 = left.m22 + right.m22;
|
||||
dest.m23 = left.m23 + right.m23;
|
||||
dest.m30 = left.m30 + right.m30;
|
||||
dest.m31 = left.m31 + right.m31;
|
||||
dest.m32 = left.m32 + right.m32;
|
||||
dest.m33 = left.m33 + right.m33;
|
||||
return dest;
|
||||
}
|
||||
|
||||
/** Subtract the right matrix from the left and place the result in a third matrix.
|
||||
*
|
||||
* @param left The left source matrix
|
||||
* @param right The right source matrix
|
||||
* @param dest The destination matrix, or null if a new one is to be created
|
||||
* @return the destination matrix */
|
||||
public static Matrix4f sub(Matrix4f left, Matrix4f right, Matrix4f dest)
|
||||
{
|
||||
if (dest == null)
|
||||
dest = new Matrix4f();
|
||||
dest.m00 = left.m00 - right.m00;
|
||||
dest.m01 = left.m01 - right.m01;
|
||||
dest.m02 = left.m02 - right.m02;
|
||||
dest.m03 = left.m03 - right.m03;
|
||||
dest.m10 = left.m10 - right.m10;
|
||||
dest.m11 = left.m11 - right.m11;
|
||||
dest.m12 = left.m12 - right.m12;
|
||||
dest.m13 = left.m13 - right.m13;
|
||||
dest.m20 = left.m20 - right.m20;
|
||||
dest.m21 = left.m21 - right.m21;
|
||||
dest.m22 = left.m22 - right.m22;
|
||||
dest.m23 = left.m23 - right.m23;
|
||||
dest.m30 = left.m30 - right.m30;
|
||||
dest.m31 = left.m31 - right.m31;
|
||||
dest.m32 = left.m32 - right.m32;
|
||||
dest.m33 = left.m33 - right.m33;
|
||||
return dest;
|
||||
}
|
||||
|
||||
/** Multiply the right matrix by the left and place the result in a third matrix.
|
||||
*
|
||||
* @param left The left source matrix
|
||||
* @param right The right source matrix
|
||||
* @param dest The destination matrix, or null if a new one is to be created
|
||||
* @return the destination matrix */
|
||||
public Matrix4f mul(Matrix4f left)
|
||||
{ return Matrix4f.mul(left, left, this); }
|
||||
|
||||
public static Matrix4f mul(Matrix4f left, Matrix4f right, Matrix4f dest)
|
||||
{
|
||||
if (dest == null)
|
||||
dest = new Matrix4f();
|
||||
float m00 = left.m00 * right.m00 + left.m10 * right.m01 + left.m20 * right.m02 + left.m30 * right.m03;
|
||||
float m01 = left.m01 * right.m00 + left.m11 * right.m01 + left.m21 * right.m02 + left.m31 * right.m03;
|
||||
float m02 = left.m02 * right.m00 + left.m12 * right.m01 + left.m22 * right.m02 + left.m32 * right.m03;
|
||||
float m03 = left.m03 * right.m00 + left.m13 * right.m01 + left.m23 * right.m02 + left.m33 * right.m03;
|
||||
float m10 = left.m00 * right.m10 + left.m10 * right.m11 + left.m20 * right.m12 + left.m30 * right.m13;
|
||||
float m11 = left.m01 * right.m10 + left.m11 * right.m11 + left.m21 * right.m12 + left.m31 * right.m13;
|
||||
float m12 = left.m02 * right.m10 + left.m12 * right.m11 + left.m22 * right.m12 + left.m32 * right.m13;
|
||||
float m13 = left.m03 * right.m10 + left.m13 * right.m11 + left.m23 * right.m12 + left.m33 * right.m13;
|
||||
float m20 = left.m00 * right.m20 + left.m10 * right.m21 + left.m20 * right.m22 + left.m30 * right.m23;
|
||||
float m21 = left.m01 * right.m20 + left.m11 * right.m21 + left.m21 * right.m22 + left.m31 * right.m23;
|
||||
float m22 = left.m02 * right.m20 + left.m12 * right.m21 + left.m22 * right.m22 + left.m32 * right.m23;
|
||||
float m23 = left.m03 * right.m20 + left.m13 * right.m21 + left.m23 * right.m22 + left.m33 * right.m23;
|
||||
float m30 = left.m00 * right.m30 + left.m10 * right.m31 + left.m20 * right.m32 + left.m30 * right.m33;
|
||||
float m31 = left.m01 * right.m30 + left.m11 * right.m31 + left.m21 * right.m32 + left.m31 * right.m33;
|
||||
float m32 = left.m02 * right.m30 + left.m12 * right.m31 + left.m22 * right.m32 + left.m32 * right.m33;
|
||||
float m33 = left.m03 * right.m30 + left.m13 * right.m31 + left.m23 * right.m32 + left.m33 * right.m33;
|
||||
dest.m00 = m00;
|
||||
dest.m01 = m01;
|
||||
dest.m02 = m02;
|
||||
dest.m03 = m03;
|
||||
dest.m10 = m10;
|
||||
dest.m11 = m11;
|
||||
dest.m12 = m12;
|
||||
dest.m13 = m13;
|
||||
dest.m20 = m20;
|
||||
dest.m21 = m21;
|
||||
dest.m22 = m22;
|
||||
dest.m23 = m23;
|
||||
dest.m30 = m30;
|
||||
dest.m31 = m31;
|
||||
dest.m32 = m32;
|
||||
dest.m33 = m33;
|
||||
return dest;
|
||||
}
|
||||
|
||||
/** Transform a Vector by a matrix and return the result in a destination
|
||||
* vector.
|
||||
*
|
||||
* @param left The left matrix
|
||||
* @param right The right vector
|
||||
* @param dest The destination vector, or null if a new one is to be created
|
||||
* @return the destination vector */
|
||||
public static Vector4f transform(Matrix4f left, Vector4f right, Vector4f dest)
|
||||
{
|
||||
if (dest == null)
|
||||
dest = new Vector4f();
|
||||
float x = left.m00 * right.x + left.m10 * right.y + left.m20 * right.z + left.m30 * right.w;
|
||||
float y = left.m01 * right.x + left.m11 * right.y + left.m21 * right.z + left.m31 * right.w;
|
||||
float z = left.m02 * right.x + left.m12 * right.y + left.m22 * right.z + left.m32 * right.w;
|
||||
float w = left.m03 * right.x + left.m13 * right.y + left.m23 * right.z + left.m33 * right.w;
|
||||
dest.x = x;
|
||||
dest.y = y;
|
||||
dest.z = z;
|
||||
dest.w = w;
|
||||
return dest;
|
||||
}
|
||||
|
||||
/** Transpose this matrix
|
||||
*
|
||||
* @return this */
|
||||
@Override
|
||||
public Matrix transpose()
|
||||
{ return transpose(this); }
|
||||
|
||||
/** Translate this matrix
|
||||
*
|
||||
* @param vec The vector to translate by
|
||||
* @return this */
|
||||
public Matrix4f translate(Vector2f vec)
|
||||
{ return translate(vec, this); }
|
||||
|
||||
/** Translate this matrix
|
||||
*
|
||||
* @param vec The vector to translate by
|
||||
* @return this */
|
||||
public Matrix4f translate(Vector3f vec)
|
||||
{ return translate(vec, this); }
|
||||
|
||||
/** Scales this matrix
|
||||
*
|
||||
* @param vec The vector to scale by
|
||||
* @return this */
|
||||
public Matrix4f scale(Vector3f vec)
|
||||
{ return scale(vec, this, this); }
|
||||
|
||||
/** Scales the source matrix and put the result in the destination matrix
|
||||
*
|
||||
* @param vec The vector to scale by
|
||||
* @param src The source matrix
|
||||
* @param dest The destination matrix, or null if a new matrix is to be created
|
||||
* @return The scaled matrix */
|
||||
public static Matrix4f scale(Vector3f vec, Matrix4f src, Matrix4f dest)
|
||||
{
|
||||
if (dest == null)
|
||||
dest = new Matrix4f();
|
||||
dest.m00 = src.m00 * vec.x;
|
||||
dest.m01 = src.m01 * vec.x;
|
||||
dest.m02 = src.m02 * vec.x;
|
||||
dest.m03 = src.m03 * vec.x;
|
||||
dest.m10 = src.m10 * vec.y;
|
||||
dest.m11 = src.m11 * vec.y;
|
||||
dest.m12 = src.m12 * vec.y;
|
||||
dest.m13 = src.m13 * vec.y;
|
||||
dest.m20 = src.m20 * vec.z;
|
||||
dest.m21 = src.m21 * vec.z;
|
||||
dest.m22 = src.m22 * vec.z;
|
||||
dest.m23 = src.m23 * vec.z;
|
||||
return dest;
|
||||
}
|
||||
|
||||
/** Rotates the matrix around the given axis the specified angle
|
||||
*
|
||||
* @param angle the angle, in radians.
|
||||
* @param axis The vector representing the rotation axis. Must be normalized.
|
||||
* @return this */
|
||||
public Matrix4f rotate(float angle, Vector3f axis)
|
||||
{ return rotate(angle, axis, this); }
|
||||
|
||||
/** Rotates the matrix around the given axis the specified angle
|
||||
*
|
||||
* @param angle the angle, in radians.
|
||||
* @param axis The vector representing the rotation axis. Must be normalized.
|
||||
* @param dest The matrix to put the result, or null if a new matrix is to be created
|
||||
* @return The rotated matrix */
|
||||
public Matrix4f rotate(float angle, Vector3f axis, Matrix4f dest)
|
||||
{ return rotate(angle, axis, this, dest); }
|
||||
|
||||
/** Rotates the source matrix around the given axis the specified angle and
|
||||
* put the result in the destination matrix.
|
||||
*
|
||||
* @param angle the angle, in radians.
|
||||
* @param axis The vector representing the rotation axis. Must be normalized.
|
||||
* @param src The matrix to rotate
|
||||
* @param dest The matrix to put the result, or null if a new matrix is to be created
|
||||
* @return The rotated matrix */
|
||||
public static Matrix4f rotate(float angle, Vector3f axis, Matrix4f src, Matrix4f dest)
|
||||
{
|
||||
if (dest == null)
|
||||
dest = new Matrix4f();
|
||||
float c = (float) Math.cos(angle);
|
||||
float s = (float) Math.sin(angle);
|
||||
float oneminusc = 1.0f - c;
|
||||
float xy = axis.x * axis.y;
|
||||
float yz = axis.y * axis.z;
|
||||
float xz = axis.x * axis.z;
|
||||
float xs = axis.x * s;
|
||||
float ys = axis.y * s;
|
||||
float zs = axis.z * s;
|
||||
float f00 = axis.x * axis.x * oneminusc + c;
|
||||
float f01 = xy * oneminusc + zs;
|
||||
float f02 = xz * oneminusc - ys;
|
||||
// n[3] not used
|
||||
float f10 = xy * oneminusc - zs;
|
||||
float f11 = axis.y * axis.y * oneminusc + c;
|
||||
float f12 = yz * oneminusc + xs;
|
||||
// n[7] not used
|
||||
float f20 = xz * oneminusc + ys;
|
||||
float f21 = yz * oneminusc - xs;
|
||||
float f22 = axis.z * axis.z * oneminusc + c;
|
||||
float t00 = src.m00 * f00 + src.m10 * f01 + src.m20 * f02;
|
||||
float t01 = src.m01 * f00 + src.m11 * f01 + src.m21 * f02;
|
||||
float t02 = src.m02 * f00 + src.m12 * f01 + src.m22 * f02;
|
||||
float t03 = src.m03 * f00 + src.m13 * f01 + src.m23 * f02;
|
||||
float t10 = src.m00 * f10 + src.m10 * f11 + src.m20 * f12;
|
||||
float t11 = src.m01 * f10 + src.m11 * f11 + src.m21 * f12;
|
||||
float t12 = src.m02 * f10 + src.m12 * f11 + src.m22 * f12;
|
||||
float t13 = src.m03 * f10 + src.m13 * f11 + src.m23 * f12;
|
||||
dest.m20 = src.m00 * f20 + src.m10 * f21 + src.m20 * f22;
|
||||
dest.m21 = src.m01 * f20 + src.m11 * f21 + src.m21 * f22;
|
||||
dest.m22 = src.m02 * f20 + src.m12 * f21 + src.m22 * f22;
|
||||
dest.m23 = src.m03 * f20 + src.m13 * f21 + src.m23 * f22;
|
||||
dest.m00 = t00;
|
||||
dest.m01 = t01;
|
||||
dest.m02 = t02;
|
||||
dest.m03 = t03;
|
||||
dest.m10 = t10;
|
||||
dest.m11 = t11;
|
||||
dest.m12 = t12;
|
||||
dest.m13 = t13;
|
||||
return dest;
|
||||
}
|
||||
|
||||
/** Translate this matrix and stash the result in another matrix
|
||||
*
|
||||
* @param vec The vector to translate by
|
||||
* @param dest The destination matrix or null if a new matrix is to be created
|
||||
* @return the translated matrix */
|
||||
public Matrix4f translate(Vector3f vec, Matrix4f dest)
|
||||
{ return translate(vec, this, dest); }
|
||||
|
||||
/** Translate the source matrix and stash the result in the destination matrix
|
||||
*
|
||||
* @param vec The vector to translate by
|
||||
* @param src The source matrix
|
||||
* @param dest The destination matrix or null if a new matrix is to be created
|
||||
* @return The translated matrix */
|
||||
public static Matrix4f translate(Vector3f vec, Matrix4f src, Matrix4f dest)
|
||||
{
|
||||
if (dest == null)
|
||||
dest = new Matrix4f();
|
||||
dest.m30 += src.m00 * vec.x + src.m10 * vec.y + src.m20 * vec.z;
|
||||
dest.m31 += src.m01 * vec.x + src.m11 * vec.y + src.m21 * vec.z;
|
||||
dest.m32 += src.m02 * vec.x + src.m12 * vec.y + src.m22 * vec.z;
|
||||
dest.m33 += src.m03 * vec.x + src.m13 * vec.y + src.m23 * vec.z;
|
||||
return dest;
|
||||
}
|
||||
|
||||
/** Translate this matrix and stash the result in another matrix
|
||||
*
|
||||
* @param vec The vector to translate by
|
||||
* @param dest The destination matrix or null if a new matrix is to be created
|
||||
* @return the translated matrix */
|
||||
public Matrix4f translate(Vector2f vec, Matrix4f dest)
|
||||
{ return translate(vec, this, dest); }
|
||||
|
||||
/** Translate the source matrix and stash the result in the destination matrix
|
||||
*
|
||||
* @param vec The vector to translate by
|
||||
* @param src The source matrix
|
||||
* @param dest The destination matrix or null if a new matrix is to be created
|
||||
* @return The translated matrix */
|
||||
public static Matrix4f translate(Vector2f vec, Matrix4f src, Matrix4f dest)
|
||||
{
|
||||
if (dest == null)
|
||||
dest = new Matrix4f();
|
||||
dest.m30 += src.m00 * vec.x + src.m10 * vec.y;
|
||||
dest.m31 += src.m01 * vec.x + src.m11 * vec.y;
|
||||
dest.m32 += src.m02 * vec.x + src.m12 * vec.y;
|
||||
dest.m33 += src.m03 * vec.x + src.m13 * vec.y;
|
||||
return dest;
|
||||
}
|
||||
|
||||
/** Transpose this matrix and place the result in another matrix
|
||||
*
|
||||
* @param dest The destination matrix or null if a new matrix is to be created
|
||||
* @return the transposed matrix */
|
||||
public Matrix4f transpose(Matrix4f dest)
|
||||
{ return transpose(this, dest); }
|
||||
|
||||
/** Transpose the source matrix and place the result in the destination matrix
|
||||
*
|
||||
* @param src The source matrix
|
||||
* @param dest The destination matrix or null if a new matrix is to be created
|
||||
* @return the transposed matrix */
|
||||
public static Matrix4f transpose(Matrix4f src, Matrix4f dest)
|
||||
{
|
||||
if (dest == null)
|
||||
dest = new Matrix4f();
|
||||
float m00 = src.m00;
|
||||
float m01 = src.m10;
|
||||
float m02 = src.m20;
|
||||
float m03 = src.m30;
|
||||
float m10 = src.m01;
|
||||
float m11 = src.m11;
|
||||
float m12 = src.m21;
|
||||
float m13 = src.m31;
|
||||
float m20 = src.m02;
|
||||
float m21 = src.m12;
|
||||
float m22 = src.m22;
|
||||
float m23 = src.m32;
|
||||
float m30 = src.m03;
|
||||
float m31 = src.m13;
|
||||
float m32 = src.m23;
|
||||
float m33 = src.m33;
|
||||
dest.m00 = m00;
|
||||
dest.m01 = m01;
|
||||
dest.m02 = m02;
|
||||
dest.m03 = m03;
|
||||
dest.m10 = m10;
|
||||
dest.m11 = m11;
|
||||
dest.m12 = m12;
|
||||
dest.m13 = m13;
|
||||
dest.m20 = m20;
|
||||
dest.m21 = m21;
|
||||
dest.m22 = m22;
|
||||
dest.m23 = m23;
|
||||
dest.m30 = m30;
|
||||
dest.m31 = m31;
|
||||
dest.m32 = m32;
|
||||
dest.m33 = m33;
|
||||
return dest;
|
||||
}
|
||||
|
||||
/** @return the determinant of the matrix */
|
||||
@Override
|
||||
public float determinant()
|
||||
{
|
||||
float f = m00
|
||||
* ((m11 * m22 * m33 + m12 * m23 * m31 + m13 * m21 * m32)
|
||||
- m13 * m22 * m31
|
||||
- m11 * m23 * m32
|
||||
- m12 * m21 * m33);
|
||||
f -= m01
|
||||
* ((m10 * m22 * m33 + m12 * m23 * m30 + m13 * m20 * m32)
|
||||
- m13 * m22 * m30
|
||||
- m10 * m23 * m32
|
||||
- m12 * m20 * m33);
|
||||
f += m02
|
||||
* ((m10 * m21 * m33 + m11 * m23 * m30 + m13 * m20 * m31)
|
||||
- m13 * m21 * m30
|
||||
- m10 * m23 * m31
|
||||
- m11 * m20 * m33);
|
||||
f -= m03
|
||||
* ((m10 * m21 * m32 + m11 * m22 * m30 + m12 * m20 * m31)
|
||||
- m12 * m21 * m30
|
||||
- m10 * m22 * m31
|
||||
- m11 * m20 * m32);
|
||||
return f;
|
||||
}
|
||||
|
||||
/** Calculate the determinant of a 3x3 matrix
|
||||
*
|
||||
* @return result */
|
||||
private static float determinant3x3(float t00, float t01, float t02,
|
||||
float t10, float t11, float t12,
|
||||
float t20, float t21, float t22)
|
||||
{ return t00 * (t11 * t22 - t12 * t21)
|
||||
+ t01 * (t12 * t20 - t10 * t22)
|
||||
+ t02 * (t10 * t21 - t11 * t20); }
|
||||
|
||||
/** Invert this matrix
|
||||
*
|
||||
* @return this if successful, null otherwise */
|
||||
@Override
|
||||
public Matrix invert()
|
||||
{ return invert(this, this); }
|
||||
|
||||
/** Invert the source matrix and put the result in the destination
|
||||
*
|
||||
* @param src The source matrix
|
||||
* @param dest The destination matrix, or null if a new matrix is to be created
|
||||
* @return The inverted matrix if successful, null otherwise */
|
||||
public static Matrix4f invert(Matrix4f src, Matrix4f dest)
|
||||
{
|
||||
float determinant = src.determinant();
|
||||
if (determinant != 0)
|
||||
{
|
||||
/*
|
||||
* m00 m01 m02 m03
|
||||
* m10 m11 m12 m13
|
||||
* m20 m21 m22 m23
|
||||
* m30 m31 m32 m33
|
||||
*/
|
||||
if (dest == null)
|
||||
dest = new Matrix4f();
|
||||
float determinant_inv = 1f / determinant;
|
||||
// first row
|
||||
float t00 = determinant3x3(src.m11, src.m12, src.m13, src.m21, src.m22, src.m23, src.m31, src.m32, src.m33);
|
||||
float t01 = -determinant3x3(src.m10, src.m12, src.m13, src.m20, src.m22, src.m23, src.m30, src.m32, src.m33);
|
||||
float t02 = determinant3x3(src.m10, src.m11, src.m13, src.m20, src.m21, src.m23, src.m30, src.m31, src.m33);
|
||||
float t03 = -determinant3x3(src.m10, src.m11, src.m12, src.m20, src.m21, src.m22, src.m30, src.m31, src.m32);
|
||||
// second row
|
||||
float t10 = -determinant3x3(src.m01, src.m02, src.m03, src.m21, src.m22, src.m23, src.m31, src.m32, src.m33);
|
||||
float t11 = determinant3x3(src.m00, src.m02, src.m03, src.m20, src.m22, src.m23, src.m30, src.m32, src.m33);
|
||||
float t12 = -determinant3x3(src.m00, src.m01, src.m03, src.m20, src.m21, src.m23, src.m30, src.m31, src.m33);
|
||||
float t13 = determinant3x3(src.m00, src.m01, src.m02, src.m20, src.m21, src.m22, src.m30, src.m31, src.m32);
|
||||
// third row
|
||||
float t20 = determinant3x3(src.m01, src.m02, src.m03, src.m11, src.m12, src.m13, src.m31, src.m32, src.m33);
|
||||
float t21 = -determinant3x3(src.m00, src.m02, src.m03, src.m10, src.m12, src.m13, src.m30, src.m32, src.m33);
|
||||
float t22 = determinant3x3(src.m00, src.m01, src.m03, src.m10, src.m11, src.m13, src.m30, src.m31, src.m33);
|
||||
float t23 = -determinant3x3(src.m00, src.m01, src.m02, src.m10, src.m11, src.m12, src.m30, src.m31, src.m32);
|
||||
// fourth row
|
||||
float t30 = -determinant3x3(src.m01, src.m02, src.m03, src.m11, src.m12, src.m13, src.m21, src.m22, src.m23);
|
||||
float t31 = determinant3x3(src.m00, src.m02, src.m03, src.m10, src.m12, src.m13, src.m20, src.m22, src.m23);
|
||||
float t32 = -determinant3x3(src.m00, src.m01, src.m03, src.m10, src.m11, src.m13, src.m20, src.m21, src.m23);
|
||||
float t33 = determinant3x3(src.m00, src.m01, src.m02, src.m10, src.m11, src.m12, src.m20, src.m21, src.m22);
|
||||
// transpose and divide by the determinant
|
||||
dest.m00 = t00 * determinant_inv;
|
||||
dest.m11 = t11 * determinant_inv;
|
||||
dest.m22 = t22 * determinant_inv;
|
||||
dest.m33 = t33 * determinant_inv;
|
||||
dest.m01 = t10 * determinant_inv;
|
||||
dest.m10 = t01 * determinant_inv;
|
||||
dest.m20 = t02 * determinant_inv;
|
||||
dest.m02 = t20 * determinant_inv;
|
||||
dest.m12 = t21 * determinant_inv;
|
||||
dest.m21 = t12 * determinant_inv;
|
||||
dest.m03 = t30 * determinant_inv;
|
||||
dest.m30 = t03 * determinant_inv;
|
||||
dest.m13 = t31 * determinant_inv;
|
||||
dest.m31 = t13 * determinant_inv;
|
||||
dest.m32 = t23 * determinant_inv;
|
||||
dest.m23 = t32 * determinant_inv;
|
||||
return dest;
|
||||
}
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Negate this matrix
|
||||
*
|
||||
* @return this */
|
||||
@Override
|
||||
public Matrix negate()
|
||||
{ return negate(this); }
|
||||
|
||||
/** Negate this matrix and place the result in a destination matrix.
|
||||
*
|
||||
* @param dest The destination matrix, or null if a new matrix is to be created
|
||||
* @return the negated matrix */
|
||||
public Matrix4f negate(Matrix4f dest)
|
||||
{ return negate(this, dest); }
|
||||
|
||||
/** Negate this matrix and place the result in a destination matrix.
|
||||
*
|
||||
* @param src The source matrix
|
||||
* @param dest The destination matrix, or null if a new matrix is to be created
|
||||
* @return The negated matrix */
|
||||
public static Matrix4f negate(Matrix4f src, Matrix4f dest)
|
||||
{
|
||||
if (dest == null)
|
||||
dest = new Matrix4f();
|
||||
dest.m00 = -src.m00;
|
||||
dest.m01 = -src.m01;
|
||||
dest.m02 = -src.m02;
|
||||
dest.m03 = -src.m03;
|
||||
dest.m10 = -src.m10;
|
||||
dest.m11 = -src.m11;
|
||||
dest.m12 = -src.m12;
|
||||
dest.m13 = -src.m13;
|
||||
dest.m20 = -src.m20;
|
||||
dest.m21 = -src.m21;
|
||||
dest.m22 = -src.m22;
|
||||
dest.m23 = -src.m23;
|
||||
dest.m30 = -src.m30;
|
||||
dest.m31 = -src.m31;
|
||||
dest.m32 = -src.m32;
|
||||
dest.m33 = -src.m33;
|
||||
return dest;
|
||||
}
|
||||
}
|
|
@ -1,50 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2002-2008 LWJGL Project
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of 'LWJGL' nor the names of
|
||||
* its contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package io.github.hydos.ginger.engine.math.vectors;
|
||||
|
||||
import java.nio.FloatBuffer;
|
||||
|
||||
/** @author foo */
|
||||
public interface ReadableVector
|
||||
{
|
||||
/** @return the length of the vector */
|
||||
float length();
|
||||
|
||||
/** @return the length squared of the vector */
|
||||
float lengthSquared();
|
||||
|
||||
/** Store this vector in a FloatBuffer
|
||||
*
|
||||
* @param buf The buffer to store it in, at the current position
|
||||
* @return this */
|
||||
Vector store(FloatBuffer buf);
|
||||
}
|
|
@ -1,42 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2002-2008 LWJGL Project
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of 'LWJGL' nor the names of
|
||||
* its contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package io.github.hydos.ginger.engine.math.vectors;
|
||||
|
||||
/** @author foo */
|
||||
public interface ReadableVector2f extends ReadableVector
|
||||
{
|
||||
/** @return x */
|
||||
float getX();
|
||||
|
||||
/** @return y */
|
||||
float getY();
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2002-2008 LWJGL Project
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of 'LWJGL' nor the names of
|
||||
* its contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package io.github.hydos.ginger.engine.math.vectors;
|
||||
|
||||
/** @author foo */
|
||||
public interface ReadableVector3f extends ReadableVector2f
|
||||
{
|
||||
/** @return z */
|
||||
float getZ();
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2002-2008 LWJGL Project
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of 'LWJGL' nor the names of
|
||||
* its contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package io.github.hydos.ginger.engine.math.vectors;
|
||||
|
||||
/** @author foo */
|
||||
public interface ReadableVector4f extends ReadableVector3f
|
||||
{
|
||||
/** @return w */
|
||||
float getW();
|
||||
}
|
|
@ -1,97 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2002-2008 LWJGL Project
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of 'LWJGL' nor the names of
|
||||
* its contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package io.github.hydos.ginger.engine.math.vectors;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.nio.FloatBuffer;
|
||||
|
||||
/** Base class for vectors.
|
||||
*
|
||||
* @author cix_foo <cix_foo@users.sourceforge.net>
|
||||
* @version $Revision$
|
||||
* $Id$ */
|
||||
public abstract class Vector implements Serializable, ReadableVector
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/** Constructor for Vector. */
|
||||
protected Vector()
|
||||
{ super(); }
|
||||
|
||||
/** @return the length of the vector */
|
||||
@Override
|
||||
public final float length()
|
||||
{ return (float) Math.sqrt(lengthSquared()); }
|
||||
|
||||
/** @return the length squared of the vector */
|
||||
@Override
|
||||
public abstract float lengthSquared();
|
||||
|
||||
/** Load this vector from a FloatBuffer
|
||||
*
|
||||
* @param buf The buffer to load it from, at the current position
|
||||
* @return this */
|
||||
public abstract Vector load(FloatBuffer buf);
|
||||
|
||||
/** Negate a vector
|
||||
*
|
||||
* @return this */
|
||||
public abstract Vector negate();
|
||||
|
||||
/** Normalise this vector
|
||||
*
|
||||
* @return this */
|
||||
public final Vector normalise()
|
||||
{
|
||||
float len = length();
|
||||
if (len != 0.0f)
|
||||
{
|
||||
float l = 1.0f / len;
|
||||
return scale(l);
|
||||
}
|
||||
else
|
||||
throw new IllegalStateException("Zero length vector");
|
||||
}
|
||||
|
||||
/** Store this vector in a FloatBuffer
|
||||
*
|
||||
* @param buf The buffer to store it in, at the current position
|
||||
* @return this */
|
||||
@Override
|
||||
public abstract Vector store(FloatBuffer buf);
|
||||
|
||||
/** Scale this vector
|
||||
*
|
||||
* @param scale The scale factor
|
||||
* @return this */
|
||||
public abstract Vector scale(float scale);
|
||||
}
|
|
@ -1,279 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2002-2008 LWJGL Project
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of 'LWJGL' nor the names of
|
||||
* its contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package io.github.hydos.ginger.engine.math.vectors;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.nio.FloatBuffer;
|
||||
|
||||
/** Holds a 2-tuple vector.
|
||||
*
|
||||
* @author cix_foo <cix_foo@users.sourceforge.net>
|
||||
* @version $Revision$
|
||||
* $Id$ */
|
||||
public class Vector2f extends Vector implements Serializable, ReadableVector2f, WritableVector2f
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
public float x, y;
|
||||
|
||||
/** Constructor for Vector2f. */
|
||||
public Vector2f()
|
||||
{ super(); }
|
||||
|
||||
/** Constructor. */
|
||||
public Vector2f(ReadableVector2f src)
|
||||
{ set(src); }
|
||||
|
||||
/** Constructor. */
|
||||
public Vector2f(float x, float y)
|
||||
{ set(x, y); }
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.lwjgl.util.vector.WritableVector2f#set(float, float)
|
||||
*/
|
||||
@Override
|
||||
public void set(float x, float y)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
/** Load from another Vector2f
|
||||
*
|
||||
* @param src The source vector
|
||||
* @return this */
|
||||
public Vector2f set(ReadableVector2f src)
|
||||
{
|
||||
x = src.getX();
|
||||
y = src.getY();
|
||||
return this;
|
||||
}
|
||||
|
||||
/** @return the length squared of the vector */
|
||||
@Override
|
||||
public float lengthSquared()
|
||||
{ return x * x + y * y; }
|
||||
|
||||
/** Translate a vector
|
||||
*
|
||||
* @param x The translation in x
|
||||
* @param y the translation in y
|
||||
* @return this */
|
||||
public Vector2f translate(float x, float y)
|
||||
{
|
||||
this.x += x;
|
||||
this.y += y;
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Negate a vector
|
||||
*
|
||||
* @return this */
|
||||
@Override
|
||||
public Vector negate()
|
||||
{
|
||||
x = -x;
|
||||
y = -y;
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Negate a vector and place the result in a destination vector.
|
||||
*
|
||||
* @param dest The destination vector or null if a new vector is to be created
|
||||
* @return the negated vector */
|
||||
public Vector2f negate(Vector2f dest)
|
||||
{
|
||||
if (dest == null)
|
||||
dest = new Vector2f();
|
||||
dest.x = -x;
|
||||
dest.y = -y;
|
||||
return dest;
|
||||
}
|
||||
|
||||
/** Normalise this vector and place the result in another vector.
|
||||
*
|
||||
* @param dest The destination vector, or null if a new vector is to be created
|
||||
* @return the normalised vector */
|
||||
public Vector2f normalise(Vector2f dest)
|
||||
{
|
||||
float l = length();
|
||||
if (dest == null)
|
||||
dest = new Vector2f(x / l, y / l);
|
||||
else
|
||||
dest.set(x / l, y / l);
|
||||
return dest;
|
||||
}
|
||||
|
||||
/** The dot product of two vectors is calculated as
|
||||
* v1.x * v2.x + v1.y * v2.y + v1.z * v2.z
|
||||
*
|
||||
* @param left The LHS vector
|
||||
* @param right The RHS vector
|
||||
* @return left dot right */
|
||||
public static float dot(Vector2f left, Vector2f right)
|
||||
{ return left.x * right.x + left.y * right.y; }
|
||||
|
||||
/** Calculate the angle between two vectors, in radians
|
||||
*
|
||||
* @param a A vector
|
||||
* @param b The other vector
|
||||
* @return the angle between the two vectors, in radians */
|
||||
public static float angle(Vector2f a, Vector2f b)
|
||||
{
|
||||
float dls = dot(a, b) / (a.length() * b.length());
|
||||
if (dls < -1f)
|
||||
dls = -1f;
|
||||
else if (dls > 1.0f)
|
||||
dls = 1.0f;
|
||||
return (float) Math.acos(dls);
|
||||
}
|
||||
|
||||
/** Add a vector to another vector and place the result in a destination
|
||||
* vector.
|
||||
*
|
||||
* @param left The LHS vector
|
||||
* @param right The RHS vector
|
||||
* @param dest The destination vector, or null if a new vector is to be created
|
||||
* @return the sum of left and right in dest */
|
||||
public static Vector2f add(Vector2f left, Vector2f right, Vector2f dest)
|
||||
{
|
||||
if (dest == null)
|
||||
return new Vector2f(left.x + right.x, left.y + right.y);
|
||||
else
|
||||
{
|
||||
dest.set(left.x + right.x, left.y + right.y);
|
||||
return dest;
|
||||
}
|
||||
}
|
||||
|
||||
/** Subtract a vector from another vector and place the result in a destination
|
||||
* vector.
|
||||
*
|
||||
* @param left The LHS vector
|
||||
* @param right The RHS vector
|
||||
* @param dest The destination vector, or null if a new vector is to be created
|
||||
* @return left minus right in dest */
|
||||
public static Vector2f sub(Vector2f left, Vector2f right, Vector2f dest)
|
||||
{
|
||||
if (dest == null)
|
||||
return new Vector2f(left.x - right.x, left.y - right.y);
|
||||
else
|
||||
{
|
||||
dest.set(left.x - right.x, left.y - right.y);
|
||||
return dest;
|
||||
}
|
||||
}
|
||||
|
||||
/** Store this vector in a FloatBuffer
|
||||
*
|
||||
* @param buf The buffer to store it in, at the current position
|
||||
* @return this */
|
||||
@Override
|
||||
public Vector store(FloatBuffer buf)
|
||||
{
|
||||
buf.put(x);
|
||||
buf.put(y);
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Load this vector from a FloatBuffer
|
||||
*
|
||||
* @param buf The buffer to load it from, at the current position
|
||||
* @return this */
|
||||
@Override
|
||||
public Vector load(FloatBuffer buf)
|
||||
{
|
||||
x = buf.get();
|
||||
y = buf.get();
|
||||
return this;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.lwjgl.vector.Vector#scale(float)
|
||||
*/
|
||||
@Override
|
||||
public Vector scale(float scale)
|
||||
{
|
||||
x *= scale;
|
||||
y *= scale;
|
||||
return this;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder(64);
|
||||
sb.append("Vector2f[");
|
||||
sb.append(x);
|
||||
sb.append(", ");
|
||||
sb.append(y);
|
||||
sb.append(']');
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/** @return x */
|
||||
@Override
|
||||
public final float getX()
|
||||
{ return x; }
|
||||
|
||||
/** @return y */
|
||||
@Override
|
||||
public final float getY()
|
||||
{ return y; }
|
||||
|
||||
/** Set X
|
||||
*
|
||||
* @param x */
|
||||
@Override
|
||||
public final void setX(float x)
|
||||
{ this.x = x; }
|
||||
|
||||
/** Set Y
|
||||
*
|
||||
* @param y */
|
||||
@Override
|
||||
public final void setY(float y)
|
||||
{ this.y = y; }
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj)
|
||||
{
|
||||
if (this == obj) return true;
|
||||
if (obj == null) return false;
|
||||
if (getClass() != obj.getClass()) return false;
|
||||
Vector2f other = (Vector2f) obj;
|
||||
if (x == other.x && y == other.y) return true;
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -1,340 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2002-2008 LWJGL Project
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of 'LWJGL' nor the names of
|
||||
* its contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package io.github.hydos.ginger.engine.math.vectors;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.nio.FloatBuffer;
|
||||
|
||||
/** Holds a 3-tuple vector.
|
||||
*
|
||||
* @author cix_foo <cix_foo@users.sourceforge.net>
|
||||
* @version $Revision$
|
||||
* $Id$ */
|
||||
public class Vector3f extends Vector implements Serializable, ReadableVector3f, WritableVector3f
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
public float x, y, z;
|
||||
|
||||
/** Constructor for Vector3f. */
|
||||
public Vector3f()
|
||||
{ super(); }
|
||||
|
||||
/** Constructor */
|
||||
public Vector3f(ReadableVector3f src)
|
||||
{ set(src); }
|
||||
|
||||
/** Constructor */
|
||||
public Vector3f(float x, float y, float z)
|
||||
{ set(x, y, z); }
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.lwjgl.util.vector.WritableVector2f#set(float, float)
|
||||
*/
|
||||
@Override
|
||||
public void set(float x, float y)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.lwjgl.util.vector.WritableVector3f#set(float, float, float)
|
||||
*/
|
||||
@Override
|
||||
public void set(float x, float y, float z)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
/** Load from another Vector3f
|
||||
*
|
||||
* @param src The source vector
|
||||
* @return this */
|
||||
public Vector3f set(ReadableVector3f src)
|
||||
{
|
||||
x = src.getX();
|
||||
y = src.getY();
|
||||
z = src.getZ();
|
||||
return this;
|
||||
}
|
||||
|
||||
/** @return the length squared of the vector */
|
||||
@Override
|
||||
public float lengthSquared()
|
||||
{ return x * x + y * y + z * z; }
|
||||
|
||||
/** Translate a vector
|
||||
*
|
||||
* @param x The translation in x
|
||||
* @param y the translation in y
|
||||
* @return this */
|
||||
public Vector3f translate(float x, float y, float z)
|
||||
{
|
||||
this.x += x;
|
||||
this.y += y;
|
||||
this.z += z;
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Add a vector to another vector and place the result in a destination
|
||||
* vector.
|
||||
*
|
||||
* @param left The LHS vector
|
||||
* @param right The RHS vector
|
||||
* @param dest The destination vector, or null if a new vector is to be created
|
||||
* @return the sum of left and right in dest */
|
||||
public static Vector3f add(Vector3f left, Vector3f right, Vector3f dest)
|
||||
{
|
||||
if (dest == null)
|
||||
return new Vector3f(left.x + right.x, left.y + right.y, left.z + right.z);
|
||||
else
|
||||
{
|
||||
dest.set(left.x + right.x, left.y + right.y, left.z + right.z);
|
||||
return dest;
|
||||
}
|
||||
}
|
||||
|
||||
public Vector3f add(Vector3f vector)
|
||||
{ return new Vector3f(x + vector.x, y + vector.y, z + vector.z); }
|
||||
|
||||
/** Subtract a vector from another vector and place the result in a destination
|
||||
* vector.
|
||||
*
|
||||
* @param left The LHS vector
|
||||
* @param right The RHS vector
|
||||
* @param dest The destination vector, or null if a new vector is to be created
|
||||
* @return left minus right in dest */
|
||||
public static Vector3f sub(Vector3f left, Vector3f right, Vector3f dest)
|
||||
{
|
||||
if (dest == null)
|
||||
return new Vector3f(left.x - right.x, left.y - right.y, left.z - right.z);
|
||||
else
|
||||
{
|
||||
dest.set(left.x - right.x, left.y - right.y, left.z - right.z);
|
||||
return dest;
|
||||
}
|
||||
}
|
||||
|
||||
/** The cross product of two vectors.
|
||||
*
|
||||
* @param left The LHS vector
|
||||
* @param right The RHS vector
|
||||
* @param dest The destination result, or null if a new vector is to be created
|
||||
* @return left cross right */
|
||||
public static Vector3f cross(
|
||||
Vector3f left,
|
||||
Vector3f right,
|
||||
Vector3f dest)
|
||||
{
|
||||
if (dest == null)
|
||||
dest = new Vector3f();
|
||||
dest.set(
|
||||
left.y * right.z - left.z * right.y,
|
||||
right.x * left.z - right.z * left.x,
|
||||
left.x * right.y - left.y * right.x);
|
||||
return dest;
|
||||
}
|
||||
|
||||
/** Negate a vector
|
||||
*
|
||||
* @return this */
|
||||
@Override
|
||||
public Vector negate()
|
||||
{
|
||||
x = -x;
|
||||
y = -y;
|
||||
z = -z;
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Negate a vector and place the result in a destination vector.
|
||||
*
|
||||
* @param dest The destination vector or null if a new vector is to be created
|
||||
* @return the negated vector */
|
||||
public Vector3f negate(Vector3f dest)
|
||||
{
|
||||
if (dest == null)
|
||||
dest = new Vector3f();
|
||||
dest.x = -x;
|
||||
dest.y = -y;
|
||||
dest.z = -z;
|
||||
return dest;
|
||||
}
|
||||
|
||||
public Vector3f mul(float value)
|
||||
{ return new Vector3f(x * value, y * value, z * value); }
|
||||
|
||||
public Vector3f mul(Vector3f vector)
|
||||
{ return new Vector3f(x * vector.x, y * vector.y, z * vector.z); }
|
||||
|
||||
/** Normalise this vector and place the result in another vector.
|
||||
*
|
||||
* @param dest The destination vector, or null if a new vector is to be created
|
||||
* @return the normalised vector */
|
||||
public Vector3f normalise(Vector3f dest)
|
||||
{
|
||||
float l = length();
|
||||
if (dest == null)
|
||||
dest = new Vector3f(x / l, y / l, z / l);
|
||||
else
|
||||
dest.set(x / l, y / l, z / l);
|
||||
return dest;
|
||||
}
|
||||
|
||||
/** The dot product of two vectors is calculated as
|
||||
* v1.x * v2.x + v1.y * v2.y + v1.z * v2.z
|
||||
*
|
||||
* @param left The LHS vector
|
||||
* @param right The RHS vector
|
||||
* @return left dot right */
|
||||
public static float dot(Vector3f left, Vector3f right)
|
||||
{ return left.x * right.x + left.y * right.y + left.z * right.z; }
|
||||
|
||||
/** Calculate the angle between two vectors, in radians
|
||||
*
|
||||
* @param a A vector
|
||||
* @param b The other vector
|
||||
* @return the angle between the two vectors, in radians */
|
||||
public static float angle(Vector3f a, Vector3f b)
|
||||
{
|
||||
float dls = dot(a, b) / (a.length() * b.length());
|
||||
if (dls < -1f)
|
||||
dls = -1f;
|
||||
else if (dls > 1.0f)
|
||||
dls = 1.0f;
|
||||
return (float) Math.acos(dls);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.lwjgl.vector.Vector#load(FloatBuffer)
|
||||
*/
|
||||
@Override
|
||||
public Vector load(FloatBuffer buf)
|
||||
{
|
||||
x = buf.get();
|
||||
y = buf.get();
|
||||
z = buf.get();
|
||||
return this;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.lwjgl.vector.Vector#scale(float)
|
||||
*/
|
||||
@Override
|
||||
public Vector scale(float scale)
|
||||
{
|
||||
x *= scale;
|
||||
y *= scale;
|
||||
z *= scale;
|
||||
return this;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.lwjgl.vector.Vector#store(FloatBuffer)
|
||||
*/
|
||||
@Override
|
||||
public Vector store(FloatBuffer buf)
|
||||
{
|
||||
buf.put(x);
|
||||
buf.put(y);
|
||||
buf.put(z);
|
||||
return this;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder(64);
|
||||
sb.append("Vector3f[");
|
||||
sb.append(x);
|
||||
sb.append(", ");
|
||||
sb.append(y);
|
||||
sb.append(", ");
|
||||
sb.append(z);
|
||||
sb.append(']');
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/** @return x */
|
||||
@Override
|
||||
public final float getX()
|
||||
{ return x; }
|
||||
|
||||
/** @return y */
|
||||
@Override
|
||||
public final float getY()
|
||||
{ return y; }
|
||||
|
||||
/** Set X
|
||||
*
|
||||
* @param x */
|
||||
@Override
|
||||
public final void setX(float x)
|
||||
{ this.x = x; }
|
||||
|
||||
/** Set Y
|
||||
*
|
||||
* @param y */
|
||||
@Override
|
||||
public final void setY(float y)
|
||||
{ this.y = y; }
|
||||
|
||||
/** Set Z
|
||||
*
|
||||
* @param z */
|
||||
@Override
|
||||
public void setZ(float z)
|
||||
{ this.z = z; }
|
||||
|
||||
/* (Overrides)
|
||||
* @see org.lwjgl.vector.ReadableVector3f#getZ()
|
||||
*/
|
||||
@Override
|
||||
public float getZ()
|
||||
{ return z; }
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj)
|
||||
{
|
||||
if (this == obj) return true;
|
||||
if (obj == null) return false;
|
||||
if (getClass() != obj.getClass()) return false;
|
||||
Vector3f other = (Vector3f) obj;
|
||||
if (x == other.x && y == other.y && z == other.z) return true;
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -1,331 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2002-2008 LWJGL Project
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of 'LWJGL' nor the names of
|
||||
* its contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package io.github.hydos.ginger.engine.math.vectors;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.nio.FloatBuffer;
|
||||
|
||||
/** Holds a 4-tuple vector.
|
||||
*
|
||||
* @author cix_foo <cix_foo@users.sourceforge.net>
|
||||
* @version $Revision$
|
||||
* $Id$ */
|
||||
public class Vector4f extends Vector implements Serializable, ReadableVector4f, WritableVector4f
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
public float x, y, z, w;
|
||||
|
||||
/** Constructor for Vector4f. */
|
||||
public Vector4f()
|
||||
{ super(); }
|
||||
|
||||
/** Constructor */
|
||||
public Vector4f(ReadableVector4f src)
|
||||
{ set(src); }
|
||||
|
||||
/** Constructor */
|
||||
public Vector4f(float x, float y, float z, float w)
|
||||
{ set(x, y, z, w); }
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.lwjgl.util.vector.WritableVector2f#set(float, float)
|
||||
*/
|
||||
@Override
|
||||
public void set(float x, float y)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.lwjgl.util.vector.WritableVector3f#set(float, float, float)
|
||||
*/
|
||||
@Override
|
||||
public void set(float x, float y, float z)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.lwjgl.util.vector.WritableVector4f#set(float, float, float, float)
|
||||
*/
|
||||
@Override
|
||||
public void set(float x, float y, float z, float w)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
this.w = w;
|
||||
}
|
||||
|
||||
/** Load from another Vector4f
|
||||
*
|
||||
* @param src The source vector
|
||||
* @return this */
|
||||
public Vector4f set(ReadableVector4f src)
|
||||
{
|
||||
x = src.getX();
|
||||
y = src.getY();
|
||||
z = src.getZ();
|
||||
w = src.getW();
|
||||
return this;
|
||||
}
|
||||
|
||||
/** @return the length squared of the vector */
|
||||
@Override
|
||||
public float lengthSquared()
|
||||
{ return x * x + y * y + z * z + w * w; }
|
||||
|
||||
/** Translate a vector
|
||||
*
|
||||
* @param x The translation in x
|
||||
* @param y the translation in y
|
||||
* @return this */
|
||||
public Vector4f translate(float x, float y, float z, float w)
|
||||
{
|
||||
this.x += x;
|
||||
this.y += y;
|
||||
this.z += z;
|
||||
this.w += w;
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Add a vector to another vector and place the result in a destination
|
||||
* vector.
|
||||
*
|
||||
* @param left The LHS vector
|
||||
* @param right The RHS vector
|
||||
* @param dest The destination vector, or null if a new vector is to be created
|
||||
* @return the sum of left and right in dest */
|
||||
public static Vector4f add(Vector4f left, Vector4f right, Vector4f dest)
|
||||
{
|
||||
if (dest == null)
|
||||
return new Vector4f(left.x + right.x, left.y + right.y, left.z + right.z, left.w + right.w);
|
||||
else
|
||||
{
|
||||
dest.set(left.x + right.x, left.y + right.y, left.z + right.z, left.w + right.w);
|
||||
return dest;
|
||||
}
|
||||
}
|
||||
|
||||
/** Subtract a vector from another vector and place the result in a destination
|
||||
* vector.
|
||||
*
|
||||
* @param left The LHS vector
|
||||
* @param right The RHS vector
|
||||
* @param dest The destination vector, or null if a new vector is to be created
|
||||
* @return left minus right in dest */
|
||||
public static Vector4f sub(Vector4f left, Vector4f right, Vector4f dest)
|
||||
{
|
||||
if (dest == null)
|
||||
return new Vector4f(left.x - right.x, left.y - right.y, left.z - right.z, left.w - right.w);
|
||||
else
|
||||
{
|
||||
dest.set(left.x - right.x, left.y - right.y, left.z - right.z, left.w - right.w);
|
||||
return dest;
|
||||
}
|
||||
}
|
||||
|
||||
/** Negate a vector
|
||||
*
|
||||
* @return this */
|
||||
@Override
|
||||
public Vector negate()
|
||||
{
|
||||
x = -x;
|
||||
y = -y;
|
||||
z = -z;
|
||||
w = -w;
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Negate a vector and place the result in a destination vector.
|
||||
*
|
||||
* @param dest The destination vector or null if a new vector is to be created
|
||||
* @return the negated vector */
|
||||
public Vector4f negate(Vector4f dest)
|
||||
{
|
||||
if (dest == null)
|
||||
dest = new Vector4f();
|
||||
dest.x = -x;
|
||||
dest.y = -y;
|
||||
dest.z = -z;
|
||||
dest.w = -w;
|
||||
return dest;
|
||||
}
|
||||
|
||||
/** Normalise this vector and place the result in another vector.
|
||||
*
|
||||
* @param dest The destination vector, or null if a new vector is to be created
|
||||
* @return the normalised vector */
|
||||
public Vector4f normalise(Vector4f dest)
|
||||
{
|
||||
float l = length();
|
||||
if (dest == null)
|
||||
dest = new Vector4f(x / l, y / l, z / l, w / l);
|
||||
else
|
||||
dest.set(x / l, y / l, z / l, w / l);
|
||||
return dest;
|
||||
}
|
||||
|
||||
/** The dot product of two vectors is calculated as
|
||||
* v1.x * v2.x + v1.y * v2.y + v1.z * v2.z + v1.w * v2.w
|
||||
*
|
||||
* @param left The LHS vector
|
||||
* @param right The RHS vector
|
||||
* @return left dot right */
|
||||
public static float dot(Vector4f left, Vector4f right)
|
||||
{ return left.x * right.x + left.y * right.y + left.z * right.z + left.w * right.w; }
|
||||
|
||||
/** Calculate the angle between two vectors, in radians
|
||||
*
|
||||
* @param a A vector
|
||||
* @param b The other vector
|
||||
* @return the angle between the two vectors, in radians */
|
||||
public static float angle(Vector4f a, Vector4f b)
|
||||
{
|
||||
float dls = dot(a, b) / (a.length() * b.length());
|
||||
if (dls < -1f)
|
||||
dls = -1f;
|
||||
else if (dls > 1.0f)
|
||||
dls = 1.0f;
|
||||
return (float) Math.acos(dls);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.lwjgl.vector.Vector#load(FloatBuffer)
|
||||
*/
|
||||
@Override
|
||||
public Vector load(FloatBuffer buf)
|
||||
{
|
||||
x = buf.get();
|
||||
y = buf.get();
|
||||
z = buf.get();
|
||||
w = buf.get();
|
||||
return this;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.lwjgl.vector.Vector#scale(float)
|
||||
*/
|
||||
@Override
|
||||
public Vector scale(float scale)
|
||||
{
|
||||
x *= scale;
|
||||
y *= scale;
|
||||
z *= scale;
|
||||
w *= scale;
|
||||
return this;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.lwjgl.vector.Vector#store(FloatBuffer)
|
||||
*/
|
||||
@Override
|
||||
public Vector store(FloatBuffer buf)
|
||||
{
|
||||
buf.put(x);
|
||||
buf.put(y);
|
||||
buf.put(z);
|
||||
buf.put(w);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{ return "Vector4f: " + x + " " + y + " " + z + " " + w; }
|
||||
|
||||
/** @return x */
|
||||
@Override
|
||||
public final float getX()
|
||||
{ return x; }
|
||||
|
||||
/** @return y */
|
||||
@Override
|
||||
public final float getY()
|
||||
{ return y; }
|
||||
|
||||
/** Set X
|
||||
*
|
||||
* @param x */
|
||||
@Override
|
||||
public final void setX(float x)
|
||||
{ this.x = x; }
|
||||
|
||||
/** Set Y
|
||||
*
|
||||
* @param y */
|
||||
@Override
|
||||
public final void setY(float y)
|
||||
{ this.y = y; }
|
||||
|
||||
/** Set Z
|
||||
*
|
||||
* @param z */
|
||||
@Override
|
||||
public void setZ(float z)
|
||||
{ this.z = z; }
|
||||
|
||||
/* (Overrides)
|
||||
* @see org.lwjgl.vector.ReadableVector3f#getZ()
|
||||
*/
|
||||
@Override
|
||||
public float getZ()
|
||||
{ return z; }
|
||||
|
||||
/** Set W
|
||||
*
|
||||
* @param w */
|
||||
@Override
|
||||
public void setW(float w)
|
||||
{ this.w = w; }
|
||||
|
||||
/* (Overrides)
|
||||
* @see org.lwjgl.vector.ReadableVector3f#getZ()
|
||||
*/
|
||||
@Override
|
||||
public float getW()
|
||||
{ return w; }
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj)
|
||||
{
|
||||
if (this == obj) return true;
|
||||
if (obj == null) return false;
|
||||
if (getClass() != obj.getClass()) return false;
|
||||
Vector4f other = (Vector4f) obj;
|
||||
if (x == other.x && y == other.y && z == other.z && w == other.w) return true;
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -1,56 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2002-2008 LWJGL Project
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of 'LWJGL' nor the names of
|
||||
* its contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package io.github.hydos.ginger.engine.math.vectors;
|
||||
|
||||
/** Writable interface to Vector2fs
|
||||
*
|
||||
* @author $author$
|
||||
* @version $revision$
|
||||
* $Id$ */
|
||||
public interface WritableVector2f
|
||||
{
|
||||
/** Set the X value
|
||||
*
|
||||
* @param x */
|
||||
void setX(float x);
|
||||
|
||||
/** Set the Y value
|
||||
*
|
||||
* @param y */
|
||||
void setY(float y);
|
||||
|
||||
/** Set the X,Y values
|
||||
*
|
||||
* @param x
|
||||
* @param y */
|
||||
void set(float x, float y);
|
||||
}
|
|
@ -1,52 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2002-2008 LWJGL Project
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of 'LWJGL' nor the names of
|
||||
* its contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package io.github.hydos.ginger.engine.math.vectors;
|
||||
|
||||
/** Writable interface to Vector3fs
|
||||
*
|
||||
* @author $author$
|
||||
* @version $revision$
|
||||
* $Id$ */
|
||||
public interface WritableVector3f extends WritableVector2f
|
||||
{
|
||||
/** Set the Z value
|
||||
*
|
||||
* @param z */
|
||||
void setZ(float z);
|
||||
|
||||
/** Set the X,Y,Z values
|
||||
*
|
||||
* @param x
|
||||
* @param y
|
||||
* @param z */
|
||||
void set(float x, float y, float z);
|
||||
}
|
|
@ -1,53 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2002-2008 LWJGL Project
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of 'LWJGL' nor the names of
|
||||
* its contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package io.github.hydos.ginger.engine.math.vectors;
|
||||
|
||||
/** Writable interface to Vector4fs
|
||||
*
|
||||
* @author $author$
|
||||
* @version $revision$
|
||||
* $Id$ */
|
||||
public interface WritableVector4f extends WritableVector3f
|
||||
{
|
||||
/** Set the W value
|
||||
*
|
||||
* @param w */
|
||||
void setW(float w);
|
||||
|
||||
/** Set the X,Y,Z,W values
|
||||
*
|
||||
* @param x
|
||||
* @param y
|
||||
* @param z
|
||||
* @param w */
|
||||
void set(float x, float y, float z, float w);
|
||||
}
|
|
@ -6,8 +6,8 @@ import java.io.InputStreamReader;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector2f;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class OBJFileLoader
|
||||
{
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package io.github.hydos.ginger.engine.obj;
|
||||
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class Vertex
|
||||
{
|
||||
|
|
|
@ -1,105 +1,103 @@
|
|||
package io.github.hydos.ginger.engine.obj.normals;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector2f;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
import io.github.hydos.ginger.engine.math.Maths;
|
||||
import io.github.hydos.ginger.engine.render.models.RawModel;
|
||||
import io.github.hydos.ginger.engine.utils.Loader;
|
||||
|
||||
public class NormalMappedObjLoader
|
||||
{
|
||||
public static RawModel loadOBJ(String objFileName)
|
||||
{
|
||||
BufferedReader isr = null;
|
||||
isr = new BufferedReader(new InputStreamReader(Class.class.getResourceAsStream("/models/" + objFileName)));
|
||||
BufferedReader reader = new BufferedReader(isr);
|
||||
String line;
|
||||
List<VertexNM> vertices = new ArrayList<VertexNM>();
|
||||
List<Vector2f> textures = new ArrayList<Vector2f>();
|
||||
List<Vector3f> normals = new ArrayList<Vector3f>();
|
||||
List<Integer> indices = new ArrayList<Integer>();
|
||||
try
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
line = reader.readLine();
|
||||
if (line.startsWith("v "))
|
||||
{
|
||||
String[] currentLine = line.split(" ");
|
||||
Vector3f vertex = new Vector3f(Float.valueOf(currentLine[1]),
|
||||
Float.valueOf(currentLine[2]),
|
||||
Float.valueOf(currentLine[3]));
|
||||
VertexNM newVertex = new VertexNM(vertices.size(), vertex);
|
||||
vertices.add(newVertex);
|
||||
}
|
||||
else if (line.startsWith("vt "))
|
||||
{
|
||||
String[] currentLine = line.split(" ");
|
||||
Vector2f texture = new Vector2f(Float.valueOf(currentLine[1]),
|
||||
Float.valueOf(currentLine[2]));
|
||||
textures.add(texture);
|
||||
}
|
||||
else if (line.startsWith("vn "))
|
||||
{
|
||||
String[] currentLine = line.split(" ");
|
||||
Vector3f normal = new Vector3f(Float.valueOf(currentLine[1]),
|
||||
Float.valueOf(currentLine[2]),
|
||||
Float.valueOf(currentLine[3]));
|
||||
normals.add(normal);
|
||||
}
|
||||
else if (line.startsWith("f "))
|
||||
{ break; }
|
||||
}
|
||||
while (line != null && line.startsWith("f "))
|
||||
{
|
||||
String[] currentLine = line.split(" ");
|
||||
String[] vertex1 = currentLine[1].split("/");
|
||||
String[] vertex2 = currentLine[2].split("/");
|
||||
String[] vertex3 = currentLine[3].split("/");
|
||||
VertexNM v0 = processVertex(vertex1, vertices, indices);
|
||||
VertexNM v1 = processVertex(vertex2, vertices, indices);
|
||||
VertexNM v2 = processVertex(vertex3, vertices, indices);
|
||||
calculateTangents(v0, v1, v2, textures);//NEW
|
||||
line = reader.readLine();
|
||||
}
|
||||
reader.close();
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
System.err.println("Error reading the file");
|
||||
}
|
||||
removeUnusedVertices(vertices);
|
||||
float[] verticesArray = new float[vertices.size() * 3];
|
||||
float[] texturesArray = new float[vertices.size() * 2];
|
||||
float[] normalsArray = new float[vertices.size() * 3];
|
||||
float[] tangentsArray = new float[vertices.size() * 3];
|
||||
@SuppressWarnings("unused")
|
||||
//some weird eclipse only error here i think
|
||||
float furthest = convertDataToArrays(vertices, textures, normals, verticesArray,
|
||||
texturesArray, normalsArray, tangentsArray);
|
||||
int[] indicesArray = convertIndicesListToArray(indices);
|
||||
return Loader.loadToVAO(verticesArray, indicesArray, normalsArray, tangentsArray, texturesArray);
|
||||
// BufferedReader isr = null;
|
||||
// isr = new BufferedReader(new InputStreamReader(Class.class.getResourceAsStream("/models/" + objFileName)));
|
||||
// BufferedReader reader = new BufferedReader(isr);
|
||||
// String line;
|
||||
// List<VertexNM> vertices = new ArrayList<VertexNM>();
|
||||
// List<Vector2f> textures = new ArrayList<Vector2f>();
|
||||
// List<Vector3f> normals = new ArrayList<Vector3f>();
|
||||
// List<Integer> indices = new ArrayList<Integer>();
|
||||
// try
|
||||
// {
|
||||
// while (true)
|
||||
// {
|
||||
// line = reader.readLine();
|
||||
// if (line.startsWith("v "))
|
||||
// {
|
||||
// String[] currentLine = line.split(" ");
|
||||
// Vector3f vertex = new Vector3f(Float.valueOf(currentLine[1]),
|
||||
// Float.valueOf(currentLine[2]),
|
||||
// Float.valueOf(currentLine[3]));
|
||||
// VertexNM newVertex = new VertexNM(vertices.size(), vertex);
|
||||
// vertices.add(newVertex);
|
||||
// }
|
||||
// else if (line.startsWith("vt "))
|
||||
// {
|
||||
// String[] currentLine = line.split(" ");
|
||||
// Vector2f texture = new Vector2f(Float.valueOf(currentLine[1]),
|
||||
// Float.valueOf(currentLine[2]));
|
||||
// textures.add(texture);
|
||||
// }
|
||||
// else if (line.startsWith("vn "))
|
||||
// {
|
||||
// String[] currentLine = line.split(" ");
|
||||
// Vector3f normal = new Vector3f(Float.valueOf(currentLine[1]),
|
||||
// Float.valueOf(currentLine[2]),
|
||||
// Float.valueOf(currentLine[3]));
|
||||
// normals.add(normal);
|
||||
// }
|
||||
// else if (line.startsWith("f "))
|
||||
// { break; }
|
||||
// }
|
||||
// while (line != null && line.startsWith("f "))
|
||||
// {
|
||||
// String[] currentLine = line.split(" ");
|
||||
// String[] vertex1 = currentLine[1].split("/");
|
||||
// String[] vertex2 = currentLine[2].split("/");
|
||||
// String[] vertex3 = currentLine[3].split("/");
|
||||
// VertexNM v0 = processVertex(vertex1, vertices, indices);
|
||||
// VertexNM v1 = processVertex(vertex2, vertices, indices);
|
||||
// VertexNM v2 = processVertex(vertex3, vertices, indices);
|
||||
// calculateTangents(v0, v1, v2, textures);//NEW
|
||||
// line = reader.readLine();
|
||||
// }
|
||||
// reader.close();
|
||||
// }
|
||||
// catch (IOException e)
|
||||
// {
|
||||
// System.err.println("Error reading the file");
|
||||
// }
|
||||
// removeUnusedVertices(vertices);
|
||||
// float[] verticesArray = new float[vertices.size() * 3];
|
||||
// float[] texturesArray = new float[vertices.size() * 2];
|
||||
// float[] normalsArray = new float[vertices.size() * 3];
|
||||
// float[] tangentsArray = new float[vertices.size() * 3];
|
||||
// @SuppressWarnings("unused")
|
||||
// //some weird eclipse only error here i think
|
||||
// float furthest = convertDataToArrays(vertices, textures, normals, verticesArray,
|
||||
// texturesArray, normalsArray, tangentsArray);
|
||||
// int[] indicesArray = convertIndicesListToArray(indices);
|
||||
// return Loader.loadToVAO(verticesArray, indicesArray, normalsArray, tangentsArray, texturesArray);
|
||||
return null;
|
||||
}
|
||||
|
||||
private static void calculateTangents(VertexNM v0, VertexNM v1, VertexNM v2, List<Vector2f> textures)
|
||||
{
|
||||
Vector3f delatPos1 = Vector3f.sub(v1.getPosition(), v0.getPosition(), null);
|
||||
Vector3f delatPos2 = Vector3f.sub(v2.getPosition(), v0.getPosition(), null);
|
||||
Vector3f delatPos1 = new Vector3f().sub(v1.getPosition(), v0.getPosition());
|
||||
Vector3f delatPos2 = new Vector3f().sub(v2.getPosition(), v0.getPosition());
|
||||
Vector2f uv0 = textures.get(v0.getTextureIndex());
|
||||
Vector2f uv1 = textures.get(v1.getTextureIndex());
|
||||
Vector2f uv2 = textures.get(v2.getTextureIndex());
|
||||
Vector2f deltaUv1 = Vector2f.sub(uv1, uv0, null);
|
||||
Vector2f deltaUv2 = Vector2f.sub(uv2, uv0, null);
|
||||
Vector2f deltaUv1 = new Vector2f().sub(uv1, uv0);
|
||||
Vector2f deltaUv2 = new Vector2f().sub(uv2, uv0);
|
||||
float r = 1.0f / (deltaUv1.x * deltaUv2.y - deltaUv1.y * deltaUv2.x);
|
||||
delatPos1.scale(deltaUv2.y);
|
||||
delatPos2.scale(deltaUv1.y);
|
||||
Vector3f tangent = Vector3f.sub(delatPos1, delatPos2, null);
|
||||
tangent.scale(r);
|
||||
Maths.scale(delatPos1, deltaUv2.y);
|
||||
Maths.scale(delatPos2, deltaUv1.y);
|
||||
Vector3f tangent = new Vector3f().sub(delatPos1, delatPos2);
|
||||
Maths.scale(tangent, r);
|
||||
v0.addTangent(tangent);
|
||||
v1.addTangent(tangent);
|
||||
v2.addTangent(tangent);
|
||||
|
|
|
@ -3,7 +3,7 @@ package io.github.hydos.ginger.engine.obj.normals;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class VertexNM
|
||||
{
|
||||
|
@ -40,8 +40,8 @@ public class VertexNM
|
|||
if (tangents.isEmpty())
|
||||
{ return; }
|
||||
for (Vector3f tangent : tangents)
|
||||
{ Vector3f.add(averagedTangent, tangent, averagedTangent); }
|
||||
averagedTangent.normalise();
|
||||
{ averagedTangent.add(averagedTangent, tangent); }
|
||||
averagedTangent.normalize();
|
||||
}
|
||||
|
||||
protected Vector3f getAverageTangent()
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
package io.github.hydos.ginger.engine.particle;
|
||||
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
import io.github.hydos.ginger.engine.cameras.ThirdPersonCamera;
|
||||
import io.github.hydos.ginger.engine.io.Window;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector2f;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
import io.github.hydos.ginger.engine.math.Maths;
|
||||
import io.github.hydos.ginger.main.settings.Constants;
|
||||
|
||||
public class Particle
|
||||
|
@ -64,9 +66,9 @@ public class Particle
|
|||
float time = (float) Window.getTime() / 1000000;
|
||||
velocity.y += Constants.gravity * gravityEffect * time;
|
||||
Vector3f change = new Vector3f(velocity);
|
||||
change.scale(time);
|
||||
Vector3f.add(change, position, position);
|
||||
distance = Vector3f.sub(camera.getPosition(), position, null).lengthSquared();
|
||||
Maths.scale(change, time);
|
||||
position.add(change, position);
|
||||
distance = position.sub(camera.getPosition(), position).lengthSquared();
|
||||
elapsedTime += time;
|
||||
updateTextureCoordInfo();
|
||||
return elapsedTime < lifeLength;
|
||||
|
|
|
@ -7,8 +7,9 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
|
||||
import io.github.hydos.ginger.engine.cameras.ThirdPersonCamera;
|
||||
import io.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import io.github.hydos.ginger.engine.render.renderers.ParticleRenderer;
|
||||
|
||||
public class ParticleMaster
|
||||
|
|
|
@ -2,10 +2,12 @@ package io.github.hydos.ginger.engine.particle;
|
|||
|
||||
import java.util.Random;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Vector3f;
|
||||
import org.joml.Vector4f;
|
||||
|
||||
import io.github.hydos.ginger.engine.io.Window;
|
||||
import io.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector4f;
|
||||
import io.github.hydos.ginger.engine.math.Maths;
|
||||
|
||||
public class ParticleSystem
|
||||
{
|
||||
|
@ -76,8 +78,8 @@ public class ParticleSystem
|
|||
{
|
||||
velocity = generateRandomUnitVector();
|
||||
}
|
||||
velocity.normalise();
|
||||
velocity.scale(generateValue(averageSpeed, speedError));
|
||||
velocity.normalize();
|
||||
Maths.scale(velocity, generateValue(averageSpeed, speedError));
|
||||
float scale = generateValue(averageScale, scaleError);
|
||||
float lifeLength = generateValue(averageLifeLength, lifeError);
|
||||
new Particle(texture, new Vector3f(center), velocity, gravityComplient, lifeLength, generateRotation(), new Vector3f(scale, scale, scale));
|
||||
|
@ -113,16 +115,16 @@ public class ParticleSystem
|
|||
Vector4f direction = new Vector4f(x, y, z, 1);
|
||||
if (coneDirection.x != 0 || coneDirection.y != 0 || (coneDirection.z != 1 && coneDirection.z != -1))
|
||||
{
|
||||
Vector3f rotateAxis = Vector3f.cross(coneDirection, new Vector3f(0, 0, 1), null);
|
||||
rotateAxis.normalise();
|
||||
float rotateAngle = (float) Math.acos(Vector3f.dot(coneDirection, new Vector3f(0, 0, 1)));
|
||||
Vector3f rotateAxis = coneDirection.cross(coneDirection, new Vector3f(0, 0, 1));
|
||||
rotateAxis.normalize();
|
||||
float rotateAngle = (float) Math.acos(coneDirection.dot(new Vector3f(0, 0, 1)));
|
||||
Matrix4f rotationMatrix = new Matrix4f();
|
||||
rotationMatrix.rotate(-rotateAngle, rotateAxis);
|
||||
Matrix4f.transform(rotationMatrix, direction, direction);
|
||||
rotationMatrix.transform(direction, direction);
|
||||
}
|
||||
else if (coneDirection.z == -1)
|
||||
{ direction.z *= -1; }
|
||||
return new Vector3f(direction);
|
||||
return Maths.Vec4ToVec3(direction);
|
||||
}
|
||||
|
||||
private Vector3f generateRandomUnitVector()
|
||||
|
|
|
@ -7,7 +7,7 @@ import org.lwjgl.opengl.GL12;
|
|||
import org.lwjgl.opengl.GL14;
|
||||
import org.lwjgl.opengl.GL30;
|
||||
|
||||
import io.github.hydos.ginger.engine.io.Window;
|
||||
import com.github.halotroop.litecraft.LiteCraftMain;
|
||||
|
||||
public class Fbo
|
||||
{
|
||||
|
@ -63,7 +63,7 @@ public class Fbo
|
|||
public void unbindFrameBuffer()
|
||||
{
|
||||
GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);
|
||||
GL11.glViewport(0, 0, Window.width, Window.height);
|
||||
GL11.glViewport(0, 0, LiteCraftMain.width, LiteCraftMain.height);
|
||||
}
|
||||
|
||||
/** Binds the current FBO to be read from (not used in tutorial 43). */
|
||||
|
|
|
@ -5,16 +5,18 @@ import java.util.HashMap;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Vector4f;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.GL13;
|
||||
|
||||
import com.github.halotroop.litecraft.LiteCraftMain;
|
||||
|
||||
import io.github.hydos.ginger.engine.cameras.ThirdPersonCamera;
|
||||
import io.github.hydos.ginger.engine.elements.GuiTexture;
|
||||
import io.github.hydos.ginger.engine.elements.objects.Entity;
|
||||
import io.github.hydos.ginger.engine.elements.objects.Light;
|
||||
import io.github.hydos.ginger.engine.io.Window;
|
||||
import io.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector4f;
|
||||
import io.github.hydos.ginger.engine.render.models.TexturedModel;
|
||||
import io.github.hydos.ginger.engine.render.renderers.EntityRenderer;
|
||||
import io.github.hydos.ginger.engine.render.renderers.GuiRenderer;
|
||||
|
@ -175,16 +177,16 @@ public class MasterRenderer
|
|||
private void createProjectionMatrix()
|
||||
{
|
||||
projectionMatrix = new Matrix4f();
|
||||
float aspectRatio = (float) Window.width / (float) Window.height;
|
||||
float aspectRatio = (float) LiteCraftMain.width / (float) LiteCraftMain.height;
|
||||
float y_scale = (float) ((1f / Math.tan(Math.toRadians(FOV / 2f))));
|
||||
float x_scale = y_scale / aspectRatio;
|
||||
float frustum_length = FAR_PLANE - NEAR_PLANE;
|
||||
projectionMatrix.m00 = x_scale;
|
||||
projectionMatrix.m11 = y_scale;
|
||||
projectionMatrix.m22 = -((FAR_PLANE + NEAR_PLANE) / frustum_length);
|
||||
projectionMatrix.m23 = -1;
|
||||
projectionMatrix.m32 = -((2 * NEAR_PLANE * FAR_PLANE) / frustum_length);
|
||||
projectionMatrix.m33 = 0;
|
||||
projectionMatrix.m00(x_scale);
|
||||
projectionMatrix.m11(y_scale);
|
||||
projectionMatrix.m22(-((FAR_PLANE + NEAR_PLANE) / frustum_length));
|
||||
projectionMatrix.m23(-1);
|
||||
projectionMatrix.m32(-((2 * NEAR_PLANE * FAR_PLANE) / frustum_length));
|
||||
projectionMatrix.m33(0);
|
||||
}
|
||||
|
||||
public void renderGui(GuiTexture guiTexture)
|
||||
|
|
|
@ -3,6 +3,7 @@ package io.github.hydos.ginger.engine.render.renderers;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.GL13;
|
||||
import org.lwjgl.opengl.GL20;
|
||||
|
@ -10,7 +11,6 @@ import org.lwjgl.opengl.GL30;
|
|||
|
||||
import io.github.hydos.ginger.engine.elements.objects.Entity;
|
||||
import io.github.hydos.ginger.engine.math.Maths;
|
||||
import io.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import io.github.hydos.ginger.engine.render.MasterRenderer;
|
||||
import io.github.hydos.ginger.engine.render.models.RawModel;
|
||||
import io.github.hydos.ginger.engine.render.models.TexturedModel;
|
||||
|
|
|
@ -2,6 +2,7 @@ package io.github.hydos.ginger.engine.render.renderers;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.GL13;
|
||||
import org.lwjgl.opengl.GL20;
|
||||
|
@ -9,7 +10,6 @@ import org.lwjgl.opengl.GL30;
|
|||
|
||||
import io.github.hydos.ginger.engine.elements.GuiTexture;
|
||||
import io.github.hydos.ginger.engine.math.Maths;
|
||||
import io.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import io.github.hydos.ginger.engine.render.models.RawModel;
|
||||
import io.github.hydos.ginger.engine.render.shaders.GuiShader;
|
||||
import io.github.hydos.ginger.engine.utils.Loader;
|
||||
|
|
|
@ -3,6 +3,8 @@ package io.github.hydos.ginger.engine.render.renderers;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Vector4f;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.GL13;
|
||||
import org.lwjgl.opengl.GL20;
|
||||
|
@ -13,8 +15,6 @@ import io.github.hydos.ginger.engine.elements.objects.Entity;
|
|||
import io.github.hydos.ginger.engine.elements.objects.Light;
|
||||
import io.github.hydos.ginger.engine.io.Window;
|
||||
import io.github.hydos.ginger.engine.math.Maths;
|
||||
import io.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector4f;
|
||||
import io.github.hydos.ginger.engine.render.MasterRenderer;
|
||||
import io.github.hydos.ginger.engine.render.models.RawModel;
|
||||
import io.github.hydos.ginger.engine.render.models.TexturedModel;
|
||||
|
|
|
@ -4,6 +4,8 @@ import java.nio.FloatBuffer;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Vector3f;
|
||||
import org.lwjgl.BufferUtils;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.GL13;
|
||||
|
@ -13,8 +15,6 @@ import org.lwjgl.opengl.GL31;
|
|||
|
||||
import io.github.hydos.ginger.engine.cameras.ThirdPersonCamera;
|
||||
import io.github.hydos.ginger.engine.math.Maths;
|
||||
import io.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
import io.github.hydos.ginger.engine.particle.Particle;
|
||||
import io.github.hydos.ginger.engine.particle.ParticleTexture;
|
||||
import io.github.hydos.ginger.engine.render.models.RawModel;
|
||||
|
@ -94,40 +94,41 @@ public class ParticleRenderer
|
|||
private void updateModelViewMatrix(Vector3f position, float rotation, float scale, Matrix4f viewMatrix, float[] vboData)
|
||||
{
|
||||
Matrix4f modelMatrix = new Matrix4f();
|
||||
Matrix4f.translate(position, modelMatrix, modelMatrix);
|
||||
modelMatrix.m00 = viewMatrix.m00;
|
||||
modelMatrix.m01 = viewMatrix.m10;
|
||||
modelMatrix.m02 = viewMatrix.m20;
|
||||
modelMatrix.m10 = viewMatrix.m01;
|
||||
modelMatrix.m11 = viewMatrix.m11;
|
||||
modelMatrix.m12 = viewMatrix.m21;
|
||||
modelMatrix.m20 = viewMatrix.m02;
|
||||
modelMatrix.m21 = viewMatrix.m12;
|
||||
modelMatrix.m22 = viewMatrix.m22;
|
||||
Matrix4f.rotate((float) Math.toRadians(rotation), new Vector3f(0, 0, 1), modelMatrix, modelMatrix);
|
||||
Matrix4f.scale(new Vector3f(scale, scale, scale), modelMatrix, modelMatrix);
|
||||
Matrix4f modelViewMatrix = Matrix4f.mul(viewMatrix, modelMatrix, null);
|
||||
modelMatrix.translate(position, modelMatrix);
|
||||
modelMatrix.m00(viewMatrix.m00());
|
||||
modelMatrix.m01(viewMatrix.m10());
|
||||
modelMatrix.m02(viewMatrix.m20());
|
||||
modelMatrix.m10(viewMatrix.m01());
|
||||
modelMatrix.m11(viewMatrix.m11());
|
||||
modelMatrix.m12(viewMatrix.m21());
|
||||
modelMatrix.m20(viewMatrix.m02());
|
||||
modelMatrix.m21(viewMatrix.m12());
|
||||
modelMatrix.m22(viewMatrix.m22());
|
||||
modelMatrix.rotate((float) Math.toRadians(rotation), new Vector3f(0, 0, 1), modelMatrix);
|
||||
modelMatrix.scale(new Vector3f(scale, scale, scale), modelMatrix);
|
||||
Matrix4f modelViewMatrix = new Matrix4f();
|
||||
modelViewMatrix.mul(viewMatrix, modelMatrix);
|
||||
storeMatrixData(modelViewMatrix, vboData);
|
||||
}
|
||||
|
||||
private void storeMatrixData(Matrix4f matrix, float[] vboData)
|
||||
{
|
||||
vboData[pointer++] = matrix.m00;
|
||||
vboData[pointer++] = matrix.m01;
|
||||
vboData[pointer++] = matrix.m02;
|
||||
vboData[pointer++] = matrix.m03;
|
||||
vboData[pointer++] = matrix.m10;
|
||||
vboData[pointer++] = matrix.m11;
|
||||
vboData[pointer++] = matrix.m12;
|
||||
vboData[pointer++] = matrix.m13;
|
||||
vboData[pointer++] = matrix.m20;
|
||||
vboData[pointer++] = matrix.m21;
|
||||
vboData[pointer++] = matrix.m22;
|
||||
vboData[pointer++] = matrix.m23;
|
||||
vboData[pointer++] = matrix.m30;
|
||||
vboData[pointer++] = matrix.m31;
|
||||
vboData[pointer++] = matrix.m32;
|
||||
vboData[pointer++] = matrix.m33;
|
||||
vboData[pointer++] = matrix.m00();
|
||||
vboData[pointer++] = matrix.m01();
|
||||
vboData[pointer++] = matrix.m02();
|
||||
vboData[pointer++] = matrix.m03();
|
||||
vboData[pointer++] = matrix.m10();
|
||||
vboData[pointer++] = matrix.m11();
|
||||
vboData[pointer++] = matrix.m12();
|
||||
vboData[pointer++] = matrix.m13();
|
||||
vboData[pointer++] = matrix.m20();
|
||||
vboData[pointer++] = matrix.m21();
|
||||
vboData[pointer++] = matrix.m22();
|
||||
vboData[pointer++] = matrix.m23();
|
||||
vboData[pointer++] = matrix.m30();
|
||||
vboData[pointer++] = matrix.m31();
|
||||
vboData[pointer++] = matrix.m32();
|
||||
vboData[pointer++] = matrix.m33();
|
||||
}
|
||||
|
||||
private void prepare()
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
package io.github.hydos.ginger.engine.render.renderers;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.GL13;
|
||||
import org.lwjgl.opengl.GL20;
|
||||
import org.lwjgl.opengl.GL30;
|
||||
|
||||
import io.github.hydos.ginger.engine.cameras.ThirdPersonCamera;
|
||||
import io.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import io.github.hydos.ginger.engine.render.models.RawModel;
|
||||
import io.github.hydos.ginger.engine.render.shaders.SkyboxShader;
|
||||
import io.github.hydos.ginger.engine.utils.Loader;
|
||||
|
|
|
@ -2,14 +2,14 @@ package io.github.hydos.ginger.engine.render.renderers;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Vector3f;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.GL13;
|
||||
import org.lwjgl.opengl.GL20;
|
||||
import org.lwjgl.opengl.GL30;
|
||||
|
||||
import io.github.hydos.ginger.engine.math.Maths;
|
||||
import io.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
import io.github.hydos.ginger.engine.render.models.RawModel;
|
||||
import io.github.hydos.ginger.engine.render.shaders.TerrainShader;
|
||||
import io.github.hydos.ginger.engine.terrain.Terrain;
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
package io.github.hydos.ginger.engine.render.shaders;
|
||||
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
import io.github.hydos.ginger.engine.font.GUIText;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector2f;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
|
||||
public class FontShader extends ShaderProgram
|
||||
{
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package io.github.hydos.ginger.engine.render.shaders;
|
||||
|
||||
import io.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import org.joml.Matrix4f;
|
||||
|
||||
public class GuiShader extends ShaderProgram
|
||||
{
|
||||
|
|
|
@ -2,11 +2,13 @@ package io.github.hydos.ginger.engine.render.shaders;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector3f;
|
||||
import org.joml.Vector4f;
|
||||
|
||||
import io.github.hydos.ginger.engine.elements.objects.Light;
|
||||
import io.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector2f;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector4f;
|
||||
import io.github.hydos.ginger.engine.math.Maths;
|
||||
|
||||
public class NormalMappingShader extends ShaderProgram
|
||||
{
|
||||
|
@ -121,8 +123,8 @@ public class NormalMappingShader extends ShaderProgram
|
|||
{
|
||||
Vector3f position = light.getPosition();
|
||||
Vector4f eyeSpacePos = new Vector4f(position.x, position.y, position.z, 1f);
|
||||
Matrix4f.transform(viewMatrix, eyeSpacePos, eyeSpacePos);
|
||||
return new Vector3f(eyeSpacePos);
|
||||
// eyeSpacePos no transform method :p
|
||||
return Maths.Vec4ToVec3(eyeSpacePos);
|
||||
}
|
||||
|
||||
public void loadSkyColour(Vector3f colour)
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package io.github.hydos.ginger.engine.render.shaders;
|
||||
|
||||
import io.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import org.joml.Matrix4f;
|
||||
|
||||
public class ParticleShader extends ShaderProgram
|
||||
{
|
||||
|
|
|
@ -5,15 +5,14 @@ import java.io.IOException;
|
|||
import java.io.InputStreamReader;
|
||||
import java.nio.FloatBuffer;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector3f;
|
||||
import org.joml.Vector4f;
|
||||
import org.lwjgl.BufferUtils;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.GL20;
|
||||
|
||||
import io.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector2f;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector4f;
|
||||
|
||||
public abstract class ShaderProgram
|
||||
{
|
||||
private int programID;
|
||||
|
@ -85,7 +84,7 @@ public abstract class ShaderProgram
|
|||
|
||||
protected void loadMatrix(int location, Matrix4f matrix)
|
||||
{
|
||||
matrix.store(matrixBuffer);
|
||||
matrix = new Matrix4f(matrixBuffer);
|
||||
matrixBuffer.flip();
|
||||
GL20.glUniformMatrix4fv(location, false, matrixBuffer);
|
||||
}
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
package io.github.hydos.ginger.engine.render.shaders;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
|
||||
import io.github.hydos.ginger.engine.cameras.ThirdPersonCamera;
|
||||
import io.github.hydos.ginger.engine.math.Maths;
|
||||
import io.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
|
||||
public class SkyboxShader extends ShaderProgram
|
||||
{
|
||||
private int location_projectionMatrix;
|
||||
|
@ -17,10 +17,11 @@ public class SkyboxShader extends ShaderProgram
|
|||
|
||||
public void loadViewMatrix(ThirdPersonCamera camera)
|
||||
{
|
||||
//TODO: make a vector3f
|
||||
Matrix4f matrix = Maths.createViewMatrix(camera);
|
||||
matrix.m30 = 0;
|
||||
matrix.m31 = 0;
|
||||
matrix.m32 = 0;
|
||||
matrix.m30(0);
|
||||
matrix.m31(0);
|
||||
matrix.m32(0);
|
||||
super.loadMatrix(location_viewMatrix, matrix);
|
||||
}
|
||||
|
||||
|
|
|
@ -2,11 +2,13 @@ package io.github.hydos.ginger.engine.render.shaders;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
import io.github.hydos.ginger.engine.cameras.ThirdPersonCamera;
|
||||
import io.github.hydos.ginger.engine.elements.objects.Light;
|
||||
import io.github.hydos.ginger.engine.math.Maths;
|
||||
import io.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
|
||||
|
||||
public class StaticShader extends ShaderProgram
|
||||
{
|
||||
|
|
|
@ -2,11 +2,12 @@ package io.github.hydos.ginger.engine.render.shaders;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
import io.github.hydos.ginger.engine.cameras.ThirdPersonCamera;
|
||||
import io.github.hydos.ginger.engine.elements.objects.Light;
|
||||
import io.github.hydos.ginger.engine.math.Maths;
|
||||
import io.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
|
||||
public class TerrainShader extends ShaderProgram
|
||||
{
|
||||
|
|
|
@ -1,12 +1,15 @@
|
|||
package io.github.hydos.ginger.engine.render.tools;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector3f;
|
||||
import org.joml.Vector4f;
|
||||
|
||||
import com.github.halotroop.litecraft.LiteCraftMain;
|
||||
|
||||
import io.github.hydos.ginger.engine.cameras.ThirdPersonCamera;
|
||||
import io.github.hydos.ginger.engine.io.Window;
|
||||
import io.github.hydos.ginger.engine.math.Maths;
|
||||
import io.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector2f;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector4f;
|
||||
import io.github.hydos.ginger.engine.terrain.Terrain;
|
||||
|
||||
public class MousePicker
|
||||
|
@ -51,7 +54,7 @@ public class MousePicker
|
|||
private Vector3f calculateMouseRay()
|
||||
{
|
||||
float mouseX = (float) Window.getMouseX();
|
||||
float mouseY = (float) (Window.height - Window.getMouseY());
|
||||
float mouseY = (float) (LiteCraftMain.height - Window.getMouseY());
|
||||
Vector2f normalizedCoords = getNormalisedDeviceCoordinates(mouseX, mouseY);
|
||||
Vector4f clipCoords = new Vector4f(normalizedCoords.x, normalizedCoords.y, -1.0f, 1.0f);
|
||||
Vector4f eyeCoords = toEyeCoords(clipCoords);
|
||||
|
@ -61,34 +64,35 @@ public class MousePicker
|
|||
|
||||
private Vector3f toWorldCoords(Vector4f eyeCoords)
|
||||
{
|
||||
Matrix4f invertedView = Matrix4f.invert(viewMatrix, null);
|
||||
Vector4f rayWorld = Matrix4f.transform(invertedView, eyeCoords, null);
|
||||
Matrix4f invertedView = viewMatrix.invert(viewMatrix);
|
||||
Vector4f rayWorld = invertedView.transform(eyeCoords);
|
||||
Vector3f mouseRay = new Vector3f(rayWorld.x, rayWorld.y, rayWorld.z);
|
||||
mouseRay.normalise();
|
||||
mouseRay.normalize();
|
||||
return mouseRay;
|
||||
}
|
||||
|
||||
private Vector4f toEyeCoords(Vector4f clipCoords)
|
||||
{
|
||||
Matrix4f invertedProjection = Matrix4f.invert(projectionMatrix, null);
|
||||
Vector4f eyeCoords = Matrix4f.transform(invertedProjection, clipCoords, null);
|
||||
Matrix4f invertedProjection = projectionMatrix.invert(projectionMatrix);
|
||||
Vector4f eyeCoords = invertedProjection.transform(clipCoords);
|
||||
return new Vector4f(eyeCoords.x, eyeCoords.y, -1f, 0f);
|
||||
}
|
||||
|
||||
private Vector2f getNormalisedDeviceCoordinates(float mouseX, float mouseY)
|
||||
{
|
||||
float x = (2.0f * mouseX) / Window.width - 1f;
|
||||
float y = (2.0f * mouseY) / Window.height - 1f;
|
||||
float x = (2.0f * mouseX) / LiteCraftMain.width - 1f;
|
||||
float y = (2.0f * mouseY) / LiteCraftMain.height - 1f;
|
||||
return new Vector2f(x, y);
|
||||
}
|
||||
//**********************************************************
|
||||
|
||||
private Vector3f getPointOnRay(Vector3f ray, float distance)
|
||||
{
|
||||
Vector3f output = new Vector3f();
|
||||
Vector3f camPos = camera.getPosition();
|
||||
Vector3f start = new Vector3f(camPos.x, camPos.y, camPos.z);
|
||||
Vector3f scaledRay = new Vector3f(ray.x * distance, ray.y * distance, ray.z * distance);
|
||||
return Vector3f.add(start, scaledRay, null);
|
||||
return output.add(start, scaledRay);
|
||||
}
|
||||
|
||||
private Vector3f binarySearch(int count, float start, float finish, Vector3f ray)
|
||||
|
@ -97,7 +101,7 @@ public class MousePicker
|
|||
if (count >= RECURSION_COUNT)
|
||||
{
|
||||
Vector3f endPoint = getPointOnRay(ray, half);
|
||||
Terrain terrain = getTerrain(endPoint.getX(), endPoint.getZ());
|
||||
Terrain terrain = getTerrain(endPoint.x, endPoint.z);
|
||||
if (terrain != null)
|
||||
{
|
||||
return endPoint;
|
||||
|
@ -133,10 +137,10 @@ public class MousePicker
|
|||
|
||||
private boolean isUnderGround(Vector3f testPoint)
|
||||
{
|
||||
Terrain terrain = getTerrain(testPoint.getX(), testPoint.getZ());
|
||||
Terrain terrain = getTerrain(testPoint.x(), testPoint.z());
|
||||
float height = 0;
|
||||
if (terrain != null)
|
||||
{ height = terrain.getHeightOfTerrain(testPoint.getX(), testPoint.getZ()); }
|
||||
{ height = terrain.getHeightOfTerrain(testPoint.x(), testPoint.z()); }
|
||||
if (testPoint.y < height)
|
||||
{
|
||||
return true;
|
||||
|
|
|
@ -1,10 +1,13 @@
|
|||
package io.github.hydos.ginger.engine.shadow;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Vector3f;
|
||||
import org.joml.Vector4f;
|
||||
|
||||
import com.github.halotroop.litecraft.LiteCraftMain;
|
||||
|
||||
import io.github.hydos.ginger.engine.cameras.ThirdPersonCamera;
|
||||
import io.github.hydos.ginger.engine.io.Window;
|
||||
import io.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector4f;
|
||||
import io.github.hydos.ginger.engine.math.Maths;
|
||||
import io.github.hydos.ginger.engine.render.MasterRenderer;
|
||||
|
||||
/** Represents the 3D cuboidal area of the world in which objects will cast
|
||||
|
@ -53,13 +56,13 @@ public class ShadowBox
|
|||
protected void update()
|
||||
{
|
||||
Matrix4f rotation = calculateCameraRotationMatrix();
|
||||
Vector3f forwardVector = new Vector3f(Matrix4f.transform(rotation, FORWARD, null));
|
||||
Vector3f forwardVector = Maths.Vec4ToVec3(rotation.transform(FORWARD));
|
||||
Vector3f toFar = new Vector3f(forwardVector);
|
||||
toFar.scale(SHADOW_DISTANCE);
|
||||
Maths.scale(toFar, SHADOW_DISTANCE);
|
||||
Vector3f toNear = new Vector3f(forwardVector);
|
||||
toNear.scale(MasterRenderer.NEAR_PLANE);
|
||||
Vector3f centerNear = Vector3f.add(toNear, cam.getPosition(), null);
|
||||
Vector3f centerFar = Vector3f.add(toFar, cam.getPosition(), null);
|
||||
Maths.scale(toNear, MasterRenderer.NEAR_PLANE);
|
||||
Vector3f centerNear = new Vector3f().add(toNear, cam.getPosition());
|
||||
Vector3f centerFar = new Vector3f().add(toFar, cam.getPosition());
|
||||
Vector4f[] points = calculateFrustumVertices(rotation, forwardVector, centerNear,
|
||||
centerFar);
|
||||
boolean first = true;
|
||||
|
@ -109,8 +112,8 @@ public class ShadowBox
|
|||
float z = (minZ + maxZ) / 2f;
|
||||
Vector4f cen = new Vector4f(x, y, z, 1);
|
||||
Matrix4f invertedLight = new Matrix4f();
|
||||
Matrix4f.invert(lightViewMatrix, invertedLight);
|
||||
return new Vector3f(Matrix4f.transform(invertedLight, cen, null));
|
||||
invertedLight = new Matrix4f().invert(lightViewMatrix);
|
||||
return Maths.Vec4ToVec3(invertedLight.transform(cen));
|
||||
}
|
||||
|
||||
/** @return The width of the "view cuboid" (orthographic projection area). */
|
||||
|
@ -142,18 +145,20 @@ public class ShadowBox
|
|||
private Vector4f[] calculateFrustumVertices(Matrix4f rotation, Vector3f forwardVector,
|
||||
Vector3f centerNear, Vector3f centerFar)
|
||||
{
|
||||
Vector3f upVector = new Vector3f(Matrix4f.transform(rotation, UP, null));
|
||||
Vector3f rightVector = Vector3f.cross(forwardVector, upVector, null);
|
||||
Matrix4f upMatrix = rotation;
|
||||
|
||||
Vector3f upVector = Maths.Vec4ToVec3(upMatrix.transform(UP));
|
||||
Vector3f rightVector = new Vector3f().cross(forwardVector, upVector);
|
||||
Vector3f downVector = new Vector3f(-upVector.x, -upVector.y, -upVector.z);
|
||||
Vector3f leftVector = new Vector3f(-rightVector.x, -rightVector.y, -rightVector.z);
|
||||
Vector3f farTop = Vector3f.add(centerFar, new Vector3f(upVector.x * farHeight,
|
||||
upVector.y * farHeight, upVector.z * farHeight), null);
|
||||
Vector3f farBottom = Vector3f.add(centerFar, new Vector3f(downVector.x * farHeight,
|
||||
downVector.y * farHeight, downVector.z * farHeight), null);
|
||||
Vector3f nearTop = Vector3f.add(centerNear, new Vector3f(upVector.x * nearHeight,
|
||||
upVector.y * nearHeight, upVector.z * nearHeight), null);
|
||||
Vector3f nearBottom = Vector3f.add(centerNear, new Vector3f(downVector.x * nearHeight,
|
||||
downVector.y * nearHeight, downVector.z * nearHeight), null);
|
||||
Vector3f farTop = new Vector3f().add(centerFar, new Vector3f(upVector.x * farHeight,
|
||||
upVector.y * farHeight, upVector.z * farHeight));
|
||||
Vector3f farBottom = new Vector3f().add(centerFar, new Vector3f(downVector.x * farHeight,
|
||||
downVector.y * farHeight, downVector.z * farHeight));
|
||||
Vector3f nearTop = new Vector3f().add(centerNear, new Vector3f(upVector.x * nearHeight,
|
||||
upVector.y * nearHeight, upVector.z * nearHeight));
|
||||
Vector3f nearBottom = new Vector3f().add(centerNear, new Vector3f(downVector.x * nearHeight,
|
||||
downVector.y * nearHeight, downVector.z * nearHeight));
|
||||
Vector4f[] points = new Vector4f[8];
|
||||
points[0] = calculateLightSpaceFrustumCorner(farTop, rightVector, farWidth);
|
||||
points[1] = calculateLightSpaceFrustumCorner(farTop, leftVector, farWidth);
|
||||
|
@ -179,10 +184,9 @@ public class ShadowBox
|
|||
private Vector4f calculateLightSpaceFrustumCorner(Vector3f startPoint, Vector3f direction,
|
||||
float width)
|
||||
{
|
||||
Vector3f point = Vector3f.add(startPoint,
|
||||
new Vector3f(direction.x * width, direction.y * width, direction.z * width), null);
|
||||
Vector3f point = new Vector3f().add(startPoint, new Vector3f(direction.x * width, direction.y * width, direction.z * width));
|
||||
Vector4f point4f = new Vector4f(point.x, point.y, point.z, 1f);
|
||||
Matrix4f.transform(lightViewMatrix, point4f, point4f);
|
||||
//TODO: may be lighting bug due to .translate missing from vector4f
|
||||
return point4f;
|
||||
}
|
||||
|
||||
|
@ -211,5 +215,5 @@ public class ShadowBox
|
|||
|
||||
/** @return The aspect ratio of the display (width:height ratio). */
|
||||
private float getAspectRatio()
|
||||
{ return (float) Window.width / (float) Window.height; }
|
||||
{ return (float) LiteCraftMain.width / (float) LiteCraftMain.height; }
|
||||
}
|
||||
|
|
|
@ -8,6 +8,8 @@ import org.lwjgl.opengl.GL14;
|
|||
import org.lwjgl.opengl.GL30;
|
||||
import org.lwjgl.opengl.GL32;
|
||||
|
||||
import com.github.halotroop.litecraft.LiteCraftMain;
|
||||
|
||||
import io.github.hydos.ginger.engine.io.Window;
|
||||
|
||||
/** The frame buffer for the shadow pass. This class sets up the depth texture
|
||||
|
@ -49,7 +51,7 @@ public class ShadowFrameBuffer
|
|||
protected void unbindFrameBuffer()
|
||||
{
|
||||
GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);
|
||||
GL11.glViewport(0, 0, Window.width, Window.height);
|
||||
GL11.glViewport(0, 0, LiteCraftMain.width, LiteCraftMain.height);
|
||||
}
|
||||
|
||||
/** @return The ID of the shadow map texture. */
|
||||
|
|
|
@ -3,6 +3,7 @@ package io.github.hydos.ginger.engine.shadow;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.GL13;
|
||||
import org.lwjgl.opengl.GL20;
|
||||
|
@ -10,7 +11,6 @@ import org.lwjgl.opengl.GL30;
|
|||
|
||||
import io.github.hydos.ginger.engine.elements.objects.Entity;
|
||||
import io.github.hydos.ginger.engine.math.Maths;
|
||||
import io.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import io.github.hydos.ginger.engine.render.MasterRenderer;
|
||||
import io.github.hydos.ginger.engine.render.models.RawModel;
|
||||
import io.github.hydos.ginger.engine.render.models.TexturedModel;
|
||||
|
@ -85,7 +85,7 @@ public class ShadowMapEntityRenderer
|
|||
{
|
||||
Matrix4f modelMatrix = Maths.createTransformationMatrix(entity.getPosition(),
|
||||
entity.getRotX(), entity.getRotY(), entity.getRotZ(), entity.getScale());
|
||||
Matrix4f mvpMatrix = Matrix4f.mul(projectionViewMatrix, modelMatrix, null);
|
||||
Matrix4f mvpMatrix = new Matrix4f().mul(projectionViewMatrix, modelMatrix);
|
||||
shader.loadMvpMatrix(mvpMatrix);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,14 +3,14 @@ package io.github.hydos.ginger.engine.shadow;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector3f;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
import io.github.hydos.ginger.engine.cameras.ThirdPersonCamera;
|
||||
import io.github.hydos.ginger.engine.elements.objects.Entity;
|
||||
import io.github.hydos.ginger.engine.elements.objects.Light;
|
||||
import io.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector2f;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
import io.github.hydos.ginger.engine.render.models.TexturedModel;
|
||||
|
||||
/** This class is in charge of using all of the classes in the shadows package to
|
||||
|
@ -76,7 +76,7 @@ public class ShadowMapMasterRenderer
|
|||
*
|
||||
* @return The to-shadow-map-space matrix. */
|
||||
public Matrix4f getToShadowMapSpaceMatrix()
|
||||
{ return Matrix4f.mul(offset, projectionViewMatrix, null); }
|
||||
{ return new Matrix4f().mul(offset, projectionViewMatrix); }
|
||||
|
||||
/** Clean up the shader and FBO on closing. */
|
||||
public void cleanUp()
|
||||
|
@ -117,7 +117,7 @@ public class ShadowMapMasterRenderer
|
|||
{
|
||||
updateOrthoProjectionMatrix(box.getWidth(), box.getHeight(), box.getLength());
|
||||
updateLightViewMatrix(lightDirection, box.getCenter());
|
||||
Matrix4f.mul(projectionMatrix, lightViewMatrix, projectionViewMatrix);
|
||||
projectionViewMatrix.mul(projectionMatrix, lightViewMatrix);
|
||||
shadowFbo.bindFrameBuffer();
|
||||
GL11.glEnable(GL11.GL_DEPTH_TEST);
|
||||
GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT);
|
||||
|
@ -147,16 +147,15 @@ public class ShadowMapMasterRenderer
|
|||
* - the center of the "view cuboid" in world space. */
|
||||
private void updateLightViewMatrix(Vector3f direction, Vector3f center)
|
||||
{
|
||||
direction.normalise();
|
||||
direction.normalize();
|
||||
center.negate();
|
||||
lightViewMatrix.setIdentity();
|
||||
lightViewMatrix.identity();
|
||||
float pitch = (float) Math.acos(new Vector2f(direction.x, direction.z).length());
|
||||
Matrix4f.rotate(pitch, new Vector3f(1, 0, 0), lightViewMatrix, lightViewMatrix);
|
||||
lightViewMatrix.rotate(pitch, new Vector3f(1, 0, 0), lightViewMatrix);
|
||||
float yaw = (float) Math.toDegrees(((float) Math.atan(direction.x / direction.z)));
|
||||
yaw = direction.z > 0 ? yaw - 180 : yaw;
|
||||
Matrix4f.rotate((float) -Math.toRadians(yaw), new Vector3f(0, 1, 0), lightViewMatrix,
|
||||
lightViewMatrix);
|
||||
Matrix4f.translate(center, lightViewMatrix, lightViewMatrix);
|
||||
lightViewMatrix.rotate((float) -Math.toRadians(yaw), new Vector3f(0, 1, 0), lightViewMatrix);
|
||||
lightViewMatrix.translate(center, lightViewMatrix);
|
||||
}
|
||||
|
||||
/** Creates the orthographic projection matrix. This projection matrix
|
||||
|
@ -171,11 +170,11 @@ public class ShadowMapMasterRenderer
|
|||
* - shadow box length. */
|
||||
private void updateOrthoProjectionMatrix(float width, float height, float length)
|
||||
{
|
||||
projectionMatrix.setIdentity();
|
||||
projectionMatrix.m00 = 2f / width;
|
||||
projectionMatrix.m11 = 2f / height;
|
||||
projectionMatrix.m22 = -2f / length;
|
||||
projectionMatrix.m33 = 1;
|
||||
projectionMatrix.identity();
|
||||
projectionMatrix.m00(2f / width);
|
||||
projectionMatrix.m11(2f / height);
|
||||
projectionMatrix.m22(-2f / length);
|
||||
projectionMatrix.m33(1);
|
||||
}
|
||||
|
||||
/** Create the offset for part of the conversion to shadow map space. This
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package io.github.hydos.ginger.engine.shadow;
|
||||
|
||||
import io.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import org.joml.Matrix4f;
|
||||
|
||||
import io.github.hydos.ginger.engine.render.shaders.ShaderProgram;
|
||||
|
||||
public class ShadowShader extends ShaderProgram
|
||||
|
|
|
@ -5,9 +5,10 @@ import java.io.IOException;
|
|||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
import io.github.hydos.ginger.engine.math.Maths;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector2f;
|
||||
import io.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
import io.github.hydos.ginger.engine.render.models.RawModel;
|
||||
import io.github.hydos.ginger.engine.utils.Loader;
|
||||
import io.github.hydos.ginger.main.settings.Constants;
|
||||
|
@ -153,7 +154,7 @@ public class Terrain
|
|||
float heightD = getHeight(x, z - 1, image);
|
||||
float heightU = getHeight(x, z + 1, image);
|
||||
Vector3f normal = new Vector3f(heightL - heightR, 2f, heightD - heightU);
|
||||
normal.normalise();
|
||||
normal.normalize();
|
||||
return normal;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue