YoggieEngine/SandboxApplication/Sandbox.cpp

159 lines
3.5 KiB
C++
Raw Normal View History

#include "BarinkEngine.h"
#include "imgui.h"
using namespace BarinkEngine;
Camera* cam;
Renderable* Cube;
Renderable* Cube2;
char* code = new char[254];
void PrintSceneTree(SceneNode& node, int depth ){
// Indent name based on depth
std::cout << " ";
for (int i = 0; i < depth; i++) {
std::cout << "-";
}
std::cout << " " << node.name << std::endl;
2022-05-04 12:39:27 +00:00
depth++;
for (auto child : node.children) {
PrintSceneTree(*child, depth);
}
}
void Start() {
SceneNode MyCube = SceneNode();
MyCube.name = "MyCube";
SceneNode MyBaby = SceneNode();
MyBaby.name = "Baby";
SceneNode MySecondCube = SceneNode();
MySecondCube.name = "MySecondCube";
MyCube.addChild(MyBaby);
Scene scene = Scene("My awesome Game Scene");
scene.GetRoot().addChild(MyCube);
scene.GetRoot().addChild(MySecondCube);
// Walk scene graph
PrintSceneTree(scene.GetRoot(),0);
Cube = Renderable::Load();
Cube2 = Renderable::Load();
Cube->addChild(*Cube2);
cam = new Camera(glm::vec3(0.0f, 1.5f, -10.0f), glm::vec3(0.0f, 0.0f, 0.0f), 90.0f);
memset(code, '\0', 254);
}
void transformWindow(Transform& transform, std::string PanelName) {
ImGui::Begin(PanelName.c_str());
ImGui::InputFloat3("Position:", (float*)&transform.Position[0]);
ImGui::InputFloat3("Rotation:", (float*)&transform.Rotation[0]);
ImGui::InputFloat3("Scale:", (float*)&transform.Scale[0]);
ImGui::End();
}
void ImmediateGraphicsDraw() {
ImGui::NewFrame();
ImGui::Begin("Camera");
ImGui::SliderFloat("Zoom:", &cam->Zoom, 10, 190);
ImGui::End();
transformWindow(Cube->transform, "Transform (Cube)");
transformWindow(Cube2->transform, "Transform (Cube2)");
ImGui::Begin("Scripting!!");
ImGui::InputTextMultiline("Lua Script", code, 255);
bool runCode = ImGui::Button("Run");
ImGui::End();
ImGui::ShowDemoWindow();
}
glm::mat4 CalculateModelMat(Transform& transform) {
glm::mat4 tran = glm::translate(glm::mat4(), transform.Position);
glm::mat4 scale = glm::scale(glm::mat4(), transform.Scale);
glm::mat4 rot =
glm::rotate(glm::mat4(), glm::radians(transform.Rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)) *
glm::rotate(glm::mat4(), glm::radians(transform.Rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)) *
glm::rotate(glm::mat4(), glm::radians(transform.Rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
return tran * rot * scale;
}
2022-05-04 12:39:27 +00:00
void Update()
{
std::string vertexShaderSource = "build/SandboxApplication/Debug/test.vs";
std::string fragmentShaderSource = "build/SandboxApplication/Debug/test.fs";
Shader shader = Shader(vertexShaderSource, fragmentShaderSource);
2022-05-04 12:39:27 +00:00
glm::mat4 projection = glm::perspective(glm::radians(cam->Zoom), (800.0f / 600.0f), 0.001f, 100.0f);
2022-05-04 12:39:27 +00:00
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
shader.Use();
shader.setUniformMat4("P", projection);
shader.setUniformMat4("M", CalculateModelMat(Cube->transform));
shader.setUniformMat4("V", cam->GetViewMatrix());
shader.setUniformVec3("MatColour", glm::vec3(1.0f, 0.0f, 0.0f));
2022-05-04 12:39:27 +00:00
Cube->Draw();
shader.setUniformMat4("M", CalculateModelMat(Cube2->transform));
shader.setUniformVec3("MatColour", glm::vec3(0.0f, 1.0f, 0.0f));
Cube2->Draw();
}
2022-04-30 18:20:07 +00:00
void Stop() {
2022-05-04 12:39:27 +00:00
// Cleanup
Cube->VAO.Delete();
Cube->elementBuffer.Delete();
Cube2->VAO.Delete();
Cube2->elementBuffer.Delete();
delete Cube2;
delete Cube;
}