Archived
Private
Public Access
1
0

Initial commit

This commit is contained in:
2022-09-04 12:45:01 +02:00
commit f4a01d6a69
11601 changed files with 4206660 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
package de.craftix.lwjgl.engine.jade;
import org.joml.Matrix4f;
import org.joml.Vector2f;
import org.joml.Vector3f;
public class Camera {
private Matrix4f projectionMatrix, viewMatrix;
public Vector2f position;
public Camera(Vector2f position) {
this.position = position;
this.projectionMatrix = new Matrix4f();
this.viewMatrix = new Matrix4f();
adjustProjection();
}
public void adjustProjection() {
projectionMatrix.identity();
projectionMatrix.ortho(0.0f, 32.0f * 40.0f, 0.0f, 32.0f * 21.0f, 0.0f, 100.0f);
}
public Matrix4f getViewMatrix() {
Vector3f cameraFront = new Vector3f(0.0f, 0.0f, -1.0f);
Vector3f cameraUp = new Vector3f(0.0f, 1.0f, 0.0f);
this.viewMatrix.identity();
viewMatrix.lookAt(new Vector3f(position.x, position.y, 20.0f),
cameraFront.add(position.x, position.y, 0.0f),
cameraUp);
return this.viewMatrix;
}
public Matrix4f getProjectionMatrix() {
return this.projectionMatrix;
}
}

View File

@@ -0,0 +1,20 @@
package de.craftix.lwjgl.engine.jade;
import static org.lwjgl.glfw.GLFW.GLFW_PRESS;
import static org.lwjgl.glfw.GLFW.GLFW_RELEASE;
public class KeyListener {
private static final KeyListener instance = new KeyListener();
private boolean[] keyPressed = new boolean[350];
private KeyListener() {}
public static KeyListener get() { return instance; }
public static void keyCallback(long window, int key, int scanCode, int action, int mods) {
if (action == GLFW_PRESS) get().keyPressed[key] = true;
if (action == GLFW_RELEASE) get().keyPressed[key] = false;
}
public static boolean isKeyPressed(int keyCode) { return get().keyPressed[keyCode]; }
}

View File

@@ -0,0 +1,65 @@
package de.craftix.lwjgl.engine.jade;
import java.awt.*;
import java.util.Arrays;
import static org.lwjgl.glfw.GLFW.GLFW_PRESS;
import static org.lwjgl.glfw.GLFW.GLFW_RELEASE;
public class MouseListener {
private static final MouseListener instance = new MouseListener();
private double scrollX, scrollY, xPos, yPos, lastX, lastY;
private final boolean mouseButtonPressed[] = new boolean[MouseInfo.getNumberOfButtons()];
private boolean isDragging;
private MouseListener() {
this.scrollX = 0;
this.scrollY = 0;
this.xPos = 0;
this.yPos = 0;
this.lastX = 0;
this.lastY = 0;
this.isDragging = false;
}
public static MouseListener get() { return instance; }
public static void mousePosCallback(long window, double xPos, double yPos) {
get().lastX = get().xPos;
get().lastY = get().yPos;
get().xPos = xPos;
get().yPos = yPos;
for (boolean button : get().mouseButtonPressed) { if (button) { get().isDragging = true; break; } }
}
public static void mouseButtonCallback(long window, int button, int action, int mods) {
if (action == GLFW_PRESS) {
get().mouseButtonPressed[button] = true;
}
if (action == GLFW_RELEASE) {
get().mouseButtonPressed[button] = false;
get().isDragging = false;
}
}
public static void mouseScrollCallback(long window, double xOffset, double yOffset) {
get().scrollX = xOffset;
get().scrollY = yOffset;
}
public static void endFrame() {
get().scrollX = 0;
get().scrollY = 0;
get().lastX = get().xPos;
get().lastY = get().yPos;
}
public static float getX() { return (float)get().xPos; }
public static float getY() { return (float)get().yPos; }
public static float getDX() { return (float)(get().lastX - get().xPos); }
public static float getDY() { return (float)(get().lastY - get().yPos); }
public static float getScrollX() { return (float)get().scrollX; }
public static float getScrollY() { return (float)get().scrollY; }
public static boolean isDragging() { return get().isDragging; }
public static boolean mouseButtonDown(int button) { return get().mouseButtonPressed[button]; }
}

View File

@@ -0,0 +1,13 @@
package de.craftix.lwjgl.engine.jade;
public abstract class Scene {
protected Camera camera;
public Scene() {}
public abstract void update(float dt);
public void init() {}
}

View File

@@ -0,0 +1,94 @@
package de.craftix.lwjgl.engine.jade;
import org.lwjgl.Version;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.opengl.GL;
import static org.lwjgl.glfw.Callbacks.glfwFreeCallbacks;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.NULL;
public class Window {
private int width, height;
private String title;
private long glfwWindow;
private boolean running = false;
public float r = 1, g = 1, b = 1, a = 1;
private static Window window = null;
private static Scene currentScene;
private Window() {}
public static Window get() {
if (window == null)
window = new Window();
return window;
}
public void changeScene(Scene scene) { currentScene = scene; if (running) currentScene.init(); }
public void run(int width, int height, String title) {
this.width = width;
this.height = height;
this.title = title;
System.out.println("Hello LWJGL " + Version.getVersion() + "!");
init();
loop();
glfwFreeCallbacks(glfwWindow);
glfwDestroyWindow(glfwWindow);
glfwTerminate();
GLFWErrorCallback.createPrint(null).free();
}
private void init() {
GLFWErrorCallback.createPrint(System.err).set();
if (!glfwInit()) throw new IllegalStateException("Unable to Initialise GLFW!");
glfwDefaultWindowHints();
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
glfwWindowHint(GLFW_MAXIMIZED, GLFW_FALSE);
glfwWindow = glfwCreateWindow(width, height, title, NULL, NULL);
if (glfwWindow == NULL) throw new IllegalStateException("Failed to create GLFW Window!");
glfwSetCursorPosCallback(glfwWindow, MouseListener::mousePosCallback);
glfwSetMouseButtonCallback(glfwWindow, MouseListener::mouseButtonCallback);
glfwSetScrollCallback(glfwWindow, MouseListener::mouseScrollCallback);
glfwSetKeyCallback(glfwWindow, KeyListener::keyCallback);
glfwMakeContextCurrent(glfwWindow);
glfwSwapInterval(1);
glfwShowWindow(glfwWindow);
GL.createCapabilities();
if (currentScene != null) currentScene.init();
}
private void loop() {
float beginTime = (float)glfwGetTime();
float endTime;
float dt = -1.0f;
while (!glfwWindowShouldClose(glfwWindow)) {
glfwPollEvents();
glClearColor(r, g, b, a);
glClear(GL_COLOR_BUFFER_BIT);
if (dt >= 0 && currentScene != null) currentScene.update(dt);
glfwSwapBuffers(glfwWindow);
endTime = (float)glfwGetTime();
dt = endTime - beginTime;
beginTime = endTime;
}
}
}

View File

@@ -0,0 +1,152 @@
package de.craftix.lwjgl.engine.render;
import org.joml.*;
import org.lwjgl.BufferUtils;
import java.io.IOException;
import java.nio.FloatBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import static org.lwjgl.opengl.GL11.GL_FALSE;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL20.glGetProgramInfoLog;
public class Shader {
private final String path;
private int shaderProgram;
private int vertexID, fragmentID;
private String vertexSource;
private String fragmentSource;
public Shader(String path) {
this.path = path;
try {
String source = new String(Files.readAllBytes(Path.of(Shader.class.getClassLoader().getResource(path).toURI())));
String[] splitString = source.split("(#type)( )+([a-zA-Z]+)");
int index = source.indexOf("#type") + 6;
int eol = source.indexOf("\r\n", index);
String firstPattern = source.substring(index, eol).trim();
index = source.indexOf("#type", eol) + 6;
eol = source.indexOf("\r\n", index);
String secondPattern = source.substring(index, eol).trim();
if (firstPattern.equalsIgnoreCase("vertex")) {
vertexSource = splitString[1];
}else if (firstPattern.equalsIgnoreCase("fragment")) {
fragmentSource = splitString[1];
} else throw new IOException("Unexpected token '" + firstPattern + "' in '" + path + "'");
if (secondPattern.equalsIgnoreCase("vertex")) {
vertexSource = splitString[2];
}else if (secondPattern.equalsIgnoreCase("fragment")) {
fragmentSource = splitString[2];
} else throw new IOException("Unexpected token '" + secondPattern + "' in '" + path + "'");
}catch (Exception e) {
e.printStackTrace();
assert false;
}
}
public void compile() {
vertexID = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexID, vertexSource);
glCompileShader(vertexID);
int success = glGetShaderi(vertexID, GL_COMPILE_STATUS);
if (success == GL_FALSE) {
int len = glGetShaderi(vertexID, GL_INFO_LOG_LENGTH);
System.err.println("ERROR: '" + path + "' \n\tVertex shader compilation failed.");
System.err.println(glGetShaderInfoLog(vertexID, len));
assert false;
}
fragmentID = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentID, fragmentSource);
glCompileShader(fragmentID);
success = glGetShaderi(fragmentID, GL_COMPILE_STATUS);
if (success == GL_FALSE) {
int len = glGetShaderi(fragmentID, GL_INFO_LOG_LENGTH);
System.err.println("ERROR: '" + path + "' \n\tFragment shader compilation failed.");
System.err.println(glGetShaderInfoLog(fragmentID, len));
assert false;
}
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexID);
glAttachShader(shaderProgram, fragmentID);
glLinkProgram(shaderProgram);
success = glGetProgrami(shaderProgram, GL_LINK_STATUS);
if (success == GL_FALSE) {
int len = glGetProgrami(shaderProgram, GL_INFO_LOG_LENGTH);
System.err.println("ERROR: '" + path + "' \n\tLinking shaders failed.");
System.err.println(glGetProgramInfoLog(shaderProgram, len));
assert false;
}
}
public void use() {
glUseProgram(shaderProgram);
}
public void detach() {
glUseProgram(0);
}
public void uploadMat4f(String varName, Matrix4f mat4) {
int varLocation = glGetUniformLocation(shaderProgram, varName);
use();
FloatBuffer matBuffer = BufferUtils.createFloatBuffer(16);
mat4.get(matBuffer);
glUniformMatrix4fv(varLocation, false, matBuffer);
}
public void uploadMat3f(String varName, Matrix3f mat3) {
int varLocation = glGetUniformLocation(shaderProgram, varName);
use();
FloatBuffer matBuffer = BufferUtils.createFloatBuffer(9);
mat3.get(matBuffer);
glUniformMatrix3fv(varLocation, false, matBuffer);
}
public void uploadVec4f(String varName, Vector4f vec) {
int varLocation = glGetUniformLocation(shaderProgram, varName);
use();
glUniform4f(varLocation, vec.x, vec.y, vec.z, vec.w);
}
public void uploadVec3f(String varName, Vector3f vec) {
int varLocation = glGetUniformLocation(shaderProgram, varName);
use();
glUniform3f(varLocation, vec.x, vec.y, vec.z);
}
public void uploadVec2f(String varName, Vector2f vec) {
int varLocation = glGetUniformLocation(shaderProgram, varName);
use();
glUniform2f(varLocation, vec.x, vec.y);
}
public void uploadFloat(String varName, float val) {
int varLocation = glGetUniformLocation(shaderProgram, varName);
use();
glUniform1f(varLocation, val);
}
public void uploadInt(String varName, int val) {
int varLocation = glGetUniformLocation(shaderProgram, varName);
use();
glUniform1i(varLocation, val);
}
public void uploadTexture(String varName, int slot) {
int varLocation = glGetUniformLocation(shaderProgram, varName);
use();
glUniform1i(varLocation, slot);
}
}

View File

@@ -0,0 +1,55 @@
package de.craftix.lwjgl.engine.render;
import org.lwjgl.BufferUtils;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.util.Objects;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.stb.STBImage.stbi_image_free;
import static org.lwjgl.stb.STBImage.stbi_load;
public class Texture {
private String path;
private int texID;
public Texture(String path) {
try {
this.path = Objects.requireNonNull(Texture.class.getClassLoader().getResource(path)).toURI().toString().replace("file:/", "");
texID = glGenTextures();
glBindTexture(GL_TEXTURE_2D, texID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
IntBuffer width = BufferUtils.createIntBuffer(1);
IntBuffer height = BufferUtils.createIntBuffer(1);
IntBuffer channels = BufferUtils.createIntBuffer(1);
ByteBuffer image = stbi_load(this.path, width, height, channels, 0);
if (image != null && channels.get(0) == 4)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width.get(0), height.get(0), 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
else if (image != null && channels.get(0) == 3)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width.get(0), height.get(0), 0, GL_RGB, GL_UNSIGNED_BYTE, image);
else throw new IllegalStateException("Could not load texture in OpenGL");
stbi_image_free(image);
} catch (URISyntaxException e) {
e.printStackTrace();
assert false;
}
}
public void bind() {
glBindTexture(GL_TEXTURE_2D, texID);
}
public void unbind() {
glBindTexture(GL_TEXTURE_2D, 0);
}
}

View File

@@ -0,0 +1,14 @@
package de.craftix.test;
import de.craftix.lwjgl.engine.jade.Window;
import de.craftix.test.scenes.LevelEditorScene;
public class Main {
public static void main(String[] args) {
Window window = Window.get();
window.changeScene(new LevelEditorScene());
window.run(1280, 720, "GLFW Engine");
}
}

View File

@@ -0,0 +1,101 @@
package de.craftix.test.scenes;
import de.craftix.lwjgl.engine.jade.Camera;
import de.craftix.lwjgl.engine.jade.Scene;
import de.craftix.lwjgl.engine.render.Shader;
import de.craftix.lwjgl.engine.render.Texture;
import org.joml.Vector2f;
import org.lwjgl.BufferUtils;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.*;
public class LevelEditorScene extends Scene {
private Shader defaultShader;
private Texture testTexture;
private float[] vertexArray = {
// position // color // UV Coordinates
100.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1, 1, // Bottom right 0
0.0f, 100.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0, 0, // Top left 1
100.0f, 100.0f, 0.0f , 1.0f, 0.0f, 1.0f, 1.0f, 1, 0, // Top right 2
0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0, 1, // Bottom left 3
};
private int[] elementArray = {
2, 1, 0,
0, 1, 3
};
private int vaoID, vboID, eboID;
public LevelEditorScene() {
}
@Override
public void init() {
this.camera = new Camera(new Vector2f(-500, -200));
defaultShader = new Shader("assets/shaders/default.glsl");
defaultShader.compile();
testTexture = new Texture("assets/textures/testImage.jpg");
//----------------------------------------
vaoID = glGenVertexArrays();
glBindVertexArray(vaoID);
FloatBuffer vertexBuffer = BufferUtils.createFloatBuffer(vertexArray.length);
vertexBuffer.put(vertexArray).flip();
vboID = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferData(GL_ARRAY_BUFFER, vertexBuffer, GL_STATIC_DRAW);
IntBuffer elementBuffer = BufferUtils.createIntBuffer(elementArray.length);
elementBuffer.put(elementArray).flip();
eboID = glGenBuffers();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eboID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, elementBuffer, GL_STATIC_DRAW);
int positionsSize = 3;
int colorSize = 4;
int uvSize = 2;
int vertexSizeInBytes = (positionsSize + colorSize + uvSize) * Float.BYTES;
glVertexAttribPointer(0, positionsSize, GL_FLOAT, false, vertexSizeInBytes, 0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, colorSize, GL_FLOAT, false, vertexSizeInBytes, positionsSize * Float.BYTES);
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, uvSize, GL_FLOAT, false, vertexSizeInBytes, (positionsSize + colorSize) * Float.BYTES);
glEnableVertexAttribArray(2);
}
@Override
public void update(float dt) {
defaultShader.use();
defaultShader.uploadTexture("TEX_SAMPLER", 0);
glActiveTexture(GL_TEXTURE0);
testTexture.bind();
defaultShader.uploadMat4f("uProjection", camera.getProjectionMatrix());
defaultShader.uploadMat4f("uView", camera.getViewMatrix());
glBindVertexArray(vaoID);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glDrawElements(GL_TRIANGLES, elementArray.length, GL_UNSIGNED_INT, 0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glBindVertexArray(0);
defaultShader.detach();
}
}

View File

@@ -0,0 +1,14 @@
package de.craftix.test.scenes;
import de.craftix.lwjgl.engine.jade.Scene;
public class LevelScene extends Scene {
public LevelScene() {}
@Override
public void update(float dt) {
}
}

View File

@@ -0,0 +1,33 @@
#type vertex
#version 330 core
layout (location=0) in vec3 aPos;
layout (location=1) in vec4 aColor;
layout (location=2) in vec2 aTexCoords;
uniform mat4 uProjection;
uniform mat4 uView;
out vec4 fColor;
out vec2 fTexCoords;
void main()
{
fColor = aColor;
fTexCoords = aTexCoords;
gl_Position = uProjection * uView * vec4(aPos, 1.0);
}
#type fragment
#version 330 core
uniform sampler2D TEX_SAMPLER;
in vec4 fColor;
in vec2 fTexCoords;
out vec4 color;
void main()
{
color = texture(TEX_SAMPLER, fTexCoords);
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 55 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 420 B