added more stuff and refactor
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -34,3 +34,5 @@ build/
|
||||
|
||||
### Mac OS ###
|
||||
.DS_Store
|
||||
|
||||
logs
|
||||
344
pom.xml
344
pom.xml
@@ -6,16 +6,53 @@
|
||||
|
||||
<groupId>me.slinky</groupId>
|
||||
<artifactId>Tony</artifactId>
|
||||
<version>0.0.1</version>
|
||||
<version>0.0.2</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>22</maven.compiler.source>
|
||||
<maven.compiler.target>22</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
|
||||
<lwjgl.version>3.4.1</lwjgl.version>
|
||||
<joml.version>1.10.8</joml.version>
|
||||
<joml-primitives.version>1.10.0</joml-primitives.version>
|
||||
</properties>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>15</source>
|
||||
<target>15</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>lwjgl-natives-linux-amd64</id>
|
||||
<activation>
|
||||
<os>
|
||||
<family>unix</family>
|
||||
<name>linux</name>
|
||||
<arch>amd64</arch>
|
||||
</os>
|
||||
</activation>
|
||||
<properties>
|
||||
<lwjgl.natives>natives-linux</lwjgl.natives>
|
||||
</properties>
|
||||
</profile>
|
||||
<profile>
|
||||
<id>lwjgl-natives-windows-amd64</id>
|
||||
<activation>
|
||||
<os>
|
||||
<family>windows</family>
|
||||
<arch>amd64</arch>
|
||||
</os>
|
||||
</activation>
|
||||
<properties>
|
||||
<lwjgl.natives>natives-windows</lwjgl.natives>
|
||||
</properties>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
@@ -38,170 +75,30 @@
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-assimp</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-bgfx</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-egl</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-fmod</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-freetype</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-glfw</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-harfbuzz</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-hwloc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-jawt</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-jemalloc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-ktx</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-llvm</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-lmdb</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-lz4</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-meshoptimizer</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-msdfgen</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-nanovg</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-nfd</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-nuklear</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-odbc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-openal</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-opencl</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-opengl</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-opengles</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-openxr</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-opus</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-par</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-remotery</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-renderdoc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-rpmalloc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-sdl</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-shaderc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-spng</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-spvc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-stb</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-tinyexr</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-tinyfd</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-vma</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-vulkan</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-xxhash</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-yoga</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-zstd</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl</artifactId>
|
||||
@@ -212,81 +109,11 @@
|
||||
<artifactId>lwjgl-assimp</artifactId>
|
||||
<classifier>${lwjgl.natives}</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-bgfx</artifactId>
|
||||
<classifier>${lwjgl.natives}</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-freetype</artifactId>
|
||||
<classifier>${lwjgl.natives}</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-glfw</artifactId>
|
||||
<classifier>${lwjgl.natives}</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-harfbuzz</artifactId>
|
||||
<classifier>${lwjgl.natives}</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-hwloc</artifactId>
|
||||
<classifier>${lwjgl.natives}</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-jemalloc</artifactId>
|
||||
<classifier>${lwjgl.natives}</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-ktx</artifactId>
|
||||
<classifier>${lwjgl.natives}</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-llvm</artifactId>
|
||||
<classifier>${lwjgl.natives}</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-lmdb</artifactId>
|
||||
<classifier>${lwjgl.natives}</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-lz4</artifactId>
|
||||
<classifier>${lwjgl.natives}</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-meshoptimizer</artifactId>
|
||||
<classifier>${lwjgl.natives}</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-msdfgen</artifactId>
|
||||
<classifier>${lwjgl.natives}</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-nanovg</artifactId>
|
||||
<classifier>${lwjgl.natives}</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-nfd</artifactId>
|
||||
<classifier>${lwjgl.natives}</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-nuklear</artifactId>
|
||||
<classifier>${lwjgl.natives}</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-openal</artifactId>
|
||||
@@ -297,95 +124,40 @@
|
||||
<artifactId>lwjgl-opengl</artifactId>
|
||||
<classifier>${lwjgl.natives}</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-opengles</artifactId>
|
||||
<classifier>${lwjgl.natives}</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-openxr</artifactId>
|
||||
<classifier>${lwjgl.natives}</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-opus</artifactId>
|
||||
<classifier>${lwjgl.natives}</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-par</artifactId>
|
||||
<classifier>${lwjgl.natives}</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-remotery</artifactId>
|
||||
<classifier>${lwjgl.natives}</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-rpmalloc</artifactId>
|
||||
<classifier>${lwjgl.natives}</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-sdl</artifactId>
|
||||
<classifier>${lwjgl.natives}</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-shaderc</artifactId>
|
||||
<classifier>${lwjgl.natives}</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-spng</artifactId>
|
||||
<classifier>${lwjgl.natives}</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-spvc</artifactId>
|
||||
<classifier>${lwjgl.natives}</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-stb</artifactId>
|
||||
<classifier>${lwjgl.natives}</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-tinyexr</artifactId>
|
||||
<classifier>${lwjgl.natives}</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-tinyfd</artifactId>
|
||||
<classifier>${lwjgl.natives}</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-vma</artifactId>
|
||||
<classifier>${lwjgl.natives}</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-xxhash</artifactId>
|
||||
<classifier>${lwjgl.natives}</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-yoga</artifactId>
|
||||
<classifier>${lwjgl.natives}</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lwjgl</groupId>
|
||||
<artifactId>lwjgl-zstd</artifactId>
|
||||
<classifier>${lwjgl.natives}</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.joml</groupId>
|
||||
<artifactId>joml</artifactId>
|
||||
<version>1.10.8</version>
|
||||
<version>${joml.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.joml</groupId>
|
||||
<artifactId>joml-primitives</artifactId>
|
||||
<version>${joml-primitives.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-api</artifactId>
|
||||
<version>2.25.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-core</artifactId>
|
||||
<version>2.25.3</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
16
src/main/java/DirectionalLight.java
Normal file
16
src/main/java/DirectionalLight.java
Normal file
@@ -0,0 +1,16 @@
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class DirectionalLight {
|
||||
public Vector3f direction;
|
||||
public Vector3f color;
|
||||
public float intensity;
|
||||
|
||||
public DirectionalLight (
|
||||
Vector3f direction,
|
||||
Vector3f color,
|
||||
float intensity) {
|
||||
this.direction = direction;
|
||||
this.color = color;
|
||||
this.intensity = intensity;
|
||||
}
|
||||
}
|
||||
@@ -15,35 +15,46 @@ public class Mesh {
|
||||
private int _vbo;
|
||||
private int _ebo;
|
||||
|
||||
public void setup(float[] positions, float[] uv, float[] normals, int[] indices) {
|
||||
private int vertexCount;
|
||||
|
||||
public Mesh(float[] p, float[] u, float[] n, int[] indices) {
|
||||
vertexCount = indices.length;
|
||||
sendToGPU(Vertex.interleaveVertexData(p, u, n), indices);
|
||||
}
|
||||
|
||||
public void render() {
|
||||
glBindVertexArray(_vao);
|
||||
glDrawElements(GL_TRIANGLES, vertexCount, GL_UNSIGNED_INT, 0);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
public void cleanup() {
|
||||
glDeleteBuffers(_vbo);
|
||||
glDeleteBuffers(_ebo);
|
||||
glDeleteVertexArrays(_vao);
|
||||
}
|
||||
|
||||
private void sendToGPU(float[] vertices, int[] indices) {
|
||||
try (MemoryStack stack = MemoryStack.stackPush()) {
|
||||
_vao = glGenVertexArrays();
|
||||
glBindVertexArray(_vao);
|
||||
|
||||
// pos
|
||||
_vbo = glGenBuffers();
|
||||
FloatBuffer posBuffer = stack.mallocFloat(positions.length);
|
||||
posBuffer.put(positions).flip();
|
||||
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW);
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
|
||||
glBufferData(GL_ARRAY_BUFFER, stack.floats(vertices), GL_STATIC_DRAW);
|
||||
|
||||
// uv
|
||||
int uvVbo = glGenBuffers();
|
||||
FloatBuffer uvBuffer = stack.mallocFloat(uv.length);
|
||||
uvBuffer.put(uv).flip();
|
||||
glBindBuffer(GL_ARRAY_BUFFER, uvVbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, uvBuffer, GL_STATIC_DRAW);
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0);
|
||||
|
||||
// indices
|
||||
_ebo = glGenBuffers();
|
||||
IntBuffer indexBuffer = stack.mallocInt(indices.length);
|
||||
indexBuffer.put(indices).flip();
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ebo);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexBuffer, GL_STATIC_DRAW);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, stack.ints(indices), GL_STATIC_DRAW);
|
||||
|
||||
// set vao positions
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, false, STRIDE, 0);
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, false, STRIDE, 3 * Float.BYTES);
|
||||
glEnableVertexAttribArray(2);
|
||||
glVertexAttribPointer(2, 3, GL_FLOAT, false, STRIDE, 5 * Float.BYTES);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
|
||||
35
src/main/java/OrthoCamera.java
Normal file
35
src/main/java/OrthoCamera.java
Normal file
@@ -0,0 +1,35 @@
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
public class OrthoCamera {
|
||||
private Matrix4f projection;
|
||||
private Matrix4f view;
|
||||
|
||||
private float size = 10.f;
|
||||
private Vector3f position = new Vector3f(size, size, size);
|
||||
|
||||
public OrthoCamera(float aspectRatio) {
|
||||
projection = new Matrix4f().ortho(
|
||||
-size * aspectRatio, // Left
|
||||
size * aspectRatio, // Right
|
||||
-size, // Bottom
|
||||
size, // Top
|
||||
0.01f, // Near
|
||||
100.0f // Far
|
||||
);
|
||||
|
||||
Vector3f camPos = new Vector3f(10, 10, 10);
|
||||
Vector3f target = new Vector3f(0, 0, 0);
|
||||
Vector3f up = new Vector3f(0, 1, 0);
|
||||
|
||||
view = new Matrix4f().lookAt(camPos, target, up);
|
||||
}
|
||||
|
||||
public Matrix4f getProjection() {
|
||||
return projection;
|
||||
}
|
||||
|
||||
public Matrix4f getView() {
|
||||
return view;
|
||||
}
|
||||
}
|
||||
@@ -3,11 +3,57 @@ import org.joml.Vector3f;
|
||||
|
||||
public class PrimitiveGenerator {
|
||||
public static Mesh getCube() {
|
||||
var mesh = new Mesh();
|
||||
float s = 0.5f;
|
||||
|
||||
Vertex[] vertices = new Vertex[8];
|
||||
// Positions: 24 vertices (6 faces * 4 vertices)
|
||||
float[] p = {
|
||||
// Front face (z = s)
|
||||
-s, s, s, -s, -s, s, s, -s, s, s, s, s,
|
||||
// Back face (z = -s)
|
||||
-s, s, -s, s, s, -s, s, -s, -s, -s, -s, -s,
|
||||
// Top face (y = s)
|
||||
-s, s, -s, -s, s, s, s, s, s, s, s, -s,
|
||||
// Bottom face (y = -s)
|
||||
-s, -s, -s, s, -s, -s, s, -s, s, -s, -s, s,
|
||||
// Right face (x = s)
|
||||
s, s, s, s, -s, s, s, -s, -s, s, s, -s,
|
||||
// Left face (x = -s)
|
||||
-s, s, -s, -s, -s, -s, -s, -s, s, -s, s, s
|
||||
};
|
||||
|
||||
// UV Coordinates (Standard 0,0 to 1,1 for every face)
|
||||
float[] u = {
|
||||
0,1, 0,0, 1,0, 1,1, // Front
|
||||
1,1, 0,1, 0,0, 1,0, // Back
|
||||
0,1, 0,0, 1,0, 1,1, // Top
|
||||
0,0, 1,0, 1,1, 0,1, // Bottom
|
||||
0,1, 0,0, 1,0, 1,1, // Right
|
||||
0,1, 0,0, 1,0, 1,1 // Left
|
||||
};
|
||||
|
||||
// Normals (Pointing straight out from each face)
|
||||
float[] n = {
|
||||
0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, // Front
|
||||
0, 0,-1, 0, 0,-1, 0, 0,-1, 0, 0,-1, // Back
|
||||
0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, // Top
|
||||
0,-1, 0, 0,-1, 0, 0,-1, 0, 0,-1, 0, // Bottom
|
||||
1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, // Right
|
||||
-1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0 // Left
|
||||
};
|
||||
|
||||
// Indices (Two triangles per face)
|
||||
int[] indices = new int[36];
|
||||
return mesh;
|
||||
for (int i = 0; i < 6; i++) {
|
||||
int offset = i * 4;
|
||||
int idx = i * 6;
|
||||
indices[idx + 0] = offset + 0;
|
||||
indices[idx + 1] = offset + 1;
|
||||
indices[idx + 2] = offset + 2;
|
||||
indices[idx + 3] = offset + 2;
|
||||
indices[idx + 4] = offset + 3;
|
||||
indices[idx + 5] = offset + 0;
|
||||
}
|
||||
|
||||
return new Mesh(p, u, n, indices);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,23 @@
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Vector3f;
|
||||
import org.lwjgl.system.MemoryStack;
|
||||
|
||||
import java.nio.FloatBuffer;
|
||||
import java.util.HashMap;
|
||||
import java.util.Vector;
|
||||
|
||||
import static org.lwjgl.opengl.GL40.*;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
public class Shader {
|
||||
private static final Logger logger = LogManager.getLogger(Shader.class);
|
||||
|
||||
private final int id;
|
||||
|
||||
private final HashMap<String, Integer> uniforms = new HashMap<>();
|
||||
|
||||
public Shader(String vertexPath, String fragmentPath) {
|
||||
id = glCreateProgram();
|
||||
|
||||
@@ -34,6 +49,56 @@ public class Shader {
|
||||
public void unbind() {
|
||||
glUseProgram(0);
|
||||
}
|
||||
|
||||
public void setUniform(String uniformName, Matrix4f data) {
|
||||
try (MemoryStack stack = MemoryStack.stackPush()) {
|
||||
FloatBuffer fb = stack.mallocFloat(16);
|
||||
data.get(fb);
|
||||
int location = getUniformLocation(uniformName);
|
||||
if (location < 0) {
|
||||
var msg = "Failed to find location for uniform name %s".formatted(uniformName);
|
||||
logger.error(msg);
|
||||
return;
|
||||
}
|
||||
glUniformMatrix4fv(location, false, fb);
|
||||
}
|
||||
}
|
||||
|
||||
public void setUniform(String uniformName, Vector3f data) {
|
||||
try (MemoryStack stack = MemoryStack.stackPush()) {
|
||||
FloatBuffer fb = stack.mallocFloat(3);
|
||||
data.get(fb);
|
||||
int location = getUniformLocation(uniformName);
|
||||
if (location < 0) {
|
||||
var msg = "Failed to find location for uniform name %s".formatted(uniformName);
|
||||
logger.error(msg);
|
||||
return;
|
||||
}
|
||||
glUniform3fv(location, fb.array());
|
||||
}
|
||||
}
|
||||
|
||||
public void setUniform(String uniformName, float data) {
|
||||
try (MemoryStack stack = MemoryStack.stackPush()) {
|
||||
FloatBuffer fb = stack.mallocFloat(1);
|
||||
fb.put(data);
|
||||
int location = getUniformLocation(uniformName);
|
||||
if (location < 0) {
|
||||
var msg = "Failed to find location for uniform name %s".formatted(uniformName);
|
||||
logger.error(msg);
|
||||
return;
|
||||
}
|
||||
glUniform1f(location, fb.get(0));
|
||||
}
|
||||
}
|
||||
|
||||
private int getUniformLocation(String uniformName) {
|
||||
if (!uniforms.containsKey(uniformName)) {
|
||||
int location = glGetUniformLocation(id, uniformName);
|
||||
uniforms.put(uniformName, location);
|
||||
}
|
||||
return uniforms.get(uniformName);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Vector3f;
|
||||
import org.lwjgl.*;
|
||||
import org.lwjgl.glfw.*;
|
||||
import org.lwjgl.opengl.*;
|
||||
import org.lwjgl.system.*;
|
||||
|
||||
import java.nio.*;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static org.lwjgl.glfw.Callbacks.*;
|
||||
import static org.lwjgl.glfw.GLFW.*;
|
||||
@@ -13,20 +13,35 @@ import static org.lwjgl.opengl.GL40.*;
|
||||
import static org.lwjgl.system.MemoryStack.*;
|
||||
import static org.lwjgl.system.MemoryUtil.*;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
public class Tony {
|
||||
private long window;
|
||||
private Shader shader;
|
||||
private Mesh cube;
|
||||
private OrthoCamera camera;
|
||||
private DirectionalLight sun;
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger( Tony.class.getName() );
|
||||
private static final Logger logger = LogManager.getLogger(Tony.class);
|
||||
|
||||
public void run() {
|
||||
LOGGER.log(Level.INFO, "Tony version: %s".formatted(Version.getVersionString()));
|
||||
LOGGER.log(Level.INFO, "LWJGL version: %s".formatted(org.lwjgl.Version.getVersion()));
|
||||
logger.info("Tony version: %s".formatted(Version.getVersionString()));
|
||||
logger.info("LWJGL version: %s".formatted(org.lwjgl.Version.getVersion()));
|
||||
|
||||
init();
|
||||
|
||||
// setup game objects
|
||||
shader = new Shader("/shaders/basic.vert", "/shaders/basic.frag");
|
||||
cube = PrimitiveGenerator.getCube();
|
||||
|
||||
camera = new OrthoCamera(1024.f / 720);
|
||||
|
||||
sun = new DirectionalLight(
|
||||
new Vector3f(-1.0f, -1.0f, -0.5f), // Pointing Down, Left, and slightly Back
|
||||
new Vector3f(1.0f, 1.0f, 0.9f), // Warm yellowish sun
|
||||
1.2f // Brightness
|
||||
);
|
||||
|
||||
loop();
|
||||
|
||||
@@ -75,19 +90,37 @@ public class Tony {
|
||||
} // the stack frame is popped automatically
|
||||
|
||||
glfwMakeContextCurrent(window);
|
||||
GL.createCapabilities();
|
||||
|
||||
glViewport(0, 0, 1024, 720);
|
||||
|
||||
glfwSwapInterval(1);
|
||||
glfwShowWindow(window);
|
||||
}
|
||||
|
||||
private void loop() {
|
||||
GL.createCapabilities();
|
||||
glClearColor(0.33f, 0.48f, 0.48f, 1.0f);
|
||||
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
Matrix4f modelMatrix = new Matrix4f();
|
||||
|
||||
while ( !glfwWindowShouldClose(window) ) {
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
shader.bind();
|
||||
|
||||
modelMatrix.rotateXYZ(0, 0.01f, 0);
|
||||
|
||||
shader.setUniform("projection", camera.getProjection());
|
||||
shader.setUniform("view", camera.getView());
|
||||
shader.setUniform("model", modelMatrix);
|
||||
shader.setUniform("lightDirection", sun.direction);
|
||||
shader.setUniform("lightColor", sun.color);
|
||||
shader.setUniform("lightIntensity", sun.intensity);
|
||||
|
||||
cube.render();
|
||||
|
||||
shader.unbind();
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
|
||||
glfwPollEvents();
|
||||
|
||||
@@ -10,5 +10,5 @@ public class Version {
|
||||
|
||||
public static final int VERSION_MAJ = 0;
|
||||
public static final int VERSION_MIN = 0;
|
||||
public static final int VERSION_PATCH = 1;
|
||||
public static final int VERSION_PATCH = 2;
|
||||
}
|
||||
@@ -5,4 +5,26 @@ public class Vertex {
|
||||
public Vector3f position;
|
||||
public Vector2f uv;
|
||||
public Vector3f norm;
|
||||
|
||||
public static float[] interleaveVertexData(float[] positions, float[] uvs, float[] norms) {
|
||||
int vertexCount = positions.length / 3;
|
||||
float[] result = new float[vertexCount * 8];
|
||||
|
||||
for (int i = 0; i < vertexCount; i++) {
|
||||
result[i * 8] = positions[i * 3];
|
||||
result[i * 8 + 1] = positions[i * 3 + 1];
|
||||
result[i * 8 + 2] = positions[i * 3 + 2];
|
||||
|
||||
// UVs (2 floats)
|
||||
result[i * 8 + 3] = uvs[i * 2];
|
||||
result[i * 8 + 4] = uvs[i * 2 + 1];
|
||||
|
||||
// Normals (3 floats)
|
||||
result[i * 8 + 5] = norms[i * 3];
|
||||
result[i * 8 + 6] = norms[i * 3 + 1];
|
||||
result[i * 8 + 7] = norms[i * 3 + 2];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
19
src/main/resources/log4j2.xml
Normal file
19
src/main/resources/log4j2.xml
Normal file
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration status="WARN">
|
||||
<Appenders>
|
||||
<Console name="Console" target="SYSTEM_OUT">
|
||||
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
|
||||
</Console>
|
||||
|
||||
<File name="LogFile" fileName="logs/tony.log">
|
||||
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
|
||||
</File>
|
||||
</Appenders>
|
||||
|
||||
<Loggers>
|
||||
<Root level="debug">
|
||||
<AppenderRef ref="Console"/>
|
||||
<AppenderRef ref="LogFile"/>
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
||||
@@ -2,6 +2,24 @@
|
||||
|
||||
out vec4 color;
|
||||
|
||||
in vec2 texCoord;
|
||||
in vec3 normal;
|
||||
|
||||
uniform sampler2D texture_diffuse;
|
||||
uniform vec3 lightDirection;
|
||||
uniform vec3 lightColor;
|
||||
uniform float lightIntensity;
|
||||
|
||||
void main() {
|
||||
color = vec4(1.0, 0.0, 0.0, 1.0);
|
||||
vec3 normal = normalize(outNormal);
|
||||
vec3 lightDir = normalize(-lightDirection);
|
||||
|
||||
float diff = max(dot(normal, lightDir), 0.0);
|
||||
|
||||
float ambient = 0.2;
|
||||
|
||||
vec3 diffuse = diff * lightColor * lightIntensity;
|
||||
vec4 texColor = texture(texture_diffuse, texCoord);
|
||||
|
||||
color = vec4(ambient + diffuse, 1.0) * texColor;
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
#version 400 core
|
||||
|
||||
layout (location = 0) in vec3 position;
|
||||
layout (location = 1) in vec2 uv;
|
||||
layout (location = 2) in vec3
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
|
||||
Reference in New Issue
Block a user