Unravel Engine C++ Reference
Loading...
Searching...
No Matches
atmospheric_pass_skybox.cpp
Go to the documentation of this file.
4
5namespace unravel
6{
7
9{
10 // 1) Load your cubemap (DDS or whichever format you have)
11 auto& am = ctx.get_cached<asset_manager>();
12
13 // 2) Load skybox shaders
14 auto vs_sky = am.get_asset<gfx::shader>("engine:/data/shaders/atmospherics/vs_skybox.sc");
15 auto fs_sky = am.get_asset<gfx::shader>("engine:/data/shaders/atmospherics/fs_skybox.sc");
16 program_.program = std::make_unique<gpu_program>(vs_sky, fs_sky);
17
18 if(!program_.program->is_valid())
19 {
20 APPLOG_ERROR("Failed to load skybox program.");
21 return false;
22 }
23
24 program_.cache_uniforms();
25
26 struct pos_vertex
27 {
28 float x = 0.0f;
29 float y = 0.0f;
30 float z = 0.0f;
31 };
32
33 // 8 corners
34 static pos_vertex s_cubeVertices[] = {
35 {-1.0f, 1.0f, 1.0f},
36 {1.0f, 1.0f, 1.0f},
37 {-1.0f, -1.0f, 1.0f},
38 {1.0f, -1.0f, 1.0f},
39 {-1.0f, 1.0f, -1.0f},
40 {1.0f, 1.0f, -1.0f},
41 {-1.0f, -1.0f, -1.0f},
42 {1.0f, -1.0f, -1.0f},
43 };
44
45 // Indices for an inside-out cube (36 total).
46 // You can flip the order if you see backfaces incorrectly.
47 // clang-format off
48 static const uint16_t s_cubeIndices[] = {
49 // -Z
50 2,1,0,
51 2,3,1,
52 // +Z
53 5,6,4,
54 7,6,5,
55 // -Y
56 2,4,0,
57 6,4,2,
58 // +Y
59 1,5,3,
60 3,5,7,
61 // -X
62 0,4,1,
63 1,4,5,
64 // +X
65 3,7,2,
66 2,7,6,
67 };
68 // clang-format on
69
70 vb_ = std::make_unique<gfx::vertex_buffer>(gfx::copy(s_cubeVertices, sizeof(s_cubeVertices)),
72
73 ib_ = std::make_unique<gfx::index_buffer>(gfx::copy(s_cubeIndices, sizeof(s_cubeIndices)));
74
75 return true;
76}
77
79 const camera& cam,
80 gfx::render_view& rview,
81 delta_t dt,
82 const run_params& params)
83{
84 if(!program_.program->is_valid())
85 {
86 return; // Can't do much if data is invalid
87 }
88
89 auto cubemap = params.cubemap.get();
90
91 // 1) Create a pass
92 gfx::render_pass pass("atmospheric_cubemap_pass");
93 pass.bind(target.get());
94
95 // 2) We want to keep the camera's orientation but remove its translation,
96 // so the skybox doesn't move with the camera.
98
99 // 3) Begin program, set up the cubemap
100 program_.program->begin();
101
102 gfx::set_texture(program_.u_texCube, 0, cubemap);
103
104 // 4) The critical part: Use DEPTH_TEST_EQUAL, so we only fill the background
105 uint64_t state =
106 BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A |
107 BGFX_STATE_DEPTH_TEST_LEQUAL; // or CULL_NONE, depending on vertex winding
108
109 gfx::set_state(state);
110
111 // 5) Submit the draw
112 gfx::set_vertex_buffer(0, vb_->native_handle());
113 gfx::set_index_buffer(ib_->native_handle());
114
115 gfx::submit(pass.id, program_.program->native_handle());
116
117 // 6) Done
118 program_.program->end();
119 gfx::discard();
120}
121
122} // namespace unravel
Manages assets, including loading, unloading, and storage.
auto get_asset(const std::string &key, load_flags flags=load_flags::standard) -> asset_handle< T >
Gets an asset by its key.
void run(gfx::frame_buffer::ptr target, const camera &cam, gfx::render_view &rview, delta_t dt, const run_params &params)
Class representing a camera. Contains functionality for manipulating and updating a camera....
Definition camera.h:35
auto get_projection() const -> const math::transform &
Retrieves the current projection matrix.
Definition camera.cpp:203
auto get_view_relative() const -> const math::transform &
Definition camera.cpp:286
std::chrono::duration< float > delta_t
#define APPLOG_ERROR(...)
Definition logging.h:20
void submit(view_id _id, program_handle _handle, int32_t _depth, bool _preserveState)
Definition graphics.cpp:899
void set_state(uint64_t _state, uint32_t _rgba)
Definition graphics.cpp:763
void set_vertex_buffer(uint8_t _stream, vertex_buffer_handle _handle)
Definition graphics.cpp:838
void discard(uint8_t _flags)
Definition graphics.cpp:968
const memory_view * copy(const void *_data, uint32_t _size)
Definition graphics.cpp:301
void set_index_buffer(index_buffer_handle _handle)
Definition graphics.cpp:808
void set_texture(uint8_t _stage, uniform_handle _sampler, texture_handle _handle, uint32_t _flags)
Definition graphics.cpp:889
float y
float x
float z
auto get(bool wait=true) const -> std::shared_ptr< T >
Gets the shared pointer to the asset.
void set_view_proj(const float *v, const float *p)
gfx::view_id id
Definition render_pass.h:98
void bind(const frame_buffer *fb=nullptr) const
static auto get_layout() -> const vertex_layout &
Definition vertex_decl.h:15
auto get_cached() -> T &
Definition context.hpp:49