[DEV] tutorial 19 'first person camera' implementation add a basic vector2f and vector 3f and Matrx4f usable ...

This commit is contained in:
Edouard DUPIN 2020-04-23 23:04:52 +02:00
parent c8548d0f78
commit 555031e910
20 changed files with 1563 additions and 112 deletions

88
src/display/Fps.java Normal file
View File

@ -0,0 +1,88 @@
package display;
public class Fps {
private long startTime = 0;
private long nbCallTime = 0;
private long nbDisplayTime = 0;
private float min = 99999999;
private float avg = 0;
private float max = 0;
private float min_idle = 99999999;
private float avg_idle = 0;
private float max_idle = 0;
private long ticTime = 0;
private boolean display = false;
private boolean drawingDone = false;
private String displayName = null;
private boolean displayFPS;
public Fps(String displayName, boolean displayFPS) {
this.displayName = displayName;
this.displayFPS = displayFPS;
}
public void tic() {
long currentTime = System.currentTimeMillis();
ticTime = currentTime;
nbCallTime++;
if (startTime == 0) {
startTime = currentTime;
}
if ( (currentTime - startTime) > 10000) {
display = true;
}
}
public void toc() {
toc(false);
}
public void toc(boolean displayTime) {
long currentTime = System.currentTimeMillis();
long processTimeLocal = (currentTime - ticTime);
if (displayTime == true) {
System.out.println(displayName + ": processTime: " + processTimeLocal);
}
if (drawingDone == true) {
min = Math.min(min, processTimeLocal);
max = Math.max(max, processTimeLocal);
avg += processTimeLocal;
drawingDone = false;
} else {
min_idle = Math.min(min_idle, processTimeLocal);
max_idle = Math.max(max_idle, processTimeLocal);
avg_idle += processTimeLocal;
}
}
public void incrementCounter() {
nbDisplayTime++;
drawingDone = true;
}
public void draw() {
if (display == true) {
if (nbDisplayTime > 0) {
System.out.println(displayName + " : Active : "
+ min + " "
+ avg / nbDisplayTime + "ms "
+ max + " ");
}
if (nbCallTime-nbDisplayTime>0) {
System.out.println(displayName + " : idle : "
+ min_idle + " "
+ avg_idle / (nbCallTime-nbDisplayTime) + "ms "
+ max_idle + " ");
}
if (displayFPS == true) {
System.out.println("FPS : " + nbDisplayTime + "/" + nbCallTime + "fps");
}
max = 0;
min = 99999999;
avg = 0;
max_idle = 0;
min_idle = 99999999;
avg_idle = 0;
nbCallTime = 0;
nbDisplayTime = 0;
startTime = 0;
display = false;
}
}
}

View File

@ -5,8 +5,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.vecmath.Vector3f;
import org.atriaSoft.etk.math.Vector3f;
import org.lwjgl.openvr.Texture;
import entities.Camera;
@ -83,7 +82,6 @@ public class MainGameLoop {
Terrain terrain = new Terrain(0,-1,loader, texturePack, blendMap);
Terrain terrain2 = new Terrain(-1,-1,loader, texturePack, blendMap);
Camera camera = new Camera();
List<Entity> entities = new ArrayList<Entity>();
Random random = new Random();
@ -107,6 +105,8 @@ public class MainGameLoop {
new ModelTexture(loader.loadTexture("white")));
Player player = new Player(stanfordBunnyModel, new Vector3f(0,0,-50), new Vector3f(0,0,0), 1);
Camera camera = new Camera(player);
MasterRenderer renderer = new MasterRenderer();
manager.setDrawer(new DisplayManagerDraw() {
@ -121,6 +121,7 @@ public class MainGameLoop {
renderer.processTerrain(terrain2);
renderer.processEntity(player);
for (Entity entity : entities) {
entity.increaseRotation(0, 0.01f, 0.0f);
renderer.processEntity(entity);
}
renderer.render(light, camera);

View File

@ -1,25 +1,50 @@
package entities;
import javax.vecmath.Vector3f;
import org.atriaSoft.etk.math.Vector3f;
import renderEngine.DisplayManager;
public class Camera {
private float distanceFromPlayer = 50;
private float angleAroundPlayer = 0;
private Vector3f position = new Vector3f(0,5,0);
private float pitch = 0;// = (float) Math.toRadians(10);
private float pitch = 0;// (float) Math.toRadians(10);
private float yaw = 0;
private float roll = 0;
public Camera() {
private Player player;
public Camera(Player player) {
this.player = player;
}
public void move() {
if (DisplayManager.isKeyDown('q')) {
position.y += 0.8f;
}
if (DisplayManager.isKeyDown('z')) {
position.y -= 0.8f;
}
calculateZoom();
calculatePitch();
CalculateAngleAroundPlayer();
float horinzontalDistance = calculateHorizontalDistance();
float verticalDistance = calculateVerticalDistance();
calculateCameraPosition(horinzontalDistance, verticalDistance);
this.yaw = 3.141596f - player.getRotation().y - angleAroundPlayer ;
}
private void calculateCameraPosition (float horizontalDistance, float verticalDistance) {
float theta = 3.141596f + player.getRotation().y + angleAroundPlayer;
float offsetX = (float) (horizontalDistance * Math.sin(theta));
float offsetZ = (float) (horizontalDistance * Math.cos(theta));
position.x = player.getPosition().x + offsetX;
position.z = player.getPosition().z + offsetZ;
position.y = player.getPosition().y+5 + verticalDistance;
}
private float calculateHorizontalDistance() {
return (float) (distanceFromPlayer * Math.cos(pitch));
}
private float calculateVerticalDistance() {
return (float) (distanceFromPlayer * Math.sin(pitch));
}
public Vector3f getPosition() {
@ -37,4 +62,29 @@ public class Camera {
public float getRoll() {
return roll;
}
private void calculateZoom() {
float zoomLevel = DisplayManager.getDWheel() * 1;
distanceFromPlayer -= zoomLevel;
}
private void calculatePitch() {
if (DisplayManager.isButtonRightDown() ) {
float pitchChange = DisplayManager.getDY() * 0.01f;
pitch += pitchChange;
if (pitch < 0) {
pitch = 0;
} else if (pitch > 3.14159f/2f) {
pitch = 3.14159f/2f;
}
}
}
private void CalculateAngleAroundPlayer() {
if (DisplayManager.isButtonLeftDown()) {
float angleChange = DisplayManager.getDX() * 0.003f;
angleAroundPlayer -= angleChange;
}
}
}

View File

@ -1,6 +1,6 @@
package entities;
import javax.vecmath.Vector3f;
import org.atriaSoft.etk.math.Vector3f;
import models.TexturedModel;

View File

@ -1,6 +1,6 @@
package entities;
import javax.vecmath.Vector3f;
import org.atriaSoft.etk.math.Vector3f;
public class Light {
private Vector3f position;

View File

@ -1,14 +1,14 @@
package entities;
import javax.vecmath.Vector3f;
import org.atriaSoft.etk.math.Vector3f;
import models.TexturedModel;
import renderEngine.DisplayManager;
public class Player extends Entity {
private static final float RUN_SPEED = 20;
private static final float TRUN_SPEED = (float) Math.toRadians(160);
private static final float RUN_SPEED = 35;
private static final float TRUN_SPEED = (float) Math.toRadians(120);
private static final float GRAVITY = -50;
private static final float JUMP_POWER = 30;

View File

@ -4,7 +4,7 @@ open module Tutorial {
//exports fr.xxx.core;
requires transitive vecmath;
//requires transitive vecmath;
requires transitive org.lwjgl;
requires transitive org.lwjgl.natives;
requires transitive org.lwjgl.glfw;

View File

@ -8,8 +8,9 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.vecmath.Vector2f;
import javax.vecmath.Vector3f;
import org.atriaSoft.etk.math.Vector2f;
import org.atriaSoft.etk.math.Vector3f;
public class OBJFileLoader {

View File

@ -1,6 +1,6 @@
package objConverter;
import javax.vecmath.Vector3f;
import org.atriaSoft.etk.math.Vector3f;
public class Vertex {

View File

@ -0,0 +1,475 @@
package org.atriaSoft.etk.math;
import java.nio.FloatBuffer;
public class Matrix4f {
private float[] mat = new float[4*4]; //!< matrix data
/**
* @brief configure identity of the matrix
*/
public void setIdentity() {
for(int iii=0; iii<4*4 ; iii++) {
this.mat[iii] = 0;
}
this.mat[0] = 1.0f;
this.mat[5] = 1.0f;
this.mat[10] = 1.0f;
this.mat[15] = 1.0f;
}
/**
* @brief Constructor that load identity
*/
public Matrix4f() {
setIdentity();
}
/**
* @brief Configuration constructor.
* @param[in] a1 1st colomn, 1 line value
* @param[in] b1 2nd colomn, 1 line value
* @param[in] c1 3rd colomn, 1 line value
* @param[in] d1 4th colomn, 1 line value
* @param[in] a2 1st colomn, 2 line value
* @param[in] b2 2nd colomn, 2 line value
* @param[in] c2 3rd colomn, 2 line value
* @param[in] d2 4th colomn, 2 line value
* @param[in] a3 1st colomn, 3 line value
* @param[in] b3 2nd colomn, 3 line value
* @param[in] c3 3rd colomn, 3 line value
* @param[in] d3 4th colomn, 3 line value
* @param[in] a4 1st colomn, 4 line value
* @param[in] b4 2nd colomn, 4 line value
* @param[in] c4 3rd colomn, 4 line value
* @param[in] d4 4th colomn, 4 line value
*/
public Matrix4f(float a1, float b1, float c1, float d1,
float a2, float b2, float c2, float d2,
float a3, float b3, float c3, float d3,
float a4, float b4, float c4, float d4) {
this.mat[0] = a1;
this.mat[1] = b1;
this.mat[2] = c1;
this.mat[3] = d1;
this.mat[4] = a2;
this.mat[5] = b2;
this.mat[6] = c2;
this.mat[7] = d2;
this.mat[8] = a3;
this.mat[9] = b3;
this.mat[10] = c3;
this.mat[11] = d3;
this.mat[12] = a4;
this.mat[13] = b4;
this.mat[14] = c4;
this.mat[15] = d4;
}
/**
* @brief Configuration constructor.
* @param[in] values vector of values
*/
public Matrix4f(float[] values) {
if (values == null) {
setIdentity();
return;
}
for(int iii=0; iii<16 ; ++iii) {
this.mat[iii] = values[iii];
}
}
/**
* @brief Operator= Asign the current object with an other object
* @param[in] obj Reference on the external object
*/
public void set(Matrix4f obj) {
for(int iii=0; iii<16 ; iii++) {
this.mat[iii] = obj.mat[iii];
}
}
/**
* @brief Equality compare operator with an other object.
* @param[in] obj Reference on the comparing object
* @return true The Objects are identical
* @return false The Objects are NOT identical
*/
public boolean isEqual(Matrix4f obj) {
for(int iii=0; iii<4*4 ; ++iii) {
if(this.mat[iii] != obj.mat[iii]) {
return false;
}
}
return true;
}
/**
* @brief In-Equality compare operator with an other object.
* @param[in] obj Reference on the comparing object
* @return true The Objects are NOT identical
* @return false The Objects are identical
*/
public boolean isDifferent(Matrix4f obj) {
for(int iii=0; iii<4*4 ; ++iii) {
if(this.mat[iii] != obj.mat[iii]) {
return true;
}
}
return false;
}
/**
* @brief Operator+= Addition an other matrix with this one
* @param[in] obj Reference on the external object
*/
public void add(Matrix4f obj) {
for(int iii=0; iii<4*4 ; ++iii) {
this.mat[iii] += obj.mat[iii];
}
}
/**
* @brief Operator-= Decrement an other matrix with this one
* @param[in] obj Reference on the external object
*/
public void decrement(Matrix4f obj) {
for(int iii=0; iii<4*4 ; ++iii) {
this.mat[iii] -= obj.mat[iii];
}
}
/**
* @brief Operator*= Multiplication an other matrix with this one
* @param[in] obj Reference on the external object
*/
public void multiply(Matrix4f obj) {
// output Matrix
float[] matrixOut = new float[16];
for(int xxx=0; xxx<4 ; xxx++) {
for(int yyy=0; yyy<4 ; yyy++) {
float value = 0;
for(int kkk=0; kkk<4 ; kkk++) {
value += this.mat[yyy*4+kkk] * obj.mat[kkk*4+xxx];
}
matrixOut[yyy*4+xxx] = value;
}
}
// set it at the output
for(int iii=0; iii<4*4 ; iii++) {
this.mat[iii] = matrixOut[iii];
}
}
/**
* @brief Operator* apply matrix on a vector
* @param[in] point Point value to apply the matrix
* @return New vector containing the value
*/
public Vector3f multiply(Vector3f point) {
return new Vector3f( this.mat[0]*point.x + this.mat[1]*point.y + this.mat[2]*point.z + this.mat[3],
this.mat[4]*point.x + this.mat[5]*point.y + this.mat[6]*point.z + this.mat[7],
this.mat[8]*point.x + this.mat[9]*point.y + this.mat[10]*point.z + this.mat[11] );
}
/**
* @brief Transpose the current matix (usefull for OpenGL display)
*/
public void transpose() {
float tmpVal = this.mat[1];
this.mat[1] = this.mat[4];
this.mat[4] = tmpVal;
tmpVal = this.mat[2];
this.mat[2] = this.mat[8];
this.mat[8] = tmpVal;
tmpVal = this.mat[6];
this.mat[6] = this.mat[9];
this.mat[9] = tmpVal;
tmpVal = this.mat[3];
this.mat[3] = this.mat[12];
this.mat[12] = tmpVal;
tmpVal = this.mat[7];
this.mat[7] = this.mat[13];
this.mat[13] = tmpVal;
tmpVal = this.mat[11];
this.mat[11] = this.mat[14];
this.mat[14] = tmpVal;
}
/**
* @brief Scale the current Matrix.
* @param[in] vect Scale vector to apply.
*/
public void scale(Vector3f vect) {
scale(vect.x, vect.y, vect.z);
}
/**
* @brief Scale the current Matrix.
* @param[in] sx Scale X value to apply.
* @param[in] sy Scale Y value to apply.
* @param[in] sz Scale Z value to apply.
*/
public void scale(float sx, float sy, float sz) {
this.mat[0] *= sx; this.mat[1] *= sy; this.mat[2] *= sz;
this.mat[4] *= sx; this.mat[5] *= sy; this.mat[6] *= sz;
this.mat[8] *= sx; this.mat[9] *= sy; this.mat[10] *= sz;
}
/**
* @brief Scale the current Matrix in all direction with 1 value.
* @param[in] scale Scale XYZ value to apply.
*/
public void scale(float scale) {
scale(scale, scale, scale);
}
/**
* @brief Makes a rotation matrix about an arbitrary axis.
* @param[in] vect vector to apply the angle.
* @param[in] angleRad angle to apply.
*/
public void rotate(Vector3f vect, float angleRad) {
Matrix4f tmpMat = createMatrixRotate(vect, angleRad);
this.multiply(tmpMat);
}
/**
* @brief Makes a translation of the matrix
* @param[in] vect Translation to apply.
*/
public void translate(Vector3f vect) {
Matrix4f tmpMat = createMatrixTranslate(vect);
this.multiply(tmpMat);
}
/**
* @brief Computes a cofactor. Used for matrix inversion.
* @param[in] row Id of raw.
* @param[in] col Id of colomn.
* @return the coFactorValue.
*/
public float coFactor(int row, int col) {
return ( ( this.mat[((row+1)&3)*4 + ((col+1)&3)] * this.mat[((row+2)&3)*4 + ((col+2)&3)] * this.mat[((row+3)&3)*4 + ((col+3)&3)]
+ this.mat[((row+1)&3)*4 + ((col+2)&3)] * this.mat[((row+2)&3)*4 + ((col+3)&3)] * this.mat[((row+3)&3)*4 + ((col+1)&3)]
+ this.mat[((row+1)&3)*4 + ((col+3)&3)] * this.mat[((row+2)&3)*4 + ((col+1)&3)] * this.mat[((row+3)&3)*4 + ((col+2)&3)] )
- ( this.mat[((row+3)&3)*4 + ((col+1)&3)] * this.mat[((row+2)&3)*4 + ((col+2)&3)] * this.mat[((row+1)&3)*4 + ((col+3)&3)]
+ this.mat[((row+3)&3)*4 + ((col+2)&3)] * this.mat[((row+2)&3)*4 + ((col+3)&3)] * this.mat[((row+1)&3)*4 + ((col+1)&3)]
+ this.mat[((row+3)&3)*4 + ((col+3)&3)] * this.mat[((row+2)&3)*4 + ((col+1)&3)] * this.mat[((row+1)&3)*4 + ((col+2)&3)] )
) * (((row + col) & 1)== 1? -1.0f : +1.0f);
}
/**
* @brief Computes the determinant of the matrix.
* @return The determinent Value.
*/
public float determinant() {
return this.mat[0] * coFactor(0, 0) +
this.mat[1] * coFactor(0, 1) +
this.mat[2] * coFactor(0, 2) +
this.mat[3] * coFactor(0, 3);
}
/**
* @brief Inverts the matrix.
* @note The determinant must be != 0, otherwithe the matrix can't be inverted.
* @return The inverted matrix.
*/
public Matrix4f invert() {
float det = determinant();
if(Math.abs(det) < (1.0e-7f)) {
// The matrix is not invertible! Singular case!
return clone();
}
Matrix4f temp = new Matrix4f();
float iDet = 1.0f / det;
temp.mat[0] = coFactor(0,0) * iDet;
temp.mat[1] = coFactor(0,1) * iDet;
temp.mat[2] = coFactor(0,2) * iDet;
temp.mat[3] = coFactor(0,3) * iDet;
temp.mat[4] = coFactor(1,0) * iDet;
temp.mat[5] = coFactor(1,1) * iDet;
temp.mat[6] = coFactor(1,2) * iDet;
temp.mat[7] = coFactor(1,3) * iDet;
temp.mat[8] = coFactor(2,0) * iDet;
temp.mat[9] = coFactor(2,1) * iDet;
temp.mat[10] = coFactor(2,2) * iDet;
temp.mat[11] = coFactor(2,3) * iDet;
temp.mat[12] = coFactor(3,0) * iDet;
temp.mat[13] = coFactor(3,1) * iDet;
temp.mat[14] = coFactor(3,2) * iDet;
temp.mat[15] = coFactor(3,3) * iDet;
return temp;
}
/**
* @brief Operator= Asign the current object with an other object
* @param[in] obj Reference on the external object
*/
public Matrix4f clone() {
Matrix4f out = new Matrix4f();
for(int iii=0; iii<16 ; iii++) {
out.mat[iii] = this.mat[iii];
}
return out;
}
/**
* @brief Create projection matrix with the box parameter (camera view in -z axis)
* @param[in] xmin X minimum size of the frustum
* @param[in] xmax X maximum size of the frustum
* @param[in] ymin Y minimum size of the frustum
* @param[in] ymax Y maximum size of the frustum
* @param[in] zNear Z minimum size of the frustum
* @param[in] zFar Z maximum size of the frustum
* @return New matrix of the transformation requested
*/
public static Matrix4f createMatrixFrustum(float xmin, float xmax, float ymin, float ymax, float zNear, float zFar) {
Matrix4f tmp = new Matrix4f();
for(int iii=0; iii<4*4 ; iii++) {
tmp.mat[iii] = 0;
}
// 0 1 2 3
// 4 5 6 7
// 8 9 10 11
// 12 13 14 15
tmp.mat[0] = (2.0f * zNear) / (xmax - xmin);
tmp.mat[5] = (2.0f * zNear) / (ymax - ymin);
tmp.mat[10] = -(zFar + zNear) / (zFar - zNear);
tmp.mat[2] = (xmax + xmin) / (xmax - xmin);
tmp.mat[6] = (ymax + ymin) / (ymax - ymin);
tmp.mat[14] = -1.0f;
tmp.mat[11] = -(2.0f * zFar * zNear) / (zFar - zNear);
return tmp;
}
/**
* @brief Create projection matrix with human repensentation view (camera view in -z axis)
* @param[in] foxy Focal in radian of the camera
* @param[in] aspect aspect ratio of the camera
* @param[in] zNear Z near size of the camera
* @param[in] zFar Z far size of the camera
* @return New matrix of the transformation requested
*/
public static Matrix4f createMatrixPerspective(float foxy, float aspect, float zNear, float zFar) {
//TKDEBUG("drax perspective: foxy=" << foxy << "->" << aspect << " " << zNear << "->" << zFar);
float xmax = zNear * (float)Math.tan(foxy/2.0);
float xmin = -xmax;
float ymin = xmin / aspect;
float ymax = xmax / aspect;
//TKDEBUG("drax perspective: " << xmin << "->" << xmax << " & " << ymin << "->" << ymax << " " << zNear << "->" << zFar);
return createMatrixFrustum(xmin, xmax, ymin, ymax, zNear, zFar);
}
/**
* @brief Create orthogonal projection matrix with the box parameter (camera view in -z axis)
* @param[in] left left size of the camera
* @param[in] right Right size of the camera
* @param[in] bottom Buttom size of the camera
* @param[in] top Top Size of the camera
* @param[in] nearVal Z near size of the camera
* @param[in] farVal Z far size of the camera
* @return New matrix of the transformation requested
*/
public static Matrix4f createMatrixOrtho(float left, float right, float bottom, float top, float nearVal, float farVal) {
Matrix4f tmp = new Matrix4f();
for(int iii=0; iii<4*4 ; iii++) {
tmp.mat[iii] = 0;
}
tmp.mat[0] = 2.0f / (right - left);
tmp.mat[5] = 2.0f / (top - bottom);
tmp.mat[10] = -2.0f / (farVal - nearVal);
tmp.mat[3] = -1*(right + left) / (right - left);
tmp.mat[7] = -1*(top + bottom) / (top - bottom);
tmp.mat[11] = -1*(farVal + nearVal) / (farVal - nearVal);
tmp.mat[15] = 1;
return tmp;
}
/**
* @brief Create a matrix 3D with a simple translation
* @param[in] translate 3 dimention translation
* @return New matrix of the transformation requested
*/
public static Matrix4f createMatrixTranslate(Vector3f translate) {
Matrix4f tmp = new Matrix4f();
// set translation :
tmp.mat[3] = translate.x;
tmp.mat[7] = translate.y;
tmp.mat[11] = translate.z;
return tmp;
}
/**
* @brief Create a matrix 3D with a simple scale
* @param[in] scale 3 dimention scale
* @return New matrix of the transformation requested
*/
public static Matrix4f createMatrixScale(Vector3f scale){
Matrix4f tmp = new Matrix4f();
tmp.scale(scale);
return tmp;
}
/**
* @brief Create a matrix 3D with a simple rotation
* @param[in] normal vector aroud witch apply the rotation
* @param[in] angleRad Radian angle to set at the matrix
* @return New matrix of the transformation requested
*/
public static Matrix4f createMatrixRotate(Vector3f normal, float angleRad) {
Matrix4f tmp = new Matrix4f();
float cosVal = (float)Math.cos(angleRad);
float sinVal = (float)Math.sin(angleRad);
float invVal = 1.0f-cosVal;
// set rotation :
tmp.mat[0] = normal.x * normal.x * invVal + cosVal;
tmp.mat[1] = normal.x * normal.y * invVal - normal.z * sinVal;
tmp.mat[2] = normal.x * normal.z * invVal + normal.y * sinVal;
tmp.mat[4] = normal.y * normal.x * invVal + normal.z * sinVal;
tmp.mat[5] = normal.y * normal.y * invVal + cosVal;
tmp.mat[6] = normal.y * normal.z * invVal - normal.x * sinVal;
tmp.mat[8] = normal.z * normal.x * invVal - normal.y * sinVal;
tmp.mat[9] = normal.z * normal.y * invVal + normal.x * sinVal;
tmp.mat[10] = normal.z * normal.z * invVal + cosVal;
return tmp;
}
//! @notindoc
public static Matrix4f createMatrixRotate2(Vector3f vect) {
return createMatrixLookAt(vect, new Vector3f(0,0,0), new Vector3f(0,1,0));
}
/**
* @brief Create projection matrix with camera property (camera view in -z axis)
* @param[in] eye Optical center of the camera
* @param[in] target Point of where the camera is showing
* @param[in] up Up vector of the camera
* @return New matrix of the transformation requested
*/
public static Matrix4f createMatrixLookAt(Vector3f eye,
Vector3f target,
Vector3f up) {
Matrix4f tmp = new Matrix4f();
Vector3f forward = eye;
forward.less(target);
forward.safeNormalize();
Vector3f xaxis = target.cross(up.normalized());
xaxis.safeNormalize();
Vector3f up2 = xaxis.cross(forward);
xaxis.safeNormalize();
tmp.mat[0] = xaxis.x;
tmp.mat[1] = up2.x;
tmp.mat[2] = forward.x;
tmp.mat[3] = eye.x;
tmp.mat[4] = xaxis.y;
tmp.mat[5] = up2.y;
tmp.mat[6] = forward.y;
tmp.mat[7] = eye.y;
tmp.mat[8] = xaxis.z;
tmp.mat[9] = up2.z;
tmp.mat[10] = forward.z;
tmp.mat[11] = eye.z;
tmp.mat[12] = 0.0f;
tmp.mat[13] = 0.0f;
tmp.mat[14] = 0.0f;
tmp.mat[15] = 1.0f;
return tmp;
}
public float[] getTable() {
return this.mat;
}
}

View File

@ -0,0 +1,358 @@
package org.atriaSoft.etk.math;
public class Vector2f {
public float x = 0;
public float y = 0;
/* ****************************************************
* Constructor
*****************************************************/
public Vector2f() {
this.x = 0;
this.y = 0;
}
/**
* @brief Constructor from scalars
* @param[in] xxx X value
* @param[in] yyy Y value
*/
public Vector2f(float xxx, float yyy) {
this.x = xxx;
this.y = yyy;
}
/**
* @brief Constructor with external vector
* @param[in] obj The vector to add to this one
*/
public Vector2f(Vector2f obj) {
this.x = obj.x;
this.y = obj.y;
}
/**
* @brief Operator= Asign the current object with an other object
* @param[in] obj Reference on the external object
*/
public void set(Vector2f obj) {
this.x = obj.x;
this.y = obj.y;
}
/**
* @brief Operator= Asign the current object with a value
* @param[in] val Value to assign on the object
*/
public void set(float val) {
this.x = val;
this.y = val;
}
/**
* @brief Operator= Asign the current object with a value
* @param[in] xxx X value
* @param[in] yyy Y value
*/
public void set(float xxx, float yyy) {
this.x = xxx;
this.y = yyy;
}
/**
* @brief Equality compare operator with an other object.
* @param[in] obj Reference on the comparing object
* @return true The Objects are identical
* @return false The Objects are NOT identical
*/
public boolean isEqual(Vector2f obj) {
return ( obj.x == this.x
&& obj.y == this.y);
}
/**
* @brief In-Equality compare operator with an other object.
* @param[in] obj Reference on the comparing object
* @return true The Objects are NOT identical
* @return false The Objects are identical
*/
public boolean isDifferent(Vector2f obj) {
return ( obj.x != this.x
|| obj.y != this.y);
}
public boolean isLowerOrEqual(Vector2f obj) {
return ( this.x <= obj.x
&& this.y <= obj.y);
}
public boolean isLower(Vector2f obj) {
return ( this.x < obj.x
&& this.y < obj.y);
}
public boolean isGreaterOrEqual(Vector2f obj) {
return ( this.x >= obj.x
&& this.y >= obj.y);
}
public boolean isGreater(Vector2f obj) {
return ( this.x > obj.x
&& this.y > obj.y);
}
/**
* @brief Operator+= Addition an other vertor with this one
* @param[in] obj Reference on the external object
*/
void add(Vector2f obj) {
this.x += obj.x;
this.y += obj.y;
}
/**
* @brief Operator+= Addition an other vertor with this one
* @param[in] val Value to addition at x/y
*/
public void add(float val) {
this.x += val;
this.y += val;
}
/**
* @brief Operator-= Decrement an other vertor with this one
* @param[in] obj Reference on the external object
*/
public void less(Vector2f obj) {
this.x -= obj.x;
this.y -= obj.y;
}
/**
* @brief Operator-= Decrement an other vertor with this one
* @param[in] val Value to addition at x/y
*/
public void less(float val) {
this.x -= val;
this.y -= val;
}
/**
* @brief Operator*= Multiplication an other vertor with this one
* @param[in] obj Reference on the external object
*/
public void multiply(Vector2f obj) {
this.x *= obj.x;
this.y *= obj.y;
}
/**
* @brief Operator*= Multiplication an other vertor with this one
* @param[in] val Value to addition at x/y
*/
public void multiply(float val) {
this.x *= val;
this.y *= val;
}
/**
* @brief Operator/ Dividing an other vertor with this one
* @param[in] obj Reference on the external object
*/
public void devide(Vector2f obj) {
this.x /= obj.x;
this.y /= obj.y;
}
/**
* @brief Operator/ Dividing an other vertor with this one
* @param[in] val Value to addition at x/y
*/
public void devide(float val) {
this.x /= val;
this.y /= val;
}
/**
* @brief Incrementation of this vector (+1 of 2 elements)
*/
public void increment() {
this.x++;
this.y++;
}
/**
* @brief Decrementation of this vector (-1 of 2 elements)
*/
public void decrement() {
this.x--;
this.y--;
}
/**
* @brief Return the cross product / determinant
* @param obj The other vector in the cross product
* @return cross product value
*/
public float cross(Vector2f obj) {
return this.x * obj.y
- this.y * obj.x;
}
/**
* @brief Return the dot product
* @param obj The other vector in the dot product
* @return Dot product value
*/
public float dot(Vector2f obj) {
return this.x * obj.x
+ this.y * obj.y;
}
/**
* @brief Get the length of the vector squared
* @return Squared length value.
*/
public float length2() {
return dot(this);
}
/**
* @brief Get the length of the vector
* @return Length value
*/
public float length() {
return (float) Math.sqrt(length2());
}
/**
* @brief Return the distance squared between the ends of this and another vector
* This is symantically treating the vector like a point
* @param[in] obj The other vector to compare distance
* @return the square distance of the 2 points
*/
public float distance2(Vector2f obj) {
float deltaX = obj.x - this.x;
float deltaY = obj.y - this.y;
return deltaX*deltaX + deltaY*deltaY;
}
/**
* @brief Return the distance between the ends of this and another vector
* This is symantically treating the vector like a point
* @param[in] obj The other vector to compare distance
* @return the distance of the 2 points
*/
public float distance(Vector2f obj) {
return (float)Math.sqrt(this.distance2(obj));
}
/**
* @brief Normalize this vector x^2 + y^2 = 1
*/
public void normalize() {
this.devide(length());
}
/**
* @brief Normalize this vector x^2 + y^2 = 1 (check if not deviding by 0, if it is the case ==> return (1,0))
* @return Local reference of the vector normalized
*/
public void safeNormalize() {
float tmp = length();
if (tmp != 0) {
this.devide(length());
return;
}
setValue(1,0);
return;
}
/**
* @brief Return a normalized version of this vector
* @return New vector containing the value
*/
public Vector2f normalized() {
Vector2f tmp = this.clone();
tmp.normalize();
return tmp;
}
/**
* @brief Return a vector will the absolute values of each element
* @return New vector containing the value
*/
public Vector2f absolute() {
return new Vector2f( Math.abs(this.x),
Math.abs(this.y));
}
/**
* @brief Return the axis with the smallest value
* @return values are 0,1 for x or y
*/
public int minAxis() {
return this.x < this.y ? 0 : 1;
}
/**
* @brief Return the axis with the largest value
* @return values are 0,1 for x or y
*/
public int maxAxis() {
return this.x < this.y ? 1 : 0;
}
/**
* @brief Return the axis with the smallest ABSOLUTE value
* @return values 0,1 for x, or z
*/
public int furthestAxis() {
return absolute().minAxis();
}
/**
* @brief Return the axis with the largest ABSOLUTE value
* @return values 0,1 for x or y
*/
public int closestAxis() {
return absolute().maxAxis();
}
/**
* @brief Set the x value
* @param[in] xxx New value
*/
public void setX(float xxx) {
this.x = xxx;
};
/**
* @brief Set the y value
* @param[in] yyy New value
*/
public void setY(float yyy) {
this.y = yyy;
};
/**
* @brief Get X value
* @return the x value
*/
public float getX() {
return this.x;
}
/**
* @brief Get Y value
* @return the y value
*/
public float getY() {
return this.y;
}
/**
* @brief Set each element to the max of the current values and the values of another vector
* @param other The other vector to compare with
*/
public void setMax(Vector2f other) {
this.x = Math.max(this.x, other.x);
this.y = Math.max(this.y, other.y);
}
/**
* @brief Set each element to the min of the current values and the values of another vector
* @param other The other vector to compare with
*/
public void setMin(Vector2f other) {
this.x = Math.min(this.x, other.x);
this.y = Math.min(this.y, other.y);
}
/**
* @brief Set Value on the vector
* @param[in] xxx X value.
* @param[in] yyy Y value.
*/
public void setValue(float xxx, float yyy) {
this.x = xxx;
this.y = yyy;
}
/**
* @brief Set 0 value on all the vector
*/
public void setZero() {
this.x = 0;
this.y = 0;
}
/**
* @brief Check if the vector is equal to (0,0)
* @return true The value is equal to (0,0)
* @return false The value is NOT equal to (0,0)
*/
public boolean isZero() {
return this.x == 0
&& this.y == 0;
}
public Vector2f clone() {
return new Vector2f(this);
}
}

View File

@ -0,0 +1,461 @@
package org.atriaSoft.etk.math;
public class Vector3f {
public float x = 0;
public float y = 0;
public float z = 0;
/**
* @brief Default contructor
*/
public Vector3f() {
}
/**
* @brief Constructor from scalars
* @param[in] xxx X value
* @param[in] yyy Y value
* @param[in] zzz Z value
*/
public Vector3f(float xxx, float yyy, float zzz) {
this.x = xxx;
this.y = yyy;
this.z = zzz;
}
/**
* @brief Constructor from scalars
* @param[in] value unique value for X,Y and Z value
*/
public Vector3f(float value) {
this.x = value;
this.y = value;
this.z = value;
}
/**
* @brief Constructor from other vector (copy)
* @param[in] obj The vector to add to this one
*/
public Vector3f(Vector3f obj) {
this.x += obj.x;
this.y += obj.y;
this.z += obj.z;
}
/**
* @brief Add a vector to this one
* @param[in] obj The vector to add to this one
*/
public void add(Vector3f obj) {
this.x += obj.x;
this.y += obj.y;
this.z += obj.z;
}
/**
* @brief Subtract a vector from this one
* @param[in] obj The vector to subtract
*/
public void less(Vector3f obj) {
this.x -= obj.x;
this.y -= obj.y;
this.z -= obj.z;
}
/**
* @brief Scale the vector
* @param[in] val Scale factor
*/
public void multiply(float val) {
this.x *= val;
this.y *= val;
this.z *= val;
}
/**
* @brief Inversely scale the vector
* @param[in] val Scale factor to divide by
*/
public void devide(float val) {
if (val != 0.0f) {
float tmpVal = 1.0f / val;
this.x *= tmpVal;
this.y *= tmpVal;
this.z *= tmpVal;
}
// TODO: maybe throw ...
}
/**
* @brief Return the dot product
* @param[in] obj The other vector in the dot product
* @return Dot product value
*/
public float dot(Vector3f obj) {
return this.x * obj.x
+ this.y * obj.y
+ this.z * obj.z;
}
/**
* @brief Get the length of the vector squared
* @return Squared length value.
*/
public float length2() {
return dot(this);
}
/**
* @brief Get the length of the vector
* @return Length value
*/
public float length() {
return (float) Math.sqrt(length2());
}
/**
* @brief Return the distance squared between the ends of this and another vector
* This is symantically treating the vector like a point
* @param[in] obj The other vector to compare distance
* @return the square distance of the 2 points
*/
public float distance2(Vector3f obj) {
float deltaX = obj.x - this.x;
float deltaY = obj.y - this.y;
float deltaZ = obj.z - this.z;
return deltaX*deltaX + deltaY*deltaY + deltaZ*deltaZ;
}
/**
* @brief Return the distance between the ends of this and another vector
* This is symantically treating the vector like a point
* @param[in] obj The other vector to compare distance
* @return the distance of the 2 points
*/
public float distance(Vector3f obj) {
return (float)Math.sqrt(this.distance2(obj));
}
/**
* @brief Normalize this vector x^2 + y^2 + z^2 = 1 (check if not deviding by 0, if it is the case ==> return (1,0,0))
*/
public void safeNormalize() {
float length = length();
if (length != 0.0f) {
this.devide(length);
}
this.setValue(1,0,0);
}
/**
* @brief Normalize this vector x^2 + y^2 + z^2 = 1
*/
public void normalize() {
this.devide(this.length());
}
/**
* @brief Return a normalized version of this vector
* @return New vector containing the value
*/
public Vector3f normalized() {
Vector3f out = new Vector3f(this);
out.normalize();
return out;
}
/**
* @brief Return a normalized version of this vector (check if not deviding by 0, if it is the case ==> return (1,0,0))
* @return New vector containing the value
*/
public Vector3f safeNormalized() {
Vector3f out = new Vector3f(this);
out.safeNormalize();
return out;
}
/**
* @brief Return a rotated version of this vector
* @param[in] wAxis The axis to rotate about
* @param[in] angle The angle to rotate by
* @return New vector containing the value
*/
public Vector3f rotate( Vector3f wAxis, float angle ) {
Vector3f out = wAxis.clone();
out.multiply( wAxis.dot( this ) );
Vector3f x = this.clone();
x.less(out);
Vector3f y = wAxis.cross( this );
x.multiply((float)Math.cos(angle));
y.multiply((float)Math.sin(angle));
out.add(x);
out.add(y);
return out;
}
/**
* @brief Calculate the angle between this and another vector
* @param[in] obj The other vector
* @return Angle in radian
*/
public float angle(Vector3f obj) {
float s = (float) Math.sqrt(length2() * obj.length2());
if (0!=s) {
return (float) Math.acos(this.dot(obj) / s);
}
return 0;
}
/**
* @brief Return a vector will the absolute values of each element
* @return New vector containing the value
*/
public Vector3f absolute() {
return new Vector3f( Math.abs(this.x),
Math.abs(this.y),
Math.abs(this.z));
}
/**
* @brief Return the cross product between this and another vector
* @param[in] obj The other vector
* @return Vector with the result of the cross product
*/
public Vector3f cross(Vector3f obj) {
return new Vector3f(this.y * obj.z - this.z * obj.y,
this.z * obj.x - this.x * obj.z,
this.x * obj.y - this.y * obj.x);
}
/**
* @brief Return the triple product between this and another vector and another
* @param[in] obj1 The other vector 1
* @param[in] obj2 The other vector 2
* @return Value with the result of the triple product
*/
public float triple(Vector3f obj1, Vector3f obj2) {
return this.x * (obj1.y * obj2.z - obj1.z * obj2.y)
+ this.y * (obj1.z * obj2.x - obj1.x * obj2.z)
+ this.z * (obj1.x * obj2.y - obj1.y * obj2.x);
}
/**
* @brief Return the axis with the smallest value
* @return values 0,1,2 for x, y, or z
*/
public int minAxis() {
if (this.x < this.y) {
return this.x < this.z ? 0 : 2;
}
return this.y < this.z ? 1 : 2;
}
/**
* @brief Return the axis with the largest value
* @return values 0,1,2 for x, y, or z
*/
public int maxAxis() {
if (this.x < this.y) {
return this.y < this.z ? 2 : 1;
}
return this.x < this.z ? 2 : 0;
}
/**
* @brief Return the axis with the smallest ABSOLUTE value
* @return values 0,1,2 for x, y, or z
*/
public int furthestAxis() {
return absolute().minAxis();
}
/**
* @brief Return the axis with the largest ABSOLUTE value
* @return values 0,1,2 for x, y, or z
*/
public int closestAxis() {
return absolute().maxAxis();
}
/**
* @brief Interpolate the vector with a ration between 2 others
* @param[in] obj0 First vector
* @param[in] obj1 Second vector
* @param[in] ratio Ratio between obj0 and obj1
*/
public void setInterpolate3(Vector3f obj0, Vector3f obj1, float ratio) {
float inverse = 1.0f - ratio;
this.x = inverse * obj0.x + ratio * obj1.x;
this.y = inverse * obj0.y + ratio * obj1.y;
this.z = inverse * obj0.z + ratio * obj1.z;
// this.co[3] = s * v0[3] + rt * v1[3];
}
/**
* @brief Return the linear interpolation between this and another vector
* @param[in] obj The other vector
* @param[in] ratio The ratio of this to obj (ratio = 0 => return copy of this, ratio=1 => return other)
* @return New vector containing the value
*/
public Vector3f lerp(Vector3f obj, float ratio) {
return new Vector3f(this.x + (obj.x - this.x) * ratio,
this.y + (obj.y - this.y) * ratio,
this.z + (obj.z - this.z) * ratio);
}
/**
* @brief Elementwise multiply this vector by the other
* @param obj The other vector
*/
public void multiply(Vector3f obj) {
this.x *= obj.x;
this.y *= obj.y;
this.z *= obj.z;
}
/**
* @brief Set the x value
* @param[in] x New value
*/
public void setX(float x) {
this.x = x;
}
/**
* @brief Set the y value
* @param[in] y New value
*/
public void setY(float y) {
this.y = y;
}
/**
* @brief Set the z value
* @param[in] z New value
*/
public void setZ(float z) {
this.z = z;
}
/**
* @brief Get X value
* @return the x value
*/
public float getX() {
return this.x;
}
/**
* @brief Get Y value
* @return the y value
*/
public float getY() {
return this.y;
}
/**
* @brief Get Z value
* @return the z value
*/
public float getZ() {
return this.z;
}
/**
* @brief Equality compare operator with an other object.
* @param[in] obj Reference on the comparing object
* @return true The Objects are identical
* @return false The Objects are NOT identical
*/
public boolean isEqual(Vector3f obj) {
return ( (this.z == obj.z)
&& (this.y == obj.y)
&& (this.x == obj.x));
}
/**
* @brief In-Equality compare operator with an other object.
* @param[in] obj Reference on the comparing object
* @return true The Objects are NOT identical
* @return false The Objects are identical
*/
public boolean isDifferent(Vector3f obj) {
return ( (this.z != obj.z)
|| (this.y != obj.y)
|| (this.x != obj.x));
}
/**
* @brief Set each element to the max of the current values and the values of another Vector3f
* @param obj The other Vector3f to compare with
*/
public void setMax(Vector3f obj) {
this.x = Math.max(this.x, obj.x);
this.y = Math.max(this.y, obj.y);
this.z = Math.max(this.z, obj.z);
}
/**
* @brief Set each element to the min of the current values and the values of another Vector3f
* @param obj The other Vector3f to compare with
*/
public void setMin(Vector3f obj) {
this.x = Math.min(this.x, obj.x);
this.y = Math.min(this.y, obj.y);
this.z = Math.min(this.z, obj.z);
}
/**
* @brief Get the minimum value of the vector (x, y, z)
* @return The min value
*/
public float getMin() {
return Math.min(Math.min(this.x, this.y), this.z);
}
/**
* @brief Get the maximum value of the vector (x, y, z)
* @return The max value
*/
public float getMax() {
return Math.max(Math.max(this.x, this.y), this.z);
}
/**
* @brief Set Value on the vector
* @param[in] xxx X value.
* @param[in] yyy Y value.
* @param[in] zzz Z value.
*/
public void setValue(float xxx, float yyy, float zzz) {
this.x = xxx;
this.y = yyy;
this.z = zzz;
}
/**
* @brief Create a skew matrix of the object
* @param[out] obj0 Vector matric first line
* @param[out] obj1 Vector matric second line
* @param[out] obj2 Vector matric third line
*/
public void getSkewSymmetricMatrix(Vector3f obj0,Vector3f obj1,Vector3f obj2) {
obj0.setValue(0 ,-z ,y);
obj1.setValue(z ,0 ,-x);
obj2.setValue(-y ,x ,0);
}
/**
* @brief Set 0 value on all the vector
*/
public void setZero() {
setValue(0,0,0);
}
/**
* @brief Check if the vector is equal to (0,0,0)
* @return true The value is equal to (0,0,0)
* @return false The value is NOT equal to (0,0,0)
*/
public boolean isZero() {
return this.x == 0
&& this.y == 0
&& this.z == 0;
}
/**
* @brief Get the Axis id with the minimum value
* @return Axis ID 0,1,2
*/
public int getMinAxis() {
return (this.x < this.y ? (this.x < this.z ? 0 : 2) : (this.y < this.z ? 1 : 2));
}
/**
* @brief Get the Axis id with the maximum value
* @return Axis ID 0,1,2
*/
public int getMaxAxis() {
return (this.x < this.y ? (this.y < this.z ? 2 : 1) : (this.x < this.z ? 2 : 0));
}
/**
* @breif Get the orthogonalm vector of the current vector
* @return The ortho vector
*/
public Vector3f getOrthoVector() {
Vector3f vectorAbs = new Vector3f(Math.abs(this.x), Math.abs(this.y), Math.abs(this.z));
int minElement = vectorAbs.getMinAxis();
if (minElement == 0) {
float devider = 1.0f / (float)Math.sqrt(this.y*this.y + this.z*this.z);
return new Vector3f(0.0f, -this.z*devider, this.y*devider);
} else if (minElement == 1) {
float devider = 1.0f / (float)Math.sqrt(this.x*this.x + this.z*this.z);
return new Vector3f(-this.z*devider, 0.0f, this.x*devider);
}
float devider = 1.0f / (float)Math.sqrt(this.x*this.x + this.y*this.y);
return new Vector3f(-this.y*devider, this.x*devider, 0.0f);
}
/**
* @brief Clone the current vector.
* @return New vector containing the value
*/
public Vector3f clone() {
Vector3f out = new Vector3f(this);
out.normalize();
return out;
}
}

View File

@ -1,13 +1,15 @@
package renderEngine;
import org.atriaSoft.etk.math.Vector2f;
import org.lwjgl.*;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
import org.lwjgl.system.*;
import display.Fps;
import java.nio.*;
import javax.vecmath.Vector2f;
import static org.lwjgl.glfw.Callbacks.*;
import static org.lwjgl.glfw.GLFW.*;
@ -31,7 +33,18 @@ public class DisplayManager {
private static long lastFrameTime;
private static float delta;
private Fps fps = new Fps("Main Loop", true);
private DisplayManagerDraw drawer = null;
private static double whellOffsetY;
private static double whellOffsetX;
private static boolean rightButtonStateDown = false;
private static boolean leftButtonStateDown = false;
private static double lastMousePositionX = 0;
private static double lastMousePositionY = 0;
private static double currentMousePositionX = 0;
private static double currentMousePositionY = 0;
public void setDrawer(DisplayManagerDraw drawer) {
this.drawer = drawer;
}
@ -119,6 +132,7 @@ public class DisplayManager {
throw new RuntimeException("Failed to create the GLFW window");
// Setup a key callback. It will be called every time a key is pressed, repeated or released.
// https://www.glfw.org/docs/latest/input_guide.html
glfwSetKeyCallback(window, (window, key, scancode, action, mods) -> {
if ( key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE ) {
glfwSetWindowShouldClose(window, true); // We will detect this in the rendering loop
@ -187,7 +201,35 @@ public class DisplayManager {
}
}
});
glfwSetCursorPosCallback(window, (window, xpos, ypos) -> {
currentMousePositionX = xpos;
currentMousePositionY = ypos;
});
glfwSetMouseButtonCallback(window, (window, button, action, mods) -> {
if (button == GLFW_MOUSE_BUTTON_RIGHT) {
if (action == GLFW_PRESS) {
rightButtonStateDown = true;
} else if (action == GLFW_RELEASE) {
rightButtonStateDown = false;
}
} else if (button == GLFW_MOUSE_BUTTON_LEFT) {
if (action == GLFW_PRESS) {
leftButtonStateDown = true;
} else if (action == GLFW_RELEASE) {
leftButtonStateDown = false;
}
}
});
glfwSetScrollCallback(window, (window, xoffset, yoffset) -> {
whellOffsetY += yoffset;
whellOffsetX += xoffset;
});
// Get the thread stack and push a new frame
try ( MemoryStack stack = stackPush() ) {
IntBuffer pWidth = stack.mallocInt(1); // int*
@ -224,18 +266,25 @@ public class DisplayManager {
// Run the rendering loop until the user has attempted to close
// the window or has pressed the ESCAPE key.
while ( !glfwWindowShouldClose(window) ) {
fps.tic();
long currentFrameTime = getCurrentTime();
delta = (currentFrameTime-lastFrameTime)/1000f;
lastFrameTime = currentFrameTime;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer
if (this.drawer != null) {
fps.incrementCounter();
this.drawer.draw();
}
lastMousePositionX = currentMousePositionX;
lastMousePositionY = currentMousePositionY;
whellOffsetY = 0;
whellOffsetY = 0;
glfwSwapBuffers(window); // swap the color buffers
// Poll for window events. The key callback above will only be
// invoked during this call.
glfwPollEvents();
fps.toc();
fps.draw();
}
}
@ -246,6 +295,28 @@ public class DisplayManager {
private static long getCurrentTime() {
return System.currentTimeMillis();
}
public static boolean isButtonRightDown() {
return rightButtonStateDown;
}
public static boolean isButtonLeftDown() {
return leftButtonStateDown;
}
public static float getDX() {
return (float) (currentMousePositionX-lastMousePositionX);
}
public static float getDY() {
return (float) (currentMousePositionY-lastMousePositionY);
}
public static float getDWheel() {
// TODO Auto-generated method stub
return (float) whellOffsetY;
}
}

View File

@ -3,8 +3,8 @@ package renderEngine;
import java.util.List;
import java.util.Map;
import javax.vecmath.Matrix4f;
import javax.vecmath.Vector2f;
import org.atriaSoft.etk.math.Matrix4f;
import org.atriaSoft.etk.math.Vector2f;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;

View File

@ -5,9 +5,9 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.vecmath.Matrix4f;
import javax.vecmath.Vector2f;
import javax.vecmath.Vector3f;
import org.atriaSoft.etk.math.Matrix4f;
import org.atriaSoft.etk.math.Vector2f;
import org.atriaSoft.etk.math.Vector3f;
import org.lwjgl.opengl.GL11;
@ -115,48 +115,10 @@ public class MasterRenderer {
// projectionMatrix.m23 = -1;;
// projectionMatrix.m32 = -((2*FAR_PLANE * NEAR_PLANE) / frustrum_length);
// projectionMatrix.m33 = 0;
projectionMatrix = matPerspective(FOV, aspectRatio, NEAR_PLANE, FAR_PLANE);
projectionMatrix = Matrix4f.createMatrixPerspective(FOV, aspectRatio, NEAR_PLANE, FAR_PLANE);
}
Matrix4f matFrustum(float xmin, float xmax, float ymin, float ymax, float zNear, float zFar) {
Matrix4f tmp = new Matrix4f();
// 0 1 2 3
// 4 5 6 7
// 8 9 10 11
// 12 13 14 15
tmp.m00 = (2.0f * zNear) / (xmax - xmin);
tmp.m11 = (2.0f * zNear) / (ymax - ymin);
tmp.m22 = -(zFar + zNear) / (zFar - zNear);
tmp.m02 = (xmax + xmin) / (xmax - xmin);
tmp.m12 = (ymax + ymin) / (ymax - ymin);
tmp.m32 = -1.0f;
tmp.m23 = -(2.0f * zFar * zNear) / (zFar - zNear);
return tmp;
}
Matrix4f matPerspective(float fovx, float aspect, float zNear, float zFar) {
//TK_DEBUG("drax perspective: fovx=" << fovx << "->" << aspect << " " << zNear << "->" << zFar);
float xmax = zNear * (float)Math.atan(fovx/2.0);
float xmin = -xmax;
float ymin = xmin / aspect;
float ymax = xmax / aspect;
//TK_DEBUG("drax perspective: " << xmin << "->" << xmax << " & " << ymin << "->" << ymax << " " << zNear << "->" << zFar);
return matFrustum(xmin, xmax, ymin, ymax, zNear, zFar);
}
Matrix4f matOrtho(float left, float right, float bottom, float top, float nearVal, float farVal) {
Matrix4f tmp = new Matrix4f();
tmp.m00 = 2.0f / (right - left);
tmp.m11 = 2.0f / (top - bottom);
tmp.m22 = -2.0f / (farVal - nearVal);
tmp.m03 = -1*(right + left) / (right - left);
tmp.m13 = -1*(top + bottom) / (top - bottom);
tmp.m23 = -1*(farVal + nearVal) / (farVal - nearVal);
tmp.m33 = 1;
return tmp;
}
}

View File

@ -2,8 +2,8 @@ package renderEngine;
import java.util.List;
import javax.vecmath.Matrix4f;
import javax.vecmath.Vector3f;
import org.atriaSoft.etk.math.Matrix4f;
import org.atriaSoft.etk.math.Vector3f;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;

View File

@ -5,8 +5,8 @@ import java.io.FileReader;
import java.io.IOException;
import java.nio.FloatBuffer;
import javax.vecmath.Matrix4f;
import javax.vecmath.Vector3f;
import org.atriaSoft.etk.math.Matrix4f;
import org.atriaSoft.etk.math.Vector3f;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
@ -75,13 +75,14 @@ public abstract class ShaderProgram {
}
protected void loadMatrix(int location, Matrix4f value) {
float[] values = new float[] {
value.m00, value.m01, value.m02, value.m03,
value.m10, value.m11, value.m12, value.m13,
value.m20, value.m21, value.m22, value.m23,
value.m30, value.m31, value.m32, value.m33
};
GL20.glUniformMatrix4fv(location, true, values);
// float[] values = new float[] {
// value.m00, value.m01, value.m02, value.m03,
// value.m10, value.m11, value.m12, value.m13,
// value.m20, value.m21, value.m22, value.m23,
// value.m30, value.m31, value.m32, value.m33
// };
// GL20.glUniformMatrix4fv(location, true, values);
GL20.glUniformMatrix4fv(location, true, value.getTable());
}
private static int loadShader(String file, int type) {

View File

@ -1,7 +1,7 @@
package shaders;
import javax.vecmath.Matrix4f;
import javax.vecmath.Vector3f;
import org.atriaSoft.etk.math.Matrix4f;
import org.atriaSoft.etk.math.Vector3f;
import entities.Camera;
import entities.Light;

View File

@ -1,7 +1,7 @@
package shaders;
import javax.vecmath.Matrix4f;
import javax.vecmath.Vector3f;
import org.atriaSoft.etk.math.Matrix4f;
import org.atriaSoft.etk.math.Vector3f;
import entities.Camera;
import entities.Light;

View File

@ -1,8 +1,7 @@
package toolbox;
import javax.vecmath.Matrix3f;
import javax.vecmath.Matrix4f;
import javax.vecmath.Vector3f;
import org.atriaSoft.etk.math.Matrix4f;
import org.atriaSoft.etk.math.Vector3f;
import entities.Camera;
@ -12,38 +11,22 @@ public class Maths {
// Need to rework all of this this is really not optimum ...
Matrix4f matrix = new Matrix4f();
matrix.setIdentity();
Matrix3f rotationMatX = new Matrix3f();
rotationMatX.rotX(rotation.x);
Matrix3f rotationMatY = new Matrix3f();
rotationMatY.rotY(rotation.y);
Matrix3f rotationMatZ = new Matrix3f();
rotationMatZ.rotZ(rotation.z);
rotationMatX.mul(rotationMatY);
rotationMatX.mul(rotationMatZ);
matrix.set(rotationMatX, translation, scale);
// matrix.rotX(rotation.x);
// matrix.rotY(rotation.y);
// matrix.rotZ(rotation.z);
// System.out.println("elementPosition :" + translation + " " + matrix) ;
// matrix.setTranslation(translation);
// matrix.setScale(scale);
//System.out.println("elementPosition :" + translation + " " + matrix) ;
matrix.translate(translation);
matrix.rotate(new Vector3f(1,0,0), rotation.x);
matrix.rotate(new Vector3f(0,1,0), rotation.y);
matrix.rotate(new Vector3f(0,0,1), rotation.z);
matrix.scale(scale);
return matrix;
}
public static Matrix4f createViewMatrix(Camera camera) {
// Need to rework all of this this is really not optimum ...
Matrix4f matrix = new Matrix4f();
matrix.setIdentity();
Matrix3f rotationMatX = new Matrix3f();
rotationMatX.rotX(camera.getPitch());
Matrix3f rotationMatY = new Matrix3f();
rotationMatY.rotY(camera.getYaw());
Matrix3f rotationMatZ = new Matrix3f();
rotationMatZ.rotZ(camera.getRoll());
rotationMatX.mul(rotationMatY);
rotationMatX.mul(rotationMatZ);
matrix.rotate(new Vector3f(1,0,0), camera.getPitch());
matrix.rotate(new Vector3f(0,1,0), camera.getYaw());
//matrix.rotate(new Vector3f(0,0,1), camera.getRoll());
Vector3f camarePos = camera.getPosition();
matrix.set(rotationMatX, new Vector3f(-camarePos.x,-camarePos.y,-camarePos.z), 1);
matrix.translate(new Vector3f(-camarePos.x,-camarePos.y,-camarePos.z));
return matrix;
}
}