Added ability to load OBJ files and local space movement
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 715 KiB |
@@ -0,0 +1,47 @@
|
||||
# Blender v3.1.2 OBJ File: ''
|
||||
# www.blender.org
|
||||
o Cube
|
||||
v 1.000000 1.000000 -1.000000
|
||||
v 1.000000 -1.000000 -1.000000
|
||||
v 1.000000 1.000000 1.000000
|
||||
v 1.000000 -1.000000 1.000000
|
||||
v -1.000000 1.000000 -1.000000
|
||||
v -1.000000 -1.000000 -1.000000
|
||||
v -1.000000 1.000000 1.000000
|
||||
v -1.000000 -1.000000 1.000000
|
||||
vt 1.000000 0.000000
|
||||
vt 0.666667 0.333333
|
||||
vt 0.666667 0.000000
|
||||
vt 0.333333 0.333333
|
||||
vt 0.000000 0.000000
|
||||
vt 0.333333 0.000000
|
||||
vt 0.333333 0.666667
|
||||
vt 0.000000 0.333333
|
||||
vt 0.333333 0.333333
|
||||
vt 0.666667 0.000000
|
||||
vt 0.333333 0.000000
|
||||
vt 0.666667 0.666667
|
||||
vt 0.333333 0.333333
|
||||
vt 0.666667 0.333333
|
||||
vt 0.333333 1.000000
|
||||
vt 0.000000 0.666667
|
||||
vt 0.333333 0.666667
|
||||
vt 1.000000 0.333333
|
||||
vt 0.000000 0.333333
|
||||
vt 0.000000 0.666667
|
||||
vt 0.666667 0.333333
|
||||
vt 0.333333 0.666667
|
||||
vt 0.000000 1.000000
|
||||
s off
|
||||
f 5/1 3/2 1/3
|
||||
f 3/4 8/5 4/6
|
||||
f 7/7 6/8 8/9
|
||||
f 2/10 8/9 6/11
|
||||
f 1/12 4/13 2/14
|
||||
f 5/15 2/16 6/17
|
||||
f 5/1 7/18 3/2
|
||||
f 3/4 7/19 8/5
|
||||
f 7/7 5/20 6/8
|
||||
f 2/10 4/21 8/9
|
||||
f 1/12 3/22 4/13
|
||||
f 5/15 1/23 2/16
|
||||
+109786
File diff suppressed because it is too large
Load Diff
Binary file not shown.
|
After Width: | Height: | Size: 31 MiB |
+29344
File diff suppressed because it is too large
Load Diff
Binary file not shown.
|
After Width: | Height: | Size: 4.9 KiB |
@@ -7,12 +7,19 @@ import Models.RawModel;
|
||||
import RenderEngine.Renderer;
|
||||
import Shaders.StaticShader;
|
||||
import Models.TexturedModel;
|
||||
import Tools.Axis;
|
||||
import Tools.OBJLoader;
|
||||
import Tools.Space;
|
||||
import org.lwjgl.LWJGLException;
|
||||
import org.lwjgl.input.Keyboard;
|
||||
import org.lwjgl.input.Mouse;
|
||||
import org.lwjgl.opengl.Display;
|
||||
import Textures.ModelTexture;
|
||||
import org.lwjgl.util.vector.Vector3f;
|
||||
import sun.awt.AWTAccessor;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class MainGameLoop
|
||||
{
|
||||
/**
|
||||
@@ -22,119 +29,90 @@ public class MainGameLoop
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
DisplayManager.createDisplay("Rendr");
|
||||
DisplayManager.createDisplay("Rendr", true);
|
||||
|
||||
Loader loader = new Loader();
|
||||
StaticShader shader = new StaticShader();
|
||||
Renderer renderer = new Renderer(shader);
|
||||
|
||||
float[] vertices = {
|
||||
-0.5f,0.5f,-0.5f,
|
||||
-0.5f,-0.5f,-0.5f,
|
||||
0.5f,-0.5f,-0.5f,
|
||||
0.5f,0.5f,-0.5f,
|
||||
OBJLoader objLoader = new OBJLoader("Object");
|
||||
float[] vertices = objLoader.getVertices();
|
||||
int[] indices = objLoader.getIndices();
|
||||
float[] uvs = objLoader.getUVs();
|
||||
|
||||
-0.5f,0.5f,0.5f,
|
||||
-0.5f,-0.5f,0.5f,
|
||||
0.5f,-0.5f,0.5f,
|
||||
0.5f,0.5f,0.5f,
|
||||
|
||||
0.5f,0.5f,-0.5f,
|
||||
0.5f,-0.5f,-0.5f,
|
||||
0.5f,-0.5f,0.5f,
|
||||
0.5f,0.5f,0.5f,
|
||||
|
||||
-0.5f,0.5f,-0.5f,
|
||||
-0.5f,-0.5f,-0.5f,
|
||||
-0.5f,-0.5f,0.5f,
|
||||
-0.5f,0.5f,0.5f,
|
||||
|
||||
-0.5f,0.5f,0.5f,
|
||||
-0.5f,0.5f,-0.5f,
|
||||
0.5f,0.5f,-0.5f,
|
||||
0.5f,0.5f,0.5f,
|
||||
|
||||
-0.5f,-0.5f,0.5f,
|
||||
-0.5f,-0.5f,-0.5f,
|
||||
0.5f,-0.5f,-0.5f,
|
||||
0.5f,-0.5f,0.5f
|
||||
|
||||
};
|
||||
|
||||
float[] uvs = {
|
||||
|
||||
0,0,
|
||||
0,1,
|
||||
1,1,
|
||||
1,0,
|
||||
0,0,
|
||||
0,1,
|
||||
1,1,
|
||||
1,0,
|
||||
0,0,
|
||||
0,1,
|
||||
1,1,
|
||||
1,0,
|
||||
0,0,
|
||||
0,1,
|
||||
1,1,
|
||||
1,0,
|
||||
0,0,
|
||||
0,1,
|
||||
1,1,
|
||||
1,0,
|
||||
0,0,
|
||||
0,1,
|
||||
1,1,
|
||||
1,0
|
||||
|
||||
|
||||
};
|
||||
|
||||
int[] indices = {
|
||||
0,1,3,
|
||||
3,1,2,
|
||||
4,5,7,
|
||||
7,5,6,
|
||||
8,9,11,
|
||||
11,9,10,
|
||||
12,13,15,
|
||||
15,13,14,
|
||||
16,17,19,
|
||||
19,17,18,
|
||||
20,21,23,
|
||||
23,21,22
|
||||
|
||||
};
|
||||
|
||||
RawModel model = loader.loadToVAO(vertices, indices, uvs);
|
||||
ModelTexture texture = new ModelTexture(loader.loadTexture("mossyBricks"));
|
||||
TexturedModel texturedModel = new TexturedModel(model, texture);
|
||||
Entity entity = new Entity(texturedModel, new Vector3f(0, 0, -5), 0, 0, 0, 1);
|
||||
|
||||
|
||||
Camera camera = new Camera();
|
||||
|
||||
float moveSpeed = 0.2f;
|
||||
|
||||
Mouse.setGrabbed(true);
|
||||
|
||||
boolean fPressed = false;
|
||||
|
||||
while(!Display.isCloseRequested())
|
||||
{
|
||||
if(Keyboard.isKeyDown(Keyboard.KEY_W))
|
||||
{
|
||||
camera.translate(0, 0, -.02f);
|
||||
camera.translate(0, 0, -moveSpeed, Space.Local);
|
||||
}
|
||||
if(Keyboard.isKeyDown(Keyboard.KEY_S))
|
||||
{
|
||||
camera.translate(0, 0, .02f);
|
||||
camera.translate(0, 0, moveSpeed, Space.Local);
|
||||
}
|
||||
if(Keyboard.isKeyDown(Keyboard.KEY_D))
|
||||
{
|
||||
camera.translate(.02f, 0, 0);
|
||||
camera.translate(moveSpeed, 0, 0, Space.Local);
|
||||
}
|
||||
if(Keyboard.isKeyDown(Keyboard.KEY_A))
|
||||
{
|
||||
camera.translate(-.02f, 0, 0);
|
||||
camera.translate(-moveSpeed, 0, 0, Space.Local);
|
||||
}
|
||||
if(Keyboard.isKeyDown(Keyboard.KEY_SPACE))
|
||||
{
|
||||
camera.translate(0, moveSpeed, 0, Space.Local);
|
||||
}
|
||||
if(Keyboard.isKeyDown(Keyboard.KEY_LSHIFT))
|
||||
{
|
||||
camera.translate(0, -moveSpeed, 0, Space.Local);
|
||||
}
|
||||
|
||||
entity.rotate(.5f, .5f, .5f);
|
||||
renderer.prepare(0, 0, 0);
|
||||
if(Mouse.isGrabbed())
|
||||
{
|
||||
camera.rotate(Axis.y, Mouse.getDX() / 10f);
|
||||
camera.rotate(Axis.x, Mouse.getDY() / -10f);
|
||||
}
|
||||
|
||||
|
||||
|
||||
if(Keyboard.isKeyDown(Keyboard.KEY_F) && !fPressed)
|
||||
{
|
||||
fPressed = true;
|
||||
DisplayManager.setFullScreen(!DisplayManager.isFullScreen());
|
||||
}
|
||||
if(!Keyboard.isKeyDown(Keyboard.KEY_F))
|
||||
{
|
||||
fPressed = false;
|
||||
}
|
||||
|
||||
if(Mouse.isButtonDown(0))
|
||||
{
|
||||
Mouse.setGrabbed(true);
|
||||
}
|
||||
|
||||
if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
|
||||
{
|
||||
Mouse.setGrabbed(false);
|
||||
}
|
||||
|
||||
//entity.rotate(0, 0.5f, 0);
|
||||
|
||||
renderer.prepare(0.2f, 0.2f, 0.2f);
|
||||
shader.start();
|
||||
shader.loadViewMatrix(camera);
|
||||
renderer.render(entity, shader);
|
||||
|
||||
@@ -1,29 +1,53 @@
|
||||
package Entity;
|
||||
|
||||
import Tools.Axis;
|
||||
import Tools.Space;
|
||||
import org.lwjgl.util.vector.Matrix4f;
|
||||
import org.lwjgl.util.vector.Vector3f;
|
||||
|
||||
public class Camera
|
||||
{
|
||||
private Vector3f position = new Vector3f(0, 0, 0);
|
||||
private float rotX, rotY, rotZ;
|
||||
private Vector3f rotation = new Vector3f(0, 0, 0);
|
||||
|
||||
public Camera()
|
||||
public void translate(float x, float y, float z, Space space)
|
||||
{
|
||||
if(space == Space.Global)
|
||||
{
|
||||
position.x += x;
|
||||
position.z += z;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(z != 0)
|
||||
{
|
||||
Vector3f rotated = new Vector3f((float)Math.sin(Math.toRadians(-getRotY())) * z, 0, (float)Math.cos(Math.toRadians(-getRotY())) * z);
|
||||
rotated.normalise();
|
||||
position.x += rotated.x / 4f;
|
||||
position.z += rotated.z / 4f;
|
||||
}
|
||||
if(x != 0)
|
||||
{
|
||||
Vector3f rotated = new Vector3f((float)Math.cos(Math.toRadians(-getRotY())) * x, 0, (float)-Math.sin(Math.toRadians(-getRotY())) * x);
|
||||
rotated.normalise();
|
||||
position.x += rotated.x / 4f;
|
||||
position.z += rotated.z / 4f;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void translate(Vector3f value)
|
||||
{
|
||||
position.x += value.x;
|
||||
position.y += value.y;
|
||||
position.z += value.z;
|
||||
}
|
||||
|
||||
public void translate(float x, float y, float z)
|
||||
{
|
||||
position.x += x;
|
||||
position.y += y;
|
||||
position.z += z;
|
||||
}
|
||||
|
||||
public void translate(Vector3f translation, Space space)
|
||||
{
|
||||
translate(translation.x, translation.y, translation.z, space);
|
||||
}
|
||||
|
||||
public void rotate(Axis axis, float rotationDegree)
|
||||
{
|
||||
this.rotation.x += axis == Axis.x ? rotationDegree : 0;
|
||||
this.rotation.y += axis == Axis.y ? rotationDegree : 0;
|
||||
this.rotation.z += axis == Axis.z ? rotationDegree : 0;
|
||||
}
|
||||
|
||||
public Vector3f getPosition()
|
||||
@@ -33,16 +57,16 @@ public class Camera
|
||||
|
||||
public float getRotX()
|
||||
{
|
||||
return rotX;
|
||||
return rotation.x;
|
||||
}
|
||||
|
||||
public float getRotY()
|
||||
{
|
||||
return rotY;
|
||||
return rotation.y;
|
||||
}
|
||||
|
||||
public float getRotZ()
|
||||
{
|
||||
return rotZ;
|
||||
return rotation.z;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package Models;
|
||||
|
||||
import RenderEngine.Loader;
|
||||
import Textures.ModelTexture;
|
||||
|
||||
public class TexturedModel
|
||||
@@ -13,6 +14,13 @@ public class TexturedModel
|
||||
this.texture = texture;
|
||||
}
|
||||
|
||||
public TexturedModel(RawModel rawModel)
|
||||
{
|
||||
Loader loader = new Loader();
|
||||
this.rawModel = rawModel;
|
||||
this.texture = new ModelTexture(loader.loadTexture("white"));;
|
||||
}
|
||||
|
||||
public RawModel getRawModel()
|
||||
{
|
||||
return rawModel;
|
||||
|
||||
@@ -9,7 +9,7 @@ public class DisplayManager
|
||||
private static final int fps = 60;
|
||||
|
||||
//creates a new display with openGL viewport in it
|
||||
public static void createDisplay(String title)
|
||||
public static void createDisplay(String title, boolean vSync)
|
||||
{
|
||||
ContextAttribs attribs = new ContextAttribs(3, 2)
|
||||
.withForwardCompatible(true).withProfileCore(true);
|
||||
@@ -18,7 +18,9 @@ public class DisplayManager
|
||||
{
|
||||
Display.setDisplayMode(new DisplayMode(width, height));
|
||||
Display.create(new PixelFormat(), attribs);
|
||||
Display.setVSyncEnabled(vSync);
|
||||
Display.setTitle(title);
|
||||
Display.setResizable(true);
|
||||
} catch (LWJGLException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
@@ -30,6 +32,10 @@ public class DisplayManager
|
||||
//updates the display (limited to the given fps)
|
||||
public static void updateDisplay()
|
||||
{
|
||||
if(Display.wasResized())
|
||||
{
|
||||
GL11.glViewport(0, 0, Display.getWidth(), Display.getHeight());
|
||||
}
|
||||
Display.sync(fps);
|
||||
Display.update();
|
||||
}
|
||||
@@ -39,4 +45,34 @@ public class DisplayManager
|
||||
{
|
||||
Display.destroy();
|
||||
}
|
||||
|
||||
public static void setFullScreen(boolean fullScreen)
|
||||
{
|
||||
if(fullScreen)
|
||||
{
|
||||
try
|
||||
{
|
||||
Display.setDisplayModeAndFullscreen(Display.getDesktopDisplayMode());
|
||||
GL11.glViewport(0, 0, Display.getWidth(), Display.getHeight());
|
||||
} catch (LWJGLException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
Display.setFullscreen(false);
|
||||
} catch (LWJGLException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isFullScreen()
|
||||
{
|
||||
return Display.isFullscreen();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,8 @@ import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.FloatBuffer;
|
||||
import java.nio.IntBuffer;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -38,12 +40,13 @@ public class Loader
|
||||
Texture texture = null;
|
||||
try
|
||||
{
|
||||
texture = TextureLoader.getTexture("PNG", new FileInputStream("res/"+fileName+".png"));
|
||||
texture = TextureLoader.getTexture("PNG", Files.newInputStream(Paths.get("res/" + fileName + ".png")));
|
||||
} catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
assert texture != null;
|
||||
int textureID = texture.getTextureID();
|
||||
textures.add(textureID);
|
||||
return textureID;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package RenderEngine;
|
||||
|
||||
import Entity.Entity;
|
||||
import Entity.*;
|
||||
import Models.RawModel;
|
||||
import Models.TexturedModel;
|
||||
import Shaders.StaticShader;
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package Tools;
|
||||
|
||||
public enum Axis
|
||||
{
|
||||
x, y, z
|
||||
}
|
||||
@@ -6,6 +6,41 @@ import org.lwjgl.util.vector.Vector3f;
|
||||
|
||||
public class Maths
|
||||
{
|
||||
public static class Vector2
|
||||
{
|
||||
public float x, y;
|
||||
|
||||
public Vector2(float x, float y)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "(" + x + ", " + y + ")";
|
||||
}
|
||||
}
|
||||
|
||||
public static class Vector3
|
||||
{
|
||||
public float x, y, z;
|
||||
|
||||
public Vector3(float x, float y, float z)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "(" + x + ", " + y + " " + z + ")";
|
||||
}
|
||||
}
|
||||
|
||||
public static Matrix4f createTransformMatrix(Vector3f translation, float rX, float rY, float rZ, float scale)
|
||||
{
|
||||
Matrix4f matrix = new Matrix4f();
|
||||
@@ -24,6 +59,7 @@ public class Maths
|
||||
viewMatrix.setIdentity();
|
||||
Matrix4f.rotate((float) Math.toRadians(camera.getRotX()), new Vector3f(1, 0, 0), viewMatrix, viewMatrix);
|
||||
Matrix4f.rotate((float) Math.toRadians(camera.getRotY()), new Vector3f(0, 1, 0), viewMatrix, viewMatrix);
|
||||
Matrix4f.rotate((float) Math.toRadians(camera.getRotZ()), new Vector3f(0, 1, 1), viewMatrix, viewMatrix);
|
||||
Vector3f cameraPos = camera.getPosition();
|
||||
Vector3f negativeCameraPos = new Vector3f(-cameraPos.x, -cameraPos.y, -cameraPos.z);
|
||||
Matrix4f.translate(negativeCameraPos, viewMatrix, viewMatrix);
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
package Tools;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class OBJLoader
|
||||
{
|
||||
private File file;
|
||||
private List<Maths.Vector3> vertices = new ArrayList<>();
|
||||
private List<Integer> indices = new ArrayList<>();
|
||||
private List<Maths.Vector2> uvs = new ArrayList<>();
|
||||
|
||||
private List<Integer> uvOrder = new ArrayList<>();
|
||||
|
||||
public OBJLoader(String fileName)
|
||||
{
|
||||
file = new File("res/" + fileName + ".obj");
|
||||
|
||||
try
|
||||
{
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(Files.newInputStream(file.toPath())));
|
||||
String line;
|
||||
|
||||
while ((line = reader.readLine()) != null)
|
||||
{
|
||||
String[] split = line.split(" ");
|
||||
switch (split[0])
|
||||
{
|
||||
case "v":
|
||||
vertices.add(new Maths.Vector3(Float.parseFloat(split[1]), Float.parseFloat(split[2]), Float.parseFloat(split[3])));
|
||||
break;
|
||||
case "vt":
|
||||
uvs.add(new Maths.Vector2(Float.parseFloat(split[1]), Float.parseFloat(split[2])));
|
||||
break;
|
||||
case "f":
|
||||
String[] s1 = split[1].split("/");
|
||||
String[] s2 = split[2].split("/");
|
||||
String[] s3 = split[3].split("/");
|
||||
indices.add(Integer.parseInt(s1[0]));
|
||||
indices.add(Integer.parseInt(s2[0]));
|
||||
indices.add(Integer.parseInt(s3[0]));
|
||||
|
||||
uvOrder.add(Integer.parseInt(s1[1]));
|
||||
uvOrder.add(Integer.parseInt(s2[1]));
|
||||
uvOrder.add(Integer.parseInt(s3[1]));
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IOException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public float[] getVertices()
|
||||
{
|
||||
float[] result = new float[indices.size() * 3];
|
||||
|
||||
int resultI = 0;
|
||||
int orderI = 0;
|
||||
|
||||
while(resultI < result.length)
|
||||
{
|
||||
result[resultI] = vertices.get(indices.get(orderI) - 1).x;
|
||||
resultI++;
|
||||
result[resultI] = vertices.get(indices.get(orderI) - 1).y;
|
||||
resultI++;
|
||||
result[resultI] = vertices.get(indices.get(orderI) - 1).z;
|
||||
resultI++;
|
||||
orderI++;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public float[] getUVs()
|
||||
{
|
||||
float[] result = new float[uvOrder.size() * 2];
|
||||
|
||||
int resultI = 0;
|
||||
int orderI = 0;
|
||||
|
||||
while(resultI < result.length)
|
||||
{
|
||||
result[resultI] = uvs.get(uvOrder.get(orderI) - 1).x;
|
||||
resultI++;
|
||||
result[resultI] = uvs.get(uvOrder.get(orderI) - 1).y;
|
||||
resultI++;
|
||||
orderI++;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public int[] getIndices()
|
||||
{
|
||||
int[] result = new int[indices.size()];
|
||||
for (int i = 0; i < result.length; i++)
|
||||
{
|
||||
result[i] = i;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package Tools;
|
||||
|
||||
public enum Space
|
||||
{
|
||||
Local, Global
|
||||
}
|
||||
Reference in New Issue
Block a user