/
daria_bel
/
VRLabs
Обзор
Документация
Войти
/
daria_bel
/
VRLabs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
Lab2_ViewPoints/fcViewPoints.cpp
381 строка
14 KB
Daria Bel
Лабораторные работы для daria_bel
14 май 2026, 09:22
14 май 2026, 09:22
e507b0f
Код
Авторство
О чём код?
//--------------------------------------------------------------------------- #include <vcl.h> #include <tchar.h> #include <stdlib.h> #include <math.h> #pragma hdrstop #include "fcViewPoints.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma link "GLS.SceneViewer" #pragma link "GLS.Coordinates" #pragma link "GLS.Objects" #pragma link "GLS.SceneViewer" #pragma link "GLS.Scene" #pragma link "GLS.Cadencer" #pragma resource "*.dfm" TForm1* Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { FormCreate(this); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { // === НАСТРОЙКА ФОРМЫ === this->Color = (TColor)0x00333333; // Тёмно-серый фон формы this->Caption = "Лабораторная работа №2 - Генерация точечных данных методом Монте-Карло"; this->Position = poScreenCenter; this->BorderStyle = bsSingle; this->Width = 1280; this->Height = 800; // Инициализация переменных keyLeftPressed = false; keyRightPressed = false; keyUpPressed = false; keyDownPressed = false; // === НАСТРОЙКА МАТЕРИАЛОВ ДЛЯ КУБА === GLDummyCube1->Material->FrontProperties->Diffuse->SetColor(0.95f, 0.55f, 0.25f, 0.7f); // Оранжево-золотой GLDummyCube1->Material->FrontProperties->Specular->SetColor(1.0f, 0.8f, 0.4f, 1.0f); GLDummyCube1->Material->FrontProperties->Emission->SetColor(0.2f, 0.1f, 0.05f, 1.0f); GLDummyCube1->Visible = true; // === НАСТРОЙКА МАТЕРИАЛОВ ДЛЯ СФЕРЫ === GLSphere1->Material->FrontProperties->Diffuse->SetColor(0.35f, 0.75f, 0.55f, 0.6f); // Мятно-зелёный GLSphere1->Material->FrontProperties->Specular->SetColor(0.6f, 0.9f, 0.7f, 1.0f); GLSphere1->Material->FrontProperties->Emission->SetColor(0.05f, 0.15f, 0.1f, 1.0f); GLSphere1->Visible = true; // === НАСТРОЙКА КАМЕР === GLCamera1->Position->Z = 9; GLCamera1->Position->Y = 1.5; GLCamera1->Position->X = 0; GLCamera2->Position->X = 7; GLCamera2->Position->Y = 1.5; GLCamera3->Position->Y = 7; GLCamera3->Position->Z = 2.5; // === НАСТРОЙКА ТЕКСТА === Label1->Font->Color = (TColor)0x00FFCC88; Label1->Font->Style = TFontStyles() << fsBold; Label1->Font->Size = 9; Label2->Font->Color = (TColor)0x00FFCC88; Label2->Font->Style = TFontStyles() << fsBold; Label2->Font->Size = 9; Label3->Font->Color = (TColor)0x00FFCC88; Label3->Font->Style = TFontStyles() << fsBold; Label3->Font->Size = 9; Label4->Font->Color = (TColor)0x0088FFAA; Label4->Font->Style = TFontStyles() << fsBold; Label4->Font->Size = 11; Label5->Font->Color = (TColor)0x00AAFF88; Label5->Font->Style = TFontStyles() << fsBold; Label5->Font->Size = 11; // === НАСТРОЙКА ФОНА СЦЕНЫ === GLSceneViewer1->Buffer->BackgroundColor = (TColor)0x00224422; // Тёмно-зелёный фон сцены // === НАСТРОЙКА ПАНЕЛИ === Panel1->Color = (TColor)0x00444444; // Серый Panel1->Font->Color = clWhite; Panel1->Caption = ""; Panel1->Align = alBottom; Panel1->Height = 130; // === НАСТРОЙКА КНОПКИ === Button1->Font->Color = clWhite; Button1->Font->Style = TFontStyles() << fsBold; Button1->Font->Size = 5; Button1->Caption = "▶ СГЕНЕРИРОВАТЬ ТОЧКИ"; Button1->Width = 220; Button1->Height = 40; Button1->Left = 20; Button1->Top = 15; Button1->Cursor = crHandPoint; // === НАСТРОЙКА ПОЛЗУНКОВ === TrackBar1->Max = 360; TrackBar2->Max = 360; TrackBar3->Max = 360; TrackBar1->Left = 250; TrackBar1->Top = 20; TrackBar1->Width = 200; TrackBar2->Left = 250; TrackBar2->Top = 55; TrackBar2->Width = 200; TrackBar3->Left = 250; TrackBar3->Top = 90; TrackBar3->Width = 200; Label1->Left = 470; Label1->Top = 22; Label2->Left = 470; Label2->Top = 57; Label3->Left = 470; Label3->Top = 92; Label4->Left = 680; Label4->Top = 25; Label5->Left = 680; Label5->Top = 60; Label1->Caption = "Наклон (Pitch): 0°"; Label2->Caption = "Крен (Roll): 0°"; Label3->Caption = "Поворот (Yaw): 0°"; Label4->Caption = "■ КУБ: 0 точек"; Label5->Caption = "● СФЕРА: 0 точек"; } //--------------------------------------------------------------------------- void __fastcall TForm1::FormMouseWheel(TObject* Sender, TShiftState Shift, int WheelDelta, TPoint &MousePos, bool &Handled) { if (GLSceneViewer1->MouseInControl == true) { GLCamera1->AdjustDistanceToTarget(Power(1.1, -WheelDelta / 120)); } } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject* Sender) { int pointCount = 600 + rand() % 1800; // 600-2400 точек // Очистка предыдущих точек for (int i = 0; i < 10000; i++) { if(cubePoints[i] != NULL) { delete cubePoints[i]; cubePoints[i] = NULL; } if(spherePoints[i] != NULL) { delete spherePoints[i]; spherePoints[i] = NULL; } } float sphereRad = GLSphere1->Radius; // === ТОЧКИ ДЛЯ КУБА (тёплые цвета) === for (int i = 0; i < pointCount; i++) { cubePoints[i] = new TGLPoints(GLDummyCube1); int colorScheme = rand() % 5; float rVal, gVal, bVal; switch(colorScheme) { case 0: // Красный rVal = 0.95f + ((float)(rand() % 50)) / 500.0f; gVal = 0.15f + ((float)(rand() % 80)) / 500.0f; bVal = 0.15f + ((float)(rand() % 80)) / 500.0f; break; case 1: // Оранжевый rVal = 0.95f + ((float)(rand() % 50)) / 500.0f; gVal = 0.55f + ((float)(rand() % 100)) / 500.0f; bVal = 0.1f + ((float)(rand() % 60)) / 500.0f; break; case 2: // Жёлтый rVal = 0.95f + ((float)(rand() % 50)) / 500.0f; gVal = 0.85f + ((float)(rand() % 100)) / 500.0f; bVal = 0.15f + ((float)(rand() % 80)) / 500.0f; break; case 3: // Розовый rVal = 0.95f + ((float)(rand() % 50)) / 500.0f; gVal = 0.35f + ((float)(rand() % 100)) / 500.0f; bVal = 0.65f + ((float)(rand() % 100)) / 500.0f; break; default: // Коралловый rVal = 0.95f + ((float)(rand() % 50)) / 500.0f; gVal = 0.45f + ((float)(rand() % 100)) / 500.0f; bVal = 0.45f + ((float)(rand() % 100)) / 500.0f; break; } cubePoints[i]->Colors->Add(rVal, gVal, bVal, 0.95f); cubePoints[i]->Size = 5; cubePoints[i]->Position->X = 1.15f * rand() / RAND_MAX - 0.575f; cubePoints[i]->Position->Y = 1.15f * rand() / RAND_MAX - 0.575f; cubePoints[i]->Position->Z = 1.15f * rand() / RAND_MAX - 0.575f; cubePoints[i]->Style = psSmooth; } // === ТОЧКИ ДЛЯ СФЕРЫ (холодные цвета) === for (int i = 0; i < pointCount; i++) { spherePoints[i] = new TGLPoints(GLSphere1); int colorScheme = rand() % 5; float rVal, gVal, bVal; switch(colorScheme) { case 0: // Синий rVal = 0.1f + ((float)(rand() % 60)) / 500.0f; gVal = 0.35f + ((float)(rand() % 100)) / 500.0f; bVal = 0.95f + ((float)(rand() % 50)) / 500.0f; break; case 1: // Голубой rVal = 0.2f + ((float)(rand() % 60)) / 500.0f; gVal = 0.75f + ((float)(rand() % 100)) / 500.0f; bVal = 0.95f + ((float)(rand() % 50)) / 500.0f; break; case 2: // Зелёный rVal = 0.1f + ((float)(rand() % 60)) / 500.0f; gVal = 0.85f + ((float)(rand() % 100)) / 500.0f; bVal = 0.55f + ((float)(rand() % 100)) / 500.0f; break; case 3: // Фиолетовый rVal = 0.65f + ((float)(rand() % 100)) / 500.0f; gVal = 0.35f + ((float)(rand() % 100)) / 500.0f; bVal = 0.95f + ((float)(rand() % 50)) / 500.0f; break; default: // Бирюзовый rVal = 0.05f + ((float)(rand() % 40)) / 500.0f; gVal = 0.85f + ((float)(rand() % 100)) / 500.0f; bVal = 0.9f + ((float)(rand() % 50)) / 500.0f; break; } spherePoints[i]->Colors->Add(rVal, gVal, bVal, 0.9f); spherePoints[i]->Size = 5; spherePoints[i]->Style = psSmooth; // Генерация точек внутри сферы методом Монте-Карло float xPos, yPos, zPos, dist; do { xPos = (2.0f * rand() / RAND_MAX - 1.0f) * sphereRad; yPos = (2.0f * rand() / RAND_MAX - 1.0f) * sphereRad; zPos = (2.0f * rand() / RAND_MAX - 1.0f) * sphereRad; dist = sqrt(xPos*xPos + yPos*yPos + zPos*zPos); } while (dist > sphereRad); spherePoints[i]->Position->X = xPos; spherePoints[i]->Position->Y = yPos; spherePoints[i]->Position->Z = zPos; } Label4->Caption = "■ КУБ: " + IntToStr(pointCount) + " точек"; Label5->Caption = "● СФЕРА: " + IntToStr(pointCount) + " точек"; GLSceneViewer1->Refresh(); } //--------------------------------------------------------------------------- void __fastcall TForm1::TrackBar1Change(TObject* Sender) { GLDummyCube1->PitchAngle = TrackBar1->Position; GLSphere1->PitchAngle = TrackBar1->Position * 0.65f; Label1->Caption = "Наклон (Pitch): " + IntToStr(TrackBar1->Position) + "°"; } //--------------------------------------------------------------------------- void __fastcall TForm1::TrackBar2Change(TObject* Sender) { GLDummyCube1->RollAngle = TrackBar2->Position; GLSphere1->RollAngle = TrackBar2->Position * 0.65f; Label2->Caption = "Крен (Roll): " + IntToStr(TrackBar2->Position) + "°"; } //--------------------------------------------------------------------------- void __fastcall TForm1::TrackBar3Change(TObject* Sender) { GLDummyCube1->TurnAngle = TrackBar3->Position; GLSphere1->TurnAngle = TrackBar3->Position * 0.65f; Label3->Caption = "Поворот (Yaw): " + IntToStr(TrackBar3->Position) + "°"; } //--------------------------------------------------------------------------- void __fastcall TForm1::GLSceneViewer1MouseDown( TObject* Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { mouseX = X; mouseY = Y; } //--------------------------------------------------------------------------- void __fastcall TForm1::GLSceneViewer1MouseMove( TObject* Sender, TShiftState Shift, int X, int Y) { if (Shift.Contains(ssLeft)) GLCamera1->MoveAroundTarget(mouseY - Y, mouseX - X); else if (Shift.Contains(ssRight)) GLCamera1->RotateTarget(mouseY - Y, mouseX - X, 0); mouseX = X; mouseY = Y; } //--------------------------------------------------------------------------- void __fastcall TForm1::GLCadencer1Progress(TObject *Sender, const double DeltaTime, const double NewTime) { bool currentLeft = GetAsyncKeyState(VK_LEFT) & 0x8000; bool currentRight = GetAsyncKeyState(VK_RIGHT) & 0x8000; bool currentUp = GetAsyncKeyState(VK_UP) & 0x8000; bool currentDown = GetAsyncKeyState(VK_DOWN) & 0x8000; // Переключение камер (вправо) if (currentRight && !keyRightPressed) { if (GLSceneViewer1->Camera == GLCamera1) GLSceneViewer1->Camera = GLCamera2; else if (GLSceneViewer1->Camera == GLCamera2) GLSceneViewer1->Camera = GLCamera3; else GLSceneViewer1->Camera = GLCamera1; } // Переключение камер (влево) if (currentLeft && !keyLeftPressed) { if (GLSceneViewer1->Camera == GLCamera1) GLSceneViewer1->Camera = GLCamera3; else if (GLSceneViewer1->Camera == GLCamera2) GLSceneViewer1->Camera = GLCamera1; else GLSceneViewer1->Camera = GLCamera2; } // Увеличение прозрачности (вверх) if (currentUp && !keyUpPressed) { float newAlphaCube = GLDummyCube1->Material->FrontProperties->Diffuse->Alpha + 0.05f; float newAlphaSphere = GLSphere1->Material->FrontProperties->Diffuse->Alpha + 0.05f; if (newAlphaCube <= 1.0f) { GLDummyCube1->Material->FrontProperties->Diffuse->Alpha = newAlphaCube; } if (newAlphaSphere <= 1.0f) { GLSphere1->Material->FrontProperties->Diffuse->Alpha = newAlphaSphere; } } // Уменьшение прозрачности (вниз) if (currentDown && !keyDownPressed) { float newAlphaCube = GLDummyCube1->Material->FrontProperties->Diffuse->Alpha - 0.05f; float newAlphaSphere = GLSphere1->Material->FrontProperties->Diffuse->Alpha - 0.05f; if (newAlphaCube >= 0.2f) { GLDummyCube1->Material->FrontProperties->Diffuse->Alpha = newAlphaCube; } if (newAlphaSphere >= 0.2f) { GLSphere1->Material->FrontProperties->Diffuse->Alpha = newAlphaSphere; } } keyLeftPressed = currentLeft; keyRightPressed = currentRight; keyUpPressed = currentUp; keyDownPressed = currentDown; } //--------------------------------------------------------------------------- void __fastcall TForm1::TrackBar3KeyDown(TObject *Sender, WORD &Key, TShiftState Shift) { Key = 0; } //--------------------------------------------------------------------------- void __fastcall TForm1::TrackBar2KeyDown(TObject *Sender, WORD &Key, TShiftState Shift) { Key = 0; } //--------------------------------------------------------------------------- void __fastcall TForm1::TrackBar1KeyDown(TObject *Sender, WORD &Key, TShiftState Shift) { Key = 0; } //---------------------------------------------------------------------------