Skip to content

Commit 741026e

Browse files
committed
Frustum culling
1 parent 9441a60 commit 741026e

File tree

118 files changed

+170217
-0
lines changed

Some content is hidden

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

118 files changed

+170217
-0
lines changed

chapter25/nbactions.xml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<actions>
3+
<action>
4+
<actionName>run</actionName>
5+
<packagings>
6+
<packaging>jar</packaging>
7+
</packagings>
8+
<goals>
9+
<goal>process-classes</goal>
10+
<goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
11+
</goals>
12+
<properties>
13+
<exec.args>-classpath %classpath org.lwjglb.game.Main</exec.args>
14+
<exec.executable>java</exec.executable>
15+
</properties>
16+
</action>
17+
<action>
18+
<actionName>debug</actionName>
19+
<packagings>
20+
<packaging>jar</packaging>
21+
</packagings>
22+
<goals>
23+
<goal>process-classes</goal>
24+
<goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
25+
</goals>
26+
<properties>
27+
<exec.args>-Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address} -classpath %classpath org.lwjglb.game.Main</exec.args>
28+
<exec.executable>java</exec.executable>
29+
<jpda.listen>true</jpda.listen>
30+
</properties>
31+
</action>
32+
<action>
33+
<actionName>profile</actionName>
34+
<packagings>
35+
<packaging>jar</packaging>
36+
</packagings>
37+
<goals>
38+
<goal>process-classes</goal>
39+
<goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
40+
</goals>
41+
<properties>
42+
<exec.args>-classpath %classpath org.lwjglb.game.Main</exec.args>
43+
<exec.executable>java</exec.executable>
44+
</properties>
45+
</action>
46+
</actions>

chapter25/pom.xml

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>org.lwjglb</groupId>
7+
<artifactId>book</artifactId>
8+
<version>1.0</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
12+
<artifactId>game-c25</artifactId>
13+
<version>1.0</version>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.lwjgl</groupId>
18+
<artifactId>lwjgl</artifactId>
19+
<version>${lwjgl.version}</version>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.lwjgl</groupId>
23+
<artifactId>lwjgl-glfw</artifactId>
24+
<version>${lwjgl.version}</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.lwjgl</groupId>
28+
<artifactId>lwjgl-opengl</artifactId>
29+
<version>${lwjgl.version}</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.lwjgl</groupId>
33+
<artifactId>lwjgl-openal</artifactId>
34+
<version>${lwjgl.version}</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.lwjgl</groupId>
38+
<artifactId>lwjgl-stb</artifactId>
39+
<version>${lwjgl.version}</version>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.lwjgl</groupId>
43+
<artifactId>lwjgl-nanovg</artifactId>
44+
<version>${lwjgl.version}</version>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.joml</groupId>
48+
<artifactId>joml</artifactId>
49+
<version>${joml.version}</version>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.l33tlabs.twl</groupId>
53+
<artifactId>pngdecoder</artifactId>
54+
<version>${pngdecoder.version}</version>
55+
</dependency>
56+
57+
<!-- Natives -->
58+
<dependency>
59+
<groupId>org.lwjgl</groupId>
60+
<artifactId>lwjgl</artifactId>
61+
<version>${lwjgl.version}</version>
62+
<classifier>${native.target}</classifier>
63+
<scope>runtime</scope>
64+
</dependency>
65+
<dependency>
66+
<groupId>org.lwjgl</groupId>
67+
<artifactId>lwjgl-opengl</artifactId>
68+
<version>${lwjgl.version}</version>
69+
<classifier>${native.target}</classifier>
70+
<scope>runtime</scope>
71+
</dependency>
72+
<dependency>
73+
<groupId>org.lwjgl</groupId>
74+
<artifactId>lwjgl-glfw</artifactId>
75+
<version>${lwjgl.version}</version>
76+
<classifier>${native.target}</classifier>
77+
<scope>runtime</scope>
78+
</dependency>
79+
<dependency>
80+
<groupId>org.lwjgl</groupId>
81+
<artifactId>lwjgl-openal</artifactId>
82+
<version>${lwjgl.version}</version>
83+
<classifier>${native.target}</classifier>
84+
<scope>runtime</scope>
85+
</dependency>
86+
<dependency>
87+
<groupId>org.lwjgl</groupId>
88+
<artifactId>lwjgl-stb</artifactId>
89+
<version>${lwjgl.version}</version>
90+
<classifier>${native.target}</classifier>
91+
<scope>runtime</scope>
92+
</dependency>
93+
<dependency>
94+
<groupId>org.lwjgl</groupId>
95+
<artifactId>lwjgl-nanovg</artifactId>
96+
<version>${lwjgl.version}</version>
97+
<classifier>${native.target}</classifier>
98+
<scope>runtime</scope>
99+
</dependency>
100+
</dependencies>
101+
</project>
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package org.lwjglb.engine;
2+
3+
import org.lwjglb.engine.sound.SoundManager;
4+
5+
public class GameEngine implements Runnable {
6+
7+
public static final int TARGET_FPS = 75;
8+
9+
public static final int TARGET_UPS = 30;
10+
11+
private final Window window;
12+
13+
private final Thread gameLoopThread;
14+
15+
private final Timer timer;
16+
17+
private final IGameLogic gameLogic;
18+
19+
private final MouseInput mouseInput;
20+
21+
private double lastFps;
22+
23+
private int fps;
24+
25+
private String windowTitle;
26+
27+
public GameEngine(String windowTitle, boolean vSync, Window.WindowOptions opts, IGameLogic gameLogic) throws Exception {
28+
this(windowTitle, 0, 0, vSync, opts, gameLogic);
29+
}
30+
31+
public GameEngine(String windowTitle, int width, int height, boolean vSync, Window.WindowOptions opts, IGameLogic gameLogic) throws Exception {
32+
this.windowTitle = windowTitle;
33+
gameLoopThread = new Thread(this, "GAME_LOOP_THREAD");
34+
window = new Window(windowTitle, width, height, vSync, opts);
35+
mouseInput = new MouseInput();
36+
this.gameLogic = gameLogic;
37+
timer = new Timer();
38+
}
39+
40+
public void start() {
41+
String osName = System.getProperty("os.name");
42+
if ( osName.contains("Mac") ) {
43+
gameLoopThread.run();
44+
} else {
45+
gameLoopThread.start();
46+
}
47+
}
48+
49+
@Override
50+
public void run() {
51+
try {
52+
init();
53+
gameLoop();
54+
} catch (Exception excp) {
55+
excp.printStackTrace();
56+
} finally {
57+
cleanup();
58+
}
59+
}
60+
61+
protected void init() throws Exception {
62+
window.init();
63+
timer.init();
64+
mouseInput.init(window);
65+
gameLogic.init(window);
66+
lastFps = timer.getTime();
67+
fps = 0;
68+
}
69+
70+
protected void gameLoop() {
71+
float elapsedTime;
72+
float accumulator = 0f;
73+
float interval = 1f / TARGET_UPS;
74+
75+
boolean running = true;
76+
while (running && !window.windowShouldClose()) {
77+
elapsedTime = timer.getElapsedTime();
78+
accumulator += elapsedTime;
79+
80+
input();
81+
82+
while (accumulator >= interval) {
83+
update(interval);
84+
accumulator -= interval;
85+
}
86+
87+
render();
88+
89+
if ( !window.isvSync() ) {
90+
sync();
91+
}
92+
}
93+
}
94+
95+
protected void cleanup() {
96+
gameLogic.cleanup();
97+
}
98+
99+
private void sync() {
100+
float loopSlot = 1f / TARGET_FPS;
101+
double endTime = timer.getLastLoopTime() + loopSlot;
102+
while (timer.getTime() < endTime) {
103+
try {
104+
Thread.sleep(1);
105+
} catch (InterruptedException ie) {
106+
}
107+
}
108+
}
109+
110+
protected void input() {
111+
mouseInput.input(window);
112+
gameLogic.input(window, mouseInput);
113+
}
114+
115+
protected void update(float interval) {
116+
gameLogic.update(interval, mouseInput, window);
117+
}
118+
119+
protected void render() {
120+
if ( window.getWindowOptions().showFps && timer.getLastLoopTime() - lastFps > 1 ) {
121+
lastFps = timer.getLastLoopTime();
122+
window.setWindowTitle(windowTitle + " - " + fps + " FPS");
123+
fps = 0;
124+
}
125+
fps++;
126+
gameLogic.render(window);
127+
window.update();
128+
}
129+
130+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.lwjglb.engine;
2+
3+
public interface IGameLogic {
4+
5+
void init(Window window) throws Exception;
6+
7+
void input(Window window, MouseInput mouseInput);
8+
9+
void update(float interval, MouseInput mouseInput, Window window);
10+
11+
void render(Window window);
12+
13+
void cleanup();
14+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package org.lwjglb.engine;
2+
3+
import org.joml.Vector2d;
4+
import org.joml.Vector2f;
5+
import static org.lwjgl.glfw.GLFW.*;
6+
import org.lwjgl.glfw.GLFWCursorPosCallback;
7+
import org.lwjgl.glfw.GLFWCursorEnterCallback;
8+
import org.lwjgl.glfw.GLFWMouseButtonCallback;
9+
10+
public class MouseInput {
11+
12+
private final Vector2d previousPos;
13+
14+
private final Vector2d currentPos;
15+
16+
private final Vector2f displVec;
17+
18+
private boolean inWindow = false;
19+
20+
private boolean leftButtonPressed = false;
21+
22+
private boolean rightButtonPressed = false;
23+
24+
private GLFWCursorPosCallback cursorPosCallback;
25+
26+
private GLFWCursorEnterCallback cursorEnterCallback;
27+
28+
private GLFWMouseButtonCallback mouseButtonCallback;
29+
30+
public MouseInput() {
31+
previousPos = new Vector2d(-1, -1);
32+
currentPos = new Vector2d(0, 0);
33+
displVec = new Vector2f();
34+
}
35+
36+
public void init(Window window) {
37+
glfwSetCursorPosCallback(window.getWindowHandle(), cursorPosCallback = new GLFWCursorPosCallback() {
38+
@Override
39+
public void invoke(long window, double xpos, double ypos) {
40+
currentPos.x = xpos;
41+
currentPos.y = ypos;
42+
}
43+
});
44+
glfwSetCursorEnterCallback(window.getWindowHandle(), cursorEnterCallback = new GLFWCursorEnterCallback() {
45+
@Override
46+
public void invoke(long window, boolean entered) {
47+
inWindow = entered;
48+
}
49+
});
50+
glfwSetMouseButtonCallback(window.getWindowHandle(), mouseButtonCallback = new GLFWMouseButtonCallback() {
51+
@Override
52+
public void invoke(long window, int button, int action, int mods) {
53+
leftButtonPressed = button == GLFW_MOUSE_BUTTON_1 && action == GLFW_PRESS;
54+
rightButtonPressed = button == GLFW_MOUSE_BUTTON_2 && action == GLFW_PRESS;
55+
}
56+
});
57+
}
58+
59+
public Vector2f getDisplVec() {
60+
return displVec;
61+
}
62+
63+
public Vector2d getCurrentPos() {
64+
return currentPos;
65+
}
66+
67+
public void input(Window window) {
68+
displVec.x = 0;
69+
displVec.y = 0;
70+
if (previousPos.x > 0 && previousPos.y > 0 && inWindow) {
71+
double deltax = currentPos.x - previousPos.x;
72+
double deltay = currentPos.y - previousPos.y;
73+
boolean rotateX = deltax != 0;
74+
boolean rotateY = deltay != 0;
75+
if (rotateX) {
76+
displVec.y = (float) deltax;
77+
}
78+
if (rotateY) {
79+
displVec.x = (float) deltay;
80+
}
81+
}
82+
previousPos.x = currentPos.x;
83+
previousPos.y = currentPos.y;
84+
}
85+
86+
public boolean isLeftButtonPressed() {
87+
return leftButtonPressed;
88+
}
89+
90+
public boolean isRightButtonPressed() {
91+
return rightButtonPressed;
92+
}
93+
}

0 commit comments

Comments
 (0)