Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/common/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
#include <locale>
#include <regex>

#include <GLFW/glfw3.h>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This include should not appear outside the platform library


#include "src/common/platform.h"
#include "src/common/exception.h"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this was only used for the monitor function, so it could be removed as well

#include "src/common/types.h"
#include "src/common/crc32.h"
#include "src/common/strutil.h"
Expand Down Expand Up @@ -135,4 +138,4 @@ std::string getUserDataDirectory() {
return userData;
}

}
} // End of namespace Common
10 changes: 6 additions & 4 deletions src/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
#ifndef OPENAWE_CONFIGURATION_H
#define OPENAWE_CONFIGURATION_H

struct ResolutionData {
unsigned int width, height;
bool fullscreen;
};

/*!
* \brief Class representing the global configuration of the engine
*
Expand All @@ -32,10 +37,7 @@ class Configuration {
public:
virtual ~Configuration() = default;

struct {
unsigned int width, height;
bool fullscreen;
} resolution;
ResolutionData resolution;

/*!
* Write the configuration
Expand Down
6 changes: 6 additions & 0 deletions src/engines/awan/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ void Configuration::read() {
if (std::filesystem::is_regular_file(resolutionFile)) {
auto resolutionStream = std::make_unique<Common::ReadFile>(resolutionFile);
readResolution(*resolutionStream);
} else {
resolution = {
0,
0,
false
};
}

if (std::filesystem::is_regular_file(configFile)) {
Expand Down
1 change: 1 addition & 0 deletions src/engines/awan/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Engine::Engine(entt::registry &registry, const LocaleConfig::Config &config) :
::Engine(registry, config, new Functions(registry, *this)) {
_storyModeRound = 1;
_configuration.reset(new AlanWakesAmericanNightmare::Configuration());
_configuration->read();
}

void Engine::init() {
Expand Down
23 changes: 23 additions & 0 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@

#include <CLI/CLI.hpp>

#include <src/graphics/fontman.h>
#include "src/configuration.h"
#include "src/graphics/gfxman.h"
#include "src/graphics/text.h"

#include "src/common/crc32.h"
#include "src/common/threadpool.h"
#include "src/common/strutil.h"
Expand All @@ -38,6 +43,7 @@
#include "src/platform/keyconversion.h"
#include "src/platform/gamepadconversion.h"
#include "src/platform/gamepadman.h"
#include "src/platform/platform.h"

#include "src/graphics/fontman.h"
#include "src/graphics/text.h"
Expand Down Expand Up @@ -270,6 +276,23 @@ void Game::init() {

_engine->init();
});

// set window resolution and fullscreen mode
Configuration & config = _engine->getConfiguration();

if (config.resolution.width == 0 || config.resolution.height == 0) {
Platform::VideoMode videoMode = _platform.getPrimaryMonitorVideoMode();
config.resolution.width = videoMode.width;
config.resolution.height = videoMode.height;
config.resolution.fullscreen = true;
}

_window->setFullscreen(config.resolution.fullscreen);
_window->setSize(config.resolution.width, config.resolution.height);
spdlog::error("Window size: {} x {}", _window->getSize().x, _window->getSize().y);
spdlog::error("Content scale: {} x {}", _window->getContentScale().x, _window->getContentScale().y);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to self - remove these log messages as well

GfxMan.setScreenSize(config.resolution.width, config.resolution.height);
GfxMan.setContentScale(_window->getContentScale());
}

void Game::start() {
Expand Down
8 changes: 8 additions & 0 deletions src/graphics/gfxman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,14 @@ void GraphicsManager::drawFrame() {
_renderer->drawFrame();
}

void GraphicsManager::setScreenSize(unsigned int width, unsigned int height) {
_renderer->setRenderPlane(width, height);
}

void GraphicsManager::setContentScale(glm::vec2 scale) {
_renderer->setContentScale(scale);
}

void GraphicsManager::setAmbianceState(const std::string &id) {
if (!_renderer)
return;
Expand Down
3 changes: 3 additions & 0 deletions src/graphics/gfxman.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ class GraphicsManager : public Common::Singleton<GraphicsManager> {

void drawFrame();

void setScreenSize(unsigned int width, unsigned int height);
void setContentScale(glm::vec2 scale);

private:
std::unique_ptr<Renderer> _renderer;
};
Expand Down
13 changes: 9 additions & 4 deletions src/graphics/opengl/framebuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/

#include <glm/gtc/type_ptr.hpp>
#include <algorithm>

#include "src/common/exception.h"

Expand All @@ -29,8 +30,10 @@ namespace Graphics::OpenGL {
Framebuffer::Framebuffer(const std::string &label) {
glGenFramebuffers(1, &_id);

if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
throw CreateException("Failed to initialize framebuffer");
const GLenum framebufferStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);

if (framebufferStatus != GL_FRAMEBUFFER_COMPLETE)
throw CreateException("Failed to initialize framebuffer: {}", framebufferStatus);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why you split up the framebuffer status?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was not specifically splitting, I was just outputting the error code in the exception message. I'll revert this change - it doesn't have to be here.


bind();
if (GLAD_GL_KHR_debug && !label.empty())
Expand Down Expand Up @@ -64,8 +67,10 @@ void Framebuffer::attachRenderBuffer(const Renderbuffer &renderbuffer, GLenum at
renderbuffer._id
);

if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
throw CreateException("Failed to attach renderbuffer tot exture");
const GLenum framebufferStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);

if (framebufferStatus != GL_FRAMEBUFFER_COMPLETE)
throw CreateException("Failed to render buffer to texture: {}", framebufferStatus);
}

void Framebuffer::clear() {
Expand Down
47 changes: 32 additions & 15 deletions src/graphics/opengl/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
#include "src/common/strutil.h"
#include "src/common/readfile.h"

#include "src/platform/window.h"

#include "src/awe/resman.h"
#include "src/awe/objfile.h"
#include "src/awe/hg.h"
Expand All @@ -51,6 +53,7 @@
#include "src/graphics/shaderconverter.h"
#include "src/graphics/skeleton.h"
#include "src/graphics/images/surface.h"
#include "src/graphics/renderer.h"
#include "src/graphics/opengl/renderer.h"
#include "src/graphics/opengl/opengl.h"
#include "src/graphics/opengl/vbo.h"
Expand All @@ -64,6 +67,7 @@ static void *loadProcAddress(const char *name) {
}

Renderer::Renderer(Platform::Window &window, const std::string &shaderDirectory) :
Graphics::Renderer(window.getSize()),
_window(window),
_shaderDirectory(shaderDirectory) {
_window.makeCurrent();
Expand Down Expand Up @@ -194,27 +198,35 @@ Renderer::Renderer(Platform::Window &window, const std::string &shaderDirectory)
//
rebuildShaders();

// Get width and height of the default framebuffer
unsigned int width, height;
window.getSize(width, height);
setRenderPlane(_window.getSize());

// Check for errors
assert(glGetError() == GL_NO_ERROR);
}

void Renderer::setRenderPlane(unsigned int width, unsigned int height) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change this to SetViewportSize

setRenderPlane(glm::vec2(width, height));
}

void Renderer::setRenderPlane(const glm::vec2 size) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change this to SetViewportSize

Graphics::Renderer::setRenderPlane(size);

// Initialize deferred shading
//
_depthTexture = std::make_unique<Texture>(_loadingTasks, width, height, kRGBA16F, "depth_buffer");
_normalTexture = std::make_unique<Texture>(_loadingTasks, width, height, kRGBA16F, "normal_buffer");
_depthstencilBuffer = std::make_unique<Renderbuffer>(width, height, GL_DEPTH24_STENCIL8,
_depthTexture = std::make_unique<Texture>(_loadingTasks, size.x, size.y, kRGBA16F, "depth_buffer");
_normalTexture = std::make_unique<Texture>(_loadingTasks, size.x, size.y, kRGBA16F, "normal_buffer");
_depthstencilBuffer = std::make_unique<Renderbuffer>(size.x, size.y, GL_DEPTH24_STENCIL8,
"depthstencil_renderbuffer");

_deferredBuffer = std::make_unique<Framebuffer>("Deferred Buffer");
_deferredBuffer->setClearColor({0.0f, 0.0f, 0.0f, 0.0f});
_deferredBuffer->bind();

_deferredBuffer->attachRenderBuffer(*_depthstencilBuffer, GL_DEPTH_STENCIL_ATTACHMENT);

_deferredBuffer->attachTexture(*_depthTexture, GL_COLOR_ATTACHMENT0);
_deferredBuffer->attachTexture(*_normalTexture, GL_COLOR_ATTACHMENT1);

_lightBufferTexture = std::make_unique<Texture>(_loadingTasks, width, height, kRGBA16F, "light_buffer");
_lightBufferTexture = std::make_unique<Texture>(_loadingTasks, size.x, size.y, kRGBA16F, "light_buffer");

_lightBuffer = std::make_unique<Framebuffer>("Light Buffer");
_lightBuffer->setClearColor({0.0f, 0.0f, 0.0f, 0.0f});
Expand All @@ -241,11 +253,18 @@ Renderer::Renderer(Platform::Window &window, const std::string &shaderDirectory)

// Initialize ImGui
ImGui_ImplOpenGL3_Init();
glViewport(0, 0, size.x, size.y);

// Check for errors
assert(glGetError() == GL_NO_ERROR);
}

void Renderer::setContentScale(glm::vec2 scale) {
Graphics::Renderer::setContentScale(scale);
glm::vec2 scaledResolution = _viewportSize * _contentScale;
glViewport(0, 0, scaledResolution.x, scaledResolution.y);
}

Renderer::~Renderer() {
ImGui_ImplOpenGL3_Shutdown();
}
Expand Down Expand Up @@ -297,8 +316,6 @@ void Renderer::drawWorld(const std::string &stage) {
const auto &defaultShader = getProgram("standardmaterial", stage, 0);
static const glm::mat4 mirrorZ = glm::scale(glm::vec3(1, 1, -1));

const auto screenResolution = glm::vec2(1920, 1080);

bool wireframe = false;
Material::CullMode cullMode = Material::kNone;

Expand Down Expand Up @@ -329,7 +346,7 @@ void Renderer::drawWorld(const std::string &stage) {
const std::optional<GLint> skinningMatrices = currentShader->getUniformLocation("GPU_skinning_matrices");

if (screenRes)
currentShader->setUniform2f(*screenRes, screenResolution);
currentShader->setUniform2f(*screenRes, _viewportSize);

GLuint textureSlotShader = 0;
if (lightBuffer) {
Expand Down Expand Up @@ -580,7 +597,7 @@ void Renderer::drawLights() {

const auto cameraLightPositionView = _view * mirrorZ * light->getTransform()[3];

pointlightProgram->setUniform2f(screenResIndex, glm::vec2(1920, 1080));
pointlightProgram->setUniform2f(screenResIndex, _viewportSize);
pointlightProgram->setUniformMatrix4f(localToClipIndex, vp * mirrorZ * light->getTransform());
pointlightProgram->setUniformMatrix4f(clipToViewIndex, clipToView);
pointlightProgram->setUniform3f(lightPositionIndex, cameraLightPositionView);
Expand All @@ -605,7 +622,7 @@ void Renderer::drawLights() {
}

void Renderer::drawGUI() {
glm::mat4 vp = glm::ortho(0.0f, 1920.0f, 0.0f, 1080.0f, -1000.0f, 1000.0f);
glm::mat4 vp = glm::ortho(0.0f, _viewportSize.x, 0.0f, _viewportSize.y, -1000.0f, 1000.0f);

pushDebug("Draw GUI");

Expand All @@ -621,14 +638,14 @@ void Renderer::drawGUI() {
for (const auto &element: _guiElements) {
glm::mat4 m = glm::translate(glm::vec3(
element->getAbsolutePosition() +
element->getRelativePosition() * glm::vec2(1920.0, 1080.0),
element->getRelativePosition() * _viewportSize,
0.0f
));
std::static_pointer_cast<Graphics::OpenGL::VAO>(element->getVertexAttributes())->bind();

for (const auto &part: element->getParts()) {
glm::mat4 m2 = m *
glm::scale(glm::vec3(part.absoluteSize + part.relativeSize * glm::vec2(1920, 1080), 1.0f)) *
glm::scale(glm::vec3(part.absoluteSize + part.relativeSize * _viewportSize, 1.0f)) *
glm::translate(glm::vec3(part.position, 1.0f));
std::static_pointer_cast<Graphics::OpenGL::VBO>(part.indices)->bind();

Expand Down
3 changes: 3 additions & 0 deletions src/graphics/opengl/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ class Renderer : public Graphics::Renderer {
void drawFrame() override;

bool isLoading() const override;
void setRenderPlane(unsigned int width, unsigned int height) override;
void setRenderPlane(glm::vec2 size) override;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename also these methods to setRenderPlane

void setContentScale(glm::vec2 scale) override;

TexturePtr createTexture(
TextureType type,
Expand Down
21 changes: 19 additions & 2 deletions src/graphics/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,31 @@

#include "renderer.h"

Graphics::Renderer::Renderer() {
Graphics::Renderer::Renderer(unsigned int width, unsigned int height) {
setRenderPlane(width, height);
}

Graphics::Renderer::Renderer(glm::vec2 renderPlane) {
setRenderPlane(renderPlane);
}

void Graphics::Renderer::setRenderPlane(unsigned int width, unsigned int height) {
setRenderPlane(glm::vec2(width, height));
}

void Graphics::Renderer::setRenderPlane(glm::vec2 renderPlane) {
// Setup initial projection matrix
_projection = glm::perspectiveFov(45.0f, 1920.0f, 1080.0f, 1.0f, 10000.0f);
_viewportSize = renderPlane;
_projection = glm::perspectiveFov(45.0f, _viewportSize.x, _viewportSize.y, 1.0f, 10000.0f);

// Initialize frustrum with projection matrix
_frustrum.setProjectionMatrix(_projection);
}

void Graphics::Renderer::setContentScale(glm::vec2 scale) {
_contentScale = scale;
}

Graphics::Renderer::~Renderer() {
}

Expand Down
9 changes: 8 additions & 1 deletion src/graphics/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,14 @@ namespace Graphics {

class Renderer {
public:
Renderer();
Renderer(unsigned int width, unsigned int height);
Renderer(glm::vec2 renderPlane);
virtual ~Renderer();

virtual void setRenderPlane(unsigned int width, unsigned int height);
virtual void setRenderPlane(glm::vec2 renderPlane);
virtual void setContentScale(glm::vec2 scale);

void addModel(Model *model);
void removeModel(Model *model);
void addGUIElement(GUIElement *gui);
Expand Down Expand Up @@ -141,6 +146,8 @@ class Renderer {

glm::mat4 _view;
glm::mat4 _projection;
glm::vec2 _viewportSize;
glm::vec2 _contentScale;

SkyPtr _sky;

Expand Down
3 changes: 3 additions & 0 deletions src/platform/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
#ifndef AWE_CONTEXT_H
#define AWE_CONTEXT_H

#include <glm/vec2.hpp>

namespace Platform {

class Context {
public:
virtual void getSize(unsigned int &width, unsigned int &height) = 0;
virtual glm::vec2 getSize() = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this method? I would prefer having the context interface to be designed in a minimal and concise way and if necessary convert it somewhere else to float

};

}
Expand Down
Loading