Files
demo/main.cpp
T
2022-04-24 13:45:14 +03:00

286 lines
12 KiB
C++

#include <memory>
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include "peripherals_glfw.h"
#include "demo/demo_engine.h"
#include "timer.h"
using namespace acidrain;
using namespace std;
INITIALIZE_EASYLOGGINGPP
int main() {
acidrain::peripherals_glfw peripherals;
peripherals.init();
LOG(INFO) << "peripherals initialized";
glDisable(GL_DEPTH_TEST);
const float DEMO_LENGTH_IN_SECONDS = 100;
// --------------------------------------------------------------------------------------
// Load resources
// --------------------------------------------------------------------------------------
acidrain::texture_generator tg(256, 256);
tg.brick(0, 50, 20, 4, glm::vec4(1), glm::vec4(0));
// tg.checker_board(0, 50, glm::vec4(1), glm::vec4(0));
shared_ptr<mesh> affectedSphere = mesh_generator::sphere(50, 50);
map_transform(affectedSphere, tg, 0, 0, 0.2f);
calculate_normals(*affectedSphere.get());
// affectedSphere = mesh_generator::cog(0.2, 0.5, 50, 4, 0.1, 0.2, 0.2, 0.1);
demo_data::meshes.push_back(affectedSphere);
demo_data::meshes.push_back(mesh_generator::sphere(3, 3));
demo_data::meshes.push_back(mesh_generator::grid(30, 30));
demo_data::meshes.push_back(mesh_generator::cube());
auto mat = std::make_shared<material>();
mat->ambient = glm::vec4(0, 0, 0.15, 1);
mat->diffuse = glm::vec4(1, 1, 1, 1);
// material->diffuse = glm::vec4(1, 0, 0, 1);
mat->specular = glm::vec4(0.3, 0.3, 0.3, 1);
mat->shininess = 200;
mat->cast_shadows = true;
auto *textureGenerator1 = new texture_generator(256, 256);
// mat->textures[texture_role::diffuse] = textureGenerator1->checker_board(0, 20,
// colour(1, 1, 1, 1),
// colour(0, 0.5, 0.2, 1)).get_texture(0);
mat->textures[texture_role::diffuse] = textureGenerator1->brick(0,
50, 20,
4,
colour(0.3, 0.1, 0, 1),
colour(1, 0.3, 0, 1)).get_texture(0);
demo_data::materials.push_back(mat);
auto material2 = make_shared<material>();
material2->ambient = glm::vec4(0, 0, 0.15, 1);
material2->textures[texture_role::diffuse] = textureGenerator1->get_texture(0);
// material2->diffuse = glm::vec4(0.2, 1.0, 0.7, 1);
material2->diffuse = glm::vec4(1, 1, 1, 1);
material2->specular = glm::vec4(0.3, 0.3, 0.3, 1);
material2->shininess = 200;
material2->cast_shadows = false;
demo_data::materials.push_back(material2);
auto material3 = make_shared<material>();
material3->ambient = glm::vec4(0, 0, 0.15, 1);
// material3->diffuse = glm::vec4(1.0, 0.7, 0.2, 1);
material3->diffuse = glm::vec4(1, 1, 1, 1);
material3->specular = glm::vec4(0.3, 0.3, 0.3, 1);
material3->shininess = 200;
material3->cast_shadows = true;
demo_data::materials.push_back(material3);
auto material4 = make_shared<material>();
material4->ambient = glm::vec4(0, 0, 0.15, 1);
// material4->diffuse = glm::vec4(1.0, 0.7, 0.2, 1);
material4->diffuse = glm::vec4(1, 1, 1, 1);
material4->specular = glm::vec4(0.3, 0.3, 0.3, 1);
material4->shininess = 200;
material4->cast_shadows = false;
demo_data::materials.push_back(material4);
auto material5 = make_shared<material>();
material5->ambient = glm::vec4(0.1, 0.1, 0.15, 1);
// material4->diffuse = glm::vec4(1.0, 0.7, 0.2, 1);
material5->diffuse = glm::vec4(1, 1, 1, 1);
material5->specular = glm::vec4(1, 1, 1, 1);
material5->shininess = 10;
material5->cast_shadows = false;
demo_data::materials.push_back(material5);
// --------------------------------------------------------------------------------------
// Set up demo parts
// --------------------------------------------------------------------------------------
DemoPartClear demoPartClear;
demoPartClear.startTime = 0;
demoPartClear.endTime = DEMO_LENGTH_IN_SECONDS;
demoPartClear.color = glm::vec4(0.1f, 0.1f, 0.3f, 1.0f);
DemoPartScene demoPartScene;
demoPartScene.startTime = 0;
demoPartScene.endTime = DEMO_LENGTH_IN_SECONDS;
demoPartScene.cameraName = "cam1";
demoPartScene.lightName = "light1";
auto *scene1 = new scene();
demoPartScene.scene_ = shared_ptr<scene>(scene1);
auto smallSphere = std::make_shared<mesh_node>("small sphere");
smallSphere->mesh_ = demo_data::meshes[1];
smallSphere->material_ = demo_data::materials[0];
smallSphere->position = glm::vec3(0, 0, 0);
smallSphere->scale = glm::vec3(0.5);
// meshNode->rotation = glm::angleAxis(2.25f , glm::vec3(1.0f, 0.0f, 0.0f));
auto ground_plane = std::make_shared<mesh_node>("ground_plane");
ground_plane->mesh_ = demo_data::meshes[2];
ground_plane->material_ = demo_data::materials[1];
ground_plane->position = glm::vec3(0, 0, 0);
ground_plane->scale = glm::vec3(10);
ground_plane->rotation = glm::angleAxis(3.141529f / 2.0f, glm::vec3(1.0f, 0.0f, 0.0f));
auto back_plane = std::make_shared<mesh_node>("back plane");
back_plane->mesh_ = demo_data::meshes[2];
back_plane->material_ = demo_data::materials[1];
back_plane->position = glm::vec3(0, 5, -5);
back_plane->scale = glm::vec3(10);
back_plane->rotation = angleAxis(3.141529f, glm::vec3(1.0f, 0.0f, 0.0f));
auto left_plane = std::make_shared<mesh_node>("left plane");
left_plane->mesh_ = demo_data::meshes[2];
left_plane->material_ = demo_data::materials[1];
left_plane->position = glm::vec3(-5, 5, 0);
left_plane->scale = glm::vec3(10);
left_plane->rotation = angleAxis(-3.141529f / 2.0f, glm::vec3(0.0f, 1.0f, 0.0f));
auto right_plane = std::make_shared<mesh_node>("right plane");
right_plane->mesh_ = demo_data::meshes[2];
right_plane->material_ = demo_data::materials[1];
right_plane->position = glm::vec3(5, 5, 0);
right_plane->scale = glm::vec3(10);
right_plane->rotation = angleAxis(3.141529f / 2.0f, glm::vec3(0.0f, 1.0f, 0.0f));
auto bigSphere = make_shared<mesh_node>("big sphere");
bigSphere->mesh_ = demo_data::meshes[0];
bigSphere->material_ = demo_data::materials[1];
bigSphere->position = glm::vec3(-2, 2, 0);
bigSphere->scale = glm::vec3(0.8);
bigSphere->rotation = glm::angleAxis(3.141529f / 2.0f, glm::vec3(1.0f, 0.0f, 0.0f));
auto lightMeshNode = std::make_shared<mesh_node>("light mesh");
lightMeshNode->mesh_ = demo_data::meshes[0];
lightMeshNode->material_ = demo_data::materials[0];
lightMeshNode->position = glm::vec3(1, 2, 1);
lightMeshNode->scale = glm::vec3(0.5);
auto camNode = std::make_shared<camera_node>();
camNode->name = "cam1";
camNode->position = glm::vec3(0, 2, 5);
camNode->target = glm::vec3(0, 0, 0);
camNode->fov = 45;
auto lightNode = make_shared<light_node>(light_type::spot);
lightNode->name = "light1";
lightNode->position = glm::vec3(-5, 5, 0);
lightNode->spot_target = glm::vec3(0, 0, 0);
lightNode->spot_cutoff = 50.0f;
lightNode->spot_exponent = 16.0f;
lightNode->ambient = glm::vec4(0.1, 0.1, 0.1, 1.0);
lightNode->diffuse = glm::vec4(0.5, 1.0, 1.0, 1.0);
lightNode->specular = glm::vec4(0.0, 0.8, 0.1, 1.0);
auto lightNode2 = make_shared<light_node>(light_type::spot);
lightNode2->name = "light2";
lightNode2->position = glm::vec3(5, 5, 0);
lightNode2->spot_target = glm::vec3(0, 0, 0);
lightNode2->spot_cutoff = 50.0f;
lightNode2->spot_exponent = 16.0f;
lightNode2->ambient = glm::vec4(0.1, 0.1, 0.1, 1.0);
lightNode2->diffuse = glm::vec4(1.0, 0.5, 1.0, 1.0);
lightNode2->specular = glm::vec4(0.0, 0.8, 0.1, 1.0);
scene1->tree->add(ground_plane);
scene1->tree->add(back_plane);
scene1->tree->add(left_plane);
scene1->tree->add(right_plane);
scene1->tree->add(smallSphere);
scene1->tree->add(bigSphere);
scene1->tree->add(lightMeshNode);
scene1->tree->add(camNode);
scene1->tree->add(lightNode);
scene1->tree->add(lightNode2);
// --------------------------------------------------------------------------------------
// Run demo
// --------------------------------------------------------------------------------------
glBindFramebuffer(GL_FRAMEBUFFER, 0);
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
ImGui::StyleColorsDark();
// Setup Platform/Renderer backends
ImGui_ImplGlfw_InitForOpenGL(((peripherals_glfw*)&peripherals)->get_window(), true);
ImGui_ImplOpenGL3_Init("#version 150");
timer timer1;
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
bool show_demo_window = false;
bool show_another_window = false;
while (!peripherals.should_close() && timer1.seconds_since_start() < DEMO_LENGTH_IN_SECONDS) {
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
{
static float f = 0.0f;
static int counter = 0;
ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
ImGui::Checkbox("Another Window", &show_another_window);
ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
counter++;
ImGui::SameLine();
ImGui::Text("counter = %d", counter);
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
ImGui::End();
}
double elapsedSeconds = timer1.seconds_since_start();
bigSphere->mesh_ = mesh_generator::extrude(mesh_generator::sphere(50, 50), {200, 201, 100, 101, 400, 401}, 0.05,
(int) ((sin(elapsedSeconds) + 0.5) * 40));
smallSphere->position = glm::vec3(0, sin(2 * M_PI * 0.13 * elapsedSeconds) * 2.0 + 0.5, 0);
// meshNode->position = glm::vec3(0, 0.001, 0);
// camNode->target = glm::vec3(0, 0.0001, 0);
// camNode->position = glm::vec3(0, 5, -10);
lightNode2->position = glm::vec3(5 * sin(2 * M_PI * 0.05 * elapsedSeconds), 2,
5 * cos(2 * M_PI * 0.05 * elapsedSeconds));
lightNode->position = glm::vec3(5 * cos(2 * M_PI * 0.75 * elapsedSeconds), 2,
5 * sin(2 * M_PI * 0.75 * elapsedSeconds));
lightMeshNode->position = lightNode2->position;
bigSphere->rotation *= angleAxis((float) (M_PI / 4.0 * timer1.lap()), normalize(glm::vec3(0.2, 0.5, 0.3)));
demoPartClear.process(demoPartClear.normalizeTime(elapsedSeconds));
demoPartScene.process(demoPartScene.normalizeTime(elapsedSeconds));
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
peripherals.swap_buffers();
peripherals.poll_events();
}
}