Added Sponza scene. Updated README.md
This commit is contained in:
150
src/main.cpp
150
src/main.cpp
@@ -1,4 +1,5 @@
|
||||
#include <cstdio>
|
||||
#include <random>
|
||||
#include <vector>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
@@ -35,6 +36,9 @@ void mouse_callback(double pos_x, double pos_y, void* user_data);
|
||||
void setup_dockspace();
|
||||
void draw_panel_window(UserData& user_data);
|
||||
void draw_viewport_window(FrameBuffer& framebuffer, UserData& user_data);
|
||||
void add_random_lights(Scene& scene, int point_light_count, int spot_light_count,
|
||||
const glm::vec3& box_min, const glm::vec3& box_max,
|
||||
unsigned int seed = std::random_device{}());
|
||||
std::vector<Vertex> get_cube_vertices();
|
||||
|
||||
template <typename... Args>
|
||||
@@ -61,8 +65,9 @@ int main()
|
||||
|
||||
Renderer::init();
|
||||
|
||||
Camera camera{{5.0f, 5.0f, 5.0f}};
|
||||
camera.look_at({0.0f, 0.0f, 0.0f});
|
||||
Camera camera{{6.0f, 3.0f, 1.0f}};
|
||||
camera.fov = 60.0f;
|
||||
camera.look_at({0.0f, 1.0f, 0.0f});
|
||||
|
||||
UserData user_data{*window, camera};
|
||||
window->set_callback_user_data(&user_data);
|
||||
@@ -73,6 +78,8 @@ int main()
|
||||
auto shader = Shader::create("./src/shaders/default.vert", "./src/shaders/default.frag");
|
||||
auto debug_shader = Shader::create("./src/shaders/debug.vert", "./src/shaders/debug.frag", false);
|
||||
|
||||
auto sponza = load_obj("./assets/sponza/sponza.obj", shader);
|
||||
|
||||
auto car = load_obj("./assets/audi_r8/Audi_R8_2017.obj", shader);
|
||||
|
||||
auto backpack = load_obj("./assets/backpack/backpack.obj", shader);
|
||||
@@ -95,18 +102,27 @@ int main()
|
||||
|
||||
/******************** Scene construction ********************/
|
||||
|
||||
// Sponza scene
|
||||
Rc<Scene> sponza_scene = make_rc<Scene>();
|
||||
user_data.scenes.push_back(sponza_scene);
|
||||
|
||||
sponza->transform.position = {0.0f, -0.5f, 0.0f};
|
||||
sponza->transform.scale = {0.008f, 0.008f, 0.008f};
|
||||
sponza_scene->add_entity(*sponza);
|
||||
|
||||
add_random_lights(*sponza_scene, 4, 4,
|
||||
glm::vec3(-10.0f, 1.0f, -3.0f), glm::vec3(10.0f, 5.0f, 3.0f),
|
||||
69420);
|
||||
|
||||
// Car scene
|
||||
Rc<Scene> car_scene = make_rc<Scene>();
|
||||
user_data.scenes.push_back(car_scene);
|
||||
|
||||
car_scene->add_entity(*car);
|
||||
|
||||
PointLight light_white1;
|
||||
light_white1.position = glm::vec3(0.0f, 3.0f, 3.0f);
|
||||
light_white1.diffuse = glm::vec3(1.0f, 1.0f, 1.0f); // Bright white
|
||||
light_white1.specular = glm::vec3(1.0f, 1.0f, 1.0f); // White highlights
|
||||
|
||||
car_scene->light_environment.point_lights.push_back(light_white1);
|
||||
add_random_lights(*car_scene, 4, 4,
|
||||
glm::vec3(-10.0f, 1.0f, -3.0f), glm::vec3(10.0f, 5.0f, 3.0f),
|
||||
42);
|
||||
|
||||
// Backpack scene
|
||||
Rc<Scene> backpack_scene = make_rc<Scene>();
|
||||
@@ -122,12 +138,9 @@ int main()
|
||||
floor.transform.scale = {20.0f, 1.0f, 20.0f};
|
||||
backpack_scene->add_entity(floor);
|
||||
|
||||
PointLight light_white2;
|
||||
light_white2.position = glm::vec3(0.0f, 3.0f, 3.0f);
|
||||
light_white2.diffuse = glm::vec3(2.0f, 2.0f, 2.0f); // Bright white
|
||||
light_white2.specular = glm::vec3(2.0f, 2.0f, 2.0f); // White highlights
|
||||
|
||||
backpack_scene->light_environment.point_lights.push_back(light_white2);
|
||||
add_random_lights(*backpack_scene, 4, 4,
|
||||
glm::vec3(-10.0f, 1.0f, -3.0f), glm::vec3(10.0f, 5.0f, 3.0f),
|
||||
37);
|
||||
|
||||
// Cubes scene
|
||||
Rc<Scene> cubes_scene = make_rc<Scene>();
|
||||
@@ -151,18 +164,9 @@ int main()
|
||||
cubes_scene->add_entity(entity);
|
||||
}
|
||||
|
||||
PointLight light_warm;
|
||||
light_warm.position = glm::vec3(0.0f, 2.0f, 2.0f);
|
||||
light_warm.diffuse = glm::vec3(1.0f, 0.6f, 0.2f); // Orange
|
||||
light_warm.specular = glm::vec3(1.0f, 0.8f, 0.5f);
|
||||
|
||||
PointLight light_cool;
|
||||
light_cool.position = glm::vec3(-3.0f, 1.0f, -5.0f);
|
||||
light_cool.diffuse = glm::vec3(0.2f, 0.5f, 1.0f); // Blue
|
||||
light_cool.specular = glm::vec3(0.5f, 0.7f, 1.0f);
|
||||
|
||||
cubes_scene->light_environment.point_lights.push_back(light_warm);
|
||||
cubes_scene->light_environment.point_lights.push_back(light_cool);
|
||||
add_random_lights(*cubes_scene, 4, 4,
|
||||
glm::vec3(-10.0f, 1.0f, -3.0f), glm::vec3(10.0f, 5.0f, 3.0f),
|
||||
152533);
|
||||
|
||||
/******************** Main loop ********************/
|
||||
|
||||
@@ -285,18 +289,16 @@ void draw_panel_window(UserData& user_data)
|
||||
ImGui::SameLine();
|
||||
ImGui::SetCursorPosY(ImGui::GetCursorPosY() + (50.0f - ImGui::GetTextLineHeight()) * 0.5f);
|
||||
ImGui::Text("Fan Engine");
|
||||
ImGui::Separator();
|
||||
|
||||
ImGui::Text("Press INSERT to toggle camera controls");
|
||||
ImGui::Separator();
|
||||
ImGui::SeparatorText("General Settings");
|
||||
|
||||
if (ImGui::Checkbox("VSync", &user_data.vsync)) {
|
||||
user_data.window.set_vsync(user_data.vsync);
|
||||
}
|
||||
ImGui::Checkbox("Show Debug Wireframes", &user_data.show_debug);
|
||||
ImGui::Separator();
|
||||
ImGui::Checkbox("Debug Wireframes", &user_data.show_debug);
|
||||
|
||||
ImGui::SeparatorText("Scene Selector");
|
||||
|
||||
ImGui::Text("Scene Selector");
|
||||
for (size_t i = 0; i < user_data.scenes.size(); ++i) {
|
||||
char label[64];
|
||||
ImFormatString(label, IM_ARRAYSIZE(label), "Scene %zu", i);
|
||||
@@ -305,6 +307,19 @@ void draw_panel_window(UserData& user_data)
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::SeparatorText("Camera Settings");
|
||||
|
||||
ImGui::DragFloat3("Position", &user_data.camera.position.x, 0.1f, -20.0f, 20.0f, "%.1f");
|
||||
ImGui::SliderFloat("FOV", &user_data.camera.fov, 30.0f, 100.0f, "%.1f");
|
||||
|
||||
ImGui::SeparatorText("Key Bindings");
|
||||
|
||||
ImGui::Text("ESCAPE: Quit application");
|
||||
ImGui::Text("INSERT: Toggle camera input");
|
||||
ImGui::Text("WASD: Camera movement");
|
||||
ImGui::Text("F11: Toggle fullscreen");
|
||||
ImGui::Text("H: Toggle renderer wireframe");
|
||||
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
@@ -332,6 +347,77 @@ void draw_viewport_window(FrameBuffer& framebuffer, UserData& user_data)
|
||||
ImGui::PopStyleVar();
|
||||
}
|
||||
|
||||
void add_random_lights(Scene& scene, int point_light_count, int spot_light_count,
|
||||
const glm::vec3& box_min, const glm::vec3& box_max,
|
||||
unsigned int seed/* = std::random_device{}()*/)
|
||||
{
|
||||
std::mt19937 rng(seed);
|
||||
|
||||
std::uniform_real_distribution<float> random_x(box_min.x, box_max.x);
|
||||
std::uniform_real_distribution<float> random_y(box_min.y, box_max.y);
|
||||
std::uniform_real_distribution<float> random_z(box_min.z, box_max.z);
|
||||
|
||||
std::uniform_real_distribution<float> random_direction(-1.0f, 1.0f);
|
||||
std::uniform_real_distribution<float> random_intensity(0.5f, 1.5f);
|
||||
|
||||
const glm::vec3 light_colors[] = {
|
||||
glm::vec3(1.0f, 1.0f, 1.0f), // White
|
||||
glm::vec3(1.0f, 0.6f, 0.2f), // Warm orange
|
||||
glm::vec3(0.2f, 0.5f, 1.0f), // Cool blue
|
||||
glm::vec3(1.0f, 0.2f, 0.2f), // Red
|
||||
glm::vec3(0.3f, 1.0f, 0.4f), // Green
|
||||
glm::vec3(0.8f, 0.3f, 1.0f) // Purple
|
||||
};
|
||||
|
||||
std::uniform_int_distribution<size_t> randomColor(0, std::size(light_colors) - 1);
|
||||
|
||||
auto generateDirection = [&]() {
|
||||
glm::vec3 direction;
|
||||
|
||||
// Avoid generating a vector that is too close to zero.
|
||||
do {
|
||||
direction = glm::vec3(random_direction(rng), random_direction(rng), random_direction(rng));
|
||||
} while (glm::dot(direction, direction) < 0.001f);
|
||||
|
||||
return glm::normalize(direction);
|
||||
};
|
||||
|
||||
auto generateColor = [&]() { return light_colors[randomColor(rng)]; };
|
||||
|
||||
// Generate point lights
|
||||
for (int i = 0; i < point_light_count; ++i) {
|
||||
PointLight light;
|
||||
|
||||
light.position = glm::vec3(random_x(rng), random_y(rng), random_z(rng));
|
||||
glm::vec3 color = generateColor();
|
||||
light.diffuse = color;
|
||||
light.specular = glm::mix(color, glm::vec3(1.0f), 0.25f);
|
||||
|
||||
scene.light_environment.point_lights.push_back(light);
|
||||
}
|
||||
|
||||
// Generate spot lights
|
||||
for (int i = 0; i < spot_light_count; ++i) {
|
||||
SpotLight light;
|
||||
|
||||
light.position = glm::vec3(random_x(rng), random_y(rng), random_z(rng));
|
||||
light.direction = generateDirection();
|
||||
glm::vec3 color = generateColor();
|
||||
light.ambient = glm::vec3(0.0f);
|
||||
light.diffuse = color;
|
||||
light.specular = glm::mix(color, glm::vec3(1.0f), 0.25f);
|
||||
|
||||
// Narrow inner cone and slightly wider soft outer cone
|
||||
light.cut_off = glm::cos(glm::radians(12.5f));
|
||||
light.outer_cut_off = glm::cos(glm::radians(17.5f));
|
||||
light.constant = 1.0f;
|
||||
light.linear = 0.09f;
|
||||
light.quadratic = 0.032f;
|
||||
|
||||
scene.light_environment.spot_lights.push_back(light);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<Vertex> get_cube_vertices()
|
||||
{
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user