1
+ #include " FlipTexture.hpp"
2
+
3
+ FlipTexture::FlipTexture (const PresetState& presetState)
4
+ : RenderItem()
5
+ , m_presetState(presetState)
6
+ {
7
+ RenderItem::Init ();
8
+
9
+ m_framebuffer.CreateColorAttachment (0 , 0 );
10
+ }
11
+
12
+ void FlipTexture::InitVertexAttrib ()
13
+ {
14
+ glEnableVertexAttribArray (0 );
15
+ glDisableVertexAttribArray (1 );
16
+ glEnableVertexAttribArray (2 );
17
+
18
+ glVertexAttribPointer (0 , 2 , GL_FLOAT, GL_FALSE, sizeof (TexturedPoint), reinterpret_cast <void *>(offsetof (TexturedPoint, x))); // Position
19
+ glVertexAttribPointer (2 , 2 , GL_FLOAT, GL_FALSE, sizeof (TexturedPoint), reinterpret_cast <void *>(offsetof (TexturedPoint, u))); // Texture coordinate
20
+
21
+ std::array<RenderItem::TexturedPoint, 4 > points;
22
+
23
+ points[0 ].x = -1.0 ;
24
+ points[0 ].y = 1.0 ;
25
+ points[1 ].x = 1.0 ;
26
+ points[1 ].y = 1.0 ;
27
+ points[2 ].x = -1.0 ;
28
+ points[2 ].y = -1.0 ;
29
+ points[3 ].x = 1.0 ;
30
+ points[3 ].y = -1.0 ;
31
+
32
+ points[0 ].u = 0.0 ;
33
+ points[0 ].v = 1.0 ;
34
+ points[1 ].u = 1.0 ;
35
+ points[1 ].v = 1.0 ;
36
+ points[2 ].u = 0.0 ;
37
+ points[2 ].v = 0.0 ;
38
+ points[3 ].u = 1.0 ;
39
+ points[3 ].v = 0.0 ;
40
+
41
+ glBufferData (GL_ARRAY_BUFFER, sizeof (points), points.data (), GL_STATIC_DRAW);
42
+ }
43
+
44
+ void FlipTexture::Draw (const std::shared_ptr<Texture>& originalTexture, const std::shared_ptr<Texture>& targetTexture)
45
+ {
46
+ if (originalTexture == nullptr || originalTexture == targetTexture)
47
+ {
48
+ return ;
49
+ }
50
+
51
+ UpdateTextureSize ();
52
+
53
+ if (m_viewportWidth == 0 || m_viewportHeight == 0 )
54
+ {
55
+ return ;
56
+ }
57
+
58
+ std::shared_ptr<Texture> internalTexture;
59
+
60
+ m_framebuffer.Bind (0 );
61
+
62
+ // Draw from unflipped texture
63
+ originalTexture->Bind (0 );
64
+
65
+ if (targetTexture)
66
+ {
67
+ internalTexture = m_framebuffer.GetColorAttachmentTexture (0 , 0 );
68
+ m_framebuffer.GetAttachment (0 , TextureAttachment::AttachmentType::Color, 0 )->Texture (targetTexture);
69
+ }
70
+
71
+ Flip ();
72
+
73
+ // Rebind our internal texture.
74
+ if (targetTexture)
75
+ {
76
+ m_framebuffer.GetAttachment (0 , TextureAttachment::AttachmentType::Color, 0 )->Texture (internalTexture);
77
+ }
78
+
79
+ Framebuffer::Unbind ();
80
+ }
81
+
82
+ void FlipTexture::Draw (const std::shared_ptr<Texture>& originalTexture, Framebuffer& framebuffer, int framebufferIndex)
83
+ {
84
+ if (originalTexture == nullptr || framebuffer.GetColorAttachmentTexture (framebufferIndex, 0 ) == nullptr )
85
+ {
86
+ return ;
87
+ }
88
+
89
+ UpdateTextureSize ();
90
+
91
+ if (m_viewportWidth == 0 || m_viewportHeight == 0 )
92
+ {
93
+ return ;
94
+ }
95
+
96
+ m_framebuffer.Bind (0 );
97
+
98
+ // Draw from unflipped texture
99
+ originalTexture->Bind (0 );
100
+
101
+ Flip ();
102
+
103
+ // Swap texture attachments
104
+ auto tempAttachment = framebuffer.GetAttachment (framebufferIndex, TextureAttachment::AttachmentType::Color, 0 );
105
+ framebuffer.RemoveColorAttachment (framebufferIndex, 0 );
106
+ framebuffer.SetAttachment (framebufferIndex, 0 , m_framebuffer.GetAttachment (0 , TextureAttachment::AttachmentType::Color, 0 ));
107
+ m_framebuffer.RemoveColorAttachment (0 , 0 );
108
+ m_framebuffer.SetAttachment (0 , 0 , tempAttachment);
109
+
110
+ Framebuffer::Unbind ();
111
+ }
112
+
113
+ auto FlipTexture::FlippedTexture () -> std::shared_ptr<Texture>
114
+ {
115
+ return m_framebuffer.GetColorAttachmentTexture (0 , 0 );
116
+ }
117
+
118
+ void FlipTexture::UpdateTextureSize ()
119
+ {
120
+ if (m_viewportWidth == m_presetState.renderContext .viewportSizeX &&
121
+ m_viewportHeight == m_presetState.renderContext .viewportSizeY )
122
+ {
123
+ return ;
124
+ }
125
+
126
+ m_viewportWidth = m_presetState.renderContext .viewportSizeX ;
127
+ m_viewportHeight = m_presetState.renderContext .viewportSizeY ;
128
+
129
+ m_framebuffer.SetSize (m_viewportWidth, m_viewportHeight);
130
+ }
131
+
132
+ void FlipTexture::Flip () const
133
+ {
134
+ m_presetState.texturedShader .Bind ();
135
+ m_presetState.texturedShader .SetUniformMat4x4 (" vertex_transformation" , PresetState::orthogonalProjection);
136
+ m_presetState.texturedShader .SetUniformInt (" texture_sampler" , 0 );
137
+
138
+ m_sampler.Bind (0 );
139
+
140
+ glBindVertexArray (m_vaoID);
141
+ glVertexAttrib4f (1 , 1.0 , 1.0 , 1.0 , 1.0 ); // Color
142
+ glDrawArrays (GL_TRIANGLE_STRIP, 0 , 4 );
143
+ glBindVertexArray (0 );
144
+
145
+ glBindTexture (GL_TEXTURE_2D, 0 );
146
+ Sampler::Unbind (0 );
147
+ Shader::Unbind ();
148
+ }
0 commit comments