LWJGL教程03 - 改进

让Render类处理渲染相关的事

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[imports ...]

public class Render {
public static void createRender() {
// createCapabilities 创建功能
GL.createCapabilities();
}

public static void cleanup() {
// TODO
}

public static void render() {
glClearColor(
Window.settings.color[0], Window.settings.color[1],
Window.settings.color[2], Window.settings.color[3]);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glViewport(0, 0, Window.getWidth(), Window.getHeight());
}
}

创建完Render类后,需要在正确的位置调用它。

所以要修改Engine

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
[imports ...]

public class Engine {
[...]

public void run() {
Window.createWindow(Engine.window_settings);
// 添加在 Window.createWindow() 后
Render.createRender();
Window.setWindowVisible();

[...]

while(!Window.windowShouldClose()) {
Window.pollEvents();

logic.input();

// 添加在 Window.update() 前
Render.render();

Window.update();

[...]
}
cleanup();
}

private void cleanup() {
// 别忘了
Render.cleanup();
Window.cleanup();
}
}

测试一下能不能修改颜色。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class TestEngine {
public static void main(String[] args) {
Engine.init();
Window.settings
.setTitle("Test Color")
.setVsync(true)
.setColor(255, 255, 255, 255);
/*
glClearColor(r, g, b, a) 的三个参数均是不大于1的浮点数
所以想用小于255的int设置颜色还需要除以255
*/
Engine.logic = new TestLogic();
new Engine().run();
}
}

变成白色底了 - 运行结果


LWJGL教程03 - 改进
https://panxy02.github.io/2024/07/18/lwjgl-03/
作者
52Hertz
发布于
2024年7月18日
许可协议