Merge branch 'worldgen' of https://github.com/halotroop/Ginger3D into worldgen
commit
58e580a497
|
@ -1,7 +1,8 @@
|
|||
package com.github.halotroop.litecraft;
|
||||
|
||||
import org.joml.Vector3f;
|
||||
|
||||
import com.github.hydos.ginger.engine.elements.objects.RenderObject;
|
||||
import com.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
import com.github.hydos.ginger.engine.render.models.TexturedModel;
|
||||
|
||||
public abstract class Entity extends RenderObject
|
||||
|
|
|
@ -1,22 +1,21 @@
|
|||
package com.github.halotroop.litecraft;
|
||||
|
||||
import java.util.Random;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.joml.Vector4i;
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
import org.joml.*;
|
||||
|
||||
import com.github.halotroop.litecraft.save.LitecraftSave;
|
||||
import com.github.halotroop.litecraft.screens.TitleScreen;
|
||||
import com.github.halotroop.litecraft.types.block.*;
|
||||
import com.github.halotroop.litecraft.types.block.Blocks;
|
||||
import com.github.halotroop.litecraft.util.RelativeDirection;
|
||||
import com.github.halotroop.litecraft.world.World;
|
||||
import com.github.halotroop.litecraft.world.gen.*;
|
||||
import com.github.halotroop.litecraft.world.dimension.Dimensions;
|
||||
import com.github.hydos.ginger.engine.api.*;
|
||||
import com.github.hydos.ginger.engine.api.game.*;
|
||||
import com.github.hydos.ginger.engine.cameras.*;
|
||||
import com.github.hydos.ginger.engine.elements.objects.*;
|
||||
import com.github.hydos.ginger.engine.font.FontType;
|
||||
import com.github.hydos.ginger.engine.io.Window;
|
||||
import com.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
import com.github.hydos.ginger.engine.obj.ModelLoader;
|
||||
import com.github.hydos.ginger.engine.obj.shapes.StaticCube;
|
||||
import com.github.hydos.ginger.engine.render.MasterRenderer;
|
||||
|
@ -32,6 +31,8 @@ public class Litecraft extends Game
|
|||
private LitecraftSave save;
|
||||
private Ginger ginger3D;
|
||||
private static Litecraft INSTANCE;
|
||||
public Player player;
|
||||
private Camera camera;
|
||||
//temp stuff to test out fbo fixes
|
||||
int oldWindowWidth = Window.width;
|
||||
int oldWindowHeight = Window.height;
|
||||
|
@ -43,59 +44,99 @@ public class Litecraft extends Game
|
|||
{
|
||||
Litecraft.INSTANCE = this;
|
||||
dbgStats = new Vector4i();
|
||||
Constants.movementSpeed = 0.5f;
|
||||
Constants.turnSpeed = 0.00006f;
|
||||
Constants.gravity = new org.joml.Vector3f(0, -0.0000000005f, 0);
|
||||
Constants.jumpPower = 0.00005f;
|
||||
Window.create(1200, 800, "LiteCraft", 60);
|
||||
KeyCallbackHandler.trackWindow(Window.window);
|
||||
MouseCallbackHandler.trackWindow(Window.window);
|
||||
setupKeybinds();
|
||||
|
||||
Block b = Blocks.AIR; // make sure blocks are initialised
|
||||
|
||||
GingerUtils.init();
|
||||
Window.setBackgroundColour(0.2f, 0.2f, 0.6f);
|
||||
TexturedModel dirtModel = ModelLoader.loadGenericCube("block/cubes/stone/brick/stonebrick.png");
|
||||
StaticCube.scaleCube(1f);
|
||||
Player player = new Player(dirtModel, new Vector3f(0, 0, -3), 0, 180f, 0, new Vector3f(0.2f, 0.2f, 0.2f));
|
||||
Camera camera = new FirstPersonCamera(player);
|
||||
player.isVisible = false;
|
||||
ginger3D = new Ginger();
|
||||
data = new GameData(player, camera, 20);
|
||||
data.handleGuis = false;
|
||||
ginger3D.setup(new MasterRenderer(camera), INSTANCE);
|
||||
FontType font = new FontType(Loader.loadFontAtlas("candara.png"), "candara.fnt");
|
||||
ginger3D.setGlobalFont(font);
|
||||
Light sun = new Light(new Vector3f(100, 105, -100), new Vector3f(1.3f, 1.3f, 1.3f), new Vector3f(0.0001f, 0.0001f, 0.0001f));
|
||||
data.lights.add(sun);
|
||||
data.entities.add(player);
|
||||
oldWindowWidth = Window.width;
|
||||
oldWindowHeight = Window.height;
|
||||
frameTimer = System.currentTimeMillis();
|
||||
// set constants
|
||||
this.setupConstants();
|
||||
this.setupWindow();
|
||||
Blocks.setup(); // make sure blocks are initialised
|
||||
GingerUtils.init(); // set up ginger utilities
|
||||
Window.setBackgroundColour(0.2f, 0.2f, 0.6f); // set the window refresh colour
|
||||
// set up Ginger3D stuff
|
||||
this.setupGinger();
|
||||
this.oldWindowWidth = Window.width;
|
||||
this.oldWindowHeight = Window.height;
|
||||
this.frameTimer = System.currentTimeMillis();
|
||||
setupKeybinds(); // set up keybinds
|
||||
//start the game loop
|
||||
ginger3D.startGame();
|
||||
this.ginger3D.startGame();
|
||||
}
|
||||
|
||||
private void setupConstants()
|
||||
{
|
||||
Constants.movementSpeed = 0.5f; // movement speed
|
||||
Constants.turnSpeed = 0.00006f; // turn speed
|
||||
Constants.gravity = new Vector3f(0, -0.0000000005f, 0); // compute gravity as a vec3f
|
||||
Constants.jumpPower = 0.00005f; // jump power
|
||||
}
|
||||
|
||||
private void setupWindow()
|
||||
{
|
||||
Window.create(1200, 800, "LiteCraft", 60); // create window
|
||||
KeyCallbackHandler.trackWindow(Window.window); // set up the gateways keybind key tracking
|
||||
MouseCallbackHandler.trackWindow(Window.window);
|
||||
}
|
||||
|
||||
private void setupKeybinds()
|
||||
{
|
||||
Input.addPressCallback(Keybind.EXIT, this::exit);
|
||||
Input.addPressCallback(Keybind.FULLSCREEN, Window::fullscreen);
|
||||
Input.addInitialPressCallback(Keybind.FULLSCREEN, Window::fullscreen);
|
||||
Input.addInitialPressCallback(Keybind.WIREFRAME, GingerRegister.getInstance()::toggleWireframe);
|
||||
Input.addPressCallback(Keybind.MOVE_FORWARD, () -> this.movePlayer(RelativeDirection.FORWARD));
|
||||
Input.addPressCallback(Keybind.MOVE_BACKWARD, () -> this.movePlayer(RelativeDirection.BACKWARD));
|
||||
Input.addPressCallback(Keybind.STRAFE_LEFT, () -> this.movePlayer(RelativeDirection.LEFT));
|
||||
Input.addPressCallback(Keybind.STRAFE_RIGHT, () -> this.movePlayer(RelativeDirection.RIGHT));
|
||||
Input.addPressCallback(Keybind.FLY_UP, () -> this.movePlayer(RelativeDirection.UP));
|
||||
Input.addPressCallback(Keybind.FLY_DOWN, () -> this.movePlayer(RelativeDirection.DOWN));
|
||||
}
|
||||
|
||||
private void setupGinger() {
|
||||
TexturedModel playerModel = ModelLoader.loadGenericCube("block/cubes/stone/brick/stonebrick.png");
|
||||
StaticCube.scaleCube(1f);
|
||||
this.player = new Player(playerModel, new Vector3f(0, 0, -3), 0, 180f, 0, new Vector3f(0.2f, 0.2f, 0.2f));
|
||||
this.camera = new FirstPersonCamera(player);
|
||||
this.player.isVisible = false;
|
||||
this.ginger3D = new Ginger();
|
||||
this.data = new GameData(this.player, this.camera, 20);
|
||||
this.data.handleGuis = false;
|
||||
this.ginger3D.setup(new MasterRenderer(this.camera), INSTANCE);
|
||||
FontType font = new FontType(Loader.loadFontAtlas("candara.png"), "candara.fnt");
|
||||
this.ginger3D.setGlobalFont(font);
|
||||
Light sun = new Light(new Vector3f(100, 105, -100), new Vector3f(1.3f, 1.3f, 1.3f), new Vector3f(0.0001f, 0.0001f, 0.0001f));
|
||||
this.data.lights.add(sun);
|
||||
this.data.entities.add(this.player);
|
||||
}
|
||||
|
||||
public Camera getCamera()
|
||||
{ return this.camera; }
|
||||
|
||||
@Override
|
||||
public void exit()
|
||||
{
|
||||
if (this.world != null)
|
||||
{
|
||||
System.out.println("Saving chunks...");
|
||||
this.world.unloadAllChunks();
|
||||
ginger3D.cleanup();
|
||||
try
|
||||
{
|
||||
this.save.saveGlobalData(this.world.getSeed(), this.player);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
System.err.println("A critical error occurred while trying to save world data!");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
ginger3D.cleanup();
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
private void movePlayer(RelativeDirection direction) {
|
||||
this.player.move(direction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render()
|
||||
{
|
||||
fps++;
|
||||
this.fps++;
|
||||
//FPS stuff sorry if i forget to remove whitespace
|
||||
if (System.currentTimeMillis() > frameTimer + 1000) // wait for one second
|
||||
{
|
||||
|
@ -105,19 +146,20 @@ public class Litecraft extends Game
|
|||
this.tps = 0;
|
||||
this.frameTimer += 1000; // reset the wait time
|
||||
}
|
||||
// TODO pls comment this code
|
||||
if (ginger3D.gingerRegister.currentScreen == null)
|
||||
ginger3D.openScreen(new TitleScreen());
|
||||
ginger3D.update(data);
|
||||
this.ginger3D.openScreen(new TitleScreen());
|
||||
this.ginger3D.update(data);
|
||||
if (oldWindowHeight != Window.height || oldWindowWidth != Window.width)
|
||||
ginger3D.contrastFbo.resizeFBOs();
|
||||
oldWindowWidth = Window.width;
|
||||
oldWindowHeight = Window.height;
|
||||
ginger3D.gingerRegister.masterRenderer.renderShadowMap(data.entities, data.lights.get(0));
|
||||
this.ginger3D.contrastFbo.resizeFBOs();
|
||||
this.oldWindowWidth = Window.width;
|
||||
this.oldWindowHeight = Window.height;
|
||||
this.ginger3D.gingerRegister.masterRenderer.renderShadowMap(data.entities, data.lights.get(0));
|
||||
if (this.world != null)
|
||||
ginger3D.renderWorld(this, this.world);
|
||||
ginger3D.renderOverlays(this);
|
||||
ginger3D.postRender();
|
||||
dbgStats.w = binds;
|
||||
this.ginger3D.renderWorld(this, this.world);
|
||||
this.ginger3D.renderOverlays(this);
|
||||
this.ginger3D.postRender();
|
||||
this.dbgStats.w = binds;
|
||||
this.binds = 0;
|
||||
}
|
||||
|
||||
|
@ -127,8 +169,6 @@ public class Litecraft extends Game
|
|||
tps++;
|
||||
Input.invokeAllListeners();
|
||||
data.player.updateMovement();
|
||||
if (Window.isKeyDown(GLFW.GLFW_KEY_TAB))
|
||||
ginger3D.gingerRegister.wireframe = !ginger3D.gingerRegister.wireframe;
|
||||
}
|
||||
|
||||
public static Litecraft getInstance()
|
||||
|
@ -140,6 +180,12 @@ public class Litecraft extends Game
|
|||
{
|
||||
this.save = new LitecraftSave("test", false);
|
||||
this.world = this.save.getWorldOrCreate(Dimensions.OVERWORLD);
|
||||
}
|
||||
ginger3D.setGingerPlayer(this.world.player);
|
||||
}
|
||||
}
|
||||
|
||||
public World getWorld()
|
||||
{
|
||||
return this.world;
|
||||
}
|
||||
}
|
|
@ -2,7 +2,9 @@ package com.github.halotroop.litecraft.logic;
|
|||
|
||||
import tk.valoeghese.sod.BinaryData;
|
||||
|
||||
public interface DataStorage {
|
||||
public interface DataStorage
|
||||
{
|
||||
void read(BinaryData data);
|
||||
|
||||
void write(BinaryData data);
|
||||
}
|
||||
|
|
|
@ -12,10 +12,10 @@ public class Timer
|
|||
{
|
||||
void onTick(float deltaTime);
|
||||
}
|
||||
|
||||
private double lastTick;
|
||||
private double nextTick;
|
||||
private int tickRate;
|
||||
|
||||
private Set<TickListener> tickListeners = new HashSet<>();
|
||||
|
||||
public Timer(int tickRate)
|
||||
|
|
|
@ -3,8 +3,13 @@ package com.github.halotroop.litecraft.save;
|
|||
import java.io.*;
|
||||
import java.util.Random;
|
||||
|
||||
import org.joml.Vector3f;
|
||||
|
||||
import com.github.halotroop.litecraft.Litecraft;
|
||||
import com.github.halotroop.litecraft.world.*;
|
||||
import com.github.halotroop.litecraft.world.gen.*;
|
||||
import com.github.halotroop.litecraft.world.dimension.Dimension;
|
||||
import com.github.hydos.ginger.engine.cameras.Camera;
|
||||
import com.github.hydos.ginger.engine.elements.objects.Player;
|
||||
|
||||
import tk.valoeghese.sod.*;
|
||||
|
||||
|
@ -14,7 +19,6 @@ public final class LitecraftSave
|
|||
{
|
||||
StringBuilder sb = new StringBuilder(SAVE_DIR).append(name);
|
||||
File saveDir = new File(sb.toString());
|
||||
|
||||
if (mustCreateNew)
|
||||
{
|
||||
while (saveDir.exists())
|
||||
|
@ -23,7 +27,6 @@ public final class LitecraftSave
|
|||
saveDir = new File(sb.toString());
|
||||
}
|
||||
}
|
||||
|
||||
this.file = saveDir;
|
||||
this.file.mkdirs();
|
||||
}
|
||||
|
@ -33,19 +36,16 @@ public final class LitecraftSave
|
|||
public boolean saveChunk(Chunk chunk)
|
||||
{
|
||||
StringBuilder fileLocBuilder = new StringBuilder(this.file.getPath())
|
||||
.append('/').append(chunk.dimension)
|
||||
.append('/').append(chunk.chunkX)
|
||||
.append('/').append(chunk.chunkZ);
|
||||
.append('/').append(chunk.dimension)
|
||||
.append('/').append(chunk.chunkX)
|
||||
.append('/').append(chunk.chunkZ);
|
||||
File chunkDir = new File(fileLocBuilder.toString());
|
||||
chunkDir.mkdirs(); // create directory for file if it does not exist
|
||||
|
||||
// format: <save dir>/<dim>/<chunkX>/<chunkZ>/<chunkY>.sod
|
||||
File chunkFile = new File(fileLocBuilder.append('/').append(chunk.chunkY).append(".sod").toString());
|
||||
|
||||
try
|
||||
{
|
||||
chunkFile.createNewFile();
|
||||
|
||||
BinaryData data = new BinaryData(); // create new empty binary data
|
||||
chunk.write(data); // write the chunk info to the binary data
|
||||
return data.write(chunkFile); // write the data to the file, return whether an io exception occurred
|
||||
|
@ -61,15 +61,13 @@ public final class LitecraftSave
|
|||
{
|
||||
// format: <save dir>/<dim>/<chunkX>/<chunkZ>/<chunkY>.sod
|
||||
File chunkFile = new File(new StringBuilder(this.file.getPath())
|
||||
.append('/').append(dimension)
|
||||
.append('/').append(chunkX)
|
||||
.append('/').append(chunkZ)
|
||||
.append('/').append(chunkY).append(".sod").toString());
|
||||
|
||||
.append('/').append(dimension)
|
||||
.append('/').append(chunkX)
|
||||
.append('/').append(chunkZ)
|
||||
.append('/').append(chunkY).append(".sod").toString());
|
||||
if (chunkFile.isFile())
|
||||
{
|
||||
BinaryData data = BinaryData.read(chunkFile);
|
||||
|
||||
Chunk result = new Chunk(chunkX, chunkY, chunkZ, dimension); // create chunk
|
||||
result.read(data); // load the chunk data we have just read into the chunk
|
||||
return result;
|
||||
|
@ -79,51 +77,79 @@ public final class LitecraftSave
|
|||
|
||||
public World getWorldOrCreate(Dimension<?> dim)
|
||||
{
|
||||
File worldFile = new File(this.file.getPath() + "/global_data.sod");
|
||||
|
||||
if (worldFile.isFile()) // load world
|
||||
File globalDataFile = new File(this.file.getPath() + "/global_data.sod");
|
||||
if (globalDataFile.isFile()) // load world
|
||||
{
|
||||
BinaryData data = BinaryData.read(worldFile); // read data from the world file
|
||||
BinaryData data = BinaryData.read(globalDataFile); // read data from the world file
|
||||
DataSection properties = data.get("properties"); // get the properties data section from the data that we have just read
|
||||
DataSection playerData = data.get("player");
|
||||
long seed = 0; // default seed if we cannot read it is 0
|
||||
|
||||
float playerX = 0, playerY = 0, playerZ = -3; // default player x/y/z
|
||||
float playerYaw = 0.0f;
|
||||
try // try read the seed from the file
|
||||
{
|
||||
seed = properties.readLong(0); // seed is at index 0
|
||||
playerX = playerData.readFloat(0); // player x/y/z is at index 0/1/2 respectively
|
||||
playerY = playerData.readFloat(1);
|
||||
playerZ = playerData.readFloat(2);
|
||||
Camera camera = Litecraft.getInstance().getCamera(); // get camera
|
||||
camera.setPitch(playerData.readFloat(3)); // read pitch, yaw, roll from 3/4/5
|
||||
playerYaw = playerData.readFloat(4);
|
||||
camera.setYaw(playerYaw);
|
||||
camera.setRoll(playerData.readFloat(5));
|
||||
}
|
||||
catch (Throwable e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
System.out.println("Exception in reading save data! This may be benign, merely an artifact of an update to the contents of the world save data.");
|
||||
}
|
||||
|
||||
World world = new World(seed, 10, dim, this); // create new world with seed read from file or 0, if it could not be read
|
||||
world.spawnPlayer(); // spawn player in world
|
||||
World world = new World(seed, RENDER_SIZE, dim, this); // create new world with seed read from file or 0, if it could not be read
|
||||
world.spawnPlayer(playerX, playerY, playerZ); // spawn player in world
|
||||
return world;
|
||||
}
|
||||
else // create world
|
||||
{
|
||||
long seed = new Random().nextLong();
|
||||
|
||||
try
|
||||
{
|
||||
worldFile.createNewFile(); // create world file
|
||||
BinaryData data = new BinaryData(); // create empty binary data
|
||||
DataSection properties = new DataSection(); // create empty data section for properties
|
||||
properties.writeLong(seed); // write seed at index 0
|
||||
data.put("properties", properties); // add properties section
|
||||
data.write(worldFile); // write to file
|
||||
globalDataFile.createNewFile(); // create world file
|
||||
this.writeGlobalData(globalDataFile, seed, new Vector3f(0, 0, -3)); // write global data with default player pos
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
// If this fails the world seed will not be consistent across saves
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
World world = new World(seed, 2, dim, this); // create new world with generated seed
|
||||
World world = new World(seed, RENDER_SIZE, dim, this); // create new world with generated seed
|
||||
world.spawnPlayer(); // spawn player in world
|
||||
return world;
|
||||
}
|
||||
}
|
||||
|
||||
public void saveGlobalData(long seed, Player player) throws IOException
|
||||
{
|
||||
File globalDataFile = new File(this.file.getPath() + "/global_data.sod");
|
||||
globalDataFile.createNewFile(); // create world file if it doesn't exist.
|
||||
writeGlobalData(globalDataFile, seed, player.getPosition());
|
||||
}
|
||||
|
||||
private void writeGlobalData(File globalDataFile, long seed, Vector3f playerPos)
|
||||
{
|
||||
BinaryData data = new BinaryData(); // create empty binary data
|
||||
DataSection properties = new DataSection(); // create empty data section for properties
|
||||
properties.writeLong(seed); // write seed at index 0
|
||||
DataSection playerData = new DataSection();
|
||||
playerData.writeFloat(playerPos.x); // default spawn player x/y/z
|
||||
playerData.writeFloat(playerPos.y);
|
||||
playerData.writeFloat(playerPos.z);
|
||||
Camera camera = Litecraft.getInstance().getCamera();
|
||||
playerData.writeFloat(camera.getPitch());
|
||||
playerData.writeFloat(camera.getYaw());
|
||||
playerData.writeFloat(camera.getRoll());
|
||||
data.put("properties", properties); // add properties section to data
|
||||
data.put("player", playerData); // add player section to data
|
||||
data.write(globalDataFile); // write to file
|
||||
}
|
||||
|
||||
private static final String SAVE_DIR = "./saves/";
|
||||
private static final int RENDER_SIZE = 8;
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package com.github.halotroop.litecraft.screens;
|
|||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.joml.Vector4i;
|
||||
import org.joml.*;
|
||||
|
||||
import com.github.halotroop.litecraft.Litecraft;
|
||||
import com.github.hydos.ginger.engine.api.Ginger;
|
||||
|
@ -10,20 +10,17 @@ import com.github.hydos.ginger.engine.elements.GuiTexture;
|
|||
import com.github.hydos.ginger.engine.elements.buttons.TextureButton;
|
||||
import com.github.hydos.ginger.engine.font.GUIText;
|
||||
import com.github.hydos.ginger.engine.io.Window;
|
||||
import com.github.hydos.ginger.engine.math.vectors.Vector2f;
|
||||
import com.github.hydos.ginger.engine.screen.Screen;
|
||||
|
||||
/*
|
||||
* YeS
|
||||
*/
|
||||
public class TitleScreen extends Screen
|
||||
{
|
||||
{
|
||||
GUIText buildText;
|
||||
|
||||
Ginger ginger3D;
|
||||
|
||||
TextureButton playButton;
|
||||
|
||||
|
||||
public TitleScreen()
|
||||
{
|
||||
ginger3D = Ginger.getInstance();
|
||||
|
@ -33,18 +30,16 @@ public class TitleScreen extends Screen
|
|||
buildText = ginger3D.registerText("LiteCraft", 3, new Vector2f(0, 0), 1f, true, "PLAYBUTTON");
|
||||
buildText.setBorderWidth(0.5f);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void render() // FIXME: This never gets called!!!
|
||||
{
|
||||
|
||||
}
|
||||
{}
|
||||
|
||||
@Override
|
||||
public void tick()
|
||||
{
|
||||
Vector4i dbg = Litecraft.getInstance().dbgStats;
|
||||
buildText.setText("FPS: "+dbg.x()+" UPS: "+dbg.y+" TPS: "+dbg.z+" Binds: "+dbg.w);
|
||||
buildText.setText("FPS: " + dbg.x() + " UPS: " + dbg.y + " TPS: " + dbg.z + " Binds: " + dbg.w);
|
||||
playButton.update();
|
||||
if (playButton.isClicked())
|
||||
{
|
||||
|
|
|
@ -55,9 +55,10 @@ public class Block
|
|||
}
|
||||
|
||||
public static final Block getBlock(String identifier)
|
||||
{
|
||||
return IDENTIFIER_TO_BLOCK.get(identifier);
|
||||
}
|
||||
{ return IDENTIFIER_TO_BLOCK.get(identifier); }
|
||||
|
||||
public static final Block getBlockOrAir(String identifier)
|
||||
{ return IDENTIFIER_TO_BLOCK.getOrDefault(identifier, Blocks.AIR); }
|
||||
|
||||
private static final Map<String, Block> IDENTIFIER_TO_BLOCK = new HashMap<>();
|
||||
}
|
||||
|
|
|
@ -1,20 +0,0 @@
|
|||
package com.github.halotroop.litecraft.types.block;
|
||||
|
||||
import com.github.halotroop.litecraft.world.Chunk;
|
||||
import com.github.hydos.ginger.engine.elements.objects.RenderObject;
|
||||
import com.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
|
||||
public class BlockEntity extends RenderObject
|
||||
{
|
||||
|
||||
|
||||
public BlockEntity(Block block, Vector3f position)
|
||||
{
|
||||
super(block.model, position, 0, 0, 0, new Vector3f(1f, 1f, 1f));
|
||||
}
|
||||
|
||||
public void processCulling(Chunk chunk) {
|
||||
Vector3f southNeighbourBlockLocation = this.getPosition();
|
||||
southNeighbourBlockLocation.x--;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.github.halotroop.litecraft.types.block;
|
||||
|
||||
import org.joml.Vector3f;
|
||||
|
||||
import com.github.halotroop.litecraft.world.Chunk;
|
||||
import com.github.hydos.ginger.engine.elements.objects.RenderObject;
|
||||
|
||||
public class BlockInstance extends RenderObject
|
||||
{
|
||||
public BlockInstance(Block block, Vector3f position)
|
||||
{ super(block.model, position, 0, 0, 0, new Vector3f(1f, 1f, 1f)); }
|
||||
|
||||
public void processCulling(Chunk chunk)
|
||||
{
|
||||
Vector3f southNeighbourBlockLocation = this.getPosition();
|
||||
southNeighbourBlockLocation.x--;
|
||||
}
|
||||
}
|
|
@ -6,5 +6,12 @@ public final class Blocks
|
|||
{
|
||||
public static final Block AIR = new Block(new Properties("air").visible(false));
|
||||
public static final Block DIRT = new Block("block/cubes/soil/dirt.png", new Properties("dirt"));
|
||||
public static final Block STONE = new Block("block/cubes/stone/basic/gneiss.png", new Properties("stone"));
|
||||
public static final Block ANDESITE = new Block("block/cubes/stone/basic/andesite.png", new Properties("andesite"));
|
||||
public static final Block DIORITE = new Block("block/cubes/stone/basic/diorite.png", new Properties("diorite"));
|
||||
public static final Block GRANITE = new Block("block/cubes/stone/basic/granite.png", new Properties("granite"));
|
||||
public static final Block GNEISS = new Block("block/cubes/stone/basic/gneiss.png", new Properties("gneiss"));
|
||||
public static Block setup()
|
||||
{
|
||||
return AIR;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
package com.github.halotroop.litecraft.util;
|
||||
|
||||
public enum RelativeDirection
|
||||
{
|
||||
UP, // up up
|
||||
DOWN, // down down
|
||||
LEFT, // >left< right
|
||||
RIGHT, // left >right<
|
||||
FORWARD,// b
|
||||
BACKWARD,// a
|
||||
}
|
|
@ -8,16 +8,17 @@ public final class OctaveSimplexNoise
|
|||
private double clamp;
|
||||
private double spread, amplitudeLow, amplitudeHigh;
|
||||
|
||||
public OctaveSimplexNoise(Random rand, int octaves)
|
||||
{
|
||||
this(rand, octaves, 1D, 1D, 1D);
|
||||
}
|
||||
|
||||
public OctaveSimplexNoise(Random rand, int octaves, double spread, double amplitudeHigh, double amplitudeLow)
|
||||
{
|
||||
this.samplers = new SimplexNoise[octaves];
|
||||
this.clamp = 1D / (1D - (1D / Math.pow(2, octaves)));
|
||||
|
||||
for (int i = 0; i < octaves; ++i)
|
||||
{
|
||||
samplers[i] = new SimplexNoise(rand.nextLong());
|
||||
}
|
||||
|
||||
{ samplers[i] = new SimplexNoise(rand.nextLong()); }
|
||||
this.spread = spread;
|
||||
this.amplitudeLow = amplitudeLow;
|
||||
this.amplitudeHigh = amplitudeHigh;
|
||||
|
@ -25,15 +26,27 @@ public final class OctaveSimplexNoise
|
|||
|
||||
public double sample(double x, double y)
|
||||
{
|
||||
double amplFreq = 0.5D;
|
||||
double amplSpread = 0.5D;
|
||||
double result = 0;
|
||||
|
||||
for (SimplexNoise sampler : this.samplers)
|
||||
{
|
||||
result += (amplFreq * sampler.sample(x / (amplFreq * this.spread), y / (amplFreq * this.spread)));
|
||||
amplFreq *= 0.5D;
|
||||
result += (amplSpread * sampler.sample(x / (amplSpread * this.spread), y / (amplSpread * this.spread)));
|
||||
amplSpread *= 0.5D;
|
||||
}
|
||||
result = result * this.clamp;
|
||||
return result > 0 ? result * this.amplitudeHigh : result * this.amplitudeLow;
|
||||
}
|
||||
|
||||
public double sample(double x, double y, double z)
|
||||
{
|
||||
double amplSpread = 0.5D;
|
||||
double result = 0;
|
||||
for (SimplexNoise sampler : this.samplers)
|
||||
{
|
||||
double divisor = amplSpread * this.spread;
|
||||
result += (amplSpread * sampler.sample(x / divisor, y / divisor, z / divisor));
|
||||
amplSpread *= 0.5D;
|
||||
}
|
||||
result = result * this.clamp;
|
||||
return result > 0 ? result * this.amplitudeHigh : result * this.amplitudeLow;
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -5,5 +5,6 @@ import com.github.halotroop.litecraft.types.block.Block;
|
|||
public interface BlockAccess
|
||||
{
|
||||
Block getBlock(int x, int y, int z);
|
||||
|
||||
void setBlock(int x, int y, int z, Block block);
|
||||
}
|
|
@ -1,16 +1,15 @@
|
|||
package com.github.halotroop.litecraft.world;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.ToIntFunction;
|
||||
|
||||
import org.joml.Vector3f;
|
||||
|
||||
import com.github.halotroop.litecraft.logic.DataStorage;
|
||||
import com.github.halotroop.litecraft.types.block.*;
|
||||
import com.github.halotroop.litecraft.world.block.BlockRenderer;
|
||||
import com.github.halotroop.litecraft.world.gen.WorldGenConstants;
|
||||
import com.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
|
||||
import it.unimi.dsi.fastutil.ints.*;
|
||||
import it.unimi.dsi.fastutil.longs.*;
|
||||
import it.unimi.dsi.fastutil.objects.*;
|
||||
import tk.valoeghese.sod.*;
|
||||
|
||||
|
@ -19,19 +18,19 @@ public class Chunk implements BlockAccess, WorldGenConstants, DataStorage
|
|||
/** @param x in-chunk x coordinate.
|
||||
* @param y in-chunk y coordinate.
|
||||
* @param z in-chunk z coordinate.
|
||||
* @return creates a long that represents a coordinate, for use as a key in maps. */
|
||||
private static long posHash(int x, int y, int z)
|
||||
{ return ((long) x & MAX_POS) | (((long) y & MAX_POS) << POS_SHIFT) | (((long) z & MAX_POS) << DOUBLE_SHIFT); }
|
||||
* @return creates a long that represents a coordinate, for use as a key in the array. */
|
||||
public static int index(int x, int y, int z)
|
||||
{ return (x & MAX_POS) | ((y & MAX_POS) << POS_SHIFT) | ((z & MAX_POS) << DOUBLE_SHIFT); }
|
||||
|
||||
private final Block[] blocks = new Block[CHUNK_SIZE*CHUNK_SIZE*CHUNK_SIZE];
|
||||
private BlockEntity[] blockEntities = new BlockEntity[CHUNK_SIZE*CHUNK_SIZE*CHUNK_SIZE];
|
||||
private final Block[] blocks = new Block[CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE];
|
||||
private BlockInstance[] blockEntities = new BlockInstance[CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE];
|
||||
private boolean render = false;
|
||||
public final int chunkX, chunkY, chunkZ;
|
||||
public final int chunkStartX, chunkStartY, chunkStartZ;
|
||||
private boolean fullyGenerated = false;
|
||||
public final int dimension;
|
||||
private boolean dirty = true;
|
||||
private BlockEntity[] renderedBlocks;
|
||||
private BlockInstance[] renderedBlocks;
|
||||
|
||||
public Chunk(int chunkX, int chunkY, int chunkZ, int dimension)
|
||||
{
|
||||
|
@ -54,57 +53,52 @@ public class Chunk implements BlockAccess, WorldGenConstants, DataStorage
|
|||
public Block getBlock(int x, int y, int z)
|
||||
{
|
||||
if (x > CHUNK_SIZE || y > CHUNK_SIZE || z > CHUNK_SIZE || x < 0 || y < 0 || z < 0)
|
||||
{
|
||||
throw new RuntimeException("Block [" + x + ", " + y + ", " + z + ", " + "] out of chunk bounds!");
|
||||
}
|
||||
|
||||
return blocks[x*CHUNK_SIZE*CHUNK_SIZE + z*CHUNK_SIZE + y];
|
||||
{ throw new RuntimeException("Block [" + x + ", " + y + ", " + z + ", " + "] out of chunk bounds!"); }
|
||||
return blocks[index(x, y, z)];
|
||||
}
|
||||
|
||||
public BlockEntity getBlockEntity(int x, int y, int z)
|
||||
public BlockInstance getBlockInstance(int x, int y, int z)
|
||||
{
|
||||
if (x > CHUNK_SIZE || y > CHUNK_SIZE || z > CHUNK_SIZE || x < 0 || y < 0 || z < 0)
|
||||
{
|
||||
throw new RuntimeException("Block [" + x + ", " + y + ", " + z + ", " + "] out of chunk bounds!");
|
||||
}
|
||||
|
||||
return this.blockEntities[x*CHUNK_SIZE*CHUNK_SIZE + z*CHUNK_SIZE + y];
|
||||
{ throw new RuntimeException("Block [" + x + ", " + y + ", " + z + ", " + "] out of chunk bounds!"); }
|
||||
return this.blockEntities[index(x, y, z)];
|
||||
}
|
||||
|
||||
public void render(BlockRenderer blockRenderer)
|
||||
{
|
||||
|
||||
if (render)
|
||||
{
|
||||
if (dirty)
|
||||
{
|
||||
dirty = false;
|
||||
renderedBlocks = new BlockEntity[CHUNK_SIZE*CHUNK_SIZE*CHUNK_SIZE];
|
||||
for(int x = 0; x < CHUNK_SIZE; x++)
|
||||
renderedBlocks = new BlockInstance[CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE];
|
||||
for (int x = 0; x < CHUNK_SIZE; x++)
|
||||
{
|
||||
for(int y = 0; y < CHUNK_SIZE; y++)
|
||||
for (int y = 0; y < CHUNK_SIZE; y++)
|
||||
{
|
||||
for(int z = 0; z < CHUNK_SIZE; z++)
|
||||
for (int z = 0; z < CHUNK_SIZE; z++)
|
||||
{
|
||||
BlockEntity block = getBlockEntity(x, y, z);
|
||||
if (x == 0 || x == CHUNK_SIZE-1 || z == 0 || z == CHUNK_SIZE-1 || y == 0 || y == CHUNK_SIZE-1)
|
||||
BlockInstance block = getBlockInstance(x, y, z);
|
||||
if (x == 0 || x == CHUNK_SIZE - 1 || z == 0 || z == CHUNK_SIZE - 1 || y == 0 || y == CHUNK_SIZE - 1)
|
||||
{
|
||||
renderedBlocks[x*CHUNK_SIZE*CHUNK_SIZE + z*CHUNK_SIZE + y] = block;
|
||||
renderedBlocks[index(x, y, z)] = block;
|
||||
continue;
|
||||
}
|
||||
|
||||
// check for air. Yes this is stupid, TODO fix this
|
||||
if (getBlockEntity(x-1, y, z) == null || getBlockEntity(x+1, y, z) == null ||
|
||||
getBlockEntity(x, y-1, z) == null || getBlockEntity(x, y+1, z) == null ||
|
||||
getBlockEntity(x, y, z-1) == null || getBlockEntity(x, y, z+1) == null)
|
||||
{
|
||||
renderedBlocks[x*CHUNK_SIZE*CHUNK_SIZE + z*CHUNK_SIZE + y] = block;
|
||||
try {
|
||||
if (getBlockInstance(x - 1, y, z).getModel() == null || getBlockInstance(x + 1, y, z).getModel() == null ||
|
||||
getBlockInstance(x, y - 1, z).getModel() == null || getBlockInstance(x, y + 1, z).getModel() == null ||
|
||||
getBlockInstance(x, y, z - 1).getModel() == null || getBlockInstance(x, y, z + 1).getModel() == null)
|
||||
renderedBlocks[index(x, y, z)] = block;
|
||||
}
|
||||
catch (NullPointerException e) { // this seems to be a hotspot for errors
|
||||
e.printStackTrace(); // so I can add a debug breakpoint on this line
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
blockRenderer.render(renderedBlocks);
|
||||
}
|
||||
}
|
||||
|
@ -121,38 +115,31 @@ public class Chunk implements BlockAccess, WorldGenConstants, DataStorage
|
|||
if (z > MAX_POS)
|
||||
z = MAX_POS;
|
||||
else if (z < 0) z = 0;
|
||||
this.blocks[x*CHUNK_SIZE*CHUNK_SIZE + z*CHUNK_SIZE + y] = block;
|
||||
this.blocks[index(x, y, z)] = block;
|
||||
if (this.render)
|
||||
{
|
||||
this.blockEntities[x*CHUNK_SIZE*CHUNK_SIZE + z*CHUNK_SIZE + y] = new BlockEntity(block, new Vector3f(this.chunkStartX + x, this.chunkStartY + y, this.chunkStartZ + z));
|
||||
}
|
||||
{ this.blockEntities[index(x, y, z)] = new BlockInstance(block, new Vector3f(this.chunkStartX + x, this.chunkStartY + y, this.chunkStartZ + z)); }
|
||||
dirty = true;
|
||||
}
|
||||
|
||||
public void setRender(boolean render)
|
||||
{
|
||||
if (render && !this.render) // if it has been changed to true
|
||||
{
|
||||
for (int x = 0; x < CHUNK_SIZE; ++x)
|
||||
{
|
||||
for (int y = 0; y < CHUNK_SIZE; ++y)
|
||||
{
|
||||
for (int z = 0; z < CHUNK_SIZE; ++z)
|
||||
{
|
||||
Block block = this.blocks[x*CHUNK_SIZE*CHUNK_SIZE + z*CHUNK_SIZE + y];
|
||||
if (block.isVisible()) this.blockEntities[x*CHUNK_SIZE*CHUNK_SIZE + z*CHUNK_SIZE + y] = new BlockEntity(block,
|
||||
new Vector3f(
|
||||
this.chunkStartX + x,
|
||||
this.chunkStartY + y,
|
||||
this.chunkStartZ + z));
|
||||
Block block = this.blocks[index(x, y, z)];
|
||||
|
||||
this.blockEntities[index(x, y, z)] = new BlockInstance(block,
|
||||
new Vector3f(
|
||||
this.chunkStartX + x,
|
||||
this.chunkStartY + y,
|
||||
this.chunkStartZ + z));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (this.render) // else if it has been changed to false
|
||||
{
|
||||
blockEntities = new BlockEntity[CHUNK_SIZE*CHUNK_SIZE*CHUNK_SIZE];
|
||||
}
|
||||
else if (!render && this.render) // else if it has been changed to false.
|
||||
// we need to check both variables because there are two cases that make
|
||||
// the if statement fall to here
|
||||
blockEntities = new BlockInstance[CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE];
|
||||
this.render = render;
|
||||
dirty = true;
|
||||
}
|
||||
|
@ -164,11 +151,10 @@ public class Chunk implements BlockAccess, WorldGenConstants, DataStorage
|
|||
public void read(BinaryData data)
|
||||
{
|
||||
Int2ObjectMap<Block> palette = new Int2ObjectArrayMap<>();
|
||||
|
||||
DataSection paletteData = data.get("palette");
|
||||
|
||||
boolean readInt = true; // whether the thing from the palette to be read is int
|
||||
int intIdCache = 0;
|
||||
//
|
||||
for (Object o : paletteData)
|
||||
{
|
||||
if (readInt)
|
||||
|
@ -178,63 +164,79 @@ public class Chunk implements BlockAccess, WorldGenConstants, DataStorage
|
|||
}
|
||||
else
|
||||
{
|
||||
palette.put(intIdCache, Block.getBlock((String) o));
|
||||
palette.put(intIdCache, Block.getBlockOrAir((String) o));
|
||||
readInt = true;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
DataSection blockData = data.get("block");
|
||||
|
||||
long posHash = 0L; // also the index
|
||||
int index = 0;
|
||||
//
|
||||
for (int z = 0; z < CHUNK_SIZE; ++z) // z, y, x order for data saving and loading so we can use incremental pos hashes
|
||||
{
|
||||
for (int y = 0; y < CHUNK_SIZE; ++y)
|
||||
{
|
||||
for (int x = 0; x < CHUNK_SIZE; ++x)
|
||||
{
|
||||
blocks[x*CHUNK_SIZE*CHUNK_SIZE + z*CHUNK_SIZE + y] = palette.get(blockData.readInt((int) posHash));
|
||||
++posHash;
|
||||
blocks[index] = palette.get(blockData.readInt(index));
|
||||
++index;
|
||||
}
|
||||
}
|
||||
}
|
||||
//
|
||||
DataSection properties = data.get("properties");
|
||||
try
|
||||
{
|
||||
this.fullyGenerated = properties.readBoolean(0); // index 0 is the "fully generated" property
|
||||
}
|
||||
catch (Throwable e)
|
||||
{
|
||||
if (exceptionOccuredReadingNotif){
|
||||
System.out.println("An exception occurred reading properties for a chunk! This could be a benign error due to updates to chunk properties.");
|
||||
exceptionOccuredReadingNotif = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean exceptionOccuredReadingNotif = true;
|
||||
|
||||
private int nextId; // for saving
|
||||
|
||||
@Override
|
||||
public void write(BinaryData data)
|
||||
{
|
||||
Object2IntMap<Block> palette = new Object2IntArrayMap<>(); // block to int id
|
||||
|
||||
DataSection paletteData = new DataSection();
|
||||
DataSection blockData = new DataSection();
|
||||
|
||||
long posHash = 0L;
|
||||
int index = 0;
|
||||
nextId = 0;
|
||||
|
||||
ToIntFunction<Block> nextIdProvider = b -> nextId++;
|
||||
|
||||
//
|
||||
for (int z = 0; z < CHUNK_SIZE; ++z) // z, y, x order for data saving and loading so we can use incremental pos hashes
|
||||
{
|
||||
for (int y = 0; y < CHUNK_SIZE; ++y)
|
||||
{
|
||||
for (int x = 0; x < CHUNK_SIZE; ++x)
|
||||
{
|
||||
Block b = blocks[x*CHUNK_SIZE*CHUNK_SIZE + z*CHUNK_SIZE + y];
|
||||
Block b = blocks[index];
|
||||
blockData.writeInt(palette.computeIntIfAbsent(b, nextIdProvider));
|
||||
++posHash;
|
||||
++index;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
palette.forEach((b, id) -> {
|
||||
//
|
||||
palette.forEach((b, id) ->
|
||||
{
|
||||
paletteData.writeInt(id);
|
||||
paletteData.writeString(b.identifier);
|
||||
});
|
||||
|
||||
//
|
||||
data.put("palette", paletteData);
|
||||
data.put("block", blockData);
|
||||
|
||||
//
|
||||
DataSection properties = new DataSection();
|
||||
properties.writeBoolean(this.fullyGenerated);
|
||||
data.put("properties", properties);
|
||||
dirty = true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,14 +1,16 @@
|
|||
package com.github.halotroop.litecraft.world;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.*;
|
||||
import java.util.function.LongConsumer;
|
||||
|
||||
import org.joml.Vector3f;
|
||||
|
||||
import com.github.halotroop.litecraft.save.LitecraftSave;
|
||||
import com.github.halotroop.litecraft.types.block.Block;
|
||||
import com.github.halotroop.litecraft.types.block.*;
|
||||
import com.github.halotroop.litecraft.world.block.BlockRenderer;
|
||||
import com.github.halotroop.litecraft.world.dimension.Dimension;
|
||||
import com.github.halotroop.litecraft.world.gen.*;
|
||||
import com.github.hydos.ginger.engine.elements.objects.Player;
|
||||
import com.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
import com.github.hydos.ginger.engine.obj.ModelLoader;
|
||||
import com.github.hydos.ginger.engine.render.models.TexturedModel;
|
||||
|
||||
|
@ -21,15 +23,17 @@ public class World implements BlockAccess, WorldGenConstants
|
|||
private final ChunkGenerator chunkGenerator;
|
||||
private final BlockAccess genBlockAccess;
|
||||
private final LitecraftSave save;
|
||||
|
||||
private final long seed;
|
||||
private final int dimension;
|
||||
|
||||
public Player player;
|
||||
private int renderBound;
|
||||
private int renderBoundVertical;
|
||||
private final BlockInstance dummy;
|
||||
|
||||
// This will likely become the main public constructor after we add dynamic chunkloading
|
||||
private World(long seed, Dimension<?> dim, LitecraftSave save)
|
||||
public World(long seed, int renderSize, Dimension<?> dim, LitecraftSave save)
|
||||
{
|
||||
this.dummy = new BlockInstance(Blocks.ANDESITE, new Vector3f(0, 0, 0));
|
||||
this.dummy.isVisible = false;
|
||||
this.chunks = new Long2ObjectArrayMap<>();
|
||||
this.seed = seed;
|
||||
this.chunkGenerator = dim.createChunkGenerator(seed);
|
||||
|
@ -37,73 +41,101 @@ public class World implements BlockAccess, WorldGenConstants
|
|||
this.genBlockAccess = new GenerationWorld(this);
|
||||
this.save = save;
|
||||
this.dimension = dim.id;
|
||||
this.renderBound = renderSize / 2;
|
||||
this.renderBoundVertical = this.renderBound / 2;
|
||||
if (this.renderBoundVertical < 2)
|
||||
this.renderBoundVertical = 2;
|
||||
}
|
||||
|
||||
public int findAir(int x, int z)
|
||||
{
|
||||
int y = SEA_LEVEL;
|
||||
int attemptsRemaining = 255;
|
||||
|
||||
while (attemptsRemaining --> 0)
|
||||
{
|
||||
// DO NOT CHANGE TO y++
|
||||
if (this.getBlock(x, ++y, z) == Blocks.AIR)
|
||||
return y;
|
||||
}
|
||||
|
||||
return -1; // if it fails, returns -1
|
||||
}
|
||||
|
||||
public void spawnPlayer()
|
||||
{
|
||||
TexturedModel dirtModel = ModelLoader.loadGenericCube("block/cubes/soil/dirt.png");
|
||||
this.player = new Player(dirtModel, new Vector3f(0, 0, -3), 0, 180f, 0, new Vector3f(0.2f, 0.2f, 0.2f));
|
||||
int y = this.findAir(0, 0);
|
||||
if (y == -1)
|
||||
y = 300; // yeet
|
||||
|
||||
this.spawnPlayer(0, y, -3);
|
||||
}
|
||||
|
||||
// this constructor will likely not be neccesary when we have dynamic chunkloading
|
||||
public World(long seed, int size, Dimension<?> dim, LitecraftSave save)
|
||||
public Player spawnPlayer(float x, float y, float z)
|
||||
{
|
||||
this(seed, dim, save);
|
||||
|
||||
// Player model and stuff
|
||||
TexturedModel dirtModel = ModelLoader.loadGenericCube("block/cubes/soil/dirt.png");
|
||||
this.player = new Player(dirtModel, new Vector3f(x, y, z), 0, 180f, 0, new Vector3f(0.2f, 0.2f, 0.2f));
|
||||
this.player.isVisible = false;
|
||||
// Generate world around player
|
||||
long time = System.currentTimeMillis();
|
||||
System.out.println("Generating world!");
|
||||
for (int i = (0 - (size/2)); i < (size/2); i++)
|
||||
for (int k = (0 - (size/2)); k < (size/2); k++)
|
||||
for (int y = -2; y < 0; ++y)
|
||||
this.getChunk(i, y, k).setRender(true);
|
||||
|
||||
this.updateLoadedChunks(this.player.getChunkX(), this.player.getChunkY(), this.player.getChunkZ());
|
||||
System.out.println("Generated world in " + (System.currentTimeMillis() - time) + " milliseconds");
|
||||
// return player
|
||||
return this.player;
|
||||
}
|
||||
|
||||
public Chunk getChunk(int chunkX, int chunkY, int chunkZ)
|
||||
{
|
||||
Chunk chunk = this.chunks.computeIfAbsent(posHash(chunkX, chunkY, chunkZ), pos -> {
|
||||
Chunk chunk = this.chunks.computeIfAbsent(posHash(chunkX, chunkY, chunkZ), pos ->
|
||||
{
|
||||
Chunk readChunk = save.readChunk(chunkX, chunkY, chunkZ, this.dimension);
|
||||
return readChunk == null ? this.chunkGenerator.generateChunk(chunkX, chunkY, chunkZ) : readChunk;
|
||||
});
|
||||
|
||||
if (chunk.isFullyGenerated()) return chunk;
|
||||
|
||||
this.populateChunk(chunkX, chunkY, chunkZ, chunk.chunkStartX, chunk.chunkStartY, chunk.chunkStartZ);
|
||||
chunk.setFullyGenerated(true);
|
||||
return chunk;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return whether the chunk was unloaded without errors. Will often, but not always, be equal to whether the chunk was already in memory.
|
||||
*/
|
||||
public boolean unloadChunk(int chunkX, int chunkY, int chunkZ)
|
||||
public Chunk getChunkToLoad(int chunkX, int chunkY, int chunkZ)
|
||||
{
|
||||
long posHash = posHash(chunkX, chunkY, chunkZ);
|
||||
Chunk chunk = this.chunks.get(posHash);
|
||||
// try get an already loaded chunk
|
||||
Chunk result = this.chunks.get(posHash(chunkX, chunkY, chunkZ));
|
||||
if (result != null)
|
||||
return result;
|
||||
// try read a chunk from memory
|
||||
result = save.readChunk(chunkX, chunkY, chunkZ, this.dimension);
|
||||
// if neither of those work, generate the chunk
|
||||
return result == null ? this.chunkGenerator.generateChunk(chunkX, chunkY, chunkZ) : result;
|
||||
}
|
||||
|
||||
/** @return whether the chunk was unloaded without errors. Will often, but not always, be equal to whether the chunk was already in memory. */
|
||||
private boolean unloadChunk(long posHash)
|
||||
{
|
||||
Chunk chunk = this.chunks.get(posHash);
|
||||
// If the chunk is not in memory, it does not need to be unloaded
|
||||
if (chunk == null) return false;
|
||||
|
||||
// Otherwise save the chunk
|
||||
boolean result = this.save.saveChunk(chunk);
|
||||
this.chunks.remove(posHash);
|
||||
return result;
|
||||
}
|
||||
|
||||
private void populateChunk(int chunkX, int chunkY, int chunkZ, int chunkStartX, int chunkStartY, int chunkStartZ)
|
||||
private void populateChunk(Chunk chunk)
|
||||
{
|
||||
Random rand = new Random(this.seed + 5828671L * (long) chunkX + -47245139L * (long) chunkY + 8972357 * (long) chunkZ);
|
||||
|
||||
for (WorldModifier modifier : this.worldModifiers)
|
||||
{
|
||||
modifier.modifyWorld(this.genBlockAccess, rand, chunkStartX, chunkStartY, chunkStartZ);
|
||||
}
|
||||
this.populateChunk(chunk.chunkX, chunk.chunkY, chunk.chunkZ, chunk.chunkStartX, chunk.chunkStartY, chunk.chunkStartZ);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return a chunk that has not neccesarily gone through chunk populating. Used in chunk populating to prevent infinite recursion.
|
||||
*/
|
||||
private void populateChunk(int chunkX, int chunkY, int chunkZ, int chunkStartX, int chunkStartY, int chunkStartZ)
|
||||
{
|
||||
Random rand = new Random(this.seed + 5828671L * chunkX + -47245139L * chunkY + 8972357 * (long) chunkZ);
|
||||
for (WorldModifier modifier : this.worldModifiers)
|
||||
{ modifier.modifyWorld(this.genBlockAccess, rand, chunkStartX, chunkStartY, chunkStartZ); }
|
||||
}
|
||||
|
||||
/** @return a chunk that has not neccesarily gone through chunk populating. Used in chunk populating to prevent infinite recursion. */
|
||||
Chunk getGenChunk(int chunkX, int chunkY, int chunkZ)
|
||||
{ return this.chunks.computeIfAbsent(posHash(chunkX, chunkY, chunkZ), pos -> this.chunkGenerator.generateChunk(chunkX, chunkY, chunkZ)); }
|
||||
|
||||
|
@ -123,38 +155,68 @@ public class World implements BlockAccess, WorldGenConstants
|
|||
|
||||
//used for model combining and culling
|
||||
public Chunk optimiseChunk(Chunk chunk)
|
||||
{
|
||||
return chunk;
|
||||
}
|
||||
{ return chunk; }
|
||||
|
||||
public void render(BlockRenderer blockRenderer)
|
||||
{
|
||||
Chunk chunk = getChunk(0, -1, 0);
|
||||
if(chunk!= null) {
|
||||
blockRenderer.prepareModel(chunk.getBlockEntity(0, 0, 0).getModel());
|
||||
this.chunks.forEach((pos, c) -> c.render(blockRenderer));
|
||||
blockRenderer.unbindModel();
|
||||
}
|
||||
blockRenderer.prepareModel(this.dummy.getModel());
|
||||
this.chunks.forEach((pos, c) -> c.render(blockRenderer));
|
||||
blockRenderer.unbindModel();
|
||||
}
|
||||
|
||||
public void unloadAllChunks()
|
||||
{
|
||||
LongList chunkPositions = new LongArrayList();
|
||||
if (this.chunks != null)
|
||||
this.chunks.forEach((pos, chunk) -> { // for every chunk in memory
|
||||
this.chunks.forEach((pos, chunk) ->
|
||||
{ // for every chunk in memory
|
||||
chunkPositions.add((long) pos); // add pos to chunk positions list for removal later
|
||||
this.save.saveChunk(chunk); // save chunk
|
||||
});
|
||||
|
||||
chunkPositions.forEach((LongConsumer) (pos -> this.chunks.remove(pos))); // remove all chunks
|
||||
}
|
||||
|
||||
public long getSeed()
|
||||
{ return this.seed; }
|
||||
|
||||
public static final int SEA_LEVEL = 0;
|
||||
|
||||
public void updateLoadedChunks(int chunkX, int chunkY, int chunkZ)
|
||||
{
|
||||
List<Chunk> toKeep = new ArrayList<>();
|
||||
// loop over rendered area, adding chunks that are needed
|
||||
for (int x = chunkX - this.renderBound; x < chunkX + this.renderBound; x++)
|
||||
for (int z = chunkZ - this.renderBound; z < chunkZ + this.renderBound; z++)
|
||||
for (int y = chunkY - this.renderBoundVertical; y < chunkY + this.renderBoundVertical; y++)
|
||||
toKeep.add(this.getChunkToLoad(x, y, z));
|
||||
// list of keys to remove
|
||||
LongList toRemove = new LongArrayList();
|
||||
// check which loaded chunks are not neccesary
|
||||
this.chunks.forEach((pos, chunk) ->
|
||||
{
|
||||
if (!toKeep.contains(chunk))
|
||||
toRemove.add((long) pos);
|
||||
});
|
||||
// unload unneccesary chunks from chunk array
|
||||
toRemove.forEach((LongConsumer) pos -> this.unloadChunk(pos));
|
||||
// populate chunks to render if they are not rendered, then render them
|
||||
toKeep.forEach(chunk -> {
|
||||
if (!chunk.isFullyGenerated())
|
||||
{
|
||||
this.populateChunk(chunk);
|
||||
chunk.setFullyGenerated(true);
|
||||
}
|
||||
boolean alreadyRendering = chunk.doRender(); // if it's already rendering then it's most likely in the map
|
||||
chunk.setRender(true);
|
||||
if (!alreadyRendering)
|
||||
this.chunks.put(posHash(chunk.chunkX, chunk.chunkY, chunk.chunkZ), chunk);
|
||||
});
|
||||
}
|
||||
|
||||
private static final class GenerationWorld implements BlockAccess, WorldGenConstants
|
||||
{
|
||||
GenerationWorld(World parent)
|
||||
{
|
||||
this.parent = parent;
|
||||
}
|
||||
{ this.parent = parent; }
|
||||
|
||||
public final World parent;
|
||||
|
||||
|
|
|
@ -1,17 +1,16 @@
|
|||
package com.github.halotroop.litecraft.world.block;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import com.github.halotroop.litecraft.world.gen.WorldGenConstants;
|
||||
import org.joml.Matrix4f;
|
||||
import org.lwjgl.opengl.*;
|
||||
|
||||
import com.github.halotroop.litecraft.Litecraft;
|
||||
import com.github.halotroop.litecraft.types.block.BlockEntity;
|
||||
import com.github.halotroop.litecraft.types.block.BlockInstance;
|
||||
import com.github.halotroop.litecraft.world.Chunk;
|
||||
import com.github.halotroop.litecraft.world.gen.WorldGenConstants;
|
||||
import com.github.hydos.ginger.engine.api.GingerRegister;
|
||||
import com.github.hydos.ginger.engine.elements.objects.RenderObject;
|
||||
import com.github.hydos.ginger.engine.io.Window;
|
||||
import com.github.hydos.ginger.engine.math.Maths;
|
||||
import com.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import com.github.hydos.ginger.engine.render.Renderer;
|
||||
import com.github.hydos.ginger.engine.render.models.*;
|
||||
import com.github.hydos.ginger.engine.render.shaders.StaticShader;
|
||||
|
@ -19,6 +18,7 @@ import com.github.hydos.ginger.engine.render.texture.ModelTexture;
|
|||
|
||||
public class BlockRenderer extends Renderer implements WorldGenConstants
|
||||
{
|
||||
|
||||
private StaticShader shader;
|
||||
|
||||
public BlockRenderer(StaticShader shader, Matrix4f projectionMatrix)
|
||||
|
@ -44,8 +44,9 @@ public class BlockRenderer extends Renderer implements WorldGenConstants
|
|||
GL20.glEnableVertexAttribArray(2);
|
||||
Litecraft.getInstance().binds++;
|
||||
}
|
||||
|
||||
private void prepTexture(ModelTexture texture, int textureID) {
|
||||
|
||||
private void prepTexture(ModelTexture texture, int textureID)
|
||||
{
|
||||
shader.loadFakeLightingVariable(texture.isUseFakeLighting());
|
||||
shader.loadShine(texture.getShineDamper(), texture.getReflectivity());
|
||||
GL13.glActiveTexture(GL13.GL_TEXTURE0);
|
||||
|
@ -60,37 +61,30 @@ public class BlockRenderer extends Renderer implements WorldGenConstants
|
|||
GL30.glBindVertexArray(0);
|
||||
}
|
||||
|
||||
public void render(BlockEntity[] renderList)
|
||||
{
|
||||
public void render(BlockInstance[] renderList)
|
||||
{
|
||||
shader.start();
|
||||
shader.loadSkyColour(Window.getColour());
|
||||
shader.loadViewMatrix(GingerRegister.getInstance().game.data.camera);
|
||||
TexturedModel model = renderList[0].getModel();
|
||||
if(GingerRegister.getInstance().wireframe)
|
||||
{
|
||||
// TexturedModel model = renderList[0].getModel();
|
||||
if (GingerRegister.getInstance().wireframe)
|
||||
GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_LINE);
|
||||
}
|
||||
for(int x = 0; x < CHUNK_SIZE; x++)
|
||||
{
|
||||
//
|
||||
for (int x = 0; x < CHUNK_SIZE; x++)
|
||||
for (int y = 0; y < CHUNK_SIZE; y++)
|
||||
{
|
||||
for (int z = 0; z < CHUNK_SIZE; z++)
|
||||
{
|
||||
BlockEntity entity = renderList[x*CHUNK_SIZE*CHUNK_SIZE + z*CHUNK_SIZE + y];
|
||||
BlockInstance entity = renderList[Chunk.index(x, y, z)];
|
||||
if (entity != null && entity.getModel() != null)
|
||||
{
|
||||
prepTexture(entity.getModel().getTexture(), entity.getModel().getTexture().getTextureID());
|
||||
TexturedModel blockModel = entity.getModel();
|
||||
prepTexture(blockModel.getTexture(), blockModel.getTexture().getTextureID());
|
||||
prepBlockInstance(entity);
|
||||
GL11.glDrawElements(GL11.GL_TRIANGLES, model.getRawModel().getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
|
||||
GL11.glDrawElements(GL11.GL_TRIANGLES, blockModel.getRawModel().getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(GingerRegister.getInstance().wireframe)
|
||||
{
|
||||
GL11.glPolygonMode( GL11.GL_FRONT_AND_BACK,GL11.GL_FILL);
|
||||
}
|
||||
if (GingerRegister.getInstance().wireframe)
|
||||
GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_FILL);
|
||||
shader.stop();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
package com.github.halotroop.litecraft.world.gen;
|
||||
package com.github.halotroop.litecraft.world.dimension;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import com.github.halotroop.litecraft.world.gen.*;
|
||||
|
||||
import it.unimi.dsi.fastutil.ints.*;
|
||||
|
||||
public abstract class Dimension<T extends ChunkGenerator>
|
||||
|
@ -21,9 +23,8 @@ public abstract class Dimension<T extends ChunkGenerator>
|
|||
return this;
|
||||
}
|
||||
|
||||
public WorldModifier[] getWorldModifierArray() {
|
||||
return this.worldModifiers.toArray(WorldModifier[]::new);
|
||||
}
|
||||
public WorldModifier[] getWorldModifierArray()
|
||||
{ return this.worldModifiers.toArray(new WorldModifier[0]); }
|
||||
|
||||
public abstract T createChunkGenerator(long seed);
|
||||
|
|
@ -1,4 +1,6 @@
|
|||
package com.github.halotroop.litecraft.world.gen;
|
||||
package com.github.halotroop.litecraft.world.dimension;
|
||||
|
||||
import com.github.halotroop.litecraft.world.gen.*;
|
||||
|
||||
public final class Dimensions
|
||||
{
|
|
@ -10,46 +10,59 @@ public class OverworldChunkGenerator implements ChunkGenerator, WorldGenConstant
|
|||
{
|
||||
public OverworldChunkGenerator(long seed, int dimension)
|
||||
{
|
||||
this.noise = new OctaveSimplexNoise(new Random(seed), 3, 250.0, 35.0, 10.0);
|
||||
Random rand = new Random(seed);
|
||||
this.noise = new OctaveSimplexNoise(rand, 3, 250.0, 35.0, 10.0);
|
||||
this.stoneNoise = new OctaveSimplexNoise(rand, 1);
|
||||
this.dimension = dimension;
|
||||
}
|
||||
|
||||
private final OctaveSimplexNoise noise;
|
||||
private final OctaveSimplexNoise stoneNoise;
|
||||
private final int dimension;
|
||||
|
||||
@Override
|
||||
public Chunk generateChunk(int chunkX, int chunkY, int chunkZ)
|
||||
{
|
||||
Chunk chunk = new Chunk(chunkX, chunkY, chunkZ, this.dimension);
|
||||
|
||||
for (int x = 0; x < CHUNK_SIZE; ++x)
|
||||
for (int x = 0; x < CHUNK_SIZE; x++)
|
||||
{
|
||||
double totalX = x + chunk.chunkStartX;
|
||||
|
||||
for (int z = 0; z < CHUNK_SIZE; ++z)
|
||||
for (int z = 0; z < CHUNK_SIZE; z++)
|
||||
{
|
||||
int height = (int) this.noise.sample(totalX, (double) (chunk.chunkStartZ + z));
|
||||
|
||||
for (int y = 0; y < CHUNK_SIZE; ++y)
|
||||
double totalZ = chunk.chunkStartZ + z;
|
||||
int height = (int) this.noise.sample(totalX, totalZ);
|
||||
for (int y = 0; y < CHUNK_SIZE; y++)
|
||||
{
|
||||
double rockNoise = this.stoneNoise.sample(totalX / 160.0, (chunk.chunkStartY + y) / 50.0, totalZ / 160.0);
|
||||
int totalY = chunk.chunkStartY + y;
|
||||
Block block = Blocks.AIR;
|
||||
|
||||
if (totalY < height - 3)
|
||||
{
|
||||
block = Blocks.DIRT;
|
||||
}
|
||||
block = pickStone(rockNoise);
|
||||
else if (totalY < height)
|
||||
{
|
||||
block = Blocks.STONE;
|
||||
}
|
||||
|
||||
block = Blocks.DIRT;
|
||||
chunk.setBlock(x, y, z, block);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return chunk;
|
||||
}
|
||||
|
||||
private static Block pickStone(double rockNoise) {
|
||||
if (rockNoise < -0.25)
|
||||
{
|
||||
return Blocks.ANDESITE;
|
||||
}
|
||||
else if (rockNoise < 0)
|
||||
{
|
||||
return Blocks.DIORITE;
|
||||
}
|
||||
else if (rockNoise < 0.25)
|
||||
{
|
||||
return Blocks.GNEISS;
|
||||
}
|
||||
else
|
||||
{
|
||||
return Blocks.GRANITE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,15 +1,13 @@
|
|||
package com.github.halotroop.litecraft.world.gen;
|
||||
|
||||
import com.github.halotroop.litecraft.world.dimension.Dimension;
|
||||
|
||||
public class OverworldDimension extends Dimension<OverworldChunkGenerator>
|
||||
{
|
||||
public OverworldDimension(int id)
|
||||
{
|
||||
super(id);
|
||||
}
|
||||
{ super(id); }
|
||||
|
||||
@Override
|
||||
public OverworldChunkGenerator createChunkGenerator(long seed)
|
||||
{
|
||||
return new OverworldChunkGenerator(seed, this.id);
|
||||
}
|
||||
{ return new OverworldChunkGenerator(seed, this.id); }
|
||||
}
|
|
@ -4,6 +4,7 @@ import java.util.Random;
|
|||
|
||||
import com.github.halotroop.litecraft.world.BlockAccess;
|
||||
|
||||
public interface WorldModifier {
|
||||
public interface WorldModifier
|
||||
{
|
||||
void modifyWorld(BlockAccess world, Random rand, int chunkStartX, int chunkStartY, int chunkStartZ);
|
||||
}
|
||||
|
|
|
@ -1,14 +1,17 @@
|
|||
package com.github.hydos.ginger.engine.api;
|
||||
|
||||
import org.joml.Vector2f;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
import com.github.halotroop.litecraft.Litecraft;
|
||||
import com.github.halotroop.litecraft.logic.Timer;
|
||||
import com.github.halotroop.litecraft.logic.Timer.TickListener;
|
||||
import com.github.halotroop.litecraft.world.World;
|
||||
import com.github.hydos.ginger.engine.api.game.*;
|
||||
import com.github.hydos.ginger.engine.elements.buttons.TextureButton;
|
||||
import com.github.hydos.ginger.engine.elements.objects.Player;
|
||||
import com.github.hydos.ginger.engine.font.*;
|
||||
import com.github.hydos.ginger.engine.io.Window;
|
||||
import com.github.hydos.ginger.engine.math.vectors.Vector2f;
|
||||
import com.github.hydos.ginger.engine.particle.ParticleMaster;
|
||||
import com.github.hydos.ginger.engine.postprocessing.*;
|
||||
import com.github.hydos.ginger.engine.render.MasterRenderer;
|
||||
|
@ -19,7 +22,6 @@ import com.github.hydos.ginger.engine.utils.Loader;
|
|||
public class Ginger
|
||||
{
|
||||
private static Ginger INSTANCE;
|
||||
|
||||
public GingerRegister gingerRegister;
|
||||
public MousePicker picker;
|
||||
public FontType globalFont;
|
||||
|
@ -29,11 +31,10 @@ public class Ginger
|
|||
{
|
||||
@Override
|
||||
public void onTick(float deltaTime)
|
||||
{
|
||||
gingerRegister.game.tick();
|
||||
if(gingerRegister.currentScreen != null) {
|
||||
gingerRegister.currentScreen.tick();
|
||||
}
|
||||
{
|
||||
gingerRegister.game.tick();
|
||||
if (gingerRegister.currentScreen != null)
|
||||
{ gingerRegister.currentScreen.tick(); }
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -46,12 +47,19 @@ public class Ginger
|
|||
TextMaster.cleanUp();
|
||||
Loader.cleanUp();
|
||||
}
|
||||
|
||||
public void openScreen(Screen screen)
|
||||
{
|
||||
gingerRegister.currentScreen = screen;
|
||||
}
|
||||
|
||||
public void openScreen(Screen screen)
|
||||
{ gingerRegister.currentScreen = screen; }
|
||||
|
||||
public void setGingerPlayer(Player player)
|
||||
{
|
||||
gingerRegister.game.data.entities.remove(Litecraft.getInstance().player); // remove the old player
|
||||
gingerRegister.game.data.player = player; // set all the player variables
|
||||
Litecraft.getInstance().player = player;
|
||||
Litecraft.getInstance().getCamera().player = player;
|
||||
gingerRegister.game.data.entities.add(player); // add the new player
|
||||
}
|
||||
|
||||
public void postRender()
|
||||
{ Window.swapBuffers(); }
|
||||
|
||||
|
@ -73,9 +81,8 @@ public class Ginger
|
|||
public void renderOverlays(Game game)
|
||||
{
|
||||
gingerRegister.masterRenderer.renderGuis(game.data.guis);
|
||||
if(gingerRegister.currentScreen != null) {
|
||||
gingerRegister.masterRenderer.renderGuis(gingerRegister.currentScreen.elements);
|
||||
}
|
||||
if (gingerRegister.currentScreen != null)
|
||||
{ gingerRegister.masterRenderer.renderGuis(gingerRegister.currentScreen.elements); }
|
||||
TextMaster.render();
|
||||
}
|
||||
|
||||
|
@ -83,13 +90,12 @@ public class Ginger
|
|||
{
|
||||
GingerUtils.preRenderScene(gingerRegister.masterRenderer);
|
||||
contrastFbo.bindFBO();
|
||||
gingerRegister.masterRenderer.renderSceneNoTerrain(game.data.entities, game.data.normalMapEntities, game.data.lights, game.data.camera, game.data.clippingPlane, world);
|
||||
gingerRegister.masterRenderer.renderScene(game.data.entities, game.data.normalMapEntities, game.data.lights, game.data.camera, game.data.clippingPlane, world);
|
||||
ParticleMaster.renderParticles(game.data.camera);
|
||||
contrastFbo.unbindFBO();
|
||||
PostProcessing.doPostProcessing(contrastFbo.colorTexture);
|
||||
if (game.data.handleGuis)
|
||||
{ renderOverlays(game); }
|
||||
|
||||
}
|
||||
|
||||
public void setGlobalFont(FontType font)
|
||||
|
@ -104,7 +110,7 @@ public class Ginger
|
|||
timer.addTickListener(gameTickListener);
|
||||
contrastFbo = new Fbo(new ContrastChanger());
|
||||
gingerRegister.masterRenderer = masterRenderer;
|
||||
picker = new MousePicker(game.data.camera, masterRenderer.getProjectionMatrix(), null);
|
||||
picker = new MousePicker(game.data.camera, masterRenderer.getProjectionMatrix());
|
||||
PostProcessing.init();
|
||||
ParticleMaster.init(masterRenderer.getProjectionMatrix());
|
||||
}
|
||||
|
@ -132,7 +138,6 @@ public class Ginger
|
|||
ParticleMaster.update(data.camera);
|
||||
}
|
||||
|
||||
public static Ginger getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
public static Ginger getInstance()
|
||||
{ return INSTANCE; }
|
||||
}
|
|
@ -15,20 +15,21 @@ public class GingerRegister
|
|||
{
|
||||
private static GingerRegister INSTANCE;
|
||||
public MasterRenderer masterRenderer;
|
||||
|
||||
public static GingerRegister getInstance()
|
||||
{ return INSTANCE; }
|
||||
|
||||
public GingerThreading threadRegister;
|
||||
public List<GUIText> texts;
|
||||
public List<TextureButton> guiButtons;
|
||||
public List<Fbo> fbos;
|
||||
|
||||
public Game game;
|
||||
public Screen currentScreen;
|
||||
public boolean wireframe = false;
|
||||
|
||||
public GingerRegister()
|
||||
{
|
||||
INSTANCE = this;
|
||||
{
|
||||
INSTANCE = this;
|
||||
threadRegister = new GingerThreading();
|
||||
}
|
||||
|
||||
|
@ -60,6 +61,11 @@ public class GingerRegister
|
|||
return null;
|
||||
}
|
||||
|
||||
public void toggleWireframe()
|
||||
{
|
||||
this.wireframe = !this.wireframe;
|
||||
}
|
||||
|
||||
public GUIText retrieveText(String string)
|
||||
{
|
||||
for (GUIText text : texts)
|
||||
|
|
|
@ -7,7 +7,6 @@ import org.joml.Vector4f;
|
|||
import com.github.hydos.ginger.engine.cameras.Camera;
|
||||
import com.github.hydos.ginger.engine.elements.GuiTexture;
|
||||
import com.github.hydos.ginger.engine.elements.objects.*;
|
||||
import com.github.hydos.ginger.engine.terrain.Terrain;
|
||||
|
||||
/*
|
||||
* Used for storing essential engine game data so main class isn't messy
|
||||
|
@ -19,7 +18,6 @@ public class GameData
|
|||
public List<RenderObject> entities;
|
||||
public List<Light> lights;
|
||||
public List<RenderObject> normalMapEntities;
|
||||
public List<Terrain> flatTerrains;
|
||||
public Player player;
|
||||
public Camera camera;
|
||||
public Vector4f clippingPlane;
|
||||
|
@ -33,7 +31,6 @@ public class GameData
|
|||
entities = new ArrayList<RenderObject>();
|
||||
lights = new ArrayList<Light>();
|
||||
normalMapEntities = new ArrayList<RenderObject>();
|
||||
flatTerrains = new ArrayList<Terrain>();
|
||||
this.player = player;
|
||||
this.camera = camera;
|
||||
this.tickSpeed = tickSpeed;
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
package com.github.hydos.ginger.engine.cameras;
|
||||
|
||||
import org.joml.Vector3f;
|
||||
import org.lwjgl.glfw.*;
|
||||
|
||||
import com.github.hydos.ginger.engine.elements.objects.Player;
|
||||
import com.github.hydos.ginger.engine.io.Window;
|
||||
import com.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
|
||||
public class Camera
|
||||
{
|
||||
|
@ -107,4 +107,13 @@ public class Camera
|
|||
calculateCameraPosition(horizontalDistance, verticalDistance);
|
||||
this.yaw = 180 - (player.getRotY() + angleAroundPlayer);
|
||||
}
|
||||
|
||||
public void setPitch(float pitch)
|
||||
{ this.pitch = pitch; }
|
||||
|
||||
public void setYaw(float yaw)
|
||||
{ this.yaw = yaw; }
|
||||
|
||||
public void setRoll(float roll)
|
||||
{ this.roll = roll; }
|
||||
}
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
package com.github.hydos.ginger.engine.cameras;
|
||||
|
||||
import org.joml.Vector3f;
|
||||
|
||||
import com.github.hydos.ginger.engine.elements.objects.Player;
|
||||
import com.github.hydos.ginger.engine.io.Window;
|
||||
import com.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
|
||||
public class FirstPersonCamera extends Camera
|
||||
{
|
||||
|
@ -16,27 +17,30 @@ public class FirstPersonCamera extends Camera
|
|||
player.isVisible = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getPitch()
|
||||
{ return pitch; }
|
||||
|
||||
@Override
|
||||
public Vector3f getPosition()
|
||||
{ return position; }
|
||||
|
||||
@Override
|
||||
public float getRoll()
|
||||
{ return roll; }
|
||||
|
||||
@Override
|
||||
public float getYaw()
|
||||
{ return yaw; }
|
||||
|
||||
@Override
|
||||
public void move()
|
||||
{
|
||||
|
||||
position.x = player.getPosition().x;
|
||||
position.z = player.getPosition().z;
|
||||
position.y = player.getPosition().y;
|
||||
|
||||
roll = player.getRotX();
|
||||
yaw = -player.getRotY() + 180 + Window.getNormalizedMouseCoordinates().getX() * 70;
|
||||
pitch = player.getRotZ() + -Window.getNormalizedMouseCoordinates().getY() * 70;
|
||||
yaw = -player.getRotY() + 180 + Window.getNormalizedMouseCoordinates().x() * 70;
|
||||
pitch = player.getRotZ() + -Window.getNormalizedMouseCoordinates().y() * 70;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package com.github.hydos.ginger.engine.elements;
|
||||
|
||||
import com.github.hydos.ginger.engine.math.vectors.Vector2f;
|
||||
import org.joml.Vector2f;
|
||||
|
||||
public class GuiTexture
|
||||
{
|
||||
|
|
|
@ -2,11 +2,11 @@ package com.github.hydos.ginger.engine.elements.buttons;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
import org.joml.Vector2f;
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
|
||||
import com.github.hydos.ginger.engine.elements.GuiTexture;
|
||||
import com.github.hydos.ginger.engine.io.Window;
|
||||
import com.github.hydos.ginger.engine.math.vectors.Vector2f;
|
||||
import com.github.hydos.ginger.engine.utils.Loader;
|
||||
|
||||
public class TextureButton
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package com.github.hydos.ginger.engine.elements.objects;
|
||||
|
||||
import com.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class Light
|
||||
{
|
||||
|
|
|
@ -1,58 +1,67 @@
|
|||
package com.github.hydos.ginger.engine.elements.objects;
|
||||
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
import com.github.halotroop.litecraft.Litecraft;
|
||||
import com.github.halotroop.litecraft.util.RelativeDirection;
|
||||
import com.github.halotroop.litecraft.world.World;
|
||||
import com.github.halotroop.litecraft.world.gen.WorldGenConstants;
|
||||
import com.github.hydos.ginger.engine.api.GingerRegister;
|
||||
import com.github.hydos.ginger.engine.io.Window;
|
||||
import com.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
import com.github.hydos.ginger.engine.render.models.TexturedModel;
|
||||
import com.github.hydos.ginger.main.settings.Constants;
|
||||
|
||||
public class Player extends RenderObject
|
||||
public class Player extends RenderObject implements WorldGenConstants
|
||||
{
|
||||
private boolean isInAir = false;
|
||||
private double upwardsSpeed;
|
||||
private boolean noWeight = true; // because the force of gravity on an object's mass is called WEIGHT, not gravity
|
||||
private int chunkX, chunkY, chunkZ;
|
||||
|
||||
public Player(TexturedModel model, Vector3f position, float rotX, float rotY, float rotZ, Vector3f scale)
|
||||
{ super(model, position, rotX, rotY, rotZ, scale); }
|
||||
|
||||
private void checkInputs()
|
||||
{
|
||||
float ry = getRotY();
|
||||
if (Window.isKeyDown(GLFW.GLFW_KEY_W))
|
||||
super(model, position, rotX, rotY, rotZ, scale);
|
||||
this.chunkX = (int) position.x >> POS_SHIFT;
|
||||
this.chunkY = (int) position.y >> POS_SHIFT;
|
||||
this.chunkZ = (int) position.z >> POS_SHIFT;
|
||||
}
|
||||
|
||||
public void move(RelativeDirection direction)
|
||||
{
|
||||
float ry = (float) Math.toRadians(GingerRegister.getInstance().game.data.camera.getYaw());
|
||||
switch (direction)
|
||||
{
|
||||
case FORWARD:
|
||||
default:
|
||||
position.z -= Math.cos(ry) * Constants.movementSpeed;
|
||||
position.x += Math.sin(ry) * Constants.movementSpeed;
|
||||
}
|
||||
|
||||
if (Window.isKeyDown(GLFW.GLFW_KEY_A))
|
||||
{
|
||||
position.z -= Math.cos(ry) * Constants.movementSpeed;
|
||||
position.x -= Math.sin(ry) * Constants.movementSpeed;
|
||||
}
|
||||
|
||||
if (Window.isKeyDown(GLFW.GLFW_KEY_S))
|
||||
{
|
||||
break;
|
||||
case BACKWARD:
|
||||
position.z += Math.cos(ry) * Constants.movementSpeed;
|
||||
position.x -= Math.sin(ry) * Constants.movementSpeed;
|
||||
}
|
||||
|
||||
if (Window.isKeyDown(GLFW.GLFW_KEY_D))
|
||||
{
|
||||
position.z += Math.cos(ry) * Constants.movementSpeed;
|
||||
break;
|
||||
case LEFT:
|
||||
ry -= RIGHT_ANGLE;
|
||||
position.z -= Math.cos(ry) * Constants.movementSpeed;
|
||||
position.x += Math.sin(ry) * Constants.movementSpeed;
|
||||
}
|
||||
|
||||
if (Window.isKeyDown(GLFW.GLFW_KEY_SPACE))
|
||||
{
|
||||
// jump();
|
||||
position.y += Constants.movementSpeed;
|
||||
}
|
||||
if (Window.isKeyDown(GLFW.GLFW_KEY_LEFT_SHIFT))
|
||||
{
|
||||
break;
|
||||
case RIGHT:
|
||||
ry += RIGHT_ANGLE;
|
||||
position.z -= Math.cos(ry) * Constants.movementSpeed;
|
||||
position.x += Math.sin(ry) * Constants.movementSpeed;
|
||||
break;
|
||||
case UP:
|
||||
if (this.noWeight) position.y += Constants.movementSpeed;
|
||||
else this.jump();
|
||||
break;
|
||||
case DOWN:
|
||||
position.y -= Constants.movementSpeed;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private static final float RIGHT_ANGLE = (float) (Math.PI / 2f);
|
||||
|
||||
private void jump()
|
||||
{
|
||||
if (!isInAir)
|
||||
|
@ -62,13 +71,32 @@ public class Player extends RenderObject
|
|||
}
|
||||
}
|
||||
|
||||
public int getChunkX()
|
||||
{ return this.chunkX; }
|
||||
|
||||
public int getChunkY()
|
||||
{ return this.chunkY; }
|
||||
|
||||
public int getChunkZ()
|
||||
{ return this.chunkZ; }
|
||||
|
||||
public void updateMovement()
|
||||
{
|
||||
checkInputs();
|
||||
super.increasePosition(0, (float) (upwardsSpeed * (Window.getTime())), 0);
|
||||
upwardsSpeed += Constants.gravity.y() * Window.getTime();
|
||||
isInAir = false;
|
||||
upwardsSpeed = 0;
|
||||
// super.getPosition().y = 0;
|
||||
|
||||
int newChunkX = (int) position.x >> POS_SHIFT;
|
||||
int newChunkY = (int) position.y >> POS_SHIFT;
|
||||
int newChunkZ = (int) position.z >> POS_SHIFT;
|
||||
|
||||
if (newChunkX != this.chunkX || newChunkY != this.chunkY || newChunkZ != this.chunkZ)
|
||||
{
|
||||
Litecraft.getInstance().getWorld().updateLoadedChunks(newChunkX, newChunkY, newChunkZ);
|
||||
this.chunkX = newChunkX;
|
||||
this.chunkY = newChunkY;
|
||||
this.chunkZ = newChunkZ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.github.hydos.ginger.engine.elements.objects;
|
||||
|
||||
import com.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
import com.github.hydos.ginger.engine.render.models.TexturedModel;
|
||||
|
||||
public class RenderObject
|
||||
|
@ -20,18 +21,15 @@ public class RenderObject
|
|||
this.rotZ = rotZ;
|
||||
this.scale = scale;
|
||||
}
|
||||
|
||||
public void x(float x) {
|
||||
this.position.x = x;
|
||||
}
|
||||
|
||||
public void y(float y) {
|
||||
this.position.y = y;
|
||||
}
|
||||
|
||||
public void z(float z) {
|
||||
this.position.z = z;
|
||||
}
|
||||
|
||||
public void x(float x)
|
||||
{ this.position.x = x; }
|
||||
|
||||
public void y(float y)
|
||||
{ this.position.y = y; }
|
||||
|
||||
public void z(float z)
|
||||
{ this.position.z = z; }
|
||||
|
||||
public TexturedModel getModel()
|
||||
{ return model; }
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package com.github.hydos.ginger.engine.font;
|
||||
|
||||
import com.github.hydos.ginger.engine.math.vectors.*;
|
||||
import org.joml.*;
|
||||
|
||||
/** Represents a piece of text in the game. */
|
||||
public class GUIText
|
||||
|
|
|
@ -43,7 +43,7 @@ public class TextMaster
|
|||
public static void render()
|
||||
{ renderer.render(texts); }
|
||||
|
||||
public static void render(GUIText buildText)
|
||||
public static void render(GUIText buildText)
|
||||
{
|
||||
Map<FontType, List<GUIText>> oldTexts = texts;
|
||||
List<GUIText> oldFontText = texts.get(Ginger.getInstance().globalFont);
|
||||
|
|
|
@ -6,6 +6,7 @@ public class TextMeshCreator
|
|||
{
|
||||
protected static final double LINE_HEIGHT = 0.03f;
|
||||
protected static final int SPACE_ASCII = 32;
|
||||
|
||||
private static void addTexCoords(List<Float> texCoords, double x, double y, double maxX, double maxY)
|
||||
{
|
||||
texCoords.add((float) x);
|
||||
|
|
|
@ -2,11 +2,11 @@ package com.github.hydos.ginger.engine.io;
|
|||
|
||||
import java.nio.*;
|
||||
|
||||
import org.joml.*;
|
||||
import org.lwjgl.BufferUtils;
|
||||
import org.lwjgl.glfw.*;
|
||||
import org.lwjgl.opengl.*;
|
||||
|
||||
import com.github.hydos.ginger.engine.math.vectors.*;
|
||||
import com.github.hydos.ginger.engine.render.texture.Image;
|
||||
|
||||
public class Window
|
||||
|
@ -84,7 +84,7 @@ public class Window
|
|||
|
||||
public static float getFloatTime()
|
||||
{
|
||||
float f = (System.nanoTime() / (long) 1000000000);
|
||||
float f = (System.nanoTime() / 1000000000);
|
||||
return f;
|
||||
}
|
||||
|
||||
|
@ -192,8 +192,5 @@ public class Window
|
|||
}
|
||||
|
||||
public static void fullscreen()
|
||||
{
|
||||
Window.fullscreen = !Window.isFullscreen();
|
||||
|
||||
}
|
||||
{ Window.fullscreen = !Window.isFullscreen(); }
|
||||
}
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
package com.github.hydos.ginger.engine.math;
|
||||
|
||||
import java.lang.Math;
|
||||
|
||||
import org.joml.*;
|
||||
|
||||
import com.github.hydos.ginger.engine.cameras.Camera;
|
||||
import com.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import com.github.hydos.ginger.engine.math.vectors.*;
|
||||
|
||||
public class Maths
|
||||
{
|
||||
|
@ -18,22 +20,22 @@ 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 org.joml.Vector3f(translation.x, translation.y, 0), 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;
|
||||
}
|
||||
|
||||
|
@ -44,25 +46,25 @@ 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), XVEC, matrix, matrix);
|
||||
Matrix4f.rotate((float) Math.toRadians(ry), YVEC, matrix, matrix);
|
||||
Matrix4f.rotate((float) Math.toRadians(rz), ZVEC, matrix, matrix);
|
||||
Matrix4f.scale(scale, matrix, matrix);
|
||||
matrix.identity();
|
||||
matrix.translate(translation, matrix);
|
||||
matrix.rotate((float) Math.toRadians(rx), XVEC, matrix);
|
||||
matrix.rotate((float) Math.toRadians(ry), YVEC, matrix);
|
||||
matrix.rotate((float) Math.toRadians(rz), ZVEC, matrix);
|
||||
matrix.scale(scale, matrix);
|
||||
return matrix;
|
||||
}
|
||||
|
||||
public static Matrix4f createViewMatrix(Camera 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,487 +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 com.github.hydos.ginger.engine.math;
|
||||
|
||||
/**
|
||||
*
|
||||
* Quaternions for LWJGL!
|
||||
*
|
||||
* @author fbi
|
||||
* @version $Revision$
|
||||
* $Id$
|
||||
*/
|
||||
import java.nio.FloatBuffer;
|
||||
|
||||
import org.joml.Vector4f;
|
||||
|
||||
import com.github.hydos.ginger.engine.math.matrixes.*;
|
||||
import com.github.hydos.ginger.engine.math.vectors.*;
|
||||
|
||||
public class Quaternion extends Vector implements ReadableVector4f
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
/** 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; }
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
||||
/** 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); }
|
||||
|
||||
/** 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); }
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
||||
public float x, y, z, w;
|
||||
|
||||
/** C'tor. The quaternion will be initialized to the identity. */
|
||||
public Quaternion()
|
||||
{
|
||||
super();
|
||||
setIdentity();
|
||||
}
|
||||
|
||||
/** C'tor */
|
||||
public Quaternion(float x, float y, float z, float w)
|
||||
{ set(x, y, z, w); }
|
||||
|
||||
/** C'tor
|
||||
*
|
||||
* @param src */
|
||||
public Quaternion(ReadableVector4f src)
|
||||
{ set(src); }
|
||||
|
||||
/*
|
||||
* (Overrides)
|
||||
*
|
||||
* @see org.lwjgl.vector.ReadableVector3f#getW()
|
||||
*/
|
||||
@Override
|
||||
public float getW()
|
||||
{ return w; }
|
||||
|
||||
/** @return x */
|
||||
@Override
|
||||
public final float getX()
|
||||
{ return x; }
|
||||
|
||||
/** @return y */
|
||||
@Override
|
||||
public final float getY()
|
||||
{ return y; }
|
||||
|
||||
/*
|
||||
* (Overrides)
|
||||
*
|
||||
* @see org.lwjgl.vector.ReadableVector3f#getZ()
|
||||
*/
|
||||
@Override
|
||||
public float getZ()
|
||||
{ return z; }
|
||||
|
||||
/** @return the length squared of the quaternion */
|
||||
@Override
|
||||
public float lengthSquared()
|
||||
{ return x * x + y * y + z * z + w * w; }
|
||||
|
||||
/* (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;
|
||||
}
|
||||
|
||||
/** Calculate the conjugate of this quaternion */
|
||||
@Override
|
||||
public Vector negate()
|
||||
{ return negate(this, this); }
|
||||
|
||||
/** 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); }
|
||||
|
||||
/** 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); }
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.lwjgl.vector.Vector#scale(float)
|
||||
*/
|
||||
@Override
|
||||
public Vector scale(float scale)
|
||||
{ return scale(scale, this, this); }
|
||||
|
||||
/*
|
||||
* (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;
|
||||
}
|
||||
|
||||
/** 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);
|
||||
}
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
||||
/** 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 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); }
|
||||
|
||||
/** Set this quaternion to the multiplication identity.
|
||||
*
|
||||
* @return this */
|
||||
public Quaternion setIdentity()
|
||||
{ return setIdentity(this); }
|
||||
|
||||
/** Set W
|
||||
*
|
||||
* @param w */
|
||||
public void setW(float w)
|
||||
{ this.w = w; }
|
||||
|
||||
/** 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; }
|
||||
|
||||
/* (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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{ return "Quaternion: " + x + " " + y + " " + z + " " + w; }
|
||||
}
|
|
@ -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 com.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(); }
|
||||
|
||||
/** @return the determinant of the matrix */
|
||||
public abstract float determinant();
|
||||
|
||||
/** 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();
|
||||
|
||||
/** Set this matrix to be the identity matrix.
|
||||
*
|
||||
* @return this */
|
||||
public abstract Matrix setIdentity();
|
||||
|
||||
/** Set this matrix to 0.
|
||||
*
|
||||
* @return this */
|
||||
public abstract Matrix setZero();
|
||||
|
||||
/** 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();
|
||||
}
|
|
@ -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 com.github.hydos.ginger.engine.math.matrixes;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.nio.FloatBuffer;
|
||||
|
||||
import com.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;
|
||||
/** 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;
|
||||
}
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
||||
/** 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 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;
|
||||
}
|
||||
|
||||
public static Matrix2f setZero(Matrix2f src)
|
||||
{
|
||||
src.m00 = 0.0f;
|
||||
src.m01 = 0.0f;
|
||||
src.m10 = 0.0f;
|
||||
src.m11 = 0.0f;
|
||||
return src;
|
||||
}
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
||||
/** 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 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;
|
||||
}
|
||||
|
||||
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); }
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.lwjgl.vector.Matrix#determinant()
|
||||
*/
|
||||
@Override
|
||||
public float determinant()
|
||||
{ return m00 * m11 - m01 * m10; }
|
||||
|
||||
/** Invert this matrix
|
||||
*
|
||||
* @return this if successful, null otherwise */
|
||||
@Override
|
||||
public Matrix invert()
|
||||
{ return invert(this, this); }
|
||||
|
||||
/** 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 another matrix
|
||||
*
|
||||
* @param src The source matrix
|
||||
* @return this */
|
||||
public Matrix2f load(Matrix2f src)
|
||||
{ return load(src, 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;
|
||||
}
|
||||
|
||||
/** 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); }
|
||||
|
||||
/** Set this matrix to be the identity matrix.
|
||||
*
|
||||
* @return this */
|
||||
@Override
|
||||
public Matrix setIdentity()
|
||||
{ return setIdentity(this); }
|
||||
|
||||
/** Set this matrix to 0.
|
||||
*
|
||||
* @return this */
|
||||
@Override
|
||||
public Matrix setZero()
|
||||
{ return setZero(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;
|
||||
}
|
||||
|
||||
/** 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();
|
||||
}
|
||||
|
||||
/** 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); }
|
||||
}
|
|
@ -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 com.github.hydos.ginger.engine.math.matrixes;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.nio.FloatBuffer;
|
||||
|
||||
import com.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;
|
||||
/** 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;
|
||||
}
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
||||
/** 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 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 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;
|
||||
}
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
||||
/** 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 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;
|
||||
}
|
||||
|
||||
public float m00,
|
||||
m01,
|
||||
m02,
|
||||
m10,
|
||||
m11,
|
||||
m12,
|
||||
m20,
|
||||
m21,
|
||||
m22;
|
||||
|
||||
/** Constructor for Matrix3f. Matrix is initialised to the identity. */
|
||||
public Matrix3f()
|
||||
{
|
||||
super();
|
||||
setIdentity();
|
||||
}
|
||||
|
||||
/** @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;
|
||||
}
|
||||
|
||||
/** Invert this matrix
|
||||
*
|
||||
* @return this if successful, null otherwise */
|
||||
@Override
|
||||
public Matrix invert()
|
||||
{ return invert(this, this); }
|
||||
|
||||
/** 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 another matrix
|
||||
*
|
||||
* @param src The source matrix
|
||||
* @return this */
|
||||
public Matrix3f load(Matrix3f src)
|
||||
{ return load(src, 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;
|
||||
}
|
||||
|
||||
/** 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); }
|
||||
|
||||
/** Set this matrix to be the identity matrix.
|
||||
*
|
||||
* @return this */
|
||||
@Override
|
||||
public Matrix setIdentity()
|
||||
{ return setIdentity(this); }
|
||||
|
||||
/** Set this matrix to 0.
|
||||
*
|
||||
* @return this */
|
||||
@Override
|
||||
public Matrix setZero()
|
||||
{ return setZero(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;
|
||||
}
|
||||
|
||||
/** 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();
|
||||
}
|
||||
|
||||
/** 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); }
|
||||
}
|
|
@ -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 com.github.hydos.ginger.engine.math.matrixes;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.nio.FloatBuffer;
|
||||
|
||||
import org.joml.Vector4f;
|
||||
|
||||
import com.github.hydos.ginger.engine.math.vectors.*;
|
||||
|
||||
/** Holds a 4x4 float matrix.
|
||||
*
|
||||
* @author foo */
|
||||
public class Matrix4f extends Matrix implements Serializable
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
/** 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;
|
||||
}
|
||||
|
||||
/** 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 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;
|
||||
}
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
||||
/** 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 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;
|
||||
}
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/** @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;
|
||||
}
|
||||
|
||||
/** Invert this matrix
|
||||
*
|
||||
* @return this if successful, null otherwise */
|
||||
@Override
|
||||
public Matrix invert()
|
||||
{ return invert(this, this); }
|
||||
|
||||
/** 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 another matrix4f
|
||||
*
|
||||
* @param src The source matrix
|
||||
* @return this */
|
||||
public Matrix4f load(Matrix4f src)
|
||||
{ return load(src, 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;
|
||||
}
|
||||
|
||||
/** 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); }
|
||||
|
||||
/** 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); }
|
||||
|
||||
/** 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); }
|
||||
|
||||
/** Scales this matrix
|
||||
*
|
||||
* @param vec The vector to scale by
|
||||
* @return this */
|
||||
public Matrix4f scale(Vector3f vec)
|
||||
{ return scale(vec, this, this); }
|
||||
|
||||
/** Set this matrix to be the identity matrix.
|
||||
*
|
||||
* @return this */
|
||||
@Override
|
||||
public Matrix setIdentity()
|
||||
{ return setIdentity(this); }
|
||||
|
||||
/** Set this matrix to 0.
|
||||
*
|
||||
* @return this */
|
||||
@Override
|
||||
public Matrix setZero()
|
||||
{ return setZero(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 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;
|
||||
}
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
||||
/** 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();
|
||||
}
|
||||
|
||||
/** Translate this matrix
|
||||
*
|
||||
* @param vec The vector to translate by
|
||||
* @return this */
|
||||
public Matrix4f translate(Vector2f vec)
|
||||
{ return translate(vec, this); }
|
||||
|
||||
/** 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 this matrix
|
||||
*
|
||||
* @param vec The vector to translate by
|
||||
* @return this */
|
||||
public Matrix4f translate(Vector3f vec)
|
||||
{ return translate(vec, this); }
|
||||
|
||||
/** 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); }
|
||||
|
||||
/** 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 Matrix4f transpose(Matrix4f dest)
|
||||
{ return transpose(this, 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 com.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 com.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 com.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 com.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 com.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");
|
||||
}
|
||||
|
||||
/** Scale this vector
|
||||
*
|
||||
* @param scale The scale factor
|
||||
* @return this */
|
||||
public abstract Vector scale(float scale);
|
||||
|
||||
/** 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);
|
||||
}
|
|
@ -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 com.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;
|
||||
/** 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;
|
||||
}
|
||||
}
|
||||
|
||||
/** 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);
|
||||
}
|
||||
|
||||
/** 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; }
|
||||
|
||||
/** 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;
|
||||
}
|
||||
}
|
||||
|
||||
public float x, y;
|
||||
|
||||
/** Constructor for Vector2f. */
|
||||
public Vector2f()
|
||||
{ super(); }
|
||||
|
||||
/** Constructor. */
|
||||
public Vector2f(float x, float y)
|
||||
{ set(x, y); }
|
||||
|
||||
/** Constructor. */
|
||||
public Vector2f(ReadableVector2f src)
|
||||
{ set(src); }
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
/** @return x */
|
||||
@Override
|
||||
public final float getX()
|
||||
{ return x; }
|
||||
|
||||
/** @return y */
|
||||
@Override
|
||||
public final float getY()
|
||||
{ return y; }
|
||||
|
||||
/** @return the length squared of the vector */
|
||||
@Override
|
||||
public float lengthSquared()
|
||||
{ return x * x + y * y; }
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.lwjgl.vector.Vector#scale(float)
|
||||
*/
|
||||
@Override
|
||||
public Vector scale(float scale)
|
||||
{
|
||||
x *= scale;
|
||||
y *= scale;
|
||||
return this;
|
||||
}
|
||||
|
||||
/* (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;
|
||||
}
|
||||
|
||||
/** 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; }
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
||||
/* (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();
|
||||
}
|
||||
|
||||
/** 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;
|
||||
}
|
||||
}
|
|
@ -1,341 +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 com.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$ */
|
||||
@Deprecated
|
||||
public class Vector3f extends Vector implements Serializable, ReadableVector3f, WritableVector3f
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
/** 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;
|
||||
}
|
||||
}
|
||||
|
||||
/** 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);
|
||||
}
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
||||
/** 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; }
|
||||
|
||||
/** 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;
|
||||
}
|
||||
}
|
||||
|
||||
public float x, y, z;
|
||||
|
||||
/** Constructor for Vector3f. */
|
||||
public Vector3f()
|
||||
{ super(); }
|
||||
|
||||
/** Constructor */
|
||||
public Vector3f(float x, float y, float z)
|
||||
{ set(x, y, z); }
|
||||
|
||||
/** Constructor */
|
||||
public Vector3f(ReadableVector3f src)
|
||||
{ set(src); }
|
||||
|
||||
public Vector3f add(Vector3f vector)
|
||||
{ return new Vector3f(x + vector.x, y + vector.y, z + vector.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;
|
||||
}
|
||||
|
||||
/** @return x */
|
||||
@Override
|
||||
public final float getX()
|
||||
{ return x; }
|
||||
|
||||
/** @return y */
|
||||
@Override
|
||||
public final float getY()
|
||||
{ return y; }
|
||||
|
||||
/* (Overrides)
|
||||
* @see org.lwjgl.vector.ReadableVector3f#getZ()
|
||||
*/
|
||||
@Override
|
||||
public float getZ()
|
||||
{ return z; }
|
||||
|
||||
/** @return the length squared of the vector */
|
||||
@Override
|
||||
public float lengthSquared()
|
||||
{ return x * x + y * y + z * z; }
|
||||
|
||||
/* (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;
|
||||
}
|
||||
|
||||
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); }
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
||||
/* (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.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;
|
||||
}
|
||||
|
||||
/** 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; }
|
||||
|
||||
/* (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();
|
||||
}
|
||||
|
||||
/** 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;
|
||||
}
|
||||
}
|
|
@ -1,332 +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 com.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$ */
|
||||
@Deprecated
|
||||
public class Vector4f extends Vector implements Serializable, ReadableVector4f, WritableVector4f
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
/** 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;
|
||||
}
|
||||
}
|
||||
|
||||
/** 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);
|
||||
}
|
||||
|
||||
/** 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; }
|
||||
|
||||
/** 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;
|
||||
}
|
||||
}
|
||||
|
||||
public float x, y, z, w;
|
||||
|
||||
/** Constructor for Vector4f. */
|
||||
public Vector4f()
|
||||
{ super(); }
|
||||
|
||||
/** Constructor */
|
||||
public Vector4f(float x, float y, float z, float w)
|
||||
{ set(x, y, z, w); }
|
||||
|
||||
/** Constructor */
|
||||
public Vector4f(ReadableVector4f src)
|
||||
{ set(src); }
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
/* (Overrides)
|
||||
* @see org.lwjgl.vector.ReadableVector3f#getZ()
|
||||
*/
|
||||
@Override
|
||||
public float getW()
|
||||
{ return w; }
|
||||
|
||||
/** @return x */
|
||||
@Override
|
||||
public final float getX()
|
||||
{ return x; }
|
||||
|
||||
/** @return y */
|
||||
@Override
|
||||
public final float getY()
|
||||
{ return y; }
|
||||
|
||||
/* (Overrides)
|
||||
* @see org.lwjgl.vector.ReadableVector3f#getZ()
|
||||
*/
|
||||
@Override
|
||||
public float getZ()
|
||||
{ return z; }
|
||||
|
||||
/** @return the length squared of the vector */
|
||||
@Override
|
||||
public float lengthSquared()
|
||||
{ return x * x + y * y + z * z + w * w; }
|
||||
|
||||
/* (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;
|
||||
}
|
||||
|
||||
/** 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;
|
||||
}
|
||||
|
||||
/* (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.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;
|
||||
}
|
||||
|
||||
/** Set W
|
||||
*
|
||||
* @param w */
|
||||
@Override
|
||||
public void setW(float w)
|
||||
{ this.w = w; }
|
||||
|
||||
/** 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; }
|
||||
|
||||
/* (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; }
|
||||
|
||||
/** 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;
|
||||
}
|
||||
}
|
|
@ -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 com.github.hydos.ginger.engine.math.vectors;
|
||||
|
||||
/** Writable interface to Vector2fs
|
||||
*
|
||||
* @author $author$
|
||||
* @version $revision$
|
||||
* $Id$ */
|
||||
public interface WritableVector2f
|
||||
{
|
||||
/** Set the X,Y values
|
||||
*
|
||||
* @param x
|
||||
* @param y */
|
||||
void set(float x, float y);
|
||||
|
||||
/** Set the X value
|
||||
*
|
||||
* @param x */
|
||||
void setX(float x);
|
||||
|
||||
/** Set the Y value
|
||||
*
|
||||
* @param y */
|
||||
void setY(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 com.github.hydos.ginger.engine.math.vectors;
|
||||
|
||||
/** Writable interface to Vector3fs
|
||||
*
|
||||
* @author $author$
|
||||
* @version $revision$
|
||||
* $Id$ */
|
||||
public interface WritableVector3f extends WritableVector2f
|
||||
{
|
||||
/** Set the X,Y,Z values
|
||||
*
|
||||
* @param x
|
||||
* @param y
|
||||
* @param z */
|
||||
void set(float x, float y, float z);
|
||||
|
||||
/** Set the Z value
|
||||
*
|
||||
* @param z */
|
||||
void setZ(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 com.github.hydos.ginger.engine.math.vectors;
|
||||
|
||||
/** Writable interface to Vector4fs
|
||||
*
|
||||
* @author $author$
|
||||
* @version $revision$
|
||||
* $Id$ */
|
||||
public interface WritableVector4f extends WritableVector3f
|
||||
{
|
||||
/** 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);
|
||||
|
||||
/** Set the W value
|
||||
*
|
||||
* @param w */
|
||||
void setW(float w);
|
||||
}
|
|
@ -17,6 +17,6 @@ public class ModelLoader
|
|||
public static TexturedModel loadModel(String objPath, String texturePath)
|
||||
{
|
||||
Mesh data = OBJFileLoader.loadModel(objPath);
|
||||
return new TexturedModel(Loader.loadToVAO(data.getVertices(), data.getIndices(),data.getNormals(), data.getTextureCoords()), new ModelTexture(texturePath));
|
||||
return new TexturedModel(Loader.loadToVAO(data.getVertices(), data.getIndices(), data.getNormals(), data.getTextureCoords()), new ModelTexture(texturePath));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
package com.github.hydos.ginger.engine.obj;
|
||||
|
||||
import org.joml.*;
|
||||
import org.lwjgl.assimp.*;
|
||||
import org.lwjgl.assimp.AIVector3D.Buffer;
|
||||
|
||||
import com.github.hydos.ginger.engine.math.vectors.*;
|
||||
|
||||
public class OBJFileLoader
|
||||
{
|
||||
public static String resourceLocation = "~/Desktop/Ginger3D/src/main/resources/models/";
|
||||
|
@ -32,8 +31,7 @@ public class OBJFileLoader
|
|||
if (mesh.mNumUVComponents().get(0) != 0)
|
||||
{
|
||||
AIVector3D texture = mesh.mTextureCoords(0).get(i);
|
||||
meshTextureCoord.setX(texture.x());
|
||||
meshTextureCoord.setY(texture.y());
|
||||
meshTextureCoord.set(texture.x(), texture.y());
|
||||
}
|
||||
vertexList[i] = new Vertex(meshVertex, meshNormal, meshTextureCoord);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package com.github.hydos.ginger.engine.obj;
|
||||
|
||||
import com.github.hydos.ginger.engine.math.vectors.*;
|
||||
import org.joml.*;
|
||||
|
||||
public class Vertex
|
||||
{
|
||||
|
|
|
@ -3,7 +3,8 @@ package com.github.hydos.ginger.engine.obj.normals;
|
|||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
import com.github.hydos.ginger.engine.math.vectors.*;
|
||||
import org.joml.*;
|
||||
|
||||
import com.github.hydos.ginger.engine.render.models.RawModel;
|
||||
import com.github.hydos.ginger.engine.utils.Loader;
|
||||
|
||||
|
@ -12,18 +13,18 @@ public class NormalMappedObjLoader
|
|||
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 = v1.getPosition().sub(v0.getPosition());
|
||||
Vector3f delatPos2 = v2.getPosition().sub(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 = uv1.sub(uv0);
|
||||
Vector2f deltaUv2 = uv2.sub(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);
|
||||
delatPos1.mul(deltaUv2.y);
|
||||
delatPos2.mul(deltaUv1.y);
|
||||
Vector3f tangent = delatPos1.sub(delatPos2);
|
||||
tangent.mul(r);
|
||||
v0.addTangent(tangent);
|
||||
v1.addTangent(tangent);
|
||||
v2.addTangent(tangent);
|
||||
|
|
|
@ -2,7 +2,7 @@ package com.github.hydos.ginger.engine.obj.normals;
|
|||
|
||||
import java.util.*;
|
||||
|
||||
import com.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class VertexNM
|
||||
{
|
||||
|
@ -31,8 +31,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();
|
||||
}
|
||||
|
||||
//NEW
|
||||
|
|
|
@ -7,44 +7,32 @@ public class StaticCube
|
|||
//@formatter:off
|
||||
public static float[] vertices =
|
||||
{
|
||||
|
||||
//North (back) face
|
||||
-0.5f, 0.5f, -0.5f,
|
||||
-0.5f, -0.5f, -0.5f,
|
||||
0.5f, -0.5f, -0.5f,
|
||||
0.5f, 0.5f, -0.5f,
|
||||
|
||||
|
||||
//South (front) face
|
||||
-0.5f, 0.5f, 0.5f,
|
||||
-0.5f, -0.5f, 0.5f,
|
||||
0.5f, -0.5f, 0.5f,
|
||||
0.5f, 0.5f, 0.5f,
|
||||
|
||||
|
||||
|
||||
0.5f, 0.5f, -0.5f,
|
||||
0.5f, -0.5f, -0.5f,
|
||||
0.5f, -0.5f, 0.5f,
|
||||
0.5f, 0.5f, 0.5f,
|
||||
|
||||
|
||||
|
||||
-0.5f, 0.5f, -0.5f,
|
||||
-0.5f, -0.5f, -0.5f,
|
||||
-0.5f, -0.5f, 0.5f,
|
||||
-0.5f, 0.5f, 0.5f,
|
||||
|
||||
//Top face
|
||||
-0.5f, 0.5f, 0.5f,
|
||||
-0.5f, 0.5f, -0.5f,
|
||||
0.5f, 0.5f, -0.5f,
|
||||
0.5f, 0.5f, 0.5f,
|
||||
|
||||
|
||||
//Bottom face
|
||||
-0.5f, -0.5f, 0.5f,
|
||||
-0.5f, -0.5f, -0.5f,
|
||||
-0.5f, -0.5f, -0.5f,
|
||||
0.5f, -0.5f, -0.5f,
|
||||
0.5f, -0.5f, 0.5f
|
||||
};
|
||||
|
@ -77,44 +65,33 @@ public class StaticCube
|
|||
};
|
||||
public static int[] indices =
|
||||
{
|
||||
0, 1, 3,
|
||||
3, 1, 2,
|
||||
|
||||
4, 5, 7,
|
||||
7, 5, 6,
|
||||
|
||||
8, 9, 11,
|
||||
11, 9, 10,
|
||||
|
||||
12, 13, 15,
|
||||
15, 13, 14,
|
||||
|
||||
16, 17, 19,
|
||||
19, 17, 18,
|
||||
|
||||
20, 21, 23,
|
||||
23, 21, 22
|
||||
|
||||
0, 1, 3,
|
||||
3, 1, 2,
|
||||
4, 5, 7,
|
||||
7, 5, 6,
|
||||
8, 9, 11,
|
||||
11, 9, 10,
|
||||
12, 13, 15,
|
||||
15, 13, 14,
|
||||
16, 17, 19,
|
||||
19, 17, 18,
|
||||
20, 21, 23,
|
||||
23, 21, 22
|
||||
};
|
||||
//@formatter:on
|
||||
|
||||
private static Mesh mesh = null;
|
||||
|
||||
public static Mesh getCube()
|
||||
public static Mesh getCube()
|
||||
{
|
||||
if (mesh == null)
|
||||
{
|
||||
mesh = new Mesh(vertices, textureCoords, new float[vertices.length], indices, vertices.length);
|
||||
}
|
||||
if (mesh == null)
|
||||
{ mesh = new Mesh(vertices, textureCoords, new float[vertices.length], indices, vertices.length); }
|
||||
return mesh;
|
||||
}
|
||||
|
||||
public static void scaleCube(float multiplier)
|
||||
public static void scaleCube(float multiplier)
|
||||
{
|
||||
for (int i = 0; i < vertices.length; i++)
|
||||
{
|
||||
vertices[i] = vertices[i] * multiplier;
|
||||
}
|
||||
{ vertices[i] = vertices[i] * multiplier; }
|
||||
mesh = new Mesh(vertices, textureCoords, new float[vertices.length], indices, vertices.length);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
package com.github.hydos.ginger.engine.particle;
|
||||
|
||||
import java.lang.Math;
|
||||
|
||||
import org.joml.*;
|
||||
|
||||
import com.github.hydos.ginger.engine.cameras.Camera;
|
||||
import com.github.hydos.ginger.engine.io.Window;
|
||||
import com.github.hydos.ginger.engine.math.vectors.*;
|
||||
import com.github.hydos.ginger.main.settings.Constants;
|
||||
|
||||
public class Particle
|
||||
|
@ -71,9 +74,9 @@ public class Particle
|
|||
float time = (float) Window.getTime() / 1000000;
|
||||
velocity.y += Constants.gravity.y() * gravityEffect * time;
|
||||
Vector3f change = new Vector3f(velocity);
|
||||
change.scale(time);
|
||||
Vector3f.add(change, position, position);
|
||||
distance = Vector3f.sub(camera.getPosition(), position, null).lengthSquared();
|
||||
change.mul(time);
|
||||
position.add(change, position);
|
||||
distance = camera.getPosition().sub(position).lengthSquared();
|
||||
elapsedTime += time;
|
||||
updateTextureCoordInfo();
|
||||
return elapsedTime < lifeLength;
|
||||
|
|
|
@ -3,8 +3,9 @@ package com.github.hydos.ginger.engine.particle;
|
|||
import java.util.*;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
|
||||
import com.github.hydos.ginger.engine.cameras.Camera;
|
||||
import com.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import com.github.hydos.ginger.engine.render.renderers.ParticleRenderer;
|
||||
|
||||
public class ParticleMaster
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
package com.github.hydos.ginger.engine.particle;
|
||||
|
||||
import java.lang.Math;
|
||||
import java.util.Random;
|
||||
|
||||
import org.joml.Vector4f;
|
||||
import org.joml.*;
|
||||
|
||||
import com.github.hydos.ginger.engine.io.Window;
|
||||
import com.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import com.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
|
||||
public class ParticleSystem
|
||||
{
|
||||
|
@ -22,24 +21,24 @@ 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(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);
|
||||
}
|
||||
else if (coneDirection.z == -1)
|
||||
{ direction.z *= -1; }
|
||||
return new Vector3f(direction.x, direction.y, direction.z);
|
||||
}
|
||||
|
||||
private float pps, averageSpeed, gravityComplient, averageLifeLength, averageScale;
|
||||
private float speedError, lifeError, scaleError = 0;
|
||||
private boolean randomRotation = false;
|
||||
private Vector3f direction;
|
||||
private float directionDeviation = 0;
|
||||
private ParticleTexture texture;
|
||||
|
||||
private Random random = new Random();
|
||||
|
||||
public ParticleSystem(ParticleTexture texture, float pps, float speed, float gravityComplient, float lifeLength, float scale)
|
||||
|
@ -63,8 +62,8 @@ public class ParticleSystem
|
|||
{
|
||||
velocity = generateRandomUnitVector();
|
||||
}
|
||||
velocity.normalise();
|
||||
velocity.scale(generateValue(averageSpeed, speedError));
|
||||
velocity.normalize();
|
||||
velocity.mul(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));
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
package com.github.hydos.ginger.engine.render;
|
||||
|
||||
import java.lang.Math;
|
||||
import java.util.*;
|
||||
|
||||
import org.joml.Vector4f;
|
||||
import org.joml.*;
|
||||
import org.lwjgl.opengl.*;
|
||||
|
||||
import com.github.halotroop.litecraft.world.World;
|
||||
|
@ -11,27 +12,26 @@ import com.github.hydos.ginger.engine.cameras.Camera;
|
|||
import com.github.hydos.ginger.engine.elements.GuiTexture;
|
||||
import com.github.hydos.ginger.engine.elements.objects.*;
|
||||
import com.github.hydos.ginger.engine.io.Window;
|
||||
import com.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import com.github.hydos.ginger.engine.render.models.TexturedModel;
|
||||
import com.github.hydos.ginger.engine.render.renderers.*;
|
||||
import com.github.hydos.ginger.engine.render.shaders.*;
|
||||
import com.github.hydos.ginger.engine.shadow.ShadowMapMasterRenderer;
|
||||
import com.github.hydos.ginger.engine.terrain.Terrain;
|
||||
|
||||
public class MasterRenderer
|
||||
{
|
||||
public static final float FOV = 80f;
|
||||
public static final float NEAR_PLANE = 0.1f;
|
||||
private static final float FAR_PLANE = 1000f;
|
||||
|
||||
public static void disableCulling()
|
||||
{ GL11.glDisable(GL11.GL_CULL_FACE); }
|
||||
|
||||
|
||||
public static void enableCulling()
|
||||
{
|
||||
// GL11.glEnable(GL11.GL_CULL_FACE);
|
||||
// GL11.glCullFace(GL11.GL_BACK);
|
||||
// GL11.glEnable(GL11.GL_CULL_FACE);
|
||||
// GL11.glCullFace(GL11.GL_BACK);
|
||||
}
|
||||
|
||||
|
||||
public BlockRenderer blockRenderer;
|
||||
private StaticShader entityShader;
|
||||
public ObjectRenderer entityRenderer;
|
||||
|
@ -41,9 +41,7 @@ public class MasterRenderer
|
|||
private NormalMappingRenderer normalRenderer;
|
||||
private Matrix4f projectionMatrix;
|
||||
private ShadowMapMasterRenderer shadowMapRenderer;
|
||||
|
||||
private Map<TexturedModel, List<RenderObject>> entities = new HashMap<TexturedModel, List<RenderObject>>();
|
||||
|
||||
private Map<TexturedModel, List<RenderObject>> normalMapEntities = new HashMap<TexturedModel, List<RenderObject>>();
|
||||
|
||||
public MasterRenderer(Camera camera)
|
||||
|
@ -74,12 +72,12 @@ public class MasterRenderer
|
|||
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 Matrix4f getProjectionMatrix()
|
||||
|
@ -159,16 +157,7 @@ public class MasterRenderer
|
|||
normalRenderer.render(normalMapEntities, clipPlane, lights, camera);
|
||||
}
|
||||
|
||||
public void renderScene(List<RenderObject> entities, List<RenderObject> normalEntities, List<Terrain> terrains, List<Light> lights, Camera camera, Vector4f clipPlane)
|
||||
{
|
||||
prepare();
|
||||
renderEntities(entities, camera, lights);
|
||||
renderNormalEntities(normalEntities, lights, camera, clipPlane);
|
||||
renderTerrains(terrains, lights, camera);
|
||||
skyboxRenderer.render(camera);
|
||||
}
|
||||
|
||||
public void renderSceneNoTerrain(List<RenderObject> entities, List<RenderObject> normalEntities, List<Light> lights, Camera camera, Vector4f clipPlane, World world)
|
||||
public void renderScene(List<RenderObject> entities, List<RenderObject> normalEntities, List<Light> lights, Camera camera, Vector4f clipPlane, World world)
|
||||
{
|
||||
prepare();
|
||||
renderEntities(entities, camera, lights);
|
||||
|
@ -184,7 +173,4 @@ public class MasterRenderer
|
|||
shadowMapRenderer.render(entities, sun);
|
||||
entities.clear();
|
||||
}
|
||||
|
||||
private void renderTerrains(List<Terrain> terrains, List<Light> lights, Camera camera)
|
||||
{}
|
||||
}
|
||||
|
|
|
@ -2,11 +2,11 @@ package com.github.hydos.ginger.engine.render.renderers;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
import org.lwjgl.opengl.*;
|
||||
|
||||
import com.github.hydos.ginger.engine.elements.GuiTexture;
|
||||
import com.github.hydos.ginger.engine.math.Maths;
|
||||
import com.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import com.github.hydos.ginger.engine.render.Renderer;
|
||||
import com.github.hydos.ginger.engine.render.models.RawModel;
|
||||
import com.github.hydos.ginger.engine.render.shaders.GuiShader;
|
||||
|
|
|
@ -2,14 +2,13 @@ package com.github.hydos.ginger.engine.render.renderers;
|
|||
|
||||
import java.util.*;
|
||||
|
||||
import org.joml.Vector4f;
|
||||
import org.joml.*;
|
||||
import org.lwjgl.opengl.*;
|
||||
|
||||
import com.github.hydos.ginger.engine.cameras.Camera;
|
||||
import com.github.hydos.ginger.engine.elements.objects.*;
|
||||
import com.github.hydos.ginger.engine.io.Window;
|
||||
import com.github.hydos.ginger.engine.math.Maths;
|
||||
import com.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import com.github.hydos.ginger.engine.render.*;
|
||||
import com.github.hydos.ginger.engine.render.models.*;
|
||||
import com.github.hydos.ginger.engine.render.shaders.NormalMappingShader;
|
||||
|
|
|
@ -2,14 +2,14 @@ package com.github.hydos.ginger.engine.render.renderers;
|
|||
|
||||
import java.util.*;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
import org.lwjgl.opengl.*;
|
||||
|
||||
import com.github.halotroop.litecraft.types.block.BlockEntity;
|
||||
import com.github.halotroop.litecraft.types.block.BlockInstance;
|
||||
import com.github.hydos.ginger.engine.api.GingerRegister;
|
||||
import com.github.hydos.ginger.engine.elements.objects.RenderObject;
|
||||
import com.github.hydos.ginger.engine.io.Window;
|
||||
import com.github.hydos.ginger.engine.math.Maths;
|
||||
import com.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import com.github.hydos.ginger.engine.render.*;
|
||||
import com.github.hydos.ginger.engine.render.models.*;
|
||||
import com.github.hydos.ginger.engine.render.shaders.StaticShader;
|
||||
|
@ -66,11 +66,11 @@ public class ObjectRenderer extends Renderer
|
|||
List<RenderObject> batch = entities.get(model);
|
||||
for (RenderObject entity : batch)
|
||||
{
|
||||
if(entity.isVisible) {
|
||||
if (entity.isVisible)
|
||||
{
|
||||
prepareInstance(entity);
|
||||
GL11.glDrawElements(GL11.GL_TRIANGLES, model.getRawModel().getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
|
||||
}
|
||||
|
||||
}
|
||||
unbindTexturedModel();
|
||||
}
|
||||
|
@ -84,15 +84,16 @@ public class ObjectRenderer extends Renderer
|
|||
GL30.glBindVertexArray(0);
|
||||
}
|
||||
|
||||
public void render(List<BlockEntity> renderList)
|
||||
{
|
||||
public void render(List<BlockInstance> renderList)
|
||||
{
|
||||
prepare();
|
||||
shader.start();
|
||||
shader.loadSkyColour(Window.getColour());
|
||||
shader.loadViewMatrix(GingerRegister.getInstance().game.data.camera);
|
||||
for (RenderObject entity : renderList)
|
||||
{
|
||||
if (entity != null && entity.getModel() != null) {
|
||||
if (entity != null && entity.getModel() != null)
|
||||
{
|
||||
TexturedModel model = entity.getModel();
|
||||
prepareTexturedModel(model);
|
||||
prepareInstance(entity);
|
||||
|
@ -102,5 +103,4 @@ public class ObjectRenderer extends Renderer
|
|||
}
|
||||
shader.stop();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,15 +1,15 @@
|
|||
package com.github.hydos.ginger.engine.render.renderers;
|
||||
|
||||
import java.lang.Math;
|
||||
import java.nio.FloatBuffer;
|
||||
import java.util.*;
|
||||
|
||||
import org.joml.*;
|
||||
import org.lwjgl.BufferUtils;
|
||||
import org.lwjgl.opengl.*;
|
||||
|
||||
import com.github.hydos.ginger.engine.cameras.Camera;
|
||||
import com.github.hydos.ginger.engine.math.Maths;
|
||||
import com.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import com.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
import com.github.hydos.ginger.engine.particle.*;
|
||||
import com.github.hydos.ginger.engine.render.Renderer;
|
||||
import com.github.hydos.ginger.engine.render.models.RawModel;
|
||||
|
@ -108,40 +108,40 @@ public class ParticleRenderer extends Renderer
|
|||
|
||||
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 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 = viewMatrix.mul(modelMatrix);
|
||||
storeMatrixData(modelViewMatrix, vboData);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
package com.github.hydos.ginger.engine.render.renderers;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
import org.lwjgl.opengl.*;
|
||||
|
||||
import com.github.hydos.ginger.engine.cameras.Camera;
|
||||
import com.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import com.github.hydos.ginger.engine.render.Renderer;
|
||||
import com.github.hydos.ginger.engine.render.models.RawModel;
|
||||
import com.github.hydos.ginger.engine.render.shaders.SkyboxShader;
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
package com.github.hydos.ginger.engine.render.shaders;
|
||||
|
||||
import org.joml.*;
|
||||
|
||||
import com.github.hydos.ginger.engine.font.GUIText;
|
||||
import com.github.hydos.ginger.engine.math.vectors.*;
|
||||
|
||||
public class FontShader extends ShaderProgram
|
||||
{
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package com.github.hydos.ginger.engine.render.shaders;
|
||||
|
||||
import com.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import org.joml.Matrix4f;
|
||||
|
||||
public class GuiShader extends ShaderProgram
|
||||
{
|
||||
|
|
|
@ -2,11 +2,9 @@ package com.github.hydos.ginger.engine.render.shaders;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
import org.joml.Vector4f;
|
||||
import org.joml.*;
|
||||
|
||||
import com.github.hydos.ginger.engine.elements.objects.Light;
|
||||
import com.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import com.github.hydos.ginger.engine.math.vectors.*;
|
||||
|
||||
public class NormalMappingShader extends ShaderProgram
|
||||
{
|
||||
|
@ -75,7 +73,7 @@ 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);
|
||||
viewMatrix.transform(eyeSpacePos);
|
||||
return new Vector3f(eyeSpacePos.x, eyeSpacePos.y, eyeSpacePos.z);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package com.github.hydos.ginger.engine.render.shaders;
|
||||
|
||||
import com.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import org.joml.Matrix4f;
|
||||
|
||||
public class ParticleShader extends ShaderProgram
|
||||
{
|
||||
|
|
|
@ -1,18 +1,12 @@
|
|||
package com.github.hydos.ginger.engine.render.shaders;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.FloatBuffer;
|
||||
|
||||
import org.joml.Vector4f;
|
||||
import org.lwjgl.BufferUtils;
|
||||
import org.joml.*;
|
||||
import org.lwjgl.opengl.*;
|
||||
|
||||
import com.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import com.github.hydos.ginger.engine.math.vectors.*;
|
||||
|
||||
public abstract class ShaderProgram
|
||||
{
|
||||
private static FloatBuffer matrixBuffer = BufferUtils.createFloatBuffer(16);
|
||||
private static int loadShader(String file, int type)
|
||||
{
|
||||
StringBuilder shaderSource = new StringBuilder();
|
||||
|
@ -40,9 +34,9 @@ public abstract class ShaderProgram
|
|||
}
|
||||
return shaderID;
|
||||
}
|
||||
|
||||
private int programID;
|
||||
private int vertexShaderID;
|
||||
|
||||
private int fragmentShaderID;
|
||||
|
||||
public ShaderProgram(String vertexFile, String fragmentFile)
|
||||
|
@ -97,9 +91,9 @@ public abstract class ShaderProgram
|
|||
|
||||
protected void loadMatrix(int location, Matrix4f matrix)
|
||||
{
|
||||
matrix.store(matrixBuffer);
|
||||
matrixBuffer.flip();
|
||||
GL20.glUniformMatrix4fv(location, false, matrixBuffer);
|
||||
float[] fm = new float[16];
|
||||
matrix.get(fm);
|
||||
GL20.glUniformMatrix4fv(location, false, fm);
|
||||
}
|
||||
|
||||
protected void loadVector(int location, Vector3f vector)
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
package com.github.hydos.ginger.engine.render.shaders;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
|
||||
import com.github.hydos.ginger.engine.cameras.Camera;
|
||||
import com.github.hydos.ginger.engine.math.Maths;
|
||||
import com.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
|
||||
public class SkyboxShader extends ShaderProgram
|
||||
{
|
||||
|
@ -29,9 +30,9 @@ public class SkyboxShader extends ShaderProgram
|
|||
public void loadViewMatrix(Camera camera)
|
||||
{
|
||||
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,11 @@ package com.github.hydos.ginger.engine.render.shaders;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
import org.joml.*;
|
||||
|
||||
import com.github.hydos.ginger.engine.cameras.Camera;
|
||||
import com.github.hydos.ginger.engine.elements.objects.Light;
|
||||
import com.github.hydos.ginger.engine.math.Maths;
|
||||
import com.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import com.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
|
||||
public class StaticShader extends ShaderProgram
|
||||
{
|
||||
|
|
|
@ -2,11 +2,11 @@ package com.github.hydos.ginger.engine.render.shaders;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
import org.joml.*;
|
||||
|
||||
import com.github.hydos.ginger.engine.cameras.Camera;
|
||||
import com.github.hydos.ginger.engine.elements.objects.Light;
|
||||
import com.github.hydos.ginger.engine.math.Maths;
|
||||
import com.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import com.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
|
||||
public class TerrainShader extends ShaderProgram
|
||||
{
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.github.hydos.ginger.engine.render.texture;
|
||||
|
||||
public enum ColorDepth {
|
||||
R,RG,RGB,RGBA
|
||||
public enum ColorDepth
|
||||
{
|
||||
R, RG, RGB, RGBA
|
||||
}
|
||||
|
|
|
@ -39,9 +39,7 @@ public class Image
|
|||
}
|
||||
|
||||
private ByteBuffer image;
|
||||
|
||||
private int width, height;
|
||||
|
||||
private IntBuffer comp;
|
||||
|
||||
Image(int width, int heigh, ByteBuffer image, IntBuffer comp)
|
||||
|
@ -86,13 +84,13 @@ public class Image
|
|||
|
||||
public int getHeight()
|
||||
{ return height; }
|
||||
|
||||
public ByteBuffer getImage()
|
||||
{ return image; }
|
||||
|
||||
public int getWidth()
|
||||
{ return width; }
|
||||
|
||||
public IntBuffer getComp() {
|
||||
return comp;
|
||||
}
|
||||
public IntBuffer getComp()
|
||||
{ return comp; }
|
||||
}
|
|
@ -1,13 +1,10 @@
|
|||
package com.github.hydos.ginger.engine.render.tools;
|
||||
|
||||
import org.joml.Vector4f;
|
||||
import org.joml.*;
|
||||
|
||||
import com.github.hydos.ginger.engine.cameras.Camera;
|
||||
import com.github.hydos.ginger.engine.io.Window;
|
||||
import com.github.hydos.ginger.engine.math.Maths;
|
||||
import com.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import com.github.hydos.ginger.engine.math.vectors.*;
|
||||
import com.github.hydos.ginger.engine.terrain.Terrain;
|
||||
|
||||
public class MousePicker
|
||||
{
|
||||
|
@ -17,33 +14,20 @@ public class MousePicker
|
|||
private Matrix4f projectionMatrix;
|
||||
private Matrix4f viewMatrix;
|
||||
private Camera camera;
|
||||
private Terrain terrain;
|
||||
private Vector3f currentTerrainPoint;
|
||||
|
||||
public MousePicker(Camera cam, Matrix4f projection, Terrain terrain)
|
||||
public MousePicker(Camera cam, Matrix4f projection)
|
||||
{
|
||||
camera = cam;
|
||||
projectionMatrix = projection;
|
||||
viewMatrix = Maths.createViewMatrix(camera);
|
||||
this.terrain = terrain;
|
||||
}
|
||||
|
||||
private Vector3f binarySearch(int count, float start, float finish, Vector3f ray)
|
||||
{
|
||||
float half = start + ((finish - start) / 2f);
|
||||
if (count >= RECURSION_COUNT)
|
||||
{
|
||||
Vector3f endPoint = getPointOnRay(ray, half);
|
||||
Terrain terrain = getTerrain(endPoint.getX(), endPoint.getZ());
|
||||
if (terrain != null)
|
||||
{
|
||||
return endPoint;
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
{ return null; }
|
||||
if (intersectionInRange(start, half, ray))
|
||||
{
|
||||
return binarySearch(count + 1, start, half, ray);
|
||||
|
@ -84,12 +68,9 @@ public class MousePicker
|
|||
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 scaledRay.add(start);
|
||||
}
|
||||
|
||||
private Terrain getTerrain(float worldX, float worldZ)
|
||||
{ return terrain; }
|
||||
|
||||
private boolean intersectionInRange(float start, float finish, Vector3f ray)
|
||||
{
|
||||
Vector3f startPoint = getPointOnRay(ray, start);
|
||||
|
@ -105,34 +86,21 @@ public class MousePicker
|
|||
}
|
||||
|
||||
private boolean isUnderGround(Vector3f testPoint)
|
||||
{
|
||||
Terrain terrain = getTerrain(testPoint.getX(), testPoint.getZ());
|
||||
float height = 0;
|
||||
if (terrain != null)
|
||||
{ height = terrain.getHeightOfTerrain(testPoint.getX(), testPoint.getZ()); }
|
||||
if (testPoint.y < height)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
{ return false; }
|
||||
|
||||
private Vector4f toEyeCoords(Vector4f clipCoords)
|
||||
{
|
||||
Matrix4f invertedProjection = Matrix4f.invert(projectionMatrix, null);
|
||||
Vector4f eyeCoords = Matrix4f.transform(invertedProjection, clipCoords, null);
|
||||
Matrix4f invertedProjection = projectionMatrix.invert();
|
||||
Vector4f eyeCoords = invertedProjection.transform(clipCoords);
|
||||
return new Vector4f(eyeCoords.x, eyeCoords.y, -1f, 0f);
|
||||
}
|
||||
|
||||
private Vector3f toWorldCoords(Vector4f eyeCoords)
|
||||
{
|
||||
Matrix4f invertedView = Matrix4f.invert(viewMatrix, null);
|
||||
Vector4f rayWorld = Matrix4f.transform(invertedView, eyeCoords, null);
|
||||
Matrix4f invertedView = viewMatrix.invert();
|
||||
Vector4f rayWorld = invertedView.transform(eyeCoords);
|
||||
Vector3f mouseRay = new Vector3f(rayWorld.x, rayWorld.y, rayWorld.z);
|
||||
mouseRay.normalise();
|
||||
mouseRay.normalize();
|
||||
return mouseRay;
|
||||
}
|
||||
|
||||
|
|
|
@ -6,9 +6,9 @@ import com.github.hydos.ginger.engine.elements.GuiTexture;
|
|||
|
||||
public abstract class Screen
|
||||
{
|
||||
|
||||
public List<GuiTexture> elements;
|
||||
|
||||
public abstract void render();
|
||||
|
||||
public abstract void tick();
|
||||
|
||||
}
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
package com.github.hydos.ginger.engine.shadow;
|
||||
|
||||
import org.joml.Vector4f;
|
||||
import java.lang.Math;
|
||||
|
||||
import org.joml.*;
|
||||
|
||||
import com.github.hydos.ginger.engine.cameras.Camera;
|
||||
import com.github.hydos.ginger.engine.io.Window;
|
||||
import com.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import com.github.hydos.ginger.engine.math.vectors.Vector3f;
|
||||
import com.github.hydos.ginger.engine.render.MasterRenderer;
|
||||
|
||||
/** Represents the 3D cuboidal area of the world in which objects will cast
|
||||
|
@ -73,19 +73,15 @@ public class ShadowBox
|
|||
private Vector4f[] calculateFrustumVertices(Matrix4f rotation, Vector3f forwardVector,
|
||||
Vector3f centerNear, Vector3f centerFar)
|
||||
{
|
||||
Vector4f upVector4F = Matrix4f.transform(rotation, UP, null);
|
||||
Vector4f upVector4F = rotation.transform(UP);
|
||||
Vector3f upVector = new Vector3f(upVector4F.x, upVector4F.y, upVector4F.z);
|
||||
Vector3f rightVector = Vector3f.cross(forwardVector, upVector, null);
|
||||
Vector3f rightVector = forwardVector.cross(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 = centerFar.add(new Vector3f(upVector.x * farHeight, upVector.y * farHeight, upVector.z * farHeight));
|
||||
Vector3f farBottom = centerFar.add(new Vector3f(downVector.x * farHeight, downVector.y * farHeight, downVector.z * farHeight));
|
||||
Vector3f nearTop = centerNear.add(new Vector3f(upVector.x * nearHeight, upVector.y * nearHeight, upVector.z * nearHeight));
|
||||
Vector3f nearBottom = centerNear.add(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);
|
||||
|
@ -111,10 +107,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 = startPoint.add(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);
|
||||
lightViewMatrix.transform(point4f);
|
||||
return point4f;
|
||||
}
|
||||
|
||||
|
@ -147,8 +142,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);
|
||||
Vector4f processedCenter = Matrix4f.transform(invertedLight, cen, null);
|
||||
lightViewMatrix.invert(invertedLight);
|
||||
Vector4f processedCenter = invertedLight.transform(cen);
|
||||
return new Vector3f(processedCenter.x, processedCenter.y, processedCenter.z);
|
||||
}
|
||||
|
||||
|
@ -171,14 +166,14 @@ public class ShadowBox
|
|||
protected void update()
|
||||
{
|
||||
Matrix4f rotation = calculateCameraRotationMatrix();
|
||||
Vector4f forwardVector4F = Matrix4f.transform(rotation, FORWARD, null);
|
||||
Vector4f forwardVector4F = rotation.transform(FORWARD);
|
||||
Vector3f forwardVector = new Vector3f(forwardVector4F.x, forwardVector4F.y, forwardVector4F.z);
|
||||
Vector3f toFar = new Vector3f(forwardVector);
|
||||
toFar.scale(SHADOW_DISTANCE);
|
||||
toFar.mul(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);
|
||||
toNear.mul(MasterRenderer.NEAR_PLANE);
|
||||
Vector3f centerNear = toNear.add(cam.getPosition());
|
||||
Vector3f centerFar = toFar.add(cam.getPosition());
|
||||
Vector4f[] points = calculateFrustumVertices(rotation, forwardVector, centerNear,
|
||||
centerFar);
|
||||
boolean first = true;
|
||||
|
|
|
@ -25,6 +25,7 @@ public class ShadowFrameBuffer
|
|||
GL30.glBindFramebuffer(GL30.GL_DRAW_FRAMEBUFFER, frameBuffer);
|
||||
GL11.glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
/** Creates a depth buffer texture attachment.
|
||||
*
|
||||
* @param width
|
||||
|
@ -45,6 +46,7 @@ public class ShadowFrameBuffer
|
|||
GL32.glFramebufferTexture(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, texture, 0);
|
||||
return texture;
|
||||
}
|
||||
|
||||
/** Creates a frame buffer and binds it so that attachments can be added to
|
||||
* it. The draw buffer is set to none, indicating that there's no colour
|
||||
* buffer to be rendered to.
|
||||
|
@ -58,12 +60,10 @@ public class ShadowFrameBuffer
|
|||
GL11.glReadBuffer(GL11.GL_NONE);
|
||||
return frameBuffer;
|
||||
}
|
||||
|
||||
private final int WIDTH;
|
||||
|
||||
private final int HEIGHT;
|
||||
|
||||
private int fbo;
|
||||
|
||||
private int shadowMap;
|
||||
|
||||
/** Initialises the frame buffer and shadow map of a certain size.
|
||||
|
|
|
@ -2,11 +2,11 @@ package com.github.hydos.ginger.engine.shadow;
|
|||
|
||||
import java.util.*;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
import org.lwjgl.opengl.*;
|
||||
|
||||
import com.github.hydos.ginger.engine.elements.objects.RenderObject;
|
||||
import com.github.hydos.ginger.engine.math.Maths;
|
||||
import com.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import com.github.hydos.ginger.engine.render.MasterRenderer;
|
||||
import com.github.hydos.ginger.engine.render.models.*;
|
||||
|
||||
|
@ -51,7 +51,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 = projectionViewMatrix.mul(modelMatrix);
|
||||
shader.loadMvpMatrix(mvpMatrix);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
package com.github.hydos.ginger.engine.shadow;
|
||||
|
||||
import java.lang.Math;
|
||||
import java.util.*;
|
||||
|
||||
import org.joml.*;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
import com.github.hydos.ginger.engine.cameras.Camera;
|
||||
import com.github.hydos.ginger.engine.elements.objects.*;
|
||||
import com.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import com.github.hydos.ginger.engine.math.vectors.*;
|
||||
import com.github.hydos.ginger.engine.render.models.TexturedModel;
|
||||
|
||||
/** This class is in charge of using all of the classes in the shadows package to
|
||||
|
@ -17,6 +17,7 @@ import com.github.hydos.ginger.engine.render.models.TexturedModel;
|
|||
public class ShadowMapMasterRenderer
|
||||
{
|
||||
private static final int SHADOW_MAP_SIZE = 5120 * 2;
|
||||
|
||||
/** Create the offset for part of the conversion to shadow map space. This
|
||||
* conversion is necessary to convert from one coordinate system to the
|
||||
* coordinate system that we can use to sample to shadow map.
|
||||
|
@ -29,6 +30,7 @@ public class ShadowMapMasterRenderer
|
|||
offset.scale(new Vector3f(0.5f, 0.5f, 0.5f));
|
||||
return offset;
|
||||
}
|
||||
|
||||
private ShadowFrameBuffer shadowFbo;
|
||||
private ShadowShader shader;
|
||||
private ShadowBox shadowBox;
|
||||
|
@ -36,7 +38,6 @@ public class ShadowMapMasterRenderer
|
|||
private Matrix4f lightViewMatrix = new Matrix4f();
|
||||
private Matrix4f projectionViewMatrix = new Matrix4f();
|
||||
private Matrix4f offset = createOffset();
|
||||
|
||||
private ShadowMapEntityRenderer entityRenderer;
|
||||
|
||||
/** Creates instances of the important objects needed for rendering the scene
|
||||
|
@ -89,7 +90,7 @@ public class ShadowMapMasterRenderer
|
|||
*
|
||||
* @return The to-shadow-map-space matrix. */
|
||||
public Matrix4f getToShadowMapSpaceMatrix()
|
||||
{ return Matrix4f.mul(offset, projectionViewMatrix, null); }
|
||||
{ return projectionViewMatrix.mul(offset); }
|
||||
|
||||
/** Prepare for the shadow render pass. This first updates the dimensions of
|
||||
* the orthographic "view cuboid" based on the information that was
|
||||
|
@ -113,7 +114,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);
|
||||
|
@ -157,16 +158,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
|
||||
|
@ -181,10 +181,10 @@ 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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.github.hydos.ginger.engine.shadow;
|
||||
|
||||
import com.github.hydos.ginger.engine.math.matrixes.Matrix4f;
|
||||
import org.joml.Matrix4f;
|
||||
|
||||
import com.github.hydos.ginger.engine.render.shaders.ShaderProgram;
|
||||
|
||||
public class ShadowShader extends ShaderProgram
|
||||
|
|
|
@ -1,161 +0,0 @@
|
|||
package com.github.hydos.ginger.engine.terrain;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import com.github.hydos.ginger.engine.math.Maths;
|
||||
import com.github.hydos.ginger.engine.math.vectors.*;
|
||||
import com.github.hydos.ginger.engine.render.models.RawModel;
|
||||
import com.github.hydos.ginger.engine.utils.Loader;
|
||||
import com.github.hydos.ginger.main.settings.Constants;
|
||||
/*
|
||||
* wtf
|
||||
*/
|
||||
@Deprecated
|
||||
public class Terrain
|
||||
{
|
||||
private static final float MAX_PIXEL_COLOUR = 256 * 256 * 256;
|
||||
private float[][] heights;
|
||||
private float x, z;
|
||||
private RawModel model;
|
||||
private TerrainTexturePack texturePack;
|
||||
private TerrainTexture blendMap;
|
||||
|
||||
public Terrain(float gridX, float gridZ, TerrainTexturePack texturePack, TerrainTexture blendMap, String heightMapLocation)
|
||||
{
|
||||
this.texturePack = texturePack;
|
||||
this.blendMap = blendMap;
|
||||
this.x = gridX * Constants.terrainSize;
|
||||
this.z = gridZ * Constants.terrainSize;
|
||||
this.model = generateTerrain(heightMapLocation);
|
||||
}
|
||||
|
||||
private Vector3f calculateNormal(int x, int z, BufferedImage image)
|
||||
{
|
||||
float heightL = getHeight(x - 1, z, image);
|
||||
float heightR = getHeight(x + 1, z, image);
|
||||
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();
|
||||
return normal;
|
||||
}
|
||||
|
||||
private RawModel generateTerrain(String heightMap)
|
||||
{
|
||||
BufferedImage image = null;
|
||||
try
|
||||
{
|
||||
image = ImageIO.read(Terrain.class.getResourceAsStream("/textures/terrain/" + heightMap));
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
int VERTEX_COUNT = image.getHeight();
|
||||
heights = new float[VERTEX_COUNT][VERTEX_COUNT];
|
||||
int count = VERTEX_COUNT * VERTEX_COUNT;
|
||||
float[] vertices = new float[count * 3];
|
||||
float[] normals = new float[count * 3];
|
||||
float[] textureCoords = new float[count * 2];
|
||||
int[] indices = new int[6 * (VERTEX_COUNT - 1) * (VERTEX_COUNT - 1)];
|
||||
int vertexPointer = 0;
|
||||
for (int i = 0; i < VERTEX_COUNT; i++)
|
||||
{
|
||||
for (int j = 0; j < VERTEX_COUNT; j++)
|
||||
{
|
||||
vertices[vertexPointer * 3] = j / ((float) VERTEX_COUNT - 1) * Constants.terrainSize;
|
||||
float height = getHeight(j, i, image);
|
||||
heights[j][i] = height;
|
||||
vertices[vertexPointer * 3 + 1] = height;
|
||||
vertices[vertexPointer * 3 + 2] = i / ((float) VERTEX_COUNT - 1) * Constants.terrainSize;
|
||||
Vector3f normal = calculateNormal(j, i, image);
|
||||
normals[vertexPointer * 3] = normal.x;
|
||||
normals[vertexPointer * 3 + 1] = normal.y;
|
||||
normals[vertexPointer * 3 + 2] = normal.z;
|
||||
textureCoords[vertexPointer * 2] = j / ((float) VERTEX_COUNT - 1);
|
||||
textureCoords[vertexPointer * 2 + 1] = i / ((float) VERTEX_COUNT - 1);
|
||||
vertexPointer++;
|
||||
}
|
||||
}
|
||||
int pointer = 0;
|
||||
for (int gz = 0; gz < VERTEX_COUNT - 1; gz++)
|
||||
{
|
||||
for (int gx = 0; gx < VERTEX_COUNT - 1; gx++)
|
||||
{
|
||||
int topLeft = (gz * VERTEX_COUNT) + gx;
|
||||
int topRight = topLeft + 1;
|
||||
int bottomLeft = ((gz + 1) * VERTEX_COUNT) + gx;
|
||||
int bottomRight = bottomLeft + 1;
|
||||
indices[pointer++] = topLeft;
|
||||
indices[pointer++] = bottomLeft;
|
||||
indices[pointer++] = topRight;
|
||||
indices[pointer++] = topRight;
|
||||
indices[pointer++] = bottomLeft;
|
||||
indices[pointer++] = bottomRight;
|
||||
}
|
||||
}
|
||||
return Loader.loadToVAO(vertices, indices, normals, textureCoords);
|
||||
}
|
||||
|
||||
public TerrainTexture getBlendMap()
|
||||
{ return blendMap; }
|
||||
|
||||
private float getHeight(int x, int z, BufferedImage image)
|
||||
{
|
||||
if (x < 0 || x >= image.getHeight() || z < 0 || z >= image.getHeight())
|
||||
{ return 0; }
|
||||
float height = image.getRGB(x, z);
|
||||
height += MAX_PIXEL_COLOUR / 2f;
|
||||
height /= MAX_PIXEL_COLOUR / 2f;
|
||||
height *= Constants.terrainMaxHeight;
|
||||
return height;
|
||||
}
|
||||
|
||||
public float getHeightOfTerrain(float worldX, float worldZ)
|
||||
{
|
||||
float terrainX = worldX - this.x;
|
||||
float terrainZ = worldZ - this.z;
|
||||
float gridSquareSize = Constants.terrainSize / ((float) heights.length - 1);
|
||||
int gridX = (int) Math.floor(terrainX / gridSquareSize);
|
||||
int gridZ = (int) Math.floor(terrainZ / gridSquareSize);
|
||||
if (gridX >= heights.length - 1 || gridZ >= heights.length - 1 || gridX < 0 || gridZ < 0)
|
||||
{ return 0; }
|
||||
float xCoord = (terrainX % gridSquareSize) / gridSquareSize;
|
||||
float zCoord = (terrainZ % gridSquareSize) / gridSquareSize;
|
||||
float answer;
|
||||
if (xCoord <= (1 - zCoord))
|
||||
{
|
||||
answer = Maths
|
||||
.barryCentric(new Vector3f(0, heights[gridX][gridZ], 0), new Vector3f(1,
|
||||
heights[gridX + 1][gridZ], 0),
|
||||
new Vector3f(0,
|
||||
heights[gridX][gridZ + 1], 1),
|
||||
new Vector2f(xCoord, zCoord));
|
||||
}
|
||||
else
|
||||
{
|
||||
answer = Maths
|
||||
.barryCentric(new Vector3f(1, heights[gridX + 1][gridZ], 0), new Vector3f(1,
|
||||
heights[gridX + 1][gridZ + 1], 1),
|
||||
new Vector3f(0,
|
||||
heights[gridX][gridZ + 1], 1),
|
||||
new Vector2f(xCoord, zCoord));
|
||||
}
|
||||
return answer;
|
||||
}
|
||||
|
||||
public RawModel getModel()
|
||||
{ return model; }
|
||||
|
||||
public TerrainTexturePack getTexturePack()
|
||||
{ return texturePack; }
|
||||
|
||||
public float getX()
|
||||
{ return x; }
|
||||
|
||||
public float getZ()
|
||||
{ return z; }
|
||||
}
|
|
@ -96,9 +96,7 @@ public class Loader
|
|||
{ return new TerrainTexture(new ModelTexture("terrain/" + string).getTextureID()); }
|
||||
|
||||
public static int loadTexture(String path)
|
||||
{
|
||||
return loadTextureDirectly("/textures/" + path);
|
||||
}
|
||||
{ return loadTextureDirectly("/textures/" + path); }
|
||||
|
||||
public static int loadTextureDirectly(String path)
|
||||
{
|
||||
|
@ -107,9 +105,12 @@ public class Loader
|
|||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);
|
||||
GL11.glTexParameterf(GL11.GL_TEXTURE_2D, 10241, 9729.0f);
|
||||
GL11.glTexParameterf(GL11.GL_TEXTURE_2D, 10240, 9729.0f);
|
||||
if(texture.getComp().get() == 3) {
|
||||
if (texture.getComp().get() == 3)
|
||||
{
|
||||
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, texture.getWidth(), texture.getHeight(), 0, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, texture.getImage());
|
||||
}else {
|
||||
}
|
||||
else
|
||||
{
|
||||
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, texture.getWidth(), texture.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, texture.getImage());
|
||||
}
|
||||
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
|
||||
|
|
|
@ -5,7 +5,7 @@ import org.joml.Vector3f;
|
|||
public class Constants
|
||||
{
|
||||
//player variables
|
||||
public static Vector3f gravity = new Vector3f(0,0,0);
|
||||
public static Vector3f gravity = new Vector3f(0, 0, 0);
|
||||
public static float jumpPower = 0;
|
||||
public static float turnSpeed = 0;
|
||||
public static double movementSpeed = 1;
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
package com.github.hydos.ginger.voxelUtils;
|
||||
|
||||
public class BlockMesher {
|
||||
|
||||
public class BlockMesher
|
||||
{
|
||||
}
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
package com.github.hydos.multiThreading;
|
||||
|
||||
public abstract class GingerThread extends Thread{
|
||||
|
||||
public abstract class GingerThread extends Thread
|
||||
{
|
||||
public boolean isRunning;
|
||||
|
||||
public String threadName;
|
||||
|
||||
}
|
||||
|
|
|
@ -2,16 +2,13 @@ package com.github.hydos.multiThreading;
|
|||
|
||||
import java.util.*;
|
||||
|
||||
public class GingerThreading {
|
||||
|
||||
public class GingerThreading
|
||||
{
|
||||
public List<GingerThread> threads;
|
||||
|
||||
public GingerThreading() {
|
||||
threads = new ArrayList<GingerThread>();
|
||||
}
|
||||
|
||||
public void registerThread(GingerThread thread) {
|
||||
threads.add(thread);
|
||||
}
|
||||
|
||||
|
||||
public GingerThreading()
|
||||
{ threads = new ArrayList<GingerThread>(); }
|
||||
|
||||
public void registerThread(GingerThread thread)
|
||||
{ threads.add(thread); }
|
||||
}
|
||||
|
|
|
@ -6,8 +6,8 @@ package tk.valoeghese.gateways.client.io;
|
|||
public final class InitialPressHandler implements KeyListener
|
||||
{
|
||||
private boolean activatedPreviously = false;
|
||||
|
||||
private final KeyCallback callback;
|
||||
|
||||
public InitialPressHandler(KeyCallback callback)
|
||||
{ this.callback = callback; }
|
||||
|
||||
|
|
|
@ -16,6 +16,9 @@ public class Input
|
|||
public static void addPressCallback(Keybind key, KeyCallback callback)
|
||||
{ CALLBACKS.computeIfAbsent(key, listener -> new ArrayList<>()).add(callback); }
|
||||
|
||||
public static void addInitialPressCallback(Keybind key, KeyCallback callback)
|
||||
{ addListener(key, new InitialPressHandler(callback)); }
|
||||
|
||||
public static void invokeAllListeners()
|
||||
{
|
||||
CALLBACKS.forEach((keybind, listeners) ->
|
||||
|
|
|
@ -8,7 +8,6 @@ import org.lwjgl.glfw.*;
|
|||
public class KeyCallbackHandler extends GLFWKeyCallback
|
||||
{
|
||||
private static final KeyCallbackHandler INSTANCE = new KeyCallbackHandler();
|
||||
|
||||
public static boolean[] keys = new boolean[GLFW.GLFW_KEY_LAST];
|
||||
|
||||
public static void trackWindow(long window)
|
||||
|
|
|
@ -5,22 +5,35 @@ import org.lwjgl.glfw.GLFW;
|
|||
/*
|
||||
* Author: Valoeghese
|
||||
*/
|
||||
public final class Keybind
|
||||
public enum Keybind
|
||||
{
|
||||
public static final Keybind MOVE_UP = new Keybind(GLFW.GLFW_KEY_W, false);
|
||||
public static final Keybind MOVE_DOWN = new Keybind(GLFW.GLFW_KEY_S, false);
|
||||
public static final Keybind MOVE_LEFT = new Keybind(GLFW.GLFW_KEY_A, false);
|
||||
public static final Keybind MOVE_RIGHT = new Keybind(GLFW.GLFW_KEY_D, false);
|
||||
public static final Keybind USE = new Keybind(GLFW.GLFW_MOUSE_BUTTON_1, true);
|
||||
public static final Keybind SELECT_0 = new Keybind(GLFW.GLFW_KEY_1, false);
|
||||
public static final Keybind SELECT_1 = new Keybind(GLFW.GLFW_KEY_2, false);
|
||||
public static final Keybind SELECT_2 = new Keybind(GLFW.GLFW_KEY_3, false);
|
||||
public static final Keybind EXIT = new Keybind(GLFW.GLFW_KEY_ESCAPE, false);
|
||||
public static final Keybind FULLSCREEN = new Keybind(GLFW.GLFW_KEY_F11, false);
|
||||
MOVE_FORWARD(GLFW.GLFW_KEY_W, false), // Move the player forward relative to its facing direction
|
||||
MOVE_BACKWARD(GLFW.GLFW_KEY_S, false), // Move the player backward relative to its facing direction
|
||||
STRAFE_LEFT(GLFW.GLFW_KEY_A, false), // Move the player left relative to its facing direction
|
||||
STRAFE_RIGHT(GLFW.GLFW_KEY_D, false), // Move the player right relative to its facing direction
|
||||
FLY_UP(GLFW.GLFW_KEY_SPACE, false), // Move the player upward
|
||||
FLY_DOWN(GLFW.GLFW_KEY_LEFT_SHIFT, false), // Move the player downward
|
||||
BREAK(GLFW.GLFW_MOUSE_BUTTON_1, true), // Place a block in front of the player
|
||||
PLACE(GLFW.GLFW_MOUSE_BUTTON_2, true), // Break the block in front of the player
|
||||
SLOT_1(GLFW.GLFW_KEY_1, false), // Select the first item slot in the toolbar
|
||||
SLOT_2(GLFW.GLFW_KEY_2, false), // Select the second item slot in the toolbar
|
||||
SLOT_3(GLFW.GLFW_KEY_3, false), // Select the third item slot in the toolbar
|
||||
SLOT_4(GLFW.GLFW_KEY_4, false), // Select the fourth item slot in the toolbar
|
||||
SLOT_5(GLFW.GLFW_KEY_5, false), // Select the fifth item slot in the toolbar
|
||||
SLOT_6(GLFW.GLFW_KEY_6, false), // Select the sixth item slot in the toolbar
|
||||
SLOT_7(GLFW.GLFW_KEY_7, false), // Select the seventh item slot in the toolbar
|
||||
SLOT_8(GLFW.GLFW_KEY_8, false), // Select the eighth item slot in the toolbar
|
||||
SLOT_9(GLFW.GLFW_KEY_9, false), // Select the ninth item slot in the toolbar
|
||||
SLOT_10(GLFW.GLFW_KEY_0, false), // Select the tenth item slot in the toolbar
|
||||
EXIT(GLFW.GLFW_KEY_ESCAPE, false), // Save and exit the game // (Open the pause menu later)
|
||||
DEBUG(GLFW.GLFW_KEY_F3, false), // Toggle debug text onscreen
|
||||
FULLSCREEN(GLFW.GLFW_KEY_F11, false), // Toggle fullscreen mode
|
||||
WIREFRAME(GLFW.GLFW_KEY_TAB, false); // Toggle wireframe
|
||||
|
||||
public int value;
|
||||
public boolean mouse;
|
||||
|
||||
public Keybind(int initValue, boolean isMouse)
|
||||
Keybind(int initValue, boolean isMouse)
|
||||
{
|
||||
this.value = initValue;
|
||||
this.mouse = isMouse;
|
||||
|
|
|
@ -8,7 +8,6 @@ import org.lwjgl.glfw.*;
|
|||
public class MouseCallbackHandler extends GLFWMouseButtonCallback
|
||||
{
|
||||
private static final MouseCallbackHandler INSTANCE = new MouseCallbackHandler();
|
||||
|
||||
public static boolean[] buttons = new boolean[GLFW.GLFW_MOUSE_BUTTON_LAST];
|
||||
|
||||
public static void trackWindow(long window)
|
||||
|
|
|
@ -28,11 +28,8 @@ public class DataSection implements Iterable<Object>
|
|||
public <T extends Enum<?>> T readEnum(int index, T[] values)
|
||||
{
|
||||
Integer i = (Integer) this.data.get(index);
|
||||
|
||||
if (i == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (i == null)
|
||||
{ return null; }
|
||||
return values[i];
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue