diff --git a/bin/Scene/Objects/Cube.class b/bin/Scene/Objects/Cube.class new file mode 100644 index 0000000..44d5895 Binary files /dev/null and b/bin/Scene/Objects/Cube.class differ diff --git a/bin/Scene/Objects/Ground.class b/bin/Scene/Objects/Ground.class index f707049..7a10030 100644 Binary files a/bin/Scene/Objects/Ground.class and b/bin/Scene/Objects/Ground.class differ diff --git a/bin/Scene/Objects/Human$1.class b/bin/Scene/Objects/Human$1.class index cd2ca04..cd9c533 100644 Binary files a/bin/Scene/Objects/Human$1.class and b/bin/Scene/Objects/Human$1.class differ diff --git a/bin/Scene/Objects/Human$2.class b/bin/Scene/Objects/Human$2.class index bf289d5..80dde9a 100644 Binary files a/bin/Scene/Objects/Human$2.class and b/bin/Scene/Objects/Human$2.class differ diff --git a/bin/Scene/Objects/Human.class b/bin/Scene/Objects/Human.class index 5eaca92..99e84a1 100644 Binary files a/bin/Scene/Objects/Human.class and b/bin/Scene/Objects/Human.class differ diff --git a/bin/Scene/Scene$1.class b/bin/Scene/Scene$1.class index b3b7583..e1f0c26 100644 Binary files a/bin/Scene/Scene$1.class and b/bin/Scene/Scene$1.class differ diff --git a/bin/Scene/Scene.class b/bin/Scene/Scene.class index 966a1ad..8c0aade 100644 Binary files a/bin/Scene/Scene.class and b/bin/Scene/Scene.class differ diff --git a/bin/Scene/base/IHittable.class b/bin/Scene/base/IHittable.class new file mode 100644 index 0000000..4bdca12 Binary files /dev/null and b/bin/Scene/base/IHittable.class differ diff --git a/bin/Scene/base/SceneObject.class b/bin/Scene/base/SceneObject.class index 17f330a..864d757 100644 Binary files a/bin/Scene/base/SceneObject.class and b/bin/Scene/base/SceneObject.class differ diff --git a/bin/Scene/skybox/Skybox.class b/bin/Scene/skybox/Skybox.class new file mode 100644 index 0000000..ef06ac1 Binary files /dev/null and b/bin/Scene/skybox/Skybox.class differ diff --git a/bin/base/objects3D/TexCube.class b/bin/base/objects3D/TexCube.class index d3b951b..c7e4f7c 100644 Binary files a/bin/base/objects3D/TexCube.class and b/bin/base/objects3D/TexCube.class differ diff --git a/bin/main/Camera.class b/bin/main/Camera.class index 1547277..c192087 100644 Binary files a/bin/main/Camera.class and b/bin/main/Camera.class differ diff --git a/bin/main/Engine.class b/bin/main/Engine.class index fd53377..e390596 100644 Binary files a/bin/main/Engine.class and b/bin/main/Engine.class differ diff --git a/bin/main/Main$1.class b/bin/main/Main$1.class index a25d9d8..747e92f 100644 Binary files a/bin/main/Main$1.class and b/bin/main/Main$1.class differ diff --git a/bin/main/Main.class b/bin/main/Main.class index e4055c0..0749d02 100644 Binary files a/bin/main/Main.class and b/bin/main/Main.class differ diff --git a/bin/main/TextureLoader.class b/bin/main/TextureLoader.class new file mode 100644 index 0000000..14e7cb6 Binary files /dev/null and b/bin/main/TextureLoader.class differ diff --git a/res/skybox/back.png b/res/skybox/back.png new file mode 100644 index 0000000..a03d76e Binary files /dev/null and b/res/skybox/back.png differ diff --git a/res/skybox/down.png b/res/skybox/down.png new file mode 100644 index 0000000..ab57d1b Binary files /dev/null and b/res/skybox/down.png differ diff --git a/res/skybox/front.png b/res/skybox/front.png new file mode 100644 index 0000000..f9e9e11 Binary files /dev/null and b/res/skybox/front.png differ diff --git a/res/skybox/left.png b/res/skybox/left.png new file mode 100644 index 0000000..4267266 Binary files /dev/null and b/res/skybox/left.png differ diff --git a/res/skybox/right.png b/res/skybox/right.png new file mode 100644 index 0000000..ad1abfd Binary files /dev/null and b/res/skybox/right.png differ diff --git a/res/skybox/up.png b/res/skybox/up.png new file mode 100644 index 0000000..36a2f12 Binary files /dev/null and b/res/skybox/up.png differ diff --git a/res/skybox_old/back.png b/res/skybox_old/back.png new file mode 100644 index 0000000..38cec73 Binary files /dev/null and b/res/skybox_old/back.png differ diff --git a/res/skybox_old/bottom.png b/res/skybox_old/bottom.png new file mode 100644 index 0000000..fba547e Binary files /dev/null and b/res/skybox_old/bottom.png differ diff --git a/res/skybox_old/front.png b/res/skybox_old/front.png new file mode 100644 index 0000000..4cb5662 Binary files /dev/null and b/res/skybox_old/front.png differ diff --git a/res/skybox_old/left.png b/res/skybox_old/left.png new file mode 100644 index 0000000..aa0b084 Binary files /dev/null and b/res/skybox_old/left.png differ diff --git a/res/skybox_old/right.png b/res/skybox_old/right.png new file mode 100644 index 0000000..2c16f15 Binary files /dev/null and b/res/skybox_old/right.png differ diff --git a/res/skybox_old/top.png b/res/skybox_old/top.png new file mode 100644 index 0000000..fcff1f9 Binary files /dev/null and b/res/skybox_old/top.png differ diff --git a/src/Scene/Objects/Cube.java b/src/Scene/Objects/Cube.java new file mode 100644 index 0000000..3cd84b1 --- /dev/null +++ b/src/Scene/Objects/Cube.java @@ -0,0 +1,52 @@ +package Scene.Objects; + +import Scene.base.SceneObject; +import base.GraphicsObjects.Point4f; +import base.GraphicsObjects.Vector4f; +import base.objects3D.TexCube; +import org.lwjgl.opengl.GL11; +import org.newdawn.slick.Color; +import org.newdawn.slick.opengl.Texture; + +import java.util.HashMap; + +/** + * @Author: WangYuyang + * @Date: 2021/11/5-23:09 + * @Project: Assignment3 + * @Package: Scene.Objects + * @Description: + **/ +public class Cube extends SceneObject { + private TexCube cube = new TexCube(); + + public Cube(Point4f origin, Point4f position, Vector4f scale) { + super(origin, position, scale); + } + + public Cube(Point4f origin, Point4f position, Vector4f scale, HashMap textures) { + super(origin, position, scale, textures); + } + + public Cube(Point4f origin, Point4f position, Vector4f scale, Vector4f rotation, HashMap textures) { + super(origin, position, scale, rotation, textures); + } + + @Override + public void draw() { + GL11.glTexParameteri( + GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, + GL11.GL_REPEAT); + GL11.glTexParameteri( + GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, + GL11.GL_REPEAT); + Color.white.bind(); + //bind texture + getTextures().get("default_dirt").bind(); + GL11.glEnable(GL11.GL_TEXTURE_2D); + GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST); + cube.DrawTexCube(1f); + GL11.glDisable(GL11.GL_TEXTURE_2D); + + } +} diff --git a/src/Scene/Objects/Ground.java b/src/Scene/Objects/Ground.java index 3eaffcf..4553406 100644 --- a/src/Scene/Objects/Ground.java +++ b/src/Scene/Objects/Ground.java @@ -18,7 +18,7 @@ import java.util.HashMap; * @Description: **/ public class Ground extends SceneObject { - private TexCube MyGrid = new TexCube(); + private TexCube cube = new TexCube(); public Ground(Point4f origin, Point4f position, Vector4f scale) { super(origin, position, scale); @@ -47,7 +47,7 @@ public class Ground extends SceneObject { getTextures().get("default_dirt").bind(); GL11.glEnable(GL11.GL_TEXTURE_2D); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST); - MyGrid.DrawTexCube(10f); + cube.DrawTexCube(10f); GL11.glDisable(GL11.GL_TEXTURE_2D); } diff --git a/src/Scene/Objects/Human.java b/src/Scene/Objects/Human.java index f3c9493..2617833 100644 --- a/src/Scene/Objects/Human.java +++ b/src/Scene/Objects/Human.java @@ -7,7 +7,9 @@ import base.GraphicsObjects.Vector4f; import base.objects3D.Cylinder; import base.objects3D.Sphere; import base.objects3D.TexSphere; +import main.Camera; import main.Engine; +import org.lwjgl.input.Keyboard; import org.lwjgl.opengl.GL11; import org.newdawn.slick.Color; import org.newdawn.slick.opengl.Texture; @@ -45,7 +47,7 @@ public class Human extends SceneObject { private long walkStartTime; private int stopCount = 0; private int angle = 0; - private int angle_target = 0; + public static int angle_target = 0; private Boolean isJumping = false; private float jump_height = 0f; @@ -60,6 +62,10 @@ public class Human extends SceneObject { @Override public void run() { while (true) { + if(Math.abs(angle - angle_target) > 200){ + angle = angle_target; + } + if (angle > angle_target) { angle -= 1f; } else if (angle < angle_target) { @@ -148,6 +154,8 @@ public class Human extends SceneObject { @Override public void draw() { + checkInput(); + GL11.glTranslatef(0, jump_height, 0); Boolean GoodAnimation = true; float theta_face = (float) (delta * 2 * Math.PI); @@ -636,6 +644,91 @@ public class Human extends SceneObject { } } + private void checkInput() { + Boolean KEY_W = Keyboard.isKeyDown(Keyboard.KEY_W); + Boolean KEY_A = Keyboard.isKeyDown(Keyboard.KEY_A); + Boolean KEY_S = Keyboard.isKeyDown(Keyboard.KEY_S); + Boolean KEY_D = Keyboard.isKeyDown(Keyboard.KEY_D); + + Human human = (Human) this; + Integer speed = 10; + float speedX = speed / human.getScale().x; + float speedY = speed / human.getScale().y; + float speedZ = speed / human.getScale().z; + Boolean hasKeyDown = false; + Vector4f move = new Vector4f(); + if (KEY_W) { + Vector4f tmp = new Vector4f(); + tmp = tmp.PlusVector(new Vector4f(0, 0, speedZ, 0)); + hasKeyDown = true; + float angle = Camera.rotation.y - 180; + float new_x = (float) (tmp.length() * Math.sin(Math.toRadians(angle))); + float new_z = (float) (tmp.length() * Math.cos(Math.toRadians(angle))); + tmp.x = -new_x; + tmp.z = new_z; +// System.out.println(tmp); + move = tmp.PlusVector(move); + human.setAngle(-angle); + } + if (KEY_D) { + Vector4f tmp = new Vector4f(); + tmp = tmp.PlusVector(new Vector4f(-speedX, 0, 0, 0)); +// System.out.println(tmp.length()); + hasKeyDown = true; + float angle = 180 - Camera.rotation.y - 90; + float new_x = (float) (tmp.length() * Math.sin(Math.toRadians(angle))); + float new_z = (float) (tmp.length() * Math.cos(Math.toRadians(angle))); + tmp.x = new_x; + tmp.z = new_z; +// System.out.println(tmp); + move = tmp.PlusVector(move); + human.setAngle(angle); + } + if (KEY_S) { + Vector4f tmp = new Vector4f(); + tmp = tmp.PlusVector(new Vector4f(0, 0, -speedZ, 0)); + hasKeyDown = true; + float angle = Camera.rotation.y - 180; + float new_x = (float) (tmp.length() * Math.sin(Math.toRadians(angle))); + float new_z = (float) (tmp.length() * Math.cos(Math.toRadians(angle))); + tmp.x = new_x; + tmp.z = -new_z; +// System.out.println(tmp); + move = tmp.PlusVector(move); + human.setAngle(-angle - 180); + } + if (KEY_A) { + Vector4f tmp = new Vector4f(); + tmp = tmp.PlusVector(new Vector4f(speedX, 0, 0, 0)); + hasKeyDown = true; + float angle = 180 - Camera.rotation.y + 90; + float new_x = (float) (tmp.length() * Math.sin(Math.toRadians(angle))); + float new_z = (float) (tmp.length() * Math.cos(Math.toRadians(angle))); + tmp.x = new_x; + tmp.z = new_z; + move = tmp.PlusVector(move); + human.setAngle(angle); + } + if (Keyboard.isKeyDown(Keyboard.KEY_RETURN)) { + move = move.PlusVector(new Vector4f(0, speedY, 0, 0)); + } + if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) { + human.jump(500); + } + if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) { + move = move.PlusVector(new Vector4f(0, -speedY, 0, 0)); + } + human.move(move); +// human.setPosition(new Point4f(posn_x * 3.0f, 0.0f, posn_y * 3.0f, 0)); + if (hasKeyDown) { + human.walk(); + + } + { + human.stop(); + } + } + } diff --git a/src/Scene/Scene.java b/src/Scene/Scene.java index 4c78f17..760703f 100644 --- a/src/Scene/Scene.java +++ b/src/Scene/Scene.java @@ -4,6 +4,7 @@ import Scene.Objects.*; import Scene.base.IDrawListener; import Scene.base.SceneManager; import Scene.base.SceneObject; +import Scene.skybox.Skybox; import base.GraphicsObjects.Point4f; import base.GraphicsObjects.Vector4f; import main.Camera; @@ -38,27 +39,27 @@ public class Scene { new Vector4f(2000f, 1f, 2000f, 0), textures )); - sceneManager.addSceneObject(new Ground( - new Point4f(0, 2000, 0, 0), - new Point4f(0, 0, 0, 0), - new Vector4f(2000f, 1f, 2000f, 0), - textures - )); - sceneManager.addSceneObject(new Wall( - new Point4f(0, 1000f, 2000, 0), - new Point4f(0, 0, 0, 0), - new Vector4f(2000f, 1f, 1000f, 0), - new Vector4f(1, 0, 0, 90), - textures - )); - - sceneManager.addSceneObject(new Wall( - new Point4f(0, 1000f, -2000, 0), - new Point4f(0, 0, 0, 0), - new Vector4f(2000f, 1f, 1000f, 0), - new Vector4f(1, 0, 0, 90), - textures - )); +// sceneManager.addSceneObject(new Ground( +// new Point4f(0, 2000, 0, 0), +// new Point4f(0, 0, 0, 0), +// new Vector4f(2000f, 1f, 2000f, 0), +// textures +// )); +// sceneManager.addSceneObject(new Wall( +// new Point4f(0, 1000f, 2000, 0), +// new Point4f(0, 0, 0, 0), +// new Vector4f(2000f, 1f, 1000f, 0), +// new Vector4f(1, 0, 0, 90), +// textures +// )); +// +// sceneManager.addSceneObject(new Wall( +// new Point4f(0, 1000f, -2000, 0), +// new Point4f(0, 0, 0, 0), +// new Vector4f(2000f, 1f, 1000f, 0), +// new Vector4f(1, 0, 0, 90), +// textures +// )); sceneManager.addSceneObject(new TNT_Door( new Point4f(0, 50f, 200, 0), @@ -74,98 +75,23 @@ public class Scene { textures )); + sceneManager.addSceneObject(new Skybox( + new Point4f(0, 0, 0, 0), + new Point4f(0, 0, 0, 0), + new Vector4f(10000f, 10000f, 10000f, 0), + textures + )); + } public static void drawAll(SceneManager sceneManager, Integer delta) { - Boolean KEY_W = Keyboard.isKeyDown(Keyboard.KEY_W); - Boolean KEY_A = Keyboard.isKeyDown(Keyboard.KEY_A); - Boolean KEY_S = Keyboard.isKeyDown(Keyboard.KEY_S); - Boolean KEY_D = Keyboard.isKeyDown(Keyboard.KEY_D); + sceneManager.drawAll(new IDrawListener() { @Override public void beforeEachDraw(SceneObject object) { GL11.glPushMatrix(); - if (object instanceof Human) { - Human human = (Human) object; - Integer speed = 10; - float speedX = speed / human.getScale().x; - float speedY = speed / human.getScale().y; - float speedZ = speed / human.getScale().z; - Boolean hasKeyDown = false; - Vector4f move = new Vector4f(); - if (KEY_W) { - Vector4f tmp = new Vector4f(); - tmp = tmp.PlusVector(new Vector4f(0, 0, speedZ, 0)); - hasKeyDown = true; - float angle = Camera.rotation.y - 180; - float new_x = (float) (tmp.length() * Math.sin(Math.toRadians(angle))); - float new_z = (float) (tmp.length() * Math.cos(Math.toRadians(angle))); - tmp.x = -new_x; - tmp.z = new_z; -// System.out.println(tmp); - move = tmp.PlusVector(move); - human.setAngle(-angle); - } - if (KEY_D) { - Vector4f tmp = new Vector4f(); - tmp = tmp.PlusVector(new Vector4f(-speedX, 0, 0, 0)); -// System.out.println(tmp.length()); - hasKeyDown = true; - float angle = 180 - Camera.rotation.y - 90; - float new_x = (float) (tmp.length() * Math.sin(Math.toRadians(angle))); - float new_z = (float) (tmp.length() * Math.cos(Math.toRadians(angle))); - tmp.x = new_x; - tmp.z = new_z; -// System.out.println(tmp); - move = tmp.PlusVector(move); - human.setAngle(angle); - } - if (KEY_S) { - Vector4f tmp = new Vector4f(); - tmp = tmp.PlusVector(new Vector4f(0, 0, -speedZ, 0)); - hasKeyDown = true; - float angle = Camera.rotation.y - 180; - float new_x = (float) (tmp.length() * Math.sin(Math.toRadians(angle))); - float new_z = (float) (tmp.length() * Math.cos(Math.toRadians(angle))); - tmp.x = new_x; - tmp.z = -new_z; -// System.out.println(tmp); - move = tmp.PlusVector(move); - human.setAngle(-angle - 180); - } - if (KEY_A) { - Vector4f tmp = new Vector4f(); - tmp = tmp.PlusVector(new Vector4f(speedX, 0, 0, 0)); - hasKeyDown = true; - float angle = 180 - Camera.rotation.y + 90; - float new_x = (float) (tmp.length() * Math.sin(Math.toRadians(angle))); - float new_z = (float) (tmp.length() * Math.cos(Math.toRadians(angle))); - tmp.x = new_x; - tmp.z = new_z; - move = tmp.PlusVector(move); - human.setAngle(angle); - } - if (Keyboard.isKeyDown(Keyboard.KEY_RETURN)) { - move = move.PlusVector(new Vector4f(0, speedY, 0, 0)); - } - if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) { - human.jump(500); - } - if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) { - move = move.PlusVector(new Vector4f(0, -speedY, 0, 0)); - } - human.move(move); -// human.setPosition(new Point4f(posn_x * 3.0f, 0.0f, posn_y * 3.0f, 0)); - if (hasKeyDown) { - human.walk(); - - } - { - human.stop(); - } - } } @Override diff --git a/src/Scene/base/IHittable.java b/src/Scene/base/IHittable.java new file mode 100644 index 0000000..3ebae4f --- /dev/null +++ b/src/Scene/base/IHittable.java @@ -0,0 +1,12 @@ +package Scene.base; + +/** + * @Author: WangYuyang + * @Date: 2021/11/5-22:33 + * @Project: Assignment3 + * @Package: Scene.base + * @Description: + **/ +public interface IHittable { + +} diff --git a/src/Scene/base/SceneObject.java b/src/Scene/base/SceneObject.java index 33549b2..6f64499 100644 --- a/src/Scene/base/SceneObject.java +++ b/src/Scene/base/SceneObject.java @@ -22,6 +22,7 @@ public abstract class SceneObject implements IDrawable, IMovable, IScalable { private Vector4f rotation = new Vector4f(); private HashMap textures = new LinkedHashMap<>(); + public SceneObject(Point4f origin, Point4f position, Vector4f scale) { this.origin = new Point4f(origin.x, origin.y, origin.z, 0); this.position = new Point4f(position.x, position.y, position.z, 0); diff --git a/src/Scene/skybox/Skybox.java b/src/Scene/skybox/Skybox.java new file mode 100644 index 0000000..723c461 --- /dev/null +++ b/src/Scene/skybox/Skybox.java @@ -0,0 +1,146 @@ +package Scene.skybox; + +import Scene.base.SceneObject; +import base.GraphicsObjects.Point4f; +import base.GraphicsObjects.Vector4f; +import base.objects3D.TexCube; +import org.lwjgl.opengl.GL11; +import org.lwjgl.opengl.GL12; +import org.newdawn.slick.Color; +import org.newdawn.slick.opengl.Texture; + +import java.util.HashMap; + +/** + * @Author: WangYuyang + * @Date: 2021/11/5-22:43 + * @Project: Assignment3 + * @Package: Scene.skybox + * @Description: + **/ +public class Skybox extends SceneObject { + private TexCube cube = new TexCube(); + + public Skybox(Point4f origin, Point4f position, Vector4f scale) { + super(origin, position, scale); + init(); + } + + public Skybox(Point4f origin, Point4f position, Vector4f scale, HashMap textures) { + super(origin, position, scale, textures); + init(); + } + + public Skybox(Point4f origin, Point4f position, Vector4f scale, Vector4f rotation, HashMap textures) { + super(origin, position, scale, rotation, textures); + init(); + } + + private void init() { + + } + + + @Override + public void draw() { + + GL11.glTexParameteri( + GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, + GL11.GL_REPEAT); + GL11.glTexParameteri( + GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, + GL11.GL_REPEAT); + Color.white.bind(); + //bind texture + + GL11.glEnable(GL11.GL_TEXTURE_2D); + GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST); + + GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE); + GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE); + getTextures().get("skybox/down").bind(); + GL11.glBegin(GL11.GL_QUADS); + GL11.glTexCoord2f(1, 1); + GL11.glVertex3f(-0.5f, -0.5f, -0.5f); + GL11.glTexCoord2f(1, 0); + GL11.glVertex3f(-0.5f, -0.5f, 0.5f); + GL11.glTexCoord2f(0, 0); + GL11.glVertex3f(0.5f, -0.5f, 0.5f); + GL11.glTexCoord2f(0, 1); + GL11.glVertex3f(0.5f, -0.5f, -0.5f); + GL11.glEnd(); + + GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE); + GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE); + getTextures().get("skybox/front").bind(); + GL11.glBegin(GL11.GL_QUADS); + GL11.glTexCoord2f(1, 1); + GL11.glVertex3f(0.5f, -0.5f, -0.5f); + GL11.glTexCoord2f(0, 1); + GL11.glVertex3f(-0.5f, -0.5f, -0.5f); + GL11.glTexCoord2f(0, 0); + GL11.glVertex3f(-0.5f, 0.5f, -0.5f); + GL11.glTexCoord2f(1, 0); + GL11.glVertex3f(0.5f, 0.5f, -0.5f); + GL11.glEnd(); + + GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE); + GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE); + getTextures().get("skybox/right").bind(); + GL11.glBegin(GL11.GL_QUADS); + GL11.glTexCoord2f(1, 1); + GL11.glVertex3f(-0.5f, -0.5f, -0.5f); + GL11.glTexCoord2f(0, 1); + GL11.glVertex3f(-0.5f, -0.5f, 0.5f); + GL11.glTexCoord2f(0, 0); + GL11.glVertex3f(-0.5f, 0.5f, 0.5f); + GL11.glTexCoord2f(1, 0); + GL11.glVertex3f(-0.5f, 0.5f, -0.5f); + GL11.glEnd(); + + GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE); + GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE); + getTextures().get("skybox/left").bind(); + GL11.glBegin(GL11.GL_QUADS); + GL11.glTexCoord2f(1, 1); + GL11.glVertex3f(0.5f, -0.5f, 0.5f); + GL11.glTexCoord2f(0, 1); + GL11.glVertex3f(0.5f, -0.5f, -0.5f); + GL11.glTexCoord2f(0, 0); + GL11.glVertex3f(0.5f, 0.5f, -0.5f); + GL11.glTexCoord2f(1, 0); + GL11.glVertex3f(0.5f, 0.5f, 0.5f); + GL11.glEnd(); + + GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE); + GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE); + getTextures().get("skybox/back").bind(); + GL11.glBegin(GL11.GL_QUADS); + GL11.glTexCoord2f(1, 1); + GL11.glVertex3f(-0.5f, -0.5f, 0.5f); + GL11.glTexCoord2f(0, 1); + GL11.glVertex3f(0.5f, -0.5f, 0.5f); + GL11.glTexCoord2f(0, 0); + GL11.glVertex3f(0.5f, 0.5f, 0.5f); + GL11.glTexCoord2f(1, 0); + GL11.glVertex3f(-0.5f, 0.5f, 0.5f); + GL11.glEnd(); + + GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE); + GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE); + getTextures().get("skybox/up").bind(); + GL11.glBegin(GL11.GL_QUADS); + GL11.glTexCoord2f(1, 1); + GL11.glVertex3f(-0.5f, 0.5f, -0.5f); + GL11.glTexCoord2f(0, 1); + GL11.glVertex3f(-0.5f, 0.5f, 0.5f); + GL11.glTexCoord2f(0, 0); + GL11.glVertex3f(0.5f, 0.5f, 0.5f); + GL11.glTexCoord2f(1, 0); + GL11.glVertex3f(0.5f, 0.5f, -0.5f); + GL11.glEnd(); + + + GL11.glDisable(GL11.GL_TEXTURE_2D); + } +} diff --git a/src/base/objects3D/TexCube.java b/src/base/objects3D/TexCube.java index 7c17d70..c5d7022 100644 --- a/src/base/objects3D/TexCube.java +++ b/src/base/objects3D/TexCube.java @@ -6,7 +6,24 @@ import org.lwjgl.opengl.GL11; public class TexCube { - + //a cube have 8 vertices so this is the coordinates of the vertices + Point4f vertices[] = {new Point4f(-1.0f, -1.0f, -1.0f, 0.0f), + new Point4f(-1.0f, -1.0f, 1.0f, 0.0f), + new Point4f(-1.0f, 1.0f, -1.0f, 0.0f), + new Point4f(-1.0f, 1.0f, 1.0f, 0.0f), + new Point4f(1.0f, -1.0f, -1.0f, 0.0f), + new Point4f(1.0f, -1.0f, 1.0f, 0.0f), + new Point4f(1.0f, 1.0f, -1.0f, 0.0f), + new Point4f(1.0f, 1.0f, 1.0f, 0.0f)}; + + //each face of the cube consists four edges, there are 6 faces in total + //so, this array has 6 element, each element contains the related edges + int faces[][] = {{1, 5, 4, 0}, + {2, 6, 4, 0}, + {3, 2, 0, 1}, + {6, 7, 5, 4}, + {7, 3, 1, 5}, + {7, 6, 2, 3}}; public TexCube() { } @@ -14,34 +31,6 @@ public class TexCube { // Implement using notes and looking at TexSphere public void DrawTexCube() { - //a cube have 8 vertices so this is the coordinates of the vertices - Point4f vertices[] = {new Point4f(-1.0f, -1.0f, -1.0f, 0.0f), - new Point4f(-1.0f, -1.0f, 1.0f, 0.0f), - new Point4f(-1.0f, 1.0f, -1.0f, 0.0f), - new Point4f(-1.0f, 1.0f, 1.0f, 0.0f), - new Point4f(1.0f, -1.0f, -1.0f, 0.0f), - new Point4f(1.0f, -1.0f, 1.0f, 0.0f), - new Point4f(1.0f, 1.0f, -1.0f, 0.0f), - new Point4f(1.0f, 1.0f, 1.0f, 0.0f)}; - - //each face of the cube consists four edges, there are 6 faces in total - //so, this array has 6 element, each element contains the related edges - int faces[][] = {{1, 5, 4, 0}, - {2, 6, 4, 0}, - {3, 2, 0, 1}, - {6, 7, 5, 4}, - {7, 3, 1, 5}, - {7, 6, 2, 3}}; - - //this array is used to calculate the normal for lighting -// int n_faces[][] = {{0, 4, 5, 1}, -// {0, 2, 6, 4}, -// {0, 1, 3, 2}, -// {4, 6, 7, 5}, -// {1, 5, 7, 3}, -// {2, 3, 7, 6}}; - - //INIT GL GL11.glBegin(GL11.GL_QUADS); @@ -87,26 +76,6 @@ public class TexCube { public void DrawTexCube(float number) { - //a cube have 8 vertices so this is the coordinates of the vertices - Point4f vertices[] = {new Point4f(-1.0f, -1.0f, -1.0f, 0.0f), - new Point4f(-1.0f, -1.0f, 1.0f, 0.0f), - new Point4f(-1.0f, 1.0f, -1.0f, 0.0f), - new Point4f(-1.0f, 1.0f, 1.0f, 0.0f), - new Point4f(1.0f, -1.0f, -1.0f, 0.0f), - new Point4f(1.0f, -1.0f, 1.0f, 0.0f), - new Point4f(1.0f, 1.0f, -1.0f, 0.0f), - new Point4f(1.0f, 1.0f, 1.0f, 0.0f)}; - - //each face of the cube consists four edges, there are 6 faces in total - //so, this array has 6 element, each element contains the related edges - int faces[][] = {{1, 5, 4, 0}, - {2, 6, 4, 0}, - {3, 2, 0, 1}, - {6, 7, 5, 4}, - {7, 3, 1, 5}, - {7, 6, 2, 3}}; - - GL11.glBegin(GL11.GL_QUADS); @@ -151,7 +120,57 @@ public class TexCube { GL11.glEnd(); } + // Implement using notes and looking at TexSphere + public void DrawTexCubeFace(int FaceNumber) { + // 0 bottom + // 1 front + // 2 right + // 3 left + // 4 back + // 5 top + //INIT GL + GL11.glBegin(GL11.GL_QUADS); + int face = FaceNumber; + + //a new way of calculating the normal vector by letting the face center minus the model center + Point4f face_center = new Point4f( + (vertices[faces[face][0]].x + vertices[faces[face][2]].x) / 2, + (vertices[faces[face][0]].y + vertices[faces[face][2]].y) / 2, + (vertices[faces[face][0]].z + vertices[faces[face][2]].z) / 2, + 0 + ); + Point4f model_center = new Point4f(0, 0, 0, 0); + Vector4f normal = face_center.MinusPoint(model_center); + + //calculate the normal vector using cross product which returns a vector + //that is perpendicular to these two vector +// Vector4f v = vertices[n_faces[face][1]].MinusPoint(vertices[n_faces[face][0]]); +// Vector4f w = vertices[n_faces[face][3]].MinusPoint(vertices[n_faces[face][0]]); +// Vector4f normal = v.cross(w).Normal(); + GL11.glNormal3f(normal.x, normal.y, normal.z); + + //top left + GL11.glTexCoord2f(0f, 1f); + GL11.glVertex3f(vertices[faces[face][0]].x, vertices[faces[face][0]].y, vertices[faces[face][0]].z); + + //top right + GL11.glTexCoord2f(1f, 1f); + GL11.glVertex3f(vertices[faces[face][1]].x, vertices[faces[face][1]].y, vertices[faces[face][1]].z); + + //right bottom + GL11.glTexCoord2f(1f, 0f); + GL11.glVertex3f(vertices[faces[face][2]].x, vertices[faces[face][2]].y, vertices[faces[face][2]].z); + + //left bottom + GL11.glTexCoord2f(0f, 0f); + GL11.glVertex3f(vertices[faces[face][3]].x, vertices[faces[face][3]].y, vertices[faces[face][3]].z); + + + GL11.glEnd(); + + + } } /* diff --git a/src/main/Camera.java b/src/main/Camera.java index a62fca1..d6588d1 100644 --- a/src/main/Camera.java +++ b/src/main/Camera.java @@ -1,5 +1,6 @@ package main; +import Scene.Objects.Human; import base.GraphicsObjects.Vector4f; import org.lwjgl.input.Keyboard; import org.lwjgl.input.Mouse; @@ -141,13 +142,16 @@ public class Camera { rotation.x = maxLookUp; } } - + if(!Mouse.isButtonDown(0)) + Human.angle_target = 180 - (int) rotation.y; glRotatef(rotation.x, 1, 0, 0); glRotatef(rotation.y, 0, 1, 0); glRotatef(-rotation.z, 0, 0, 1); glTranslatef(position.x, position.y, position.z); + + Main.engine.setOrtho(Camera.OrthoNumber); diff --git a/src/main/Engine.java b/src/main/Engine.java index 10ebe13..9be3241 100644 --- a/src/main/Engine.java +++ b/src/main/Engine.java @@ -9,7 +9,6 @@ import org.lwjgl.opengl.Display; import org.lwjgl.opengl.DisplayMode; import org.lwjgl.opengl.GL11; import org.newdawn.slick.opengl.Texture; -import org.newdawn.slick.opengl.TextureLoader; import org.newdawn.slick.util.ResourceLoader; import java.io.IOException; @@ -85,6 +84,7 @@ public class Engine { public void init() { Display.setVSyncEnabled(true); + glEnable(GL_DEPTH_TEST); loadTexture(); } @@ -117,7 +117,7 @@ public class Engine { // position // of the // light -// GL11.glEnable(GL11.GL_LIGHT0); // switch light #0 on // I've setup specific materials so in real light it will look abit strange + GL11.glEnable(GL11.GL_LIGHT0); // switch light #0 on // I've setup specific materials so in real light it will look abit strange GL11.glLight(GL11.GL_LIGHT1, GL11.GL_POSITION, lightPos); // specify the // position @@ -181,17 +181,17 @@ public class Engine { } public void setOrtho(int OrthoNumber) { - if (600 + OrthoNumber > 0 && 700 + OrthoNumber * 0.66 > 0) { +// if (600 + OrthoNumber > 0 && 700 + OrthoNumber * 0.66 > 0) { GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glLoadIdentity(); //Placing 0,0 at the center of the screen // GL11.glOrtho(1200 / 2 - OrthoNumber, OrthoNumber / 2, (800 / 2 - (OrthoNumber * 0.66f)), (OrthoNumber / 2 * 0.66f), 100000, -100000); // GL11.glOrtho(1200 - OrthoNumber, OrthoNumber, (800 - (OrthoNumber * 0.66f)), (OrthoNumber * 0.66f), 100000, -100000);F - gluPerspective((float) 60, Display.getWidth() / Display.getHeight(), 100f, 10000); + gluPerspective((float) 60, Display.getWidth() / Display.getHeight(), 100f, 17000); // GL11.glOrtho(-600 - OrthoNumber, 600 + OrthoNumber, -100 - OrthoNumber * 0.66, 700 + OrthoNumber * 0.66, 100000, -100000); enterModelView(); - } +// } } @@ -207,24 +207,7 @@ public class Engine { } private void loadTexture() { - //LOAD textures - try { - textures.put("awesomeface", TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/awesomeface.png"))); - - textures.put("default_dirt", TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/default_dirt.png"))); - textures.put("earthspace", TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/earthspace.png"))); - textures.put("tnt_side", TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/tnt_side.png"))); - textures.put("farming_wheat_8", TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/farming_wheat_8.png"))); - textures.put("wool_pink", TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/wool_pink.png"))); - textures.put("default_lava", TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/default_lava.png"))); - textures.put("default_stone", TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/default_stone.png"))); - textures.put("default_cobble", TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/default_cobble.png"))); -// textures.put("awesomeface",TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/awesomeface.png"))); -// textures.put("awesomeface",TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/awesomeface.png"))); - - } catch (IOException e) { - e.printStackTrace(); - } + TextureLoader.loadTexture(textures); System.out.println("Texture loaded okay "); } diff --git a/src/main/Main.java b/src/main/Main.java index ef50045..f999b58 100644 --- a/src/main/Main.java +++ b/src/main/Main.java @@ -4,11 +4,17 @@ import Scene.Scene; import Scene.base.SceneManager; import base.GraphicsObjects.Vector4f; import base.RenderProgramStatement; +import org.lwjgl.LWJGLException; import org.lwjgl.input.Keyboard; import org.lwjgl.opengl.Display; +import org.lwjgl.opengl.DisplayMode; -import static org.lwjgl.opengl.GL11.glPopMatrix; -import static org.lwjgl.opengl.GL11.glPushMatrix; +import java.util.logging.Level; +import java.util.logging.Logger; + +import static org.lwjgl.opengl.GL11.*; +import static org.lwjgl.opengl.GL11.glLoadIdentity; +import static org.lwjgl.util.glu.GLU.gluPerspective; /** * @Author: WangYuyang @@ -18,9 +24,10 @@ import static org.lwjgl.opengl.GL11.glPushMatrix; * @Description: **/ public class Main { - public static final int WIDTH = 1200; + public static final int WIDTH = 1600; public static final int HEIGHT = 800; private static final boolean DEBUG = false; + private static final boolean resizable = true; public static Engine engine; private static SceneManager sceneManager = new SceneManager(); private static Camera camera = new Camera(); @@ -30,7 +37,9 @@ public class Main { engine = new Engine(WIDTH, HEIGHT); engine.init(); engine.setOrtho(Camera.OrthoNumber); - engine.setLight(); + + + engine.enterModelView(); engine.initTimer(); @@ -48,6 +57,7 @@ public class Main { @Override public void render(int delta) { camera.update(); + engine.setLight(); Scene.drawAll(sceneManager, delta); } @@ -70,6 +80,35 @@ public class Main { 0, -300, -600, 0 )); } + + if (Keyboard.isKeyDown(Keyboard.KEY_EQUALS)) { + System.out.println("KEY_EQUALS"); + try { + Display.setFullscreen(!Display.isFullscreen()); + if (!Display.isFullscreen()) { + Display.setResizable(resizable); + Display.setDisplayMode(new DisplayMode(800, 600)); + glViewport(0, 0, Display.getWidth(), Display.getHeight()); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective((float) 60, Display.getWidth() / Display.getHeight(), 100f, 17000); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + } else { + glViewport(0, 0, Display.getWidth(), Display.getHeight()); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective((float) 60, Display.getWidth() / Display.getHeight(), 100f, 17000); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + } + } catch (LWJGLException ex) { + Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); + System.out.println("ERROR"); + engine.close(); + } + } + } engine.close(); diff --git a/src/main/TextureLoader.java b/src/main/TextureLoader.java new file mode 100644 index 0000000..4b9c737 --- /dev/null +++ b/src/main/TextureLoader.java @@ -0,0 +1,54 @@ +package main; + +import org.newdawn.slick.opengl.Texture; +import org.newdawn.slick.util.ResourceLoader; + +import java.io.IOException; +import java.util.HashMap; + +/** + * @Author: WangYuyang + * @Date: 2021/11/5-22:46 + * @Project: Assignment3 + * @Package: main + * @Description: + **/ +public class TextureLoader { + private static HashMap textures_map; + + public static void loadTexture(HashMap textures) { + //LOAD textures + textures_map = textures; + try { + loadTexture("awesomeface.png"); + loadTexture("default_dirt.png"); + loadTexture("earthspace.png"); + loadTexture("tnt_side.png"); + loadTexture("farming_wheat_8.png"); + loadTexture("wool_pink.png"); + loadTexture("default_lava.png"); + loadTexture("default_stone.png"); + loadTexture("default_cobble.png"); + + //sky box + loadTexture("skybox/back.png"); + loadTexture("skybox/down.png"); + loadTexture("skybox/front.png"); + loadTexture("skybox/left.png"); + loadTexture("skybox/right.png"); + loadTexture("skybox/up.png"); + + + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static void loadTexture(String filename) throws IOException { + String key_name = filename.split("\\.")[0]; + textures_map.put( + key_name, + org.newdawn.slick.opengl.TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/" + filename)) + ); + } +}