<commit_before>#include "OakVR.h"
#include "Time/Timer.h"
#include "input/MouseInput/MouseInput.h"
#include "input/KeyboardInput/KeyboardInput.h"
#include "Math/Matrix.h"
#include "Math/Vector3.h"
#include "Log/Log.h"

#include "ResourceManager/ResourceManager.h"
#include "ResourceManager/FileLoaders.h"
#include "Renderer/Renderer/Renderer.h"
#include "Renderer/Renderer/RenderWindow.h"
#include "CameraManager.h"

#include "Renderer/Renderer/Shader.h"
#include "Renderer/Renderer/Mesh.h"
#include "Renderer/Renderer/MeshElement.h"
#include "Renderer/Renderer/Material.h"
#include "Renderer/Renderer/Color.h"

#include "Text/Text.h"

#include "Profiler/Profiler.h"


#include <algorithm>
#include <memory>


namespace oakvr
{
	// --------------------------------------------------------------------------------
	bool OakVR::Update(float dt)
	{
		oakvr::input::keyboard::Update();
		oakvr::input::mouse::Update();


		if(m_pRW->IsOpen())
		{
			if ((oakvr::input::keyboard::IsDown(oakvr::input::Key::lShift) ||
				oakvr::input::keyboard::IsDown(oakvr::input::Key::rShift))
				&& oakvr::input::keyboard::IsDown(oakvr::input::Key::f5)
				)
				return false;

			// Keep updateables from interfering with the updateables vector while they are updated
			// there is a problem with deleting updateables while the loop is under way in that deleted objects might
			// have Update called for them
			auto pUpdateables = m_pUpdateables;
			for (auto &e : pUpdateables)
				e->Update(dt);

			m_pRenderer->SetViewMatrix(GetCurrentCamera()->ComputeViewMatrix());
			m_pRenderer->SetProjMatrix(GetCurrentCamera()->GetProjMatrix());
			m_pRenderer->Update(dt);
			profiler::Profiler::GetInstance().PrintSortedData();
		}
		else
		{
			m_pRenderer->Cleanup();
			return false;
		}
		
		return true;
	}

	// --------------------------------------------------------------------------------
	OakVR::OakVR()
		: m_pRM{ std::make_shared<oakvr::core::ResourceManager>() }
		, m_pRW{ std::make_shared<oakvr::render::RenderWindow>("oakvr", 100, 100, 1024, 768) }
		, m_pRenderer{ std::make_shared<oakvr::render::Renderer>() }
		, m_pCM{ std::make_shared<oakvr::render::CameraManager>() }
	{
		m_timer = oakvr::Timer();

		Initialize();
	}

	// --------------------------------------------------------------------------------
	OakVR::~OakVR()
	{
		Cleanup();
	}

	// --------------------------------------------------------------------------------
	// callback functions begin
	// --------------------------------------------------------------------------------

	void WindowSizeChangedCallback(void *pNativeHandler, int width, int height)
	{
		oakvr::OakVR::GetInstance().OnWindowSizeChanged(pNativeHandler, width, height);
	}

	void WindowFocusChangedCallback(void *pNativeHandler, int focused)
	{
		oakvr::OakVR::GetInstance().OnWindowFocusChanged(pNativeHandler, focused);
	}

	// --------------------------------------------------------------------------------
	// callback functions end
	// --------------------------------------------------------------------------------


	// --------------------------------------------------------------------------------
	bool OakVR::Initialize()
	{
		oakvr::core::InitializeFileLoaders();
		
		if(!m_pRW || !m_pRW->Initialize())
			return false;
		
		// Configure callbacks for necessary render window events
		m_pRW->SetWindowSizeCallback(WindowSizeChangedCallback);
		m_pRW->SetWindowFocusCallback(WindowFocusChangedCallback);

		if(m_pRenderer)
		{
			m_pRenderer->SetRenderWindow(m_pRW);
			m_pRenderer->SetResourceManager(m_pRM);
			m_pRenderer->Initialize();
			
			// Initialize Text manager
			oakvr::core::Text::GetInstance().SetResourceManagerPtr(m_pRM);
			oakvr::core::Text::GetInstance().SetRendererPtr(m_pRenderer);
			
		}

		m_timer.Reset();
		return true;
	}

	void OakVR::Cleanup()
	{
		m_pRenderer->Cleanup();
	}

	// --------------------------------------------------------------------------------
	bool OakVR::Update()
	{
		m_timer.Tick();
		return Update(m_timer.GetDeltaTime());
	}

	void OakVR::DrawLine(const oakvr::math::Vector3 &start, const oakvr::math::Vector3 &end, const oakvr::render::Color &color)
	{
		DrawLine(start, end, color, color);
	}

	void OakVR::DrawLine(const oakvr::math::Vector3 &start, const oakvr::math::Vector3 &end, const  oakvr::render::Color &color, const oakvr::render::Color &startColor)
	{
		std::vector<oakvr::render::VertexElementDescriptor> ved{
			oakvr::render::VertexElementDescriptor::Semantic::position,
			oakvr::render::VertexElementDescriptor::Semantic::color
		};

		oakvr::core::MemoryBuffer vb{ 8 * ComputeVertexStride(ved) * sizeof(float) };
		oakvr::core::MemoryBuffer ib{ 6 * 2 * 3 * sizeof(uint32_t) };
		float pVertices[] = {
			start.x,	start.y,	start.z,	startColor.r,	startColor.g,	startColor.b,
			start.x,	start.y,	end.z,		startColor.r,	startColor.g,	color.b,
			end.x,		start.y,	end.z,		color.r,		startColor.g,	color.b,
			end.x,		start.y,	start.z,	color.r,		startColor.g,	startColor.b,

			start.x,	end.y,		start.z,	startColor.r,	color.g,		startColor.b,
			start.x,	end.y,		end.z,		startColor.r,	color.g,		color.b,
			end.x,		end.y,		end.z,		color.r,		color.g,		color.b,
			end.x,		end.y,		start.z,	color.r,		color.g,		startColor.b,
		};

		uint32_t pIndices[] = {
			0, 1, 2,
			0, 2, 3,
			1, 6, 2,
			1, 5, 6,

			0, 5, 1,
			0, 4, 5,
			4, 6, 5,
			4, 7, 6,

			2, 7, 3,
			2, 6, 7,
			3, 4, 0,
			3, 7, 4
		};

		memcpy(vb.GetDataPtr(), pVertices, vb.Size());
		memcpy(ib.GetDataPtr(), pIndices, ib.Size());

		auto pMaterial = std::make_shared<oakvr::render::Material>(std::string("DefaultColor"));

		
		auto pMeshElem = std::make_shared<oakvr::render::MeshElement>(ved, vb, sizeof(uint32_t), ib, pMaterial, std::vector<std::string>());
		
		auto pMesh = std::make_shared<oakvr::render::Mesh>();
		pMesh->AddMeshElement(pMeshElem);

		m_pRenderer->RegisterOneFrameMesh(pMesh);
		oakvr::render::RegisterShader("DefaultColor");
	}

	// --------------------------------------------------------------------------------
	void OakVR::DrawMeshBoundingBoxes()
	{
		
	}<commit_msg>fixed winding for DrawLine vertices<commit_after>#include "OakVR.h"
#include "Time/Timer.h"
#include "input/MouseInput/MouseInput.h"
#include "input/KeyboardInput/KeyboardInput.h"
#include "Math/Matrix.h"
#include "Math/Vector3.h"
#include "Log/Log.h"

#include "ResourceManager/ResourceManager.h"
#include "ResourceManager/FileLoaders.h"
#include "Renderer/Renderer/Renderer.h"
#include "Renderer/Renderer/RenderWindow.h"
#include "CameraManager.h"

#include "Renderer/Renderer/Shader.h"
#include "Renderer/Renderer/Mesh.h"
#include "Renderer/Renderer/MeshElement.h"
#include "Renderer/Renderer/Material.h"
#include "Renderer/Renderer/Color.h"

#include "Text/Text.h"

#include "Profiler/Profiler.h"


#include <algorithm>
#include <memory>


namespace oakvr
{
	// --------------------------------------------------------------------------------
	bool OakVR::Update(float dt)
	{
		oakvr::input::keyboard::Update();
		oakvr::input::mouse::Update();


		if(m_pRW->IsOpen())
		{
			if ((oakvr::input::keyboard::IsDown(oakvr::input::Key::lShift) ||
				oakvr::input::keyboard::IsDown(oakvr::input::Key::rShift))
				&& oakvr::input::keyboard::IsDown(oakvr::input::Key::f5)
				)
				return false;

			// Keep updateables from interfering with the updateables vector while they are updated
			// there is a problem with deleting updateables while the loop is under way in that deleted objects might
			// have Update called for them
			auto pUpdateables = m_pUpdateables;
			for (auto &e : pUpdateables)
				e->Update(dt);

			m_pRenderer->SetViewMatrix(GetCurrentCamera()->ComputeViewMatrix());
			m_pRenderer->SetProjMatrix(GetCurrentCamera()->GetProjMatrix());
			m_pRenderer->Update(dt);
			profiler::Profiler::GetInstance().PrintSortedData();
		}
		else
		{
			m_pRenderer->Cleanup();
			return false;
		}
		
		return true;
	}

	// --------------------------------------------------------------------------------
	OakVR::OakVR()
		: m_pRM{ std::make_shared<oakvr::core::ResourceManager>() }
		, m_pRW{ std::make_shared<oakvr::render::RenderWindow>("oakvr", 100, 100, 1024, 768) }
		, m_pRenderer{ std::make_shared<oakvr::render::Renderer>() }
		, m_pCM{ std::make_shared<oakvr::render::CameraManager>() }
	{
		m_timer = oakvr::Timer();

		Initialize();
	}

	// --------------------------------------------------------------------------------
	OakVR::~OakVR()
	{
		Cleanup();
	}

	// --------------------------------------------------------------------------------
	// callback functions begin
	// --------------------------------------------------------------------------------

	void WindowSizeChangedCallback(void *pNativeHandler, int width, int height)
	{
		oakvr::OakVR::GetInstance().OnWindowSizeChanged(pNativeHandler, width, height);
	}

	void WindowFocusChangedCallback(void *pNativeHandler, int focused)
	{
		oakvr::OakVR::GetInstance().OnWindowFocusChanged(pNativeHandler, focused);
	}

	// --------------------------------------------------------------------------------
	// callback functions end
	// --------------------------------------------------------------------------------


	// --------------------------------------------------------------------------------
	bool OakVR::Initialize()
	{
		oakvr::core::InitializeFileLoaders();
		
		if(!m_pRW || !m_pRW->Initialize())
			return false;
		
		// Configure callbacks for necessary render window events
		m_pRW->SetWindowSizeCallback(WindowSizeChangedCallback);
		m_pRW->SetWindowFocusCallback(WindowFocusChangedCallback);

		if(m_pRenderer)
		{
			m_pRenderer->SetRenderWindow(m_pRW);
			m_pRenderer->SetResourceManager(m_pRM);
			m_pRenderer->Initialize();
			
			// Initialize Text manager
			oakvr::core::Text::GetInstance().SetResourceManagerPtr(m_pRM);
			oakvr::core::Text::GetInstance().SetRendererPtr(m_pRenderer);
			
		}

		m_timer.Reset();
		return true;
	}

	void OakVR::Cleanup()
	{
		m_pRenderer->Cleanup();
	}

	// --------------------------------------------------------------------------------
	bool OakVR::Update()
	{
		m_timer.Tick();
		return Update(m_timer.GetDeltaTime());
	}

	void OakVR::DrawLine(const oakvr::math::Vector3 &start, const oakvr::math::Vector3 &end, const oakvr::render::Color &color)
	{
		DrawLine(start, end, color, color);
	}

	void OakVR::DrawLine(const oakvr::math::Vector3 &start, const oakvr::math::Vector3 &end, const  oakvr::render::Color &endColor, const oakvr::render::Color &startColor)
	{
		std::vector<oakvr::render::VertexElementDescriptor> ved{
			oakvr::render::VertexElementDescriptor::Semantic::position,
			oakvr::render::VertexElementDescriptor::Semantic::color
		};

		oakvr::core::MemoryBuffer vb{ 8 * ComputeVertexStride(ved) * sizeof(float) };
		oakvr::core::MemoryBuffer ib{ 6 * 2 * 3 * sizeof(uint32_t) };
		float pVertices[] = {
			start.x,	start.y,	start.z,	startColor.r,	startColor.g,	startColor.b,
			start.x,	start.y,	end.z,		startColor.r,	startColor.g,	endColor.b,
			end.x,		start.y,	end.z,		endColor.r,		startColor.g,	endColor.b,
			end.x,		start.y,	start.z,	endColor.r,		startColor.g,	startColor.b,

			start.x,	end.y,		start.z,	startColor.r,	endColor.g,		startColor.b,
			start.x,	end.y,		end.z,		startColor.r,	endColor.g,		endColor.b,
			end.x,		end.y,		end.z,		endColor.r,		endColor.g,		endColor.b,
			end.x,		end.y,		start.z,	endColor.r,		endColor.g,		startColor.b,
		};

		uint32_t pIndices[] = {
			0, 3, 1,
			1, 3, 2,
			1, 6, 5,
			1, 2, 6,

			1, 4, 0,
			1, 5, 4,
			5, 6, 7,
			5, 7, 4,

			2, 3, 7,
			2, 7, 6,
			0, 7, 3,
			0, 4, 7,
		};

		memcpy(vb.GetDataPtr(), pVertices, vb.Size());
		memcpy(ib.GetDataPtr(), pIndices, ib.Size());

		auto pMaterial = std::make_shared<oakvr::render::Material>(std::string("DefaultColor"));

		
		auto pMeshElem = std::make_shared<oakvr::render::MeshElement>(ved, vb, sizeof(uint32_t), ib, pMaterial, std::vector<std::string>());
		
		auto pMesh = std::make_shared<oakvr::render::Mesh>();
		pMesh->AddMeshElement(pMeshElem);

		m_pRenderer->RegisterOneFrameMesh(pMesh);
		oakvr::render::RegisterShader("DefaultColor");
	}

	// --------------------------------------------------------------------------------
	void OakVR::DrawMeshBoundingBoxes()
	{
		
	}
{'fraction_non_alphanumeric': 0.2733736515201046, 'fraction_numerical': 0.010624387054593004, 'mean_word_length': 2.48731832430892, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5291337', 'n_tokens_mistral': 4479, 'n_tokens_neox': 4020, 'n_words': 686}