-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathearth_renderer.cpp
More file actions
245 lines (194 loc) · 8.31 KB
/
earth_renderer.cpp
File metadata and controls
245 lines (194 loc) · 8.31 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
// earth_renderer.cpp
#include "earth_renderer.h"
#include <QDebug>
#include <QtMath>
#include <QCoreApplication>
EarthRenderer::EarthRenderer(float earthRadius)
: radius(earthRadius)
{
}
EarthRenderer::~EarthRenderer() {
if (vbo.isCreated())
vbo.destroy();
if (ibo.isCreated())
ibo.destroy();
if (vao.isCreated())
vao.destroy();
atmosphereRenderer.reset();
}
void EarthRenderer::initialize() {
if (!init()) {
qDebug() << "Failed to initialize OpenGL functions for EarthRenderer";
return;
}
initShaders();
initTextures();
initGeometry();
// Инициализируем атмосферу с тем же радиусом
atmosphereRenderer = std::make_unique<AtmosphereRenderer>(radius);
atmosphereRenderer->initialize();
}
void EarthRenderer::initShaders() {
if (!program.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/earth_vertex.glsl")) {
qDebug() << "Failed to compile vertex shader";
return;
}
if (!program.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/earth_fragment.glsl")) {
qDebug() << "Failed to compile fragment shader";
return;
}
if (!program.link()) {
qDebug() << "Failed to link shader program";
return;
}
}
void EarthRenderer::initTextures() {
QString buildDir = QCoreApplication::applicationDirPath();
earthTextureTiles = std::make_unique<TileTextureManager>(
buildDir + "/textures/earth.jpg", RINGS, SEGMENTS);
heightMapTiles = std::make_unique<TileTextureManager>(
buildDir + "/textures/earth_height.png", RINGS, SEGMENTS);
normalMapTiles = std::make_unique<TileTextureManager>(
buildDir + "/textures/earth_normal.png", RINGS, SEGMENTS);
earthTextureTiles->initialize();
heightMapTiles->initialize();
normalMapTiles->initialize();
// Новые текстуры
nightLightsTiles = std::make_unique<TileTextureManager>(
buildDir + "/textures/earth_night.jpg", RINGS, SEGMENTS);
cloudTiles = std::make_unique<TileTextureManager>(
buildDir + "/textures/earth_clouds.jpg", RINGS, SEGMENTS);
specularTiles = std::make_unique<TileTextureManager>(
buildDir + "/textures/earth_specular.jpg", RINGS, SEGMENTS);
temperatureTiles = std::make_unique<TileTextureManager>(
buildDir + "/textures/earth_temperature.jpg", RINGS, SEGMENTS);
snowTiles = std::make_unique<TileTextureManager>(
buildDir + "/textures/earth_snow.jpg", RINGS, SEGMENTS);
nightLightsTiles->initialize();
cloudTiles->initialize();
specularTiles->initialize();
temperatureTiles->initialize();
snowTiles->initialize();
}
void EarthRenderer::initGeometry() {
vao.create();
vao.bind();
vbo.create();
ibo.create();
createSphere();
vao.release();
}
void EarthRenderer::render(const QMatrix4x4& projection, const QMatrix4x4& view, const QMatrix4x4& model) {
if (!program.bind())
return;
vao.bind();
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glDepthFunc(GL_LESS);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
// Установка матриц
program.setUniformValue("projectionMatrix", projection);
program.setUniformValue("viewMatrix", view);
program.setUniformValue("modelMatrix", model);
// Установка параметров освещения
QVector3D cameraPos = view.inverted().column(3).toVector3D();
program.setUniformValue("viewPos", cameraPos);
program.setUniformValue("lightPos", cameraPos); // или другая позиция источника света
// Важно! Установка масштаба высоты
program.setUniformValue("heightScale", 0.05f);
// Привязываем все текстуры один раз
glActiveTexture(GL_TEXTURE0);
earthTextureTiles->bindTileTexture(0, 0); // Привязываем атлас текстур
program.setUniformValue("earthTexture", 0);
glActiveTexture(GL_TEXTURE1);
heightMapTiles->bindTileTexture(0, 0);
program.setUniformValue("heightMap", 1);
glActiveTexture(GL_TEXTURE2);
normalMapTiles->bindTileTexture(0, 0);
program.setUniformValue("normalMap", 2);
// Новые текстуры
glActiveTexture(GL_TEXTURE3);
nightLightsTiles->bindTileTexture(0, 0);
program.setUniformValue("nightLightMap", 3);
// glActiveTexture(GL_TEXTURE4);
// cloudTiles->bindTileTexture(0, 0);
// program.setUniformValue("cloudMap", 4);
glActiveTexture(GL_TEXTURE5);
specularTiles->bindTileTexture(0, 0);
program.setUniformValue("specularMap", 5);
glActiveTexture(GL_TEXTURE6);
temperatureTiles->bindTileTexture(0, 0);
program.setUniformValue("temperatureMap", 6);
glActiveTexture(GL_TEXTURE7);
snowTiles->bindTileTexture(0, 0);
program.setUniformValue("snowMap", 7);
// Рендерим всю геометрию за один draw call
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, nullptr);
vao.release();
program.release();
// if (atmosphereRenderer) {
atmosphereRenderer->render(projection, view, model);
// }
}
void EarthRenderer::createSphere() {
vertices.clear();
indices.clear();
for (int ring = 0; ring < RINGS; ++ring) {
float phi1 = M_PI * float(ring) / RINGS;
float phi2 = M_PI * float(ring + 1) / RINGS;
for (int segment = 0; segment < SEGMENTS; ++segment) {
float theta1 = 2.0f * M_PI * float(segment) / SEGMENTS;
float theta2 = 2.0f * M_PI * float(segment + 1) / SEGMENTS;
QVector3D v1 = sphericalToCartesian(radius, phi1, theta1);
QVector3D v2 = sphericalToCartesian(radius, phi1, theta2);
QVector3D v3 = sphericalToCartesian(radius, phi2, theta2);
QVector3D v4 = sphericalToCartesian(radius, phi2, theta1);
// Получаем UV-координаты из атласа текстур
QRectF uvCoords = earthTextureTiles->getTileUVCoords(ring, segment);
QVector2D uv1(uvCoords.left(), uvCoords.top());
QVector2D uv2(uvCoords.right(), uvCoords.top());
QVector2D uv3(uvCoords.right(), uvCoords.bottom());
QVector2D uv4(uvCoords.left(), uvCoords.bottom());
// Нормали
QVector3D n1 = v1.normalized();
QVector3D n2 = v2.normalized();
QVector3D n3 = v3.normalized();
QVector3D n4 = v4.normalized();
int baseIndex = vertices.size();
vertices.append(Vertex{v1, uv1, n1, QVector2D(ring, segment)});
vertices.append(Vertex{v2, uv2, n2, QVector2D(ring, segment)});
vertices.append(Vertex{v3, uv3, n3, QVector2D(ring, segment)});
vertices.append(Vertex{v4, uv4, n4, QVector2D(ring, segment)});
indices.append(baseIndex);
indices.append(baseIndex + 1);
indices.append(baseIndex + 2);
indices.append(baseIndex);
indices.append(baseIndex + 2);
indices.append(baseIndex + 3);
}
}
vbo.bind();
vbo.allocate(vertices.constData(), vertices.size() * sizeof(Vertex));
ibo.bind();
ibo.allocate(indices.constData(), indices.size() * sizeof(GLuint));
program.enableAttributeArray("position");
program.setAttributeBuffer("position", GL_FLOAT, offsetof(Vertex, position), 3, sizeof(Vertex));
program.enableAttributeArray("texCoord");
program.setAttributeBuffer("texCoord", GL_FLOAT, offsetof(Vertex, texCoord), 2, sizeof(Vertex));
program.enableAttributeArray("normal");
program.setAttributeBuffer("normal", GL_FLOAT, offsetof(Vertex, normal), 3, sizeof(Vertex));
program.enableAttributeArray("tileCoord");
program.setAttributeBuffer("tileCoord", GL_FLOAT, offsetof(Vertex, tileCoord), 2, sizeof(Vertex));
}
void EarthRenderer::updateVisibleTiles(const QMatrix4x4& viewProjection) {
earthTextureTiles->updateVisibleTiles(viewProjection);
heightMapTiles->updateVisibleTiles(viewProjection);
normalMapTiles->updateVisibleTiles(viewProjection);
}
QVector3D EarthRenderer::sphericalToCartesian(float radius, float phi, float theta) const {
float x = radius * sin(phi) * cos(theta);
float y = radius * cos(phi);
float z = radius * sin(phi) * sin(theta);
return QVector3D(x, y, z);
}