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 "PerfCounter.h"
extern BarinkEngine::Renderer renderer;
extern void Start();
extern void Update();
extern void ImmediateGraphicsDraw();

View File

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

View File

@ -1,7 +1,7 @@
#include "BarinkEngine.h"
EngineStatistics* ES;
BarinkEngine::Renderer renderer;
int main(int argc, char* argv[]) {
// Setup performance sampler
PerfomanceSamplerInit();
@ -10,7 +10,7 @@ int main(int argc, char* argv[]) {
// Startup services
BarinkWindow MainWindow = BarinkWindow(800, 600);
BarinkEngine::Renderer renderer = BarinkEngine::Renderer();
renderer = BarinkEngine::Renderer();
InputSystem = BarinkEngine::InputManager();
InputSystem.attach(&MainWindow);
@ -44,9 +44,9 @@ int main(int argc, char* argv[]) {
GUISystem.Render();
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.setBufferData((void*)&renderable.vertices[0], renderable.vertices.size() * sizeof(BarinkEngine::Vertex), false);
if (renderable.elements.empty() == false) {
elementBuffer.createBuffer();
elementBuffer.Bind(true);
elementBuffer.setBufferData((void*)&renderable.elements[0], renderable.elements.size() * sizeof(unsigned int), true);
}
elementBuffer.createBuffer();
elementBuffer.Bind(true);
elementBuffer.setBufferData((void*)&renderable.elements[0], renderable.elements.size() * sizeof(unsigned int), true);
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);
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;
// create a renderable (per mesh ?? )
root->renderable = new Renderable();
root->renderable->mesh = &(meshes[0]);
root->renderable->mesh = new Mesh(meshes[0]);
return root;

View File

@ -1,12 +1,8 @@
#include "Graphics/Renderer.h"
BarinkEngine::Renderer::Renderer()
{
models = std::vector<GPU_Bucket*>();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
}
BarinkEngine::Renderer::~Renderer()
@ -25,38 +21,46 @@ BarinkEngine::Renderer::~Renderer()
void BarinkEngine::Renderer::Render()
{
// 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);
for (auto model : models) {
// Push matrices etc ....
model->vertexarray.Bind();
model->elementBuffer.Bind(true);
// Push matrices etc ....
model->vertexarray.Bind();
model->elementBuffer.Bind(true);
if (model->material == nullptr) {
std::cout << "No material attached!" << std::endl;
}
else {
model->material->Apply();
}
// Update perf counters
ES->verts = model->mesh->vertices.size();
ES->DC++;
glDrawElements( GL_TRIANGLES,
static_cast<unsigned int>(model->mesh->elements.size()),
GL_UNSIGNED_INT,
NULL
);
model->vertexarray.Unbind();
if (model->material == nullptr) {
std::cout << "No material attached!" << std::endl;
}
else {
model->material->shader.Use();
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
ES->verts = model->mesh->vertices.size();
ES->DC++;
glDrawElements(GL_TRIANGLES,
static_cast<unsigned int>(model->mesh->elements.size()),
GL_UNSIGNED_INT,
NULL
);
model->vertexarray.Unbind();
}
}
@ -72,6 +76,6 @@ void BarinkEngine::Renderer::Submit(Renderable* model)
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();
Scene* Level1;
BarinkEngine::Renderer* renderer;
BarinkEngine::SceneObject* cube;
/*
* Runs once at startup
@ -51,9 +50,8 @@ void Start() {
memset(code, '\0', 254);
// TODO: Move to runtime/ Engine
renderer = new BarinkEngine::Renderer();
// 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
// at possible GUI elements to use
ImGui::ShowDemoWindow();
// ImGui::ShowDemoWindow();
// Show internal BarinkEngine stats
ShowStats();
// Show different tooling for this specific sandbox
// CameraTool(cam);
ScriptingTool(code);
//ScriptingTool(code);
SceneExplorer(*Level1, "Scene Explorer");
//SceneExplorer(*Level1, "Scene Explorer");
}
@ -86,8 +84,6 @@ void ImmediateGraphicsDraw() {
*/
void Update()
{
renderer->Render();
}
@ -97,6 +93,5 @@ void Update()
*/
void Stop() {
delete MI;
delete renderer;
delete shader;
}