Skip to content

Commit cc1417e

Browse files
authored
Merge pull request #2 from OpenGameOrg/v0.0.1a
First outlines, basic rendering and camera features
2 parents 4e4ae22 + d145797 commit cc1417e

52 files changed

Lines changed: 68140 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@
2121

2222
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2323
hs_err_pid*
24+
25+
.idea/
26+
target/

pom.xml

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.opengame</groupId>
8+
<artifactId>engine</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>11</maven.compiler.source>
13+
<maven.compiler.target>11</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<lwjgl.version>3.3.1</lwjgl.version>
16+
<joml.version>1.10.5</joml.version>
17+
<lwjgl.natives>natives-linux</lwjgl.natives>
18+
</properties>
19+
20+
<dependencyManagement>
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.lwjgl</groupId>
24+
<artifactId>lwjgl-bom</artifactId>
25+
<version>${lwjgl.version}</version>
26+
<scope>import</scope>
27+
<type>pom</type>
28+
</dependency>
29+
</dependencies>
30+
</dependencyManagement>
31+
32+
<dependencies>
33+
<dependency>
34+
<groupId>org.lwjgl</groupId>
35+
<artifactId>lwjgl</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.lwjgl</groupId>
39+
<artifactId>lwjgl</artifactId>
40+
<classifier>${lwjgl.natives}</classifier>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.lwjgl</groupId>
44+
<artifactId>lwjgl-bgfx</artifactId>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.lwjgl</groupId>
48+
<artifactId>lwjgl-bgfx</artifactId>
49+
<classifier>${lwjgl.natives}</classifier>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.lwjgl</groupId>
53+
<artifactId>lwjgl-glfw</artifactId>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.lwjgl</groupId>
57+
<artifactId>lwjgl-glfw</artifactId>
58+
<classifier>${lwjgl.natives}</classifier>
59+
</dependency>
60+
<dependency>
61+
<groupId>org.lwjgl</groupId>
62+
<artifactId>lwjgl-assimp</artifactId>
63+
</dependency>
64+
<dependency>
65+
<groupId>org.lwjgl</groupId>
66+
<artifactId>lwjgl-assimp</artifactId>
67+
<classifier>${lwjgl.natives}</classifier>
68+
</dependency>
69+
<dependency>
70+
<groupId>org.joml</groupId>
71+
<artifactId>joml</artifactId>
72+
<version>${joml.version}</version>
73+
</dependency>
74+
75+
<dependency>
76+
<groupId>org.projectlombok</groupId>
77+
<artifactId>lombok</artifactId>
78+
<version>1.18.24</version>
79+
<scope>provided</scope>
80+
</dependency>
81+
<dependency>
82+
<groupId>org.apache.logging.log4j</groupId>
83+
<artifactId>log4j-api</artifactId>
84+
<version>2.19.0</version>
85+
</dependency>
86+
<dependency>
87+
<groupId>org.apache.logging.log4j</groupId>
88+
<artifactId>log4j-core</artifactId>
89+
<version>2.19.0</version>
90+
</dependency>
91+
</dependencies>
92+
93+
<build>
94+
<plugins>
95+
<plugin>
96+
<artifactId>maven-assembly-plugin</artifactId>
97+
<configuration>
98+
<archive>
99+
<manifest>
100+
<mainClass>org.opengame.engine.TestClient</mainClass>
101+
</manifest>
102+
</archive>
103+
<descriptorRefs>
104+
<descriptorRef>jar-with-dependencies</descriptorRef>
105+
</descriptorRefs>
106+
</configuration>
107+
</plugin>
108+
</plugins>
109+
</build>
110+
</project>
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
package org.opengame.engine;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import lombok.extern.java.Log;
6+
import lombok.extern.log4j.Log4j2;
7+
import lombok.extern.slf4j.Slf4j;
8+
import org.joml.Vector2f;
9+
import org.lwjgl.PointerBuffer;
10+
import org.lwjgl.bgfx.BGFXInit;
11+
import org.lwjgl.glfw.GLFWErrorCallback;
12+
import org.lwjgl.glfw.GLFWNativeCocoa;
13+
import org.lwjgl.glfw.GLFWNativeWin32;
14+
import org.lwjgl.glfw.GLFWNativeX11;
15+
import org.lwjgl.system.MemoryStack;
16+
import org.lwjgl.system.Platform;
17+
import org.opengame.engine.app.AppConfig;
18+
import org.opengame.engine.event.EventBus;
19+
import org.opengame.engine.event.EventType;
20+
import org.opengame.engine.event.KeyEventData;
21+
import org.opengame.engine.event.MouseEventData;
22+
import org.opengame.engine.scene.Scene;
23+
24+
import java.nio.ByteBuffer;
25+
import java.util.Objects;
26+
27+
import static org.lwjgl.bgfx.BGFX.*;
28+
import static org.lwjgl.glfw.GLFW.*;
29+
import static org.lwjgl.system.MemoryUtil.NULL;
30+
31+
/**
32+
* Entry point for engine, init here
33+
*/
34+
@Log
35+
public class Engine {
36+
private static Engine instance;
37+
private long windowHandle;
38+
@Getter
39+
private AppConfig config;
40+
@Getter
41+
@Setter
42+
private Scene currentScene;
43+
44+
private float tickTime = 1000;
45+
private int msPerUpdate = 1000 / 20;
46+
47+
/**
48+
* Init Vulkan context and window
49+
*/
50+
public void Init(AppConfig config) {
51+
log.info("Init");
52+
log.info("Working directory: " + config.getWorkingDirectory());
53+
instance = this;
54+
this.config = config;
55+
initWindow(config);
56+
}
57+
58+
private void initWindow(AppConfig config) {
59+
if (!glfwInit()) {
60+
throw new RuntimeException("Cannot initialize GLFW");
61+
}
62+
63+
GLFWErrorCallback.createPrint().set();
64+
65+
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
66+
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
67+
68+
windowHandle = glfwCreateWindow(config.getWindowWidth(), config.getWindowHeight(),
69+
config.getAppName(), NULL, NULL);
70+
71+
setupInputCallbacks();
72+
73+
try (MemoryStack stack = MemoryStack.stackPush()) {
74+
BGFXInit init = BGFXInit.malloc();
75+
bgfx_init_ctor(init);
76+
init.type(BGFX_RENDERER_TYPE_OPENGL);
77+
init.resolution(it -> it
78+
.width(config.getWindowWidth())
79+
.height(config.getWindowHeight())
80+
.reset(BGFX_RESET_VSYNC));
81+
82+
switch (Platform.get()) {
83+
case LINUX:
84+
init.platformData()
85+
.ndt(GLFWNativeX11.glfwGetX11Display())
86+
.nwh(GLFWNativeX11.glfwGetX11Window(windowHandle));
87+
break;
88+
case MACOSX:
89+
init.platformData()
90+
.nwh(GLFWNativeCocoa.glfwGetCocoaWindow(windowHandle));
91+
break;
92+
case WINDOWS:
93+
init.platformData()
94+
.nwh(GLFWNativeWin32.glfwGetWin32Window(windowHandle));
95+
break;
96+
}
97+
98+
if (!bgfx_init(init)) {
99+
throw new RuntimeException("Error initializing bgfx renderer");
100+
}
101+
}
102+
103+
104+
logAvailableDevices();
105+
logAvailableRenderers();
106+
107+
log.info("bgfx renderer: " + bgfx_get_renderer_name(bgfx_get_renderer_type()));
108+
109+
bgfx_set_debug(BGFX_DEBUG_TEXT);
110+
bgfx_set_view_clear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x303030ff, 1.0f, 0);
111+
}
112+
113+
private void logAvailableRenderers() {
114+
var types = new int[BGFX_RENDERER_TYPE_COUNT];
115+
116+
var rendererCount = bgfx_get_supported_renderers(types);
117+
118+
log.info("Supported renderers:");
119+
for (int i = 0; i < rendererCount; i++) {
120+
log.info(bgfx_get_renderer_name(types[i]));
121+
}
122+
}
123+
124+
private void logAvailableDevices() {
125+
}
126+
127+
private void setupInputCallbacks() {
128+
glfwSetKeyCallback(windowHandle, (window, key, scancode, action, mods) -> {
129+
if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE) {
130+
glfwSetWindowShouldClose(window, true);
131+
}
132+
if (action == GLFW_PRESS || action == GLFW_RELEASE) {
133+
EventBus.broadcastEvent(EventType.KEY_PRESSED, new KeyEventData(key, action == GLFW_PRESS));
134+
}
135+
});
136+
137+
glfwSetMouseButtonCallback(windowHandle, (window, button, action, mods) -> {
138+
EventBus.broadcastEvent(EventType.MOUSE_BUTTON_EVENT, new KeyEventData(button, action == GLFW_PRESS));
139+
});
140+
141+
glfwSetCursorPosCallback(windowHandle, (window, xpos, ypos) -> {
142+
EventBus.broadcastEvent(EventType.MOUSE_MOVED, new MouseEventData(xpos, ypos));
143+
});
144+
}
145+
146+
public static void setCursorPos(Vector2f cursorPos) {
147+
glfwSetCursorPos(instance.windowHandle, cursorPos.x, cursorPos.y);
148+
}
149+
150+
public void startLoop() {
151+
long lastTime;
152+
long startTime = lastTime = glfwGetTimerValue();
153+
while (!glfwWindowShouldClose(windowHandle)) {
154+
glfwPollEvents();
155+
156+
long now = glfwGetTimerValue();
157+
long frameTime = now - lastTime;
158+
lastTime = now;
159+
160+
double freq = glfwGetTimerFrequency();
161+
double toMs = 1000.0 / freq;
162+
163+
double time = (now - startTime) / freq;
164+
165+
bgfx_set_view_rect(0, 0, 0, config.getWindowWidth(), config.getWindowHeight());
166+
bgfx_touch(0);
167+
168+
bgfx_dbg_text_clear(0, false);
169+
bgfx_dbg_text_printf(0, 1, 0x1f, "Hello bgfx!");
170+
171+
var frameMs = (float) toMs * frameTime;
172+
if (currentScene != null) {
173+
if (tickTime >= msPerUpdate) {
174+
currentScene.update();
175+
tickTime = 0;
176+
}
177+
tickTime += frameMs;
178+
currentScene.render((float) time, frameMs);
179+
}
180+
181+
bgfx_frame(false);
182+
}
183+
184+
bgfx_shutdown();
185+
glfwDestroyWindow(windowHandle);
186+
glfwTerminate();
187+
}
188+
189+
public static int getRenderer() {
190+
return bgfx_get_renderer_type();
191+
}
192+
193+
public static String getWorkingDirectory() {
194+
return instance.config.getWorkingDirectory();
195+
}
196+
197+
public static Scene getCurrentScene() {
198+
return instance.currentScene;
199+
}
200+
201+
public static void setScene(Scene scene) {
202+
instance.currentScene = scene;
203+
}
204+
205+
public static int getScreenWidth() {
206+
return instance.config.getWindowWidth();
207+
}
208+
209+
public static int getScreenHeight() {
210+
return instance.getConfig().getWindowHeight();
211+
}
212+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.opengame.engine;
2+
3+
import lombok.extern.java.Log;
4+
import org.joml.Vector3f;
5+
import org.opengame.engine.app.AppConfig;
6+
import org.opengame.engine.camera.FlyingCamera;
7+
import org.opengame.engine.scene.MeshLoader;
8+
import org.opengame.engine.render.TestCube;
9+
import org.opengame.engine.scene.Scene;
10+
11+
import java.io.IOException;
12+
import java.net.URISyntaxException;
13+
14+
@Log
15+
public class TestClient {
16+
public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException {
17+
log.info("LOG TEST");
18+
// start engine
19+
var engine = new Engine();
20+
var config = new AppConfig();
21+
if (args.length > 0) {
22+
config.setWorkingDirectory(args[0]);
23+
}
24+
engine.Init(config);
25+
26+
var scene = new Scene();
27+
var camera = (FlyingCamera) scene.getCamera();
28+
camera.setFlySpeed(0.5f);
29+
// for (int i = 0; i < 1; i++) {
30+
// scene.add(new TestCube());
31+
// }
32+
33+
var model2 = MeshLoader.loadModel( Engine.getWorkingDirectory() + "models/cube.obj");
34+
model2.setTexture("flowers_cloth.dds");
35+
model2.setRotation(new Vector3f(5, 11.4f, 0));
36+
model2.setPosition(new Vector3f(0, 4, 20));
37+
scene.add(model2);
38+
39+
var model3 = MeshLoader.loadModel( Engine.getWorkingDirectory() + "models/cube.obj");
40+
model3.setTexture("cat.dds");
41+
model3.setRotation(new Vector3f(5, 21.4f, 0));
42+
model3.setPosition(new Vector3f(11, 4, 20));
43+
scene.add(model3);
44+
45+
var model = MeshLoader.loadModel( Engine.getWorkingDirectory() + "models/cube.obj");
46+
model.setRotation(new Vector3f(0, 1.4f, 0));
47+
model.setPosition(new Vector3f(0, 1, 20));
48+
scene.add(model);
49+
50+
engine.setCurrentScene(scene);
51+
52+
log.info("Current scene stats: " + scene.getStats());
53+
54+
engine.startLoop();
55+
}
56+
}

0 commit comments

Comments
 (0)