Browse Source

Improve API regarding server-side GL capabilities, fix font transparency

master
Riyyi 4 years ago
parent
commit
3ce136741d
  1. 5
      inferno/src/inferno/application.cpp
  2. 44
      inferno/src/inferno/render/context.cpp
  3. 10
      inferno/src/inferno/render/context.h
  4. 36
      inferno/src/inferno/render/renderer.cpp
  5. 10
      inferno/src/inferno/render/renderer.h
  6. 52
      inferno/src/inferno/window.cpp
  7. 8
      inferno/src/inferno/window.h

5
inferno/src/inferno/application.cpp

@ -46,6 +46,8 @@ namespace Inferno {
m_scene = std::make_shared<Scene>(); m_scene = std::make_shared<Scene>();
m_scene->initialize(); m_scene->initialize();
RenderCommand::initialize();
Renderer2D* renderer2D = new Renderer2D(); Renderer2D* renderer2D = new Renderer2D();
renderer2D->initialize(); renderer2D->initialize();
@ -65,6 +67,7 @@ namespace Inferno {
FontManager::the().destroy(); FontManager::the().destroy();
RendererCharacter::the().destroy(); RendererCharacter::the().destroy();
Renderer2D::the().destroy(); Renderer2D::the().destroy();
RenderCommand::destroy();
m_scene->destroy(); m_scene->destroy();
TextureManager::the().destroy(); TextureManager::the().destroy();
ShaderManager::the().destroy(); ShaderManager::the().destroy();
@ -186,7 +189,7 @@ namespace Inferno {
infoln("WindowResizeEvent {}x{} triggered", e.getWidth(), e.getHeight()); infoln("WindowResizeEvent {}x{} triggered", e.getWidth(), e.getHeight());
m_window->getContext()->setViewport(0, 0, e.getWidth(), e.getHeight()); RenderCommand::setViewport(0, 0, e.getWidth(), e.getHeight());
return true; return true;
} }

44
inferno/src/inferno/render/context.cpp

@ -12,14 +12,14 @@ namespace Inferno {
Context::Context(GLFWwindow* window) : Context::Context(GLFWwindow* window) :
m_window(window) m_window(window)
{ {
ASSERT(window, "Context window is nullptr!");
} }
// -----------------------------------------
void Context::initialize() void Context::initialize()
{ {
Context::setCurrent();
// Initialize glad // Initialize glad
glfwMakeContextCurrent(m_window);
int glad = gladLoadGLLoader((GLADloadproc)glfwGetProcAddress); int glad = gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
ASSERT(glad, "Failed to initialize glad!"); ASSERT(glad, "Failed to initialize glad!");
@ -29,34 +29,12 @@ namespace Inferno {
comment() << " Renderer: " << glGetString(GL_RENDERER); comment() << " Renderer: " << glGetString(GL_RENDERER);
comment() << " Version: " << glGetString(GL_VERSION); comment() << " Version: " << glGetString(GL_VERSION);
#ifdef NF_ENABLE_ASSERTS // Check OpenGL version
int versionMajor; ASSERT(GLVersion.major > 4 || (GLVersion.major == 4 && GLVersion.minor >= 5),
int versionMinor;
glGetIntegerv(GL_MAJOR_VERSION, &versionMajor);
glGetIntegerv(GL_MINOR_VERSION, &versionMinor);
ASSERT(versionMajor > 4 || (versionMajor == 4 && versionMinor >= 5),
"Inferno requires at least OpenGL version 4.5!"); "Inferno requires at least OpenGL version 4.5!");
#endif
Window& w = *(Window*)glfwGetWindowUserPointer(m_window);
// Disable vsync
if (!w.isVSync()) {
glfwSwapInterval(0);
} }
// Set viewport void Context::destroy()
this->setViewport(0, 0, w.getWidth(), w.getHeight());
// Enable z-buffer / depth buffer
glEnable(GL_DEPTH_TEST);
// Enable transparency
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
}
void Context::update()
{ {
} }
@ -65,14 +43,10 @@ namespace Inferno {
glfwSwapBuffers(m_window); glfwSwapBuffers(m_window);
} }
void Context::destroy() void Context::setCurrent()
{ {
// Set current OpenGL context to this window
glfwMakeContextCurrent(m_window);
} }
// -----------------------------------------
void Context::setViewport(int x, int y, int width, int height) const
{
glViewport(x, y, width, height);
}
} }

10
inferno/src/inferno/render/context.h

@ -8,18 +8,12 @@ namespace Inferno {
class Context { class Context {
public: public:
Context(GLFWwindow* window); Context(GLFWwindow* window);
// virtual ~Context();
// -----------------------------------------
void initialize(); void initialize();
void update();
void render();
void destroy(); void destroy();
void render();
// ----------------------------------------- void setCurrent();
void setViewport(int x, int y, int width, int height) const;
private: private:
GLFWwindow* m_window; GLFWwindow* m_window;

36
inferno/src/inferno/render/renderer.cpp

@ -11,6 +11,21 @@
namespace Inferno { namespace Inferno {
void RenderCommand::initialize()
{
setDepthTest(true);
// Enable transparency
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
info() << "RenderCommand initialized";
}
void RenderCommand::destroy()
{
}
void RenderCommand::clear() void RenderCommand::clear()
{ {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@ -27,6 +42,24 @@ namespace Inferno {
glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, nullptr); glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, nullptr);
} }
void RenderCommand::setViewport(int32_t x, int32_t y, uint32_t width, uint32_t height)
{
glViewport(x, y, width, height);
}
void RenderCommand::setDepthTest(bool enabled)
{
// Set z-buffer / depth buffer
enabled ? glEnable(GL_DEPTH_TEST) : glDisable(GL_DEPTH_TEST);
}
bool RenderCommand::depthTest()
{
unsigned char depthTest = GL_FALSE;
glGetBooleanv(GL_DEPTH_TEST, &depthTest);
return depthTest == GL_TRUE;
}
int32_t RenderCommand::textureUnitAmount() int32_t RenderCommand::textureUnitAmount()
{ {
int32_t amount = 0; int32_t amount = 0;
@ -397,7 +430,10 @@ namespace Inferno {
bind(); bind();
// Render // Render
bool depthTest = RenderCommand::depthTest();
RenderCommand::setDepthTest(false);
RenderCommand::drawIndexed(m_vertexArray, m_quadIndex * indexPerQuad); RenderCommand::drawIndexed(m_vertexArray, m_quadIndex * indexPerQuad);
RenderCommand::setDepthTest(depthTest);
unbind(); unbind();
} }

10
inferno/src/inferno/render/renderer.h

@ -1,7 +1,7 @@
#ifndef RENDERER_H #ifndef RENDERER_H
#define RENDERER_H #define RENDERER_H
#include <cstdint> // std::uint32_t #include <cstdint> // std::int32_t, std::uint32_t
#include <memory> // std::shared_ptr, std::unique_ptr, std::make_shared, std::make_unique #include <memory> // std::shared_ptr, std::unique_ptr, std::make_shared, std::make_unique
#include "glm/ext/matrix_float4x4.hpp" // glm::mat4 #include "glm/ext/matrix_float4x4.hpp" // glm::mat4
@ -42,11 +42,17 @@ namespace Inferno {
class RenderCommand { class RenderCommand {
public: public:
static void initialize();
static void destroy();
static void clear(); static void clear();
static void clearColor(const glm::vec4& color); static void clearColor(const glm::vec4& color);
static void drawIndexed(const std::shared_ptr<VertexArray>& vertexArray, uint32_t indexCount = 0); static void drawIndexed(const std::shared_ptr<VertexArray>& vertexArray, uint32_t indexCount = 0);
static void setViewport(int32_t x, int32_t y, uint32_t width, uint32_t height);
static void setDepthTest(bool enabled);
static bool depthTest();
static int32_t textureUnitAmount(); static int32_t textureUnitAmount();
}; };

52
inferno/src/inferno/window.cpp

@ -5,10 +5,11 @@
#include "inferno/event/applicationevent.h" #include "inferno/event/applicationevent.h"
#include "inferno/event/keyevent.h" #include "inferno/event/keyevent.h"
#include "inferno/event/mouseevent.h" #include "inferno/event/mouseevent.h"
#include "inferno/keycodes.h"
#include "inferno/io/input.h" #include "inferno/io/input.h"
#include "inferno/io/log.h" #include "inferno/io/log.h"
#include "inferno/keycodes.h"
#include "inferno/render/context.h" #include "inferno/render/context.h"
#include "inferno/render/renderer.h"
#include "inferno/settings.h" #include "inferno/settings.h"
#include "inferno/window.h" #include "inferno/window.h"
@ -41,6 +42,7 @@ namespace Inferno {
std::string title = m_properties.title; std::string title = m_properties.title;
unsigned int width = m_properties.width; unsigned int width = m_properties.width;
unsigned int height = m_properties.height; unsigned int height = m_properties.height;
bool vsync = m_properties.vsync;
// Only init once // Only init once
if (s_windowCount == 0) { if (s_windowCount == 0) {
@ -68,8 +70,9 @@ namespace Inferno {
m_context = std::make_shared<Context>(m_window); m_context = std::make_shared<Context>(m_window);
m_context->initialize(); m_context->initialize();
// Capture cursor and hide it // Set vsync, viewport
// glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); setVSync(vsync);
RenderCommand::setViewport(0, 0, width, height);
// Error callback // Error callback
glfwSetErrorCallback([](int error, const char* description) { glfwSetErrorCallback([](int error, const char* description) {
@ -160,18 +163,29 @@ namespace Inferno {
}); });
} }
void Window::destroy()
{
m_context->destroy();
glfwDestroyWindow(m_window);
s_windowCount--;
if (s_windowCount == 0) {
glfwTerminate();
}
}
void Window::update() void Window::update()
{ {
glfwPollEvents(); glfwPollEvents();
// Lock mouse in window // Capture cursor in window and hide it
if (Input::isKeyPressed(keyCode("GLFW_KEY_LEFT_SUPER"))) { if (!Input::isKeyPressed(keyCode("GLFW_KEY_LEFT_SUPER"))) {
glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
} }
else { else {
glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
} }
} }
void Window::render() void Window::render()
@ -179,19 +193,9 @@ namespace Inferno {
m_context->render(); m_context->render();
} }
void Window::destroy()
{
glfwDestroyWindow(m_window);
s_windowCount--;
if (s_windowCount == 0) {
glfwTerminate();
}
}
// ----------------------------------------- // -----------------------------------------
void Window::setWindowMonitor() void Window::setWindowMonitor() const
{ {
GLFWmonitor* monitor = glfwGetPrimaryMonitor(); GLFWmonitor* monitor = glfwGetPrimaryMonitor();
int xPos = 0; int xPos = 0;
@ -222,8 +226,10 @@ namespace Inferno {
glfwSetWindowMonitor(m_window, monitor, xPos, yPos, width, height, refresh); glfwSetWindowMonitor(m_window, monitor, xPos, yPos, width, height, refresh);
} }
bool Window::shouldClose() const { void Window::setVSync(bool enabled)
return glfwWindowShouldClose(m_window); {
enabled ? glfwSwapInterval(GL_TRUE) : glfwSwapInterval(GL_FALSE);
m_properties.vsync = enabled;
} }
void Window::setShouldClose(bool close) const void Window::setShouldClose(bool close) const
@ -231,4 +237,8 @@ namespace Inferno {
glfwSetWindowShouldClose(m_window, close ? GL_TRUE : GL_FALSE); glfwSetWindowShouldClose(m_window, close ? GL_TRUE : GL_FALSE);
} }
bool Window::shouldClose() const {
return glfwWindowShouldClose(m_window);
}
} }

8
inferno/src/inferno/window.h

@ -28,20 +28,20 @@ namespace Inferno {
// ----------------------------------------- // -----------------------------------------
void initialize(); void initialize();
void destroy();
void update(); void update();
void render(); void render();
void destroy();
// ----------------------------------------- // -----------------------------------------
void setWindowMonitor(); void setWindowMonitor() const;
bool shouldClose() const; void setVSync(bool enabled);
void setShouldClose(bool close) const; void setShouldClose(bool close) const;
bool shouldClose() const;
inline float getAspect() const { return static_cast<float>(m_properties.width) / static_cast<float>(m_properties.height); } inline float getAspect() const { return static_cast<float>(m_properties.width) / static_cast<float>(m_properties.height); }
inline int getWidth() const { return m_properties.width; } inline int getWidth() const { return m_properties.width; }
inline int getHeight() const { return m_properties.height; } inline int getHeight() const { return m_properties.height; }
inline bool isVSync() const { return m_properties.vsync; }
inline GLFWwindow* getWindow() const { return m_window; } inline GLFWwindow* getWindow() const { return m_window; }
inline const std::shared_ptr<Context>& getContext() const { return m_context; } inline const std::shared_ptr<Context>& getContext() const { return m_context; }

Loading…
Cancel
Save