-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomOpenGLWidget.cpp
More file actions
285 lines (248 loc) · 8.68 KB
/
CustomOpenGLWidget.cpp
File metadata and controls
285 lines (248 loc) · 8.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include "CustomOpenGLWidget.h"
#include <QApplication>
#include <QDebug>
#include <QMouseEvent>
#define GET_GLSTR(x) #x
const char* vsrc = GET_GLSTR(
attribute vec4 inPosition;
attribute vec2 inTexCoord;
uniform int orientation;
uniform float offsetX;
uniform float offsetY;
varying vec2 texCoord;
void main(void)
{
gl_Position = inPosition + vec4(offsetX, offsetY, 0.0f, 0.0f);
// 根据旋转角度变换纹理坐标
vec2 rotatedTexCoords;
switch (orientation) {
case 1:
rotatedTexCoords = inTexCoord; // 不旋转
break;
case 2:
rotatedTexCoords = vec2(1.0 - inTexCoord.x, inTexCoord.y); // 水平翻转
break;
case 3:
rotatedTexCoords = vec2(1.0 - inTexCoord.x, 1.0 - inTexCoord.y); // 顺时针旋转180°
break;
case 4:
rotatedTexCoords = vec2(inTexCoord.x, 1.0 - inTexCoord.y); // 垂直翻转
break;
case 5:
rotatedTexCoords = vec2(1.0 - inTexCoord.y, inTexCoord.x); // 顺时针旋转90° + 水平翻转
break;
case 6:
rotatedTexCoords = vec2(inTexCoord.y, inTexCoord.x); // 顺时针旋转90°
break;
case 7:
rotatedTexCoords = vec2(1.0 - inTexCoord.y, 1.0 - inTexCoord.x); // 顺时针旋转90° + 垂直翻转
break;
case 8:
rotatedTexCoords = vec2(inTexCoord.y, 1.0 - inTexCoord.x); // 逆时针旋转90°
break;
default:
rotatedTexCoords = inTexCoord; // 不旋转
break;
}
texCoord = vec2(rotatedTexCoords.x, 1.0 - rotatedTexCoords.y);
}
);
const char* fsrc = GET_GLSTR(
varying vec2 texCoord;
uniform sampler2D tex;
void main(void)
{
gl_FragColor = texture2D(tex, texCoord);
}
);
CustomOpenGLWidget::CustomOpenGLWidget(QWidget* parent): QOpenGLWidget(parent)
{}
void CustomOpenGLWidget::mousePressEvent(QMouseEvent* event)
{
if (event->button() == Qt::LeftButton) {
m_dragMoveVariable.isPressed = true;
m_dragMoveVariable.lastX = event->pos().x();
m_dragMoveVariable.lastY = event->pos().y();
}
}
void CustomOpenGLWidget::wheelEvent(QWheelEvent* event)
{
if (event->angleDelta().y() > 10) {
if (m_scaleRatio == 0.01) {
m_scaleRatio = 0.1; // 凑整,避免从1%变成11%
} else {
m_scaleRatio += 0.1;
}
if (m_scaleRatio >= 10.0) { // 最大可放大至1000%
m_scaleRatio = 10.0;
}
} else {
m_scaleRatio -= 0.1;
if (m_scaleRatio <= 0.01) { // 最小可缩小至1%
m_scaleRatio = 0.01;
}
}
resizeGL(width(), height());
emit sig_scaleChanged(m_scaleRatio);
}
void CustomOpenGLWidget::mouseMoveEvent(QMouseEvent* event)
{
if (m_dragMoveVariable.isPressed) {
m_dragMoveVariable.offsetX += event->pos().x() - m_dragMoveVariable.lastX;
m_dragMoveVariable.offsetY += event->pos().y() - m_dragMoveVariable.lastY;
update();
}
m_dragMoveVariable.lastX = event->pos().x();
m_dragMoveVariable.lastY = event->pos().y();
}
void CustomOpenGLWidget::mouseReleaseEvent(QMouseEvent* event)
{
m_dragMoveVariable.isPressed = false;
}
void CustomOpenGLWidget::mouseDoubleClickEvent(QMouseEvent* event)
{
m_dragMoveVariable = DragMoveVariable();
m_scaleRatio = 1.0;
resizeGL(width(), height());
emit sig_scaleChanged(m_scaleRatio);
update();
}
void CustomOpenGLWidget::initializeGL()
{
initializeOpenGLFunctions();
static const GLfloat vertices[] {
// 顶点坐标
-1.0f, -1.0f,
-1.0f, +1.0f,
+1.0f, +1.0f,
+1.0f, -1.0f,
// 纹理坐标
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f,
1.0f, 0.0f
};
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GL_FLOAT), 0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GL_FLOAT), (void*)(8 * sizeof(GL_FLOAT)));
glEnableVertexAttribArray(1);
// 创建着色器程序
program = glCreateProgram();
// 读取顶点着色器代码
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vsrc, nullptr);
glCompileShader(vertexShader);
// 读取片段着色器代码
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fsrc, nullptr);
glCompileShader(fragmentShader);
// 绑定代码,编译链接
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
glLinkProgram(program);
glValidateProgram(program);
glUseProgram(program);
glBindAttribLocation(program, 0, "inPosition");
glBindAttribLocation(program, 1, "inTexCoord");
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glClearColor(240 / 255.0, 240 / 255.0, 240 / 255.0, 0.0);
}
void CustomOpenGLWidget::resizeGL(int w, int h)
{
if (m_image == nullptr) {
return;
}
double ratio = devicePixelRatio();
// 计算窗口的实际尺寸
int windowWidth = w * ratio;
int windowHeight = h * ratio;
// 计算宽度的缩放比例
double scaleWidth = (double)windowWidth / (m_isRotated ? m_image->height : m_image->width);
// 计算高度的缩放比例
double scaleHeight = (double)windowHeight / (m_isRotated ? m_image->width : m_image->height);
// 选取较小的缩放比例以保证图片完整的显示在窗口中,同时不改变图片的宽高比
double scale = std::min(scaleWidth, scaleHeight);
// 计算缩放后的图像尺寸
m_viewportWidth = (m_isRotated ? m_image->height : m_image->width) * scale * m_scaleRatio;
m_viewportHeight = (m_isRotated ? m_image->width : m_image->height) * scale * m_scaleRatio;
// 计算图像在窗口中的显示位置
m_horizontalOffset = (windowWidth - m_viewportWidth) / 2;
m_verticalOffset = (windowHeight - m_viewportHeight) / 2;
update();
}
void CustomOpenGLWidget::paintGL()
{
if (m_image == nullptr) {
return;
}
glViewport(m_horizontalOffset, m_verticalOffset, m_viewportWidth, m_viewportHeight);
glBindTexture(GL_TEXTURE_2D, texture);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
if (m_image->channels == 3) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_image->width, m_image->height, 0, GL_RGB, GL_UNSIGNED_BYTE, m_image->pixels);
} else if (m_image->channels == 4){
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_image->width, m_image->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_image->pixels);
}
glUniform1f(glGetUniformLocation(program, "offsetX"), m_dragMoveVariable.offsetX / m_image->width);
glUniform1f(glGetUniformLocation(program, "offsetY"), -1.0 * m_dragMoveVariable.offsetY / m_image->height);
glUniform1i(glGetUniformLocation(program, "orientation"), m_orientation);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
}
void CustomOpenGLWidget::slot_showImage(std::shared_ptr<ImageData>& imageData, int orientation)
{
m_image = imageData;
m_orientation = orientation;
if (m_orientation >= 5 && m_orientation <= 8) {
m_isRotated = true;
} else {
m_isRotated = false;
}
slot_resizeViewport();
update();
}
void CustomOpenGLWidget::slot_resizeViewport()
{
resizeGL(width(), height());
}
void CustomOpenGLWidget::slot_changeScale(int flag)
{
if (flag == 1) { // 放大
if (m_scaleRatio == 0.01) {
m_scaleRatio = 0.1; // 凑整,避免从1%变成11%
} else {
m_scaleRatio += 0.1;
}
if (m_scaleRatio >= 10.0) { // 最大可放大至1000%
m_scaleRatio = 10.0;
}
} else { // 缩小
m_scaleRatio -= 0.1;
if (m_scaleRatio <= 0.01) { // 最小可缩小至1%
m_scaleRatio = 0.01;
}
}
resizeGL(width(), height());
emit sig_scaleChanged(m_scaleRatio);
}
void CustomOpenGLWidget::slot_rotateImage()
{
m_orientation++;
if (m_orientation > 8) {
m_orientation = 1;
}
if (m_orientation >= 5 && m_orientation <= 8) {
m_isRotated = true;
} else {
m_isRotated = false;
}
slot_resizeViewport();
update();
}