Browse Source

Add simple renderer class, add example implementation

master
Riyyi 5 years ago
parent
commit
81751442bb
  1. 60
      inferno/src/inferno/application.cpp
  2. 37
      inferno/src/inferno/application.h
  3. 41
      inferno/src/inferno/render/renderer.cpp
  4. 32
      inferno/src/inferno/render/renderer.h

60
inferno/src/inferno/application.cpp

@ -1,12 +1,16 @@
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "inferno/application.h"
#include "inferno/core.h"
#include "inferno/event/applicationevent.h"
#include "inferno/event/event.h"
#include "inferno/input.h"
#include "inferno/log.h"
#include "inferno/settings.h"
#include "inferno/render/buffer.h"
#include "inferno/render/context.h"
#include "inferno/render/renderer.h"
#include "inferno/render/shader.h"
#include "inferno/window.h"
namespace Inferno {
@ -17,6 +21,39 @@ namespace Inferno {
{
NF_CORE_ASSERT(!s_instance, "Application already exists!");
s_instance = this;
// Initialize Settings
new Settings();
m_window = std::make_unique<Window>();
m_window->setEventCallback(NF_BIND_EVENT(Application::onEvent));
float vertices[] = {
-0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f,
0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f,
};
uint32_t indices[] = {
0, 1, 2, 2, 3, 0
};
m_vertexArray = std::make_shared<VertexArray>();
std::shared_ptr<VertexBuffer> vertexBuffer = std::make_shared<VertexBuffer>(vertices, sizeof(vertices));
vertexBuffer->setLayout({
{ BufferElementType::Vec3, "a_position" },
{ BufferElementType::Vec4, "a_color" },
});
m_vertexArray->addVertexBuffer(vertexBuffer);
std::shared_ptr<IndexBuffer> indexBuffer = std::make_shared<IndexBuffer>(indices, sizeof(indices));
m_vertexArray->setIndexBuffer(indexBuffer);
m_shader = std::make_unique<Shader>("assets/glsl/simple.vert", "assets/glsl/simple.frag");
}
Application::~Application()
@ -27,20 +64,19 @@ namespace Inferno {
{
NF_CORE_LOG("Application startup");
// Initialize Settings
new Settings();
m_window = std::make_unique<Window>();
m_window->setEventCallback(NF_BIND_EVENT(Application::onEvent));
while(!glfwWindowShouldClose(m_window->getWindow())) {
if(glfwGetKey(m_window->getWindow(), GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(m_window->getWindow(), GL_TRUE);
}
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Command::clearColor({ 0.2f, 0.3f, 0.3f, 1.0f });
Command::clear();
Renderer::beginScene(); // camera, lights, environment
m_shader->bind();
Renderer::submit(m_vertexArray);
Renderer::endScene();
m_window->update();
}
@ -57,7 +93,9 @@ namespace Inferno {
bool Application::onWindowClose(WindowCloseEvent &e)
{
// Suppress unused warning
(void)e;
NF_CORE_INFO("WindowCloseEvent triggered");
glfwSetWindowShouldClose(m_window->getWindow(), GL_TRUE);
@ -67,9 +105,13 @@ namespace Inferno {
bool Application::onWindowResize(WindowResizeEvent &e)
{
// Suppress unused warning
(void)e;
NF_CORE_INFO("WindowResizeEvent %dx%d triggered", e.getWidth(), e.getHeight());
m_window->getContext()->setViewport(0, 0, e.getWidth(), e.getHeight());
return true;
}

37
inferno/src/inferno/application.h

@ -10,6 +10,9 @@ namespace Inferno {
class WindowResizeEvent;
class Window;
class VertexArray;
class Shader;
class Application {
public:
Application();
@ -23,11 +26,18 @@ namespace Inferno {
// -----------------------------------------
inline Window &getWindow() { return *m_window; }
static inline Application &get() { return *s_instance; }
private:
std::unique_ptr<Window> m_window;
//
std::shared_ptr<VertexArray> m_vertexArray;
std::unique_ptr<Shader> m_shader;
//
static Application* s_instance;
};
@ -37,3 +47,30 @@ namespace Inferno {
}
#endif // APPLICATION_H
// Gameplan
// v Entrypoint
// v Logging
// v Events
// v Window
// v Settings loader (json)
// v Input polling
// v OpenGL context
// - GPUDriver (?)
// v Renderer
// v Buffers
// ~ Shader
// - Schene (camera, lights, environment)
// - Texture loading
// - Model loading
// - Entity Component System
// - Serialization
// - Level format
// - Tools (Tiled?)
// - Scripting (Lua)
// - Global object access can be done in 3 ways:
// - Singleton, static, extern
// @Todo
// - Settings should contain all file paths (ex: shaders)

41
inferno/src/inferno/render/renderer.cpp

@ -0,0 +1,41 @@
#include <glad/glad.h>
#include "inferno/render/buffer.h"
#include "inferno/render/renderer.h"
namespace Inferno {
void Command::clear()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void Command::clearColor(const glm::vec4 &color)
{
glClearColor(color.r, color.g, color.b, color.a);
}
void Command::drawIndexed(const std::shared_ptr<VertexArray> &vertexArray)
{
glDrawElements(GL_TRIANGLES, vertexArray->getIndexBuffer()->getCount(), GL_UNSIGNED_INT, nullptr);
}
// -----------------------------------------
void Renderer::beginScene()
{
}
void Renderer::endScene()
{
}
void Renderer::submit(const std::shared_ptr<VertexArray> &vertexArray)
{
vertexArray->bind();
Command::drawIndexed(vertexArray);
}
}

32
inferno/src/inferno/render/renderer.h

@ -0,0 +1,32 @@
#ifndef RENDERER_H
#define RENDERER_H
#include <memory> // std::shared_ptr
#include <glm/vec4.hpp>
namespace Inferno {
class VertexArray;
class Command {
public:
static void clear();
static void clearColor(const glm::vec4 &color);
static void drawIndexed(const std::shared_ptr<VertexArray> &vertexArray);
};
// -----------------------------------------
class Renderer {
public:
static void beginScene();
static void endScene();
static void submit(const std::shared_ptr<VertexArray> &vertexArray);
};
}
#endif // RENDERER_H
Loading…
Cancel
Save