-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTRenderer.cpp
More file actions
269 lines (231 loc) · 7.13 KB
/
Copy pathTRenderer.cpp
File metadata and controls
269 lines (231 loc) · 7.13 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
#include "TRenderer.h"
#include "ui_TRenderer.h"
#include <QTextStream>
static inline QString GenerateStyleSheet(QColor color)
{
return "background-color: rgb("
+ QString::number(static_cast<int>(color.red())) + ','
+ QString::number(static_cast<int>(color.green())) + ','
+ QString::number(static_cast<int>(color.blue())) + ");";
}
TRenderer::TRenderer(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::TRenderer)
{
ui->setupUi(this);
InitUI();
InitSignalAndSlot();
}
TRenderer::~TRenderer()
{
delete ui;
}
// 设置界面的样式, 包括背景颜色、线条颜色、点颜色等
void TRenderer::SetStyle(Style style)
{
QFile f;
if(style == LIGHT)
{
f.setFileName(":/qdarkstyle/light/style.qss");
ui->RenderWidget->SetRenderColor({0.98f, 0.98f, 0.98f}, BACKGROUND);
ui->RenderWidget->SetRenderColor({0.098f, 0.137f, 0.176f}, LINE);
ui->RenderWidget->SetRenderColor({0.098f, 0.137f, 0.176f}, POINT);
ui->actionLight->setChecked(true);
ui->actionDark->setChecked(false);
}
else
{
f.setFileName(":/qdarkstyle/dark/style.qss");
ui->RenderWidget->SetRenderColor({0.098f, 0.137f, 0.176f}, BACKGROUND);
ui->RenderWidget->SetRenderColor({0.98f, 0.98f, 0.98f}, LINE);
ui->RenderWidget->SetRenderColor({0.98f, 0.98f, 0.98f}, POINT);
ui->actionDark->setChecked(true);
ui->actionLight->setChecked(false);
}
f.open(QFile::ReadOnly | QFile::Text);
QTextStream ts(&f);
qApp->setStyleSheet(ts.readAll());
}
// 设置渲染选项, 多线程渲染和面剔除
void TRenderer::SetOption(Option option, bool val)
{
if(option == MUTITHREAD)
{
ui->actionMultiThread->setChecked(val);
ui->RenderWidget->SetMultiThread(val);
}
else
{
ui->actionFaceCulling->setChecked(val);
ui->RenderWidget->SetFaceCulling(val);
}
}
// 设置光照的颜色, 包括环境光、漫反射光和镜面光
void TRenderer::SetLightColor(LightColorType type, QColor color)
{
switch(type)
{
case SPECULAR:
ui->SpecularColor->setStyleSheet(GenerateStyleSheet(color));
specularColor = color;
break;
case DIFFUSE:
ui->DiffuseColor->setStyleSheet(GenerateStyleSheet(color));
diffuseColor = color;
break;
case AMBIENT:
ui->AmbientColor->setStyleSheet(GenerateStyleSheet(color));
ambientColor = color;
break;
}
// 更新对应的UI控件样式
ui->RenderWidget->SetLightColor({color.red() / 255.f,
color.green() / 255.f,
color.blue() / 255.f}, type);
}
// 设置相机的参数, 包括视角FOV和近裁剪平面距离
void TRenderer::SetCameraPara(CameraPara para, float val)
{
if(para == FOV)
{
ui->FovLabel->setText(QString::number(static_cast<int>(val)));
ui->RenderWidget->camera.fov = val;
}
else
{
ui->NearLabel->setText(QString::number(val, 'f', 1));
ui->RenderWidget->camera.zNear = val;
}
}
// 计算光照的方向
void TRenderer::SetLightDir()
{
Vector3D lightDir;
// 俯仰角
float pitch = glm::radians(glm::clamp(static_cast<float>(ui->PitchSlider->value()), -89.9f, 89.9f));
// 偏航角
float yaw = -glm::radians(static_cast<float>(ui->YawDial->value()));
// 俯仰角和偏航角
lightDir.x = (1.f * (float)std::cos(pitch) * (float)std::sin(yaw));
lightDir.y = (1.f * (float)std::sin(pitch));
lightDir.z = (1.f * (float)std::cos(pitch) * (float)std::cos(yaw));
// 设置w分量为0来确保在进行平移变换时, 方向向量不会发生偏移, 仅影响旋转和缩放
ui->RenderWidget->SetLightDir(Vector4D(-lightDir, 0.f));
}
void TRenderer::on_actionLight_triggered()
{
SetStyle(LIGHT);
}
void TRenderer::on_actionDark_triggered()
{
SetStyle(DARK);
}
void TRenderer::on_actionopen_file_triggered()
{
QString filePath = QFileDialog::getOpenFileName(this, "Open Model File", "", "OBJ(*.obj)");
if(!filePath.isEmpty())
ui->RenderWidget->LoadModel(filePath);
}
void TRenderer::on_actionsave_image_triggered()
{
QString fileName = QFileDialog::getSaveFileName(this, "Save Image", "", "PNG(*.png)");
if(!fileName.isEmpty())
ui->RenderWidget->SaveImage(fileName);
}
// 初始化渲染器参数
void TRenderer::InitUI()
{
setFixedSize(1120,672);
ui->Setting->setTabText(0, "model");
ui->Setting->setTabText(1, "light");
SetStyle(LIGHT);
SetOption(MUTITHREAD,true);
SetOption(FACECULLING,true);
SetCameraPara(FOV, 60.f);
SetCameraPara(NEAR, 1.0f);
SetLightColor(AMBIENT, QColor(102, 102, 102));
SetLightColor(DIFFUSE, QColor(153, 153, 153));
SetLightColor(SPECULAR, QColor(255, 255, 255));
SetLightDir();
QColorDialog::setCustomColor(0,QColor(255, 255, 255));
QColorDialog::setCustomColor(2,QColor(153, 153, 153));
QColorDialog::setCustomColor(4,QColor(102, 102, 102));
ui->VertexCheckBox->setChecked(true);
}
void TRenderer::InitSignalAndSlot()
{
connect(ui->RenderWidget, &TRendererWidget::SendModelData,this,
[this](int triangleCount, int vertexCount)
{
ui->TriangleNumberLabel->setText(QString::number(triangleCount));
ui->VertexNumberLabel->setText(QString::number(vertexCount));
});
}
void TRenderer::on_LineCheckBox_clicked()
{
if(ui->LineCheckBox->isChecked())
{
if(ui->VertexCheckBox->isChecked())
ui->VertexCheckBox->setChecked(false);
ui->RenderWidget->SetRenderMode(EDGE);
}
else
{
ui->RenderWidget->SetRenderMode(FACE);
}
}
void TRenderer::on_VertexCheckBox_clicked()
{
if(ui->VertexCheckBox->isChecked())
{
if(ui->LineCheckBox->isChecked())
ui->LineCheckBox->setChecked(false);
ui->RenderWidget->SetRenderMode(VERTEX);
}
else
{
ui->RenderWidget->SetRenderMode(FACE);
}
}
void TRenderer::on_actionMultiThread_triggered()
{
SetOption(MUTITHREAD, ui->actionMultiThread->isChecked());
}
void TRenderer::on_actionFaceCulling_triggered()
{
SetOption(FACECULLING, ui->actionFaceCulling->isChecked());
}
void TRenderer::on_FovSilder_valueChanged(int value)
{
SetCameraPara(FOV, static_cast<float>(value));
}
void TRenderer::on_NearSilder_valueChanged(int value)
{
SetCameraPara(NEAR, value / 10.f);
}
void TRenderer::on_SpecularColorSet_clicked()
{
QColor color = QColorDialog::getColor(specularColor,this,"Select Specular Color");
if(color.isValid())
SetLightColor(SPECULAR, color);
}
void TRenderer::on_DiffuseColorSet_clicked()
{
QColor color = QColorDialog::getColor(diffuseColor,this,"Select Diffuse Color");
if(color.isValid())
SetLightColor(DIFFUSE, color);
}
void TRenderer::on_AmbientColorSet_clicked()
{
QColor color = QColorDialog::getColor(ambientColor,this,"Select Ambient Color");
if(color.isValid())
SetLightColor(AMBIENT, color);
}
void TRenderer::on_PitchSlider_valueChanged(int value)
{
SetLightDir();
}
void TRenderer::on_YawDial_valueChanged(int value)
{
SetLightDir();
}