nigel
8cc6ed1f15
* Rendering 2 cubes * per cube transform panels * Updated TODO.md * Updated README.md
159 lines
3.5 KiB
C++
159 lines
3.5 KiB
C++
#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;
|
|
|
|
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;
|
|
}
|
|
|
|
|
|
void Update()
|
|
{
|
|
|
|
std::string vertexShaderSource = "build/SandboxApplication/Debug/test.vs";
|
|
std::string fragmentShaderSource = "build/SandboxApplication/Debug/test.fs";
|
|
Shader shader = Shader(vertexShaderSource, fragmentShaderSource);
|
|
|
|
|
|
|
|
|
|
glm::mat4 projection = glm::perspective(glm::radians(cam->Zoom), (800.0f / 600.0f), 0.001f, 100.0f);
|
|
|
|
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));
|
|
|
|
Cube->Draw();
|
|
|
|
shader.setUniformMat4("M", CalculateModelMat(Cube2->transform));
|
|
shader.setUniformVec3("MatColour", glm::vec3(0.0f, 1.0f, 0.0f));
|
|
|
|
Cube2->Draw();
|
|
|
|
}
|
|
|
|
|
|
void Stop() {
|
|
// Cleanup
|
|
Cube->VAO.Delete();
|
|
Cube->elementBuffer.Delete();
|
|
|
|
Cube2->VAO.Delete();
|
|
Cube2->elementBuffer.Delete();
|
|
|
|
delete Cube2;
|
|
delete Cube;
|
|
} |