Adding a really basic ambient light component

This commit is contained in:
Nigel Barink 2022-10-23 17:33:49 +02:00
parent adf2331ab1
commit f0984b6117
7 changed files with 67 additions and 19 deletions

View File

@ -35,10 +35,8 @@ void GUIManager::Render()
ImGui::NewFrame(); ImGui::NewFrame();
ImGui::Begin("##App");
ImmediateGraphicsDraw(); ImmediateGraphicsDraw();
ImGui::End();
ImGui::Render(); ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

View File

@ -48,16 +48,20 @@ void BarinkEngine::Renderer::Prepare(Scene& scene ) {
void BarinkEngine::Renderer::Render(Scene& scene) void BarinkEngine::Renderer::Render(Scene& scene)
{ {
auto group = scene.getReg().view<TransformComponent, Render3DComponent>(); auto group = scene.getReg().view<TransformComponent, Render3DComponent>();
group.each([](auto entity , TransformComponent& trans, Render3DComponent& renderComponent) group.each([&](auto entity , TransformComponent& trans, Render3DComponent& renderComponent)
{ {
glBindVertexArray(renderComponent.VAO); glBindVertexArray(renderComponent.VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, renderComponent.IBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, renderComponent.IBO);
renderComponent.shader.Use(); renderComponent.shader.Use();
renderComponent.shader.setUniformVec3("Color", renderComponent.color); auto lights = scene.getReg().view<LightComponent>();
lights.each([&](auto entity, LightComponent& light) {
renderComponent.shader.setUniformVec3("lighting.color", light.Color);
renderComponent.shader.setUniformFloat("lighting.strength", light.Strength);
});
renderComponent.shader.setUniformVec3("Color", renderComponent.color);
renderComponent.shader.setUniformMat4("M", trans.transform); renderComponent.shader.setUniformMat4("M", trans.transform);
renderComponent.shader.setUniformMat4("V", cam.GetViewMatrix()); renderComponent.shader.setUniformMat4("V", cam.GetViewMatrix());
renderComponent.shader.setUniformMat4("P", projection); renderComponent.shader.setUniformMat4("P", projection);

View File

@ -39,7 +39,7 @@ inline void SamplePerformance(void) {
inline void ShowStats() { inline void ShowStats() {
ImGui::Begin("Statistics", false, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove); ImGui::Begin("Statistics", false, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize );
ImGui::Text("FPS: %i", ES.FPS); ImGui::Text("FPS: %i", ES.FPS);
ImGui::Text("Frame Time: %f", ES.frameTime); ImGui::Text("Frame Time: %f", ES.frameTime);

View File

@ -12,6 +12,12 @@ namespace BarinkEngine {
}; };
struct LightComponent {
float Strength = 1.0f;
glm::vec3 Color = glm::vec3(1.0f, 1.0f, 1.0f);
};
struct CameraComponent { struct CameraComponent {
glm::mat4 view; glm::mat4 view;
@ -22,7 +28,6 @@ namespace BarinkEngine {
unsigned int VAO = 0; unsigned int VAO = 0;
unsigned int IBO = 0; unsigned int IBO = 0;
Mesh mesh; Mesh mesh;
// TODO: becomes a material // TODO: becomes a material
glm::vec3 color; glm::vec3 color;
Shader shader; Shader shader;

View File

@ -8,11 +8,12 @@ Scene::Scene()
Scene::~Scene() Scene::~Scene()
{} {}
Entity Scene::AddEntity(std::string& name) Entity Scene::AddEntity(std::string name)
{ {
Entity entity = { m_registry.create(), this }; Entity entity = { m_registry.create(), this };
entity.AddComponent<BarinkEngine::IdentifierComponent>(); auto& ident = entity.AddComponent<BarinkEngine::IdentifierComponent>();
ident.name = name;
entity.AddComponent<BarinkEngine::TransformComponent>(); entity.AddComponent<BarinkEngine::TransformComponent>();

View File

@ -9,7 +9,7 @@ public:
Scene(); Scene();
~Scene(); ~Scene();
Entity AddEntity(std::string& name); Entity AddEntity(std::string name);
entt::registry& getReg() { return m_registry; } entt::registry& getReg() { return m_registry; }

View File

@ -21,28 +21,36 @@ Entity cube;
* - USe to initialize the game/sandbox/demo * - USe to initialize the game/sandbox/demo
*/ */
void Start() { void Start() {
cube = scene.AddEntity((std::string&)"cube");
auto& render3DComponent = cube.AddComponent<BarinkEngine::Render3DComponent>();
auto importer = BarinkEngine::ModelImporter(); auto importer = BarinkEngine::ModelImporter();
// Load in asset(S)
object = importer.Import("build/Debug/Models/Cube.obj"); object = importer.Import("build/Debug/Models/Cube.obj");
renderable = object->renderable; renderable = object->renderable;
// Add Entities to the scene
cube = scene.AddEntity("cube");
auto& render3DComponent = cube.AddComponent<BarinkEngine::Render3DComponent>();
render3DComponent.mesh = *renderable->mesh; render3DComponent.mesh = *renderable->mesh;
cube.GetComponent<BarinkEngine::TransformComponent>() cube.GetComponent<BarinkEngine::TransformComponent>()
.transform = glm::rotate(glm::mat4(1.0f), 32.0f, glm::vec3(0.5f,1.0f,0.0f)); .transform = glm::rotate(glm::mat4(1.0f), 32.0f, glm::vec3(0.5f,1.0f,0.0f));
// Create a second cube // Create a second cube
auto cube2 = scene.AddEntity((std::string&)"Cube2"); auto cube2 = scene.AddEntity("Cube2");
auto& cube2Render = cube2.AddComponent<BarinkEngine::Render3DComponent>(); auto& cube2Render = cube2.AddComponent<BarinkEngine::Render3DComponent>();
cube2Render.mesh = *renderable->mesh; cube2Render.mesh = *renderable->mesh;
cube2Render.color = glm::vec3(0.0f, 1.0f, 0.0f); cube2Render.color = glm::vec3(0.0f, 1.0f, 0.0f);
auto& cube2Trans = cube2.GetComponent<BarinkEngine::TransformComponent>(); auto& cube2Trans = cube2.GetComponent<BarinkEngine::TransformComponent>();
cube2Trans.transform = glm::translate( glm::mat4(1.0f), glm::vec3(1.0f,0.0f, 5.0f)); cube2Trans.transform = glm::translate( glm::mat4(1.0f), glm::vec3(1.0f,0.0f, 5.0f));
// Create a light
auto AmbientLight = scene.AddEntity("AmbientLight");
AmbientLight.AddComponent<BarinkEngine::LightComponent>();
renderer.Prepare(scene); renderer.Prepare(scene);
@ -53,6 +61,7 @@ void Start() {
bool showImGuiMetrics = false;
/* /*
* Runs every frame * Runs every frame
* - Use to draw Immediate mode graphics (Not meant for HUD's ) * - Use to draw Immediate mode graphics (Not meant for HUD's )
@ -62,16 +71,47 @@ void ImmediateGraphicsDraw()
// Show internal BarinkEngine stats // Show internal BarinkEngine stats
ShowStats(); ShowStats();
ImGui::Begin("Render edit"); ImGui::Begin("Scene view");
auto group = scene.getReg().view<BarinkEngine::IdentifierComponent>();
group.each([](auto entity, BarinkEngine::IdentifierComponent& identifier) {
auto& a = cube.GetComponent<BarinkEngine::Render3DComponent>(); ImGui::Text("%s", identifier.name.c_str());
});
ImGui::End();
ImGui::DragFloat3("Color", &a.color[0], 0.01f, 0.0f, 1.0f);
ImGui::Begin("Settings");
if (ImGui::Button("ImGui Debug"))
{
std::cout << "Click!" << std::endl;
showImGuiMetrics = true;
}
ImGui::ShowMetricsWindow(&showImGuiMetrics);
auto& a = cube.GetComponent<BarinkEngine::Render3DComponent>();
auto& b = cube.GetComponent<BarinkEngine::TransformComponent>();
ImGui::DragFloat3("Color", &a.color[0], 0.01f, 0.0f, 1.0f);
ImGui::DragFloat3("Position", &b.transform[3][0], 0.01f, 0.0f, 16.0f);
auto l = scene.getReg().view<BarinkEngine::LightComponent>();
l.each([](auto entity, BarinkEngine::LightComponent& light) {
ImGui::Text("Lighting");
ImGui::SliderFloat("Intensity", &light.Strength, 0.0f, 1.0f);
ImGui::SliderFloat3("l-Color", &light.Color[0], 0.0f, 1.0f);
});
ImGui::End(); ImGui::End();
} }