Added the very basics of my own 3d graphics engine!

This commit is contained in:
Andrew Lalis 2022-07-03 22:12:40 +02:00
parent e47592fcaf
commit 26881c1520
14 changed files with 619 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.idea/
target/

215
client/pom.xml Normal file
View File

@ -0,0 +1,215 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>ace-of-shades-2</artifactId>
<groupId>nl.andrewl</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>aos2-client</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<lwjgl.version>3.3.1</lwjgl.version>
</properties>
<profiles>
<profile>
<id>lwjgl-natives-linux-amd64</id>
<activation>
<os>
<family>unix</family>
<arch>amd64</arch>
</os>
</activation>
<properties>
<lwjgl.natives>natives-linux</lwjgl.natives>
</properties>
</profile>
<profile>
<id>lwjgl-natives-linux-aarch64</id>
<activation>
<os>
<family>unix</family>
<arch>aarch64</arch>
</os>
</activation>
<properties>
<lwjgl.natives>natives-linux-arm64</lwjgl.natives>
</properties>
</profile>
<profile>
<id>lwjgl-natives-linux-arm</id>
<activation>
<os>
<family>unix</family>
<arch>arm</arch>
</os>
</activation>
<properties>
<lwjgl.natives>natives-linux-arm32</lwjgl.natives>
</properties>
</profile>
<profile>
<id>lwjgl-natives-linux-arm32</id>
<activation>
<os>
<family>unix</family>
<arch>arm32</arch>
</os>
</activation>
<properties>
<lwjgl.natives>natives-linux-arm32</lwjgl.natives>
</properties>
</profile>
<profile>
<id>lwjgl-natives-macos-x86_64</id>
<activation>
<os>
<family>mac</family>
<arch>x86_64</arch>
</os>
</activation>
<properties>
<lwjgl.natives>natives-macos</lwjgl.natives>
</properties>
</profile>
<profile>
<id>lwjgl-natives-macos-aarch64</id>
<activation>
<os>
<family>mac</family>
<arch>aarch64</arch>
</os>
</activation>
<properties>
<lwjgl.natives>natives-macos-arm64</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>
<profile>
<id>lwjgl-natives-windows-x86</id>
<activation>
<os>
<family>windows</family>
<arch>x86</arch>
</os>
</activation>
<properties>
<lwjgl.natives>natives-windows-x86</lwjgl.natives>
</properties>
</profile>
<profile>
<id>lwjgl-natives-windows-aarch64</id>
<activation>
<os>
<family>windows</family>
<arch>aarch64</arch>
</os>
</activation>
<properties>
<lwjgl.natives>natives-windows-arm64</lwjgl.natives>
</properties>
</profile>
</profiles>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-bom</artifactId>
<version>${lwjgl.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>nl.andrewl</groupId>
<artifactId>aos2-core</artifactId>
<version>${parent.version}</version>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl</artifactId>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-assimp</artifactId>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-glfw</artifactId>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-meshoptimizer</artifactId>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-openal</artifactId>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-opengl</artifactId>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-stb</artifactId>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl</artifactId>
<classifier>${lwjgl.natives}</classifier>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-assimp</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-meshoptimizer</artifactId>
<classifier>${lwjgl.natives}</classifier>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-openal</artifactId>
<classifier>${lwjgl.natives}</classifier>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-opengl</artifactId>
<classifier>${lwjgl.natives}</classifier>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-stb</artifactId>
<classifier>${lwjgl.natives}</classifier>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,99 @@
package nl.andrewl.aos2_client;
import nl.andrewl.aos_core.FileUtils;
import nl.andrewl.aos_core.model.Chunk;
import org.joml.Matrix4f;
import org.joml.Vector3f;
import org.lwjgl.BufferUtils;
import org.lwjgl.Version;
import org.lwjgl.glfw.Callbacks;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GLUtil;
import org.lwjgl.system.MemoryUtil;
import java.io.IOException;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL46.*;
public class Aos2Client {
public static void main(String[] args) throws IOException {
System.out.println("LWJGL Version: " + Version.getVersion());
GLFWErrorCallback.createPrint(System.err).set();
if (!glfwInit()) throw new IllegalStateException("Could not initialize GLFW");
glfwDefaultWindowHints();
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
long windowHandle = glfwCreateWindow(800, 600, "Ace of Shades 2", MemoryUtil.NULL, MemoryUtil.NULL);
if (windowHandle == MemoryUtil.NULL) throw new RuntimeException("Failed to create GLFW window.");
glfwSetKeyCallback(windowHandle, (window, key, scancode, action, mods) -> {
if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE) {
glfwSetWindowShouldClose(windowHandle, true);
}
});
glfwSetInputMode(windowHandle, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetInputMode(windowHandle, GLFW_RAW_MOUSE_MOTION, GLFW_TRUE);
glfwSetWindowPos(windowHandle, 50, 50);
glfwSetCursorPos(windowHandle, 0, 0);
glfwMakeContextCurrent(windowHandle);
glfwSwapInterval(1);
glfwShowWindow(windowHandle);
GL.createCapabilities();
GLUtil.setupDebugMessageCallback(System.out);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glCullFace(GL_BACK);
Chunk chunk = Chunk.of((byte) 64);
Matrix4f projectionTransform = new Matrix4f().perspective(70, 800 / 600.0f, 0.01f, 100.0f);
Matrix4f viewTransform = new Matrix4f()
.lookAt(new Vector3f(-5, 50, -10), new Vector3f(8, 0, 8), new Vector3f(0, 1, 0));
ChunkMesh mesh = new ChunkMesh(chunk);
int shaderProgram = createShaderProgram();
int projectionTransformUniform = glGetUniformLocation(shaderProgram, "projectionTransform");
int viewTransformUniform = glGetUniformLocation(shaderProgram, "viewTransform");
glUniformMatrix4fv(projectionTransformUniform, false, projectionTransform.get(new float[16]));
while (!glfwWindowShouldClose(windowHandle)) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUniformMatrix4fv(viewTransformUniform, false, viewTransform.get(new float[16]));
mesh.draw();
glfwSwapBuffers(windowHandle);
glfwPollEvents();
}
Callbacks.glfwFreeCallbacks(windowHandle);
glfwDestroyWindow(windowHandle);
glfwTerminate();
glfwSetErrorCallback(null).free();
}
private static int createShaderProgram() throws IOException {
int prog = glCreateProgram();
int fragShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragShader, FileUtils.readClasspathFile("shader/fragment.glsl"));
glCompileShader(fragShader);
glAttachShader(prog, fragShader);
int vertShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertShader, FileUtils.readClasspathFile("shader/vertex.glsl"));
glCompileShader(vertShader);
glAttachShader(prog, vertShader);
glValidateProgram(prog);
glLinkProgram(prog);
glUseProgram(prog);
return prog;
}
}

View File

@ -0,0 +1,58 @@
package nl.andrewl.aos2_client;
import nl.andrewl.aos_core.model.Chunk;
import org.lwjgl.BufferUtils;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import static org.lwjgl.opengl.GL46.*;
public class ChunkMesh {
private final int vboId;
private final int vaoId;
private final int eboId;
private final int indiciesCount;
public ChunkMesh(Chunk chunk) {
this.vboId = glGenBuffers();
this.vaoId = glGenVertexArrays();
this.eboId = glGenBuffers();
var meshData = chunk.generateMesh();
System.out.println(meshData.first().size());
FloatBuffer vertexBuffer = BufferUtils.createFloatBuffer(3 * meshData.first().size());
for (var vertex : meshData.first()) {
vertexBuffer.put(vertex.x);
vertexBuffer.put(vertex.y);
vertexBuffer.put(vertex.z);
}
vertexBuffer.flip();
IntBuffer indexBuffer = BufferUtils.createIntBuffer(meshData.second().size());
for (var index : meshData.second()) {
indexBuffer.put(index);
}
indexBuffer.flip();
this.indiciesCount = meshData.second().size();
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, vertexBuffer, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eboId);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexBuffer, GL_STATIC_DRAW);
glBindVertexArray(vaoId);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 3 * Float.BYTES, 0);
}
public void draw() {
// Bind elements.
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eboId);
glBindVertexArray(vaoId);
glDrawElements(GL_TRIANGLES, indiciesCount, GL_UNSIGNED_INT, 0);
}
}

View File

@ -0,0 +1,10 @@
#version 460 core
in vec3 vertexPosition;
in vec3 vertexColor;
out vec4 fragmentColor;
void main() {
fragmentColor = vec4(vertexColor, 1.0);
}

View File

@ -0,0 +1,15 @@
#version 460 core
layout (location = 0) in vec3 vertexPositionIn;
uniform mat4 projectionTransform;
uniform mat4 viewTransform;
out vec3 vertexPosition;
out vec3 vertexColor;
void main() {
gl_Position = projectionTransform * viewTransform * vec4(vertexPositionIn, 1.0);
vertexPosition = vertexPositionIn;
vertexColor = vec3(1.0, 0.5, 0.5);
}

27
core/pom.xml Normal file
View File

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>ace-of-shades-2</artifactId>
<groupId>nl.andrewl</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>aos2-core</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.joml/joml -->
<dependency>
<groupId>org.joml</groupId>
<artifactId>joml</artifactId>
<version>1.10.4</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,15 @@
package nl.andrewl.aos_core;
import java.io.IOException;
import java.io.InputStream;
public final class FileUtils {
private FileUtils() {}
public static String readClasspathFile(String resource) throws IOException {
try (InputStream in = FileUtils.class.getClassLoader().getResourceAsStream(resource)) {
if (in == null) throw new IOException("Could not load classpath resource: " + resource);
return new String(in.readAllBytes());
}
}
}

View File

@ -0,0 +1,3 @@
package nl.andrewl.aos_core;
public record Pair<A, B>(A first, B second) {}

View File

@ -0,0 +1,3 @@
package nl.andrewl.aos_core;
public record Triple<A, B, C>(A first, B second, C third) {}

View File

@ -0,0 +1,97 @@
package nl.andrewl.aos_core.model;
import nl.andrewl.aos_core.Pair;
import org.joml.Vector3f;
import org.joml.Vector3i;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Chunk {
/**
* The size of a chunk, in terms of the number of blocks on one axis of the cube.
*/
public static final byte SIZE = 16;
private final byte[] blocks = new byte[SIZE * SIZE * SIZE];
public byte getBlockAt(int x, int y, int z) {
int idx = x * SIZE * SIZE + y * SIZE + z;
if (idx < 0 || idx >= SIZE * SIZE * SIZE) return 0;
return blocks[x * SIZE * SIZE + y * SIZE + z];
}
public byte getBlockAt(Vector3i localPosition) {
return getBlockAt(localPosition.x, localPosition.y, localPosition.z);
}
public byte[] getBlocks() {
return blocks;
}
public Pair<List<Vector3f>, List<Integer>> generateMesh() {
List<Vector3f> vertexList = new ArrayList<>();
List<Integer> indexList = new ArrayList<>();
int elementIdx = 0;
for (int x = 0; x < SIZE; x++) {
for (int y = 0; y < SIZE; y++) {
for (int z = 0; z < SIZE; z++) {
byte block = getBlockAt(x, y, z);
if (block == 0) continue;
// Top
if (getBlockAt(x, y + 1, z) == 0) {
vertexList.add(new Vector3f(x + 1, y, z)); // 0
vertexList.add(new Vector3f(x, y, z)); // 1
vertexList.add(new Vector3f(x, y, z + 1)); // 2
vertexList.add(new Vector3f(x + 1, y, z + 1));// 3
indexList.add(elementIdx);
indexList.add(elementIdx + 1);
indexList.add(elementIdx + 2);
indexList.add(elementIdx + 2);
indexList.add(elementIdx + 3);
indexList.add(elementIdx);
elementIdx += 4;
}
// Bottom
if (getBlockAt(x, y - 1, z) == 0) {
vertexList.add(new Vector3f(x + 1, y - 1, z)); // 0
vertexList.add(new Vector3f(x, y - 1, z)); // 1
vertexList.add(new Vector3f(x, y - 1, z + 1)); // 2
vertexList.add(new Vector3f(x + 1, y - 1, z + 1));// 3
indexList.add(elementIdx);
indexList.add(elementIdx + 2);
indexList.add(elementIdx + 1);
indexList.add(elementIdx);
indexList.add(elementIdx + 3);
indexList.add(elementIdx + 2);
elementIdx += 4;
}
// Positive z
// Negative z
// Positive x
// Negative x
}
}
}
return new Pair<>(vertexList, indexList);
}
public static Chunk of(byte value) {
Chunk c = new Chunk();
Arrays.fill(c.blocks, value);
return c;
}
public static Vector3f getColor(byte blockValue) {
float v = blockValue / 128.0f;
return new Vector3f(v);
}
}

View File

@ -0,0 +1,27 @@
package nl.andrewl.aos_core.model;
import org.joml.Vector3i;
import java.util.HashMap;
import java.util.Map;
public class World {
Map<Vector3i, Chunk> chunkMap = new HashMap<>();
public byte getBlockAt(int x, int y, int z) {
int chunkX = x / Chunk.SIZE;
int localX = x % Chunk.SIZE;
int chunkY = y / Chunk.SIZE;
int localY = y % Chunk.SIZE;
int chunkZ = z / Chunk.SIZE;
int localZ = z % Chunk.SIZE;
Vector3i chunkPos = new Vector3i(chunkX, chunkY, chunkZ);
Chunk chunk = chunkMap.get(chunkPos);
if (chunk == null) return 0;
return chunk.getBlockAt(localX, localY, localZ);
}
public Chunk getChunkAt(Vector3i chunkPos) {
return chunkMap.get(chunkPos);
}
}

22
pom.xml Normal file
View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>nl.andrewl</groupId>
<artifactId>ace-of-shades-2</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>core</module>
<module>server</module>
<module>client</module>
</modules>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
</project>

26
server/pom.xml Normal file
View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>ace-of-shades-2</artifactId>
<groupId>nl.andrewl</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>aos2-server</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>nl.andrewl</groupId>
<artifactId>aos2-core</artifactId>
<version>${parent.version}</version>
</dependency>
</dependencies>
</project>