diff --git a/VBO/res/shaders/main.frag b/VBO/res/shaders/main.frag deleted file mode 100644 index 1650bb2..0000000 --- a/VBO/res/shaders/main.frag +++ /dev/null @@ -1,8 +0,0 @@ -#version 330 -out vec4 fragColor; - -in vec4 color; - -void main(void){ - fragColor = color; -} \ No newline at end of file diff --git a/VBO/res/shaders/main.vert b/VBO/res/shaders/main.vert deleted file mode 100644 index ae77d04..0000000 --- a/VBO/res/shaders/main.vert +++ /dev/null @@ -1,11 +0,0 @@ -#version 330 - -layout (location = 0) in vec3 in_position; -layout (location = 1) in vec4 in_color; - -out vec4 color; - -void main(void){ - color = in_color; - gl_Position = ftransform(); -} \ No newline at end of file diff --git a/VBO/res/shaders/skybox.frag b/VBO/res/shaders/skybox.frag deleted file mode 100644 index ac8be2f..0000000 --- a/VBO/res/shaders/skybox.frag +++ /dev/null @@ -1,10 +0,0 @@ -#version 330 -out vec4 fragColor; -uniform samplerCube cubeMap; - -in vec3 position; - - -void main(void){ - fragColor = texture(cubeMap, position); -} \ No newline at end of file diff --git a/VBO/res/shaders/skybox.vert b/VBO/res/shaders/skybox.vert deleted file mode 100644 index fb38a21..0000000 --- a/VBO/res/shaders/skybox.vert +++ /dev/null @@ -1,12 +0,0 @@ -#version 330 - -layout (location = 0) in vec3 in_position; - -out vec3 position; -out vec3 rotation; - -void main(void){ - rotation = vec3(0,0,0); - position = gl_Vertex.xyz; - gl_Position = ftransform(); -} \ No newline at end of file diff --git a/VBO/res/tex/cubemap/back.jpg b/VBO/res/tex/cubemap/back.jpg deleted file mode 100644 index e729e97..0000000 Binary files a/VBO/res/tex/cubemap/back.jpg and /dev/null differ diff --git a/VBO/res/tex/cubemap/bottom.jpg b/VBO/res/tex/cubemap/bottom.jpg deleted file mode 100644 index 3df65a2..0000000 Binary files a/VBO/res/tex/cubemap/bottom.jpg and /dev/null differ diff --git a/VBO/res/tex/cubemap/front.jpg b/VBO/res/tex/cubemap/front.jpg deleted file mode 100644 index dc65562..0000000 Binary files a/VBO/res/tex/cubemap/front.jpg and /dev/null differ diff --git a/VBO/res/tex/cubemap/left.jpg b/VBO/res/tex/cubemap/left.jpg deleted file mode 100644 index 502f903..0000000 Binary files a/VBO/res/tex/cubemap/left.jpg and /dev/null differ diff --git a/VBO/res/tex/cubemap/right.jpg b/VBO/res/tex/cubemap/right.jpg deleted file mode 100644 index 3249c33..0000000 Binary files a/VBO/res/tex/cubemap/right.jpg and /dev/null differ diff --git a/VBO/res/tex/cubemap/top.jpg b/VBO/res/tex/cubemap/top.jpg deleted file mode 100644 index 3cf86e0..0000000 Binary files a/VBO/res/tex/cubemap/top.jpg and /dev/null differ diff --git a/VBO/src/game/Camera.java b/VBO/src/game/Camera.java new file mode 100644 index 0000000..9d0ce53 --- /dev/null +++ b/VBO/src/game/Camera.java @@ -0,0 +1,191 @@ +package game; + +import static org.lwjgl.opengl.GL11.*; +import world.*; +import blocks.*; +import main.*; +import math.*; + +public class Camera { + + private static Vector3f position = new Vector3f(0,4,0); + private static Vector2f rotation = new Vector2f(0,120); + private static PlayerRayCast playerRaycast; + private static final float HEIGHT = (float) (1.5f - (Block.getSize()/2.0f)); + public static boolean gravity = true; + public static float gravityFactor = 0; + public static boolean grounded = false; + public static boolean noClip = false; + + public static void initCamera(){ + playerRaycast = new PlayerRayCast(); + } + + public static void renderCamera(){ + glLoadIdentity(); + glRotatef(rotation.getX(), 1, 0, 0); + glRotatef(rotation.getY(), 0, 1, 0); + glTranslatef(-position.getX(), -(position.getY()+getHeight()), -position.getZ()); + } + + public static void move(float xa,float ya,float za){ + if(true){ + gravityFactor += World.GRAVITY * 0.01f; + if(grounded) gravityFactor = 0; + ya -= gravityFactor; + }else{ + gravityFactor = 0; + } + int xStep = (int)Math.abs(xa * 100); + for(int i = 0; i < xStep; i++){ + if(!isColliding(xa/xStep, 0, 0) || noClip){ + position.x += xa/xStep; + }else{ + xa = 0; + } + } + int yStep = (int)Math.abs(ya * 100); + for(int i = 0; i < yStep; i++){ + if(!isColliding(0, ya/yStep, 0) || noClip){ + position.y += ya/yStep; + }else{ + ya = 0; + } + } + int zStep = (int)Math.abs(za * 100); + for(int i = 0; i < zStep; i++){ + if(!isColliding(0, 0, za/zStep) || noClip){ + position.z += za/zStep; + }else{ + za = 0; + } + } + } + + public static Vector3f getDirection(){ + Vector3f r = new Vector3f(); + + float cosY = (float)Math.cos(Math.toRadians(rotation.getY() - 90)); + float sinY = (float)Math.sin(Math.toRadians(rotation.getY() - 90)); + float cosP = (float)Math.cos(Math.toRadians(-rotation.getX())); + float sinP = (float)Math.sin(Math.toRadians(-rotation.getX())); + + r.setX(cosY * cosP); + r.setY(sinP); + r.setZ(sinY * cosP); + + r.normalize(); + + return r; + } + + public static Vector3f getPosition() { + return position; + } + + public static void setPosition(Vector3f position) { + Camera.position = position; + } + + public static Vector2f getRotation() { + return rotation; + } + + public static void setRotation(Vector2f rotation) { + Camera.rotation = rotation; + } + + public static float getHeight() { + return HEIGHT; + } + + public static PlayerRayCast getPlayerRaycast() { + return playerRaycast; + } + + public static void setPlayerRaycast(PlayerRayCast playerRaycast) { + Camera.playerRaycast = playerRaycast; + } + + public static boolean isColliding(float xa,float ya, float za){ + World world = Main.getGame().getWorld(); + float r = 0.3f; + + boolean nx = false, ny = false, nz = false; + + int x0 = (int)(position.x + xa - r); + int x1 = (int)(position.x + xa + r); + + int y0 = (int)(position.y + ya - r); + int y1 = (int)(position.y + ya + r); + + int z0 = (int)(position.z + za - r); + int z1 = (int)(position.z + za + r); + + int xmid = (int)(position.x + xa); + int yb = (int)(position.y + ya - r - 0.01f); + int zmid = (int)(position.z + za); + + if(xmid < 0)nx = true;else nx = false; + if(yb < 0)ny = true;else ny = false; + if(zmid < 0)nz = true;else nz = false; + if(world.getBlock(xmid, yb, zmid,nx,ny,nz) != null ){ + grounded = true; + }else{ + grounded = false; + } + + if(x0 < 0)nx = true;else nx = false; + if(y0 < 0)ny = true;else ny = false; + if(z0 < 0)nz = true;else nz = false; + if(world.getBlock(x0, y0, z0,nx,ny,nz) != null) return true; + + if(x1 < 0)nx = true;else nx = false; + if(y0 < 0)ny = true;else ny = false; + if(z0 < 0)nz = true;else nz = false; + if(world.getBlock(x1, y0, z0,nx,ny,nz) != null) return true; + + if(x1 < 0)nx = true;else nx = false; + if(y1 < 0)ny = true;else ny = false; + if(z0 < 0)nz = true;else nz = false; + if(world.getBlock(x1, y1, z0,nx,ny,nz) != null) return true; + + if(x0 < 0)nx = true;else nx = false; + if(y1 < 0)ny = true;else ny = false; + if(z0 < 0)nz = true;else nz = false; + if(world.getBlock(x0, y1, z0,nx,ny,nz) != null) return true; + + if(x0 < 0)nx = true;else nx = false; + if(y0 < 0)ny = true;else ny = false; + if(z1 < 0)nz = true;else nz = false; + if(world.getBlock(x0, y0, z1,nx,ny,nz) != null) return true; + + if(x1 < 0)nx = true;else nx = false; + if(y0 < 0)ny = true;else ny = false; + if(z1 < 0)nz = true;else nz = false; + if(world.getBlock(x1, y0, z1,nx,ny,nz) != null) return true; + + if(x1 < 0)nx = true;else nx = false; + if(y1 < 0)ny = true;else ny = false; + if(z1 < 0)nz = true;else nz = false; + if(world.getBlock(x1, y1, z1,nx,ny,nz) != null) return true; + + if(x0 < 0)nx = true;else nx = false; + if(y1 < 0)ny = true;else ny = false; + if(z1 < 0)nz = true;else nz = false; + if(world.getBlock(x0, y1, z1,nx,ny,nz) != null) return true; + +// if(world.getBlock(x0, y0+1, z0) != null) return true; +// if(world.getBlock(x1, y0+1, z0) != null) return true; +// if(world.getBlock(x1, y1+1, z0) != null) return true; +// if(world.getBlock(x0, y1+1, z0) != null) return true; +// +// if(world.getBlock(x0, y0+1, z1) != null) return true; +// if(world.getBlock(x1, y0+1, z1) != null) return true; +// if(world.getBlock(x1, y1+1, z1) != null) return true; +// if(world.getBlock(x0, y1+1, z1) != null) return true; + + return false; + } + +} diff --git a/VBO/src/main/DisplayManager.java b/VBO/src/game/DisplayManager.java similarity index 93% rename from VBO/src/main/DisplayManager.java rename to VBO/src/game/DisplayManager.java index fa741e4..5b2be4e 100644 --- a/VBO/src/main/DisplayManager.java +++ b/VBO/src/game/DisplayManager.java @@ -1,6 +1,7 @@ -package main; +package game; import static org.lwjgl.opengl.GL11.*; import static org.lwjgl.util.glu.GLU.*; +import main.*; import org.lwjgl.input.*; import org.lwjgl.opengl.*; @@ -17,6 +18,7 @@ public class DisplayManager { */ public static void clearScreen(){ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glClearColor(0.19f, 0.79f, 0.82f, 1); } /** @@ -43,7 +45,7 @@ public class DisplayManager { renderBlock((int)Update.getSelectedVector().x,(int)Update.getSelectedVector().y,(int)Update.getSelectedVector().z); } } - + /** * @Info Definie le mode d'affichage pour le rendu en 2d */ diff --git a/VBO/src/main/Game.java b/VBO/src/game/Game.java similarity index 89% rename from VBO/src/main/Game.java rename to VBO/src/game/Game.java index c004420..edbc2e2 100644 --- a/VBO/src/main/Game.java +++ b/VBO/src/game/Game.java @@ -1,4 +1,4 @@ -package main; +package game; import world.*; diff --git a/VBO/src/main/PlayerRayCast.java b/VBO/src/game/PlayerRayCast.java similarity index 94% rename from VBO/src/main/PlayerRayCast.java rename to VBO/src/game/PlayerRayCast.java index 93be7a5..424ddbb 100644 --- a/VBO/src/main/PlayerRayCast.java +++ b/VBO/src/game/PlayerRayCast.java @@ -1,4 +1,4 @@ -package main; +package game; import java.util.*; diff --git a/VBO/src/main/Update.java b/VBO/src/game/Update.java similarity index 71% rename from VBO/src/main/Update.java rename to VBO/src/game/Update.java index 9643e88..30e1748 100644 --- a/VBO/src/main/Update.java +++ b/VBO/src/game/Update.java @@ -1,4 +1,5 @@ -package main; +package game; +import main.*; import math.*; import org.lwjgl.input.*; @@ -10,7 +11,7 @@ import blocks.*; public class Update { private static float xDir,yDir,zDir; - private static float speed = 0.01f; + private static float speed = 0.02f; private static float xa = 0,ya = 0,za = 0; private static Block selectedBlock = null; private static Vector3f selectedVector = new Vector3f(0,0,0); @@ -19,15 +20,17 @@ public class Update { * @Info Fonction permettant de gerer les action de la souris */ public static void updateMouse(){ - - Camera.getRotation().x -= Mouse.getDY() * 0.5; - Camera.getRotation().y += Mouse.getDX() * 0.5; - - while(Mouse.next()){ - if(Mouse.getEventButtonState()){ - - }else{ - + if(Mouse.isGrabbed()){ + + Camera.getRotation().x -= Mouse.getDY() * 0.5; + Camera.getRotation().y += Mouse.getDX() * 0.5; + + while(Mouse.next()){ + if(Mouse.getEventButtonState()){ + + }else{ + + } } } } @@ -47,47 +50,53 @@ public class Update { if(Keyboard.getEventKey() == Keyboard.KEY_F2){ Mouse.setGrabbed(!Mouse.isGrabbed()); } + if(Keyboard.getEventKey() == Keyboard.KEY_F5){ + Camera.setPosition(new Vector3f(0,2,0)); + } + if(Keyboard.getEventKey() == Keyboard.KEY_X){ + Camera.noClip = !Camera.noClip; + } }else{ } } - if(Camera.getRotation().x < -90) Camera.getRotation().x = -90; - if(Camera.getRotation().x > 90) Camera.getRotation().x = 90; - - - if(Keyboard.isKeyDown(Keyboard.KEY_Z)){ - zDir = -speed; - } - - if(Keyboard.isKeyDown(Keyboard.KEY_S)){ - zDir = speed; - } - - if(Keyboard.isKeyDown(Keyboard.KEY_Q)){ - xDir = -speed; - } - - if(Keyboard.isKeyDown(Keyboard.KEY_D)){ - xDir = speed; - } - - if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)){ -// if(grounded)yDir = 0.3f; -// if(!gravity)yDir = speed; - yDir = speed; - } - - if(Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)){ - speed = 0.05f; - }else{ - speed = 0.01f; - } - - if(Keyboard.isKeyDown(Keyboard.KEY_LCONTROL)){ -// if(!gravity)yDir = -speed; - yDir = -speed; + if(Mouse.isGrabbed()){ + if(Camera.getRotation().x < -90) Camera.getRotation().x = -90; + if(Camera.getRotation().x > 90) Camera.getRotation().x = 90; + + + if(Keyboard.isKeyDown(Keyboard.KEY_Z)){ + zDir = -speed; + } + + if(Keyboard.isKeyDown(Keyboard.KEY_S)){ + zDir = speed; + } + + if(Keyboard.isKeyDown(Keyboard.KEY_Q)){ + xDir = -speed; + } + + if(Keyboard.isKeyDown(Keyboard.KEY_D)){ + xDir = speed; + } + + if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)){ + if(Camera.grounded)yDir = 0.3f; + if(!Camera.gravity)yDir = speed; + } + + if(Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)){ + speed = 0.01f; + }else{ + speed = 0.02f; + } + + if(Keyboard.isKeyDown(Keyboard.KEY_LCONTROL)){ + if(!Camera.gravity)yDir = -speed; + } } xa += xDir * Math.cos(Math.toRadians(Camera.getRotation().y)) - zDir * Math.sin(Math.toRadians(Camera.getRotation().y)); diff --git a/VBO/src/main/VBO.java b/VBO/src/game/VBO.java similarity index 79% rename from VBO/src/main/VBO.java rename to VBO/src/game/VBO.java index 2c00170..e44218e 100644 --- a/VBO/src/main/VBO.java +++ b/VBO/src/game/VBO.java @@ -1,4 +1,4 @@ -package main; +package game; import static org.lwjgl.opengl.GL11.*; import static org.lwjgl.opengl.GL15.*; import static org.lwjgl.opengl.GL20.*; @@ -85,16 +85,19 @@ public class VBO { public void renderVBO(){ glBindBuffer(GL_ARRAY_BUFFER, vboID); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_COLOR_ARRAY); - glVertexAttribPointer(0,3,GL_FLOAT,false,7 * 4,0); - glVertexAttribPointer(1,4,GL_FLOAT,false,7 * 4,12); + glVertexPointer(3, GL_FLOAT, 7 * 4, 0); + glColorPointer(4, GL_FLOAT, 7 * 4, 12); + +// glVertexAttribPointer(0,3,GL_FLOAT,false,7 * 4,0); +// glVertexAttribPointer(1,4,GL_FLOAT,false,7 * 4,12); glDrawArrays(GL_QUADS, 0, bufferSize); - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_COLOR_ARRAY); } diff --git a/VBO/src/main/Camera.java b/VBO/src/main/Camera.java deleted file mode 100644 index 3334657..0000000 --- a/VBO/src/main/Camera.java +++ /dev/null @@ -1,107 +0,0 @@ -package main; - -import static org.lwjgl.opengl.GL11.*; -import world.*; -import blocks.*; -import math.*; - -public class Camera { - - private static Vector3f position = new Vector3f(0,4,0); - private static Vector2f rotation = new Vector2f(0,120); - private static PlayerRayCast playerRaycast; - private static final float HEIGHT = (float) (1.5f - (Block.getSize()/2.0f)); - - public static void initCamera(){ - playerRaycast = new PlayerRayCast(); - } - - public static void renderCamera(){ - glLoadIdentity(); - glRotatef(rotation.getX(), 1, 0, 0); - glRotatef(rotation.getY(), 0, 1, 0); - glTranslatef(-position.getX(), -(position.getY()+getHeight()), -position.getZ()); - } - - public static void move(float xa,float ya,float za){ -// if(true){ -// gravityFactor += World.GRAVITY * 0.01f; -// if(grounded) gravityFactor = 0; -// ya -= gravityFactor; -// }else{ -// gravityFactor = 0; -// } - int xStep = (int)Math.abs(xa * 100); - for(int i = 0; i < xStep; i++){ -// if(!isColliding(xa/xStep, 0, 0) || noClip){ - position.x += xa/xStep; -// }else{ -// xa = 0; -// } - } - int yStep = (int)Math.abs(ya * 100); - for(int i = 0; i < yStep; i++){ -// if(!isColliding(0, ya/yStep, 0) || noClip){ - position.y += ya/yStep; -// }else{ -// ya = 0; -// } - } - int zStep = (int)Math.abs(za * 100); - for(int i = 0; i < zStep; i++){ -// if(!isColliding(0, 0, za/zStep) || noClip){ - position.z += za/zStep; -// }else{ -// za = 0; -// } - } - } - - public static Vector3f getDirection(){ - Vector3f r = new Vector3f(); - - float cosY = (float)Math.cos(Math.toRadians(rotation.getY() - 90)); - float sinY = (float)Math.sin(Math.toRadians(rotation.getY() - 90)); - float cosP = (float)Math.cos(Math.toRadians(-rotation.getX())); - float sinP = (float)Math.sin(Math.toRadians(-rotation.getX())); - - r.setX(cosY * cosP); - r.setY(sinP); - r.setZ(sinY * cosP); - - r.normalize(); - - return r; - } - - public static Vector3f getPosition() { - return position; - } - - public static void setPosition(Vector3f position) { - Camera.position = position; - } - - public static Vector2f getRotation() { - return rotation; - } - - public static void setRotation(Vector2f rotation) { - Camera.rotation = rotation; - } - - public static float getHeight() { - return HEIGHT; - } - - public static PlayerRayCast getPlayerRaycast() { - return playerRaycast; - } - - public static void setPlayerRaycast(PlayerRayCast playerRaycast) { - Camera.playerRaycast = playerRaycast; - } - - - -} diff --git a/VBO/src/main/Main.java b/VBO/src/main/Main.java index 966e872..cc60085 100644 --- a/VBO/src/main/Main.java +++ b/VBO/src/main/Main.java @@ -1,6 +1,7 @@ package main; import rendering.*; +import game.*; import java.util.concurrent.*; @@ -28,7 +29,6 @@ public class Main { public static ExecutorService mainPool; private static Game game; - private static SkyBox skybox; /** * @param args @@ -45,15 +45,6 @@ public class Main { Display.setDisplayMode(new DisplayMode(width, height)); Display.setResizable(true); Display.create(); - String back = "/tex/cubemap/back.jpg"; - String bottom = "/tex/cubemap/bottom.jpg"; - String front = "/tex/cubemap/front.jpg"; - String top = "/tex/cubemap/top.jpg"; - String left = "/tex/cubemap/left.jpg"; - String right = "/tex/cubemap/right.jpg"; - - skybox = new SkyBox(new String[] { right, left, top, bottom, back, - front }); Camera.initCamera(); game = new Game(); Mouse.setGrabbed(true); @@ -99,9 +90,6 @@ public class Main { DisplayManager.clearScreen(); DisplayManager.preRender3D(); Camera.renderCamera(); - Shader.SKYBOX.bind(); - skybox.render(Camera.getPosition()); - Shader.MAIN.bind(); DisplayManager.render3D(); DisplayManager.preRender2D(); DisplayManager.render2D(); @@ -124,8 +112,7 @@ public class Main { + Camera.getPosition().getZ() + " | " + World.updateWorldTime + " " + DisplayManager.getDelta() + " " + Update.getSelectedBlock() + " | " - + getGame().getWorld().getBlock((int)Camera.getPosition().getX(), (int) Camera.getPosition().getY(), (int) Camera.getPosition().getZ(),nx,ny,nz) + " | " - + getGame().getWorld().getLocalChunk((int)Camera.getPosition().getX(), (int) Camera.getPosition().getY(), (int) Camera.getPosition().getZ(),nx,ny,nz).toString()); + + Runtime.getRuntime().totalMemory()/1024); FPS = 0; TICKS = 0; elapsedInfo = 0; diff --git a/VBO/src/rendering/Shader.java b/VBO/src/rendering/Shader.java deleted file mode 100644 index 2e6ee6a..0000000 --- a/VBO/src/rendering/Shader.java +++ /dev/null @@ -1,77 +0,0 @@ -package rendering; - -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL20.*; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; - -import math.Vector3f; - -public class Shader { - - public static final Shader MAIN = new Shader("/shaders/main.vert", "/shaders/main.frag"); - public static final Shader SKYBOX = new Shader("/shaders/skybox.vert", "/shaders/skybox.frag"); - - public int program; - - public Shader(String vertex, String fragment) { - program = glCreateProgram(); - - if (program == GL_FALSE) { - System.err.println("Shader program error"); - System.exit(1); - } - - createShader(loadShader(vertex), GL_VERTEX_SHADER); - createShader(loadShader(fragment), GL_FRAGMENT_SHADER); - - glLinkProgram(program); - glValidateProgram(program); - } - - private void createShader(String source, int type) { - int shader = glCreateShader(type); - if (shader == GL_FALSE) { - System.err.println("Shader error: " + shader); - System.exit(1); - } - glShaderSource(shader, source); - glCompileShader(shader); - if (glGetShaderi(shader, GL_COMPILE_STATUS) == GL_FALSE) { - System.err.println(glGetShaderInfoLog(shader, 2048)); - System.exit(1); - } - glAttachShader(program, shader); - } - - private String loadShader(String input) { - String r = ""; - - try { - BufferedReader reader = new BufferedReader(new InputStreamReader(Shader.class.getResourceAsStream(input))); - String buffer = ""; - while ((buffer = reader.readLine()) != null) { - r += buffer + "\n"; - } - reader.close(); - } - catch (IOException e) { - e.printStackTrace(); - } - return r; - } - - public void setUniform(String name, float v) { - glUniform1f(glGetUniformLocation(program, name), v); - } - - public void setUniform(String name, Vector3f v) { - glUniform3f(glGetUniformLocation(program, name), v.getX(), v.getY(), v.getZ()); - } - - public void bind() { - glUseProgram(program); - } -} \ No newline at end of file diff --git a/VBO/src/rendering/SkyBox.java b/VBO/src/rendering/SkyBox.java deleted file mode 100644 index 1a08f41..0000000 --- a/VBO/src/rendering/SkyBox.java +++ /dev/null @@ -1,147 +0,0 @@ -package rendering; - -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL12.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; - -import java.awt.image.BufferedImage; -import java.io.IOException; -import java.nio.FloatBuffer; -import java.nio.IntBuffer; - -import javax.imageio.ImageIO; - -import math.Vector3f; - -import org.lwjgl.BufferUtils; - -public class SkyBox { - - private int vbo; - private FloatBuffer buffer; - - private int textureID; - - - public SkyBox(String[] img){ - vbo = glGenBuffers(); - buffer = BufferUtils.createFloatBuffer(3 * 4 * 6); - buffer.put(blockData()); - buffer.flip(); - - glBindBuffer(GL_ARRAY_BUFFER, vbo); - glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW); - - createCubeMap(img); - } - - private void createCubeMap(String[] img){ - textureID = glGenTextures(); -// glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_CUBE_MAP, textureID); - for(int i = 0; i < img.length; i++){ - TextureData data = decode(img[i]); - glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0 , GL_RGBA,data.width, data.height,0,GL_RGBA,GL_UNSIGNED_BYTE,data.buffer); - } - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); - - } - - private TextureData decode(String path){ - int[] pixels = null; - int width = 0,height = 0; - try{ - BufferedImage image = ImageIO.read(SkyBox.class.getResource(path)); - width = image.getWidth(); - height = image.getHeight(); - pixels = new int[width * height]; - image.getRGB(0, 0, width, height, pixels, 0, width); - }catch(IOException e){ - e.printStackTrace(); - } - - int[] data = new int[pixels.length]; - for(int i = 0; i < pixels.length; i++){ - int a = (pixels[i] & 0xff000000) >> 24; - int r = (pixels[i] & 0xff0000) >> 16; - int g = (pixels[i] & 0xff00) >> 8; - int b = (pixels[i] & 0xff); - - data[i] = a << 24 | b << 16 | g << 8 | r; - } - - IntBuffer buffer = BufferUtils.createIntBuffer(data.length); - buffer.put(data).flip(); - - return new TextureData(glGenTextures(),width,height,buffer); - } - - public void render(Vector3f pos){ - glPushMatrix(); -// glLoadIdentity(); - glTranslatef(pos.x, pos.y + 1.75f, pos.z); - glBindTexture(GL_TEXTURE_CUBE_MAP, textureID); - glEnableVertexAttribArray(0); - glBindBuffer(GL_ARRAY_BUFFER, vbo); - glVertexAttribPointer(0, 3,GL_FLOAT, false, 3*4, 0); - glDrawArrays(GL_QUADS, 0, 4 * 6); - glDisableVertexAttribArray(0); - glBindTexture(GL_TEXTURE_CUBE_MAP, 0); - glPopMatrix(); - } - - private float[] blockData(){ - int size = 500; - - return new float[] { - +size,-size,-size, - -size,-size,-size, - -size,+size,-size, - +size,+size,-size, - - -size,-size,+size, - +size,-size,+size, - +size,+size,+size, - -size,+size,+size, - - -size,-size,-size, - +size,-size,-size, - +size,-size,+size, - -size,-size,+size, - - +size,+size,-size, - -size,+size,-size, - -size,+size,+size, - +size,+size,+size, - - -size,+size,-size, - -size,-size,-size, - -size,-size,+size, - -size,+size,+size, - - +size,-size,-size, - +size,+size,-size, - +size,+size,+size, - +size,-size,+size - }; - } - class TextureData{ - public TextureData(int id,int width, int height, IntBuffer buffer) { - this.id = id; - this.width = width; - this.height = height; - this.buffer = buffer; - } - public int id; - public int width, height; - public IntBuffer buffer; - } - -} diff --git a/VBO/src/world/Chunk.java b/VBO/src/world/Chunk.java index 374acdd..c0d3b6c 100644 --- a/VBO/src/world/Chunk.java +++ b/VBO/src/world/Chunk.java @@ -1,5 +1,7 @@ package world; +import game.*; + import java.util.*; import java.util.concurrent.*; @@ -11,7 +13,7 @@ import math.*; public class Chunk { - public final static int SIZE = 16; + public final static int SIZE = 64; private int x, y, z; private VBO vbo; private World world; @@ -334,8 +336,9 @@ class Generate implements Runnable { long current = System.currentTimeMillis(); long elapsed1 = 0; boolean IsError = true; - Noise noise = new Noise(world.seed, 30, 7); + Noise noise = new Noise(world.seed, 50, 16); Random random = new Random(world.seed); + Noise colorVariationNoise = new Noise(world.seed,50,2); for (int x = 0; x < chunk.SIZE; x++) { for (int z = 0; z < chunk.SIZE; z++) { int xa = (int)((Camera.getPosition().getX()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) - World.VIEW_CHUNK; @@ -350,7 +353,14 @@ class Generate implements Runnable { int xx = chunk.getX() * chunk.SIZE + x; int zz = chunk.getZ() * chunk.SIZE + z; - chunk.blocks[x][(int)noise.getNoise(xx, zz)][z] = Block.GRASS; + + Color4f green = new Color4f(0.2f,0.6f,0); + Color4f yellow = new Color4f(0.36f,0.77f,0.17f); + float v = colorVariationNoise.getNoise(xx, zz); + + Color4f finalColor = Color4f.interpolate(yellow, green, v); + + chunk.blocks[x][(int)noise.getNoise(xx, zz)][z] = new GrassBlock().setColor(finalColor); } } diff --git a/VBO/src/world/World.java b/VBO/src/world/World.java index f158190..0d4e951 100644 --- a/VBO/src/world/World.java +++ b/VBO/src/world/World.java @@ -1,5 +1,8 @@ package world; +import static org.lwjgl.opengl.GL11.*; +import game.*; + import java.util.*; import main.*; @@ -11,7 +14,7 @@ public class World { public long seed; public final int SIZE = 1,HEIGHT = 1; public static final float GRAVITY = 1; - public static final int VIEW_CHUNK = 4; + public static final int VIEW_CHUNK = 2; public static WorldNoise worldNoise; public ArrayList chunks = new ArrayList(); @@ -38,7 +41,6 @@ public class World { int za = (int)((Camera.getPosition().getZ()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) - VIEW_CHUNK; int zb = (int)((Camera.getPosition().getZ()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) + VIEW_CHUNK; - int delta_x = xb - xa; int delta_z = zb - za; for(int i = 0; i <= delta_x;i++){ @@ -71,11 +73,21 @@ public class World { public void render(){ for(Chunk c : chunks){ - if(c.isLoaded()) - c.render(); + if(c.isLoaded()){ + c.render(); + } } } + public void renderPoints(Vector2f... a){ + glPointSize(10); + glBegin(GL_POINTS); + for(Vector2f b : a){ + glVertex3f(b.getX(),Camera.getPosition().getY() + Camera.getHeight(),b.getY()); + } + glEnd(); + } + public void removeByChunk(Chunk ch){ for(int i = 0;i < chunks.size();i++){ if(chunks.get(i).equals(ch)){