Moved rendering code outside of the sandbox update function, renderer instance is now rendering the cube ( although colour is still missing but should be something simple)

This commit is contained in:
Nigel Barink 2022-10-08 20:40:06 +02:00
parent b03b82272f
commit cbbdafcb3e
7 changed files with 51 additions and 50 deletions

View File

@ -16,6 +16,8 @@
#include "Scene.h" #include "Scene.h"
#include "PerfCounter.h" #include "PerfCounter.h"
extern BarinkEngine::Renderer renderer;
extern void Start(); extern void Start();
extern void Update(); extern void Update();
extern void ImmediateGraphicsDraw(); extern void ImmediateGraphicsDraw();

View File

@ -3,6 +3,7 @@
#include "Buffer.h" #include "Buffer.h"
#include "Mesh.h" #include "Mesh.h"
#include "Material.h" #include "Material.h"
#include <glm/glm.hpp>
namespace BarinkEngine { namespace BarinkEngine {
@ -18,6 +19,7 @@ namespace BarinkEngine {
GpuBuffer elementBuffer; GpuBuffer elementBuffer;
VertexArray vertexarray; VertexArray vertexarray;
const Mesh* mesh; const Mesh* mesh;
const Material* material; const Material* material;

View File

@ -1,7 +1,7 @@
#include "BarinkEngine.h" #include "BarinkEngine.h"
EngineStatistics* ES; EngineStatistics* ES;
BarinkEngine::Renderer renderer;
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
// Setup performance sampler // Setup performance sampler
PerfomanceSamplerInit(); PerfomanceSamplerInit();
@ -10,7 +10,7 @@ int main(int argc, char* argv[]) {
// Startup services // Startup services
BarinkWindow MainWindow = BarinkWindow(800, 600); BarinkWindow MainWindow = BarinkWindow(800, 600);
BarinkEngine::Renderer renderer = BarinkEngine::Renderer(); renderer = BarinkEngine::Renderer();
InputSystem = BarinkEngine::InputManager(); InputSystem = BarinkEngine::InputManager();
InputSystem.attach(&MainWindow); InputSystem.attach(&MainWindow);
@ -44,9 +44,9 @@ int main(int argc, char* argv[]) {
GUISystem.Render(); GUISystem.Render();
MainWindow.SwapBuffers(); MainWindow.SwapBuffers();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
} }

View File

@ -19,15 +19,14 @@ void BarinkEngine::GPU_Bucket::Upload(const Mesh& renderable) {
vertexBuffer.Bind(false); vertexBuffer.Bind(false);
vertexBuffer.setBufferData((void*)&renderable.vertices[0], renderable.vertices.size() * sizeof(BarinkEngine::Vertex), false); vertexBuffer.setBufferData((void*)&renderable.vertices[0], renderable.vertices.size() * sizeof(BarinkEngine::Vertex), false);
if (renderable.elements.empty() == false) {
elementBuffer.createBuffer(); elementBuffer.createBuffer();
elementBuffer.Bind(true); elementBuffer.Bind(true);
elementBuffer.setBufferData((void*)&renderable.elements[0], renderable.elements.size() * sizeof(unsigned int), true); elementBuffer.setBufferData((void*)&renderable.elements[0], renderable.elements.size() * sizeof(unsigned int), true);
}
vertexarray.AttachAttribute(0, 3, sizeof(Vertex)); vertexarray.AttachAttribute(0, 3, sizeof(Vertex));
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(BarinkEngine::Vertex), (void*)offsetof(BarinkEngine::Vertex, vertices)); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(BarinkEngine::Vertex), (void*)0);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1); glEnableVertexAttribArray(1);
vertexBuffer.Unbind(false); vertexBuffer.Unbind(false);

View File

@ -14,10 +14,9 @@ BarinkEngine::SceneObject* BarinkEngine::ModelImporter::Import(const std::string
std::cout << "[DEBUG]: Loaded "<< meshes.size() << " meshes!" << std::endl; std::cout << "[DEBUG]: Loaded "<< meshes.size() << " meshes!" << std::endl;
// create a renderable (per mesh ?? ) // create a renderable (per mesh ?? )
root->renderable = new Renderable(); root->renderable = new Renderable();
root->renderable->mesh = &(meshes[0]); root->renderable->mesh = new Mesh(meshes[0]);
return root; return root;

View File

@ -1,12 +1,8 @@
#include "Graphics/Renderer.h" #include "Graphics/Renderer.h"
BarinkEngine::Renderer::Renderer() BarinkEngine::Renderer::Renderer()
{ {
models = std::vector<GPU_Bucket*>(); models = std::vector<GPU_Bucket*>();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
} }
BarinkEngine::Renderer::~Renderer() BarinkEngine::Renderer::~Renderer()
@ -25,7 +21,7 @@ BarinkEngine::Renderer::~Renderer()
void BarinkEngine::Renderer::Render() void BarinkEngine::Renderer::Render()
{ {
// This creation of the projection and camera is somewhat wastefull // This creation of the projection and camera is somewhat wastefull
Camera cam = Camera(glm::vec3(0.0f, 1.5f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), 90.0f); Camera cam = Camera(glm::vec3(8.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), 90.0f);
glm::mat4 projection = glm::perspective(glm::radians(cam.Zoom), (800.0f / 600.0f), 0.001f, 100.0f); glm::mat4 projection = glm::perspective(glm::radians(cam.Zoom), (800.0f / 600.0f), 0.001f, 100.0f);
@ -40,7 +36,14 @@ void BarinkEngine::Renderer::Render()
std::cout << "No material attached!" << std::endl; std::cout << "No material attached!" << std::endl;
} }
else { else {
model->material->shader.Use();
model->material->Apply(); model->material->Apply();
model->material->shader.setUniformVec3("Color", model->material->Color);
model->material->shader.setUniformMat4("M", glm::mat4());
model->material->shader.setUniformMat4("V", cam.GetViewMatrix());
model->material->shader.setUniformMat4("P", projection);
} }
// Update perf counters // Update perf counters
@ -54,9 +57,10 @@ void BarinkEngine::Renderer::Render()
); );
model->vertexarray.Unbind(); model->vertexarray.Unbind();
}
}
} }
@ -72,6 +76,6 @@ void BarinkEngine::Renderer::Submit(Renderable* model)
bucket->Upload(*model->mesh); bucket->Upload(*model->mesh);
models.push_back(bucket); // Maybe push a GPU packet or something instead models.push_back(bucket);
} }

View File

@ -20,7 +20,6 @@ const std::string fragmentShaderSource = "../build/SandboxApplication/Debug/test
BarinkEngine::ModelImporter* MI = new BarinkEngine::ModelImporter(); BarinkEngine::ModelImporter* MI = new BarinkEngine::ModelImporter();
Scene* Level1; Scene* Level1;
BarinkEngine::Renderer* renderer;
BarinkEngine::SceneObject* cube; BarinkEngine::SceneObject* cube;
/* /*
* Runs once at startup * Runs once at startup
@ -51,9 +50,8 @@ void Start() {
memset(code, '\0', 254); memset(code, '\0', 254);
// TODO: Move to runtime/ Engine // TODO: Move to runtime/ Engine
renderer = new BarinkEngine::Renderer();
// NOTE: Submits should later be done through walking the sceneTree // NOTE: Submits should later be done through walking the sceneTree
renderer->Submit(cube->renderable); renderer.Submit(cube->renderable);
} }
@ -67,16 +65,16 @@ void ImmediateGraphicsDraw() {
// Show ImGui demo such that I can easily look // Show ImGui demo such that I can easily look
// at possible GUI elements to use // at possible GUI elements to use
ImGui::ShowDemoWindow(); // ImGui::ShowDemoWindow();
// Show internal BarinkEngine stats // Show internal BarinkEngine stats
ShowStats(); ShowStats();
// Show different tooling for this specific sandbox // Show different tooling for this specific sandbox
// CameraTool(cam); // CameraTool(cam);
ScriptingTool(code); //ScriptingTool(code);
SceneExplorer(*Level1, "Scene Explorer"); //SceneExplorer(*Level1, "Scene Explorer");
} }
@ -87,8 +85,6 @@ void ImmediateGraphicsDraw() {
void Update() void Update()
{ {
renderer->Render();
} }
/* /*
@ -97,6 +93,5 @@ void Update()
*/ */
void Stop() { void Stop() {
delete MI; delete MI;
delete renderer;
delete shader; delete shader;
} }