commit 5ccf9988345ba40f7b1f6de0e44b481e2800d15e Author: MrDev023 Date: Wed Jul 15 17:19:40 2015 +0200 First Commit diff --git a/VBO/.classpath b/VBO/.classpath new file mode 100644 index 0000000..27b5ef8 --- /dev/null +++ b/VBO/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/VBO/.gitignore b/VBO/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/VBO/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/VBO/.project b/VBO/.project new file mode 100644 index 0000000..ecfdf06 --- /dev/null +++ b/VBO/.project @@ -0,0 +1,17 @@ + + + VBO + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/VBO/.settings/org.eclipse.jdt.core.prefs b/VBO/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..bb35fa0 --- /dev/null +++ b/VBO/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/VBO/res/shaders/main.frag b/VBO/res/shaders/main.frag new file mode 100644 index 0000000..1650bb2 --- /dev/null +++ b/VBO/res/shaders/main.frag @@ -0,0 +1,8 @@ +#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 new file mode 100644 index 0000000..ae77d04 --- /dev/null +++ b/VBO/res/shaders/main.vert @@ -0,0 +1,11 @@ +#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 new file mode 100644 index 0000000..ac8be2f --- /dev/null +++ b/VBO/res/shaders/skybox.frag @@ -0,0 +1,10 @@ +#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 new file mode 100644 index 0000000..b0375eb --- /dev/null +++ b/VBO/res/shaders/skybox.vert @@ -0,0 +1,10 @@ +#version 330 + +layout (location = 0) in vec3 in_position; + +out vec3 position; + +void main(void){ + 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 new file mode 100644 index 0000000..e729e97 Binary files /dev/null and b/VBO/res/tex/cubemap/back.jpg differ diff --git a/VBO/res/tex/cubemap/bottom.jpg b/VBO/res/tex/cubemap/bottom.jpg new file mode 100644 index 0000000..3df65a2 Binary files /dev/null and b/VBO/res/tex/cubemap/bottom.jpg differ diff --git a/VBO/res/tex/cubemap/front.jpg b/VBO/res/tex/cubemap/front.jpg new file mode 100644 index 0000000..dc65562 Binary files /dev/null and b/VBO/res/tex/cubemap/front.jpg differ diff --git a/VBO/res/tex/cubemap/left.jpg b/VBO/res/tex/cubemap/left.jpg new file mode 100644 index 0000000..502f903 Binary files /dev/null and b/VBO/res/tex/cubemap/left.jpg differ diff --git a/VBO/res/tex/cubemap/right.jpg b/VBO/res/tex/cubemap/right.jpg new file mode 100644 index 0000000..3249c33 Binary files /dev/null and b/VBO/res/tex/cubemap/right.jpg differ diff --git a/VBO/res/tex/cubemap/top.jpg b/VBO/res/tex/cubemap/top.jpg new file mode 100644 index 0000000..3cf86e0 Binary files /dev/null and b/VBO/res/tex/cubemap/top.jpg differ diff --git a/VBO/src/blocks/Block.java b/VBO/src/blocks/Block.java new file mode 100644 index 0000000..10deae3 --- /dev/null +++ b/VBO/src/blocks/Block.java @@ -0,0 +1,119 @@ +package blocks; + + + + +import math.Color4f; +import math.Vector3f; + + +public abstract class Block { + + public static final Block OAK_WOOD = new OakWoodBlock(), + GRASS = new GrassBlock(), + LEAF = new LeafBlock(), + FIR_LEAF = new FirLeafBlock(), + FIR_WOOD = new FirWoodBlock(); + + private float r,g,b,a; + + private static final float size = 1f; + + public Block(float r,float g,float b,float a){ + this.r = r; + this.g = g; + this.b = b; + this.a = a; + } + + + public Block(Color4f c) { + this.r = c.getR(); + this.g = c.getG(); + this.b = c.getB(); + this.a = c.getA(); + } + + + public Block setColor(float r,float g,float b){ + + this.r = r; + this.g = g; + this.b = b; + + return this; + } + + public Block setColor(Color4f c){ + + this.r = c.getR(); + this.g = c.getG(); + this.b = c.getB(); + + return this; + } + + public float[] getDataFront(float x,float y,float z,float[] shading) { + return new float[]{ + x,y,z, r * 0.9f * shading[0],g * 0.9f * shading[0],b * 0.9f * shading[0],a, + x+size,y,z, r * 0.9f * shading[1],g * 0.9f * shading[1],b * 0.9f * shading[1],a, + x+size,y+size,z, r * 0.9f * shading[2],g * 0.9f * shading[2],b * 0.9f * shading[2],a, + x,y+size,z, r * 0.9f * shading[3],g * 0.9f * shading[3],b * 0.9f * shading[3],a + }; + } + + public float[] getDataBack(float x,float y,float z,float[] shading) { + return new float[]{ + x+size,y,z+size, r * 0.9f * shading[0],g * 0.9f * shading[0],b * 0.9f * shading[0],a, + x,y,z+size, r * 0.9f * shading[1],g * 0.9f * shading[1],b * 0.9f * shading[1],a, + x,y+size,z+size, r * 0.9f * shading[2],g * 0.9f * shading[2],b * 0.9f * shading[2],a, + x+size,y+size,z+size, r * 0.9f * shading[3],g * 0.9f * shading[3],b * 0.9f * shading[3],a + }; + } + + public float[] getDataLeft(float x,float y,float z,float[] shading) { + return new float[]{ + x,y,z, r * 0.8f * shading[0],g * 0.8f * shading[0],b * 0.8f * shading[0],a, + x,y+size,z, r * 0.8f * shading[1],g * 0.8f * shading[1],b * 0.8f * shading[1],a, + x,y+size,z+size, r * 0.8f * shading[2],g * 0.8f * shading[2],b * 0.8f * shading[2],a, + x,y,z+size, r * 0.8f * shading[3],g * 0.8f * shading[3],b * 0.8f * shading[3],a + }; + } + + public float[] getDataRight(float x,float y,float z,float[] shading) { + return new float[]{ + x+size,y+size,z, r * 0.8f * shading[0],g * 0.8f * shading[0],b * 0.8f * shading[0],a, + x+size,y,z, r * 0.8f * shading[1],g * 0.8f * shading[1],b * 0.8f * shading[1],a, + x+size,y,z+size, r * 0.8f * shading[2],g * 0.8f * shading[2],b * 0.8f * shading[2],a, + x+size,y+size,z+size, r * 0.8f * shading[3],g * 0.8f * shading[3],b * 0.8f * shading[3],a + }; + } + + + public float[] getDataDown(float x,float y,float z,float[] shading) { + return new float[]{ + x+size,y,z, r * 0.7f * shading[0],g * 0.7f * shading[0],b * 0.7f * shading[0],a, + x,y,z, r * 0.7f * shading[1],g * 0.7f * shading[1],b * 0.7f * shading[1],a, + x,y,z+size, r * 0.7f * shading[2],g * 0.7f * shading[2],b * 0.7f * shading[2],a, + x+size,y,z+size, r * 0.7f * shading[3],g * 0.7f * shading[3],b * 0.7f * shading[3],a + + }; + } + + public float[] getDataUp(float x,float y,float z,float[] shading) { + return new float[]{ + x,y+size,z, r * 1.0f * shading[0],g * 1.0f * shading[0],b * 1.0f * shading[0],a, + x+size,y+size,z, r * 1.0f * shading[1],g * 1.0f * shading[1],b * 1.0f * shading[1],a, + x+size,y+size,z+size, r * 1.0f * shading[2],g * 1.0f * shading[2],b * 1.0f * shading[2],a, + x,y+size,z+size, r * 1.0f * shading[3],g * 1.0f * shading[3],b * 1.0f * shading[3],a + }; + } + + public static float getSize() { + return size; + } + + + + +} diff --git a/VBO/src/blocks/FirLeafBlock.java b/VBO/src/blocks/FirLeafBlock.java new file mode 100644 index 0000000..5ddb8e6 --- /dev/null +++ b/VBO/src/blocks/FirLeafBlock.java @@ -0,0 +1,11 @@ +package blocks; + +import math.Color4f; + +public class FirLeafBlock extends Block{ + + public FirLeafBlock() { + super(new Color4f(0.0f,0.4f,0f)); + } + +} diff --git a/VBO/src/blocks/FirWoodBlock.java b/VBO/src/blocks/FirWoodBlock.java new file mode 100644 index 0000000..47567af --- /dev/null +++ b/VBO/src/blocks/FirWoodBlock.java @@ -0,0 +1,11 @@ +package blocks; + +import math.Color4f; + +public class FirWoodBlock extends WoodBlock{ + + public FirWoodBlock() { + super(new Color4f(0.32f,0.22f,0.04f)); + } + +} diff --git a/VBO/src/blocks/GrassBlock.java b/VBO/src/blocks/GrassBlock.java new file mode 100644 index 0000000..aa3c3a6 --- /dev/null +++ b/VBO/src/blocks/GrassBlock.java @@ -0,0 +1,10 @@ +package blocks; + +import math.Color4f; + +public class GrassBlock extends Block{ + public GrassBlock() { + super(new Color4f(0.2f,0.6f,0f)); + } + +} diff --git a/VBO/src/blocks/LeafBlock.java b/VBO/src/blocks/LeafBlock.java new file mode 100644 index 0000000..d49356d --- /dev/null +++ b/VBO/src/blocks/LeafBlock.java @@ -0,0 +1,11 @@ +package blocks; + +import math.Color4f; + +public class LeafBlock extends Block{ + + public LeafBlock() { + super(new Color4f(0,0.6f,0,1)); + } + +} diff --git a/VBO/src/blocks/OakWoodBlock.java b/VBO/src/blocks/OakWoodBlock.java new file mode 100644 index 0000000..b5afb91 --- /dev/null +++ b/VBO/src/blocks/OakWoodBlock.java @@ -0,0 +1,11 @@ +package blocks; + +import math.*; + +public class OakWoodBlock extends WoodBlock{ + + public OakWoodBlock() { + super(new Color4f(0.42f,0.32f,0.14f)); + } + +} diff --git a/VBO/src/blocks/TransparentBlock.java b/VBO/src/blocks/TransparentBlock.java new file mode 100644 index 0000000..edaeb2d --- /dev/null +++ b/VBO/src/blocks/TransparentBlock.java @@ -0,0 +1,11 @@ +package blocks; + +import math.Color4f; + +public class TransparentBlock extends Block { + + public TransparentBlock(){ + super(new Color4f(1,1,1,0)); + } + +} diff --git a/VBO/src/blocks/WoodBlock.java b/VBO/src/blocks/WoodBlock.java new file mode 100644 index 0000000..c3f6f14 --- /dev/null +++ b/VBO/src/blocks/WoodBlock.java @@ -0,0 +1,11 @@ +package blocks; + +import math.Color4f; + +public class WoodBlock extends Block{ + + public WoodBlock(Color4f c) { + super(c); + } + +} diff --git a/VBO/src/main/Camera.java b/VBO/src/main/Camera.java new file mode 100644 index 0000000..e617f01 --- /dev/null +++ b/VBO/src/main/Camera.java @@ -0,0 +1,71 @@ +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 final float HEIGHT = (float) (1.5f - (Block.getSize()/2.0f)); + + public static void renderCamera(){ + glLoadIdentity(); + glRotatef(rotation.getX(), 1, 0, 0); + glRotatef(rotation.getY(), 0, 1, 0); + glTranslatef(-position.getX(), -(position.getY()+HEIGHT), -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 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; + } + +} diff --git a/VBO/src/main/DisplayManager.java b/VBO/src/main/DisplayManager.java new file mode 100644 index 0000000..4ed97ab --- /dev/null +++ b/VBO/src/main/DisplayManager.java @@ -0,0 +1,88 @@ +package main; +import static org.lwjgl.opengl.GL11.*; +import static org.lwjgl.util.glu.GLU.*; + +import org.lwjgl.input.*; +import org.lwjgl.opengl.*; +import org.lwjgl.util.glu.*; + + +public class DisplayManager { + + private static long delta = 0; + private static float fov = 45; + + /** + * @Info Nettoie l'ecran + */ + public static void clearScreen(){ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + } + + /** + * @Info Definie le mode d'affichage pour le rendu en 3d + */ + public static void preRender3D(){ + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective(fov, (float)Display.getDisplayMode().getWidth()/(float)Display.getDisplayMode().getHeight(), 0.1f, 1000f); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glEnable(GL_DEPTH_TEST); + glEnable(GL_CULL_FACE); + glCullFace(GL_FRONT); + glEnable(GL_TEXTURE_2D); + } + + /** + * @Info Fait le rendu 3d + */ + public static void render3D(){ + Camera.renderCamera(); + Main.getGame().render(); + } + + /** + * @Info Definie le mode d'affichage pour le rendu en 2d + */ + public static void preRender2D(){ + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + GLU.gluOrtho2D(0, Display.getDisplayMode().getWidth(), Display + .getDisplayMode().getHeight(), 0); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + } + + /** + * @Info Fait le rendu 2d + */ + public static void render2D(){ + Main.getGame().renderGUI(); + } + + /** + * @Info mets a jour la resolution de l'ecran + */ + public static void updateDisplay() { + glViewport(0, 0, Display.getWidth(), Display.getHeight()); + } + + public static long getDelta() { + return delta; + } + + public static void setDelta(long delta) { + DisplayManager.delta = delta; + } + + public static float getFov() { + return fov; + } + + public static void setFov(float fov) { + DisplayManager.fov = fov; + } + + +} diff --git a/VBO/src/main/Game.java b/VBO/src/main/Game.java new file mode 100644 index 0000000..a03868e --- /dev/null +++ b/VBO/src/main/Game.java @@ -0,0 +1,25 @@ +package main; + +import world.*; + +public class Game { + + private World world; + + public Game(){ + world= new World(0); + } + + public void update(){ + world.update(); + } + + public void render(){ + world.render(); + } + + public void renderGUI(){ + + } + +} diff --git a/VBO/src/main/Main.java b/VBO/src/main/Main.java new file mode 100644 index 0000000..1aa4842 --- /dev/null +++ b/VBO/src/main/Main.java @@ -0,0 +1,152 @@ +package main; + +import org.lwjgl.input.*; +import org.lwjgl.opengl.*; + +import rendering.*; + +public class Main { + + private static boolean IsRunning = true; + private static long current = System.currentTimeMillis(), elapsedInfo = 0, + elapsed = 0, previous = 0; + private static long timeTicks = 0, timeFps = 0; + private static int FPS = 0, TICKS = 0, LAST_TICKS = 60, LAST_FPS = 60; + + private static final String TITLE = "Test VBO"; + private static final int width = 1280, height = 720; + + private static Game game; + + /** + * @param args + * @Info Fonction principal + */ + public static void main(String[] args) { + try { + Display.setTitle(TITLE); + Display.setDisplayMode(new DisplayMode(width, height)); + Display.setResizable(true); + Mouse.setGrabbed(true); + Display.create(); + game = new Game(); + loop(); + } catch (Exception e) { + + } + } + + /** + * @Info Boucle principal avec Timer + */ + public static void loop() { + while (IsRunning) { + previous = current; + current = System.currentTimeMillis(); + elapsed += current - previous; + elapsedInfo += current - previous; + DisplayManager.setDelta(current - previous); + + if (Display.wasResized()) { + DisplayManager.updateDisplay(); + } + + if (elapsed >= 1000 / 60) { + Update.updateMouse(); + Update.updateKeyboard(); + Update.update(); + TICKS++; + elapsed = 0; + timeTicks = System.currentTimeMillis() - current; + } else { + DisplayManager.clearScreen(); + Shader.MAIN.bind(); + DisplayManager.preRender3D(); + DisplayManager.render3D(); + DisplayManager.preRender2D(); + DisplayManager.render2D(); + FPS++; + timeFps = System.currentTimeMillis() - current; + } + + if (elapsedInfo >= 1000) { + LAST_FPS = FPS; + LAST_TICKS = TICKS; + Display.setTitle(TITLE + " | FPS:" + LAST_FPS + " TICKS:" + + LAST_TICKS + " timeFps:" + timeFps + "ms timeTicks:" + + timeTicks + "ms" + " | PX:" + + Camera.getPosition().getX() + " PY:" + + Camera.getPosition().getY() + " PZ:" + + Camera.getPosition().getZ()); + FPS = 0; + TICKS = 0; + elapsedInfo = 0; + } + } + } + + public static boolean isRunning() { + return IsRunning; + } + + public static void setRunning(boolean isRunning) { + IsRunning = isRunning; + } + + public static long getCurrent() { + return current; + } + + public static long getElapsedInfo() { + return elapsedInfo; + } + + public static long getElapsed() { + return elapsed; + } + + public static long getPrevious() { + return previous; + } + + public static long getTimeTicks() { + return timeTicks; + } + + public static long getTimeFps() { + return timeFps; + } + + public static int getFPS() { + return FPS; + } + + public static int getTICKS() { + return TICKS; + } + + public static int getLAST_TICKS() { + return LAST_TICKS; + } + + public static int getLAST_FPS() { + return LAST_FPS; + } + + public static String getTitle() { + return TITLE; + } + + public static int getWidth() { + return width; + } + + public static int getHeight() { + return height; + } + + public static Game getGame() { + return game; + } + +} diff --git a/VBO/src/main/Update.java b/VBO/src/main/Update.java new file mode 100644 index 0000000..14ca726 --- /dev/null +++ b/VBO/src/main/Update.java @@ -0,0 +1,111 @@ +package main; +import org.lwjgl.input.*; +import org.lwjgl.opengl.*; + + +public class Update { + + private static float xDir,yDir,zDir; + private static float speed = 0.01f; + private static float xa = 0,ya = 0,za = 0; + + + /** + * @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{ + + } + } + } + + /** + * @Info Fonction permettant de gerer les action du clavier + */ + public static void updateKeyboard(){ + xDir = 0; + yDir = 0; + zDir = 0; + while(Keyboard.next()){ + if(Keyboard.getEventKeyState()){ + if(Keyboard.getEventKey() == Keyboard.KEY_ESCAPE){ + Main.setRunning(false); + } + if(Keyboard.getEventKey() == Keyboard.KEY_F2){ + Mouse.setGrabbed(!Mouse.isGrabbed()); + } + }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; + } + + xa += xDir * Math.cos(Math.toRadians(Camera.getRotation().y)) - zDir * Math.sin(Math.toRadians(Camera.getRotation().y)); + ya += yDir; + za += zDir * Math.cos(Math.toRadians(Camera.getRotation().y)) + xDir * Math.sin(Math.toRadians(Camera.getRotation().y)); + + Camera.move(xa,ya,za); + + xa *= 0.9f; + ya *= 0.9f; + za *= 0.9f; + + if(Display.isCloseRequested()){ + Main.setRunning(false); + } + } + + /** + * @Info Fonction de mettre a jour le display et d'autre choses predefinie + */ + public static void update(){ + Main.getGame().update(); + Display.update(); + } + +} diff --git a/VBO/src/main/VBO.java b/VBO/src/main/VBO.java new file mode 100644 index 0000000..10d148b --- /dev/null +++ b/VBO/src/main/VBO.java @@ -0,0 +1,119 @@ +package main; +import static org.lwjgl.opengl.GL15.*; +import static org.lwjgl.opengl.GL11.*; +import static org.lwjgl.opengl.GL13.*; +import static org.lwjgl.opengl.GL20.*; + +import java.nio.*; + +import org.lwjgl.*; + +public class VBO { + + + private int bufferSize = 0; + private int vboID = 0; + private FloatBuffer buffer; + + public VBO(){ + this.vboID = createVBO(); + } + + /** + * @Info creer le vbo + */ + public int createVBO(){ + return glGenBuffers(); + } + + /** + * @param a + * @Info () doit contenir par ligne au moins l'axe (x,y,z) + les couleurs (r,g,b,a) + * Ex: getFloatBufferByFloatArray(new float[]{x,y,z,r,g,b,a}) + * + * + */ + + public void clearBuffer(){ + buffer.clear(); + } + + public void addDataByFloatArray(float[] a){ + if(a == null)return; + if(buffer != null){ + int size = buffer.limit(); + float[] previousBuffer = new float[size]; + buffer.get(previousBuffer); + buffer.clear(); + buffer = BufferUtils.createFloatBuffer(size + a.length); + buffer.put(previousBuffer).put(a); + buffer.flip(); + }else{ + buffer = BufferUtils.createFloatBuffer(a.length); + buffer.put(a); + buffer.flip(); + } + } + + /** + * @param a + * @Info () doit contenir par ligne au moins l'axe (x,y,z) + les couleurs (r,g,b,a) + * Ex: getFloatBufferbyFloatList(x,y,z,r,g,b,a) + * + * + */ + public void addDataByFloatList(float... a){ + if(a == null)return; + if(buffer != null){ + int size = buffer.limit(); + float[] previousBuffer = new float[size]; + buffer.get(previousBuffer); + buffer.clear(); + buffer = BufferUtils.createFloatBuffer(size + a.length); + buffer.put(previousBuffer).put(a); + buffer.flip(); + }else{ + buffer = BufferUtils.createFloatBuffer(a.length); + buffer.put(a); + buffer.flip(); + } + } + + /** + * @param id + * @param data + * @Info Stocke le FloatBuffer dans le GPU + */ + public void bufferData(){ + glBindBuffer(GL_ARRAY_BUFFER, vboID); + glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); + bufferSize = buffer.limit() / 7;// 7 = 3 vertex(x,y,z) + 4 color (r,g,b,a) + } + + /** + * @Info fait le rendu du vbo + */ + public void renderVBO(){ + glBindBuffer(GL_ARRAY_BUFFER, vboID); + + glEnableVertexAttribArray(0); + glEnableVertexAttribArray(1); + + 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); + + } + + /** + * @Info detruit le vbo + */ + public void destroyVBO(){ + glDeleteBuffers(vboID); + } +} diff --git a/VBO/src/math/Color4f.java b/VBO/src/math/Color4f.java new file mode 100644 index 0000000..21649f7 --- /dev/null +++ b/VBO/src/math/Color4f.java @@ -0,0 +1,105 @@ +package math; + +public class Color4f { + + private float r,g,b,a; + + public Color4f(float r,float g,float b,float a){ + this.r = r; + this.b = b; + this.g = g; + this.a = a; + } + + public Color4f(float r,float g,float b){ + this.r = r; + this.b = b; + this.g = g; + this.a = 1; + } + + public Color4f(Color4f c){ + this.r = c.getR(); + this.b = c.getG(); + this.g = c.getB(); + this.a = c.getA(); + } + + public static Color4f interpolate(Color4f ca, Color4f cb, float t){ + float r = ca.r + (cb.r - ca.r) * t; + float g = ca.g + (cb.g - ca.g) * t; + float b = ca.b + (cb.b - ca.b) * t; + float a = ca.a + (cb.a - ca.a) * t; + return new Color4f(r,g,b,a); + } + + public Color4f add(Color4f c){ + r += c.getR(); + g += c.getG(); + b += c.getB(); + a += c.getA(); + return this; + } + + public Color4f sub(Color4f c){ + r -= c.getR(); + g -= c.getG(); + b -= c.getB(); + a -= c.getA(); + return this; + } + + public Color4f mul(float c){ + r *= c; + g *= c; + b *= c; + a *= c; + return this; + } + + public Color4f copy(){ + return new Color4f(r,g,b,a); + } + + public void div(Color4f c){ + r /= c.getR(); + g /= c.getG(); + b /= c.getB(); + a /= c.getA(); + } + + public float getR() { + return r; + } + + public void setR(float r) { + this.r = r; + } + + public float getG() { + return g; + } + + public void setG(float g) { + this.g = g; + } + + public float getB() { + return b; + } + + public void setB(float b) { + this.b = b; + } + + public float getA() { + return a; + } + + public void setA(float a) { + this.a = a; + } + + + +} diff --git a/VBO/src/math/Vector2f.java b/VBO/src/math/Vector2f.java new file mode 100644 index 0000000..8663702 --- /dev/null +++ b/VBO/src/math/Vector2f.java @@ -0,0 +1,31 @@ +package math; + + +public class Vector2f { + + public float x,y; + + public Vector2f(float x,float y){ + this.x = x; + this.y = y; + } + + public float getX() { + return x; + } + + public void setX(float x) { + this.x = x; + } + + public float getY() { + return y; + } + + public void setY(float y) { + this.y = y; + } + + + +} \ No newline at end of file diff --git a/VBO/src/math/Vector3f.java b/VBO/src/math/Vector3f.java new file mode 100644 index 0000000..9170d11 --- /dev/null +++ b/VBO/src/math/Vector3f.java @@ -0,0 +1,196 @@ +package math; + + +public class Vector3f { + + public float x, y, z; + + public Vector3f() { + this(0, 0, 0); + } + + public Vector3f(Vector3f v) { + this(v.x, v.y, v.z); + } + + public Vector3f(float x, float y, float z) { + this.x = x; + this.y = y; + this.z = z; + } + + public float length() { + return (float) Math.sqrt(x * x + y * y + z * z); + } + + public Vector3f normalize() { + x /= length(); + y /= length(); + z /= length(); + + return this; + } + + public Vector3f add(Vector3f vec) { + x += vec.getX(); + y += vec.getY(); + z += vec.getZ(); + + return this; + } + + public Vector3f check(){ + float max = Math.max(Math.max(x, y),z); + float min = Math.min(Math.min(x, y),z); + + float absMax = Math.abs(max - 1); + float absMin = Math.abs(min); + + float v = 0; + + if(absMax>absMin)v=min; + else v=max; + + int rv = 1; + + if(v<0.5f)rv=-1; + + return new Vector3f(v == x ? rv : 0,v == y ? rv : 0,v == z ? rv : 0); + } + + public Vector3f copy(){ + return new Vector3f(this); + } + + public Vector3f sub(Vector3f vec) { + x -= vec.getX(); + y -= vec.getY(); + z -= vec.getZ(); + + return this; + } + + public void set(Vector3f vec){ + this.x = vec.x; + this.y = vec.y; + this.z = vec.z; + } + + public Vector3f mul(Vector3f vec) { + x *= vec.getX(); + y *= vec.getY(); + z *= vec.getZ(); + + return this; + } + + public Vector3f div(Vector3f vec) { + x /= vec.getX(); + y /= vec.getY(); + z /= vec.getZ(); + + return this; + } + + + public Vector3f add(float v) { + x += v; + y += v; + z += v; + + return this; + } + + public Vector3f add(float x,float y, float z) { + this.x += x; + this.y += y; + this.z += z; + + return this; + } + + public Vector3f sub(float v) { + x -= v; + y -= v; + z -= v; + + return this; + } + + public Vector3f mul(float v) { + x *= v; + y *= v; + z *= v; + + return this; + } + + public Vector3f div(float v) { + x /= v; + y /= v; + z /= v; + + return this; + } + + // ---- X + public float getX() { + return x; + } + + public void setX(float x) { + this.x = x; + } + + public Vector3f addX(float v) { + x += v; + return this; + } + public Vector3f subX(float v) { + x -= v; + return this; + } + + + // ----- Y + public float getY() { + return y; + } + + public void setY(float y) { + this.y = y; + } + + public Vector3f addY(float v) { + y += v; + return this; + } + public Vector3f subY(float v) { + y -= v; + return this; + } + + + // ----- Z + public float getZ() { + return z; + } + + public void setZ(float z) { + this.z = z; + } + + public Vector3f addZ(float v) { + z += v; + return this; + } + public Vector3f subZ(float v) { + z -= v; + return this; + } + + public static float distance(Vector3f a,Vector3f b){ + return (float)Math.sqrt((Math.pow(b.getX()-a.getX(), 2))+(Math.pow(b.getY()-a.getY(), 2))+(Math.pow(b.getZ()-a.getZ(), 2))); + } + +} diff --git a/VBO/src/rendering/Shader.java b/VBO/src/rendering/Shader.java new file mode 100644 index 0000000..2e6ee6a --- /dev/null +++ b/VBO/src/rendering/Shader.java @@ -0,0 +1,77 @@ +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/world/Chunk.java b/VBO/src/world/Chunk.java new file mode 100644 index 0000000..d48a841 --- /dev/null +++ b/VBO/src/world/Chunk.java @@ -0,0 +1,261 @@ +package world; + +import blocks.*; +import main.*; +import math.*; + +public class Chunk { + + public final static int SIZE = 4; + private int x, y, z; + private VBO vbo; + private World world; + Block[][][] blocks; + private boolean IsLoad = false; + + public Chunk(int x, int y, int z, World world) { + this.x = x; + this.y = y; + this.z = z; + this.world = world; + this.blocks = new Block[SIZE][SIZE][SIZE]; + vbo = new VBO(); + } + + public void render() { + vbo.renderVBO(); + } + + public void update() { + + } + + public void createChunk(World world) { + this.world = world; + for (int i = 0; i < SIZE; i++) { + for (int j = 0; j < 2; j++) { + for (int k = 0; k < SIZE; k++) { + blocks[i][j][k] = Block.GRASS; + } + } + } + for (int i = 0; i < SIZE; i++) { + for (int j = 0; j < 2; j++) { + for (int k = 0; k < SIZE; k++) { + loopChunk(i, j, k); + } + } + } + + vbo.bufferData(); + IsLoad = true; + } + + public void updateChunk() { + vbo.clearBuffer(); + for (int i = 0; i < SIZE; i++) { + for (int j = 0; j < 2; j++) { + for (int k = 0; k < SIZE; k++) { + loopChunk(i, j, k); + } + } + } + vbo.bufferData(); + } + + public int getSIZE() { + return SIZE; + } + + public int getX() { + return x; + } + + public int getY() { + return y; + } + + public int getZ() { + return z; + } + + public Vector3f getPosition(){ + return new Vector3f(x,y,z); + } + + public Block getBlock(int x, int y, int z) { + if (x < 0 || y < 0 || z < 0 || x >= SIZE || y >= SIZE || z >= SIZE) + return null; + return blocks[x][y][z]; + } + + public void destroyChunk(){ + vbo.destroyVBO(); + } + + public void loopChunk(int x, int y, int z) { + int xx = this.x * SIZE + x; + int yy = this.y * SIZE + y; + int zz = this.z * SIZE + z; + boolean up = world.getBlock(xx, yy + 1, zz) == null; + boolean down = world.getBlock(xx, yy - 1, zz) == null; + boolean left = world.getBlock(xx - 1, yy, zz) == null; + boolean right = world.getBlock(xx + 1, yy, zz) == null; + boolean front = world.getBlock(xx, yy, zz - 1) == null; + boolean back = world.getBlock(xx, yy, zz + 1) == null; + if (!up && !down && !left && !right && !front && !back) + return; + if (blocks[x][y][z] == null) + return; + Block b = blocks[x][y][z]; + + float ao = 0.7f; + + // up + 1 = down - 1 = y + // left - 1 = right + 1 = x + // front - 1 = back + 1 = z + + if (up) { + // aa ab bb ba + float[] a = new float[] { 1, 1, 1, 1 }; + + if (world.getBlock(xx - 1, yy + 1, zz) != null + || world.getBlock(xx - 1, yy + 1, zz - 1) != null + || world.getBlock(xx, yy + 1, zz - 1) != null) + a[0] = ao; + if (world.getBlock(xx + 1, yy + 1, zz) != null + || world.getBlock(xx + 1, yy + 1, zz - 1) != null + || world.getBlock(xx, yy + 1, zz - 1) != null) + a[1] = ao; + if (world.getBlock(xx + 1, yy + 1, zz) != null + || world.getBlock(xx + 1, yy + 1, zz + 1) != null + || world.getBlock(xx, yy + 1, zz + 1) != null) + a[2] = ao; + if (world.getBlock(xx - 1, yy + 1, zz) != null + || world.getBlock(xx - 1, yy + 1, zz + 1) != null + || world.getBlock(xx, yy + 1, zz + 1) != null) + a[3] = ao; + + vbo.addDataByFloatArray(b.getDataUp(xx, yy, zz, a)); + } + if (down) { + float[] a = new float[] { 1, 1, 1, 1 }; + // ambient occlusion + + if (world.getBlock(xx - 1, yy - 1, zz) != null + || world.getBlock(xx - 1, yy - 1, zz - 1) != null + || world.getBlock(xx, yy - 1, zz - 1) != null) + a[1] = ao; + if (world.getBlock(xx + 1, yy - 1, zz) != null + || world.getBlock(xx + 1, yy - 1, zz - 1) != null + || world.getBlock(xx, yy - 1, zz - 1) != null) + a[0] = ao; + if (world.getBlock(xx + 1, yy - 1, zz) != null + || world.getBlock(xx + 1, yy - 1, zz + 1) != null + || world.getBlock(xx, yy - 1, zz + 1) != null) + a[3] = ao; + if (world.getBlock(xx - 1, yy - 1, zz) != null + || world.getBlock(xx - 1, yy - 1, zz + 1) != null + || world.getBlock(xx, yy - 1, zz + 1) != null) + a[2] = ao; + + // affiche la face si il n'y a rien en dessous + vbo.addDataByFloatArray(b.getDataDown(xx, yy, zz, a)); + } + if (left) { + float[] a = new float[] { 1, 1, 1, 1 }; + + if (world.getBlock(xx - 1, yy - 1, zz) != null + || world.getBlock(xx - 1, yy - 1, zz - 1) != null + || world.getBlock(xx - 1, yy, zz - 1) != null) + a[0] = ao; + if (world.getBlock(xx - 1, yy + 1, zz) != null + || world.getBlock(xx - 1, yy + 1, zz - 1) != null + || world.getBlock(xx - 1, yy, zz - 1) != null) + a[1] = ao; + if (world.getBlock(xx - 1, yy + 1, zz) != null + || world.getBlock(xx - 1, yy + 1, zz + 1) != null + || world.getBlock(xx - 1, yy, zz + 1) != null) + a[2] = ao; + if (world.getBlock(xx - 1, yy - 1, zz) != null + || world.getBlock(xx - 1, yy - 1, zz + 1) != null + || world.getBlock(xx - 1, yy, zz + 1) != null) + a[3] = ao; + + vbo.addDataByFloatArray(b.getDataLeft(xx, yy, zz, a)); + } + if (right) { + float[] a = new float[] { 1, 1, 1, 1 }; + + if (world.getBlock(xx + 1, yy - 1, zz) != null + || world.getBlock(xx + 1, yy - 1, zz - 1) != null + || world.getBlock(xx + 1, yy, zz - 1) != null) + a[1] = ao; + if (world.getBlock(xx + 1, yy + 1, zz) != null + || world.getBlock(xx + 1, yy + 1, zz - 1) != null + || world.getBlock(xx + 1, yy, zz - 1) != null) + a[0] = ao; + if (world.getBlock(xx + 1, yy + 1, zz) != null + || world.getBlock(xx + 1, yy + 1, zz + 1) != null + || world.getBlock(xx + 1, yy, zz + 1) != null) + a[3] = ao; + if (world.getBlock(xx + 1, yy - 1, zz) != null + || world.getBlock(xx + 1, yy - 1, zz + 1) != null + || world.getBlock(xx + 1, yy, zz + 1) != null) + a[2] = ao; + + vbo.addDataByFloatArray(b.getDataRight(xx, yy, zz, a)); + } + if (front) { + float[] a = new float[] { 1, 1, 1, 1 }; + + if (world.getBlock(xx, yy - 1, zz - 1) != null + || world.getBlock(xx - 1, yy - 1, zz - 1) != null + || world.getBlock(xx - 1, yy, zz - 1) != null) + a[0] = ao; + if (world.getBlock(xx, yy + 1, zz - 1) != null + || world.getBlock(xx - 1, yy + 1, zz - 1) != null + || world.getBlock(xx - 1, yy, zz - 1) != null) + a[3] = ao; + if (world.getBlock(xx, yy + 1, zz - 1) != null + || world.getBlock(xx + 1, yy + 1, zz - 1) != null + || world.getBlock(xx + 1, yy, zz - 1) != null) + a[2] = ao; + if (world.getBlock(xx, yy - 1, zz - 1) != null + || world.getBlock(xx + 1, yy - 1, zz - 1) != null + || world.getBlock(xx + 1, yy, zz - 1) != null) + a[1] = ao; + + vbo.addDataByFloatArray(b.getDataFront(xx, yy, zz, a)); + } + if (back) { + float[] a = new float[] { 1, 1, 1, 1 }; + + if (world.getBlock(xx, yy - 1, zz + 1) != null + || world.getBlock(xx - 1, yy - 1, zz + 1) != null + || world.getBlock(xx - 1, yy, zz + 1) != null) + a[1] = ao; + if (world.getBlock(xx, yy + 1, zz + 1) != null + || world.getBlock(xx - 1, yy + 1, zz + 1) != null + || world.getBlock(xx - 1, yy, zz + 1) != null) + a[2] = ao; + if (world.getBlock(xx, yy + 1, zz + 1) != null + || world.getBlock(xx + 1, yy + 1, zz + 1) != null + || world.getBlock(xx + 1, yy, zz + 1) != null) + a[3] = ao; + if (world.getBlock(xx, yy - 1, zz + 1) != null + || world.getBlock(xx + 1, yy - 1, zz + 1) != null + || world.getBlock(xx + 1, yy, zz + 1) != null) + a[0] = ao; + + vbo.addDataByFloatArray(b.getDataBack(xx, yy, zz, a)); + } + } + + public boolean isLoaded() { + return IsLoad; + } + + + +} diff --git a/VBO/src/world/World.java b/VBO/src/world/World.java new file mode 100644 index 0000000..3d10e1b --- /dev/null +++ b/VBO/src/world/World.java @@ -0,0 +1,101 @@ +package world; + +import java.util.*; + +import main.*; +import math.*; +import blocks.*; + +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 = 2; + + public ArrayList chunks = new ArrayList(); + + public World(long seed){ + this.seed= seed; + for(int x = 0;x < SIZE;x++){ + for(int y = 0;y < HEIGHT;y++){ + for(int z = 0;z < SIZE;z++){ + Chunk ch = new Chunk(x,y,z,this); + chunks.add(ch); + } + } + } + for(Chunk ch : chunks)ch.createChunk(this); + } + + public void update(){ + int xa = (int)((Camera.getPosition().getX()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) - VIEW_CHUNK; + int xb = (int)((Camera.getPosition().getX()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) + VIEW_CHUNK; + 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; + boolean isC = false; + for(int i = 0; i <= delta_x;i++){ + for(int k = 0;k <= delta_z;k++){ + for(int j = 0; j < HEIGHT; j++){ + if(getChunk((xa + i), 0, (za + k)) == null){ + Chunk ch = new Chunk((xa + i),j,(za + k),this); + chunks.add(ch); + isC = true; + } + } + } + } + for(int i = 0; i < chunks.size();i++){ + Chunk c = chunks.get(i); + if(c.getPosition().getX() < xa || c.getPosition().getX() > xb || c.getPosition().getZ() < za || c.getPosition().getZ() > zb){ + c.destroyChunk(); + chunks.remove(i); + break; + } + } + for(Chunk c : chunks){ + if(!c.isLoaded())c.createChunk(this); + c.update(); + } + System.gc(); + } + + public void render(){ + for(Chunk c : chunks){ + if(c.isLoaded()) + c.render(); + } + } + + public Chunk getChunk(int xc, int yc, int zc) { + Chunk c = null; + for(Chunk ch : chunks){ + if(ch.getX() == xc && ch.getY() == yc && ch.getZ() == zc){ + c = ch; + break; + } + } + return c; + } + + public Block getBlock(int x, int y, int z) { + int xc = (x / Chunk.SIZE); + int zc = (z / Chunk.SIZE); + int yc = (y / Chunk.SIZE); + + Chunk chunk = getChunk(xc, yc, zc); + if(chunk == null)return null; + + int xb = x % Chunk.SIZE; + int yb = y % Chunk.SIZE; + int zb = z % Chunk.SIZE; + + return chunk.getBlock(xb, yb, zb); + } + + + +}