+ Added PKGV0015 support

+ Added basic support for mouse position on shaders (still needs to be adjusted for backgrounds that are too big for the screen)
  (this makes XRAY effects work)

Signed-off-by: Alexis Maiquez <almamu@almamu.com>
This commit is contained in:
Alexis Maiquez 2021-12-03 02:02:46 +01:00
parent d0572b257f
commit b55f2e8bf4
14 changed files with 147 additions and 30 deletions

View File

@ -54,6 +54,9 @@ add_executable(
src/WallpaperEngine/Core/Core.h
src/WallpaperEngine/Core/Core.cpp
src/WallpaperEngine/Input/CMouseInput.h
src/WallpaperEngine/Input/CMouseInput.cpp
src/WallpaperEngine/Render/Shaders/Variables/CShaderVariable.h
src/WallpaperEngine/Render/Shaders/Variables/CShaderVariable.cpp
src/WallpaperEngine/Render/Shaders/Variables/CShaderVariableFloat.h

View File

@ -180,9 +180,6 @@ int main (int argc, char* argv[])
auto project = WallpaperEngine::Core::CProject::fromFile ("project.json", containers);
WallpaperEngine::Render::CWallpaper* wallpaper;
// initialize custom context class
WallpaperEngine::Render::CContext* context = new WallpaperEngine::Render::CContext (screens);
// auto projection = project->getWallpaper ()->as <WallpaperEngine::Core::CScene> ()->getOrthogonalProjection ();
// create the window!
// TODO: DO WE NEED TO PASS MONITOR HERE OR ANYTHING?
@ -198,6 +195,11 @@ int main (int argc, char* argv[])
glfwMakeContextCurrent (window);
// initialize inputs
CMouseInput* mouseInput = new CMouseInput (window);
// initialize custom context class
WallpaperEngine::Render::CContext* context = new WallpaperEngine::Render::CContext (screens, mouseInput);
// TODO: FIGURE THESE OUT BASED ON THE SCREEN
int windowWidth = 1920;
int windowHeight = 1080;
@ -218,7 +220,7 @@ int main (int argc, char* argv[])
if (project->getType () == "scene")
{
WallpaperEngine::Core::CScene* scene = project->getWallpaper ()->as <WallpaperEngine::Core::CScene> ();
wallpaper = new WallpaperEngine::Render::CScene (scene, containers);
wallpaper = new WallpaperEngine::Render::CScene (scene, containers, context);
}
else if (project->getType () == "video")
{
@ -227,7 +229,7 @@ int main (int argc, char* argv[])
chdir (path.c_str ());
WallpaperEngine::Core::CVideo* video = project->getWallpaper ()->as <WallpaperEngine::Core::CVideo> ();
wallpaper = new WallpaperEngine::Render::CVideo (video, containers);
wallpaper = new WallpaperEngine::Render::CVideo (video, containers, context);
}
else
{
@ -277,6 +279,8 @@ int main (int argc, char* argv[])
g_Time = (float) glfwGetTime ();
// get the start time of the frame
startTime = clock ();
// update our inputs first
mouseInput->update ();
// render the scene
context->render ();
// do buffer swapping

View File

@ -111,7 +111,8 @@ void CPackage::validateHeader (FILE* fp)
strcmp ("PKGV0005", pointer) != 0 &&
strcmp ("PKGV0006", pointer) != 0 &&
strcmp ("PKGV0013", pointer) != 0 &&
strcmp ("PKGV0014", pointer) != 0)
strcmp ("PKGV0014", pointer) != 0 &&
strcmp ("PKGV0015", pointer) != 0)
{
delete[] pointer;
throw std::runtime_error ("Unsupported package version");

View File

@ -0,0 +1,14 @@
#include <glm/common.hpp>
#include "CMouseInput.h"
using namespace WallpaperEngine::Input;
CMouseInput::CMouseInput (GLFWwindow* window) : position(0, 0), m_window (window) {}
void CMouseInput::update ()
{
// update current mouse position
glfwGetCursorPos (this->m_window, &this->m_mousePosition.x, &this->m_mousePosition.y);
// interpolate to the new position
this->position = glm::mix (this->position, this->m_mousePosition, 1.0);
}

View File

@ -0,0 +1,37 @@
#pragma once
#include <glm/vec2.hpp>
#include "GLFW/glfw3.h"
namespace WallpaperEngine::Input
{
/**
* Handles mouse input for the background
*/
class CMouseInput
{
public:
CMouseInput(GLFWwindow* window);
/**
* Takes current mouse position and updates it
*/
void update ();
/**
* The virtual pointer's position
*/
glm::dvec2 position;
private:
/**
* The GLFW window to get mouse position from
*/
GLFWwindow* m_window;
/**
* The current mouse position
*/
glm::dvec2 m_mousePosition;
};
};

View File

@ -6,15 +6,16 @@
#include <GL/glx.h>
#include "CContext.h"
#include "CVideo.h"
using namespace WallpaperEngine::Render;
CContext::CContext (std::vector <std::string> screens) :
CContext::CContext (std::vector <std::string> screens, CMouseInput* mouse) :
m_wallpaper (nullptr),
m_screens (std::move (screens)),
m_isRootWindow (m_screens.empty () == false),
m_window (0),
m_defaultViewport ({0, 0, 1920, 1080})
m_defaultViewport ({0, 0, 1920, 1080}),
m_mouse (mouse)
{
this->initializeViewports ();
}
@ -76,8 +77,6 @@ void CContext::initializeViewports ()
XRRFreeScreenResources (screenResources);
// store the window
this->m_window = DefaultRootWindow (display);
// set the
glfwWindowHintPointer (GLFW_NATIVE_PARENT_HANDLE, reinterpret_cast <void*> (DefaultRootWindow (display)));
}
@ -96,7 +95,9 @@ void CContext::render ()
for (; cur != end; cur ++)
{
this->m_wallpaper->render (*cur, firstFrame);
firstFrame = false;
// scenes need to render a new frame for each viewport as they produce different results
// but videos should only be rendered once per group of viewports
firstFrame = !this->m_wallpaper->is <CVideo> ();
}
}
else
@ -111,4 +112,9 @@ void CContext::setWallpaper (CWallpaper* wallpaper)
void CContext::setDefaultViewport (glm::vec4 defaultViewport)
{
this->m_defaultViewport = defaultViewport;
}
CMouseInput* CContext::getMouse () const
{
return this->m_mouse;
}

View File

@ -3,26 +3,31 @@
#include <vector>
#include <glm/vec4.hpp>
#include "WallpaperEngine/Input/CMouseInput.h"
#include "CWallpaper.h"
using namespace WallpaperEngine::Input;
namespace WallpaperEngine::Render
{
class CWallpaper;
class CContext
{
public:
CContext (std::vector <std::string> screens);
CContext (std::vector <std::string> screens, CMouseInput* mouse);
void initializeViewports ();
void render ();
void setWallpaper (CWallpaper* wallpaper);
void setDefaultViewport (glm::vec4 defaultViewport);
CMouseInput* getMouse () const;
private:
Window m_window;
std::vector <std::string> m_screens;
std::vector <glm::vec4> m_viewports;
glm::vec4 m_defaultViewport;
CWallpaper* m_wallpaper;
CMouseInput* m_mouse;
bool m_isRootWindow;
};
}

View File

@ -9,8 +9,8 @@
using namespace WallpaperEngine;
using namespace WallpaperEngine::Render;
CScene::CScene (Core::CScene* scene, CContainer* container) :
CWallpaper (scene, Type, container)
CScene::CScene (Core::CScene* scene, CContainer* container, CContext* context) :
CWallpaper (scene, Type, container, context)
{
// setup the scene camera
this->m_camera = new CCamera (this, scene->getCamera ());
@ -56,12 +56,15 @@ CCamera* CScene::getCamera () const
return this->m_camera;
}
void CScene::renderFrame ()
void CScene::renderFrame (glm::vec4 viewport)
{
auto projection = this->getScene ()->getOrthogonalProjection ();
auto cur = this->m_objects.begin ();
auto end = this->m_objects.end ();
// ensure the virtual mouse position is up to date
this->updateMouse (viewport);
// clear screen
FloatColor clearColor = this->getScene ()->getClearColor ();
@ -79,9 +82,28 @@ void CScene::renderFrame ()
glViewport (0, 0, projection->getWidth (), projection->getHeight ());
}
void CScene::updateMouse (glm::vec4 viewport)
{
// projection also affects the mouse position
auto projection = this->getScene ()->getOrthogonalProjection ();
// update virtual mouse position first
CMouseInput* mouse = this->getContext ()->getMouse ();
// TODO: PROPERLY TRANSLATE THESE TO WHAT'S VISIBLE ON SCREEN (FOR BACKGROUNDS THAT DO NOT EXACTLY FIT ON SCREEN)
this->m_mousePosition.x = glm::clamp ((mouse->position.x - viewport.x) / viewport.z, 0.0, 1.0);
this->m_mousePosition.y = glm::clamp ((mouse->position.y - viewport.y) / viewport.w, 0.0, 1.0);
// screen-space positions have to be transposed to what the screen will actually show
}
Core::CScene* CScene::getScene ()
{
return this->getWallpaperData ()->as<Core::CScene> ();
}
glm::vec2* CScene::getMousePosition ()
{
return &this->m_mousePosition;
}
const std::string CScene::Type = "scene";

View File

@ -15,14 +15,17 @@ namespace WallpaperEngine::Render
class CScene : public CWallpaper
{
public:
CScene (Core::CScene* scene, CContainer* container);
CScene (Core::CScene* scene, CContainer* container, CContext* context);
CCamera* getCamera () const;
Core::CScene* getScene ();
glm::vec2* getMousePosition ();
protected:
void renderFrame () override;
void renderFrame (glm::vec4 viewport) override;
void updateMouse (glm::vec4 viewport);
friend class CWallpaper;
@ -31,5 +34,6 @@ namespace WallpaperEngine::Render
private:
CCamera* m_camera;
std::vector<CObject*> m_objects;
glm::vec2 m_mousePosition;
};
}

View File

@ -3,8 +3,8 @@
using namespace WallpaperEngine;
using namespace WallpaperEngine::Render;
CVideo::CVideo (Core::CVideo* video, CContainer* container) :
CWallpaper (video, Type, container)
CVideo::CVideo (Core::CVideo* video, CContainer* container, CContext* context) :
CWallpaper (video, Type, container, context)
{
if (avformat_open_input (&m_formatCtx, video->getFilename ().c_str (), NULL, NULL) < 0)
throw std::runtime_error ("Failed to open video file");
@ -115,7 +115,7 @@ void CVideo::setSize (int width, int height)
SWS_BILINEAR, NULL, NULL, NULL);
}
void CVideo::renderFrame ()
void CVideo::renderFrame (glm::vec4 viewport)
{
// do not render using the CWallpaper function, just use this one
this->setSize (m_codecCtx->width, m_codecCtx->height);

View File

@ -17,7 +17,7 @@ namespace WallpaperEngine::Render
class CVideo : public CWallpaper
{
public:
CVideo (Core::CVideo* video, CContainer* container);
CVideo (Core::CVideo* video, CContainer* container, CContext* context);
Core::CVideo* getVideo ();
@ -25,7 +25,7 @@ namespace WallpaperEngine::Render
int getHeight ();
protected:
void renderFrame () override;
void renderFrame (glm::vec4 viewport) override;
friend class CWallpaper;

View File

@ -8,10 +8,11 @@
using namespace WallpaperEngine::Render;
CWallpaper::CWallpaper (Core::CWallpaper* wallpaperData, std::string type, CContainer* container) :
CWallpaper::CWallpaper (Core::CWallpaper* wallpaperData, std::string type, CContainer* container, CContext* context) :
m_container (container),
m_wallpaperData (wallpaperData),
m_type (std::move(type))
m_type (std::move(type)),
m_context (context)
{
this->setupShaders ();
@ -187,7 +188,7 @@ void CWallpaper::setupShaders ()
void CWallpaper::render (glm::vec4 viewport, bool newFrame)
{
if (newFrame == true)
this->renderFrame ();
this->renderFrame (viewport);
int windowWidth = 1920;
int windowHeight = 1080;
@ -310,6 +311,11 @@ void CWallpaper::setupFramebuffers ()
this->m_sceneFBO = this->createFBO ("_rt_FullFrameBuffer", ITexture::TextureFormat::ARGB8888, 1.0, windowWidth, windowHeight, windowWidth, windowHeight);
}
CContext* CWallpaper::getContext ()
{
return this->m_context;
}
CFBO* CWallpaper::createFBO (const std::string& name, ITexture::TextureFormat format, float scale, uint32_t realWidth, uint32_t realHeight, uint32_t textureWidth, uint32_t textureHeight)
{
CFBO* fbo = new CFBO (name, format, scale, realWidth, realHeight, textureWidth, textureHeight);

View File

@ -9,11 +9,14 @@
#include "WallpaperEngine/Assets/CContainer.h"
#include "CFBO.h"
#include "CContext.h"
using namespace WallpaperEngine::Assets;
namespace WallpaperEngine::Render
{
class CContext;
class CWallpaper
{
public:
@ -22,7 +25,7 @@ namespace WallpaperEngine::Render
template<class T> bool is () { return this->m_type == T::Type; }
CWallpaper (Core::CWallpaper* wallpaperData, std::string type, CContainer* container);
CWallpaper (Core::CWallpaper* wallpaperData, std::string type, CContainer* container, CContext* context);
~CWallpaper ();
/**
@ -72,13 +75,18 @@ namespace WallpaperEngine::Render
/**
* Renders a frame of the wallpaper
*/
virtual void renderFrame () = 0;
virtual void renderFrame (glm::vec4 viewport) = 0;
/**
* Setups OpenGL's framebuffers for ping-pong and scene rendering
*/
void setupFramebuffers ();
/**
* @return The current context rendering this wallpaper
*/
CContext* getContext ();
CContainer* m_container;
Core::CWallpaper* m_wallpaperData;
@ -113,5 +121,9 @@ namespace WallpaperEngine::Render
* List of FBOs registered for this wallpaper
*/
std::map<std::string, CFBO*> m_fbos;
/**
* Context that is using this wallpaper
*/
CContext* m_context;
};
}

View File

@ -470,6 +470,9 @@ void CPass::setupUniforms ()
this->addUniform ("g_Time", &g_Time);
// add model-view-projection matrix
this->addUniform ("g_ModelViewProjectionMatrix", &this->m_modelViewProjectionMatrix);
this->addUniform ("g_PointerPosition", this->m_material->getImage ()->getScene ()->getMousePosition ());
this->addUniform ("g_EffectTextureProjectionMatrix", glm::mat4(1.0));
this->addUniform ("g_EffectTextureProjectionMatrixInverse", glm::mat4(1.0));
}
void CPass::addAttribute (const std::string& name, GLint type, GLint elements, const GLuint* value)