MXVK Vulkan Framework 0.24.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
starfield.cpp
Go to the documentation of this file.
1#include "starfield.hpp"
2
3#include "mxvk/mxvk.hpp"
4
5namespace space {
6
7 void StarField::init(int star_count, float min_radius, float max_radius) {
8 if (initialized) {
9 return;
10 }
11
12 stars.resize(static_cast<size_t>(star_count));
13 this->min_radius = min_radius;
14 this->max_radius = max_radius;
15 for (auto &star : stars) {
16 respawn(star, glm::vec3(0.0f));
17 }
18 initialized = true;
19 }
20
22 sprite = sprite_batch;
23 }
24
26 if (sprite != nullptr) {
27 sprite->resize(window);
28 }
29 }
30
31 void StarField::update(float delta_time, const glm::vec3 &camera_position, float elapsed_time) {
32 for (auto &star : stars) {
33 star.position += star.velocity * delta_time;
34 const float distance = glm::length(star.position - camera_position);
35 if (distance > max_radius * 1.2f || distance < min_radius * 0.35f) {
36 respawn(star, camera_position);
37 }
38
39 const float twinkle = 0.65f + 0.35f * std::sin(elapsed_time * star.twinkle_speed + star.twinkle_phase);
40 const float fade = std::clamp(1.0f - ((distance - min_radius) / (max_radius - min_radius)), 0.2f, 0.7f);
41 const float brightness = star.brightness * twinkle * fade;
42 star.color = glm::vec4(
43 std::clamp(star.base_color.r * brightness, 0.0f, 1.0f),
44 std::clamp(star.base_color.g * brightness, 0.0f, 1.0f),
45 std::clamp(star.base_color.b * brightness, 0.0f, 1.0f),
46 std::clamp(0.2f + brightness, 0.0f, 1.0f));
47 }
48 }
49
51 if (sprite == nullptr) {
52 return;
53 }
54 for (const auto &star : stars) {
55 sprite->drawSprite(star.position, glm::vec2(star.size), star.color);
56 }
57 }
58
59 void StarField::respawn(Star &star, const glm::vec3 &center) {
60 const float theta = random_float(0.0f, 2.0f * PI);
61 const float phi = std::acos(random_float(-1.0f, 1.0f));
62 const float radius = random_float(min_radius, max_radius);
63
64 star.position.x = center.x + radius * std::sin(phi) * std::cos(theta);
65 star.position.y = center.y + radius * std::sin(phi) * std::sin(theta);
66 star.position.z = center.z + radius * std::cos(phi);
67 star.velocity = glm::vec3(
68 random_float(-0.06f, 0.06f),
69 random_float(-0.06f, 0.06f),
70 random_float(-0.06f, 0.06f));
71
72 const float roll = random_float(0.0f, 1.0f);
73 if (roll < 0.5f) {
74 star.base_color = {0.90f, 0.90f, 1.00f, 1.0f};
75 star.size = random_float(0.026f, 0.056f);
76 } else if (roll < 0.65f) {
77 star.base_color = {0.60f, 0.78f, 1.00f, 1.0f};
78 star.size = random_float(0.034f, 0.073f);
79 } else if (roll < 0.75f) {
80 star.base_color = {1.00f, 0.95f, 0.65f, 1.0f};
81 star.size = random_float(0.035f, 0.078f);
82 } else if (roll < 0.85f) {
83 star.base_color = {1.00f, 0.66f, 0.32f, 1.0f};
84 star.size = random_float(0.042f, 0.090f);
85 } else if (roll < 0.92f) {
86 star.base_color = {1.00f, 0.40f, 0.40f, 1.0f};
87 star.size = random_float(0.045f, 0.095f);
88 } else {
89 star.base_color = {1.00f, 1.00f, 1.00f, 1.0f};
90 star.size = random_float(0.070f, 0.133f);
91 }
92
93 star.brightness = random_float(0.25f, 1.0f);
94 star.color = star.base_color;
95 star.twinkle_phase = random_float(0.0f, 2.0f * PI);
96 star.twinkle_speed = random_float(0.4f, 3.2f);
97 star.layer = roll < 0.5f ? 0 : (roll < 0.85f ? 1 : 2);
98 }
99
100} // namespace space
Animated starfield used by the Asteroids scene.
Depth-tested 3D billboard sprite batch.
Main Vulkan window wrapper for MXVK.
Definition mxvk.hpp:37
void init(int star_count, float min_radius, float max_radius)
Creates the stars.
Definition starfield.cpp:7
void setSprite(mxvk::VK_Sprite3D *sprite_batch)
Assigns the non-owning sprite batch used to render stars.
Definition starfield.cpp:21
void update(float delta_time, const glm::vec3 &camera_position, float elapsed_time)
Advances star movement and twinkling.
Definition starfield.cpp:31
void resize(mxvk::VK_Window *window)
Updates rendering resources after a window-size change.
Definition starfield.cpp:25
void draw()
Submits the current stars to the assigned sprite batch.
Definition starfield.cpp:50
constexpr float PI
Single-precision value of pi.
float random_float(float min_value, float max_value)
Generates a uniformly distributed floating-point value.
Runtime state for one animated background star.
glm::vec3 position
World-space position.
glm::vec4 base_color
Color before brightness modulation.
float brightness
Base brightness multiplier.
float twinkle_phase
Twinkle animation phase.
glm::vec3 velocity
World-space velocity.
float size
Rendered point size.
int layer
Parallax layer index.
glm::vec4 color
Current rendered color.
float twinkle_speed
Twinkle animation rate.