├── README.md ├── src ├── util │ ├── Text.java │ ├── TextManager.java │ └── Access.java ├── launcher │ └── Launcher.java ├── engine │ ├── Keyboard.java │ ├── Syncer.java │ └── Engine.java ├── world │ ├── Stars.java │ ├── World.java │ └── Camera.java └── vertexBuffer │ └── VBO.java ├── bin ├── util │ ├── Text.class │ ├── Access.class │ ├── TextManager.class │ └── Access$GenericMethod.class ├── engine │ ├── Engine.class │ ├── Syncer.class │ ├── Keyboard.class │ └── Engine$TimeAction.class ├── world │ ├── Camera.class │ ├── Stars.class │ ├── World.class │ └── Stars$Star.class ├── launcher │ └── Launcher.class └── vertexBuffer │ └── VBO.class └── ver.2 ├── bin ├── tex │ └── tx.jpg ├── gui │ └── GUI.class ├── util │ ├── VBO.class │ ├── Frustum.class │ ├── Syncer.class │ ├── text │ │ ├── Text.class │ │ ├── ShapeRenderer.class │ │ └── Text_DEPRECATED.class │ └── TextureLoader.class ├── font │ ├── mono │ │ └── mono.ttf │ └── silk │ │ ├── slkscr.ttf │ │ ├── slkscrb.ttf │ │ ├── slkscre.ttf │ │ └── slkscreb.ttf ├── physics │ └── AABB.class ├── world │ ├── Blocks.class │ ├── Camera.class │ ├── Stars.class │ ├── World.class │ ├── Stars$Star.class │ └── chunk │ │ ├── Block.class │ │ └── Chunk.class ├── engine │ ├── Engine.class │ ├── Ticker.class │ ├── Renderer.class │ ├── Tesselator.class │ └── Engine$TimeAction.class ├── input │ └── Keyboard.class └── launcher │ └── Launcher.class ├── res ├── tex │ ├── tx.jpg │ └── default.png └── font │ ├── mono │ └── mono.ttf │ └── silk │ ├── slkscr.ttf │ ├── slkscrb.ttf │ ├── slkscre.ttf │ └── slkscreb.ttf ├── libs ├── lwjgl-natives-windows.jar ├── lwjgl-glfw-natives-windows.jar └── lwjgl-opengl-natives-windows.jar └── src ├── engine ├── Ticker.java ├── Renderer.java ├── Tesselator.java └── Engine.java ├── input └── Keyboard.java ├── physics └── AABB.java ├── world ├── chunk │ ├── Block.java │ └── Chunk.java ├── Stars.java ├── World.java ├── Blocks.java └── Camera.java ├── launcher └── Launcher.java ├── util ├── Syncer.java ├── text │ ├── ShapeRenderer.java │ ├── Text_DEPRECATED.java │ └── Text.java ├── TextureLoader.java ├── VBO.java └── Frustum.java └── gui └── GUI.java /README.md: -------------------------------------------------------------------------------- 1 | # Minecraft-LWJGL-3 2 | My attempt to recreate minecraft 3 | -------------------------------------------------------------------------------- /src/util/Text.java: -------------------------------------------------------------------------------- 1 | package util; 2 | 3 | public class Text { 4 | 5 | 6 | } 7 | -------------------------------------------------------------------------------- /bin/util/Text.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/bin/util/Text.class -------------------------------------------------------------------------------- /ver.2/bin/tex/tx.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/bin/tex/tx.jpg -------------------------------------------------------------------------------- /ver.2/res/tex/tx.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/res/tex/tx.jpg -------------------------------------------------------------------------------- /bin/engine/Engine.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/bin/engine/Engine.class -------------------------------------------------------------------------------- /bin/engine/Syncer.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/bin/engine/Syncer.class -------------------------------------------------------------------------------- /bin/util/Access.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/bin/util/Access.class -------------------------------------------------------------------------------- /bin/world/Camera.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/bin/world/Camera.class -------------------------------------------------------------------------------- /bin/world/Stars.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/bin/world/Stars.class -------------------------------------------------------------------------------- /bin/world/World.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/bin/world/World.class -------------------------------------------------------------------------------- /ver.2/bin/gui/GUI.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/bin/gui/GUI.class -------------------------------------------------------------------------------- /bin/engine/Keyboard.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/bin/engine/Keyboard.class -------------------------------------------------------------------------------- /ver.2/bin/util/VBO.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/bin/util/VBO.class -------------------------------------------------------------------------------- /ver.2/res/tex/default.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/res/tex/default.png -------------------------------------------------------------------------------- /bin/launcher/Launcher.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/bin/launcher/Launcher.class -------------------------------------------------------------------------------- /bin/util/TextManager.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/bin/util/TextManager.class -------------------------------------------------------------------------------- /bin/vertexBuffer/VBO.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/bin/vertexBuffer/VBO.class -------------------------------------------------------------------------------- /bin/world/Stars$Star.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/bin/world/Stars$Star.class -------------------------------------------------------------------------------- /ver.2/bin/font/mono/mono.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/bin/font/mono/mono.ttf -------------------------------------------------------------------------------- /ver.2/bin/physics/AABB.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/bin/physics/AABB.class -------------------------------------------------------------------------------- /ver.2/bin/util/Frustum.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/bin/util/Frustum.class -------------------------------------------------------------------------------- /ver.2/bin/util/Syncer.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/bin/util/Syncer.class -------------------------------------------------------------------------------- /ver.2/bin/world/Blocks.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/bin/world/Blocks.class -------------------------------------------------------------------------------- /ver.2/bin/world/Camera.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/bin/world/Camera.class -------------------------------------------------------------------------------- /ver.2/bin/world/Stars.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/bin/world/Stars.class -------------------------------------------------------------------------------- /ver.2/bin/world/World.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/bin/world/World.class -------------------------------------------------------------------------------- /ver.2/res/font/mono/mono.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/res/font/mono/mono.ttf -------------------------------------------------------------------------------- /ver.2/bin/engine/Engine.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/bin/engine/Engine.class -------------------------------------------------------------------------------- /ver.2/bin/engine/Ticker.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/bin/engine/Ticker.class -------------------------------------------------------------------------------- /ver.2/bin/font/silk/slkscr.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/bin/font/silk/slkscr.ttf -------------------------------------------------------------------------------- /ver.2/bin/input/Keyboard.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/bin/input/Keyboard.class -------------------------------------------------------------------------------- /ver.2/bin/util/text/Text.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/bin/util/text/Text.class -------------------------------------------------------------------------------- /ver.2/res/font/silk/slkscr.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/res/font/silk/slkscr.ttf -------------------------------------------------------------------------------- /ver.2/bin/engine/Renderer.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/bin/engine/Renderer.class -------------------------------------------------------------------------------- /ver.2/bin/engine/Tesselator.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/bin/engine/Tesselator.class -------------------------------------------------------------------------------- /ver.2/bin/font/silk/slkscrb.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/bin/font/silk/slkscrb.ttf -------------------------------------------------------------------------------- /ver.2/bin/font/silk/slkscre.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/bin/font/silk/slkscre.ttf -------------------------------------------------------------------------------- /ver.2/bin/font/silk/slkscreb.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/bin/font/silk/slkscreb.ttf -------------------------------------------------------------------------------- /ver.2/bin/launcher/Launcher.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/bin/launcher/Launcher.class -------------------------------------------------------------------------------- /ver.2/bin/world/Stars$Star.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/bin/world/Stars$Star.class -------------------------------------------------------------------------------- /ver.2/bin/world/chunk/Block.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/bin/world/chunk/Block.class -------------------------------------------------------------------------------- /ver.2/bin/world/chunk/Chunk.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/bin/world/chunk/Chunk.class -------------------------------------------------------------------------------- /ver.2/res/font/silk/slkscrb.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/res/font/silk/slkscrb.ttf -------------------------------------------------------------------------------- /ver.2/res/font/silk/slkscre.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/res/font/silk/slkscre.ttf -------------------------------------------------------------------------------- /ver.2/res/font/silk/slkscreb.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/res/font/silk/slkscreb.ttf -------------------------------------------------------------------------------- /bin/engine/Engine$TimeAction.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/bin/engine/Engine$TimeAction.class -------------------------------------------------------------------------------- /bin/util/Access$GenericMethod.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/bin/util/Access$GenericMethod.class -------------------------------------------------------------------------------- /ver.2/bin/util/TextureLoader.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/bin/util/TextureLoader.class -------------------------------------------------------------------------------- /ver.2/libs/lwjgl-natives-windows.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/libs/lwjgl-natives-windows.jar -------------------------------------------------------------------------------- /ver.2/bin/engine/Engine$TimeAction.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/bin/engine/Engine$TimeAction.class -------------------------------------------------------------------------------- /ver.2/bin/util/text/ShapeRenderer.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/bin/util/text/ShapeRenderer.class -------------------------------------------------------------------------------- /ver.2/bin/util/text/Text_DEPRECATED.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/bin/util/text/Text_DEPRECATED.class -------------------------------------------------------------------------------- /ver.2/libs/lwjgl-glfw-natives-windows.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/libs/lwjgl-glfw-natives-windows.jar -------------------------------------------------------------------------------- /ver.2/libs/lwjgl-opengl-natives-windows.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiftoo/Minecraft-LWJGL-3/master/ver.2/libs/lwjgl-opengl-natives-windows.jar -------------------------------------------------------------------------------- /src/util/TextManager.java: -------------------------------------------------------------------------------- 1 | package util; 2 | 3 | import org.newdawn.slick.TrueTypeFont; 4 | 5 | public class TextManager { 6 | 7 | 8 | } -------------------------------------------------------------------------------- /ver.2/src/engine/Ticker.java: -------------------------------------------------------------------------------- 1 | package engine; 2 | 3 | import world.World; 4 | 5 | public class Ticker { 6 | 7 | public static void load() { 8 | 9 | } 10 | 11 | public static void tick() { 12 | World.update(); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/launcher/Launcher.java: -------------------------------------------------------------------------------- 1 | package launcher; 2 | 3 | import engine.Engine; 4 | import util.Access; 5 | 6 | public class Launcher { 7 | 8 | public static void main(String[] args) { 9 | Access.drop(args, "_arguments"); 10 | Access.drop(new Engine(1280, 720, "My Game", false), "engine"); 11 | } 12 | } -------------------------------------------------------------------------------- /src/engine/Keyboard.java: -------------------------------------------------------------------------------- 1 | package engine; 2 | 3 | import org.lwjgl.glfw.GLFW; 4 | import util.Access; 5 | 6 | public class Keyboard { 7 | 8 | public static boolean isKeyDown(int key) 9 | { 10 | return GLFW.GLFW_PRESS == GLFW.glfwGetKey(Access.pull("window"), key); 11 | } 12 | 13 | public static boolean isKeyUp(int key) 14 | { 15 | return GLFW.GLFW_RELEASE == GLFW.glfwGetKey(Access.pull("window"), key); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /ver.2/src/input/Keyboard.java: -------------------------------------------------------------------------------- 1 | package input; 2 | 3 | import org.lwjgl.glfw.GLFW; 4 | 5 | import engine.Engine; 6 | 7 | public class Keyboard { 8 | 9 | public static boolean isKeyDown(int key) 10 | { 11 | return GLFW.GLFW_PRESS == GLFW.glfwGetKey(Engine.window, key); 12 | } 13 | 14 | public static boolean isKeyUp(int key) 15 | { 16 | return GLFW.GLFW_RELEASE == GLFW.glfwGetKey(Engine.window, key); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /ver.2/src/physics/AABB.java: -------------------------------------------------------------------------------- 1 | package physics; 2 | 3 | public class AABB { 4 | 5 | public float x0, x1; 6 | public float y0, y1; 7 | public float z0, z1; 8 | 9 | public AABB(float x0, float y0, float z0, float x1, float y1, float z1) { 10 | this.x0 = x0; 11 | this.y0 = y0; 12 | this.z0 = z0; 13 | this.x1 = x1; 14 | this.y1 = y1; 15 | this.z1 = z1; 16 | } 17 | 18 | @Override 19 | public String toString() { 20 | return x0 + ":" + y0 + ":" + z0 + " " + x1 + ":" + y1 + ":" + z1; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /ver.2/src/world/chunk/Block.java: -------------------------------------------------------------------------------- 1 | package world.chunk; 2 | 3 | public class Block { 4 | 5 | public static Block AIR; 6 | public static Block GRASS; 7 | public static Block DIRT; 8 | public static Block ROCK; 9 | 10 | public final int id; 11 | public final boolean opaque; 12 | 13 | public Block(int id, boolean opaque) { 14 | this.id = id; 15 | this.opaque = opaque; 16 | } 17 | 18 | public static void createBlocks() { 19 | AIR = new Block(0, false); 20 | GRASS = new Block(1, true); 21 | DIRT = new Block(2, true); 22 | ROCK = new Block(3, true); 23 | } 24 | 25 | @Override 26 | public String toString() { 27 | return "Block:id="+id; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /ver.2/src/engine/Renderer.java: -------------------------------------------------------------------------------- 1 | package engine; 2 | 3 | import gui.GUI; 4 | import util.Syncer; 5 | import util.text.Text; 6 | import world.Camera; 7 | import world.World; 8 | 9 | public class Renderer { 10 | 11 | public static void load() throws Exception { 12 | Camera.create(); 13 | 14 | World.load(); 15 | 16 | Text.load("c:/users/user/desktop/default.png"); 17 | 18 | GUI.load(); 19 | 20 | last_tick = Syncer.getTime(); 21 | } 22 | 23 | public static void render() { 24 | //World.applyFog(); 25 | World.render(); 26 | //World.removeFog(); 27 | 28 | GUI.render(); 29 | } 30 | 31 | static double ready = 0; 32 | static long now_tick; 33 | static long last_tick; 34 | 35 | public static void applyCamera() { 36 | now_tick = Syncer.getTime(); 37 | ready += (now_tick - last_tick) / (double)Syncer.sleepTime; 38 | last_tick = now_tick; 39 | 40 | if(ready >= 1) { 41 | Camera.acceptInput(Syncer.getDeltaCeil()); 42 | Camera.apply(); 43 | ready--; 44 | } 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/util/Access.java: -------------------------------------------------------------------------------- 1 | package util; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | import java.util.HashMap; 8 | 9 | public class Access { 10 | 11 | private static final HashMap map; 12 | static { 13 | map = new HashMap<>(); 14 | } 15 | 16 | 17 | 18 | @GenericMethod 19 | public static void drop(TYPE value, String name) { 20 | map.put(name, value); 21 | } 22 | 23 | @GenericMethod 24 | public static void drop(TYPE value, Class name) { 25 | drop(value, name.toString()); 26 | } 27 | 28 | @SuppressWarnings("unchecked") 29 | @GenericMethod 30 | public static TYPE pull(String name) { 31 | if (!map.containsKey(name)) 32 | throw new ClassCastException("There is no such key as: " + name); 33 | else { 34 | try { 35 | return (TYPE) map.get(name); 36 | } catch (ClassCastException cce) { 37 | System.err.println("Invalid cast!,"); 38 | cce.printStackTrace(); 39 | } 40 | } 41 | return null; 42 | } 43 | 44 | 45 | @Retention(RetentionPolicy.SOURCE) 46 | @Target(ElementType.METHOD) 47 | private @interface GenericMethod {} 48 | } 49 | -------------------------------------------------------------------------------- /src/engine/Syncer.java: -------------------------------------------------------------------------------- 1 | package engine; 2 | 3 | import static org.lwjgl.glfw.GLFW.glfwGetTime; 4 | 5 | public class Syncer { 6 | public static float DELTA = 0; 7 | 8 | public static int fps; 9 | 10 | public static long sleepTime; 11 | 12 | public static long variableYieldTime; 13 | public static long lastTime; 14 | 15 | public static void syncer(int desiredFps) { 16 | fps = desiredFps; 17 | sleepTime = 1000000000 / fps; 18 | } 19 | public static void sync() { 20 | final long yieldTime = Math.min(sleepTime, variableYieldTime + sleepTime % (1000*1000)); 21 | long overSleep = 0; 22 | 23 | try { 24 | while (true) { 25 | 26 | long t = getTime() - lastTime; 27 | 28 | if (t < sleepTime - yieldTime) { 29 | Thread.sleep(1); 30 | } 31 | else if (t < sleepTime) { 32 | Thread.yield(); 33 | } 34 | else { 35 | overSleep = t - sleepTime; 36 | break; 37 | } 38 | } 39 | } catch (InterruptedException e) {} 40 | 41 | lastTime = getTime() - Math.min(overSleep, sleepTime); 42 | 43 | if (overSleep > variableYieldTime) { 44 | variableYieldTime = Math.min(variableYieldTime + 200*1000, sleepTime); 45 | } 46 | else if (overSleep < variableYieldTime - 200*1000) { 47 | variableYieldTime = Math.max(variableYieldTime - 2*1000, 0); 48 | } 49 | } 50 | public static long getTime() { 51 | return (long)(glfwGetTime() * 1e9); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /ver.2/src/launcher/Launcher.java: -------------------------------------------------------------------------------- 1 | package launcher; 2 | 3 | import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; 4 | import java.lang.reflect.Field; 5 | import javax.swing.JOptionPane; 6 | import org.lwjgl.opengl.GL11; 7 | 8 | import engine.Engine; 9 | 10 | public class Launcher { 11 | public static String[] arguments; 12 | public static void main(String[] args) { 13 | 14 | arguments = args; 15 | try { 16 | new Engine(1280, 720, "My Game", false); 17 | } catch (Exception e) { 18 | JOptionPane.showMessageDialog(null, e, "Eroor", JOptionPane.ERROR_MESSAGE); 19 | e.printStackTrace(); 20 | } 21 | } 22 | 23 | public static String getGlValue(long val) { 24 | Class[] classes = { 25 | GL11.class 26 | }; 27 | 28 | int vali = (int) val; 29 | 30 | String ret = null; 31 | 32 | for (Class cl : classes) { 33 | for(Field f : cl.getDeclaredFields()) { 34 | try { 35 | Object o = f.get(null); 36 | Long v1 = -1l; Integer v2 = -1; 37 | 38 | if(o instanceof Long) { 39 | v1 = (Long)o; 40 | if(v1 == val) { 41 | ret = f.getName(); 42 | break; 43 | } 44 | } else if(o instanceof Integer) { 45 | v2 = (Integer)o; 46 | if(v2 == vali) { 47 | ret = f.getName(); 48 | break; 49 | } 50 | } 51 | 52 | } catch (Exception e) { System.out.println("Lmao how?");} 53 | } 54 | } 55 | 56 | return ret; 57 | } 58 | } -------------------------------------------------------------------------------- /src/world/Stars.java: -------------------------------------------------------------------------------- 1 | package world; 2 | 3 | import static org.lwjgl.opengl.GL11.*; 4 | import static org.lwjgl.opengl.GL15.*; 5 | import java.nio.DoubleBuffer; 6 | import java.nio.FloatBuffer; 7 | import java.util.Random; 8 | 9 | import org.lwjgl.BufferUtils; 10 | import org.lwjgl.opengl.GL11; 11 | import org.lwjgl.opengl.GL15; 12 | import org.lwjgl.opengl.GL20; 13 | import org.lwjgl.opengl.GL30; 14 | import org.lwjgl.opengl.GLUtil; 15 | import org.lwjgl.system.MemoryUtil; 16 | 17 | import util.Text; 18 | import vertexBuffer.VBO; 19 | 20 | public class Stars { 21 | public static boolean renderBuffer = false; 22 | 23 | private static Star[] stars; 24 | 25 | private static VBO starvbo; 26 | public static void generateNew(int amount) 27 | { 28 | stars = new Star[amount]; 29 | for (int i = 0; i < stars.length; i++) { 30 | stars[i] = new Star(); 31 | } 32 | DoubleBuffer buf = MemoryUtil.memAllocDouble(stars.length * 3); 33 | for (Star star : stars) { 34 | buf.put(star.x); 35 | buf.put(star.y); 36 | buf.put(star.z); 37 | } 38 | buf.flip(); 39 | starvbo = new VBO(buf, 3, GL_STATIC_DRAW, GL_POINTS); 40 | } 41 | 42 | public static void render() 43 | { 44 | glColor3f(0, 1f, 0); 45 | starvbo.draw(); 46 | } 47 | 48 | 49 | private static class Star 50 | { 51 | public final double x, y, z; 52 | private final byte r, g, b; 53 | private float size; 54 | public Star() { 55 | Random rnd = new Random(); 56 | x = (rnd.nextDouble() - 0.5) * 100; 57 | y = (rnd.nextDouble() - 0.5) * 100; 58 | z = rnd.nextInt(200) - 200d; 59 | 60 | byte[] rgb = new byte[3]; rnd.nextBytes(rgb); 61 | 62 | r = rgb[0]; 63 | g = rgb[1]; 64 | b = rgb[2]; 65 | 66 | size = 1f + (5f - 1f) * rnd.nextFloat(); 67 | } 68 | public void applyColor() 69 | { 70 | glColor3b(r, g, b); 71 | } 72 | public void applySize() 73 | { 74 | glPointSize(size); 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /ver.2/src/world/Stars.java: -------------------------------------------------------------------------------- 1 | package world; 2 | 3 | import static org.lwjgl.opengl.GL11.*; 4 | import static org.lwjgl.opengl.GL15.*; 5 | 6 | import java.nio.ByteBuffer; 7 | import java.nio.ByteOrder; 8 | import java.nio.DoubleBuffer; 9 | import java.nio.FloatBuffer; 10 | import java.util.Random; 11 | 12 | import org.lwjgl.BufferUtils; 13 | import org.lwjgl.opengl.GL11; 14 | import org.lwjgl.opengl.GL15; 15 | import org.lwjgl.opengl.GL20; 16 | import org.lwjgl.opengl.GL30; 17 | import org.lwjgl.opengl.GLUtil; 18 | import org.lwjgl.system.MemoryUtil; 19 | 20 | import util.VBO; 21 | import util.text.Text; 22 | 23 | public class Stars { 24 | public static boolean renderBuffer = false; 25 | 26 | private static Star[] stars; 27 | 28 | private static VBO starvbo; 29 | public static void generateNew(int amount) 30 | { 31 | stars = new Star[amount]; 32 | for (int i = 0; i < stars.length; i++) { 33 | stars[i] = new Star(); 34 | } 35 | DoubleBuffer buf = MemoryUtil.memAllocDouble(stars.length * 3); 36 | for (Star star : stars) { 37 | buf.put(star.x); 38 | buf.put(star.y); 39 | buf.put(star.z); 40 | } 41 | buf.flip(); 42 | starvbo = new VBO(buf, 3, GL_STATIC_DRAW, GL_POINTS); 43 | } 44 | 45 | public static void render() 46 | { 47 | glColor3f((1f / 255f) * 213f, (1f / 255f) * 213, (1f / 255f) * 154); 48 | starvbo.draw(); 49 | } 50 | 51 | 52 | private static class Star 53 | { 54 | public final double x, y, z; 55 | private final byte r, g, b; 56 | private float size; 57 | public Star() { 58 | Random rnd = new Random(); 59 | x = (rnd.nextDouble() - 0.5) * 100; 60 | y = (rnd.nextDouble() - 0.5) * 100; 61 | z = rnd.nextInt(200) - 200d; 62 | 63 | byte[] rgb = new byte[3]; rnd.nextBytes(rgb); 64 | 65 | r = rgb[0]; 66 | g = rgb[1]; 67 | b = rgb[2]; 68 | 69 | size = 1f + (5f - 1f) * rnd.nextFloat(); 70 | } 71 | public void applyColor() 72 | { 73 | glColor3b(r, g, b); 74 | } 75 | public void applySize() 76 | { 77 | glPointSize(size); 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/world/World.java: -------------------------------------------------------------------------------- 1 | package world; 2 | 3 | import static org.lwjgl.opengl.GL11.*; 4 | import static org.lwjgl.opengl.GL15.*; 5 | import java.nio.DoubleBuffer; 6 | import java.nio.FloatBuffer; 7 | import org.lwjgl.system.MemoryUtil; 8 | 9 | import engine.Syncer; 10 | import util.Text; 11 | import vertexBuffer.VBO; 12 | 13 | public class World { 14 | private static int vbo = -1; 15 | 16 | static { 17 | glPointSize(2.5f); 18 | glLineWidth(5f); 19 | 20 | Stars.generateNew(1000); 21 | Camera.create(); 22 | 23 | vbo = glGenBuffers(); 24 | double[] arr = 25 | { 26 | 0.5,0,-0.5, 27 | -0.5,0,-0.5, 28 | 0.5,0, 0.5, 29 | 0.5,0, 0.5, 30 | -0.5,0,-0.5, 31 | -0.5,0, 0.5, 32 | 33 | -0.5,0,-0.5, 34 | 0.5,0,-0.5, 35 | 36 | 0.5,0,-0.5, 37 | 0.5,0, 0.5, 38 | 39 | 0.5,0, 0.5, 40 | -0.5,0, 0.5, 41 | 42 | -0.5,0, 0.5, 43 | -0.5,0,-0.5, 44 | }; 45 | DoubleBuffer buffer = MemoryUtil.memAllocDouble(arr.length); 46 | buffer.put(arr); 47 | buffer.flip(); 48 | glBindBuffer(GL_ARRAY_BUFFER, vbo); 49 | glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW); 50 | glBindBuffer(GL_ARRAY_BUFFER, 0); 51 | } 52 | 53 | private static void renderPlane() 54 | { 55 | glColor3f(1f, 0.3f, 0.1f); 56 | glBindBuffer(GL_ARRAY_BUFFER, vbo); 57 | glVertexPointer(3, GL_DOUBLE, 0, 0); 58 | glDrawArrays(GL_TRIANGLES, 0, 6); 59 | glColor3f(0f, 1f, 0.5f); 60 | glDrawArrays(GL_LINES, 6, 8); 61 | glBindBuffer(GL_ARRAY_BUFFER,0); 62 | } 63 | 64 | private static void renderXZacrossYdown(int x, int y, int z) { 65 | glPushMatrix(); 66 | glScaled(5, 5, 5); 67 | for (int i = 0; i < x; i++) { 68 | for (int j = 0; j < z; j++) { 69 | glPushMatrix(); 70 | glTranslated(i, y, j); 71 | renderPlane(); 72 | glPopMatrix(); 73 | } 74 | } 75 | glPopMatrix(); 76 | } 77 | 78 | public static void render() 79 | { 80 | for (int i = -1; i > -16; i--) { 81 | renderXZacrossYdown(16, i, 16); 82 | } 83 | 84 | 85 | 86 | Camera.acceptInput(Syncer.DELTA); 87 | Camera.apply(); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /ver.2/src/util/Syncer.java: -------------------------------------------------------------------------------- 1 | package util; 2 | 3 | import static org.lwjgl.glfw.GLFW.glfwGetTime; 4 | 5 | public class Syncer { 6 | public static float DELTA = 0; 7 | public static float ACT_DELTA = 0; 8 | public static float AVERAGE_DELTA = 0; 9 | 10 | public static int desiredFps; 11 | 12 | public static long sleepTime; 13 | 14 | public static long variableYieldTime; 15 | public static long lastTime; 16 | 17 | public static void syncer(int desiredFps) { 18 | Syncer.desiredFps = desiredFps; 19 | measuredfps = 0; 20 | sleepTime = 1000000000 / Syncer.desiredFps; 21 | } 22 | public static void sync() { 23 | final long yieldTime = Math.min(sleepTime, variableYieldTime + sleepTime % (1000*1000)); 24 | long overSleep = 0; 25 | 26 | try { 27 | while (true) { 28 | 29 | long t = getTime() - lastTime; 30 | 31 | if (t < sleepTime - yieldTime) { 32 | Thread.sleep(1); 33 | } 34 | else if (t < sleepTime) { 35 | Thread.yield(); 36 | } 37 | else { 38 | overSleep = t - sleepTime; 39 | break; 40 | } 41 | } 42 | } catch (InterruptedException e) {} 43 | 44 | lastTime = getTime() - Math.min(overSleep, sleepTime); 45 | 46 | if (overSleep > variableYieldTime) { 47 | variableYieldTime = Math.min(variableYieldTime + 200*1000, sleepTime); 48 | } 49 | else if (overSleep < variableYieldTime - 200*1000) { 50 | variableYieldTime = Math.max(variableYieldTime - 2*1000, 0); 51 | } 52 | } 53 | public static long getTime() { 54 | return (long)(glfwGetTime() * 1e9); 55 | } 56 | 57 | 58 | 59 | private static int fpsframes = 0; 60 | private static double measuredfps; // First time 61 | 62 | private static int precision = 1; 63 | 64 | public static void setPrecision(int precision) { 65 | Syncer.precision = precision; 66 | } 67 | 68 | public static double measureFPS(double deltaa) { 69 | if(fpsframes < precision) { 70 | AVERAGE_DELTA += deltaa; 71 | fpsframes++; 72 | } else { 73 | measuredfps = 1d / (AVERAGE_DELTA / (double)precision); 74 | fpsframes = 0; 75 | AVERAGE_DELTA = 0; 76 | } 77 | return measuredfps; 78 | } 79 | 80 | 81 | public static double getFps() { 82 | return measuredfps; 83 | } 84 | public static float getDeltaCeil() { 85 | return DELTA; 86 | } 87 | public static float getDelta() { 88 | return ACT_DELTA; 89 | } 90 | public static float getDeltaAvg() { 91 | return AVERAGE_DELTA; 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /ver.2/src/gui/GUI.java: -------------------------------------------------------------------------------- 1 | package gui; 2 | 3 | import static org.lwjgl.opengl.GL11.GL_COMPILE; 4 | import static org.lwjgl.opengl.GL11.GL_CULL_FACE; 5 | import static org.lwjgl.opengl.GL11.GL_DEPTH_TEST; 6 | import static org.lwjgl.opengl.GL11.GL_MODELVIEW; 7 | import static org.lwjgl.opengl.GL11.GL_PROJECTION; 8 | import static org.lwjgl.opengl.GL11.glCallList; 9 | import static org.lwjgl.opengl.GL11.glDisable; 10 | import static org.lwjgl.opengl.GL11.glEnable; 11 | import static org.lwjgl.opengl.GL11.glEnd; 12 | import static org.lwjgl.opengl.GL11.glEndList; 13 | import static org.lwjgl.opengl.GL11.glGenLists; 14 | import static org.lwjgl.opengl.GL11.glLoadIdentity; 15 | import static org.lwjgl.opengl.GL11.glMatrixMode; 16 | import static org.lwjgl.opengl.GL11.glNewList; 17 | import static org.lwjgl.opengl.GL11.glOrtho; 18 | import static org.lwjgl.opengl.GL11.glPopMatrix; 19 | import static org.lwjgl.opengl.GL11.glPushMatrix; 20 | 21 | import engine.Engine; 22 | import engine.Tesselator; 23 | import util.Syncer; 24 | import util.text.Text; 25 | import world.Camera; 26 | import world.World; 27 | 28 | public class GUI { 29 | 30 | private static int make2dlist, make3dlist; 31 | 32 | public static void load() { 33 | make2dlist = glGenLists(2); 34 | make3dlist = make2dlist+1; 35 | 36 | glNewList(make2dlist, GL_COMPILE); 37 | { 38 | glMatrixMode(GL_PROJECTION); 39 | glPushMatrix(); 40 | glLoadIdentity(); 41 | 42 | glOrtho(0f, Engine.width, Engine.height, 0f, -1, 1); 43 | 44 | glMatrixMode(GL_MODELVIEW); 45 | glPushMatrix(); 46 | glLoadIdentity(); 47 | 48 | glDisable(GL_DEPTH_TEST); 49 | } 50 | glEndList(); 51 | 52 | glNewList(make3dlist, GL_COMPILE); 53 | { 54 | glMatrixMode(GL_PROJECTION); 55 | glPopMatrix(); 56 | glMatrixMode(GL_MODELVIEW); 57 | glPopMatrix(); 58 | 59 | glEnable(GL_DEPTH_TEST); 60 | } 61 | glEndList(); 62 | } 63 | 64 | 65 | public static void render() { 66 | make2d(); 67 | 68 | renderDebug(); 69 | 70 | make3d(); 71 | } 72 | 73 | private static void renderDebug() { 74 | Text.render(4, 2, 2, String.format("x : %.1f", Camera.getX())); 75 | Text.render(4, 12, 2, String.format("y : %.1f", Camera.getY())); 76 | Text.render(4, 22, 2, String.format("z : %.1f ", Camera.getZ())); 77 | Text.render(4, 32, 2, String.format("rt: %s", Camera.getDir())); 78 | Text.render(4, 42, 2, String.format("Delta: F:%.2f, DT:%.2f", Syncer.getDeltaCeil(), Syncer.ACT_DELTA)); 79 | Text.render(4, 52, 2, String.format("FPS: %.1f", Syncer.getFps())); 80 | Text.render(4, 62, 2, Tesselator.getMemoryUsage()); 81 | Text.render(4, 72, 2, "Chunk updates: " + World.getChunkUpdates()); 82 | } 83 | 84 | 85 | public static void make2d() { 86 | glCallList(make2dlist); 87 | } 88 | public static void make3d() { 89 | glCallList(make3dlist); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /ver.2/src/engine/Tesselator.java: -------------------------------------------------------------------------------- 1 | package engine; 2 | 3 | import static org.lwjgl.opengl.GL11.GL_FLOAT; 4 | import static org.lwjgl.opengl.GL11.GL_QUADS; 5 | import static org.lwjgl.opengl.GL11.GL_TRIANGLES; 6 | 7 | import java.nio.FloatBuffer; 8 | import java.util.function.DoubleBinaryOperator; 9 | 10 | import org.lwjgl.BufferUtils; 11 | import org.lwjgl.opengl.GL11; 12 | import org.lwjgl.system.MemoryUtil; 13 | 14 | public class Tesselator { 15 | 16 | private static final int MAX_VERTICES = 524288; 17 | private FloatBuffer vertexBuffer = BufferUtils.createFloatBuffer(524288); 18 | private FloatBuffer texCoordBuffer = BufferUtils.createFloatBuffer(262144); 19 | private FloatBuffer colorBuffer = BufferUtils.createFloatBuffer(524288); 20 | private int vertices = 0; 21 | 22 | private int usedVertices = 0; 23 | private int usedColors = 0; 24 | private int usedTxcoords = 0; 25 | 26 | private float u; 27 | private float v; 28 | private float r; 29 | private float g; 30 | private float b; 31 | private boolean hasColor = false; 32 | private boolean hasTexture = false; 33 | 34 | public static final Tesselator get = new Tesselator(); 35 | 36 | public void flush() { 37 | this.vertexBuffer.flip(); 38 | this.texCoordBuffer.flip(); 39 | this.colorBuffer.flip(); 40 | 41 | GL11.glVertexPointer(3, GL_FLOAT, 0, this.vertexBuffer); 42 | if (this.hasTexture) { 43 | GL11.glTexCoordPointer(2, GL_FLOAT, 0, this.texCoordBuffer); 44 | } 45 | 46 | if (this.hasColor) { 47 | GL11.glColorPointer(3, GL_FLOAT, 0, this.colorBuffer); 48 | } 49 | 50 | GL11.glDrawArrays(GL_QUADS, 0, this.vertices); 51 | 52 | this.clear(); 53 | } 54 | 55 | private void clear() { 56 | this.vertices = 0; 57 | 58 | this.vertexBuffer.clear(); 59 | this.texCoordBuffer.clear(); 60 | this.colorBuffer.clear(); 61 | } 62 | 63 | public void init() { 64 | this.clear(); 65 | this.hasColor = false; 66 | this.hasTexture = false; 67 | } 68 | 69 | public void tex(float u, float v) { 70 | this.hasTexture = true; 71 | this.u = u; 72 | this.v = v; 73 | } 74 | 75 | public void color(float r, float g, float b) { 76 | this.hasColor = true; 77 | this.r = r; 78 | this.g = g; 79 | this.b = b; 80 | } 81 | 82 | public void vertex(float x, float y, float z) { 83 | this.vertexBuffer.put(this.vertices * 3 + 0, x).put(this.vertices * 3 + 1, y).put(this.vertices * 3 + 2, z); 84 | if (this.hasTexture) { 85 | this.texCoordBuffer.put(this.vertices * 2 + 0, this.u).put(this.vertices * 2 + 1, this.v); 86 | usedTxcoords+=3; 87 | } 88 | 89 | if (this.hasColor) { 90 | this.colorBuffer.put(this.vertices * 3 + 0, this.r).put(this.vertices * 3 + 1, this.g) 91 | .put(this.vertices * 3 + 2, this.b); 92 | usedColors+=3; 93 | } 94 | 95 | ++this.vertices; 96 | usedVertices+=3; 97 | 98 | if (this.vertices == MAX_VERTICES) { 99 | this.flush(); 100 | } 101 | } 102 | 103 | public static String getMemoryUsage() { 104 | Runtime r = Runtime.getRuntime(); 105 | return (String.format("Memory usage: %.1f Mb" ,(get.usedVertices+get.usedColors+get.usedTxcoords + r.totalMemory() - r.freeMemory()) / 1024f / 1024f)); 106 | } 107 | } -------------------------------------------------------------------------------- /ver.2/src/world/World.java: -------------------------------------------------------------------------------- 1 | package world; 2 | 3 | import static org.lwjgl.opengl.GL11.GL_EXP2; 4 | import static org.lwjgl.opengl.GL11.GL_FOG; 5 | import static org.lwjgl.opengl.GL11.GL_FOG_COLOR; 6 | import static org.lwjgl.opengl.GL11.GL_FOG_DENSITY; 7 | import static org.lwjgl.opengl.GL11.GL_FOG_END; 8 | import static org.lwjgl.opengl.GL11.GL_FOG_MODE; 9 | import static org.lwjgl.opengl.GL11.GL_FOG_START; 10 | import static org.lwjgl.opengl.GL11.glDisable; 11 | import static org.lwjgl.opengl.GL11.glEnable; 12 | import static org.lwjgl.opengl.GL11.glFogf; 13 | import static org.lwjgl.opengl.GL11.glFogfv; 14 | import static org.lwjgl.opengl.GL11.glFogi; 15 | import static org.lwjgl.opengl.GL11.glLineWidth; 16 | import static org.lwjgl.opengl.GL11.*; 17 | 18 | import java.nio.ByteBuffer; 19 | import java.nio.FloatBuffer; 20 | 21 | import org.lwjgl.BufferUtils; 22 | import org.lwjgl.glfw.GLFW; 23 | import org.lwjgl.system.MemoryUtil; 24 | 25 | import input.Keyboard; 26 | import util.Frustum; 27 | import util.text.Text; 28 | import world.chunk.Block; 29 | import world.chunk.Chunk; 30 | 31 | public class World { 32 | private static FloatBuffer fogColor; 33 | 34 | private static Chunk[][] chunks; 35 | 36 | public static int chunkUpdates = 0; 37 | 38 | private static final int WORLD_X_SIZE = 16, WORLD_Z_SIZE = 16; 39 | 40 | public static void load() { 41 | glPointSize(2.5f); 42 | glLineWidth(5f); 43 | 44 | fogColor = BufferUtils.createFloatBuffer(4); 45 | 46 | int col = 920330; 47 | fogColor.put(new float[]{(float)(col >> 16 & 255) / 255.0F, (float)(col >> 8 & 255) / 255.0F, (float)(col & 255) / 255.0F, 1.0F}); 48 | fogColor.flip(); 49 | 50 | chunks = new Chunk[WORLD_X_SIZE][WORLD_Z_SIZE]; 51 | for (int i = 0; i < WORLD_X_SIZE; i++) { 52 | for (int j = 0; j < WORLD_Z_SIZE; j++) { 53 | chunks[i][j] = new Chunk(i, j); 54 | } 55 | } 56 | } 57 | 58 | public static Block getBlockWorld(int x, int y, int z) { 59 | int xChunk = Math.floorDiv(x , 16); 60 | int zChunk = Math.floorDiv(z , 16); 61 | 62 | if(xChunk < 0 || xChunk >= WORLD_X_SIZE || y < 0 || zChunk < 0 || zChunk >= WORLD_Z_SIZE) return Block.AIR; 63 | 64 | return chunks[xChunk][zChunk].getBlock(x - 16 * xChunk, y, z - 16 * zChunk); 65 | } 66 | public static void setBlockWorld(int x, int y, int z, Block block) { 67 | int xChunk = Math.floorDiv(x , 16); 68 | int zChunk = Math.floorDiv(z , 16); 69 | 70 | if(xChunk < 0 || xChunk >= WORLD_X_SIZE || y < 0 || zChunk < 0 || zChunk >= WORLD_Z_SIZE) return; 71 | 72 | chunks[xChunk][zChunk].setBlock(x - 16 * xChunk, y, z - 16 * zChunk, block).setDirty(); 73 | 74 | } 75 | 76 | public static void render() 77 | { 78 | Frustum f = Frustum.getFrustum(); 79 | 80 | for (int i = 0; i < WORLD_X_SIZE; i++) { 81 | for (int j = 0; j < WORLD_Z_SIZE; j++) { 82 | chunks[i][j].render(f); 83 | } 84 | } 85 | } 86 | 87 | public static void update() { 88 | for (int i = 0; i < WORLD_X_SIZE; i++) { 89 | for (int j = 0; j < WORLD_Z_SIZE; j++) { 90 | if(chunks[i][j].isDirty()) { 91 | chunks[i][j].rebuild(); 92 | } 93 | } 94 | } 95 | if(Keyboard.isKeyDown(GLFW.GLFW_KEY_0)) { 96 | setBlockWorld(1, 16, 1, Block.AIR); 97 | setBlockWorld(1, 15, 1, Block.AIR); 98 | } 99 | } 100 | 101 | public static int getChunkUpdates() { 102 | return chunkUpdates; 103 | } 104 | 105 | public static void applyFog() { 106 | glEnable(GL_FOG); 107 | glFogi(GL_FOG_MODE, GL_EXP2); 108 | glFogf(GL_FOG_START, 1f); 109 | glFogf(GL_FOG_END, 100f); 110 | glFogf(GL_FOG_DENSITY, 0.001F); 111 | glFogfv(GL_FOG_COLOR, fogColor); 112 | } 113 | 114 | public static void removeFog() { 115 | glDisable(GL_FOG); 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /ver.2/src/util/text/ShapeRenderer.java: -------------------------------------------------------------------------------- 1 | package util.text; 2 | 3 | import static org.lwjgl.opengl.GL11.*; 4 | 5 | import java.nio.FloatBuffer; 6 | 7 | import org.lwjgl.BufferUtils; 8 | import org.lwjgl.opengl.GL11; 9 | 10 | public final class ShapeRenderer { 11 | 12 | private FloatBuffer buffer = BufferUtils.createFloatBuffer(524288); 13 | private float[] data = new float[524288]; 14 | private int vertices = 0; 15 | private float u; 16 | private float v; 17 | private float r; 18 | private float g; 19 | private float b; 20 | private boolean color = false; 21 | private boolean texture = false; 22 | private int vertexLength = 3; 23 | private int length = 0; 24 | private boolean noColor = false; 25 | public static ShapeRenderer instance = new ShapeRenderer(); 26 | 27 | public final void end() { 28 | if (this.vertices > 0) { 29 | this.buffer.clear(); 30 | this.buffer.put(this.data, 0, this.length); 31 | this.buffer.flip(); 32 | if (this.texture && this.color) { 33 | GL11.glInterleavedArrays(GL_T2F_C3F_V3F, 0, this.buffer); 34 | } else if (this.texture) { 35 | GL11.glInterleavedArrays(GL_T2F_V3F, 0, this.buffer); 36 | } else if (this.color) { 37 | GL11.glInterleavedArrays(GL_C3F_V3F, 0, this.buffer); 38 | } else { 39 | GL11.glInterleavedArrays(GL_V3F, 0, this.buffer); 40 | } 41 | 42 | GL11.glDrawArrays(GL_QUADS, 0, this.vertices); 43 | } 44 | 45 | this.clear(); 46 | } 47 | 48 | private void clear() { 49 | this.vertices = 0; 50 | this.buffer.clear(); 51 | this.length = 0; 52 | } 53 | 54 | public final void begin() { 55 | this.clear(); 56 | this.color = false; 57 | this.texture = false; 58 | this.noColor = false; 59 | } 60 | 61 | public final void color(float var1, float var2, float var3) { 62 | if (!this.noColor) { 63 | if (!this.color) { 64 | this.vertexLength += 3; 65 | } 66 | 67 | this.color = true; 68 | this.r = var1; 69 | this.g = var2; 70 | this.b = var3; 71 | } 72 | } 73 | 74 | public final void vertexUV(float x, float y, float z, float u, float v) { 75 | if (!this.texture) { 76 | this.vertexLength += 2; 77 | } 78 | this.texture = true; 79 | this.u = u; 80 | this.v = v; 81 | this.vertex(x, y, z); 82 | } 83 | 84 | public final void vertex(float x, float y, float z) { 85 | if (this.texture) { 86 | this.data[this.length++] = this.u; 87 | this.data[this.length++] = this.v; 88 | } 89 | 90 | if (this.color) { 91 | this.data[this.length++] = this.r; 92 | this.data[this.length++] = this.g; 93 | this.data[this.length++] = this.b; 94 | } 95 | 96 | this.data[this.length++] = x; 97 | this.data[this.length++] = y; 98 | this.data[this.length++] = z; 99 | ++this.vertices; 100 | if (this.vertices % 4 == 0 && this.length >= 524288 - (this.vertexLength << 2)) { 101 | this.end(); 102 | } 103 | 104 | } 105 | 106 | public final void color(int var1) { 107 | int var2 = var1 >> 16 & 255; 108 | int var3 = var1 >> 8 & 255; 109 | var1 &= 255; 110 | int var10001 = var2; 111 | int var10002 = var3; 112 | var3 = var1; 113 | var2 = var10002; 114 | var1 = var10001; 115 | byte var7 = (byte) var1; 116 | byte var4 = (byte) var2; 117 | byte var8 = (byte) var3; 118 | byte var6 = var4; 119 | byte var5 = var7; 120 | if (!this.noColor) { 121 | if (!this.color) { 122 | this.vertexLength += 3; 123 | } 124 | 125 | this.color = true; 126 | this.r = (float) (var5 & 255) / 255.0F; 127 | this.g = (float) (var6 & 255) / 255.0F; 128 | this.b = (float) (var8 & 255) / 255.0F; 129 | } 130 | 131 | } 132 | 133 | public final void noColor() { 134 | this.noColor = true; 135 | } 136 | 137 | public final void normal(float var1, float var2, float var3) { 138 | GL11.glNormal3f(var1, var2, var3); 139 | } 140 | 141 | } -------------------------------------------------------------------------------- /ver.2/src/util/text/Text_DEPRECATED.java: -------------------------------------------------------------------------------- 1 | package util.text; 2 | 3 | import static org.lwjgl.opengl.GL11.*; 4 | import java.awt.Color; 5 | import java.awt.Font; 6 | import java.awt.FontFormatException; 7 | import java.awt.FontMetrics; 8 | import java.awt.Graphics2D; 9 | import java.awt.RenderingHints; 10 | import java.awt.font.TextAttribute; 11 | import java.awt.image.BufferedImage; 12 | import java.io.IOException; 13 | import java.util.HashMap; 14 | import java.util.Map; 15 | import org.lwjgl.opengl.GL11; 16 | 17 | import util.TextureLoader; 18 | 19 | public class Text_DEPRECATED { 20 | 21 | private static int[] ascii_lists = new int[255]; 22 | 23 | private static final int FONT_SAMPLES = 16; 24 | 25 | public static Font mono; 26 | 27 | public static String base; 28 | static { 29 | StringBuilder b = new StringBuilder(255); 30 | for (int i = 32; i < 127; i++) { 31 | b.append((char)i); 32 | } 33 | base = b.toString(); 34 | 35 | Map att = new HashMap<>(); 36 | att.put(TextAttribute.SIZE, FONT_SAMPLES); 37 | att.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_EXTRA_LIGHT); 38 | // att.put(TextAttribute.WIDTH, TextAttribute.WIDTH_CONDENSED); 39 | try { 40 | mono = Font.createFont(Font.TRUETYPE_FONT, Text_DEPRECATED.class.getResourceAsStream("/font/mono/mono.ttf")).deriveFont(att); 41 | } catch (Exception e) { 42 | } 43 | } 44 | 45 | private static void genList(BufferedImage img, int aval) { 46 | int texture = TextureLoader.loadTexture(img, GL_RGBA, false); 47 | 48 | int list = glGenLists(1); 49 | 50 | glNewList(list, GL_COMPILE); 51 | { 52 | glBindTexture(GL_TEXTURE_2D, texture); 53 | glBegin(GL_QUADS); 54 | { 55 | glTexCoord2f(0, 0); 56 | glVertex2f(0, 0); 57 | 58 | glTexCoord2f(1, 0); 59 | glVertex2f(img.getWidth(), 0); 60 | 61 | glTexCoord2f(1, 1); 62 | glVertex2f(img.getWidth(), img.getHeight()); 63 | 64 | glTexCoord2f(0, 1); 65 | glVertex2f(0, img.getHeight()); 66 | } 67 | glEnd(); 68 | glTranslated(img.getWidth(), 0, 0); 69 | glBindTexture(GL_TEXTURE_2D, 0); 70 | } 71 | glEndList(); 72 | ascii_lists[aval] = list; 73 | } 74 | 75 | public static void load() throws Exception { 76 | char[] chars = new char[base.length()]; 77 | base.getChars(0, base.length(), chars, 0); 78 | 79 | Graphics2D g2 = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB).createGraphics(); 80 | FontMetrics m = g2.getFontMetrics(mono); 81 | 82 | for (char c : chars) { 83 | int ascii_value = (int)c; 84 | 85 | String ch = Character.toString(c); 86 | 87 | BufferedImage img = new BufferedImage(m.stringWidth(ch), m.getAscent() + m.getDescent()+ 3, BufferedImage.TYPE_INT_ARGB); 88 | Graphics2D g = img.createGraphics(); 89 | 90 | g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); 91 | g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR); 92 | 93 | g.setFont(mono); 94 | g.drawString(ch, img.getWidth()-m.stringWidth(ch), m.getAscent()+2); 95 | g.dispose(); 96 | 97 | genList(img, ascii_value); 98 | } 99 | } 100 | 101 | private static final HashMap string_lists = new HashMap<>(16, 0.9f); 102 | 103 | public static void drawString(float x, float y, float size, float r, float g, float b, boolean saveAsList, String what) { 104 | Integer thelist; 105 | 106 | if(saveAsList) { 107 | if((thelist = string_lists.get(what)) != null) { 108 | glCallList(thelist); 109 | } else { 110 | int newlist = glGenLists(1); 111 | 112 | glNewList(newlist, GL_COMPILE_AND_EXECUTE); 113 | { 114 | draw_internal(x, y, size, r, g, b, what); 115 | } 116 | glEndList(); 117 | 118 | string_lists.put(what, newlist); 119 | } 120 | } else { 121 | draw_internal(x, y, size, r, g, b, what); 122 | } 123 | } 124 | 125 | private static void draw_internal(float x, float y, float size, float r, float g, float b, String what) { 126 | glEnable(GL11.GL_BLEND); 127 | glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); 128 | 129 | glPushMatrix(); 130 | glTranslated(x, y, 0); 131 | //glScaled(size / (double)FONT_SAMPLES, size / (double)FONT_SAMPLES, 0); 132 | glColor3f(r, g, b); 133 | for (int i = 0; i < what.length(); i++) { 134 | glCallList(ascii_lists[what.charAt(i)]); 135 | } 136 | glPopMatrix(); 137 | 138 | glDisable(GL11.GL_BLEND); 139 | } 140 | 141 | public static void drawString(float x, float y, float size, String what) { 142 | drawString(x, y, size, 1f, 1f, 1f, false, what); 143 | } 144 | 145 | } -------------------------------------------------------------------------------- /ver.2/src/world/chunk/Chunk.java: -------------------------------------------------------------------------------- 1 | package world.chunk; 2 | 3 | import static org.lwjgl.opengl.GL11.GL_COMPILE; 4 | import static org.lwjgl.opengl.GL11.GL_COMPILE_AND_EXECUTE; 5 | import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; 6 | import static org.lwjgl.opengl.GL11.glCallList; 7 | import static org.lwjgl.opengl.GL11.glColor3f; 8 | import static org.lwjgl.opengl.GL11.glDisable; 9 | import static org.lwjgl.opengl.GL11.glEnable; 10 | import static org.lwjgl.opengl.GL11.glEndList; 11 | import static org.lwjgl.opengl.GL11.glGenLists; 12 | import static org.lwjgl.opengl.GL11.glLoadIdentity; 13 | import static org.lwjgl.opengl.GL11.glNewList; 14 | import static org.lwjgl.opengl.GL11.glPopMatrix; 15 | import static org.lwjgl.opengl.GL11.glPushMatrix; 16 | import static org.lwjgl.opengl.GL11.glTranslated; 17 | import static org.lwjgl.opengl.GL11.glTranslatef; 18 | 19 | import java.nio.IntBuffer; 20 | 21 | import engine.Tesselator; 22 | import physics.AABB; 23 | import util.Frustum; 24 | import world.Blocks; 25 | import world.Camera; 26 | import world.World; 27 | 28 | public class Chunk { 29 | public static final int CHUNK_DIM = 16, CHUNK_HEIGHT = 16; 30 | 31 | private Block[] blocks; 32 | private boolean dirty = true; 33 | private final int posX, posZ; 34 | 35 | private AABB aabb; 36 | 37 | private int glList; 38 | 39 | public Chunk(int pX, int pZ) { 40 | Block[][][] _blocks = new Block[CHUNK_HEIGHT][CHUNK_DIM][CHUNK_DIM]; 41 | 42 | for (int y = 0; y < CHUNK_HEIGHT; y++) { 43 | for (int x = 0; x < CHUNK_DIM; x++) { 44 | for (int z = 0; z < CHUNK_DIM; z++) { 45 | 46 | _blocks[y][x][z] = Block.GRASS; 47 | 48 | } 49 | } 50 | } 51 | blocks = new Block[CHUNK_DIM * CHUNK_DIM * CHUNK_HEIGHT]; 52 | 53 | for (int x = 0; x < CHUNK_HEIGHT; x++) { 54 | for (int y = 0; y < CHUNK_DIM; y++) { 55 | for (int z = 0; z < CHUNK_DIM; z++) { 56 | blocks[z + (x * z) + (x * y * z)] = _blocks[y][x][z]; 57 | } 58 | } 59 | } 60 | posX = pX; 61 | posZ = pZ; 62 | 63 | aabb = new AABB(pX * 16, 0, pZ * 16, pX * 16 + 16, CHUNK_HEIGHT, pZ * 16 + 16); 64 | 65 | glList = glGenLists(1); 66 | } 67 | 68 | public void render(Frustum f) { 69 | if(f.cubeInFrustum(aabb) || isPlayerOn()) { 70 | glCallList(glList); 71 | } 72 | } 73 | 74 | public Block getBlock(int x, int y, int z) { 75 | if ((x >= CHUNK_DIM || x < 0) || (y >= CHUNK_HEIGHT || y < 0) || (z >= CHUNK_DIM || z < 0)) 76 | return Block.AIR; 77 | else 78 | return blocks[z + (x * z) + (x * y * z)]; 79 | } 80 | 81 | public Chunk setBlock(int x, int y, int z, Block block) { 82 | if ((x >= CHUNK_DIM || x < 0) || (y >= CHUNK_HEIGHT || y < 0) || (z >= CHUNK_DIM || z < 0)) 83 | return this; 84 | else { 85 | blocks[z + (x * z) + (x * y * z)] = block; 86 | return this; 87 | } 88 | } 89 | 90 | public void rebuild() { 91 | World.chunkUpdates++; 92 | 93 | glNewList(glList, GL_COMPILE); 94 | 95 | glPushMatrix(); 96 | 97 | glTranslated(posX * CHUNK_DIM, 0, posZ * CHUNK_DIM); 98 | 99 | Tesselator.get.init(); 100 | 101 | for (int y = 0; y < CHUNK_HEIGHT; y++) { 102 | for (int x = 0; x < CHUNK_DIM; x++) { 103 | for (int z = 0; z < CHUNK_DIM; z++) { 104 | 105 | boolean up = !World.getBlockWorld(posX * 16 + x, y + 1, posZ * 16 + z).opaque; 106 | boolean down = !World.getBlockWorld(posX * 16 + x, y - 1, posZ * 16 + z).opaque; 107 | boolean left = !World.getBlockWorld(posX * 16 + x + 1, y, posZ * 16 + z).opaque; 108 | boolean right = !World.getBlockWorld(posX * 16 + x - 1, y, posZ * 16 + z).opaque; 109 | boolean fw = !World.getBlockWorld(posX * 16 + x, y, posZ * 16 + z - 1).opaque; 110 | boolean bw = !World.getBlockWorld(posX * 16 + x, y, posZ * 16 + z + 1).opaque; 111 | 112 | Blocks.render(getBlock(x, y, z), up, down, left, right, fw, bw, x, y, z); 113 | // Blocks.render(getBlock(x, y, z), true, true, true, true, true, true, x, y, z); 114 | } 115 | } 116 | } 117 | 118 | Tesselator.get.flush(); 119 | 120 | glPopMatrix(); 121 | 122 | glEndList(); 123 | 124 | dirty = false; 125 | } 126 | 127 | public boolean isPlayerOn() { 128 | /*double x = Camera.getX(); 129 | double z = Camera.getZ(); 130 | if(x >= posX*16 && x <= posX*16+16 && z >= posZ*16 && z <= posZ*16+16) 131 | return true; 132 | return false;*/ 133 | return false; 134 | } 135 | 136 | public void setDirty() { 137 | this.dirty = true; 138 | } 139 | public boolean isDirty() { 140 | return dirty; 141 | } 142 | 143 | public void setGlList(int glList) { 144 | this.glList = glList; 145 | } 146 | 147 | public int getGlList() { 148 | return glList; 149 | } 150 | 151 | public AABB getAabb() { 152 | return aabb; 153 | } 154 | 155 | } 156 | -------------------------------------------------------------------------------- /ver.2/src/util/TextureLoader.java: -------------------------------------------------------------------------------- 1 | package util; 2 | 3 | import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; 4 | import static org.lwjgl.opengl.GL11.GL_UNSIGNED_BYTE; 5 | import static org.lwjgl.opengl.GL11.glBindTexture; 6 | import static org.lwjgl.opengl.GL11.*; 7 | import static org.lwjgl.opengl.GL12.*; 8 | import static org.lwjgl.opengl.GL11.glTexParameteri; 9 | 10 | import java.awt.image.BufferedImage; 11 | import java.io.File; 12 | import java.nio.ByteBuffer; 13 | 14 | import javax.imageio.ImageIO; 15 | import javax.swing.ImageIcon; 16 | import javax.swing.JLabel; 17 | import javax.swing.JOptionPane; 18 | 19 | import org.lwjgl.opengl.ARBInternalformatQuery2; 20 | import org.lwjgl.opengl.GL11; 21 | import org.lwjgl.opengl.GL12; 22 | import org.lwjgl.opengl.GL14; 23 | import org.lwjgl.opengl.GL30; 24 | import org.lwjgl.opengl.GL33; 25 | import org.lwjgl.opengl.GL41; 26 | import org.lwjgl.opengl.GL43; 27 | import org.lwjgl.system.MemoryUtil; 28 | 29 | public class TextureLoader { 30 | 31 | private static final int DIMENSIONS = 16; 32 | 33 | public static int grasstop = -1; 34 | public static int grassside = -1; 35 | public static int dirt = -1; 36 | 37 | private static BufferedImage sheet; 38 | static { 39 | try { 40 | sheet = ImageIO.read(TextureLoader.class.getResourceAsStream("/tex/tx.jpg")); 41 | } catch (Exception e) { 42 | e.printStackTrace(); 43 | } 44 | } 45 | 46 | private static int loadTexture(int col, int row) { 47 | try { 48 | BufferedImage image = sheet.getSubimage(row * DIMENSIONS, col * DIMENSIONS, DIMENSIONS, DIMENSIONS); 49 | 50 | int[] pixels = new int[image.getWidth() * image.getHeight()]; 51 | image.getRGB(0, 0, DIMENSIONS, DIMENSIONS, pixels, 0, DIMENSIONS); 52 | 53 | ByteBuffer buffer = MemoryUtil.memAlloc(DIMENSIONS * DIMENSIONS * 4); // 4 for RGBA, 3 for RGB 54 | 55 | for (int i = 0; i < pixels.length; ++i) { 56 | int a = pixels[i] >> 24 & 255; 57 | int r = pixels[i] >> 16 & 255; 58 | int g = pixels[i] >> 8 & 255; 59 | int b = pixels[i] & 255; 60 | pixels[i] = a << 24 | r << 16 | g << 8 | b; 61 | } 62 | 63 | buffer.asIntBuffer().put(pixels); 64 | buffer.flip(); 65 | 66 | int texture = glGenTextures(); 67 | glBindTexture(GL_TEXTURE_2D, texture); 68 | 69 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, DIMENSIONS, DIMENSIONS, 0, GL_BGRA, GL_UNSIGNED_BYTE, pixels); 70 | 71 | GL30.glGenerateMipmap(GL_TEXTURE_2D); 72 | 73 | glTexParameteri(GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR_MIPMAP_LINEAR); 74 | glTexParameteri(GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL_LINEAR); 75 | glTexParameteri(GL_TEXTURE_2D, GL12.GL_TEXTURE_MAX_LEVEL, 4); 76 | 77 | glBindTexture(GL_TEXTURE_2D, 0); 78 | 79 | return texture; 80 | } 81 | catch(Exception e) { 82 | e.printStackTrace(); 83 | return -1; 84 | } 85 | } 86 | 87 | public static int loadTexture(BufferedImage image, int format, boolean mipmap) { 88 | try { 89 | int[] pixels = new int[image.getWidth() * image.getHeight()]; 90 | image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth()); 91 | 92 | ByteBuffer buffer = MemoryUtil.memAlloc(image.getWidth() * image.getHeight() * 4); // 4 for RGBA, 3 for RGB 93 | 94 | for (int i = 0; i < pixels.length; ++i) { 95 | int a = pixels[i] >> 24 & 255; 96 | int r = pixels[i] >> 16 & 255; 97 | int g = pixels[i] >> 8 & 255; 98 | int b = pixels[i] & 255; 99 | pixels[i] = a << 24 | b << 16 | g << 8 | r; 100 | } 101 | 102 | buffer.asIntBuffer().put(pixels); 103 | buffer.flip(); 104 | 105 | int texture = glGenTextures(); 106 | glBindTexture(GL_TEXTURE_2D, texture); 107 | 108 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 109 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mipmap ? GL_NEAREST_MIPMAP_NEAREST : GL_NEAREST); 110 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 111 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 112 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 113 | 114 | glTexImage2D(GL_TEXTURE_2D, 0, format, image.getWidth(), image.getHeight(), 0, format, GL_UNSIGNED_BYTE, pixels); 115 | 116 | if(mipmap) { 117 | GL30.glGenerateMipmap(GL_TEXTURE_2D); 118 | } 119 | 120 | glBindTexture(GL_TEXTURE_2D, 0); 121 | 122 | return texture; 123 | } 124 | catch(Exception e) { 125 | e.printStackTrace(); 126 | return -1; 127 | } 128 | } 129 | 130 | public static void loadGrassTop() { 131 | grasstop = loadTexture(0, 0); 132 | } 133 | public static void loadGrassSide() { 134 | grassside = loadTexture(0, 3); 135 | } 136 | public static void loadDirt() { 137 | dirt = loadTexture(0, 2); 138 | } 139 | 140 | } 141 | -------------------------------------------------------------------------------- /ver.2/src/util/text/Text.java: -------------------------------------------------------------------------------- 1 | package util.text; 2 | 3 | import static org.lwjgl.opengl.GL11.*; 4 | 5 | import java.awt.Color; 6 | import java.awt.image.BufferedImage; 7 | import java.io.File; 8 | import java.io.IOException; 9 | import javax.imageio.ImageIO; 10 | import org.lwjgl.opengl.GL11; 11 | 12 | import engine.Engine; 13 | import gui.GUI; 14 | import util.TextureLoader; 15 | 16 | public class Text { 17 | 18 | private static int[] widthmap = new int[256]; 19 | private static int fontTexture = -1; 20 | 21 | public static void load(String path) { 22 | BufferedImage img; 23 | try { 24 | img = ImageIO.read(new File(path)); 25 | } catch (IOException var13) { 26 | throw new RuntimeException(var13); 27 | } 28 | 29 | int imgW = img.getWidth(); 30 | int imgH = img.getHeight(); 31 | int[] rgbarr = new int[imgW * imgH]; 32 | img.getRGB(0, 0, imgW, imgH, rgbarr, 0, imgW); 33 | 34 | for (int i = 0; i < 128; ++i) { 35 | imgH = i % 16; 36 | int var7 = i / 16; 37 | int var8 = 0; 38 | 39 | for (boolean bool = false; var8 < 8 && !bool; ++var8) { 40 | int var10 = (imgH << 3) + var8; 41 | bool = true; 42 | 43 | for (int var11 = 0; var11 < 8 && bool; ++var11) { 44 | int var12 = ((var7 << 3) + var11) * imgW; 45 | if ((rgbarr[var10 + var12] & 255) > 128) { 46 | bool = false; 47 | } 48 | } 49 | } 50 | 51 | if (i == 32) { 52 | var8 = 4; 53 | } 54 | 55 | widthmap[i] = var8; 56 | } 57 | 58 | fontTexture = TextureLoader.loadTexture(img, GL_RGBA, false); 59 | } 60 | 61 | public static void render(int x, int y, double size, String text) { 62 | render(text, x + 1, y + 1, 0xffffff, size, true); 63 | renderNoShadow(x, y, size, 0xffffff, text); 64 | } 65 | 66 | public static void render(int x, int y, double size, int rgb, String text) { 67 | render(text, x + 1, y + 1, rgb, size, true); 68 | renderNoShadow(x, y, size, rgb, text); 69 | } 70 | 71 | public static void renderNoShadow(int x, int y, double size, int rgb, String text) { 72 | render(text, x, y, rgb, size, false); 73 | } 74 | 75 | private static void render(String text, int x, int y, int rgb, double size, boolean shadow) { 76 | if (text != null) { 77 | char[] characters = text.toCharArray(); 78 | if (shadow) { 79 | rgb = (rgb & 16579836) >> 2; 80 | } 81 | 82 | glPushMatrix(); 83 | 84 | glScaled(size, size, 0); 85 | 86 | glEnable(GL_BLEND); 87 | glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); 88 | 89 | GL11.glBindTexture(GL_TEXTURE_2D, fontTexture); 90 | ShapeRenderer var6 = ShapeRenderer.instance; 91 | ShapeRenderer.instance.begin(); 92 | var6.color(rgb); 93 | int var7 = 0; 94 | 95 | for (int i = 0; i < characters.length; ++i) { 96 | int var9; 97 | 98 | 99 | if (characters[i] == 38 && characters.length > i + 1) { 100 | if ((rgb = "0123456789abcde".indexOf(characters[i + 1])) < 0) { 101 | rgb = 16; 102 | } 103 | 104 | var9 = (rgb & 8) << 3; 105 | int var10 = (rgb & 1) * 191 + var9; 106 | int var11 = ((rgb & 2) >> 1) * 191 + var9; 107 | rgb = ((rgb & 4) >> 2) * 191 + var9; 108 | 109 | rgb = rgb << 16 | var11 << 8 | var10; 110 | i += 2; 111 | if (shadow) { 112 | rgb = (rgb & 16579836) >> 2; 113 | } 114 | 115 | var6.color(rgb); 116 | } 117 | 118 | 119 | rgb = characters[i] % 16 << 3; 120 | var9 = characters[i] / 16 << 3; 121 | float var13 = 7.99F; 122 | var6.vertexUV((float) (x + var7), (float) y + var13, 0.0F, (float) rgb / 128.0F, 123 | ((float) var9 + var13) / 128.0F); 124 | var6.vertexUV((float) (x + var7) + var13, (float) y + var13, 0.0F, ((float) rgb + var13) / 128.0F, 125 | ((float) var9 + var13) / 128.0F); 126 | var6.vertexUV((float) (x + var7) + var13, (float) y, 0.0F, ((float) rgb + var13) / 128.0F, 127 | (float) var9 / 128.0F); 128 | var6.vertexUV((float) (x + var7), (float) y, 0.0F, (float) rgb / 128.0F, (float) var9 / 128.0F); 129 | var7 += widthmap[characters[i]]; 130 | } 131 | 132 | var6.end(); 133 | 134 | glDisable(GL_BLEND); 135 | 136 | glPopMatrix(); 137 | } 138 | } 139 | 140 | public final int getWidth(String var1) { 141 | if (var1 == null) { 142 | return 0; 143 | } else { 144 | char[] var4 = var1.toCharArray(); 145 | int var2 = 0; 146 | 147 | for (int var3 = 0; var3 < var4.length; ++var3) { 148 | if (var4[var3] == 38) { 149 | ++var3; 150 | } else { 151 | var2 += this.widthmap[var4[var3]]; 152 | } 153 | } 154 | 155 | return var2; 156 | } 157 | } 158 | 159 | public static String stripColor(String var0) { 160 | char[] var3 = var0.toCharArray(); 161 | String var1 = ""; 162 | 163 | for (int var2 = 0; var2 < var3.length; ++var2) { 164 | if (var3[var2] == 38) { 165 | ++var2; 166 | } else { 167 | var1 = var1 + var3[var2]; 168 | } 169 | } 170 | 171 | return var1; 172 | } 173 | 174 | } -------------------------------------------------------------------------------- /src/vertexBuffer/VBO.java: -------------------------------------------------------------------------------- 1 | package vertexBuffer; 2 | 3 | import static org.lwjgl.opengl.GL11.GL_BYTE; 4 | import static org.lwjgl.opengl.GL11.GL_DOUBLE; 5 | import static org.lwjgl.opengl.GL11.GL_FLOAT; 6 | import static org.lwjgl.opengl.GL11.glDrawArrays; 7 | import static org.lwjgl.opengl.GL11.glVertexPointer; 8 | import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; 9 | import static org.lwjgl.opengl.GL15.glBindBuffer; 10 | import static org.lwjgl.opengl.GL15.glBufferData; 11 | import static org.lwjgl.opengl.GL15.glDeleteBuffers; 12 | import static org.lwjgl.opengl.GL15.glGenBuffers; 13 | import java.nio.Buffer; 14 | import java.nio.ByteBuffer; 15 | import java.nio.DoubleBuffer; 16 | import java.nio.FloatBuffer; 17 | import java.util.HashSet; 18 | 19 | import org.lwjgl.opengl.GL15; 20 | import org.lwjgl.system.MemoryUtil; 21 | 22 | public class VBO { 23 | 24 | private static final HashSet vboIndices = new HashSet<>(); 25 | 26 | 27 | private final int vboID; 28 | private boolean invalid = false; 29 | private boolean shouldmemfree = false; 30 | 31 | private final int datatype; 32 | private final int usage; 33 | private final int mode; 34 | private final int vertexcount; 35 | private final int vertexsize; 36 | 37 | private final Buffer dataPointer; 38 | 39 | 40 | // Array versions 41 | 42 | public VBO(float[] arr, int vertexsize, int usage, int mode) { 43 | shouldmemfree = true; 44 | 45 | FloatBuffer buf = MemoryUtil.memAllocFloat(arr.length); 46 | buf.put(arr); 47 | buf.flip(); 48 | 49 | this.dataPointer = buf; 50 | 51 | this.datatype = GL_FLOAT; 52 | this.usage = usage; 53 | this.mode = mode; 54 | 55 | this.vertexsize = vertexsize; 56 | this.vertexcount = arr.length / vertexsize; 57 | 58 | this.vboID = glGenBuffers(); 59 | vboIndices.add(vboID); 60 | 61 | feedF(buf); 62 | } 63 | public VBO(double[] arr, int vertexsize, int usage, int mode) { 64 | shouldmemfree = true; 65 | 66 | DoubleBuffer buf = MemoryUtil.memAllocDouble(arr.length); 67 | buf.put(arr); 68 | buf.flip(); 69 | 70 | this.dataPointer = buf; 71 | 72 | this.datatype = GL_DOUBLE; 73 | this.usage = usage; 74 | this.mode = mode; 75 | 76 | this.vertexsize = vertexsize; 77 | this.vertexcount = arr.length / vertexsize; 78 | 79 | this.vboID = glGenBuffers(); 80 | vboIndices.add(vboID); 81 | 82 | feedD(buf); 83 | } 84 | public VBO(byte[] arr, int vertexsize, int usage, int mode) { 85 | shouldmemfree = true; 86 | 87 | ByteBuffer buf = MemoryUtil.memAlloc(arr.length); 88 | buf.put(arr); 89 | buf.flip(); 90 | 91 | this.dataPointer = buf; 92 | 93 | this.datatype = GL_BYTE; 94 | this.usage = usage; 95 | this.mode = mode; 96 | 97 | this.vertexsize = vertexsize; 98 | this.vertexcount = arr.length / vertexsize; 99 | 100 | this.vboID = glGenBuffers(); 101 | vboIndices.add(vboID); 102 | 103 | feedB(buf); 104 | } 105 | 106 | // Buffer versions 107 | 108 | public VBO(FloatBuffer buf, int vertexsize, int usage, int mode) { 109 | this.dataPointer = buf; 110 | 111 | this.datatype = GL_FLOAT; 112 | this.usage = usage; 113 | this.mode = mode; 114 | 115 | this.vertexsize = vertexsize; 116 | this.vertexcount = buf.limit() / vertexsize; 117 | 118 | this.vboID = glGenBuffers(); 119 | vboIndices.add(vboID); 120 | 121 | feedF(buf); 122 | } 123 | public VBO(DoubleBuffer buf, int vertexsize, int usage, int mode) { 124 | this.dataPointer = buf; 125 | 126 | this.datatype = GL_DOUBLE; 127 | this.usage = usage; 128 | this.mode = mode; 129 | 130 | this.vertexsize = vertexsize; 131 | this.vertexcount = buf.limit() / vertexsize; 132 | 133 | this.vboID = glGenBuffers(); 134 | vboIndices.add(vboID); 135 | 136 | feedD(buf); 137 | } 138 | public VBO(ByteBuffer buf, int vertexsize, int usage, int mode) { 139 | this.dataPointer = buf; 140 | 141 | this.datatype = GL_BYTE; 142 | this.usage = usage; 143 | this.mode = mode; 144 | 145 | this.vertexsize = vertexsize; 146 | this.vertexcount = buf.limit() / vertexsize; 147 | 148 | this.vboID = glGenBuffers(); 149 | vboIndices.add(vboID); 150 | 151 | feedB(buf); 152 | } 153 | 154 | 155 | public void bind() { 156 | checkDeleted(); 157 | glBindBuffer(GL_ARRAY_BUFFER, vboID); 158 | } 159 | public void unbind() { 160 | glBindBuffer(GL_ARRAY_BUFFER, 0); 161 | } 162 | 163 | 164 | public void draw() { 165 | bind(); 166 | System.out.println(GL15.glIsBuffer(vboID)); 167 | 168 | glVertexPointer(vertexsize, datatype, 0, 0l); 169 | glDrawArrays(mode, 0, vertexcount); 170 | 171 | unbind(); 172 | } 173 | 174 | public void delete() { 175 | checkDeleted(); 176 | glDeleteBuffers(vboID); 177 | 178 | if(shouldmemfree) 179 | MemoryUtil.memFree(dataPointer); 180 | } 181 | 182 | 183 | 184 | private void checkDeleted() { 185 | if(invalid) throw new IllegalStateException("This buffer is deleted."); 186 | } 187 | 188 | private void feedF(FloatBuffer b) 189 | { 190 | glBindBuffer(GL_ARRAY_BUFFER, vboID); 191 | glBufferData(GL_ARRAY_BUFFER, b, usage); 192 | glBindBuffer(GL_ARRAY_BUFFER, 0); 193 | } 194 | private void feedD(DoubleBuffer b) 195 | { 196 | glBindBuffer(GL_ARRAY_BUFFER, vboID); 197 | glBufferData(GL_ARRAY_BUFFER, b, usage); 198 | glBindBuffer(GL_ARRAY_BUFFER, 0); 199 | } 200 | private void feedB(ByteBuffer b) 201 | { 202 | glBindBuffer(GL_ARRAY_BUFFER, vboID); 203 | glBufferData(GL_ARRAY_BUFFER, b, usage); 204 | glBindBuffer(GL_ARRAY_BUFFER, 0); 205 | } 206 | } 207 | -------------------------------------------------------------------------------- /ver.2/src/util/VBO.java: -------------------------------------------------------------------------------- 1 | package util; 2 | 3 | import static org.lwjgl.opengl.GL11.GL_BYTE; 4 | import static org.lwjgl.opengl.GL11.GL_DOUBLE; 5 | import static org.lwjgl.opengl.GL11.GL_FLOAT; 6 | import static org.lwjgl.opengl.GL11.glDrawArrays; 7 | import static org.lwjgl.opengl.GL11.glVertexPointer; 8 | import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; 9 | import static org.lwjgl.opengl.GL15.glBindBuffer; 10 | import static org.lwjgl.opengl.GL15.glBufferData; 11 | import static org.lwjgl.opengl.GL15.glDeleteBuffers; 12 | import static org.lwjgl.opengl.GL15.glGenBuffers; 13 | 14 | import java.nio.Buffer; 15 | import java.nio.ByteBuffer; 16 | import java.nio.DoubleBuffer; 17 | import java.nio.FloatBuffer; 18 | import java.util.Iterator; 19 | import java.util.LinkedList; 20 | 21 | import org.lwjgl.opengl.GL15; 22 | import org.lwjgl.system.MemoryUtil; 23 | 24 | public class VBO { 25 | 26 | private static final LinkedList vboIndices = new LinkedList<>(); 27 | public static void clearBuffersOnExit() { 28 | Iterator i = vboIndices.iterator(); 29 | while (i.hasNext()) { 30 | VBO vbo = i.next(); 31 | i.remove(); 32 | vbo.delete(); 33 | } 34 | } 35 | 36 | private final int vboID; 37 | private boolean invalid = false; 38 | private boolean shouldmemfree = false; 39 | 40 | private final int datatype; 41 | private final int usage; 42 | private final int mode; 43 | private final int vertexcount; 44 | private final int vertexsize; 45 | 46 | private final Buffer dataPointer; 47 | 48 | // Array versions 49 | 50 | public VBO(float[] arr, int vertexsize, int usage, int mode) { 51 | shouldmemfree = true; 52 | 53 | FloatBuffer buf = MemoryUtil.memAllocFloat(arr.length); 54 | buf.put(arr); 55 | buf.flip(); 56 | 57 | this.dataPointer = buf; 58 | 59 | this.datatype = GL_FLOAT; 60 | this.usage = usage; 61 | this.mode = mode; 62 | 63 | this.vertexsize = vertexsize; 64 | this.vertexcount = arr.length / vertexsize; 65 | 66 | this.vboID = glGenBuffers(); 67 | vboIndices.add(this); 68 | 69 | feedF(buf); 70 | } 71 | public VBO(double[] arr, int vertexsize, int usage, int mode) { 72 | shouldmemfree = true; 73 | 74 | DoubleBuffer buf = MemoryUtil.memAllocDouble(arr.length); 75 | buf.put(arr); 76 | buf.flip(); 77 | 78 | this.dataPointer = buf; 79 | 80 | this.datatype = GL_DOUBLE; 81 | this.usage = usage; 82 | this.mode = mode; 83 | 84 | this.vertexsize = vertexsize; 85 | this.vertexcount = arr.length / vertexsize; 86 | 87 | this.vboID = glGenBuffers(); 88 | vboIndices.add(this); 89 | 90 | feedD(buf); 91 | } 92 | public VBO(byte[] arr, int vertexsize, int usage, int mode) { 93 | shouldmemfree = true; 94 | 95 | ByteBuffer buf = MemoryUtil.memAlloc(arr.length); 96 | buf.put(arr); 97 | buf.flip(); 98 | 99 | this.dataPointer = buf; 100 | 101 | this.datatype = GL_BYTE; 102 | this.usage = usage; 103 | this.mode = mode; 104 | 105 | this.vertexsize = vertexsize; 106 | this.vertexcount = arr.length / vertexsize; 107 | 108 | this.vboID = glGenBuffers(); 109 | vboIndices.add(this); 110 | 111 | feedB(buf); 112 | } 113 | 114 | // Buffer versions 115 | 116 | public VBO(FloatBuffer buf, int vertexsize, int usage, int mode) { 117 | this.dataPointer = buf; 118 | 119 | this.datatype = GL_FLOAT; 120 | this.usage = usage; 121 | this.mode = mode; 122 | 123 | this.vertexsize = vertexsize; 124 | this.vertexcount = buf.limit() / vertexsize; 125 | 126 | this.vboID = glGenBuffers(); 127 | vboIndices.add(this); 128 | 129 | feedF(buf); 130 | } 131 | public VBO(DoubleBuffer buf, int vertexsize, int usage, int mode) { 132 | this.dataPointer = buf; 133 | 134 | this.datatype = GL_DOUBLE; 135 | this.usage = usage; 136 | this.mode = mode; 137 | 138 | this.vertexsize = vertexsize; 139 | this.vertexcount = buf.limit() / vertexsize; 140 | 141 | this.vboID = glGenBuffers(); 142 | vboIndices.add(this); 143 | 144 | feedD(buf); 145 | } 146 | public VBO(ByteBuffer buf, int vertexsize, int usage, int mode) { 147 | this.dataPointer = buf; 148 | 149 | this.datatype = GL_BYTE; 150 | this.usage = usage; 151 | this.mode = mode; 152 | 153 | this.vertexsize = vertexsize; 154 | this.vertexcount = buf.limit() / vertexsize; 155 | 156 | this.vboID = glGenBuffers(); 157 | vboIndices.add(this); 158 | 159 | feedB(buf); 160 | } 161 | 162 | 163 | public void bind() { 164 | checkDeleted(); 165 | glBindBuffer(GL_ARRAY_BUFFER, vboID); 166 | } 167 | public void unbind() { 168 | glBindBuffer(GL_ARRAY_BUFFER, 0); 169 | } 170 | 171 | 172 | public void draw() { 173 | bind(); 174 | 175 | glVertexPointer(vertexsize, datatype, 0, 0l); 176 | glDrawArrays(mode, 0, vertexcount); 177 | 178 | unbind(); 179 | } 180 | 181 | public void delete() { 182 | checkDeleted(); 183 | 184 | glDeleteBuffers(vboID); 185 | 186 | if(shouldmemfree) 187 | MemoryUtil.memFree(dataPointer); 188 | 189 | vboIndices.remove(this); 190 | } 191 | 192 | 193 | 194 | private void checkDeleted() { 195 | boolean isbuf = GL15.glIsBuffer(vboID); 196 | if(invalid || !isbuf) throw new IllegalStateException("This buffer is deleted. inv = "+invalid+", isbuf = "+isbuf); 197 | } 198 | 199 | private void feedF(FloatBuffer b) 200 | { 201 | glBindBuffer(GL_ARRAY_BUFFER, vboID); 202 | glBufferData(GL_ARRAY_BUFFER, b, usage); 203 | glBindBuffer(GL_ARRAY_BUFFER, 0); 204 | } 205 | private void feedD(DoubleBuffer b) 206 | { 207 | glBindBuffer(GL_ARRAY_BUFFER, vboID); 208 | glBufferData(GL_ARRAY_BUFFER, b, usage); 209 | glBindBuffer(GL_ARRAY_BUFFER, 0); 210 | } 211 | private void feedB(ByteBuffer b) 212 | { 213 | glBindBuffer(GL_ARRAY_BUFFER, vboID); 214 | glBufferData(GL_ARRAY_BUFFER, b, usage); 215 | glBindBuffer(GL_ARRAY_BUFFER, 0); 216 | } 217 | } 218 | -------------------------------------------------------------------------------- /src/engine/Engine.java: -------------------------------------------------------------------------------- 1 | package engine; 2 | 3 | import static org.lwjgl.glfw.GLFW.GLFW_CONTEXT_VERSION_MAJOR; 4 | import static org.lwjgl.glfw.GLFW.GLFW_CONTEXT_VERSION_MINOR; 5 | import static org.lwjgl.glfw.GLFW.GLFW_OPENGL_ANY_PROFILE; 6 | import static org.lwjgl.glfw.GLFW.GLFW_OPENGL_PROFILE; 7 | import static org.lwjgl.glfw.GLFW.glfwCreateWindow; 8 | import static org.lwjgl.glfw.GLFW.glfwGetPrimaryMonitor; 9 | import static org.lwjgl.glfw.GLFW.glfwGetVideoMode; 10 | import static org.lwjgl.glfw.GLFW.glfwInit; 11 | import static org.lwjgl.glfw.GLFW.glfwMakeContextCurrent; 12 | import static org.lwjgl.glfw.GLFW.glfwPollEvents; 13 | import static org.lwjgl.glfw.GLFW.glfwSetWindowPos; 14 | import static org.lwjgl.glfw.GLFW.glfwShowWindow; 15 | import static org.lwjgl.glfw.GLFW.glfwSwapBuffers; 16 | import static org.lwjgl.glfw.GLFW.glfwSwapInterval; 17 | import static org.lwjgl.glfw.GLFW.glfwWindowHint; 18 | import static org.lwjgl.glfw.GLFW.glfwWindowShouldClose; 19 | import static org.lwjgl.opengl.GL.createCapabilities; 20 | import static org.lwjgl.opengl.GL11.GL_BACK; 21 | import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT; 22 | import static org.lwjgl.opengl.GL11.GL_CULL_FACE; 23 | import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT; 24 | import static org.lwjgl.opengl.GL11.GL_DEPTH_TEST; 25 | import static org.lwjgl.opengl.GL11.GL_FRONT_AND_BACK; 26 | import static org.lwjgl.opengl.GL11.GL_INVALID_ENUM; 27 | import static org.lwjgl.opengl.GL11.GL_INVALID_OPERATION; 28 | import static org.lwjgl.opengl.GL11.GL_INVALID_VALUE; 29 | import static org.lwjgl.opengl.GL11.GL_LEFT; 30 | import static org.lwjgl.opengl.GL11.GL_LESS; 31 | import static org.lwjgl.opengl.GL11.GL_MODELVIEW; 32 | import static org.lwjgl.opengl.GL11.GL_NO_ERROR; 33 | import static org.lwjgl.opengl.GL11.GL_OUT_OF_MEMORY; 34 | import static org.lwjgl.opengl.GL11.GL_PROJECTION; 35 | import static org.lwjgl.opengl.GL11.GL_STACK_OVERFLOW; 36 | import static org.lwjgl.opengl.GL11.GL_STACK_UNDERFLOW; 37 | import static org.lwjgl.opengl.GL11.GL_VERTEX_ARRAY; 38 | import static org.lwjgl.opengl.GL11.glClear; 39 | import static org.lwjgl.opengl.GL11.glClearColor; 40 | import static org.lwjgl.opengl.GL11.glCullFace; 41 | import static org.lwjgl.opengl.GL11.glDepthFunc; 42 | import static org.lwjgl.opengl.GL11.glEnableClientState; 43 | import static org.lwjgl.opengl.GL11.glFrustum; 44 | import static org.lwjgl.opengl.GL11.glGetError; 45 | import static org.lwjgl.opengl.GL11.glLoadIdentity; 46 | import static org.lwjgl.opengl.GL11.glMatrixMode; 47 | import static org.lwjgl.opengl.GL11.glPointSize; 48 | import static org.lwjgl.system.MemoryUtil.NULL; 49 | 50 | import java.util.ArrayList; 51 | import java.util.Iterator; 52 | 53 | import org.lwjgl.glfw.GLFWErrorCallback; 54 | import org.lwjgl.glfw.GLFWVidMode; 55 | import org.lwjgl.opengl.GL11; 56 | 57 | import util.Access; 58 | import world.World; 59 | 60 | public class Engine { 61 | 62 | public static long runningTimeMillis = 0; 63 | 64 | public static long Window = NULL; 65 | 66 | public Engine(int width, int height, String title, boolean fullscreen) { 67 | GLFWErrorCallback.createPrint(System.err).set(); 68 | 69 | initGLFW(); 70 | createWindow(width, height, title, fullscreen); 71 | } 72 | 73 | private void initGLFW() { 74 | if (!glfwInit()) 75 | System.err.println("Failed to init GLFW"); 76 | } 77 | 78 | private void createWindow(int width, int height, String title, boolean fullscreen) { 79 | long window = glfwCreateWindow(width, height, title, fullscreen ? glfwGetPrimaryMonitor() : NULL, NULL); 80 | Access.drop(window, "window"); 81 | Access.drop(width, "width"); 82 | Access.drop(height, "height"); 83 | 84 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); 85 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); 86 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_ANY_PROFILE); 87 | 88 | glfwMakeContextCurrent(window); 89 | glfwSwapInterval(1); 90 | glfwShowWindow(window); 91 | 92 | GLFWVidMode vmode = glfwGetVideoMode(glfwGetPrimaryMonitor()); 93 | glfwSetWindowPos(window, vmode.width()/2 - width / 2, vmode.height()/2 - height / 2); 94 | 95 | createCapabilities(); 96 | 97 | 98 | GL11.glEnable(GL11.GL_TEXTURE_2D); 99 | GL11.glEnable(GL11.GL_DEPTH_TEST); glDepthFunc(GL_LESS); 100 | //GL11.glEnable(GL11.GL_CULL_FACE); glCullFace(GL_BACK); 101 | 102 | glEnableClientState(GL_VERTEX_ARRAY); 103 | 104 | 105 | glClearColor(0.1f, 0.1f, 0.1f, 1f); 106 | 107 | glMatrixMode(GL_PROJECTION); 108 | glLoadIdentity(); 109 | gluPerspective(70, (float)width / (float)height, 1f, 1000); 110 | glMatrixMode(GL_MODELVIEW); 111 | 112 | Syncer.syncer(79); 113 | 114 | last_time = Syncer.getTime(); 115 | 116 | while ( !glfwWindowShouldClose(window) ) { 117 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 118 | 119 | 120 | World.render(); 121 | Syncer.sync(); 122 | 123 | glfwSwapBuffers(window); 124 | glfwPollEvents(); 125 | 126 | postEvents(); 127 | } 128 | } 129 | 130 | public static void gluPerspective(float fovy, float aspect, float near, float far) { 131 | double fw, fh; 132 | fh = Math.tan(fovy / 360 * 3.14159265358979323846d) * near; 133 | fw = fh * aspect; 134 | glFrustum(-fw, fw, -fh, fh, near, far); 135 | } 136 | 137 | private static int errocode = GL_NO_ERROR; 138 | private static long last_time; 139 | 140 | private void postEvents() 141 | { 142 | checkActions(); 143 | 144 | long time = Syncer.getTime(); 145 | 146 | Syncer.DELTA = (float) ((time - last_time) / 1e7f); 147 | System.out.format(" Delta: %.2f" ,Syncer.DELTA); 148 | System.out.format(" FPS: %.1f\n", Syncer.fps / Syncer.DELTA); 149 | runningTimeMillis += Syncer.DELTA / 1e6f; 150 | last_time = time; 151 | 152 | if((errocode = glGetError()) != GL_NO_ERROR) 153 | { 154 | String[] errors = {"Invalid enum", "Invalid operation", "Invalid value", "Stack overflow", "Stack underflow", "Out of memory", "Unknown"}; 155 | String errstr; 156 | switch (errocode) { 157 | case GL_INVALID_ENUM: errstr = errors[0]; break; 158 | case GL_INVALID_OPERATION: errstr = errors[1]; break; 159 | case GL_INVALID_VALUE: errstr = errors[2]; break; 160 | case GL_STACK_OVERFLOW: errstr = errors[3]; break; 161 | case GL_STACK_UNDERFLOW: errstr = errors[4]; break; 162 | case GL_OUT_OF_MEMORY: errstr = errors[5]; break; 163 | default: errstr = errors[7]; break; 164 | } 165 | System.err.println("Error detected, code: "+errstr+ " - "+ errocode); 166 | System.exit(errocode); 167 | } 168 | } 169 | 170 | private static ArrayList time_actions = new ArrayList<>(); 171 | 172 | public static void everyN_MS(int millis, Runnable action) 173 | { 174 | time_actions.add(new TimeAction(runningTimeMillis, millis, action)); 175 | } 176 | private void checkActions() 177 | { 178 | Iterator i = time_actions.iterator(); 179 | while (i.hasNext()) { 180 | TimeAction timeAction = i.next(); 181 | if(runningTimeMillis > timeAction.msecondsi + timeAction.msecondst) 182 | { 183 | timeAction.run.run(); 184 | timeAction.msecondsi = runningTimeMillis; 185 | } 186 | } 187 | } 188 | private static class TimeAction { 189 | public long msecondsi, msecondst; 190 | public Runnable run; 191 | public TimeAction(long msecondsi, long msecondst, Runnable run) { 192 | this.msecondsi = msecondsi; 193 | this.msecondst = msecondst; 194 | this.run = run; 195 | } 196 | } 197 | } -------------------------------------------------------------------------------- /src/world/Camera.java: -------------------------------------------------------------------------------- 1 | package world; 2 | 3 | import java.awt.Font; 4 | import java.nio.DoubleBuffer; 5 | 6 | import org.joml.Vector3d; 7 | import org.lwjgl.BufferUtils; 8 | import org.lwjgl.glfw.GLFW; 9 | import org.lwjgl.opengl.GL; 10 | import org.lwjgl.opengl.GL11; 11 | import org.newdawn.slick.TrueTypeFont; 12 | 13 | import engine.Keyboard; 14 | import util.Access; 15 | 16 | public class Camera { 17 | public static double moveSpeed = 0.5f; 18 | 19 | private static double maxLook = 85; 20 | 21 | private static double mouseSensitivity = 0.025f; 22 | 23 | private static Vector3d pos; 24 | private static Vector3d rotation; 25 | 26 | public static void create() { 27 | pos = new Vector3d(0, 0, 0); 28 | rotation = new Vector3d(0, 0, 0); 29 | 30 | GLFW.glfwSetInputMode(Access.pull("window"), GLFW.GLFW_CURSOR, GLFW.GLFW_CURSOR_DISABLED); 31 | } 32 | 33 | public static void apply() { 34 | if(rotation.y / 360 > 1) { 35 | rotation.y -= 360; 36 | } else if(rotation.y / 360 < -1) { 37 | rotation.y += 360; 38 | } 39 | GL11.glLoadIdentity(); 40 | GL11.glRotated(rotation.x, 1, 0, 0); 41 | GL11.glRotated(rotation.y, 0, 1, 0); 42 | GL11.glRotated(rotation.z, 0, 0, 1); 43 | GL11.glTranslated(-pos.x, -pos.y, -pos.z); 44 | 45 | System.out.format("Pos x: %.2f", pos.x); 46 | System.out.format(" Pos y: %.2f", pos.y); 47 | System.out.format(" Pos z: %.2f ", pos.z); 48 | } 49 | 50 | 51 | public static void acceptInput(float delta) { 52 | acceptInputRotate(delta); 53 | acceptInputMove(delta); 54 | } 55 | 56 | private static double mouseX, mouseY; 57 | private static double[] pollMouseDelta() { 58 | DoubleBuffer x = BufferUtils.createDoubleBuffer(1); 59 | DoubleBuffer y = BufferUtils.createDoubleBuffer(1); 60 | 61 | GLFW.glfwGetCursorPos(Access.pull("window"), x, y); 62 | x.rewind(); 63 | y.rewind(); 64 | 65 | double tempX = mouseX; 66 | double tempY = mouseY; 67 | 68 | mouseX = x.get(); 69 | mouseY = y.get(); 70 | 71 | return new double[] { 72 | mouseX - tempX, 73 | mouseY - tempY 74 | }; 75 | } 76 | 77 | public static void acceptInputRotate(float delta) { 78 | double[] mdelta = pollMouseDelta(); 79 | if(GLFW.glfwGetInputMode(Access.pull("window"), GLFW.GLFW_CURSOR) == GLFW.GLFW_CURSOR_DISABLED) { 80 | rotation.y += mdelta[0] * mouseSensitivity * delta; 81 | rotation.x += mdelta[1] * mouseSensitivity * delta; 82 | rotation.x = Math.max(-maxLook, Math.min(maxLook, rotation.x)); 83 | } 84 | } 85 | 86 | public static void acceptInputMove(float delta) { 87 | boolean keyForward = Keyboard.isKeyDown(GLFW.GLFW_KEY_W); 88 | boolean keyBack = Keyboard.isKeyDown(GLFW.GLFW_KEY_S); 89 | boolean keyRight = Keyboard.isKeyDown(GLFW.GLFW_KEY_D); 90 | boolean keyLeft = Keyboard.isKeyDown(GLFW.GLFW_KEY_A); 91 | boolean keyFast = Keyboard.isKeyDown(GLFW.GLFW_KEY_Q); 92 | boolean keySlow = Keyboard.isKeyDown(GLFW.GLFW_KEY_E); 93 | boolean keyFlyUp = Keyboard.isKeyDown(GLFW.GLFW_KEY_LEFT_SHIFT); 94 | boolean keyFlyDown = Keyboard.isKeyDown(GLFW.GLFW_KEY_SPACE); 95 | 96 | if(Keyboard.isKeyDown(GLFW.GLFW_KEY_ESCAPE)) System.exit(0); 97 | Stars.renderBuffer = Keyboard.isKeyDown(GLFW.GLFW_KEY_TAB); 98 | 99 | double speed; 100 | 101 | if(keyFast) { 102 | speed = moveSpeed * 5; 103 | } 104 | else if(keySlow) { 105 | speed = moveSpeed / 2; 106 | } 107 | else { 108 | speed = moveSpeed; 109 | } 110 | 111 | speed *= delta; 112 | 113 | if(keyFlyUp) { 114 | pos.y -= speed; 115 | } 116 | if(keyFlyDown) { 117 | pos.y += speed; 118 | } 119 | 120 | if(keyBack) { 121 | pos.x -= Math.sin(Math.toRadians(rotation.y)) * speed; 122 | pos.z += Math.cos(Math.toRadians(rotation.y)) * speed; 123 | } 124 | if(keyForward) { 125 | pos.x += Math.sin(Math.toRadians(rotation.y)) * speed; 126 | pos.z -= Math.cos(Math.toRadians(rotation.y)) * speed; 127 | } 128 | if(keyLeft) { 129 | pos.x += Math.sin(Math.toRadians(rotation.y - 90)) * speed; 130 | pos.z -= Math.cos(Math.toRadians(rotation.y - 90)) * speed; 131 | } 132 | if(keyRight) { 133 | pos.x += Math.sin(Math.toRadians(rotation.y + 90)) * speed; 134 | pos.z -= Math.cos(Math.toRadians(rotation.y + 90)) * speed; 135 | } 136 | } 137 | 138 | public static void setSpeed(double speed) { 139 | moveSpeed = speed; 140 | } 141 | 142 | public static void setPos(Vector3d pos) { 143 | Camera.pos = pos; 144 | } 145 | 146 | public static Vector3d getPos() { 147 | return pos; 148 | } 149 | 150 | public static void setX(double x) { 151 | pos.x = x; 152 | } 153 | 154 | public static double getX() { 155 | return pos.x; 156 | } 157 | 158 | public static void addToX(double x) { 159 | pos.x += x; 160 | } 161 | 162 | public static void setY(double y) { 163 | pos.y = y; 164 | } 165 | 166 | public static double getY() { 167 | return pos.y; 168 | } 169 | 170 | public static void addToY(double y) { 171 | pos.y += y; 172 | } 173 | 174 | public static void setZ(double z) { 175 | pos.z = z; 176 | } 177 | 178 | public static double getZ() { 179 | return pos.z; 180 | } 181 | 182 | public static void addToZ(double z) { 183 | pos.z += z; 184 | } 185 | 186 | public static void setRotation(Vector3d rotation) { 187 | Camera.rotation = rotation; 188 | } 189 | 190 | public static Vector3d getRotation() { 191 | return rotation; 192 | } 193 | 194 | public static void setRotationX(double x) { 195 | rotation.x = x; 196 | } 197 | 198 | public static double getRotationX() { 199 | return rotation.x; 200 | } 201 | 202 | public static void addToRotationX(double x) { 203 | rotation.x += x; 204 | } 205 | 206 | public static void setRotationY(double y) { 207 | rotation.y = y; 208 | } 209 | 210 | public static double getRotationY() { 211 | return rotation.y; 212 | } 213 | 214 | public static void addToRotationY(double y) { 215 | rotation.y += y; 216 | } 217 | 218 | public static void setRotationZ(double z) { 219 | rotation.z = z; 220 | } 221 | 222 | public static double getRotationZ() { 223 | return rotation.z; 224 | } 225 | 226 | public static void addToRotationZ(double z) { 227 | rotation.z += z; 228 | } 229 | 230 | public static void setMaxLook(double maxLook) { 231 | Camera.maxLook = maxLook; 232 | } 233 | 234 | public static double getMaxLook() { 235 | return maxLook; 236 | } 237 | 238 | public static void setMouseSensitivity(double mouseSensitivity) { 239 | Camera.mouseSensitivity = mouseSensitivity; 240 | } 241 | 242 | public static double getMouseSensitivity() { 243 | return mouseSensitivity; 244 | } 245 | } 246 | -------------------------------------------------------------------------------- /ver.2/src/world/Blocks.java: -------------------------------------------------------------------------------- 1 | package world; 2 | 3 | import static org.lwjgl.opengl.GL11.GL_COMPILE; 4 | import static org.lwjgl.opengl.GL11.GL_QUADS; 5 | import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; 6 | import static org.lwjgl.opengl.GL11.glBegin; 7 | import static org.lwjgl.opengl.GL11.glBindTexture; 8 | import static org.lwjgl.opengl.GL11.glEnd; 9 | import static org.lwjgl.opengl.GL11.glEndList; 10 | import static org.lwjgl.opengl.GL11.glGenLists; 11 | import static org.lwjgl.opengl.GL11.glNewList; 12 | import static org.lwjgl.opengl.GL11.glTexCoord2f; 13 | import static org.lwjgl.opengl.GL11.*; 14 | import static world.chunk.Block.*; 15 | 16 | import engine.Tesselator; 17 | import util.TextureLoader; 18 | import world.chunk.Block; 19 | 20 | public class Blocks { 21 | 22 | private static final int[][] lists = new int[3][6]; // 1 - ID, 2 - sides: t0 b1 l2 r3 fw4 bw5 23 | 24 | public static void loadBlocks() { 25 | Block.createBlocks(); 26 | 27 | //genlist6tx(GRASS.id, TextureLoader.grasstop, TextureLoader.grasstop, TextureLoader.grassside, 28 | // TextureLoader.grassside, TextureLoader.grassside, TextureLoader.grassside); 29 | glBindTexture(GL_TEXTURE_2D, TextureLoader.grasstop); 30 | } 31 | 32 | public static void render(Block block, boolean upfree, boolean downfree, boolean leftfree, boolean rightfree, 33 | boolean frontfree, boolean backfree, float x, float y, float z) { 34 | 35 | if (upfree) { 36 | up(TextureLoader.grasstop, x, y, z); 37 | } 38 | if (downfree) { 39 | down(TextureLoader.grasstop, x, y, z); 40 | } 41 | if (leftfree) { 42 | left(TextureLoader.grasstop, x, y, z); 43 | } 44 | if (rightfree) { 45 | right(TextureLoader.grasstop, x, y, z); 46 | } 47 | if (frontfree) { 48 | front(TextureLoader.grasstop, x, y, z); 49 | } 50 | if (backfree) { 51 | back(TextureLoader.grasstop, x, y, z); 52 | } 53 | } 54 | 55 | private static void up(int tx, float x, float y, float z) { 56 | glBindTexture(GL_TEXTURE_2D, tx); 57 | 58 | Tesselator.get.color(0xff, 0xff, 0xff); 59 | 60 | Tesselator.get.tex(0, 1); 61 | Tesselator.get.vertex(1+x, 1+y, 0+z); 62 | Tesselator.get.tex(1, 1); 63 | Tesselator.get.vertex(0+x, 1+y, 0+z); 64 | Tesselator.get.tex(1, 0); 65 | Tesselator.get.vertex(0+x, 1+y, 1+z); 66 | Tesselator.get.tex(0, 0); 67 | Tesselator.get.vertex(1+x, 1+y, 1+z); 68 | 69 | /*glBegin(GL_QUADS); 70 | { 71 | // top face 72 | glTexCoord2f(0f, 1f); 73 | glVertex3f(1f, 1f, 0f); // 1 74 | glTexCoord2f(1f, 1f); 75 | glVertex3f(0f, 1f, 0f); // 2 76 | glTexCoord2f(1f, 0f); 77 | glVertex3f(0f, 1f, 1f); // 3 78 | glTexCoord2f(0f, 0f); 79 | glVertex3f(1f, 1f, 1f); // 4 80 | } 81 | glEnd();*/ 82 | } 83 | 84 | private static void down(int tx, float x, float y, float z) { 85 | //glBindTexture(GL_TEXTURE_2D, tx); 86 | 87 | Tesselator.get.color(0xff, 0xff, 0xff); 88 | 89 | Tesselator.get.tex(0, 1); 90 | Tesselator.get.vertex(1+x, 0+y, 1+z); 91 | Tesselator.get.tex(1, 1); 92 | Tesselator.get.vertex(0+x, 0+y, 1+z); 93 | Tesselator.get.tex(1, 0); 94 | Tesselator.get.vertex(0+x, 0+y, 0+z); 95 | Tesselator.get.tex(0, 0); 96 | Tesselator.get.vertex(1+x, 0+y, 0+z); 97 | 98 | /*glBegin(GL_QUADS); 99 | { 100 | // bottom face 101 | glTexCoord2f(0f, 1f); 102 | glVertex3f(1f, 0f, 1f); // 1 103 | glTexCoord2f(1f, 1f); 104 | glVertex3f(0f, 0f, 1f); // 2 105 | glTexCoord2f(1f, 0f); 106 | glVertex3f(0f, 0f, 0f); // 3 107 | glTexCoord2f(0f, 0f); 108 | glVertex3f(1f, 0f, 0f); // 4 109 | } 110 | glEnd();*/ 111 | } 112 | 113 | private static void left(int tx, float x, float y, float z) { 114 | //glBindTexture(GL_TEXTURE_2D, tx); 115 | 116 | Tesselator.get.color(0xff, 0xff, 0xff); 117 | 118 | Tesselator.get.tex(0, 1); 119 | Tesselator.get.vertex(1+x, 0+y, 1+z); 120 | Tesselator.get.tex(1, 1); 121 | Tesselator.get.vertex(1+x, 0+y, 0+z); 122 | Tesselator.get.tex(1, 0); 123 | Tesselator.get.vertex(1+x, 1+y, 0+z); 124 | Tesselator.get.tex(0, 0); 125 | Tesselator.get.vertex(1+x, 1+y, 1+z); 126 | 127 | /*glBegin(GL_QUADS); 128 | { 129 | // left face 130 | glTexCoord2f(0f, 1f); 131 | glVertex3f(1f, 0f, 1f); // 1 132 | glTexCoord2f(1f, 1f); 133 | glVertex3f(1f, 0f, 0f); // 2 134 | glTexCoord2f(1f, 0f); 135 | glVertex3f(1f, 1f, 0f); // 3 136 | glTexCoord2f(0f, 0f); 137 | glVertex3f(1f, 1f, 1f); // 4 138 | } 139 | glEnd();*/ 140 | } 141 | 142 | private static void right(int tx, float x, float y, float z) { 143 | //glBindTexture(GL_TEXTURE_2D, tx); 144 | 145 | Tesselator.get.color(0xff, 0xff, 0xff); 146 | 147 | Tesselator.get.tex(0, 1); 148 | Tesselator.get.vertex(0+x, 0+y, 0+z); 149 | Tesselator.get.tex(1, 1); 150 | Tesselator.get.vertex(0+x, 0+y, 1+z); 151 | Tesselator.get.tex(1, 0); 152 | Tesselator.get.vertex(0+x, 1+y, 1+z); 153 | Tesselator.get.tex(0, 0); 154 | Tesselator.get.vertex(0+x, 1+y, 0+z); 155 | 156 | /*glBegin(GL_QUADS); 157 | { 158 | // right face 159 | glTexCoord2f(0f, 1f); 160 | glVertex3f(0f, 0f, 0f); // 1 161 | glTexCoord2f(1f, 1f); 162 | glVertex3f(0f, 0f, 1f); // 2 163 | glTexCoord2f(1f, 0f); 164 | glVertex3f(0f, 1f, 1f); // 3 165 | glTexCoord2f(0f, 0f); 166 | glVertex3f(0f, 1f, 0f); // 4 167 | } 168 | glEnd(); 169 | glBindTexture(GL_TEXTURE_2D, 0);*/ 170 | } 171 | 172 | private static void front(int tx, float x, float y, float z) { 173 | //glBindTexture(GL_TEXTURE_2D, tx); 174 | 175 | Tesselator.get.color(0xff, 0xff, 0xff); 176 | 177 | Tesselator.get.tex(0, 1); 178 | Tesselator.get.vertex(1+x, 0+y, 0+z); 179 | Tesselator.get.tex(1, 1); 180 | Tesselator.get.vertex(0+x, 0+y, 0+z); 181 | Tesselator.get.tex(1, 0); 182 | Tesselator.get.vertex(0+x, 1+y, 0+z); 183 | Tesselator.get.tex(0, 0); 184 | Tesselator.get.vertex(1+x, 1+y, 0+z); 185 | 186 | /*glBegin(GL_QUADS); 187 | { 188 | // front face 189 | glTexCoord2f(0f, 1f); 190 | glVertex3f(1f, 0f, 0f); // 1 191 | glTexCoord2f(1f, 1f); 192 | glVertex3f(0f, 0f, 0f); // 2 193 | glTexCoord2f(1f, 0f); 194 | glVertex3f(0f, 1f, 0f); // 3 195 | glTexCoord2f(0f, 0f); 196 | glVertex3f(1f, 1f, 0f); // 4 197 | } 198 | glEnd();*/ 199 | } 200 | 201 | private static void back(int tx, float x, float y, float z) { 202 | 203 | Tesselator.get.color(0xff, 0xff, 0xff); 204 | 205 | Tesselator.get.tex(0, 1); 206 | Tesselator.get.vertex(0+x, 0+y, 1+z); 207 | Tesselator.get.tex(1, 1); 208 | Tesselator.get.vertex(1+x, 0+y, 1+z); 209 | Tesselator.get.tex(1, 0); 210 | Tesselator.get.vertex(1+x, 1+y, 1+z); 211 | Tesselator.get.tex(0, 0); 212 | Tesselator.get.vertex(0+x, 1+y, 1+z); 213 | 214 | /*glBegin(GL_QUADS); 215 | { 216 | // back face 217 | glTexCoord2f(0f, 1f); 218 | glVertex3f(0f, 0f, 1f); // 1 219 | glTexCoord2f(1f, 1f); 220 | glVertex3f(1f, 0f, 1f); // 2 221 | glTexCoord2f(1f, 0f); 222 | glVertex3f(1f, 1f, 1f); // 3 223 | glTexCoord2f(0f, 0f); 224 | glVertex3f(0f, 1f, 1f); // 4 225 | } 226 | glEnd();*/ 227 | } 228 | 229 | private static void genlist6tx(int arrindex, int txup, int txdown, int txright, int txleft, int txfw, int txbw) { 230 | /*int li = glGenLists(6); 231 | 232 | glNewList(li, GL_COMPILE); 233 | { 234 | front(txfw); 235 | } 236 | glEndList(); 237 | lists[arrindex][4] = li; 238 | 239 | glNewList(li + 1, GL_COMPILE); 240 | { 241 | back(txbw); 242 | } 243 | glEndList(); 244 | lists[arrindex][5] = li + 1; 245 | 246 | glNewList(li + 2, GL_COMPILE); 247 | { 248 | right(txright); 249 | } 250 | glEndList(); 251 | lists[arrindex][3] = li + 2; 252 | 253 | glNewList(li + 3, GL_COMPILE); 254 | { 255 | left(txleft); 256 | } 257 | glEndList(); 258 | lists[arrindex][2] = li + 3; 259 | 260 | glNewList(li + 4, GL_COMPILE); 261 | { 262 | up(txup); 263 | } 264 | glEndList(); 265 | lists[arrindex][0] = li + 4; 266 | 267 | glNewList(li + 5, GL_COMPILE); 268 | { 269 | down(txdown); 270 | } 271 | glEndList(); 272 | lists[arrindex][1] = li + 5;*/ 273 | } 274 | 275 | private static void genlist1tx(int arrindex, int texture) { 276 | genlist6tx(arrindex, texture, texture, texture, texture, texture, texture); 277 | } 278 | } 279 | -------------------------------------------------------------------------------- /ver.2/src/world/Camera.java: -------------------------------------------------------------------------------- 1 | package world; 2 | 3 | import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; 4 | import static org.lwjgl.opengl.GL11.glDisable; 5 | import static org.lwjgl.opengl.GL11.glEnable; 6 | 7 | import java.awt.Font; 8 | import java.nio.DoubleBuffer; 9 | import org.joml.Vector3d; 10 | import org.lwjgl.BufferUtils; 11 | import org.lwjgl.glfw.GLFW; 12 | import org.lwjgl.opengl.GL; 13 | import org.lwjgl.opengl.GL11; 14 | import org.newdawn.slick.TrueTypeFont; 15 | import engine.Engine; 16 | import engine.Tesselator; 17 | import input.Keyboard; 18 | import util.Syncer; 19 | import util.text.Text; 20 | 21 | public class Camera { 22 | public static double moveSpeed = 0.05f; 23 | 24 | private static double maxLook = 85; 25 | 26 | private static double mouseSensitivity = 0.04f; 27 | 28 | private static Vector3d pos; 29 | private static Vector3d rotation; 30 | 31 | public static int direction; 32 | 33 | public static void create() { 34 | pos = new Vector3d(0, 20, 0); 35 | rotation = new Vector3d(15, 138, 0); 36 | 37 | direction = 0; 38 | 39 | GLFW.glfwSetInputMode(Engine.window, GLFW.GLFW_CURSOR, GLFW.GLFW_CURSOR_DISABLED); 40 | } 41 | 42 | public static void apply() { 43 | if(rotation.y / 360 > 1) { 44 | rotation.y -= 360; 45 | } else if(rotation.y / 360 < -1) { 46 | rotation.y += 360; 47 | } 48 | GL11.glLoadIdentity(); 49 | GL11.glRotated(rotation.x, 1, 0, 0); 50 | GL11.glRotated(rotation.y, 0, 1, 0); 51 | GL11.glRotated(rotation.z, 0, 0, 1); 52 | GL11.glTranslated(-pos.x, -pos.y, -pos.z); 53 | 54 | double rot = rotation.y < 0 ? 360 - Math.abs(rotation.y + 45d) : rotation.y + 45d; 55 | if(rot >= 0 && rot < 90d) 56 | direction = 0; 57 | else if(rot >= 90d && rot < 180d) 58 | direction = 1; 59 | else if(rot >= 180d && rot < 270d) 60 | direction = 2; 61 | else if(rot >= 270d && rot < 360d) 62 | direction = 3; 63 | 64 | 65 | } 66 | 67 | public static void printValues() { 68 | 69 | } 70 | 71 | public static String getDir() { 72 | switch (direction) { 73 | case 0: return "negative z"; 74 | case 1: return "positive x"; 75 | case 2: return "positive z"; 76 | case 3: return "negative x"; 77 | default:return "unknown"; 78 | } 79 | } 80 | 81 | 82 | public static void acceptInput(float delta) { 83 | acceptInputRotate(delta); 84 | acceptInputMove(delta); 85 | } 86 | 87 | private static double mouseX, mouseY; 88 | private static double[] pollMouseDelta() { 89 | DoubleBuffer x = BufferUtils.createDoubleBuffer(1); 90 | DoubleBuffer y = BufferUtils.createDoubleBuffer(1); 91 | 92 | GLFW.glfwGetCursorPos(Engine.window, x, y); 93 | x.rewind(); 94 | y.rewind(); 95 | 96 | double tempX = mouseX; 97 | double tempY = mouseY; 98 | 99 | mouseX = x.get(); 100 | mouseY = y.get(); 101 | 102 | return new double[] { 103 | mouseX - tempX, 104 | mouseY - tempY 105 | }; 106 | } 107 | 108 | public static void acceptInputRotate(float delta) { 109 | double[] mdelta = pollMouseDelta(); 110 | if(GLFW.glfwGetInputMode(Engine.window, GLFW.GLFW_CURSOR) == GLFW.GLFW_CURSOR_DISABLED) { 111 | rotation.y += mdelta[0] * mouseSensitivity * delta; 112 | rotation.x += mdelta[1] * mouseSensitivity * delta; 113 | rotation.x = Math.max(-maxLook, Math.min(maxLook, rotation.x)); 114 | } 115 | } 116 | 117 | private static double sspeed = 1; 118 | 119 | public static void acceptInputMove(float delta) { 120 | boolean keyForward = Keyboard.isKeyDown(GLFW.GLFW_KEY_W); 121 | boolean keyBack = Keyboard.isKeyDown(GLFW.GLFW_KEY_S); 122 | boolean keyRight = Keyboard.isKeyDown(GLFW.GLFW_KEY_D); 123 | boolean keyLeft = Keyboard.isKeyDown(GLFW.GLFW_KEY_A); 124 | boolean keyFast = Keyboard.isKeyDown(GLFW.GLFW_KEY_Q); 125 | boolean keySlow = Keyboard.isKeyDown(GLFW.GLFW_KEY_E); 126 | boolean keyFlyUp = Keyboard.isKeyDown(GLFW.GLFW_KEY_LEFT_SHIFT); 127 | boolean keyFlyDown = Keyboard.isKeyDown(GLFW.GLFW_KEY_SPACE); 128 | 129 | if(Keyboard.isKeyDown(GLFW.GLFW_KEY_ESCAPE)) GLFW.glfwSetWindowShouldClose(Engine.window, true); 130 | Stars.renderBuffer = Keyboard.isKeyDown(GLFW.GLFW_KEY_TAB); 131 | 132 | double speed; 133 | 134 | if(keyFast) { 135 | speed = moveSpeed * 5; 136 | } 137 | else if(keySlow) { 138 | speed = moveSpeed / 4; 139 | } 140 | else { 141 | speed = moveSpeed; 142 | } 143 | 144 | speed *= delta; 145 | sspeed = speed; 146 | 147 | if(keyFlyUp) { 148 | pos.y -= speed; 149 | } 150 | if(keyFlyDown) { 151 | pos.y += speed; 152 | } 153 | 154 | if(keyBack) { 155 | pos.x -= Math.sin(Math.toRadians(rotation.y)) * speed; 156 | pos.z += Math.cos(Math.toRadians(rotation.y)) * speed; 157 | } 158 | if(keyForward) { 159 | pos.x += Math.sin(Math.toRadians(rotation.y)) * speed; 160 | pos.z -= Math.cos(Math.toRadians(rotation.y)) * speed; 161 | } 162 | if(keyLeft) { 163 | pos.x += Math.sin(Math.toRadians(rotation.y - 90)) * speed; 164 | pos.z -= Math.cos(Math.toRadians(rotation.y - 90)) * speed; 165 | } 166 | if(keyRight) { 167 | pos.x += Math.sin(Math.toRadians(rotation.y + 90)) * speed; 168 | pos.z -= Math.cos(Math.toRadians(rotation.y + 90)) * speed; 169 | } 170 | } 171 | 172 | public static void setSpeed(double speed) { 173 | moveSpeed = speed; 174 | } 175 | 176 | public static void setPos(Vector3d pos) { 177 | Camera.pos = pos; 178 | } 179 | 180 | public static Vector3d getPos() { 181 | return pos; 182 | } 183 | 184 | public static void setX(double x) { 185 | pos.x = x; 186 | } 187 | 188 | public static double getX() { 189 | return pos.x; 190 | } 191 | 192 | public static void addToX(double x) { 193 | pos.x += x; 194 | } 195 | 196 | public static void setY(double y) { 197 | pos.y = y; 198 | } 199 | 200 | public static double getY() { 201 | return pos.y; 202 | } 203 | 204 | public static void addToY(double y) { 205 | pos.y += y; 206 | } 207 | 208 | public static void setZ(double z) { 209 | pos.z = z; 210 | } 211 | 212 | public static double getZ() { 213 | return pos.z; 214 | } 215 | 216 | public static void addToZ(double z) { 217 | pos.z += z; 218 | } 219 | 220 | public static void setRotation(Vector3d rotation) { 221 | Camera.rotation = rotation; 222 | } 223 | 224 | public static Vector3d getRotation() { 225 | return rotation; 226 | } 227 | 228 | public static void setRotationX(double x) { 229 | rotation.x = x; 230 | } 231 | 232 | public static double getRotationX() { 233 | return rotation.x; 234 | } 235 | 236 | public static void addToRotationX(double x) { 237 | rotation.x += x; 238 | } 239 | 240 | public static void setRotationY(double y) { 241 | rotation.y = y; 242 | } 243 | 244 | public static double getRotationY() { 245 | return rotation.y; 246 | } 247 | 248 | public static void addToRotationY(double y) { 249 | rotation.y += y; 250 | } 251 | 252 | public static void setRotationZ(double z) { 253 | rotation.z = z; 254 | } 255 | 256 | public static double getRotationZ() { 257 | return rotation.z; 258 | } 259 | 260 | public static void addToRotationZ(double z) { 261 | rotation.z += z; 262 | } 263 | 264 | public static void setMaxLook(double maxLook) { 265 | Camera.maxLook = maxLook; 266 | } 267 | 268 | public static double getMaxLook() { 269 | return maxLook; 270 | } 271 | 272 | public static void setMouseSensitivity(double mouseSensitivity) { 273 | Camera.mouseSensitivity = mouseSensitivity; 274 | } 275 | 276 | public static double getMouseSensitivity() { 277 | return mouseSensitivity; 278 | } 279 | } 280 | -------------------------------------------------------------------------------- /ver.2/src/util/Frustum.java: -------------------------------------------------------------------------------- 1 | package util; 2 | 3 | import static org.lwjgl.opengl.GL11.glGetFloatv; 4 | 5 | import java.nio.FloatBuffer; 6 | 7 | import org.lwjgl.BufferUtils; 8 | 9 | import physics.AABB; 10 | 11 | public class Frustum { 12 | 13 | public float[][] m_Frustum = new float[6][4]; 14 | public static final int RIGHT = 0; 15 | public static final int LEFT = 1; 16 | public static final int BOTTOM = 2; 17 | public static final int TOP = 3; 18 | public static final int BACK = 4; 19 | public static final int FRONT = 5; 20 | public static final int A = 0; 21 | public static final int B = 1; 22 | public static final int C = 2; 23 | public static final int D = 3; 24 | private static Frustum frustum = new Frustum(); 25 | private FloatBuffer _proj = BufferUtils.createFloatBuffer(16); 26 | private FloatBuffer _modl = BufferUtils.createFloatBuffer(16); 27 | private FloatBuffer _clip = BufferUtils.createFloatBuffer(16); 28 | float[] proj = new float[16]; 29 | float[] modl = new float[16]; 30 | float[] clip = new float[16]; 31 | 32 | public static Frustum getFrustum() { 33 | frustum.calculateFrustum(); 34 | return frustum; 35 | } 36 | 37 | private void normalizePlane(float[][] frustum, int side) { 38 | float magnitude = (float) Math.sqrt((double) (frustum[side][0] * frustum[side][0] 39 | + frustum[side][1] * frustum[side][1] + frustum[side][2] * frustum[side][2])); 40 | frustum[side][0] /= magnitude; 41 | frustum[side][1] /= magnitude; 42 | frustum[side][2] /= magnitude; 43 | frustum[side][3] /= magnitude; 44 | } 45 | 46 | private void calculateFrustum() { 47 | this._proj.clear(); 48 | this._modl.clear(); 49 | this._clip.clear(); 50 | glGetFloatv(2983, this._proj); 51 | glGetFloatv(2982, this._modl); 52 | this._proj.flip().limit(16); 53 | this._proj.get(this.proj); 54 | this._modl.flip().limit(16); 55 | this._modl.get(this.modl); 56 | this.clip[0] = this.modl[0] * this.proj[0] + this.modl[1] * this.proj[4] + this.modl[2] * this.proj[8] 57 | + this.modl[3] * this.proj[12]; 58 | this.clip[1] = this.modl[0] * this.proj[1] + this.modl[1] * this.proj[5] + this.modl[2] * this.proj[9] 59 | + this.modl[3] * this.proj[13]; 60 | this.clip[2] = this.modl[0] * this.proj[2] + this.modl[1] * this.proj[6] + this.modl[2] * this.proj[10] 61 | + this.modl[3] * this.proj[14]; 62 | this.clip[3] = this.modl[0] * this.proj[3] + this.modl[1] * this.proj[7] + this.modl[2] * this.proj[11] 63 | + this.modl[3] * this.proj[15]; 64 | this.clip[4] = this.modl[4] * this.proj[0] + this.modl[5] * this.proj[4] + this.modl[6] * this.proj[8] 65 | + this.modl[7] * this.proj[12]; 66 | this.clip[5] = this.modl[4] * this.proj[1] + this.modl[5] * this.proj[5] + this.modl[6] * this.proj[9] 67 | + this.modl[7] * this.proj[13]; 68 | this.clip[6] = this.modl[4] * this.proj[2] + this.modl[5] * this.proj[6] + this.modl[6] * this.proj[10] 69 | + this.modl[7] * this.proj[14]; 70 | this.clip[7] = this.modl[4] * this.proj[3] + this.modl[5] * this.proj[7] + this.modl[6] * this.proj[11] 71 | + this.modl[7] * this.proj[15]; 72 | this.clip[8] = this.modl[8] * this.proj[0] + this.modl[9] * this.proj[4] + this.modl[10] * this.proj[8] 73 | + this.modl[11] * this.proj[12]; 74 | this.clip[9] = this.modl[8] * this.proj[1] + this.modl[9] * this.proj[5] + this.modl[10] * this.proj[9] 75 | + this.modl[11] * this.proj[13]; 76 | this.clip[10] = this.modl[8] * this.proj[2] + this.modl[9] * this.proj[6] + this.modl[10] * this.proj[10] 77 | + this.modl[11] * this.proj[14]; 78 | this.clip[11] = this.modl[8] * this.proj[3] + this.modl[9] * this.proj[7] + this.modl[10] * this.proj[11] 79 | + this.modl[11] * this.proj[15]; 80 | this.clip[12] = this.modl[12] * this.proj[0] + this.modl[13] * this.proj[4] + this.modl[14] * this.proj[8] 81 | + this.modl[15] * this.proj[12]; 82 | this.clip[13] = this.modl[12] * this.proj[1] + this.modl[13] * this.proj[5] + this.modl[14] * this.proj[9] 83 | + this.modl[15] * this.proj[13]; 84 | this.clip[14] = this.modl[12] * this.proj[2] + this.modl[13] * this.proj[6] + this.modl[14] * this.proj[10] 85 | + this.modl[15] * this.proj[14]; 86 | this.clip[15] = this.modl[12] * this.proj[3] + this.modl[13] * this.proj[7] + this.modl[14] * this.proj[11] 87 | + this.modl[15] * this.proj[15]; 88 | this.m_Frustum[0][0] = this.clip[3] - this.clip[0]; 89 | this.m_Frustum[0][1] = this.clip[7] - this.clip[4]; 90 | this.m_Frustum[0][2] = this.clip[11] - this.clip[8]; 91 | this.m_Frustum[0][3] = this.clip[15] - this.clip[12]; 92 | this.normalizePlane(this.m_Frustum, 0); 93 | this.m_Frustum[1][0] = this.clip[3] + this.clip[0]; 94 | this.m_Frustum[1][1] = this.clip[7] + this.clip[4]; 95 | this.m_Frustum[1][2] = this.clip[11] + this.clip[8]; 96 | this.m_Frustum[1][3] = this.clip[15] + this.clip[12]; 97 | this.normalizePlane(this.m_Frustum, 1); 98 | this.m_Frustum[2][0] = this.clip[3] + this.clip[1]; 99 | this.m_Frustum[2][1] = this.clip[7] + this.clip[5]; 100 | this.m_Frustum[2][2] = this.clip[11] + this.clip[9]; 101 | this.m_Frustum[2][3] = this.clip[15] + this.clip[13]; 102 | this.normalizePlane(this.m_Frustum, 2); 103 | this.m_Frustum[3][0] = this.clip[3] - this.clip[1]; 104 | this.m_Frustum[3][1] = this.clip[7] - this.clip[5]; 105 | this.m_Frustum[3][2] = this.clip[11] - this.clip[9]; 106 | this.m_Frustum[3][3] = this.clip[15] - this.clip[13]; 107 | this.normalizePlane(this.m_Frustum, 3); 108 | this.m_Frustum[4][0] = this.clip[3] - this.clip[2]; 109 | this.m_Frustum[4][1] = this.clip[7] - this.clip[6]; 110 | this.m_Frustum[4][2] = this.clip[11] - this.clip[10]; 111 | this.m_Frustum[4][3] = this.clip[15] - this.clip[14]; 112 | this.normalizePlane(this.m_Frustum, 4); 113 | this.m_Frustum[5][0] = this.clip[3] + this.clip[2]; 114 | this.m_Frustum[5][1] = this.clip[7] + this.clip[6]; 115 | this.m_Frustum[5][2] = this.clip[11] + this.clip[10]; 116 | this.m_Frustum[5][3] = this.clip[15] + this.clip[14]; 117 | this.normalizePlane(this.m_Frustum, 5); 118 | } 119 | 120 | public boolean pointInFrustum(float x, float y, float z) { 121 | for (int i = 0; i < 6; ++i) { 122 | if (this.m_Frustum[i][0] * x + this.m_Frustum[i][1] * y + this.m_Frustum[i][2] * z 123 | + this.m_Frustum[i][3] <= 0.0F) { 124 | return false; 125 | } 126 | } 127 | 128 | return true; 129 | } 130 | 131 | public boolean sphereInFrustum(float x, float y, float z, float radius) { 132 | for (int i = 0; i < 6; ++i) { 133 | if (this.m_Frustum[i][0] * x + this.m_Frustum[i][1] * y + this.m_Frustum[i][2] * z 134 | + this.m_Frustum[i][3] <= -radius) { 135 | return false; 136 | } 137 | } 138 | 139 | return true; 140 | } 141 | 142 | public boolean cubeFullyInFrustum(float x1, float y1, float z1, float x2, float y2, float z2) { 143 | for (int i = 0; i < 6; ++i) { 144 | if (this.m_Frustum[i][0] * x1 + this.m_Frustum[i][1] * y1 + this.m_Frustum[i][2] * z1 145 | + this.m_Frustum[i][3] <= 0.0F) { 146 | return false; 147 | } 148 | 149 | if (this.m_Frustum[i][0] * x2 + this.m_Frustum[i][1] * y1 + this.m_Frustum[i][2] * z1 150 | + this.m_Frustum[i][3] <= 0.0F) { 151 | return false; 152 | } 153 | 154 | if (this.m_Frustum[i][0] * x1 + this.m_Frustum[i][1] * y2 + this.m_Frustum[i][2] * z1 155 | + this.m_Frustum[i][3] <= 0.0F) { 156 | return false; 157 | } 158 | 159 | if (this.m_Frustum[i][0] * x2 + this.m_Frustum[i][1] * y2 + this.m_Frustum[i][2] * z1 160 | + this.m_Frustum[i][3] <= 0.0F) { 161 | return false; 162 | } 163 | 164 | if (this.m_Frustum[i][0] * x1 + this.m_Frustum[i][1] * y1 + this.m_Frustum[i][2] * z2 165 | + this.m_Frustum[i][3] <= 0.0F) { 166 | return false; 167 | } 168 | 169 | if (this.m_Frustum[i][0] * x2 + this.m_Frustum[i][1] * y1 + this.m_Frustum[i][2] * z2 170 | + this.m_Frustum[i][3] <= 0.0F) { 171 | return false; 172 | } 173 | 174 | if (this.m_Frustum[i][0] * x1 + this.m_Frustum[i][1] * y2 + this.m_Frustum[i][2] * z2 175 | + this.m_Frustum[i][3] <= 0.0F) { 176 | return false; 177 | } 178 | 179 | if (this.m_Frustum[i][0] * x2 + this.m_Frustum[i][1] * y2 + this.m_Frustum[i][2] * z2 180 | + this.m_Frustum[i][3] <= 0.0F) { 181 | return false; 182 | } 183 | } 184 | 185 | return true; 186 | } 187 | 188 | public boolean cubeInFrustum(float x1, float y1, float z1, float x2, float y2, float z2) { 189 | for (int i = 0; i < 6; ++i) { 190 | if (this.m_Frustum[i][0] * x1 + this.m_Frustum[i][1] * y1 + this.m_Frustum[i][2] * z1 191 | + this.m_Frustum[i][3] <= 0.0F 192 | && this.m_Frustum[i][0] * x2 + this.m_Frustum[i][1] * y1 + this.m_Frustum[i][2] * z1 193 | + this.m_Frustum[i][3] <= 0.0F 194 | && this.m_Frustum[i][0] * x1 + this.m_Frustum[i][1] * y2 + this.m_Frustum[i][2] * z1 195 | + this.m_Frustum[i][3] <= 0.0F 196 | && this.m_Frustum[i][0] * x2 + this.m_Frustum[i][1] * y2 + this.m_Frustum[i][2] * z1 197 | + this.m_Frustum[i][3] <= 0.0F 198 | && this.m_Frustum[i][0] * x1 + this.m_Frustum[i][1] * y1 + this.m_Frustum[i][2] * z2 199 | + this.m_Frustum[i][3] <= 0.0F 200 | && this.m_Frustum[i][0] * x2 + this.m_Frustum[i][1] * y1 + this.m_Frustum[i][2] * z2 201 | + this.m_Frustum[i][3] <= 0.0F 202 | && this.m_Frustum[i][0] * x1 + this.m_Frustum[i][1] * y2 + this.m_Frustum[i][2] * z2 203 | + this.m_Frustum[i][3] <= 0.0F 204 | && this.m_Frustum[i][0] * x2 + this.m_Frustum[i][1] * y2 + this.m_Frustum[i][2] * z2 205 | + this.m_Frustum[i][3] <= 0.0F) { 206 | return false; 207 | } 208 | } 209 | 210 | return true; 211 | } 212 | 213 | public boolean cubeInFrustum(AABB aabb) { 214 | return this.cubeInFrustum(aabb.x0, aabb.y0, aabb.z0, aabb.x1, aabb.y1, aabb.z1); 215 | } 216 | } 217 | -------------------------------------------------------------------------------- /ver.2/src/engine/Engine.java: -------------------------------------------------------------------------------- 1 | package engine; 2 | 3 | import static org.lwjgl.glfw.GLFW.GLFW_CONTEXT_VERSION_MAJOR; 4 | import static org.lwjgl.glfw.GLFW.GLFW_CONTEXT_VERSION_MINOR; 5 | import static org.lwjgl.glfw.GLFW.GLFW_OPENGL_ANY_PROFILE; 6 | import static org.lwjgl.glfw.GLFW.GLFW_OPENGL_PROFILE; 7 | import static org.lwjgl.glfw.GLFW.glfwCreateWindow; 8 | import static org.lwjgl.glfw.GLFW.glfwGetPrimaryMonitor; 9 | import static org.lwjgl.glfw.GLFW.glfwGetVideoMode; 10 | import static org.lwjgl.glfw.GLFW.glfwInit; 11 | import static org.lwjgl.glfw.GLFW.glfwMakeContextCurrent; 12 | import static org.lwjgl.glfw.GLFW.glfwPollEvents; 13 | import static org.lwjgl.glfw.GLFW.glfwSetWindowPos; 14 | import static org.lwjgl.glfw.GLFW.glfwShowWindow; 15 | import static org.lwjgl.glfw.GLFW.glfwSwapBuffers; 16 | import static org.lwjgl.glfw.GLFW.glfwSwapInterval; 17 | import static org.lwjgl.glfw.GLFW.glfwWindowHint; 18 | import static org.lwjgl.glfw.GLFW.glfwWindowShouldClose; 19 | import static org.lwjgl.opengl.GL.createCapabilities; 20 | import static org.lwjgl.opengl.GL11.GL_CCW; 21 | import static org.lwjgl.opengl.GL11.GL_COLOR_ARRAY; 22 | import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT; 23 | import static org.lwjgl.opengl.GL11.GL_CULL_FACE; 24 | import static org.lwjgl.opengl.GL11.GL_CW; 25 | import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT; 26 | import static org.lwjgl.opengl.GL11.GL_DEPTH_TEST; 27 | import static org.lwjgl.opengl.GL11.GL_FOG; 28 | import static org.lwjgl.opengl.GL11.GL_INVALID_ENUM; 29 | import static org.lwjgl.opengl.GL11.GL_INVALID_OPERATION; 30 | import static org.lwjgl.opengl.GL11.GL_INVALID_VALUE; 31 | import static org.lwjgl.opengl.GL11.GL_LEQUAL; 32 | import static org.lwjgl.opengl.GL11.GL_MODELVIEW; 33 | import static org.lwjgl.opengl.GL11.GL_NICEST; 34 | import static org.lwjgl.opengl.GL11.GL_NO_ERROR; 35 | import static org.lwjgl.opengl.GL11.GL_OUT_OF_MEMORY; 36 | import static org.lwjgl.opengl.GL11.GL_PERSPECTIVE_CORRECTION_HINT; 37 | import static org.lwjgl.opengl.GL11.GL_POLYGON_SMOOTH; 38 | import static org.lwjgl.opengl.GL11.GL_POLYGON_SMOOTH_HINT; 39 | import static org.lwjgl.opengl.GL11.GL_PROJECTION; 40 | import static org.lwjgl.opengl.GL11.GL_SMOOTH; 41 | import static org.lwjgl.opengl.GL11.GL_STACK_OVERFLOW; 42 | import static org.lwjgl.opengl.GL11.GL_STACK_UNDERFLOW; 43 | import static org.lwjgl.opengl.GL11.GL_STENCIL_BITS; 44 | import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; 45 | import static org.lwjgl.opengl.GL11.GL_TEXTURE_COORD_ARRAY; 46 | import static org.lwjgl.opengl.GL11.GL_VERTEX_ARRAY; 47 | import static org.lwjgl.opengl.GL11.glBlendFunc; 48 | import static org.lwjgl.opengl.GL11.glClear; 49 | import static org.lwjgl.opengl.GL11.glClearColor; 50 | import static org.lwjgl.opengl.GL11.glClearDepth; 51 | import static org.lwjgl.opengl.GL11.glCullFace; 52 | import static org.lwjgl.opengl.GL11.glDepthFunc; 53 | import static org.lwjgl.opengl.GL11.glDisable; 54 | import static org.lwjgl.opengl.GL11.glEnable; 55 | import static org.lwjgl.opengl.GL11.glEnableClientState; 56 | import static org.lwjgl.opengl.GL11.glFlush; 57 | import static org.lwjgl.opengl.GL11.glFrontFace; 58 | import static org.lwjgl.opengl.GL11.glFrustum; 59 | import static org.lwjgl.opengl.GL11.glGetError; 60 | import static org.lwjgl.opengl.GL11.glHint; 61 | import static org.lwjgl.opengl.GL11.glLoadIdentity; 62 | import static org.lwjgl.opengl.GL11.glMatrixMode; 63 | import static org.lwjgl.opengl.GL11.glOrtho; 64 | import static org.lwjgl.opengl.GL11.glPopMatrix; 65 | import static org.lwjgl.opengl.GL11.glPushMatrix; 66 | import static org.lwjgl.system.MemoryUtil.NULL; 67 | 68 | import java.util.ArrayList; 69 | import java.util.Iterator; 70 | import java.util.Vector; 71 | 72 | import javax.swing.JOptionPane; 73 | 74 | import org.lwjgl.glfw.GLFW; 75 | import org.lwjgl.glfw.GLFWErrorCallback; 76 | import org.lwjgl.glfw.GLFWVidMode; 77 | import org.lwjgl.opengl.GL11; 78 | 79 | import util.Syncer; 80 | import util.TextureLoader; 81 | import util.VBO; 82 | import util.text.Text; 83 | import world.Blocks; 84 | import world.Camera; 85 | import world.chunk.Block; 86 | 87 | public class Engine { 88 | 89 | public static long runningTimeMillis = 0; 90 | 91 | public static long window = NULL; 92 | public static int width, height; 93 | 94 | 95 | public Engine(int width, int height, String title, boolean fullscreen) throws Exception { 96 | GLFWErrorCallback.createPrint(System.err).set(); 97 | 98 | initGLFW(); 99 | 100 | createWindow(width, height, title, fullscreen); 101 | 102 | cleanup(); 103 | } 104 | 105 | private void initGLFW() { 106 | if (!glfwInit()) { 107 | System.err.println("Failed to init GLFW"); 108 | JOptionPane.showMessageDialog(null, "Failed to init GLFW", "Eroor", JOptionPane.ERROR_MESSAGE); 109 | } 110 | 111 | glfwWindowHint(GLFW.GLFW_STENCIL_BITS, 2); 112 | glfwWindowHint(GLFW.GLFW_SAMPLES, 4); 113 | } 114 | 115 | private void setupMatrix() { 116 | glMatrixMode(GL_PROJECTION); 117 | glLoadIdentity(); 118 | gluPerspective(70, (float)width / (float)height, 0.05f, 1000); 119 | glMatrixMode(GL_MODELVIEW); 120 | } 121 | 122 | private void setupOther() { 123 | Syncer.syncer(79); 124 | Syncer.setPrecision(50); 125 | last_time = Syncer.getTime(); 126 | 127 | TextureLoader.loadGrassSide(); 128 | TextureLoader.loadGrassTop(); 129 | TextureLoader.loadDirt(); 130 | 131 | Blocks.loadBlocks(); 132 | } 133 | 134 | private void configureOpenGL() { 135 | GL11.glEnable(GL11.GL_TEXTURE_2D); 136 | GL11.glEnable(GL11.GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glClearDepth(1f); 137 | // GL11.glEnable(GL11.GL_CULL_FACE); glCullFace(GL11.GL_BACK); glFrontFace(GL_CCW); 138 | 139 | glEnableClientState(GL_VERTEX_ARRAY); 140 | GL11.glEnableClientState(GL_TEXTURE_COORD_ARRAY); 141 | GL11.glEnableClientState(GL_COLOR_ARRAY); 142 | 143 | glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); 144 | 145 | glClearColor(0.5f, 0.8f, 1f, 0f); 146 | glClearDepth(1.0f); 147 | } 148 | 149 | private void loadData() throws Exception { 150 | Ticker.load(); 151 | Renderer.load(); 152 | } 153 | 154 | private void createWindow(int width, int height, String title, boolean fullscreen) throws Exception { 155 | 156 | setupWindow(title, fullscreen, width, height); 157 | 158 | configureOpenGL(); 159 | 160 | setupMatrix(); 161 | 162 | setupOther(); 163 | 164 | loadData(); 165 | 166 | while ( !glfwWindowShouldClose(window) ) { 167 | 168 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 169 | 170 | for (int t = 0; t < 8; t++) { 171 | Ticker.tick(); 172 | } 173 | Renderer.render(); 174 | Renderer.applyCamera(); 175 | 176 | //Syncer.sync(); 177 | 178 | glfwPollEvents(); 179 | 180 | postEvents(); 181 | 182 | glfwSwapBuffers(window); 183 | } 184 | } 185 | 186 | private void setupWindow(String title, boolean fullscreen, int width, int height) { 187 | Engine.window = glfwCreateWindow(width, height, title, fullscreen ? glfwGetPrimaryMonitor() : NULL, NULL); 188 | Engine.height = height; 189 | Engine.width = width; 190 | 191 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); 192 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); 193 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_ANY_PROFILE); 194 | 195 | glfwMakeContextCurrent(window); 196 | glfwSwapInterval(0); 197 | glfwShowWindow(window); 198 | 199 | GLFWVidMode vmode = glfwGetVideoMode(glfwGetPrimaryMonitor()); 200 | glfwSetWindowPos(window, vmode.width()/2 - width / 2, vmode.height()/2 - height / 2); 201 | 202 | createCapabilities(); 203 | } 204 | 205 | private void cleanup() { 206 | System.out.println("\n******************SHUTDOWN CLEANUP*******************"); 207 | 208 | VBO.clearBuffersOnExit(); System.out.println("- Deleted buffers"); 209 | 210 | GLFW.glfwDestroyWindow(window); System.out.println("- Destroyed window"); 211 | 212 | System.out.println("******************Cleanup finished!******************"); 213 | } 214 | 215 | public static void gluPerspective(float fovy, float aspect, float near, float far) { 216 | double fw, fh; 217 | fh = Math.tan(fovy / 360 * 3.14159265358979323846d) * near; 218 | fw = fh * aspect; 219 | glFrustum(-fw, fw, -fh, fh, near, far); 220 | } 221 | 222 | private static int errocode = GL_NO_ERROR; 223 | private static long last_time; 224 | 225 | private void postEvents() 226 | { 227 | //checkActions(); 228 | 229 | long time = Syncer.getTime(); 230 | 231 | float rawdelta = (time - last_time) / 1e9f; 232 | 233 | Syncer.DELTA = Math.max(1, rawdelta); 234 | Syncer.ACT_DELTA = rawdelta; 235 | 236 | Syncer.measureFPS(rawdelta); 237 | 238 | runningTimeMillis += Syncer.DELTA / 1e6f; 239 | last_time = time; 240 | 241 | if((errocode = glGetError()) != GL_NO_ERROR) 242 | { 243 | String[] errors = {"Invalid enum", "Invalid operation", "Invalid value", "Stack overflow", "Stack underflow", "Out of memory", "Unknown"}; 244 | String errstr; 245 | switch (errocode) { 246 | case GL_INVALID_ENUM: errstr = errors[0]; break; 247 | case GL_INVALID_OPERATION: errstr = errors[1]; break; 248 | case GL_INVALID_VALUE: errstr = errors[2]; break; 249 | case GL_STACK_OVERFLOW: errstr = errors[3]; break; 250 | case GL_STACK_UNDERFLOW: errstr = errors[4]; break; 251 | case GL_OUT_OF_MEMORY: errstr = errors[5]; break; 252 | default: errstr = errors[7]; break; 253 | } 254 | System.err.println("Error detected, code: "+errstr+ " - "+ errocode); 255 | System.exit(errocode); 256 | } 257 | } 258 | 259 | private static ArrayList time_actions = new ArrayList<>(); 260 | 261 | public static void everyN_MS(int millis, Runnable action) 262 | { 263 | time_actions.add(new TimeAction(runningTimeMillis, millis, action)); 264 | } 265 | private void checkActions() 266 | { 267 | Iterator i = time_actions.iterator(); 268 | while (i.hasNext()) { 269 | TimeAction timeAction = i.next(); 270 | if(runningTimeMillis > timeAction.msecondsi + timeAction.msecondst) 271 | { 272 | timeAction.run.run(); 273 | timeAction.msecondsi = runningTimeMillis; 274 | } 275 | } 276 | } 277 | 278 | private static class TimeAction { 279 | public long msecondsi, msecondst; 280 | public Runnable run; 281 | public TimeAction(long msecondsi, long msecondst, Runnable run) { 282 | this.msecondsi = msecondsi; 283 | this.msecondst = msecondst; 284 | this.run = run; 285 | } 286 | } 287 | } --------------------------------------------------------------------------------