add scaling

This commit is contained in:
zongor 2025-07-24 14:12:15 -04:00
parent 07d4587584
commit 138db9bda3
3 changed files with 19 additions and 19 deletions

View File

@ -64,9 +64,7 @@ private:
return 0.5 * (rec.normal + Color(1, 1, 1));
}
Vec3 unit_direction = unit_vector(r.direction());
auto a = 0.5 * (unit_direction.y() + 1.0);
return (1.0 - a) * Color(1.0, 1.0, 1.0) + a * Color(0.5, 0.7, 1.0);
return Color(0, 0, 0);
}
};

View File

@ -19,9 +19,13 @@ int main() {
std::thread render(render_thread, std::ref(buffer), std::ref(canvas));
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *window =
SDL_CreateWindow("Solar System", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, canvas.width, canvas.height, 0);
int scale = 3;
SDL_Window *window = SDL_CreateWindow(
"Solar System", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
canvas.width * scale, // window width
canvas.height * scale, // window height
SDL_WINDOW_RESIZABLE // optional, allows manual resize
);
SDL_Renderer *renderer =
SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB24,
@ -36,7 +40,8 @@ int main() {
SDL_UnlockTexture(texture);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, nullptr, nullptr);
SDL_Rect dest = {0, 0, canvas.width * scale, canvas.height * scale};
SDL_RenderCopy(renderer, texture, nullptr, &dest);
SDL_RenderPresent(renderer);
SDL_Event e;

View File

@ -49,6 +49,7 @@ SimulationState initialize_solar_system() {
*/
void simulation_thread(RingBuffer<SimulationState> &buffer) {
SimulationState state = initialize_solar_system();
float BIG_G = 6.67e-11; // gravitational constant
float dt = 0.01; // if we didnt do this it would run too fast for render
std::chrono::duration<float> elapsed;
@ -59,10 +60,7 @@ void simulation_thread(RingBuffer<SimulationState> &buffer) {
elapsed = now - last;
if (elapsed.count() >= dt) {
float t_0 = 0;
float t = t_0;
float dt = 86400;
float BIG_G = 6.67e-11; // gravitational constant
float bdt = 86400;
size_t n_bodies = state.bodies.size();
for (size_t m1_idx = 0; m1_idx < n_bodies; m1_idx++) {
@ -95,21 +93,20 @@ void simulation_thread(RingBuffer<SimulationState> &buffer) {
}
}
state.bodies[m1_idx].velocity.x() += a_g.x() * dt;
state.bodies[m1_idx].velocity.y() += a_g.y() * dt;
state.bodies[m1_idx].velocity.z() += a_g.z() * dt;
state.bodies[m1_idx].velocity.x() += a_g.x() * bdt;
state.bodies[m1_idx].velocity.y() += a_g.y() * bdt;
state.bodies[m1_idx].velocity.z() += a_g.z() * bdt;
}
for (size_t entity_idx = 0; entity_idx < n_bodies; entity_idx++) {
state.bodies[entity_idx].position.x() +=
state.bodies[entity_idx].velocity.x() * dt;
state.bodies[entity_idx].velocity.x() * bdt;
state.bodies[entity_idx].position.y() +=
state.bodies[entity_idx].velocity.y() * dt;
state.bodies[entity_idx].velocity.y() * bdt;
state.bodies[entity_idx].position.z() +=
state.bodies[entity_idx].velocity.z() * dt;
state.bodies[entity_idx].velocity.z() * bdt;
}
t += dt;
// Try to push it into the buffer
while (!buffer.push(state)) {
// Buffer full, drop or wait (e.g. yield or sleep)
@ -131,7 +128,7 @@ void render_thread(RingBuffer<SimulationState> &buffer, Canvas &canvas) {
Camera cam;
cam.aspect_ratio = 16.0 / 9.0;
cam.image_width = 400;
cam.move_to(Point3(0, 0, 100));
cam.move_to(Point3(0, 10, 100));
while (running) {
auto maybe_state = buffer.pop();