-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlandscape.cpp
More file actions
1060 lines (793 loc) · 32.6 KB
/
landscape.cpp
File metadata and controls
1060 lines (793 loc) · 32.6 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "landscape.h"
#include "utils.h"
#include "camera3d.h"
#include <QThread>
Landscape::Landscape(uint width, uint height, uint blockSize, Mfunc generateId) :
m_width(width), m_length(height), m_blockSize(blockSize),
m_indexBuffer(QOpenGLBuffer::IndexBuffer),
m_vertexBuffer(QOpenGLBuffer::VertexBuffer)
{
m_landscapeMaterial.Diffuse = QVector3D(0.01f, 0.01f, 0.01f);
m_landscapeMaterial.Ambient = QVector3D(0.33f, 0.38f, 0.44f);
m_landscapeMaterial.Specular = QVector3D(0, 0, 0);
fillIndexes(width, height);
fillVertexes(width, height, blockSize, generateId);
fillTextureCoords(width, height);
m_indexBuffer.create();
m_indexBuffer.bind();
m_indexBuffer.allocate(m_indexes.constData(), m_indexes.size() * sizeof(uint));
m_indexBuffer.release();
m_vertexBuffer.create();
m_vertexBuffer.bind();
m_vertexBuffer.allocate(m_vertexes.constData(), m_vertexes.size() * sizeof(VertexData));
m_vertexBuffer.release();
}
Landscape::~Landscape()
{
int asd;
}
void Landscape::rotate(const QQuaternion &rotation)
{
}
void Landscape::translate(const QVector3D &translation)
{
}
void Landscape::scale(const float scaleKoef)
{
}
void Landscape::setGlobalTransform(const QMatrix4x4 &matrix)
{
}
void Landscape::draw(QOpenGLShaderProgram *shaderProgram, QOpenGLFunctions *functions)
{
QMatrix4x4 modelMatrix;
modelMatrix.setToIdentity();
shaderProgram->setUniformValue("u_modelMatrix", modelMatrix);
shaderProgram->setUniformValue("u_isLandscape", 1);
if (m_landscapeMaterial.bind(aiTextureType_DIFFUSE, 0))
{
shaderProgram->setUniformValue("u_texture0", 0);
shaderProgram->setUniformValue("u_hasTexture", 1);
}
else
{
shaderProgram->setUniformValue("u_hasTexture", 0);
shaderProgram->setUniformValue("u_diffuse", m_landscapeMaterial.Diffuse);
shaderProgram->setUniformValue("u_ambient", m_landscapeMaterial.Ambient);
shaderProgram->setUniformValue("u_specular", m_landscapeMaterial.Specular);
shaderProgram->setUniformValue("u_shininess", m_landscapeMaterial.Shininess);
}
if (m_isSculpToolUse)
{
shaderProgram->setUniformValue("u_isLandscapeSculptToolEnable", 1);
shaderProgram->setUniformValue("u_spotLightPosition", LandscapeSculptTool::Instance().getCenter());
}
else
{
shaderProgram->setUniformValue("u_isLandscapeSculptToolEnable", 0);
}
// Bind buffers
m_vertexBuffer.bind();
m_indexBuffer.bind();
int offset = 0;
// Set varying attribute "a_position"
int location = shaderProgram->attributeLocation("a_position");
shaderProgram->enableAttributeArray(location);
shaderProgram->setAttributeBuffer(location, GL_FLOAT, offset, 3, sizeof(VertexData));
offset += sizeof(QVector3D);
// Set varying attribute "a_textCoord"
location = shaderProgram->attributeLocation("a_textCoord");
shaderProgram->enableAttributeArray(location);
shaderProgram->setAttributeBuffer(location, GL_FLOAT, offset, 2, sizeof(VertexData));
offset += sizeof(QVector2D);
// Set varying attribute "a_normal"
location = shaderProgram->attributeLocation("a_normal");
shaderProgram->enableAttributeArray(location);
shaderProgram->setAttributeBuffer(location, GL_FLOAT, offset, 3, sizeof(VertexData));
// Draw
functions->glDrawElements(GL_TRIANGLES, m_indexBuffer.size(), GL_UNSIGNED_INT, 0);
if (m_landscapeMaterial.isBound(aiTextureType_DIFFUSE))
m_landscapeMaterial.release(aiTextureType_DIFFUSE);
// Release
m_indexBuffer.release();
m_vertexBuffer.release();
}
void Landscape::objectPicking(QOpenGLShaderProgram *shaderProgram, QOpenGLFunctions *functions)
{
QMatrix4x4 modelMatrix;
modelMatrix.setToIdentity();
shaderProgram->setUniformValue("u_modelMatrix", modelMatrix);
// Bind buffers
m_vertexBuffer.bind();
m_indexBuffer.bind();
int offset = 0;
// Set varying attribute "a_position"
int location = shaderProgram->attributeLocation("a_position");
shaderProgram->enableAttributeArray(location);
shaderProgram->setAttributeBuffer(location, GL_FLOAT, offset, 3, sizeof(VertexData));
offset += sizeof(QVector3D);
// Set varying attribute "a_textCoord"
location = shaderProgram->attributeLocation("a_textCoord");
shaderProgram->enableAttributeArray(location);
shaderProgram->setAttributeBuffer(location, GL_FLOAT, offset, 2, sizeof(VertexData));
offset += sizeof(QVector2D);
// Set varying attribute "a_normal"
location = shaderProgram->attributeLocation("a_normal");
shaderProgram->enableAttributeArray(location);
shaderProgram->setAttributeBuffer(location, GL_FLOAT, offset, 3, sizeof(VertexData));
offset += sizeof(QVector3D);
// Set varying attribute "a_id"
location = shaderProgram->attributeLocation("a_id");
shaderProgram->enableAttributeArray(location);
shaderProgram->setAttributeBuffer(location, GL_FLOAT, offset, 4, sizeof(VertexData));
// Pick objects
functions->glDrawElements(GL_TRIANGLES, m_indexBuffer.size(), GL_UNSIGNED_INT, 0);
// Release
m_indexBuffer.release();
m_vertexBuffer.release();
}
QVector3D Landscape::getPosition() const
{
return QVector3D(0, 0, 0);
}
Landscape::PrimitivePointPositions Landscape::getTrianglePointGlobalPositions(
int triangleId, QMatrix4x4 projectionMatrix)
{
int triangleRow = triangleId / (2 * m_width);
int triangleColumn = (triangleId - triangleRow * (2 * m_width)) / 2;
int pointA, pointB, pointC;
if (triangleId % 2 == 0)
{
pointA = triangleRow * (m_width + 1) + triangleColumn;
pointB = pointA + (m_width + 1);
pointC = pointA + 1;
}
else
{
pointA = triangleRow * (m_width + 1) + triangleColumn + 1;
pointB = pointA + m_width;
pointC = pointB + 1;
}
QMatrix4x4 modelMatrix;
modelMatrix.setToIdentity();
QMatrix4x4 transformMat = Camera3D::Instance().getViewMatrix() * modelMatrix;
transformMat = projectionMatrix * transformMat;
PrimitivePointPositions positions;
positions.pointA = m_vertexes[pointA].position;
positions.pointB = m_vertexes[pointB].position;
positions.pointC = m_vertexes[pointC].position;
return positions;
}
void Landscape::addTexture(aiTextureType type, QOpenGLTexture *image)
{
m_landscapeMaterial.addTexture(type, image);
}
void Landscape::calculateNormals()
{
for (int i = 0; i < m_length + 1; ++i) {
for (int j = 0; i < m_width + 1; ++j) {
QVector3D normal;
QVector<int> faceIndexes = {
(i - 1) * (m_width * 2) + j * 2 - 1, // Top left
(i - 1) * (m_width * 2) + j * 2, // Top middle
(i - 1) * (m_width * 2) + j * 2 + 1, // Top right
i * (m_width * 2) + j * 2 - 2, // Down left
i * (m_width * 2) + j * 2 - 1, // Down middle
i * (m_width * 2) + j * 2 // Down right
};
for (uint k = 0; k < 6; ++k) {
if (faceIndexes[k] >= 0 && faceIndexes[k] < m_width * m_length * 2)
normal += getNormalByFaceIndex(faceIndexes[k]);
}
normal.normalize();
m_vertexes[i * (m_width + 1) + j].normal = normal;
}
}
}
void Landscape::resetBuffers()
{
if (m_indexBuffer.isCreated() != true) {
m_indexBuffer.create();
}
m_indexBuffer.bind();
if (m_indexBuffer.size() == 0) {
m_indexBuffer.allocate(m_indexes.size() * sizeof(uint));
}
else {
//m_indexBuffer.write();
}
m_indexBuffer.release();
}
void Landscape::setSculptToolUsage(bool isLandscapeSculptToolUse)
{
m_isSculpToolUse = isLandscapeSculptToolUse;
}
void Landscape::clear()
{
m_vertexes.clear();
m_indexes.clear();
m_width = -1;
m_length = -1;
}
void Landscape::fillIndexes(uint width, uint height)
{
m_indexes.resize(width * height * 2 * 3);
long size = sizeof(m_indexes);
uint lastAddIndex = 0;
uint plusShift = width + 1;
// Form even row (from left to right)
for (uint j = 0; j < height; ++j) {
for (uint i = 0; i < width; ++i) {
uint pos = j * (width * 2 * 3) + 6 * i;
m_indexes[pos] = lastAddIndex;
m_indexes[pos + 1] = lastAddIndex + plusShift;
m_indexes[pos + 2] = lastAddIndex + 1;
m_indexes[3 + pos] = lastAddIndex + 1;
m_indexes[3 + pos + 1] = lastAddIndex + plusShift;
m_indexes[3 + pos + 2] = lastAddIndex + plusShift + 1;
++lastAddIndex;
}
++lastAddIndex;
}
}
void Landscape::fillVertexes(uint width, uint height, uint blockSize, Mfunc generateId)
{
m_vertexes.resize((width + 1) * (height + 1));
QVector3D halfLensOfLandscape(width * blockSize / 2, 0, height * blockSize / 2);
for (uint i = 0; i < height + 1; ++i) {
for (uint j = 0; j < width + 1; ++j) {
m_vertexes[i * (width + 1) + j].position =
QVector3D(blockSize * j, 0, blockSize * i) - halfLensOfLandscape;
m_vertexes[i * (width + 1) + j].normal = QVector3D(0, 1, 0);
m_vertexes[i * (width + 1) + j].id = generateId();
}
}
}
void Landscape::fillTextureCoords(uint width, uint height)
{
for (uint i = 0; i < height + 1; ++i) {
for (uint j = 0; j < width + 1; ++j) {
m_vertexes[i * (width + 1) + j].texCoord = QVector2D(
(float)j / (float)width,
(float)i / (float)height
);
}
}
}
bool Landscape::isCircleIntersectsLandscape(QVector2D circleCenter, float circleSize)
{
Border2D border(QVector2D(-m_width / 2, -m_length / 2), // TopLeft
QVector2D(m_width / 2 + (m_width % 2), -m_length / 2), // TopRight
QVector2D(-m_width / 2, m_length / 2 + (m_length % 2)), // DownLeft
QVector2D(m_width / 2 + (m_width % 2), m_length / 2 + (m_length % 2))); // DownRight
border *= m_blockSize;
bool isBetweenTopAndDown = circleCenter.y() >= border.TopLeft.y() && circleCenter.y() <= border.DownLeft.y();
bool isBetweenLeftAndRight = circleCenter.x() >= border.DownLeft.x() && circleCenter.x() <= border.DownRight.x();
// If circle center inside border
if (isBetweenTopAndDown && isBetweenLeftAndRight)
return true;
// If circle intersects border's corners
if ((border.TopLeft - circleCenter).length() - circleSize <= 0 ||
(border.TopRight - circleCenter).length() - circleSize <= 0 ||
(border.DownLeft - circleCenter).length() - circleSize <= 0 ||
(border.DownRight - circleCenter).length() - circleSize <= 0)
return true;
// If circle intersects border's sides
if (isBetweenTopAndDown && circleCenter.x() <= border.TopLeft.x() && border.TopLeft.x() - circleCenter.x() <= circleSize || // Left
isBetweenTopAndDown && circleCenter.x() >= border.TopRight.x() && circleCenter.x() - border.TopRight.x() <= circleSize || // Right
isBetweenLeftAndRight && circleCenter.y() <= border.TopLeft.y() && border.TopLeft.y() - circleCenter.y() <= circleSize || // Top
isBetweenLeftAndRight && circleCenter.y() >= border.DownLeft.y() && circleCenter.y() - border.DownLeft.y() <= circleSize) // Down
return true;
return false;
}
int Landscape::getShiftedIndexForPointOnBorderIntersects(int index)
{
bool isLeftLandBorderIntersects = index % (m_width + 1) == 0;
bool isTopLandBorderIntersects = index < m_width + 1;
bool isRightLandBorderIntersects = index % (m_width + 1) == m_width;
bool isDownLandBorderIntersects = index >= m_length * (m_width + 1);
if (isDownLandBorderIntersects || isTopLandBorderIntersects)
return -2;
if (isRightLandBorderIntersects || isDownLandBorderIntersects
|| isLeftLandBorderIntersects || isTopLandBorderIntersects)
{
uint indexForAdd = index;
if (isRightLandBorderIntersects)
--indexForAdd;
//if (isDownLandBorderIntersects)
//indexForAdd -= m_width + 1;
if (isLeftLandBorderIntersects)
++indexForAdd;
//if (isTopLandBorderIntersects)
//indexForAdd += m_width + 1;
return indexForAdd;
}
// else
else
return -1;
}
QVector3D Landscape::getNormalByFaceIndex(uint index) const
{
uint ind1 = m_indexes[3 * index];
uint ind2 = m_indexes[3 * index + 1];
uint ind3 = m_indexes[3 * index + 2];
return QVector3D::crossProduct(
m_vertexes[ind1].position - m_vertexes[ind2].position,
m_vertexes[ind3].position - m_vertexes[ind2].position);
}
QVector3D Landscape::calculateNormalByIndex(uint index) const
{
QVector3D normal(0, 0, 0);
int i = index / (m_width + 1);
int j = index % (m_width + 1);
std::vector<int> faceIndexes = {
(i - 1) * (m_width * 2) + j * 2 - 1, // Top left
(i - 1) * (m_width * 2) + j * 2, // Top middle
(i - 1) * (m_width * 2) + j * 2 + 1, // Top right
i * (m_width * 2) + j * 2 - 2, // Down left
i * (m_width * 2) + j * 2 - 1, // Down middle
i * (m_width * 2) + j * 2 // Down right
};
for (uint k = 0; k < 6; ++k) {
if (faceIndexes[k] >= 0 && faceIndexes[k] < m_width * m_length * 2)
normal += getNormalByFaceIndex(faceIndexes[k]);
}
return normal;
}
IndexBorder2D Landscape::getIndexBorderForToolBySize(QVector2D toolCenter, float circleRadius)
{
IndexBorder2D border;
if (isCircleIntersectsLandscape(toolCenter, circleRadius))
{
// Определить примерный прямоугольник, в котором находится центр
int leftX = qMax(int(toolCenter.x() - circleRadius), -m_width / 2); // 41.2 -> 41
int rightX = qMin(int(toolCenter.x() + circleRadius), m_width / 2);
int topZ = qMax(int(toolCenter.y() - circleRadius), -m_length / 2);
int downZ = qMin(int(toolCenter.y() + circleRadius), m_length / 2 + m_length % 2);
//int numIndexesInRow = rightX - leftX + 1;
//int numIndexesInColumn = downZ - topZ + 1;
// Определить верхнюю левую и нижнюю левую точки для перерасчёта координат и нормалей
border.TopLeft = getIndexByShiftFromCenter(leftX, topZ);
border.DownLeft = getIndexByShiftFromCenter(leftX, downZ);
border.TopRight = getIndexByShiftFromCenter(rightX, topZ);
border.DownRight = getIndexByShiftFromCenter(rightX, downZ);
border.IsValid = true;
}
else
border.IsValid = false;
return border;
}
QPair<int, int> Landscape::getStartAndXIndexes(const IndexBorder2D &border, const QVector2D &toolCenter, float circleRadius) const
{
uint numOfRows = (border.DownLeft - border.TopLeft) / (m_width + 1) + 1;
int startIndex = -1;
//check (x_index + 1_row), (x_index), (x_index - 1_row) and (x_index - 2_row)
uint x_index = border.TopLeft + (numOfRows / 2) * (m_width + 1);
LandscapeSculptTool::InsideCircleChecker checker(circleRadius, toolCenter);
for (int i = -1; i < 3; ++i) {
uint index = x_index - i * (m_width + 1);
if (index >= border.TopLeft && index <= border.DownLeft + m_width + 1 &&
checker.isInside(m_vertexes[index].position))
{
startIndex = index;
break;
}
}
return qMakePair(startIndex, x_index);
}
uint Landscape::getIndexByShiftFromCenter(int x, int y) const
{
uint columnShift = (m_width / 2) + x;
uint rowShift = (m_length / 2) + y;
return rowShift * (m_width + 1) + columnShift;
}
void Landscape::refreshByLandscapeTool()
{
qDebug() << "Start refresh";
LandscapeSculptTool& tool = LandscapeSculptTool::Instance();
if (isCircleIntersectsLandscape(tool.getCenter(), tool.getRadius()) == false)
return;
QVector<uint> indexesForUpdateNormals;
InformationForUpdate infoForUpdate(tool.getCenter(), tool.getRadius(), m_width + 1);
prepareInfoForUpdateOuterRing(infoForUpdate);
QVector<uint> downSide = getCircleDownSide(infoForUpdate);
QVector<uint> topSide = getCircleTopSide(infoForUpdate);
if (topSide.size() % 2 != 0 ||
downSide.size() % 2 != 0)
{
qDebug() << "\n ERROR!!! some side vector's size remainder of division by 2 doesn't equal zero\n";
return;
}
//qDebug() << "After topSide and DownSide";
if (isCircleIntersectsLandscape(tool.getCenter(), tool.getRadius() * tool.getBrushFalloff()) == false)
infoForUpdate.CanUpdate = false;
else
prepareInfoForUpdateInnerCircle(infoForUpdate);
//qDebug() << "After prepate for inner circle update";
QVector<uint> downInteriorSide = getCircleDownSide(infoForUpdate);
QVector<uint> topInteriorSide = getCircleTopSide(infoForUpdate);
if (topInteriorSide.size() % 2 != 0 ||
downInteriorSide.size() % 2 != 0)
{
qDebug() << "\n ERROR!!! some InteriorSide vector's size remainder of division by 2 doesn't equal zero\n";
return;
}
// startIndex должен быть на той же строке на которой был при расчёте внешнего кольца
//fillPerimeter(downInteriorSide, topInteriorSide, indexesForUpdateVertexes, indexForUpdateNormalsOnIntersects,
// toolCenter, size * toolFalloff, false);
//qDebug() << "After downInteriorSide and topInteriorSide";
// Update Down side
updateVertexPositions(downSide, downInteriorSide, tool.getToolStrength());
// Update Top side
updateVertexPositions(topSide, topInteriorSide, tool.getToolStrength());
infoForUpdate.ToolRadius = tool.getRadius();
// Find and update vertex positions on intersects
//findAndUpdateVertexesOnBorderIntersects(infoForUpdate);
prepareInfoForUpdateOuterRing(infoForUpdate);
// Обновлять нормали надо только когда перестаёт быть активным LandscapeSculptTool
//qDebug() << "Before rewrite buffer";
// Определить с какого места и сколько VertexDatas надо обновить в вершинном буфере
int offset = infoForUpdate.IndexBorder.TopLeft * sizeof(VertexData);
int count = (infoForUpdate.IndexBorder.DownRight - infoForUpdate.IndexBorder.TopLeft + 1) * sizeof(VertexData);
m_vertexBuffer.bind();
m_vertexBuffer.write(offset, m_vertexes.data() + infoForUpdate.IndexBorder.TopLeft, count);
m_vertexBuffer.release();
qDebug() << "After rewrite buffer";
// Обновить данные в вершинном буфере
//return getSmallestBorderForTool(center, size);
}
void Landscape::prepareInfoForUpdateOuterRing(Landscape::InformationForUpdate &info)
{
info.IndexBorder = getIndexBorderForToolBySize(info.ToolCenter, info.ToolRadius);
info.RowLength = m_width + 1;
info.LandscapeDownLeftIndex = m_vertexes.size() - info.RowLength;
QPair<int, int> startAndX_indexes = getStartAndXIndexes(info.IndexBorder, info.ToolCenter, info.ToolRadius);
info.StartIndex = startAndX_indexes.first;
if (info.StartIndex == -1)
info.StartIndex = info.CurrentIndex = info.PrevIndex = startAndX_indexes.second + 1;
else
info.CurrentIndex = info.PrevIndex = info.StartIndex;
info.CanUpdate = true;
}
void Landscape::prepareInfoForUpdateInnerCircle(Landscape::InformationForUpdate &info)
{
qDebug() << "In the start of prepate for inner circle update";
info.ToolRadius = LandscapeSculptTool::Instance().getRadius() * LandscapeSculptTool::Instance().getBrushFalloff();
info.IndexBorder = getIndexBorderForToolBySize(info.ToolCenter, info.ToolRadius);
info.IsNeedToCheckBorderIntersects = false;
LandscapeSculptTool::InsideCircleChecker checker(info.ToolRadius, info.ToolCenter);
for (int i = 0; i + info.StartIndex < m_vertexes.size(); ++i)
{
int index = info.StartIndex + i;
if (checker.isInside(m_vertexes[index].position))
{
info.StartIndex = info.CurrentIndex = info.PrevIndex = index - 1;
return;
}
}
info.statusNoErr = false;
qDebug() << "In the end of prepate for inner circle update";
/*
QPair<int, int> startAndX_indexes = getStartAndXIndexes(info.IndexBorder, info.ToolCenter, info.ToolRadius);
info.StartIndex = startAndX_indexes.first;
if (info.StartIndex == -1)
info.StartIndex = info.CurrentIndex = info.PrevIndex = startAndX_indexes.second + 1;
else
info.CurrentIndex = info.PrevIndex = info.StartIndex;
*/
}
QVector<uint> Landscape::getCircleDownSide(Landscape::InformationForUpdate info)
{
if (info.CanUpdate == false)
return QVector<uint>();
LandscapeSculptTool::InsideCircleChecker checker(info.ToolRadius, info.ToolCenter);
QVector<uint> downSide;
for (int i = info.CurrentIndex; i <= info.IndexBorder.DownLeft; i += info.RowLength)
{
for (int j = 0; j < info.RowLength; ++j)
{
int curIndex = i + j;
QVector3D pos = m_vertexes[curIndex].position;
if (checker.isInside(m_vertexes[curIndex].position) == true)
{
downSide.push_back(curIndex);
break;
}
}
}
for (int i = info.IndexBorder.DownRight; i > info.CurrentIndex; i -= info.RowLength)
{
for (int j = 0; j < info.RowLength; ++j)
{
int curIndex = i - j;
if (checker.isInside(m_vertexes[curIndex].position) == true)
{
downSide.push_back(curIndex);
break;
}
}
}
return downSide;
// Fill DownLeft Side
while (info.CurrentIndex < info.IndexBorder.DownLeft + info.RowLength) {
if (checker.isInside(m_vertexes[info.CurrentIndex].position) == true)
{
downSide.push_back(info.CurrentIndex);
info.CurrentIndex += info.RowLength;
}
else
{
++info.CurrentIndex;
}
}
// Find the most right index in the most down row
//info.CurrentIndex -= info.RowLength;
if (downSide.isEmpty() == true)
return downSide;
info.CurrentIndex = downSide.last();
while (checker.isInside(m_vertexes[info.CurrentIndex].position)) {
++info.CurrentIndex;
}
// Prepare for fill DownRight Side
info.PrevIndex = info.CurrentIndex - 1;
// Fill DownRight Side
while (info.CurrentIndex > info.StartIndex)
{
if (info.CurrentIndex + 1 > m_vertexes.size())
{
info.statusNoErr = false;
return QVector<uint>();
}
bool isRowChanged = false;
// Because if row changed that we can add PrevIndex to the vector.
// It's the defence from case when row changed but CurrentIndex is still inside circle
if (checker.isInside(m_vertexes[info.CurrentIndex].position) == false || isRowChanged == true)
{
downSide.push_back(info.PrevIndex);
info.PrevIndex = info.CurrentIndex - info.RowLength - 1;
info.CurrentIndex -= info.RowLength + 1;
}
else
{
info.PrevIndex = info.CurrentIndex;
++info.CurrentIndex;
// Flag for protect from infinite cycle. For more info check the begin of the cycle
if (info.CurrentIndex % info.RowLength == 0)
isRowChanged = true;
}
}
return downSide;
}
QVector<uint> Landscape::getCircleTopSide(Landscape::InformationForUpdate info)
{
qDebug() << "In circle top side";
if (info.CanUpdate == false)
return QVector<uint>();
LandscapeSculptTool::InsideCircleChecker checker(info.ToolRadius, info.ToolCenter);
QVector<uint> topSide;
// Prepare for fill TopLeft Side
info.CurrentIndex = info.StartIndex - (m_width + 1);
for (int i = info.CurrentIndex + info.RowLength; i > info.IndexBorder.TopLeft; i -= info.RowLength)
{
for (int j = 0; j < info.RowLength; ++j)
{
int curIndex = i - j;
if (checker.isInside(m_vertexes[curIndex].position) == true)
{
topSide.push_back(curIndex);
break;
}
}
}
for (int i = info.IndexBorder.TopLeft; i < info.CurrentIndex + info.RowLength; i += info.RowLength)
{
for (int j = 0; j < info.RowLength; ++j)
{
int curIndex = i + j;
if (checker.isInside(m_vertexes[curIndex].position) == true)
{
topSide.push_back(curIndex);
break;
}
}
}
return topSide;
// Fill TopLeft Side
while (info.CurrentIndex >= info.IndexBorder.TopLeft) {
qDebug() << info.CurrentIndex;
if (checker.isInside(m_vertexes[info.CurrentIndex].position) == true)
{
topSide.push_back(info.CurrentIndex);
info.CurrentIndex -= info.RowLength;
}
else {
++info.CurrentIndex;
}
}
qDebug() << "After first cycle 1";
// Find the most right index in the most down row
//info.CurrentIndex += info.RowLength;
if (topSide.isEmpty() == true) {
qDebug() << "Top side is empty";
return topSide;
}
info.CurrentIndex = topSide.last();
while (checker.isInside(m_vertexes[info.CurrentIndex].position)) {
++info.CurrentIndex;
}
qDebug() << "After first cycle 2";
// Prepare for fill DownRight Side
info.PrevIndex = info.CurrentIndex - 1;
// Fill TopRight Side
while (info.CurrentIndex < info.StartIndex) {
if (checker.isInside(m_vertexes[info.CurrentIndex].position) == false)
{
topSide.push_back(info.PrevIndex);
info.CurrentIndex += info.RowLength;
info.PrevIndex = info.CurrentIndex - 1;
}
else
{
info.PrevIndex = info.CurrentIndex;
++info.CurrentIndex;
}
}
return topSide;
}
void Landscape::updateVertexPositions(const QVector<uint> &side, const QVector<uint> &interiorSide, float toolStrength)
{
LandscapeSculptTool & tool = LandscapeSculptTool::Instance();
uint l = 0, r = side.size() - 1;
for (uint inner_r = interiorSide.size() - 1; l < interiorSide.size() / 2; ++l, --r, --inner_r)
{
// Fill inner circle
for (uint index = interiorSide[l]; index <= interiorSide[inner_r]; ++index) {
QVector3D & pos = m_vertexes[index].position;
pos.setY(pos.y() + toolStrength);
}
// Fill outer left part of the ring
for (uint index = side[l]; index < interiorSide[l]; ++index) {
QVector3D & pos = m_vertexes[index].position;
pos.setY(pos.y() + toolStrength * tool.getKoefDependsFromLen(pos));
}
// Fill outer right part of the ring
for (uint index = interiorSide[inner_r] + 1; index <= side[r]; ++index) {
QVector3D & pos = m_vertexes[index].position;
pos.setY(pos.y() + toolStrength * tool.getKoefDependsFromLen(pos));
}
}
for (; l <= r; ++l, --r)
{
for (uint index = side[l]; index <= side[r]; ++index) {
QVector3D & pos = m_vertexes[index].position;
pos.setY(pos.y() + toolStrength * tool.getKoefDependsFromLen(pos));
}
}
}
void Landscape::updateVertexesNormals(const QVector<uint> &side)
{
for (uint l = 0, r = side.size() - 1; l <= r; ++l, --r)
{
for (uint index = side[l]; index <= side[r]; ++index)
m_vertexes[index].normal = calculateNormalByIndex(index);
}
}
void Landscape::findAndUpdateVertexesOnBorderIntersects(InformationForUpdate const &info)
{
LandscapeSculptTool & tool = LandscapeSculptTool::Instance();
float toolStrength = tool.getToolStrength();
if (info.IndexBorder.DownLeft % info.RowLength == 0) // Is circle intersects by left side
{
int low = info.IndexBorder.TopLeft;
int high = info.IndexBorder.DownLeft;
int step = info.RowLength;
QPair<int, int> low_high = getBordersOnIntersects(info, low, high, step);
if (low_high.first == -1 && low_high.second != -1)
low_high.first = low_high.second;
else if (low_high.first != -1 && low_high.second == -1)
low_high.second = low_high.first;
if (low_high.first != -1 && low_high.second != -1)
{
for (int index = low_high.first; index <= low_high.second; index += step) {
QVector3D & pos = m_vertexes[index].position;
pos.setY(pos.y() + toolStrength * tool.getKoefDependsFromLen(pos));
}
}
}
if (info.IndexBorder.DownLeft >= m_length * info.RowLength) // Is circle intersects by down side
{
int low = info.IndexBorder.DownLeft;
int high = info.IndexBorder.DownLeft + m_width;
int step = 1;
QPair<int, int> low_high = getBordersOnIntersects(info, low, high, step);
if (low_high.first == -1 && low_high.second != -1)
low_high.first = low_high.second;
else if (low_high.first != -1 && low_high.second == -1)
low_high.second = low_high.first;
if (low_high.first != -1 && low_high.second != -1)
{
for (int index = low_high.first; index <= low_high.second; index += step) {
QVector3D & pos = m_vertexes[index].position;
pos.setY(pos.y() + toolStrength * tool.getKoefDependsFromLen(pos));
}
}
}
if (info.IndexBorder.TopRight % info.RowLength == m_width) // Is circle intersects by right side
{
int low = info.IndexBorder.TopRight;
int high = info.IndexBorder.DownRight;
int step = info.RowLength;
QPair<int, int> low_high = getBordersOnIntersects(info, low, high, step);
if (low_high.first == -1 && low_high.second != -1)
low_high.first = low_high.second;
else if (low_high.first != -1 && low_high.second == -1)
low_high.second = low_high.first;
if (low_high.first != -1 && low_high.second != -1)
{
for (int index = low_high.first; index <= low_high.second; index += step) {
QVector3D & pos = m_vertexes[index].position;
pos.setY(pos.y() + toolStrength * tool.getKoefDependsFromLen(pos));
}
}
}
if (info.IndexBorder.TopLeft < info.RowLength) // Is circle intersects by top side
{
int low = info.IndexBorder.TopLeft;
int high = info.IndexBorder.TopLeft + m_width;
int step = 1;
QPair<int, int> low_high = getBordersOnIntersects(info, low, high, step);
if (low_high.first == -1 && low_high.second != -1)
low_high.first = low_high.second;
else if (low_high.first != -1 && low_high.second == -1)
low_high.second = low_high.first;
if (low_high.first != -1 && low_high.second != -1)
{
for (int index = low_high.first; index <= low_high.second; index += step) {
QVector3D & pos = m_vertexes[index].position;
pos.setY(pos.y() + toolStrength * tool.getKoefDependsFromLen(pos));
}
}
}
}
QPair<int, int> Landscape::getBordersOnIntersects(const InformationForUpdate & info,
int low, int high, int step)
{
LandscapeSculptTool::InsideCircleChecker checker(info.ToolRadius, info.ToolCenter);
int minIntersect = low, maxIntersect = high;
QPair<int, int> res{-1, -1};