Skip to content

Commit 67cf2d1

Browse files
committed
renderer: Frustum culling demo.
1 parent 6fe3f9a commit 67cf2d1

21 files changed

+786
-10
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#version 330
2+
3+
in vec2 vert_uv;
4+
5+
layout(location=0) out vec4 col;
6+
7+
uniform sampler2D tex;
8+
9+
// position (top left corner) and size: (x, y, width, height)
10+
uniform vec4 tile_params;
11+
12+
vec2 uv = vec2(
13+
vert_uv.x * tile_params.z + tile_params.x,
14+
vert_uv.y * tile_params.w + tile_params.y
15+
);
16+
17+
void main() {
18+
vec4 tex_val = texture(tex, uv);
19+
int alpha = int(round(tex_val.a * 255));
20+
switch (alpha) {
21+
case 0:
22+
col = tex_val;
23+
discard;
24+
case 254:
25+
col = vec4(1.0f, 0.0f, 0.0f, 1.0f);
26+
break;
27+
case 252:
28+
col = vec4(0.0f, 1.0f, 0.0f, 1.0f);
29+
break;
30+
case 250:
31+
col = vec4(0.0f, 0.0f, 1.0f, 1.0f);
32+
break;
33+
default:
34+
col = tex_val;
35+
break;
36+
}
37+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#version 330
2+
3+
layout(location=0) in vec2 v_position;
4+
layout(location=1) in vec2 uv;
5+
6+
out vec2 vert_uv;
7+
8+
// camera parameters for transforming the object position
9+
// and scaling the subtex to the correct size
10+
layout (std140) uniform camera {
11+
// view matrix (world to view space)
12+
mat4 view;
13+
// projection matrix (view to clip space)
14+
mat4 proj;
15+
// inverse zoom factor (1.0 / zoom)
16+
// high zoom = upscale subtex
17+
// low zoom = downscale subtex
18+
float inv_zoom;
19+
// inverse viewport size (1.0 / viewport size)
20+
vec2 inv_viewport_size;
21+
};
22+
23+
// position of the object in world space
24+
uniform vec3 obj_world_position;
25+
26+
// parameters for scaling and moving the subtex
27+
// to the correct position in clip space
28+
29+
// animation scalefactor
30+
// scales the vertex positions so that they
31+
// match the subtex dimensions
32+
//
33+
// high animation scale = downscale subtex
34+
// low animation scale = upscale subtex
35+
uniform float scale;
36+
37+
// size of the subtex (in pixels)
38+
uniform vec2 subtex_size;
39+
40+
// offset of the subtex anchor point
41+
// from the subtex center (in pixels)
42+
// used to move the subtex so that the anchor point
43+
// is at the object position
44+
uniform vec2 anchor_offset;
45+
46+
void main() {
47+
// translate the position of the object from world space to clip space
48+
// this is the position where we want to draw the subtex in 2D
49+
vec4 obj_clip_pos = proj * view * vec4(obj_world_position, 1.0);
50+
51+
// subtex has to be scaled to account for the zoom factor
52+
// and the animation scale factor. essentially this is (animation scale / zoom).
53+
float zoom_scale = scale * inv_zoom;
54+
55+
// Scale the subtex vertices
56+
// we have to account for the viewport size to get the correct dimensions
57+
// and then scale the subtex to the zoom factor to get the correct size
58+
vec2 vert_scale = zoom_scale * subtex_size * inv_viewport_size;
59+
60+
// Scale the anchor offset with the same method as above
61+
// to get the correct anchor position in the viewport
62+
vec2 anchor_scale = zoom_scale * anchor_offset * inv_viewport_size;
63+
64+
// offset the clip position by the offset of the subtex anchor
65+
// imagine this as pinning the subtex to the object position at the subtex anchor point
66+
obj_clip_pos += vec4(anchor_scale.x, anchor_scale.y, 0.0, 0.0);
67+
68+
// create a move matrix for positioning the vertices
69+
// uses the vert scale and the transformed object position in clip space
70+
mat4 move = mat4(vert_scale.x, 0.0, 0.0, 0.0,
71+
0.0, vert_scale.y, 0.0, 0.0,
72+
0.0, 0.0, 1.0, 0.0,
73+
obj_clip_pos.x, obj_clip_pos.y, obj_clip_pos.z, 1.0);
74+
75+
// calculate the final vertex position
76+
gl_Position = move * vec4(v_position, 0.0, 1.0);
77+
78+
// flip y axis because OpenGL uses bottom-left as its origin
79+
float uv_x = uv.x;
80+
float uv_y = 1.0 - uv.y;
81+
82+
vert_uv = vec2(uv_x, uv_y);
83+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#version 330
2+
3+
out vec4 col;
4+
5+
void main() {
6+
col = vec4(1.0, 0.0, 0.0, 0.8);
7+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#version 330
2+
3+
layout(location=0) in vec2 v_position;
4+
5+
// camera parameters for transforming the object position
6+
// and scaling the subtex to the correct size
7+
layout (std140) uniform camera {
8+
// view matrix (world to view space)
9+
mat4 view;
10+
// projection matrix (view to clip space)
11+
mat4 proj;
12+
// inverse zoom factor (1.0 / zoom)
13+
float inv_zoom;
14+
// inverse viewport size (1.0 / viewport size)
15+
vec2 inv_viewport_size;
16+
};
17+
18+
// position of the object in world space
19+
uniform vec3 obj_world_position;
20+
21+
// parameters for scaling and moving the subtex
22+
// to the correct position in clip space
23+
24+
// animation scalefactor
25+
// scales the vertex positions so that they
26+
// match the subtex dimensions
27+
//
28+
// high animation scale = downscale subtex
29+
// low animation scale = upscale subtex
30+
uniform float scale;
31+
32+
// size of the frame (in pixels)
33+
uniform vec2 frame_size;
34+
35+
void main() {
36+
// translate the position of the object from world space to clip space
37+
// this is the position where we want to draw the subtex in 2D
38+
vec4 obj_clip_pos = proj * view * vec4(obj_world_position, 1.0);
39+
40+
// subtex has to be scaled to account for the zoom factor
41+
// and the animation scale factor. essentially this is (animation scale / zoom).
42+
float zoom_scale = scale * inv_zoom;
43+
44+
// Scale the subtex vertices
45+
// we have to account for the viewport size to get the correct dimensions
46+
// and then scale the frame to the zoom factor to get the correct size
47+
vec2 vert_scale = zoom_scale * frame_size * inv_viewport_size;
48+
49+
// create a move matrix for positioning the vertices
50+
// uses the vert scale and the transformed object position in clip space
51+
mat4 move = mat4(vert_scale.x, 0.0, 0.0, 0.0,
52+
0.0, vert_scale.y, 0.0, 0.0,
53+
0.0, 0.0, 1.0, 0.0,
54+
obj_clip_pos.x, obj_clip_pos.y, obj_clip_pos.z, 1.0);
55+
56+
// calculate the final vertex position
57+
gl_Position = move * vec4(v_position, 0.0, 1.0);
58+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#version 330
2+
3+
in vec2 tex_pos;
4+
5+
layout(location=0) out vec4 out_col;
6+
7+
uniform sampler2D tex;
8+
9+
void main()
10+
{
11+
vec4 tex_val = texture(tex, tex_pos);
12+
out_col = tex_val;
13+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#version 330
2+
3+
layout (location = 0) in vec3 position;
4+
layout (location = 1) in vec2 uv;
5+
6+
out vec2 tex_pos;
7+
8+
// camera parameters for transforming the object position
9+
// and scaling the subtex to the correct size
10+
layout (std140) uniform camera {
11+
// view matrix (world to view space)
12+
mat4 view;
13+
// projection matrix (view to clip space)
14+
mat4 proj;
15+
// inverse zoom factor (1.0 / zoom)
16+
float inv_zoom;
17+
// inverse viewport size (1.0 / viewport size)
18+
vec2 inv_viewport_size;
19+
};
20+
21+
void main() {
22+
gl_Position = proj * view * vec4(position, 1.0);
23+
tex_pos = vec2(uv.x, 1.0 - uv.y);
24+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#version 330
2+
3+
uniform sampler2D color_texture;
4+
5+
in vec2 v_uv;
6+
out vec4 col;
7+
8+
void main() {
9+
col = texture(color_texture, v_uv);
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#version 330
2+
3+
layout(location=0) in vec2 position;
4+
layout(location=1) in vec2 uv;
5+
out vec2 v_uv;
6+
7+
void main() {
8+
gl_Position = vec4(position, 0.0, 1.0);
9+
v_uv = uv;
10+
}

libopenage/renderer/demo/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ add_sources(libopenage
55
demo_3.cpp
66
demo_4.cpp
77
demo_5.cpp
8+
demo_6.cpp
89
stresstest_0.cpp
910
stresstest_1.cpp
1011
tests.cpp

libopenage/renderer/demo/demo_0.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2015-2024 the openage authors. See copying.md for legal info.
1+
// Copyright 2023-2024 the openage authors. See copying.md for legal info.
22

33
#include "demo_0.h"
44

0 commit comments

Comments
 (0)