human-rotate

master
王宇洋 3 years ago
parent a2981e7064
commit 82ca1270be

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 429 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 776 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 382 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 816 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 709 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 504 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 203 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 605 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 379 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 708 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 415 KiB

@ -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<String, Texture> textures) {
super(origin, position, scale, textures);
}
public Cube(Point4f origin, Point4f position, Vector4f scale, Vector4f rotation, HashMap<String, Texture> 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);
}
}

@ -18,7 +18,7 @@ import java.util.HashMap;
* @Description: * @Description:
**/ **/
public class Ground extends SceneObject { public class Ground extends SceneObject {
private TexCube MyGrid = new TexCube(); private TexCube cube = new TexCube();
public Ground(Point4f origin, Point4f position, Vector4f scale) { public Ground(Point4f origin, Point4f position, Vector4f scale) {
super(origin, position, scale); super(origin, position, scale);
@ -47,7 +47,7 @@ public class Ground extends SceneObject {
getTextures().get("default_dirt").bind(); getTextures().get("default_dirt").bind();
GL11.glEnable(GL11.GL_TEXTURE_2D); 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_MAG_FILTER, GL11.GL_NEAREST);
MyGrid.DrawTexCube(10f); cube.DrawTexCube(10f);
GL11.glDisable(GL11.GL_TEXTURE_2D); GL11.glDisable(GL11.GL_TEXTURE_2D);
} }

@ -7,7 +7,9 @@ import base.GraphicsObjects.Vector4f;
import base.objects3D.Cylinder; import base.objects3D.Cylinder;
import base.objects3D.Sphere; import base.objects3D.Sphere;
import base.objects3D.TexSphere; import base.objects3D.TexSphere;
import main.Camera;
import main.Engine; import main.Engine;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color; import org.newdawn.slick.Color;
import org.newdawn.slick.opengl.Texture; import org.newdawn.slick.opengl.Texture;
@ -45,7 +47,7 @@ public class Human extends SceneObject {
private long walkStartTime; private long walkStartTime;
private int stopCount = 0; private int stopCount = 0;
private int angle = 0; private int angle = 0;
private int angle_target = 0; public static int angle_target = 0;
private Boolean isJumping = false; private Boolean isJumping = false;
private float jump_height = 0f; private float jump_height = 0f;
@ -60,6 +62,10 @@ public class Human extends SceneObject {
@Override @Override
public void run() { public void run() {
while (true) { while (true) {
if(Math.abs(angle - angle_target) > 200){
angle = angle_target;
}
if (angle > angle_target) { if (angle > angle_target) {
angle -= 1f; angle -= 1f;
} else if (angle < angle_target) { } else if (angle < angle_target) {
@ -148,6 +154,8 @@ public class Human extends SceneObject {
@Override @Override
public void draw() { public void draw() {
checkInput();
GL11.glTranslatef(0, jump_height, 0); GL11.glTranslatef(0, jump_height, 0);
Boolean GoodAnimation = true; Boolean GoodAnimation = true;
float theta_face = (float) (delta * 2 * Math.PI); 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();
}
}
} }

@ -4,6 +4,7 @@ import Scene.Objects.*;
import Scene.base.IDrawListener; import Scene.base.IDrawListener;
import Scene.base.SceneManager; import Scene.base.SceneManager;
import Scene.base.SceneObject; import Scene.base.SceneObject;
import Scene.skybox.Skybox;
import base.GraphicsObjects.Point4f; import base.GraphicsObjects.Point4f;
import base.GraphicsObjects.Vector4f; import base.GraphicsObjects.Vector4f;
import main.Camera; import main.Camera;
@ -38,27 +39,27 @@ public class Scene {
new Vector4f(2000f, 1f, 2000f, 0), new Vector4f(2000f, 1f, 2000f, 0),
textures textures
)); ));
sceneManager.addSceneObject(new Ground( // sceneManager.addSceneObject(new Ground(
new Point4f(0, 2000, 0, 0), // new Point4f(0, 2000, 0, 0),
new Point4f(0, 0, 0, 0), // new Point4f(0, 0, 0, 0),
new Vector4f(2000f, 1f, 2000f, 0), // new Vector4f(2000f, 1f, 2000f, 0),
textures // textures
)); // ));
sceneManager.addSceneObject(new Wall( // sceneManager.addSceneObject(new Wall(
new Point4f(0, 1000f, 2000, 0), // new Point4f(0, 1000f, 2000, 0),
new Point4f(0, 0, 0, 0), // new Point4f(0, 0, 0, 0),
new Vector4f(2000f, 1f, 1000f, 0), // new Vector4f(2000f, 1f, 1000f, 0),
new Vector4f(1, 0, 0, 90), // new Vector4f(1, 0, 0, 90),
textures // textures
)); // ));
//
sceneManager.addSceneObject(new Wall( // sceneManager.addSceneObject(new Wall(
new Point4f(0, 1000f, -2000, 0), // new Point4f(0, 1000f, -2000, 0),
new Point4f(0, 0, 0, 0), // new Point4f(0, 0, 0, 0),
new Vector4f(2000f, 1f, 1000f, 0), // new Vector4f(2000f, 1f, 1000f, 0),
new Vector4f(1, 0, 0, 90), // new Vector4f(1, 0, 0, 90),
textures // textures
)); // ));
sceneManager.addSceneObject(new TNT_Door( sceneManager.addSceneObject(new TNT_Door(
new Point4f(0, 50f, 200, 0), new Point4f(0, 50f, 200, 0),
@ -74,98 +75,23 @@ public class Scene {
textures 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) { 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() { sceneManager.drawAll(new IDrawListener() {
@Override @Override
public void beforeEachDraw(SceneObject object) { public void beforeEachDraw(SceneObject object) {
GL11.glPushMatrix(); 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 @Override

@ -0,0 +1,12 @@
package Scene.base;
/**
* @Author: WangYuyang
* @Date: 2021/11/5-22:33
* @Project: Assignment3
* @Package: Scene.base
* @Description:
**/
public interface IHittable {
}

@ -22,6 +22,7 @@ public abstract class SceneObject implements IDrawable, IMovable, IScalable {
private Vector4f rotation = new Vector4f(); private Vector4f rotation = new Vector4f();
private HashMap<String, Texture> textures = new LinkedHashMap<>(); private HashMap<String, Texture> textures = new LinkedHashMap<>();
public SceneObject(Point4f origin, Point4f position, Vector4f scale) { public SceneObject(Point4f origin, Point4f position, Vector4f scale) {
this.origin = new Point4f(origin.x, origin.y, origin.z, 0); this.origin = new Point4f(origin.x, origin.y, origin.z, 0);
this.position = new Point4f(position.x, position.y, position.z, 0); this.position = new Point4f(position.x, position.y, position.z, 0);

@ -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<String, Texture> textures) {
super(origin, position, scale, textures);
init();
}
public Skybox(Point4f origin, Point4f position, Vector4f scale, Vector4f rotation, HashMap<String, Texture> 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);
}
}

@ -6,7 +6,24 @@ import org.lwjgl.opengl.GL11;
public class TexCube { 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() { public TexCube() {
} }
@ -14,34 +31,6 @@ public class TexCube {
// Implement using notes and looking at TexSphere // Implement using notes and looking at TexSphere
public void DrawTexCube() { 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 //INIT GL
GL11.glBegin(GL11.GL_QUADS); GL11.glBegin(GL11.GL_QUADS);
@ -87,26 +76,6 @@ public class TexCube {
public void DrawTexCube(float number) { 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); GL11.glBegin(GL11.GL_QUADS);
@ -151,7 +120,57 @@ public class TexCube {
GL11.glEnd(); 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();
}
} }
/* /*

@ -1,5 +1,6 @@
package main; package main;
import Scene.Objects.Human;
import base.GraphicsObjects.Vector4f; import base.GraphicsObjects.Vector4f;
import org.lwjgl.input.Keyboard; import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse; import org.lwjgl.input.Mouse;
@ -141,13 +142,16 @@ public class Camera {
rotation.x = maxLookUp; rotation.x = maxLookUp;
} }
} }
if(!Mouse.isButtonDown(0))
Human.angle_target = 180 - (int) rotation.y;
glRotatef(rotation.x, 1, 0, 0); glRotatef(rotation.x, 1, 0, 0);
glRotatef(rotation.y, 0, 1, 0); glRotatef(rotation.y, 0, 1, 0);
glRotatef(-rotation.z, 0, 0, 1); glRotatef(-rotation.z, 0, 0, 1);
glTranslatef(position.x, position.y, position.z); glTranslatef(position.x, position.y, position.z);
Main.engine.setOrtho(Camera.OrthoNumber); Main.engine.setOrtho(Camera.OrthoNumber);

@ -9,7 +9,6 @@ import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode; import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture; import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader; import org.newdawn.slick.util.ResourceLoader;
import java.io.IOException; import java.io.IOException;
@ -85,6 +84,7 @@ public class Engine {
public void init() { public void init() {
Display.setVSyncEnabled(true); Display.setVSyncEnabled(true);
glEnable(GL_DEPTH_TEST);
loadTexture(); loadTexture();
} }
@ -117,7 +117,7 @@ public class Engine {
// position // position
// of the // of the
// light // 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 GL11.glLight(GL11.GL_LIGHT1, GL11.GL_POSITION, lightPos); // specify the
// position // position
@ -181,17 +181,17 @@ public class Engine {
} }
public void setOrtho(int OrthoNumber) { 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.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity(); GL11.glLoadIdentity();
//Placing 0,0 at the center of the screen //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 / 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 // 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); // GL11.glOrtho(-600 - OrthoNumber, 600 + OrthoNumber, -100 - OrthoNumber * 0.66, 700 + OrthoNumber * 0.66, 100000, -100000);
enterModelView(); enterModelView();
} // }
} }
@ -207,24 +207,7 @@ public class Engine {
} }
private void loadTexture() { private void loadTexture() {
//LOAD textures TextureLoader.loadTexture(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();
}
System.out.println("Texture loaded okay "); System.out.println("Texture loaded okay ");
} }

@ -4,11 +4,17 @@ import Scene.Scene;
import Scene.base.SceneManager; import Scene.base.SceneManager;
import base.GraphicsObjects.Vector4f; import base.GraphicsObjects.Vector4f;
import base.RenderProgramStatement; import base.RenderProgramStatement;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard; import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display; import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.opengl.GL11.glPopMatrix; import java.util.logging.Level;
import static org.lwjgl.opengl.GL11.glPushMatrix; 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 * @Author: WangYuyang
@ -18,9 +24,10 @@ import static org.lwjgl.opengl.GL11.glPushMatrix;
* @Description: * @Description:
**/ **/
public class Main { public class Main {
public static final int WIDTH = 1200; public static final int WIDTH = 1600;
public static final int HEIGHT = 800; public static final int HEIGHT = 800;
private static final boolean DEBUG = false; private static final boolean DEBUG = false;
private static final boolean resizable = true;
public static Engine engine; public static Engine engine;
private static SceneManager sceneManager = new SceneManager(); private static SceneManager sceneManager = new SceneManager();
private static Camera camera = new Camera(); private static Camera camera = new Camera();
@ -30,7 +37,9 @@ public class Main {
engine = new Engine(WIDTH, HEIGHT); engine = new Engine(WIDTH, HEIGHT);
engine.init(); engine.init();
engine.setOrtho(Camera.OrthoNumber); engine.setOrtho(Camera.OrthoNumber);
engine.setLight();
engine.enterModelView(); engine.enterModelView();
engine.initTimer(); engine.initTimer();
@ -48,6 +57,7 @@ public class Main {
@Override @Override
public void render(int delta) { public void render(int delta) {
camera.update(); camera.update();
engine.setLight();
Scene.drawAll(sceneManager, delta); Scene.drawAll(sceneManager, delta);
} }
@ -70,6 +80,35 @@ public class Main {
0, -300, -600, 0 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(); engine.close();

@ -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<String, Texture> textures_map;
public static void loadTexture(HashMap<String, Texture> 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))
);
}
}
Loading…
Cancel
Save