-
-
Notifications
You must be signed in to change notification settings - Fork 381
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix upside-down and vertical effect misalignment issues.
If anyone else can come up with a solution that doesn't involve up to three vertical flips, please implement.
- Loading branch information
Showing
19 changed files
with
321 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
#include "FlipTexture.hpp" | ||
|
||
FlipTexture::FlipTexture(const PresetState& presetState) | ||
: RenderItem() | ||
, m_presetState(presetState) | ||
{ | ||
RenderItem::Init(); | ||
|
||
m_framebuffer.CreateColorAttachment(0, 0); | ||
} | ||
|
||
void FlipTexture::InitVertexAttrib() | ||
{ | ||
glEnableVertexAttribArray(0); | ||
glDisableVertexAttribArray(1); | ||
glEnableVertexAttribArray(2); | ||
|
||
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedPoint), reinterpret_cast<void*>(offsetof(TexturedPoint, x))); // Position | ||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedPoint), reinterpret_cast<void*>(offsetof(TexturedPoint, u))); // Texture coordinate | ||
|
||
std::array<RenderItem::TexturedPoint, 4> points; | ||
|
||
points[0].x = -1.0; | ||
points[0].y = 1.0; | ||
points[1].x = 1.0; | ||
points[1].y = 1.0; | ||
points[2].x = -1.0; | ||
points[2].y = -1.0; | ||
points[3].x = 1.0; | ||
points[3].y = -1.0; | ||
|
||
points[0].u = 0.0; | ||
points[0].v = 1.0; | ||
points[1].u = 1.0; | ||
points[1].v = 1.0; | ||
points[2].u = 0.0; | ||
points[2].v = 0.0; | ||
points[3].u = 1.0; | ||
points[3].v = 0.0; | ||
|
||
glBufferData(GL_ARRAY_BUFFER, sizeof(points), points.data(), GL_STATIC_DRAW); | ||
} | ||
|
||
void FlipTexture::Draw(const std::shared_ptr<Texture>& originalTexture, const std::shared_ptr<Texture>& targetTexture) | ||
{ | ||
if (originalTexture == nullptr || originalTexture == targetTexture) | ||
{ | ||
return; | ||
} | ||
|
||
UpdateTextureSize(); | ||
|
||
if (m_viewportWidth == 0 || m_viewportHeight == 0) | ||
{ | ||
return; | ||
} | ||
|
||
std::shared_ptr<Texture> internalTexture; | ||
|
||
m_framebuffer.Bind(0); | ||
|
||
// Draw from unflipped texture | ||
originalTexture->Bind(0); | ||
|
||
if (targetTexture) | ||
{ | ||
internalTexture = m_framebuffer.GetColorAttachmentTexture(0, 0); | ||
m_framebuffer.GetAttachment(0, TextureAttachment::AttachmentType::Color, 0)->Texture(targetTexture); | ||
} | ||
|
||
Flip(); | ||
|
||
// Rebind our internal texture. | ||
if (targetTexture) | ||
{ | ||
m_framebuffer.GetAttachment(0, TextureAttachment::AttachmentType::Color, 0)->Texture(internalTexture); | ||
} | ||
|
||
Framebuffer::Unbind(); | ||
} | ||
|
||
void FlipTexture::Draw(const std::shared_ptr<Texture>& originalTexture, Framebuffer& framebuffer, int framebufferIndex) | ||
{ | ||
if (originalTexture == nullptr || framebuffer.GetColorAttachmentTexture(framebufferIndex, 0) == nullptr) | ||
{ | ||
return; | ||
} | ||
|
||
UpdateTextureSize(); | ||
|
||
if (m_viewportWidth == 0 || m_viewportHeight == 0) | ||
{ | ||
return; | ||
} | ||
|
||
m_framebuffer.Bind(0); | ||
|
||
// Draw from unflipped texture | ||
originalTexture->Bind(0); | ||
|
||
Flip(); | ||
|
||
// Swap texture attachments | ||
auto tempAttachment = framebuffer.GetAttachment(framebufferIndex, TextureAttachment::AttachmentType::Color, 0); | ||
framebuffer.RemoveColorAttachment(framebufferIndex, 0); | ||
framebuffer.SetAttachment(framebufferIndex, 0, m_framebuffer.GetAttachment(0, TextureAttachment::AttachmentType::Color, 0)); | ||
m_framebuffer.RemoveColorAttachment(0, 0); | ||
m_framebuffer.SetAttachment(0, 0, tempAttachment); | ||
|
||
Framebuffer::Unbind(); | ||
} | ||
|
||
auto FlipTexture::FlippedTexture() -> std::shared_ptr<Texture> | ||
{ | ||
return m_framebuffer.GetColorAttachmentTexture(0, 0); | ||
} | ||
|
||
void FlipTexture::UpdateTextureSize() | ||
{ | ||
if (m_viewportWidth == m_presetState.renderContext.viewportSizeX && | ||
m_viewportHeight == m_presetState.renderContext.viewportSizeY) | ||
{ | ||
return; | ||
} | ||
|
||
m_viewportWidth = m_presetState.renderContext.viewportSizeX; | ||
m_viewportHeight = m_presetState.renderContext.viewportSizeY; | ||
|
||
m_framebuffer.SetSize(m_viewportWidth, m_viewportHeight); | ||
} | ||
|
||
void FlipTexture::Flip() const | ||
{ | ||
m_presetState.texturedShader.Bind(); | ||
m_presetState.texturedShader.SetUniformMat4x4("vertex_transformation", PresetState::orthogonalProjection); | ||
m_presetState.texturedShader.SetUniformInt("texture_sampler", 0); | ||
|
||
m_sampler.Bind(0); | ||
|
||
glBindVertexArray(m_vaoID); | ||
glVertexAttrib4f(1, 1.0, 1.0, 1.0, 1.0); // Color | ||
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); | ||
glBindVertexArray(0); | ||
|
||
glBindTexture(GL_TEXTURE_2D, 0); | ||
Sampler::Unbind(0); | ||
Shader::Unbind(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#pragma once | ||
|
||
#include "PresetState.hpp" | ||
|
||
#include <Renderer/Framebuffer.hpp> | ||
#include <Renderer/RenderItem.hpp> | ||
|
||
/** | ||
* @class FlipTexture | ||
* @brief Flips the given input texture upside-down. | ||
* | ||
* Milkdrop uses HLSL, so the input and output UV coordinates in the draw call are upside-down because | ||
* DirectX has the origin at the top-left while OpenGL/Vulkan use the bottom-left. | ||
* | ||
* Some presets need the input, the calculated UVs and the output to be properly aligned, so at some | ||
* point, input textures must be flipped for the next rendering step. This class uses a simple draw | ||
* call with a pass-through shader to draw the same image 1:1, but vertically flipped. | ||
*/ | ||
class FlipTexture : public RenderItem | ||
{ | ||
public: | ||
FlipTexture() = delete; | ||
explicit FlipTexture(const PresetState& presetState); | ||
|
||
void InitVertexAttrib(); | ||
|
||
/** | ||
* @brief Flips the original texture either into the object's internal framebuffer or a given target texture. | ||
* The original and target textures must not be the same. | ||
* @param originalTexture The texture to be flipped. | ||
* @param targetTexture Optional target texture to draw onto. | ||
*/ | ||
void Draw(const std::shared_ptr<Texture>& originalTexture, const std::shared_ptr<Texture>& targetTexture = {}); | ||
|
||
/** | ||
* @brief Flips the texture bound the given framebuffer's first color attachment. | ||
* This is done by drawing into a second framebuffer, then swapping the textures, so the original texture | ||
* can be the current color attachment of targetFramebuffer. | ||
* @param originalTexture The texture to be flipped. | ||
* @param targetFramebuffer Optional target texture to draw onto. | ||
* @param framebufferIndex The index of the framebuffer to use. | ||
*/ | ||
void Draw(const std::shared_ptr<Texture>& originalTexture, Framebuffer& framebuffer, int framebufferIndex); | ||
|
||
/** | ||
* @brief Returns the flipped texture. | ||
* | ||
* @return The flipped texture. | ||
*/ | ||
auto FlippedTexture() -> std::shared_ptr<Texture>; | ||
|
||
private: | ||
/** | ||
* Updates the mesh | ||
*/ | ||
void UpdateTextureSize(); | ||
|
||
void Flip() const; | ||
|
||
const PresetState& m_presetState; //!< The global preset state. | ||
|
||
Framebuffer m_framebuffer{1}; //!< Framebuffer for drawing the flipped texture | ||
Sampler m_sampler{GL_CLAMP_TO_EDGE, GL_NEAREST}; //!< Texture sampler settings | ||
|
||
int m_viewportWidth{}; //!< Last known viewport width | ||
int m_viewportHeight{}; //!< Last known viewport height | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.