Skip to content

Code example

Simon Tippe edited this page Jun 29, 2020 · 1 revision
#include "App.h"

std::string Atlas::EngineInstance::assetDirectory = "../data";
std::string Atlas::EngineInstance::shaderDirectory = "shader";

void App::LoadContent() {

	UnlockFramerate();

	renderTarget = new Atlas::RenderTarget(1920, 1080);

	viewport = Atlas::Viewport(0, 0, window.GetWidth(), window.GetHeight());

	auto icon = Atlas::Texture::Texture2D("icon.png");
	window.SetIcon(&icon);
	window.Update();

	font = Atlas::Font("font/roboto.ttf", 88, 10);

	DisplayLoadingScreen();

	camera = Atlas::Camera(47.0f, 2.0f, .25f, 400.0f,
		vec3(30.0f, 25.0f, 0.0f), vec2(-3.14f / 2.0f, 0.0f));

	scene = Atlas::Scene::Scene(vec3(-2048.0f), vec3(2048.0f));

	mouseHandler = Atlas::Input::MouseHandler(&camera, 1.5f, 6.0f);
	keyboardHandler = Atlas::Input::KeyboardHandler(&camera, 7.0f, 6.0f);
	controllerHandler = Atlas::Input::ControllerHandler(&camera, 1.5f, 7.0f, 6.0f, 5000.0f);

	Atlas::Events::EventManager::ControllerDeviceEventDelegate.Subscribe(
		[this](Atlas::Events::ControllerDeviceEvent event) {
		if (event.type == AE_CONTROLLER_ADDED) {
			useControllerHandler = true;
		}
		else if (event.type == AE_CONTROLLER_REMOVED) {
			useControllerHandler = false;
		}
	});

	Atlas::Events::EventManager::KeyboardEventDelegate.Subscribe(
		[this](Atlas::Events::KeyboardEvent event) {
		if (event.keycode == AE_KEY_ESCAPE) {
			Exit();
		}
	});

	sponzaMesh = Atlas::Mesh::Mesh("sponza/sponza.dae");
	sponzaActor = Atlas::Actor::StaticMeshActor(&sponzaMesh, scale(mat4(1.0f), vec3(.05f)));

	directionalLight = Atlas::Lighting::DirectionalLight(AE_STATIONARY_LIGHT);

	directionalLight.direction = vec3(0.0f, -1.0f, 0.1f);
	directionalLight.ambient = 0.05f;
	directionalLight.color = vec3(253, 194, 109) / 255.0f;

	// Shadow mapping that is fixed to a point
	mat4 orthoProjection = glm::ortho(-100.0f, 100.0f, -70.0f, 120.0f, -120.0f, 120.0f);
	directionalLight.AddShadow(200.0f, 0.01f, 4096, vec3(0.0f), orthoProjection);
	directionalLight.GetShadow()->sampleCount = 1;
	directionalLight.GetShadow()->sampleRange = 1.5;
	directionalLight.AddVolumetric(renderTarget->GetWidth() / 2, renderTarget->GetHeight() / 2, 20, -0.5f);

	pointLight0 = Atlas::Lighting::PointLight(AE_STATIONARY_LIGHT);
	pointLight0.location = vec3(24.35f, 6.5f, 7.1f);
	pointLight0.color = 2.0f * vec3(255.0f, 128.0f, 0.0f) / 255.0f;
	pointLight0.AddShadow(0.0f, 512);

	pointLight1 = Atlas::Lighting::PointLight(AE_STATIONARY_LIGHT);
	pointLight1.location = vec3(24.35f, 6.5f, -11.0f);
	pointLight1.color = 2.0f * vec3(255.0f, 128.0f, 0.0f) / 255.0f;
	pointLight1.AddShadow(0.0f, 512);

	pointLight2 = Atlas::Lighting::PointLight(AE_STATIONARY_LIGHT);
	pointLight2.location = vec3(-31.0f, 6.5f, 7.1f);
	pointLight2.color = 2.0f * vec3(255.0f, 128.0f, 0.0f) / 255.0f;
	pointLight2.AddShadow(0.0f, 512);

	pointLight3 = Atlas::Lighting::PointLight(AE_STATIONARY_LIGHT);
	pointLight3.location = vec3(-31.0f, 6.5f, -11.0f);
	pointLight3.color = 2.0f * vec3(255.0f, 128.0f, 0.0f) / 255.0f;
	pointLight3.AddShadow(0.0f, 512);

	scene.Add(&sponzaActor);
	
	scene.Add(&directionalLight);

	scene.Add(&pointLight0);
	scene.Add(&pointLight1);
	scene.Add(&pointLight2);
	scene.Add(&pointLight3);

}

void App::UnloadContent() {

	delete renderTarget;

}

void App::Update(float deltaTime) {

	if (!useControllerHandler) {
		mouseHandler.Update(&camera, deltaTime);
		keyboardHandler.Update(&camera, deltaTime);
	}
	else {
		controllerHandler.Update(&camera, deltaTime);
	}

	camera.UpdateView();
	camera.UpdateProjection();

	scene.Update(&camera, deltaTime);

}

void App::Render(float deltaTime) {	
	
	viewport.Set(0, 0, window.GetWidth(), window.GetHeight());
    
	masterRenderer.RenderScene(&viewport, renderTarget, &camera, &scene);
	
	float averageFramerate = Atlas::Clock::GetAverage();

	std::string out = "Average " + std::to_string(averageFramerate) + " ms  Currently " + std::to_string(deltaTime) + " ms";

	masterRenderer.textRenderer.Render(&viewport, &font, out, 0, 0, vec4(1.0f, 0.0f, 0.0f, 1.0f), 2.5f / 10.0f);

}

void App::DisplayLoadingScreen() {

	float textWidth, textHeight;
	font.ComputeDimensions("Loading...", 2.5f, &textWidth, &textHeight);

	window.Clear();

	auto screenSize = GetScreenSize();

	auto x = (float)window.GetWidth() / 2.0f - textWidth / 2.0f;
	auto y = (float)window.GetHeight() / 2.0f - textHeight / 2.0f;

	masterRenderer.textRenderer.Render(&viewport, &font, "Loading...", x, y, vec4(1.0f, 1.0f, 1.0f, 1.0f), 2.5f);

	window.Update();

}

Atlas::EngineInstance* GetEngineInstance() {

	return new App();

}