/
DmitryBar
/
computer_modeling
Обзор
Документация
Войти
/
DmitryBar
/
computer_modeling
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
Lab1/source/fRandomStars.cpp
229 строк
6 KB
Dmitry
Выполнение первой лабораторной работы
17 окт 2024, 13:08
17 окт 2024, 13:08
569e597
Код
Авторство
О чём код?
//--------------------------------------------------------------------------- #include <vcl.h> #include <cstdlib> #include <ctime> #include <typeinfo> #include <iostream> #include <cmath> #pragma hdrstop #include "fRandomStars.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma link "GLS.SceneViewer" #pragma link "GLS.Cadencer" #pragma link "GLS.Scene" #pragma link "GLS.Objects" #pragma link "GLS.BaseClasses" #pragma link "GLS.Coordinates" #pragma link "GLS.FPSMovement" #pragma resource "*.dfm" TForm1* Form1; int mx, my; // vars for saving position unsigned int start, end; // vars for checking time TStringList* S1 = new TStringList; TStringList* T1 = new TStringList; unsigned char FColorIndex, FX, FY, FZ; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) {} //--------------------------------------------------------------------------- float* generatePointsForCube() { float* coords = new float[3]; float min = -0.5; float max = 0.5; for (int i = 0; i < 3; ++i) { float random = ((float)rand()) / (float)RAND_MAX; float diff = max - min; float r = random * diff; coords[i] = min + r; } return coords; } //--------------------------------------------------------------------------- float* generatePointsForSphereSurface() { float* coords = new float[3]; float r = 0.5; float theta = (((float)rand()) / (float)RAND_MAX) * 2.0 * M_PI; float phi = (((float)rand()) / (float)RAND_MAX) * M_PI; coords[0] = r * sin(phi) * cos(theta); coords[1] = r * sin(phi) * sin(theta); coords[2] = r * cos(phi); return coords; } //--------------------------------------------------------------------------- float* generatePointsForSphere() { float* coords = new float[3]; float random = ((float)rand()) / (float)RAND_MAX; float r = -0.5 + random; float theta = (((float)rand()) / (float)RAND_MAX) * 2.0 * M_PI; float phi = (((float)rand()) / (float)RAND_MAX) * M_PI; coords[0] = r * sin(phi) * cos(theta); coords[1] = r * sin(phi) * sin(theta); coords[2] = r * cos(phi); return coords; } //--------------------------------------------------------------------------- float* generatePointsForCylinder() { float radius = 0.4; float height = 0.8; float* coords = new float[3]; float angle = static_cast<float>(rand()) / RAND_MAX * 2 * M_PI; float r = static_cast<float>(rand()) / RAND_MAX * radius; float h = static_cast<float>(rand()) / RAND_MAX * height; coords[0] = r * cos(angle); coords[1] = h - 0.4; coords[2] = r * sin(angle); return coords; } //--------------------------------------------------------------------------- float* generatePointsForCylinderSurface() { float radius = 0.4; float height = 0.8; float* coords = new float[3]; float angle = static_cast<float>(rand()) / RAND_MAX * 2 * M_PI; float r = static_cast<float>(rand()) / RAND_MAX * radius; float h = static_cast<float>(rand()) / RAND_MAX * height; if ((rand() % 2) == 0) { coords[0] = cos(angle) * radius; coords[1] = h - 0.4; coords[2] = sin(angle) * radius; return coords; } else { int side = rand() % 2; coords[0] = r * cos(angle); coords[1] = side * height - 0.4; coords[2] = r * sin(angle); return coords; } } void __fastcall TForm1::FormCreate(TObject* Sender) { srand((unsigned)time(0)); } //--------------------------------------------------------------------------- void __fastcall TForm1::GLSceneViewer1MouseDown( TObject* Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { mx = X; my = Y; } //--------------------------------------------------------------------------- void __fastcall TForm1::GLSceneViewer1MouseMove( TObject* Sender, TShiftState Shift, int X, int Y) { if (Shift.Contains(ssLeft)) { GLCamera1->MoveAroundTarget(my - Y, mx - X); mx = X; my = Y; } } //--------------------------------------------------------------------------- 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::Timer1Timer(TObject* Sender) { StatusBar1->Panels->Items[1]->Text = Format("FPS: %.2f", ARRAYOFCONST((GLSceneViewer1->FramesPerSecond()))); GLSceneViewer1->ResetPerformanceMonitor(); } //--------------------------------------------------------------------------- void __fastcall TForm1::GLCadencer1Progress( TObject* Sender, const double deltaTime, const double newTime) { GLSceneViewer1->Invalidate(); } //--------------------------------------------------------------------------- void __fastcall TForm1::btnDrawClick(TObject* Sender) { rgBlock->Enabled = false; // Start checking time start = clock(); float* coords; for (int i = 0; i < seNStars->Value; i++) { switch (rgBlock->ItemIndex) { case 0: coords = generatePointsForCube(); break; case 1: coords = generatePointsForSphere(); break; case 2: coords = generatePointsForSphereSurface(); break; case 3: coords = generatePointsForCylinder(); break; case 4: coords = generatePointsForCylinderSurface(); break; /* case 5: coords = generatePointsForConer(); break; case 6: coords = generatePointsForConerSurface(); break; */ } TGLPoints* star = new TGLPoints(Stars); Stars->Size = 3; Stars->Positions->Add(coords[0], coords[1], coords[2]); Stars->Colors->AddPoint(((float)(rand() % 256)) / 256.0,((float)(rand() % 256)) / 256.0, ((float)(rand() % 256)) / 256.0); } // End of time-checking end = clock(); double ex_time = (end - start) / (double)CLOCKS_PER_SEC; StatusBar1->Panels->Items[0]->Text = "����� ��������: " + FloatToStr(ex_time) + " ���. "; } //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonClearClick(TObject* Sender) { // Delete all points from the scene Stars->Free(); Stars = (TGLPoints*)(dcBlock->AddNewChild(__classid(TGLPoints))); rgBlock->Enabled = true; } //---------------------------------------------------------------------------