Vivid
Loading...
Searching...
No Matches
ecs_test.cpp
1#include "Vivid.h"
2
4{
5private:
6 Vivid::Maths::Vec3 lightColor = Vivid::Maths::Vec3(1.0f, 0.5f, 1.0f);
7 Vivid::Maths::Vec3 lightPos = Vivid::Maths::Vec3(0.0f, 0.0f, -100.0f);
8
9 glm::vec3 suzannePosition = glm::vec3(0, 50, -200);
10 glm::vec3 lightPosition = glm::vec3(0, 0, 0);
11
12 Vivid::Mesh lightMesh;
13 Ref<Vivid::Shader> lightShader;
14 Ref<Vivid::Entity> suzanne = Vivid::ECS::CreateEntity("Suzanne");
15 Ref<Vivid::Entity> light = Vivid::ECS::CreateEntity("DirectionalLight");
16 Ref<Vivid::ModelComponent> modelComponent1;
17 Ref<Vivid::TransformComponent> sphereTransformComponent = Vivid::ECS::CreateComponent<Vivid::TransformComponent>();
18 Vivid::Mesh* mesh;
19
20 Ref<Vivid::DirectionalLightComponent> directionalLightComponent;
21 Ref<Vivid::Shader> shader;
22
23 float shininess = 32.0f;
24
25public:
26 void Setup() override
27 {
28 // Can write custom opengl confs here
29 OPENGL_CONFS
30
31 shader = MakeRef<Vivid::Shader>("./../assets/shaders/phong.vertexShader.glsl",
32 "./../assets/shaders/phong.pixelShader.glsl");
33
34 mesh = new Vivid::Mesh("./../assets/obj/suzanne.obj", 1);
35 mesh->BindShader(shader);
36
37 modelComponent1 = Vivid::ECS::CreateComponent<Vivid::ModelComponent>();
38 modelComponent1->AddMesh(mesh);
39
40 directionalLightComponent = Vivid::ECS::CreateComponent<Vivid::DirectionalLightComponent>();
41 directionalLightComponent->SetDirection(Vivid::Maths::Vec3(0.0f, -1.0f, 0.0f));
42 //
43
44 sphereTransformComponent->SetScale(Vivid::Maths::Vec3(50.0f, 50.0f, 50.0f));
45 Vivid::ECS::AddComponent(modelComponent1->GetComponentID(), suzanne->GetEntityID());
46 Vivid::ECS::AddComponent(sphereTransformComponent->GetComponentID(), suzanne->GetEntityID());
47
48 Vivid::ECS::AddComponent(directionalLightComponent->GetComponentID(), light->GetEntityID());
49 }
50
51 void Draw() override
52 {
53 Vector<Vivid::DirectionalLightComponent*> directionalLights;
54 Vivid::ECS::GetAllComponents(Vivid::ComponentType::DirectionalLightComponent, directionalLights);
55 float intensity = directionalLights[0]->GetIntensity();
56 Vivid::Maths::Vec3 lightDir = directionalLights[0]->GetDirection();
57
58 mesh->BindShader(shader);
59 shader->Bind();
60 shader->SetUniform3f("LightDir", lightDir);
61 shader->SetUniform1f("LightIntensity", intensity);
62 shader->SetUniform3f("LightColor", lightColor);
63 shader->SetUniform1f("shininess", shininess);
64 }
65
66 void ImGuiRender() override
67 {
68 ImGui::Begin("Debug");
69
70 ImGui::Text("Application average %.3f ms/frame (%.1f FPS)",
71 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
72
73 ImGui::SliderFloat("shininess", &shininess, 0.0f, 32.0f);
74
75 ImGui::End();
76 }
77};
78
79Application* Vivid::CreateApplication()
80{
81 Application* app = Application::GetInstance(1920, 1080, "Vivid: ECS TEST Example");
82
83 app->SetRenderingInterface(new ExampleInterface);
84
85 return app;
86}
87
88int main()
89{
90 return Vivid::main(0, nullptr);
91}
Application class.
Definition: Application.h:19
void Setup() override
Setup function.
Definition: ecs_test.cpp:26
void ImGuiRender() override
ImGuiRender function.
Definition: ecs_test.cpp:66
void Draw() override
Draw function.
Definition: ecs_test.cpp:51
RenderingInterface class.
Contains a Mesh.
Definition: Mesh.h:32
Contains a 3D vector.
Definition: Vec.h:51