Compare commits
	
		
			3 Commits
		
	
	
		
			0b2148ad55
			...
			8ef886df83
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 8ef886df83 | |||
| 817d0bdca9 | |||
| c640ac574b | 
| @ -84,11 +84,12 @@ YoggieEngine::Mesh* AssetRegistry::LoadFromAssetFile(const std::filesystem::path | |||||||
|  |  | ||||||
|  |  | ||||||
| } | } | ||||||
|  | /* | ||||||
|  |  | ||||||
|  |  | ||||||
| YoggieEngine::Renderable* AssetRegistry::LoadFromSource(const std::filesystem::path srcPath, const std::filesystem::path assetFolder) | YoggieEngine::Renderable* AssetRegistry::LoadFromSource(const std::filesystem::path srcPath, const std::filesystem::path assetFolder) | ||||||
| { | { | ||||||
|  |  | ||||||
| 	/* |  | ||||||
| 	* 	auto model = (YoggieEngine::ModelImporter()).Import(srcPath.string()); | 	* 	auto model = (YoggieEngine::ModelImporter()).Import(srcPath.string()); | ||||||
| 	YoggieEngine::Mesh* exportMesh = model->renderable->mesh; | 	YoggieEngine::Mesh* exportMesh = model->renderable->mesh; | ||||||
| 	std::filesystem::path MeshFileName = assetFolder / srcPath.filename().replace_extension(".mesh"); | 	std::filesystem::path MeshFileName = assetFolder / srcPath.filename().replace_extension(".mesh"); | ||||||
| @ -134,6 +135,6 @@ YoggieEngine::Renderable* AssetRegistry::LoadFromSource(const std::filesystem::p | |||||||
|  |  | ||||||
| 	return model->renderable; | 	return model->renderable; | ||||||
|  |  | ||||||
| 	*/ |  | ||||||
| 	return nullptr; | 	return nullptr; | ||||||
| } | } | ||||||
|  | 	*/ | ||||||
|  | |||||||
| @ -18,7 +18,7 @@ public: | |||||||
|  |  | ||||||
|  |  | ||||||
| 	static YoggieEngine::Mesh* LoadFromAssetFile(const std::filesystem::path assetPath); | 	static YoggieEngine::Mesh* LoadFromAssetFile(const std::filesystem::path assetPath); | ||||||
| 	static YoggieEngine::Renderable* LoadFromSource(const std::filesystem::path srcPath, const std::filesystem::path assetFolder); | //	static YoggieEngine::Renderable* LoadFromSource(const std::filesystem::path srcPath, const std::filesystem::path assetFolder); | ||||||
| 	 | 	 | ||||||
| private:  | private:  | ||||||
| 	int unique_number = 0; | 	int unique_number = 0; | ||||||
|  | |||||||
| @ -8,22 +8,11 @@ public: | |||||||
| 		Right = glm::vec3(-1.0f, 0.0f, 0.0f); | 		Right = glm::vec3(-1.0f, 0.0f, 0.0f); | ||||||
| 		Up = glm::vec3(0.0f, 1.0f, 0.0f); | 		Up = glm::vec3(0.0f, 1.0f, 0.0f); | ||||||
|  |  | ||||||
| 		 |  | ||||||
| 		view = glm::translate(glm::mat4(1.0f), Position) * glm::toMat4(glm::quat(Rotation)); |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	void Update() { |  | ||||||
|  |  | ||||||
| 		view = glm::translate(glm::mat4(1.0f), Position) * glm::toMat4(glm::quat(Rotation)); |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	glm::vec3 Position = glm::vec3(0.0f); |  | ||||||
| 	glm::vec3 Rotation = glm::vec3(0.0f);  |  | ||||||
|  |  | ||||||
| private: | private: | ||||||
| 	 | 	 | ||||||
|  |  | ||||||
|  |  | ||||||
| 	glm::vec3 Front; | 	glm::vec3 Front; | ||||||
| 	glm::vec3 Right; | 	glm::vec3 Right; | ||||||
| 	glm::vec3 Up; | 	glm::vec3 Up; | ||||||
|  | |||||||
| @ -21,8 +21,15 @@ | |||||||
| #include "../../YoggieEngine/src/Graphics/Memory/Buffer.h" | #include "../../YoggieEngine/src/Graphics/Memory/Buffer.h" | ||||||
|  |  | ||||||
| using namespace YoggieEngine; | using namespace YoggieEngine; | ||||||
|  |  | ||||||
|  | const float movement_speed = 0.1f; | ||||||
|  | static float lastX = 400, lastY = 300; | ||||||
|  | const float sensitivity = 0.1; | ||||||
|  | static bool firstMouse = true; | ||||||
|  |  | ||||||
| class EditorLayer : public Layer { | class EditorLayer : public Layer { | ||||||
|  |  | ||||||
|  |  | ||||||
| public: | public: | ||||||
|     EditorLayer() : |     EditorLayer() : | ||||||
|         Layer(), |         Layer(), | ||||||
| @ -60,15 +67,9 @@ public: | |||||||
|         //Console console = Console(); |         //Console console = Console(); | ||||||
|         |         | ||||||
| 	} | 	} | ||||||
|     glm::vec3 cameraPosition = glm::vec3(0.0f, 0.0f, -5.0f); |  | ||||||
|     glm::vec3 cameraRotation = glm::vec3(0.0f); |  | ||||||
| 	void OnUpdate() override { | 	void OnUpdate() override { | ||||||
|         scene.Update(); |         scene.Update(); | ||||||
|         /* |  | ||||||
|         * if (Sceneview.isFocused) { |  | ||||||
| 			UpdateSceneCamera(sceneview); |  | ||||||
| 		} |  | ||||||
|         */ |  | ||||||
|  |  | ||||||
|         auto components = scene.getReg().view<Render3DComponent>(); |         auto components = scene.getReg().view<Render3DComponent>(); | ||||||
|         for(auto component : components){ |         for(auto component : components){ | ||||||
| @ -113,13 +114,47 @@ public: | |||||||
|         } |         } | ||||||
|  |  | ||||||
|  |  | ||||||
|         camera->view = glm::translate(glm::mat4(1.0f), cameraPosition) * glm::toMat4(glm::quat(cameraRotation)); |  | ||||||
|        |        | ||||||
|         renderer.Render(scene, *camera); |         renderer.Render(scene, *camera); | ||||||
|  |  | ||||||
|  |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  |     bool OnKey(int key, int mode) override { | ||||||
|  |         | ||||||
|  |         if (SceneisFocused) { | ||||||
|  |             spdlog::info("update camera!"); | ||||||
|  |             if (key == YOGGIE_KEY_UP) | ||||||
|  |                 camera->Rotation.x += movement_speed; | ||||||
|  |  | ||||||
|  |             if (key == YOGGIE_KEY_DOWN) | ||||||
|  |                 camera->Rotation.x -= movement_speed; | ||||||
|  |  | ||||||
|  |             if (key == YOGGIE_KEY_LEFT) | ||||||
|  |                 camera->Rotation.y += movement_speed; | ||||||
|  |  | ||||||
|  |             if (key == YOGGIE_KEY_RIGHT) | ||||||
|  |                 camera->Rotation.y -= movement_speed; | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |             if (key == YOGGIE_KEY_A) | ||||||
|  |                 camera->Position += glm::vec3(1.0f, 0.0f, 0.0f) * movement_speed; | ||||||
|  |  | ||||||
|  |             if (key == YOGGIE_KEY_S) | ||||||
|  |                 camera->Position += glm::vec3(0.0f, 0.0f, -1.0f) * movement_speed; | ||||||
|  |  | ||||||
|  |             if (key == YOGGIE_KEY_D) | ||||||
|  |                 camera->Position -= glm::vec3(1.0f, 0.0f, 0.0f) * movement_speed; | ||||||
|  |  | ||||||
|  |             if (key == GLFW_KEY_W) | ||||||
|  |                 camera->Position -= glm::vec3(0.0f, 0.0f, -1.0f) * movement_speed; | ||||||
|  |  | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |  | ||||||
|  |         return true; | ||||||
|  |     } | ||||||
|  |  | ||||||
|     ImGuizmo::OPERATION activeOperation = ImGuizmo::OPERATION::TRANSLATE; |     ImGuizmo::OPERATION activeOperation = ImGuizmo::OPERATION::TRANSLATE; | ||||||
|  |  | ||||||
| @ -215,11 +250,13 @@ public: | |||||||
|                 switch (result) { |                 switch (result) { | ||||||
|                 case(NFD_OKAY): |                 case(NFD_OKAY): | ||||||
|                     // Import Model |                     // Import Model | ||||||
|  |                     /* | ||||||
|                     AssetRegistry::LoadFromSource( |                     AssetRegistry::LoadFromSource( | ||||||
|                         path, |                         path, | ||||||
|                         "build/Debug/Assets"//project.get()->GetProjectDirectory() / "Assets" |                         "build/Debug/Assets"//project.get()->GetProjectDirectory() / "Assets" | ||||||
|                     ); |                     ); | ||||||
|  |                     */ | ||||||
|  |                      | ||||||
|                     break; |                     break; | ||||||
|                 case(NFD_CANCEL): |                 case(NFD_CANCEL): | ||||||
|                     break; |                     break; | ||||||
| @ -373,8 +410,8 @@ public: | |||||||
|             | ImGuiWindowFlags_NoCollapse; |             | ImGuiWindowFlags_NoCollapse; | ||||||
|         ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2{ 0,0 }); |         ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2{ 0,0 }); | ||||||
|         ImGui::Begin("SceneView",nullptr,viewportWindowFlags); |         ImGui::Begin("SceneView",nullptr,viewportWindowFlags); | ||||||
|  |        // spdlog::info("{0}x{1}", ImGui::GetWindowWidth(), ImGui::GetWindowHeight()); | ||||||
|  |         SceneisFocused = ImGui::IsWindowFocused() || ImGui::IsWindowHovered(); | ||||||
|         ImGui::Image((ImTextureID)(intptr_t)renderer.getCurrentFrameBuffer().GetColourAttachment(), |         ImGui::Image((ImTextureID)(intptr_t)renderer.getCurrentFrameBuffer().GetColourAttachment(), | ||||||
|             ImVec2{(float)ImGui::GetWindowWidth(),(float)ImGui::GetWindowHeight()}); |             ImVec2{(float)ImGui::GetWindowWidth(),(float)ImGui::GetWindowHeight()}); | ||||||
|  |  | ||||||
| @ -391,31 +428,33 @@ public: | |||||||
|  |  | ||||||
|             ImGuizmo::SetRect(ImGui::GetWindowPos().x, ImGui::GetWindowPos().y, ImGui::GetWindowWidth(), ImGui::GetWindowHeight()); |             ImGuizmo::SetRect(ImGui::GetWindowPos().x, ImGui::GetWindowPos().y, ImGui::GetWindowWidth(), ImGui::GetWindowHeight()); | ||||||
|  |  | ||||||
|             const auto& ProjMatrix = camera->projection; |             const auto& ProjMatrix = camera->getProjection(ImGui::GetWindowWidth(), ImGui::GetWindowHeight()); | ||||||
|  |             const glm::mat4& viewMatrix  = glm::inverse(camera->getTransform()); | ||||||
|             const glm::mat4& viewMatrix  = ((EditorCamera*)camera)->view; |  | ||||||
|             glm::mat4 cameraView = glm::inverse(viewMatrix); |  | ||||||
|             glm::mat4 cameraDelta = glm::mat4(1.0f); |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|             auto& tc = Selected.GetComponent<YoggieEngine::TransformComponent>(); |             auto& tc = Selected.GetComponent<YoggieEngine::TransformComponent>(); | ||||||
|             auto transform = tc.GetTransform(); |             auto transform = tc.GetTransform(); | ||||||
|             ImGuizmo::Manipulate(glm::value_ptr(cameraView), glm::value_ptr(ProjMatrix), activeOperation, ImGuizmo::WORLD, glm::value_ptr(transform), nullptr, nullptr); |             ImGuizmo::Manipulate( | ||||||
|             if(ImGuizmo::IsUsing()) |                 glm::value_ptr(viewMatrix), | ||||||
|  |                 glm::value_ptr(ProjMatrix), | ||||||
|  |                 activeOperation, ImGuizmo::LOCAL, glm::value_ptr(transform)); | ||||||
|  |             if (ImGuizmo::IsUsing()) | ||||||
|  |             { | ||||||
|                 tc.Decompose(transform); |                 tc.Decompose(transform); | ||||||
|  |  | ||||||
|             } |             } | ||||||
|  |  | ||||||
|  |  | ||||||
|  |         }    | ||||||
|  |  | ||||||
|  |  | ||||||
|          |          | ||||||
|         ImGui::End(); |         ImGui::End(); | ||||||
|         ImGui::PopStyleVar(); |         ImGui::PopStyleVar(); | ||||||
|  |  | ||||||
|         ImGui::Begin("EditorCamera"); |         ImGui::Begin("EditorCamera"); | ||||||
|          |          | ||||||
|         ImGui::SliderFloat3("position", glm::value_ptr(cameraPosition), -50, 50); |         ImGui::SliderFloat3("position", glm::value_ptr(camera->Position), -50, 50); | ||||||
|         ImGui::End(); |         ImGui::End(); | ||||||
|  |  | ||||||
|  |  | ||||||
| @ -469,8 +508,11 @@ private: | |||||||
|     char* path = nullptr; |     char* path = nullptr; | ||||||
|     Texture Logo; |     Texture Logo; | ||||||
|     Renderer renderer; |     Renderer renderer; | ||||||
|     Camera* camera = new EditorCamera(); |     EditorCamera* camera = new EditorCamera(); | ||||||
|     Mesh cube ; |     Mesh cube ; | ||||||
|  |     bool SceneisFocused = false; | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|     void LoadLastOrEmptyProject() { |     void LoadLastOrEmptyProject() { | ||||||
|         // Check if there is a last known loaded project and  |         // Check if there is a last known loaded project and  | ||||||
| @ -504,101 +546,4 @@ private: | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |  | ||||||
|     void UpdateSceneCamera() { |  | ||||||
|         const float movement_speed = 0.01f; |  | ||||||
|         static float lastX = 400, lastY = 300; |  | ||||||
|         const float sensitivity = 0.1; |  | ||||||
|         static bool firstMouse = true; |  | ||||||
|  |  | ||||||
|         /* |  | ||||||
|  |  | ||||||
|         if (MouseButtonPressed(YOGGIE_MOUSE_BUTTON_RIGHT)) { |  | ||||||
|  |  | ||||||
|             glfwSetInputMode((GLFWwindow*)appWindow->GetHandle(), GLFW_CURSOR, GLFW_CURSOR_HIDDEN); |  | ||||||
|             auto newX = getCursorPosX(appWindow); |  | ||||||
|             auto newY = getCursorPosY(appWindow); |  | ||||||
|  |  | ||||||
|             if (firstMouse) |  | ||||||
|             { |  | ||||||
|                 lastX = newX; |  | ||||||
|                 lastY = newY; |  | ||||||
|                 firstMouse = false; |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|  |  | ||||||
|             float xoffset = newX - lastX; |  | ||||||
|             float yoffset = newY - lastY; |  | ||||||
|  |  | ||||||
|             lastX = newX; |  | ||||||
|             lastY = newY; |  | ||||||
|  |  | ||||||
|             xoffset *= sensitivity; |  | ||||||
|             yoffset *= sensitivity; |  | ||||||
|  |  | ||||||
|             sceneview.cam.Rotation.x += (xoffset / 2); |  | ||||||
|             sceneview.cam.Rotation.y += (xoffset /2); |  | ||||||
|             sceneview.cam.Rotation.z += yoffset; |  | ||||||
|  |  | ||||||
|  |  | ||||||
|             if (sceneview.cam.pitch > 89.0f) |  | ||||||
|                 sceneview.cam.pitch = 89.0f; |  | ||||||
|             if (sceneview.cam.pitch < -89.0f) |  | ||||||
|                 sceneview.cam.pitch = -89.0f; |  | ||||||
|  |  | ||||||
|  |  | ||||||
|         } |  | ||||||
|         else if (firstMouse == false) |  | ||||||
|         { |  | ||||||
|             glfwSetInputMode((GLFWwindow*)appWindow->GetHandle(), GLFW_CURSOR, GLFW_CURSOR_NORMAL); |  | ||||||
|             firstMouse = true; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|         */ |  | ||||||
|          |  | ||||||
|        /* |  | ||||||
|        EditorCamera& cam = sceneview.GetCamera(); |  | ||||||
|  |  | ||||||
|          |  | ||||||
|          |  | ||||||
|         if (keyIsPressed(YOGGIE_KEY_UP)) |  | ||||||
|             cam.Rotation.x += movement_speed; |  | ||||||
|  |  | ||||||
|         if (keyIsPressed(YOGGIE_KEY_DOWN)) |  | ||||||
|             cam.Rotation.x -= movement_speed; |  | ||||||
|  |  | ||||||
|         if (keyIsPressed(YOGGIE_KEY_LEFT)) |  | ||||||
|             cam.Rotation.y += movement_speed; |  | ||||||
|  |  | ||||||
|         if (keyIsPressed(YOGGIE_KEY_RIGHT)) |  | ||||||
|             cam.Rotation.y -= movement_speed; |  | ||||||
|  |  | ||||||
|  |  | ||||||
|         cam.Update(); |  | ||||||
|  |  | ||||||
|        */ |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|         /* |  | ||||||
|  |  | ||||||
|         // Check for Camara movement input here! |  | ||||||
|         if (keyIsPressed(YOGGIE_KEY_W)) |  | ||||||
|             sceneview.cam.Position -= sceneview.cam.Front * movement_speed; |  | ||||||
|  |  | ||||||
|         if (keyIsPressed(YOGGIE_KEY_A)) |  | ||||||
|             sceneview.cam.Position += sceneview.cam.Right * movement_speed; |  | ||||||
|  |  | ||||||
|         if (keyIsPressed(YOGGIE_KEY_S)) |  | ||||||
|             sceneview.cam.Position += sceneview.cam.Front * movement_speed; |  | ||||||
|  |  | ||||||
|         if (keyIsPressed(YOGGIE_KEY_D)) |  | ||||||
|             sceneview.cam.Position -= sceneview.cam.Right * movement_speed; |  | ||||||
|             */ |  | ||||||
|  |  | ||||||
|  |  | ||||||
|     } |  | ||||||
|  |  | ||||||
| }; | }; | ||||||
| @ -11,43 +11,12 @@ public: | |||||||
|  |  | ||||||
|     void Run() override |     void Run() override | ||||||
|     { |     { | ||||||
|  |         PushLayer(new EditorLayer()); | ||||||
|  |  | ||||||
|  |         Application::Run(); | ||||||
|         // Create EditorLayer |  | ||||||
|         EditorLayer* firstLayer = new EditorLayer(); |  | ||||||
|     |  | ||||||
|         firstLayer->OnStartup(); |  | ||||||
|          |  | ||||||
|  |  | ||||||
|         double previous = glfwGetTime(); |  | ||||||
|         double lag = 0.0; |  | ||||||
|         while (!appWindow->WindowShouldClose()) { |  | ||||||
|             PollEvents(); |  | ||||||
|             double now = glfwGetTime(); |  | ||||||
|             double elapsed = now - previous; |  | ||||||
|             previous = now; |  | ||||||
|             lag += elapsed; |  | ||||||
|             GuiBegin(); |  | ||||||
|             firstLayer->OnUpdate(); |  | ||||||
|             firstLayer->OnUI(); |  | ||||||
|             GuiEnd(); |  | ||||||
|  |  | ||||||
|             SwapBuffers(); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         firstLayer->OnDestroy(); |  | ||||||
|          |  | ||||||
|  |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
| private: |  | ||||||
|  |  | ||||||
|  |  | ||||||
|     std::vector<Layer*> layers = std::vector<Layer*>(); |  | ||||||
|   |  | ||||||
|  |  | ||||||
| }; | }; | ||||||
|  |  | ||||||
| YoggieEngine::Application* CreateApplication() { | YoggieEngine::Application* CreateApplication() { | ||||||
|  | |||||||
| @ -1,4 +1,5 @@ | |||||||
| #include <YoggieEngine.h> | #include <YoggieEngine.h> | ||||||
|  | #include <ImGuizmo.h> | ||||||
| #include <gtest/gtest.h> | #include <gtest/gtest.h> | ||||||
|  |  | ||||||
|  |  | ||||||
| @ -23,6 +24,42 @@ TEST(TRANSFORM_COMPONENT_TESTS , CAN_EXTRACT_TRANSLATION_FROM_TRANSFORM_MATRIX) | |||||||
|  |  | ||||||
| } | } | ||||||
|  |  | ||||||
|  | TEST(TRANSFORM_COMPONENT_TESTS, CAN_EXTRACT_SCALE_FROM_TRANSFORM_MATRIX) { | ||||||
|  | 	auto component = YoggieEngine::TransformComponent{}; | ||||||
|  |  | ||||||
|  | 	component.Scale = glm::vec3(1.0f, 2.0f, 3.0f); | ||||||
|  |  | ||||||
|  | 	auto tranformationMatrix = component.GetTransform(); | ||||||
|  |  | ||||||
|  | 	auto newComponent = YoggieEngine::TransformComponent{}; | ||||||
|  |  | ||||||
|  | 	newComponent.Decompose(tranformationMatrix); | ||||||
|  |  | ||||||
|  | 	EXPECT_EQ(newComponent.Scale.x, component.Scale.x); | ||||||
|  | 	EXPECT_EQ(newComponent.Scale.y, component.Scale.y); | ||||||
|  | 	EXPECT_EQ(newComponent.Scale.z, component.Scale.z); | ||||||
|  |  | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | TEST(TRANSFORM_COMPONENT_TESTS, CAN_EXTRACT_ROTATION_FROM_TRANSFORM_MATRIX) { | ||||||
|  | 	auto component = YoggieEngine::TransformComponent{}; | ||||||
|  | 	component.Rotation = glm::vec3(2.0f, 10.0f, 20.0f); | ||||||
|  |  | ||||||
|  | 	auto transformMatrix = component.GetTransform(); | ||||||
|  | 	 | ||||||
|  | 	auto newComponent = YoggieEngine::TransformComponent{}; | ||||||
|  |  | ||||||
|  | 	newComponent.Decompose(transformMatrix); | ||||||
|  |  | ||||||
|  |  | ||||||
|  | 	EXPECT_EQ(newComponent.Rotation.x, component.Rotation.x); | ||||||
|  | 	EXPECT_EQ(newComponent.Rotation.y, component.Rotation.y); | ||||||
|  | 	EXPECT_EQ(newComponent.Rotation.z, component.Rotation.z); | ||||||
|  |  | ||||||
|  |  | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
| int main(int argc, char** argv) { | int main(int argc, char** argv) { | ||||||
|  |  | ||||||
|  | |||||||
| @ -50,12 +50,57 @@ namespace YoggieEngine { | |||||||
| 		//ImGuizmo::SetOrthographic(true); | 		//ImGuizmo::SetOrthographic(true); | ||||||
|  |  | ||||||
|  |  | ||||||
| 		init_inputSystem(appWindow); | 		//init_inputSystem(appWindow); | ||||||
|  | 		glfwSetWindowUserPointer((GLFWwindow*)this->appWindow->GetHandle(), this); | ||||||
|  | 		glfwSetKeyCallback((GLFWwindow*)this->appWindow->GetHandle(), HandleKey); | ||||||
|  |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  | 	void Application::HandleKey(GLFWwindow* window, int key, int scancode, int action, int mods) { | ||||||
|  | 		auto app = (Application*)glfwGetWindowUserPointer(window); | ||||||
|  | 		for (auto i = app->AppLayerstack.begin(); i < app->AppLayerstack.end(); i++) { | ||||||
|  | 			if ((*i)->OnKey(key, action)) { | ||||||
|  | 				break; | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	void Application::Run() { | 	void Application::Run() { | ||||||
| 		 | 		 | ||||||
|  |  | ||||||
|  | 		for (auto i = AppLayerstack.begin(); i < AppLayerstack.end(); i++) { | ||||||
|  | 			(*i)->OnStartup(); | ||||||
|  | 		} | ||||||
|  | 		 | ||||||
|  | 		double previous = glfwGetTime(); | ||||||
|  | 		double lag = 0.0; | ||||||
|  |  | ||||||
|  | 		while (!appWindow->WindowShouldClose()) { | ||||||
|  | 			PollEvents(); | ||||||
|  | 			double now = glfwGetTime(); | ||||||
|  | 			double elapsed = now - previous; | ||||||
|  | 			previous = now; | ||||||
|  | 			lag += elapsed; | ||||||
|  |  | ||||||
|  |  | ||||||
|  | 			for (auto i = AppLayerstack.begin(); i < AppLayerstack.end(); i++) { | ||||||
|  | 				(*i)->OnUpdate(); | ||||||
|  | 			} | ||||||
|  |  | ||||||
|  | 			GuiBegin(); | ||||||
|  | 			for (auto i = AppLayerstack.begin(); i < AppLayerstack.end(); i++) { | ||||||
|  | 				(*i)->OnUI(); | ||||||
|  | 			} | ||||||
|  | 			GuiEnd(); | ||||||
|  |  | ||||||
|  | 			SwapBuffers(); | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		for (auto i = AppLayerstack.begin(); i < AppLayerstack.end(); i++) { | ||||||
|  | 			(*i)->OnDestroy(); | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  |  | ||||||
|  | |||||||
| @ -20,6 +20,7 @@ namespace YoggieEngine { | |||||||
|  |  | ||||||
| 		void PushLayer(Layer* layer); | 		void PushLayer(Layer* layer); | ||||||
|  |  | ||||||
|  | 		static void HandleKey(GLFWwindow* window, int key, int scancode, int action, int mods); | ||||||
|  |  | ||||||
|  |  | ||||||
| 	protected: | 	protected: | ||||||
|  | |||||||
| @ -1,11 +1,37 @@ | |||||||
| #pragma once | #pragma once | ||||||
|  | #include <glm/gtx/quaternion.hpp> | ||||||
|  |  | ||||||
| namespace YoggieEngine { | namespace YoggieEngine { | ||||||
| 	class Camera { | 	class Camera { | ||||||
| 	public: | 	public: | ||||||
| 		Camera() { | 		Camera() { | ||||||
|  | 			projection = glm::perspective(glm::radians(90.0f), 800.0f / 600.0f, 0.001f, 1000.0f); | ||||||
|  | 			//projection[3][1] *= -1; | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		glm::mat4 getTransform() const { | ||||||
|  |  | ||||||
|  | 			auto transform = glm::translate(glm::mat4(1.0f), Position) | ||||||
|  | 				* glm::toMat4(glm::quat(Rotation)) | ||||||
|  | 				* glm::scale(glm::mat4(1.0f), glm::vec3(1.0f)); | ||||||
|  |  | ||||||
|  | 			return glm::inverse(transform); | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		glm::mat4& getProjection(float width, float height) { | ||||||
|  |  | ||||||
|  | 			//projection[3][1] *= -1; | ||||||
|  |  | ||||||
|  | 			return projection; | ||||||
|  |  | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
|  | 		glm::vec3 Position = glm::vec3(0.0f); | ||||||
|  | 		glm::vec3 Rotation = glm::vec3(0.0f); | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  | 	private: | ||||||
| 		glm::mat4 view; | 		glm::mat4 view; | ||||||
| 		glm::mat4 projection; | 		glm::mat4 projection; | ||||||
|  |  | ||||||
|  | |||||||
| @ -4,6 +4,12 @@ | |||||||
| #include "../Scene/Components.h" | #include "../Scene/Components.h" | ||||||
| #include "../Graphics/Memory/Buffer.h" | #include "../Graphics/Memory/Buffer.h" | ||||||
| #include "../Graphics/Memory/VertexArray.h" | #include "../Graphics/Memory/VertexArray.h" | ||||||
|  | #include "Renderable.h" | ||||||
|  | #include "Memory/Framebuffer.h" | ||||||
|  | #include "../Scene/Components.h" | ||||||
|  | #include"../Scene/Scene.h" | ||||||
|  | #include "Primitives/Camera.h" | ||||||
|  | #include "OpenglAPI.h" | ||||||
|  |  | ||||||
| namespace YoggieEngine { | namespace YoggieEngine { | ||||||
| unsigned int quadVAO = 0; | unsigned int quadVAO = 0; | ||||||
| @ -66,7 +72,7 @@ float skyboxVertices[]{ | |||||||
|  |  | ||||||
|  |  | ||||||
| Renderer::Renderer() : | Renderer::Renderer() : | ||||||
| 	m_framebuffer(960, 540), | 	m_framebuffer(800, 600), | ||||||
| 	gBufferShader("build/Debug/Shaders/deferred/geometry.vert", "build/Debug/Shaders/deferred/geometry.frag"), | 	gBufferShader("build/Debug/Shaders/deferred/geometry.vert", "build/Debug/Shaders/deferred/geometry.frag"), | ||||||
| 	lightingPassShader("build/Debug/Shaders/deferred/lightPass.vert", "build/Debug/Shaders/deferred/lightPass.frag"), | 	lightingPassShader("build/Debug/Shaders/deferred/lightPass.vert", "build/Debug/Shaders/deferred/lightPass.frag"), | ||||||
| 	SkyboxShader("build/Debug/Shaders/Cubemaps/Skybox.vert", "build/Debug/Shaders/Cubemaps/Skybox.frag"), | 	SkyboxShader("build/Debug/Shaders/Cubemaps/Skybox.vert", "build/Debug/Shaders/Cubemaps/Skybox.frag"), | ||||||
| @ -74,8 +80,8 @@ Renderer::Renderer() : | |||||||
| 	forwardShader("build/Debug/Shaders/forward/geometry.vert", "build/Debug/Shaders/forward/geometry.frag"), | 	forwardShader("build/Debug/Shaders/forward/geometry.vert", "build/Debug/Shaders/forward/geometry.frag"), | ||||||
| 	postProcessingShader("build/Debug/Shaders/forward/postprocessing.vert", "build/Debug/Shaders/forward/postprocessing.frag") | 	postProcessingShader("build/Debug/Shaders/forward/postprocessing.vert", "build/Debug/Shaders/forward/postprocessing.frag") | ||||||
| { | { | ||||||
| 	width = 960; | 	width = 800; | ||||||
| 	height = 540; | 	height = 600; | ||||||
| 	glEnable(GL_DEPTH_TEST); | 	glEnable(GL_DEPTH_TEST); | ||||||
| 	CreateGBuffer(); | 	CreateGBuffer(); | ||||||
|  |  | ||||||
| @ -142,6 +148,180 @@ void Renderer::CreateGBuffer() { | |||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | void Renderer::Render(Scene& scene , Camera MainCamera){ | ||||||
|  | 	int oldviewport[4]; | ||||||
|  | 	glGetIntegerv(GL_VIEWPORT, oldviewport); | ||||||
|  | 	glViewport(0, 0, width, height); | ||||||
|  |  | ||||||
|  |  | ||||||
|  | 	glBindFramebuffer(GL_FRAMEBUFFER, gBuffer); | ||||||
|  | 	glClearColor(0.0f, 0.0f, 0.0f, 1.0f); | ||||||
|  | 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | ||||||
|  |  | ||||||
|  | 	// Render skybox if the scene has one  | ||||||
|  |  | ||||||
|  | 	SkyboxShader.Use(); | ||||||
|  | 	SkyboxShader.setUniformMat4("projection", MainCamera.getProjection(width, height)); | ||||||
|  | 	SkyboxShader.setUniformMat4("view", glm::inverse( glm::mat4(glm::mat3(MainCamera.getTransform())))); | ||||||
|  |  | ||||||
|  | 	if (!skyboxVAO) { | ||||||
|  | 		unsigned int VBO; | ||||||
|  | 		glGenVertexArrays(1, &skyboxVAO); | ||||||
|  | 		glGenBuffers(1, &VBO); | ||||||
|  | 		glBindVertexArray(skyboxVAO); | ||||||
|  | 		glBindBuffer(GL_ARRAY_BUFFER, VBO); | ||||||
|  | 		glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), &skyboxVertices, GL_STATIC_DRAW); | ||||||
|  | 		glEnableVertexAttribArray(0); | ||||||
|  | 		glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); | ||||||
|  |  | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	OpenGLApi::DrawCubeMap(skyboxVAO, skybox); | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  | 	auto renderables = scene.getReg().view<TransformComponent, Render3DComponent>(); | ||||||
|  | 	gBufferShader.Use(); | ||||||
|  | 	for (auto renderable : renderables) { | ||||||
|  | 		auto entity = Entity(renderable, &scene); | ||||||
|  | 		auto& renderComponent = entity.GetComponent<Render3DComponent>(); | ||||||
|  | 		auto& transform = entity.GetComponent<TransformComponent>(); | ||||||
|  |  | ||||||
|  | 		// Geometry pass  | ||||||
|  | 		gBufferShader.setUniformVec3("Color", renderComponent.color); | ||||||
|  | 		gBufferShader.setUniformMat4("Model", transform.GetTransform()); | ||||||
|  | 		gBufferShader.setUniformMat4("View", glm::inverse(MainCamera.getTransform())); | ||||||
|  | 		gBufferShader.setUniformMat4("Projection", MainCamera.getProjection(width, height)); | ||||||
|  | 		OpenGLApi::DrawTriangles(renderComponent); | ||||||
|  |  | ||||||
|  |  | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Light pass | ||||||
|  | 	glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer.GetId()); | ||||||
|  | 	lightingPassShader.Use(); | ||||||
|  | 	lightingPassShader.setUniformInt("gPosition", 0); | ||||||
|  | 	lightingPassShader.setUniformInt("gNormal", 1); | ||||||
|  | 	lightingPassShader.setUniformInt("gColorSpec", 2); | ||||||
|  |  | ||||||
|  | 	glActiveTexture(GL_TEXTURE0); | ||||||
|  | 	glBindTexture(GL_TEXTURE_2D, gPosition); | ||||||
|  |  | ||||||
|  | 	glActiveTexture(GL_TEXTURE1); | ||||||
|  | 	glBindTexture(GL_TEXTURE_2D, gNormal); | ||||||
|  |  | ||||||
|  | 	glActiveTexture(GL_TEXTURE2); | ||||||
|  | 	glBindTexture(GL_TEXTURE_2D, gColorSpec); | ||||||
|  |  | ||||||
|  | 	auto lights = scene.getReg().view<LightComponent, TransformComponent>(); | ||||||
|  | 	unsigned int lightnr = 0; | ||||||
|  | 	for (auto light : lights) { | ||||||
|  | 		auto lightComponent = Entity(light, &scene).GetComponent<LightComponent>(); | ||||||
|  | 		auto transformComponent = Entity(light, &scene).GetComponent<TransformComponent>(); | ||||||
|  |  | ||||||
|  | 		auto name = "lights[" + std::to_string(lightnr) + "]"; | ||||||
|  | 		lightingPassShader.setUniformVec3(name + ".Position", transformComponent.Position); | ||||||
|  | 		lightingPassShader.setUniformVec3(name + ".Color", lightComponent.Color); | ||||||
|  |  | ||||||
|  | 		const float linear = 0.7f; | ||||||
|  | 		const float quadratic = 1.8f; | ||||||
|  | 		lightingPassShader.setUniformFloat(name + ".Linear", linear); | ||||||
|  | 		lightingPassShader.setUniformFloat(name + ".Quadratic", quadratic); | ||||||
|  |  | ||||||
|  | 		lightnr++; | ||||||
|  |  | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if (quadVAO == 0) | ||||||
|  | 	{ | ||||||
|  | 		float quadVertices[] = { | ||||||
|  | 			-1.0f,	 1.0f, 0.0f, 0.0f, -1.0f, | ||||||
|  | 			-1.0f,	-1.0f, 0.0f, 0.0f, 0.0f, | ||||||
|  | 			 1.0f,	 1.0f, 0.0f, 1.0f, -1.0f, | ||||||
|  | 			 1.0f,	-1.0f, 0.0f, 1.0f, 0.0f, | ||||||
|  | 		}; | ||||||
|  | 		unsigned int quadVBO; | ||||||
|  | 		// setup plane VAO ; | ||||||
|  | 		glGenVertexArrays(1, &quadVAO); | ||||||
|  | 		glGenBuffers(1, &quadVBO); | ||||||
|  | 		glBindVertexArray(quadVAO); | ||||||
|  | 		glBindBuffer(GL_ARRAY_BUFFER, quadVBO); | ||||||
|  | 		glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), &quadVertices, GL_STATIC_DRAW); | ||||||
|  | 		glEnableVertexAttribArray(0); | ||||||
|  | 		glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0); | ||||||
|  | 		glEnableVertexAttribArray(1); | ||||||
|  | 		glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float))); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	glBindVertexArray(quadVAO); | ||||||
|  | 	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); | ||||||
|  | 	glBindVertexArray(0); | ||||||
|  |  | ||||||
|  |  | ||||||
|  | 	// Copy GBuffer | ||||||
|  | 	glBindFramebuffer(GL_READ_FRAMEBUFFER, gBuffer); | ||||||
|  | 	glBindFramebuffer(GL_DRAW_BUFFER, m_framebuffer.GetId()); | ||||||
|  | 	glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_DEPTH_BUFFER_BIT, GL_NEAREST); | ||||||
|  |  | ||||||
|  |  | ||||||
|  | 	if (transparentVAO == 0) { | ||||||
|  | 		unsigned int transparentVBO; | ||||||
|  | 		float transparentVertices[] = { | ||||||
|  | 			// positions         // texture Coords (swapped y coordinates because texture is flipped upside down) | ||||||
|  | 			0.0f,  0.5f,  0.0f,  0.0f,  1.0f, | ||||||
|  | 			0.0f, -0.5f,  0.0f,  0.0f,  0.0f, | ||||||
|  | 			1.0f, -0.5f,  0.0f,  1.0f,  0.0f, | ||||||
|  |  | ||||||
|  | 			0.0f,  0.5f,  0.0f,  0.0f,  1.0f, | ||||||
|  | 			1.0f, -0.5f,  0.0f,  1.0f,  0.0f, | ||||||
|  | 			1.0f,  0.5f,  0.0f,  1.0f,  1.0f | ||||||
|  | 		}; | ||||||
|  |  | ||||||
|  | 		glGenVertexArrays(1, &transparentVAO); | ||||||
|  | 		glGenBuffers(1, &transparentVBO); | ||||||
|  | 		glBindVertexArray(transparentVAO); | ||||||
|  | 		glBindBuffer(GL_ARRAY_BUFFER, transparentVBO); | ||||||
|  | 		glBufferData(GL_ARRAY_BUFFER, sizeof(transparentVertices), transparentVertices, GL_STATIC_DRAW); | ||||||
|  | 		glEnableVertexAttribArray(0); | ||||||
|  | 		glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0); | ||||||
|  | 		glEnableVertexAttribArray(1); | ||||||
|  | 		glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float))); | ||||||
|  | 		glBindVertexArray(0); | ||||||
|  |  | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	glEnable(GL_BLEND); | ||||||
|  | 	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | ||||||
|  |  | ||||||
|  | 	BlendingShader.Use(); | ||||||
|  | 	glBindVertexArray(transparentVAO); | ||||||
|  | 	glActiveTexture(GL_TEXTURE0); | ||||||
|  | 	glBindTexture(GL_TEXTURE_2D, grassTexture.GetID()); | ||||||
|  |  | ||||||
|  | 	BlendingShader.setUniformMat4("V", glm::inverse(MainCamera.getTransform())); | ||||||
|  | 	BlendingShader.setUniformMat4("P", MainCamera.getProjection(width, height)); | ||||||
|  | 	for (unsigned int i = 0; i < vegetation.size(); i++) { | ||||||
|  |  | ||||||
|  | 		auto translation = glm::translate(glm::mat4(1.0f), vegetation[i]); | ||||||
|  | 		BlendingShader.setUniformMat4("M", translation); | ||||||
|  | 		glDrawArrays(GL_TRIANGLES, 0, 6); | ||||||
|  |  | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	glBindVertexArray(0); | ||||||
|  |  | ||||||
|  | 	glDisable(GL_BLEND); | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  | 	glBindFramebuffer(GL_FRAMEBUFFER, 0); | ||||||
|  | 	glViewport(oldviewport[0], oldviewport[1], oldviewport[2], oldviewport[3]); | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
| Renderer::~Renderer(){} | Renderer::~Renderer(){} | ||||||
|  |  | ||||||
|  |  | ||||||
|  | |||||||
| @ -2,13 +2,8 @@ | |||||||
| #include "GLFW/glfw3.h" | #include "GLFW/glfw3.h" | ||||||
| #include <vector> | #include <vector> | ||||||
| #include "../PerfCounter.h" | #include "../PerfCounter.h" | ||||||
|  | #include "../Scene/Scene.h" | ||||||
| #include "Renderable.h" |  | ||||||
| #include "Memory/Framebuffer.h" | #include "Memory/Framebuffer.h" | ||||||
| #include "../Scene/Components.h" |  | ||||||
| #include"../Scene/Scene.h" |  | ||||||
| #include "Primitives/Camera.h" |  | ||||||
| #include "OpenglAPI.h" |  | ||||||
|  |  | ||||||
| namespace YoggieEngine { | namespace YoggieEngine { | ||||||
|  |  | ||||||
| @ -35,182 +30,7 @@ namespace YoggieEngine { | |||||||
| 		OpenGLApi::DrawTriangles(renderComponent); | 		OpenGLApi::DrawTriangles(renderComponent); | ||||||
| 		*/ | 		*/ | ||||||
|  |  | ||||||
| 		void Render(Scene& scene, Camera MainCamera) { | 		void Render(Scene& scene, Camera MainCamera); | ||||||
| 			 |  | ||||||
| 			MainCamera.projection = glm::perspective(glm::radians(65.0f), ((float)width/(float)height), 0.0001f, 1000.0f); |  | ||||||
| 			int oldviewport[4]; |  | ||||||
| 			glGetIntegerv(GL_VIEWPORT, oldviewport); |  | ||||||
| 			glViewport(0, 0, width, height); |  | ||||||
|  |  | ||||||
|  |  | ||||||
| 			glBindFramebuffer(GL_FRAMEBUFFER, gBuffer); |  | ||||||
| 			glClearColor(0.0f, 0.0f, 0.0f, 1.0f); |  | ||||||
| 			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |  | ||||||
|  |  | ||||||
| 			// Render skybox if the scene has one  |  | ||||||
|  |  | ||||||
| 			SkyboxShader.Use(); |  | ||||||
| 			SkyboxShader.setUniformMat4("projection", MainCamera.projection); |  | ||||||
| 			SkyboxShader.setUniformMat4("view", glm::mat4(glm::mat3(MainCamera.view))); |  | ||||||
|  |  | ||||||
| 			if (!skyboxVAO) { |  | ||||||
| 				unsigned int VBO; |  | ||||||
| 				glGenVertexArrays(1, &skyboxVAO); |  | ||||||
| 				glGenBuffers(1, &VBO); |  | ||||||
| 				glBindVertexArray(skyboxVAO); |  | ||||||
| 				glBindBuffer(GL_ARRAY_BUFFER, VBO); |  | ||||||
| 				glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), &skyboxVertices, GL_STATIC_DRAW); |  | ||||||
| 				glEnableVertexAttribArray(0); |  | ||||||
| 				glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); |  | ||||||
| 					 |  | ||||||
| 			} |  | ||||||
|  |  | ||||||
| 			OpenGLApi::DrawCubeMap( skyboxVAO, skybox); |  | ||||||
| 			 |  | ||||||
|  |  | ||||||
|  |  | ||||||
| 		 |  | ||||||
| 			auto renderables = scene.getReg().view<TransformComponent, Render3DComponent>(); |  | ||||||
| 			gBufferShader.Use(); |  | ||||||
| 			for (auto renderable : renderables) { |  | ||||||
| 				auto entity = Entity(renderable, &scene); |  | ||||||
| 				auto& renderComponent = entity.GetComponent<Render3DComponent>(); |  | ||||||
| 				auto& transform = entity.GetComponent<TransformComponent>(); |  | ||||||
| 				 |  | ||||||
| 				// Geometry pass  |  | ||||||
| 				gBufferShader.setUniformVec3("Color", renderComponent.color); |  | ||||||
| 				gBufferShader.setUniformMat4("Model", transform.GetTransform()); |  | ||||||
| 				gBufferShader.setUniformMat4("View", MainCamera.view); |  | ||||||
| 				gBufferShader.setUniformMat4("Projection", MainCamera.projection); |  | ||||||
| 				OpenGLApi::DrawTriangles(renderComponent); |  | ||||||
|  |  | ||||||
| 				 |  | ||||||
| 			} |  | ||||||
|  |  | ||||||
| 			// Light pass |  | ||||||
| 			glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer.GetId()); |  | ||||||
| 			lightingPassShader.Use(); |  | ||||||
| 			lightingPassShader.setUniformInt("gPosition", 0); |  | ||||||
| 			lightingPassShader.setUniformInt("gNormal", 1); |  | ||||||
| 			lightingPassShader.setUniformInt("gColorSpec", 2); |  | ||||||
|  |  | ||||||
| 			glActiveTexture(GL_TEXTURE0); |  | ||||||
| 			glBindTexture(GL_TEXTURE_2D, gPosition); |  | ||||||
|  |  | ||||||
| 			glActiveTexture(GL_TEXTURE1); |  | ||||||
| 			glBindTexture(GL_TEXTURE_2D, gNormal); |  | ||||||
|  |  | ||||||
| 			glActiveTexture(GL_TEXTURE2); |  | ||||||
| 			glBindTexture(GL_TEXTURE_2D, gColorSpec); |  | ||||||
|  |  | ||||||
| 			auto lights = scene.getReg().view<LightComponent, TransformComponent>(); |  | ||||||
| 			unsigned int lightnr = 0; |  | ||||||
| 			for (auto light : lights) { |  | ||||||
| 				auto lightComponent = Entity(light, &scene).GetComponent<LightComponent>(); |  | ||||||
| 				auto transformComponent = Entity(light, &scene).GetComponent<TransformComponent>(); |  | ||||||
|  |  | ||||||
| 				auto name = "lights[" + std::to_string(lightnr) + "]"; |  | ||||||
| 				lightingPassShader.setUniformVec3(name + ".Position",transformComponent.Position ); |  | ||||||
| 				lightingPassShader.setUniformVec3(name + ".Color", lightComponent.Color); |  | ||||||
|  |  | ||||||
| 				const float linear = 0.7f; |  | ||||||
| 				const float quadratic = 1.8f; |  | ||||||
| 				lightingPassShader.setUniformFloat(name + ".Linear", linear); |  | ||||||
| 				lightingPassShader.setUniformFloat(name + ".Quadratic", quadratic); |  | ||||||
|  |  | ||||||
| 				lightnr++; |  | ||||||
|  |  | ||||||
| 			} |  | ||||||
|  |  | ||||||
| 			if (quadVAO == 0) |  | ||||||
| 			{ |  | ||||||
| 				float quadVertices[] = { |  | ||||||
| 					-1.0f,	 1.0f, 0.0f, 0.0f, 1.0f, |  | ||||||
| 					-1.0f,	-1.0f, 0.0f, 0.0f, 0.0f, |  | ||||||
| 					 1.0f,	 1.0f, 0.0f, 1.0f, 1.0f, |  | ||||||
| 					 1.0f,	-1.0f, 0.0f, 1.0f, 0.0f, |  | ||||||
| 				}; |  | ||||||
| 				unsigned int quadVBO; |  | ||||||
| 				// setup plane VAO ; |  | ||||||
| 				glGenVertexArrays(1, &quadVAO); |  | ||||||
| 				glGenBuffers(1, &quadVBO); |  | ||||||
| 				glBindVertexArray(quadVAO); |  | ||||||
| 				glBindBuffer(GL_ARRAY_BUFFER, quadVBO); |  | ||||||
| 				glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), &quadVertices, GL_STATIC_DRAW); |  | ||||||
| 				glEnableVertexAttribArray(0); |  | ||||||
| 				glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0); |  | ||||||
| 				glEnableVertexAttribArray(1); |  | ||||||
| 				glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float))); |  | ||||||
| 			} |  | ||||||
|  |  | ||||||
| 			glBindVertexArray(quadVAO); |  | ||||||
| 			glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); |  | ||||||
| 			glBindVertexArray(0); |  | ||||||
|  |  | ||||||
|  |  | ||||||
| 			// Copy GBuffer |  | ||||||
| 			glBindFramebuffer(GL_READ_FRAMEBUFFER, gBuffer); |  | ||||||
| 			glBindFramebuffer(GL_DRAW_BUFFER, m_framebuffer.GetId()); |  | ||||||
| 			glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_DEPTH_BUFFER_BIT, GL_NEAREST); |  | ||||||
|  |  | ||||||
|  |  | ||||||
| 			if (transparentVAO == 0) { |  | ||||||
| 				unsigned int transparentVBO; |  | ||||||
| 				float transparentVertices[] = { |  | ||||||
| 					// positions         // texture Coords (swapped y coordinates because texture is flipped upside down) |  | ||||||
| 					0.0f,  0.5f,  0.0f,  0.0f,  1.0f, |  | ||||||
| 					0.0f, -0.5f,  0.0f,  0.0f,  0.0f, |  | ||||||
| 					1.0f, -0.5f,  0.0f,  1.0f,  0.0f, |  | ||||||
|  |  | ||||||
| 					0.0f,  0.5f,  0.0f,  0.0f,  1.0f, |  | ||||||
| 					1.0f, -0.5f,  0.0f,  1.0f,  0.0f, |  | ||||||
| 					1.0f,  0.5f,  0.0f,  1.0f,  1.0f |  | ||||||
| 				}; |  | ||||||
|  |  | ||||||
| 				glGenVertexArrays(1, &transparentVAO); |  | ||||||
| 				glGenBuffers(1, &transparentVBO); |  | ||||||
| 				glBindVertexArray(transparentVAO); |  | ||||||
| 				glBindBuffer(GL_ARRAY_BUFFER, transparentVBO); |  | ||||||
| 				glBufferData(GL_ARRAY_BUFFER, sizeof(transparentVertices), transparentVertices, GL_STATIC_DRAW); |  | ||||||
| 				glEnableVertexAttribArray(0); |  | ||||||
| 				glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0); |  | ||||||
| 				glEnableVertexAttribArray(1); |  | ||||||
| 				glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float))); |  | ||||||
| 				glBindVertexArray(0); |  | ||||||
|  |  | ||||||
| 			} |  | ||||||
|  |  | ||||||
| 			glEnable(GL_BLEND); |  | ||||||
| 			glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |  | ||||||
|  |  | ||||||
| 			BlendingShader.Use(); |  | ||||||
| 			glBindVertexArray(transparentVAO); |  | ||||||
| 			glActiveTexture(GL_TEXTURE0); |  | ||||||
| 			glBindTexture(GL_TEXTURE_2D, grassTexture.GetID()); |  | ||||||
|  |  | ||||||
| 			BlendingShader.setUniformMat4("V", MainCamera.view); |  | ||||||
| 			BlendingShader.setUniformMat4("P", MainCamera.projection); |  | ||||||
| 			for (unsigned int i = 0; i < vegetation.size(); i++) { |  | ||||||
| 				 |  | ||||||
| 				auto translation = glm::translate(glm::mat4(1.0f), vegetation[i]) ; |  | ||||||
| 				BlendingShader.setUniformMat4("M", translation); |  | ||||||
| 				glDrawArrays(GL_TRIANGLES, 0, 6); |  | ||||||
|  |  | ||||||
| 			} |  | ||||||
|  |  | ||||||
| 			glBindVertexArray(0); |  | ||||||
|  |  | ||||||
| 			glDisable(GL_BLEND); |  | ||||||
|  |  | ||||||
| 		 |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
| 			glBindFramebuffer(GL_FRAMEBUFFER, 0); |  | ||||||
| 			glViewport(oldviewport[0], oldviewport[1], oldviewport[2], oldviewport[3]); |  | ||||||
| 			 |  | ||||||
|  |  | ||||||
| 		} |  | ||||||
| 			 | 			 | ||||||
| 		void setCurrentFrameBuffer(const Framebuffer& fb); | 		void setCurrentFrameBuffer(const Framebuffer& fb); | ||||||
| 		void setClearColor(const glm::vec3& ClearColor); | 		void setClearColor(const glm::vec3& ClearColor); | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #pragma once | #pragma once | ||||||
|  | #include <spdlog/spdlog.h> | ||||||
| class Layer { | class Layer { | ||||||
|  |  | ||||||
| public: | public: | ||||||
| @ -12,12 +12,17 @@ public: | |||||||
|  |  | ||||||
| 	virtual void OnUpdate(){} | 	virtual void OnUpdate(){} | ||||||
| 	virtual void OnUI(){} | 	virtual void OnUI(){} | ||||||
|  | 	 | ||||||
|  | 	virtual bool OnKey(int key , int status ) { | ||||||
|  | 		spdlog::info( "Key {0} , {1}", key, status); | ||||||
|  | 		return false; | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	 | ||||||
| 	virtual void OnStartup(){} | 	virtual void OnStartup(){} | ||||||
|  |  | ||||||
| 	virtual void OnAttach() {} | 	virtual void OnAttach() {} | ||||||
| 	virtual void OnDetach() {} | 	virtual void OnDetach() {} | ||||||
|  |  | ||||||
|  |  | ||||||
| 	virtual void OnCreate() {} | 	virtual void OnCreate() {} | ||||||
| 	virtual void OnDestroy(){} | 	virtual void OnDestroy(){} | ||||||
|  |  | ||||||
|  | |||||||
| @ -18,32 +18,38 @@ namespace YoggieEngine { | |||||||
| 		glm::mat4 LocalTransform = glm::mat4(1.0f); | 		glm::mat4 LocalTransform = glm::mat4(1.0f); | ||||||
|  |  | ||||||
| 		glm::mat4 GetTransform() const { | 		glm::mat4 GetTransform() const { | ||||||
|  | 			glm::mat4 rotation = glm::toMat4(glm::quat(Rotation)); | ||||||
|  |  | ||||||
|  | 			auto transform = glm::translate(glm::mat4(1.0f), Position)* rotation  | ||||||
|  | 				* glm::scale(glm::mat4(1.0f), Scale); | ||||||
|  |  | ||||||
| 			 | 			 | ||||||
|  |  | ||||||
| 			return glm::translate(glm::mat4(1.0f), Position) * glm::toMat4(glm::quat(Rotation)) * glm::scale(glm::mat4(1.0f), Scale); | 			return transform; | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		void Decompose(glm::mat4 transformationMatrix) { | 		void Decompose(glm::mat4& transformationMatrix) { | ||||||
| 			glm::mat4& tm = transformationMatrix; |  | ||||||
|  |  | ||||||
| 			 | 			 | ||||||
| 			auto& a = tm[0][0]; |  | ||||||
| 			auto& b = tm[1][0]; |  | ||||||
| 			auto& c = tm[2][0]; |  | ||||||
| 			auto& d = tm[3][0]; |  | ||||||
|  |  | ||||||
| 			auto& e = tm[0][1]; | 			auto& a = transformationMatrix[0][0]; | ||||||
| 			auto& f = tm[1][1]; | 			auto& b = transformationMatrix[1][0]; | ||||||
| 			auto& g = tm[2][1]; | 			auto& c = transformationMatrix[2][0]; | ||||||
| 			auto& h = tm[3][1]; | 			auto& d = transformationMatrix[3][0]; | ||||||
|  |  | ||||||
| 			auto& i = tm[0][2]; | 			auto& e = transformationMatrix[0][1]; | ||||||
| 			auto& j = tm[1][2]; | 			auto& f = transformationMatrix[1][1]; | ||||||
| 			auto& k = tm[2][2]; | 			auto& g = transformationMatrix[2][1]; | ||||||
| 			auto& l = tm[3][2]; | 			auto& h = transformationMatrix[3][1]; | ||||||
|  |  | ||||||
| 			Position = glm::vec3(d, h, l); | 			auto& i = transformationMatrix[0][2]; | ||||||
|  | 			auto& j = transformationMatrix[1][2]; | ||||||
|  | 			auto& k = transformationMatrix[2][2]; | ||||||
|  | 			auto& l = transformationMatrix[3][2]; | ||||||
|  |  | ||||||
|  | 			Position = glm::vec3(transformationMatrix[3]); | ||||||
|  | 			 | ||||||
|  | 			// Remove the position from the matrix | ||||||
| 			d = 0; | 			d = 0; | ||||||
| 			h = 0; | 			h = 0; | ||||||
| 			l = 0; | 			l = 0; | ||||||
| @ -54,6 +60,7 @@ namespace YoggieEngine { | |||||||
|  |  | ||||||
| 			Scale = glm::vec3(sx, sy, sz); | 			Scale = glm::vec3(sx, sy, sz); | ||||||
| 	 | 	 | ||||||
|  | 			// Remove the scale from the matrix; | ||||||
| 			a/= sx; | 			a/= sx; | ||||||
| 			e /= sx; | 			e /= sx; | ||||||
| 			i /= sx; | 			i /= sx; | ||||||
| @ -67,12 +74,12 @@ namespace YoggieEngine { | |||||||
| 			k /= sz; | 			k /= sz; | ||||||
| 			 | 			 | ||||||
|  |  | ||||||
| 			auto w = glm::sqrt(1 + tm[0][0] + tm[1][1] + tm[2][2]) / 2; | 			auto w = glm::sqrt(1 + transformationMatrix[0][0] + transformationMatrix[1][1] + transformationMatrix[2][2]) / 2; | ||||||
|  |  | ||||||
|  |  | ||||||
| 			auto x = (tm[2][1] - tm[1][2]) / (4 * w); | 			auto x = (transformationMatrix[2][1] - transformationMatrix[1][2]) / (4 * w); | ||||||
| 			auto y = (tm[0][2] - tm[2][0]) / (4 * w); | 			auto y = (transformationMatrix[0][2] - transformationMatrix[2][0]) / (4 * w); | ||||||
| 			auto z = (tm[1][0] - tm[0][1]) / (4 * w); | 			auto z = (transformationMatrix[1][0] - transformationMatrix[0][1]) / (4 * w); | ||||||
|  |  | ||||||
|  |  | ||||||
| 			auto rot = glm::quat(w, x, y, z); | 			auto rot = glm::quat(w, x, y, z); | ||||||
| @ -127,7 +134,6 @@ namespace YoggieEngine { | |||||||
|  |  | ||||||
|  |  | ||||||
| 	struct Render3DComponent { | 	struct Render3DComponent { | ||||||
| 		Renderable* renderable; |  | ||||||
|  |  | ||||||
| 		unsigned int VAO = 0; | 		unsigned int VAO = 0; | ||||||
| 		unsigned int IBO = 0; | 		unsigned int IBO = 0; | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user