MXVK Vulkan Framework 0.35.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
21 void StarField::setSprite(mxvk::VK_Sprite3D *sprite_batch) { sprite = sprite_batch; }
22
24 if (sprite != nullptr) {
25 sprite->resize(window);
26 }
27 }
28
29 void StarField::update(float delta_time, const glm::vec3 &camera_position, float elapsed_time) {
30 for (auto &star : stars) {
31 star.position += star.velocity * delta_time;
32 const float distance = glm::length(star.position - camera_position);
33 if (distance > max_radius * 1.2f || distance < min_radius * 0.35f) {
34 respawn(star, camera_position);
35 }
36
37 const float twinkle = 0.65f + 0.35f * std::sin(elapsed_time * star.twinkle_speed + star.twinkle_phase);
38 const float fade = std::clamp(1.0f - ((distance - min_radius) / (max_radius - min_radius)), 0.2f, 0.7f);
39 const float brightness = star.brightness * twinkle * fade;
40 star.color = glm::vec4(std::clamp(star.base_color.r * brightness, 0.0f, 1.0f), std::clamp(star.base_color.g * brightness, 0.0f, 1.0f), std::clamp(star.base_color.b * brightness, 0.0f, 1.0f), std::clamp(0.2f + brightness, 0.0f, 1.0f));
41 }
42 }
43
45 if (sprite == nullptr) {
46 return;
47 }
48 for (const auto &star : stars) {
49 sprite->drawSprite(star.position, glm::vec2(star.size), star.color);
50 }
51 }
52
53 void StarField::respawn(Star &star, const glm::vec3 &center) {
54 const float theta = random_float(0.0f, 2.0f * PI);
55 const float phi = std::acos(random_float(-1.0f, 1.0f));
56 const float radius = random_float(min_radius, max_radius);
57
58 star.position.x = center.x + radius * std::sin(phi) * std::cos(theta);
59 star.position.y = center.y + radius * std::sin(phi) * std::sin(theta);
60 star.position.z = center.z + radius * std::cos(phi);
61 star.velocity = glm::vec3(random_float(-0.06f, 0.06f), random_float(-0.06f, 0.06f), random_float(-0.06f, 0.06f));
62
63 const float roll = random_float(0.0f, 1.0f);
64 if (roll < 0.5f) {
65 star.base_color = {0.90f, 0.90f, 1.00f, 1.0f};
66 star.size = random_float(0.026f, 0.056f);
67 } else if (roll < 0.65f) {
68 star.base_color = {0.60f, 0.78f, 1.00f, 1.0f};
69 star.size = random_float(0.034f, 0.073f);
70 } else if (roll < 0.75f) {
71 star.base_color = {1.00f, 0.95f, 0.65f, 1.0f};
72 star.size = random_float(0.035f, 0.078f);
73 } else if (roll < 0.85f) {
74 star.base_color = {1.00f, 0.66f, 0.32f, 1.0f};
75 star.size = random_float(0.042f, 0.090f);
76 } else if (roll < 0.92f) {
77 star.base_color = {1.00f, 0.40f, 0.40f, 1.0f};
78 star.size = random_float(0.045f, 0.095f);
79 } else {
80 star.base_color = {1.00f, 1.00f, 1.00f, 1.0f};
81 star.size = random_float(0.070f, 0.133f);
82 }
83
84 star.brightness = random_float(0.25f, 1.0f);
85 star.color = star.base_color;
86 star.twinkle_phase = random_float(0.0f, 2.0f * PI);
87 star.twinkle_speed = random_float(0.4f, 3.2f);
88 star.layer = roll < 0.5f ? 0 : (roll < 0.85f ? 1 : 2);
89 }
90
91} // namespace space
Animated starfield used by the Asteroids scene.
Depth-tested 3D billboard sprite batch.
Main Vulkan window wrapper for MXVK.
Definition mxvk.hpp:39
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:29
void resize(mxvk::VK_Window *window)
Updates rendering resources after a window-size change.
Definition starfield.cpp:23
void draw()
Submits the current stars to the assigned sprite batch.
Definition starfield.cpp:44
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.