3#include "../../global.hpp"
5#include "../_decl/decl_opengl_image_renderer.hpp"
21 QVector2D(
static_cast<float>(sourceRect.left()),
static_cast<float>(sourceRect.top()));
23 QVector2D(
static_cast<float>(sourceRect.left()),
static_cast<float>(sourceRect.bottom()));
25 QVector2D(
static_cast<float>(sourceRect.right()),
static_cast<float>(sourceRect.bottom()));
27 QVector2D(
static_cast<float>(sourceRect.right()),
static_cast<float>(sourceRect.top()));
29 auto topAdjust =
static_cast<float>(sourceRect.top() - qFloor(sourceRect.top()));
30 auto leftAdjust =
static_cast<float>(sourceRect.left() - qFloor(sourceRect.left()));
31 auto bottomAdjust =
static_cast<float>(qCeil(sourceRect.bottom()) - sourceRect.bottom());
32 auto rightAdjust =
static_cast<float>(qCeil(sourceRect.right()) - sourceRect.right());
34 if (textureRect.Width() && textureRect.Height())
36 topAdjust /=
static_cast<float>(textureRect.Height());
37 leftAdjust /=
static_cast<float>(textureRect.Width());
38 bottomAdjust /=
static_cast<float>(textureRect.Height());
39 rightAdjust /=
static_cast<float>(textureRect.Width());
42 coordArray.
TopLeft.
TexPoint = QVector2D(0.0f + leftAdjust, 0.0f + topAdjust);
45 coordArray.
TopRight.
TexPoint = QVector2D(1.0f - rightAdjust, 0.0f + topAdjust);
49 coordBuffer_->Release();
54 if (!coordBuffer_ || !texture_)
59 float r_l =
static_cast<float>(rect.right() - rect.left());
60 float t_b =
static_cast<float>(rect.top() - rect.bottom());
62 float tx =
static_cast<float>(-(rect.right() + rect.left()) / (rect.right() - rect.left()));
63 float ty =
static_cast<float>(-(rect.top() + rect.bottom()) / (rect.top() - rect.bottom()));
64 float tz = -(fr + nr) / (fr - nr);
67 auto modelViewData = modelView.data();
69 modelViewData[0] = 2.0f / r_l;
70 modelViewData[1] = 0.0f;
71 modelViewData[2] = 0.0f;
72 modelViewData[3] = 0.0f;
74 modelViewData[4] = 0.0f;
75 modelViewData[5] = 2.0f / t_b;
76 modelViewData[6] = 0.0f;
77 modelViewData[7] = 0.0f;
79 modelViewData[8] = 0.0f;
80 modelViewData[9] = 0.0f;
81 modelViewData[10] = 2.0f / f_n;
82 modelViewData[11] = 0.0f;
84 modelViewData[12] = tx;
85 modelViewData[13] = ty;
86 modelViewData[14] = tz;
87 modelViewData[15] = 1.0;
93 program_.setUniformValue(unifModelView_, modelView);
94 program_.setUniformValue(unifTexture_, 0);
95 program_.enableAttributeArray(attrVertex_);
96 program_.enableAttributeArray(attrTexcoord_);
98 program_.setAttributeBuffer(attrVertex_, GL_FLOAT, 0, 2,
99 4 *
sizeof(GL_FLOAT));
100 program_.setAttributeBuffer(attrTexcoord_, GL_FLOAT, 2 *
sizeof(GL_FLOAT), 2,
101 4 *
sizeof(GL_FLOAT));
103 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
105 program_.disableAttributeArray(attrTexcoord_);
106 program_.disableAttributeArray(attrVertex_);
110 coordBuffer_->Release();
113 inline OpenGLImageRenderer::OpenGLImageRenderer() noexcept
115 initializeOpenGLFunctions();
117 program_.addShaderFromSourceCode(QOpenGLShader::Vertex,
119 "// Set default precision to medium\n"
120 "precision mediump int;\n"
121 "precision mediump float;\n"
124 "uniform mat4 matrix;"
125 "attribute vec2 coord2d;\n"
126 "attribute vec2 texcoord;\n"
127 "varying vec2 f_texcoord;\n"
130 " gl_Position = matrix * vec4(coord2d, 0.0, 1.0);\n"
131 " f_texcoord = texcoord;\n"
134 program_.addShaderFromSourceCode(QOpenGLShader::Fragment,
136 "// Set default precision to medium\n"
137 "precision mediump int;\n"
138 "precision mediump float;\n"
141 "varying vec2 f_texcoord;"
142 "uniform sampler2D texture;"
145 "gl_FragColor = texture2D(texture, f_texcoord);"
150 attrVertex_ = program_.attributeLocation(
"coord2d");
151 attrTexcoord_ = program_.attributeLocation(
"texcoord");
152 unifModelView_ = program_.uniformLocation(
"matrix");
153 unifTexture_ = program_.uniformLocation(
"texture");
157 coordBuffer_.reset(
new (std::nothrow) GLCoordBuffer);
160 coordBuffer_->Bind();
162 coordBuffer_->Data(
static_cast<int>(
sizeof(
OpenGLCoordArray)), &coordArray);
163 coordBuffer_->Release();
166 texture_.reset(
new (std::nothrow) GLTexture);
171 inline void OpenGLImageRenderer::BindTexture() noexcept
176 inline void OpenGLImageRenderer::ReleaseTexture() noexcept
181 inline void OpenGLImageRenderer::Image2DTexture(
const OpenGLBufferFormat &pixelBufferFormat,
void *pixels,
182 bool swizzleSupport)
noexcept
184 texture_->Image2D(pixelBufferFormat, pixels, swizzleSupport);
187 inline void OpenGLImageRenderer::Image2DTexture(
void *pixels,
bool swizzleSupport)
noexcept
189 texture_->Image2D(pixels, swizzleSupport);
192 inline OpenGLImageRenderer::GLTexture::GLTexture() noexcept
194 initializeOpenGLFunctions();
195 glGenTextures(1, &handle_);
198 inline OpenGLImageRenderer::GLTexture::~GLTexture()
200 glDeleteTextures(1, &handle_);
203 inline void OpenGLImageRenderer::GLTexture::Bind() noexcept
205 glBindTexture(GL_TEXTURE_2D, handle_);
208 inline void OpenGLImageRenderer::GLTexture::Release() noexcept
210 glBindTexture(GL_TEXTURE_2D, 0);
213 inline void OpenGLImageRenderer::GLTexture::Image2D(
const OpenGLBufferFormat &pixelBufferFormat,
void *pixels,
214 bool swizzleSupport)
noexcept
216 pixelBufferFormat_ = pixelBufferFormat;
217 Image2D(pixels, swizzleSupport);
220 inline void OpenGLImageRenderer::GLTexture::Image2D(
void *pixels,
bool swizzleSupport)
noexcept
222 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
223 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
230 std::array<GLint, 4> swizzleMask = {GL_RED, GL_RED, GL_RED, GL_ALPHA};
231 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, &swizzleMask[0]);
232 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, &swizzleMask[1]);
233 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, &swizzleMask[2]);
234 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, &swizzleMask[3]);
238 std::array<GLint, 4> swizzleMask = {GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA};
239 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, &swizzleMask[0]);
240 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, &swizzleMask[1]);
241 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, &swizzleMask[2]);
242 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, &swizzleMask[3]);
246 glTexImage2D(GL_TEXTURE_2D, 0,
248 static_cast<GLsizei
>(pixelBufferFormat_.Width),
static_cast<GLsizei
>(pixelBufferFormat_.Height), 0,
250 GL_UNSIGNED_BYTE, pixels);
253 inline OpenGLImageRenderer::GLCoordBuffer::GLCoordBuffer() noexcept
255 initializeOpenGLFunctions();
256 glGenBuffers(1, &handle_);
259 inline OpenGLImageRenderer::GLCoordBuffer::~GLCoordBuffer()
261 glDeleteBuffers(1, &handle_);
264 inline void OpenGLImageRenderer::GLCoordBuffer::Bind() noexcept
266 glBindBuffer(GL_ARRAY_BUFFER, handle_);
269 inline void OpenGLImageRenderer::GLCoordBuffer::Release() noexcept
271 glBindBuffer(GL_ARRAY_BUFFER, 0);
274 inline void OpenGLImageRenderer::GLCoordBuffer::Data(
int size,
void *buffer)
noexcept
276 glBufferData(GL_ARRAY_BUFFER,
static_cast<qopengl_GLsizeiptr
>(size), buffer, GL_STREAM_DRAW);
Stores a pair of numbers that represents the width and the height of a subject, typically a rectangle...
Definition size_2d.hpp:20
void Render(const QRectF &viewportRect) noexcept
Actually renders the image.
Definition detail_opengl_image_renderer.hpp:52
void UpdateMaping(const QRectF &sourceRect, Size2D< int > textureSize) noexcept
Updates the vertex and texture coordinates before rendering.
Definition detail_opengl_image_renderer.hpp:15
Namespace for user interface components.
Definition decl_image_scene.hpp:39
@ Mono
Definition ui.hpp:144
@ RGB
Definition ui.hpp:140
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
Set of all coordinates required to render a texture.
Definition ui.hpp:217
OpenGLCoord BottomRight
Definition ui.hpp:229
OpenGLCoord TopRight
Definition ui.hpp:233
OpenGLCoord TopLeft
Definition ui.hpp:221
OpenGLCoord BottomLeft
Definition ui.hpp:225
QVector2D VerPoint
Definition ui.hpp:206
QVector2D TexPoint
Definition ui.hpp:210