diff options
| author | Andrew Guschin <saintruler@gmail.com> | 2020-11-28 22:27:21 +0400 |
|---|---|---|
| committer | Andrew Guschin <saintruler@gmail.com> | 2020-11-28 22:27:21 +0400 |
| commit | 83a6ab41fbdeff8586cfc19bdd31cf0784e992c1 (patch) | |
| tree | b1ba58627c1b41068d6ebea02d52656bb852356b | |
| parent | 064022b153daf8bb9b47036037eebec92cc57ec7 (diff) | |
Что-то обновил
| -rw-r--r-- | task06/Guschin/MyForm.h | 130 |
1 files changed, 92 insertions, 38 deletions
diff --git a/task06/Guschin/MyForm.h b/task06/Guschin/MyForm.h index e63953e..44324f1 100644 --- a/task06/Guschin/MyForm.h +++ b/task06/Guschin/MyForm.h @@ -14,8 +14,18 @@ namespace Guschin { float Vy; float aspectFig; vector<model> models; - mat3 T; + mat4 T; mat3 initT; + + vec3 S, P, u; + float dist; + float fovy, aspect; + float fovy_work, aspect_work; + float near, far; + float n, f; + float l, r, t, b; + + enum projType { Ortho, Frustum, Perspective } pType; public ref class MyForm : public System::Windows::Forms::Form { @@ -49,8 +59,8 @@ namespace Guschin { // openFileDialog // this->openFileDialog->DefaultExt = L"txt"; - this->openFileDialog->Filter = L"Текстовые файлы (*.txt)|*.txt|Все файлы (*.*)|*.*"; - this->openFileDialog->Title = L"Открыть файл"; + this->openFileDialog->Filter = L" (*.txt)|*.txt| (*.*)|*.*"; + this->openFileDialog->Title = L" "; // // btnOpen // @@ -59,7 +69,7 @@ namespace Guschin { this->btnOpen->Name = L"btnOpen"; this->btnOpen->Size = System::Drawing::Size(151, 48); this->btnOpen->TabIndex = 0; - this->btnOpen->Text = L"Открыть"; + this->btnOpen->Text = L""; this->btnOpen->UseVisualStyleBackColor = true; this->btnOpen->Click += gcnew System::EventHandler(this, &MyForm::btnOpen_Click); // @@ -103,23 +113,60 @@ namespace Guschin { Refresh(); } private: + System::Void initWorkPars() + { + n = near; + f = far; + fovy_work = fovy; + aspect_work = aspect; + + float Vy = 2 * near * tan(fovy / 2); + float Vx = aspect * Vy; + l = -Vx / 2; + r = Vx / 2; + b = -Vy / 2; + t = Vy / 2; + dist = length(P - S); + T = lookAt(S, P, u); + } + private: System::Void MyForm_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e) { Graphics^ g = e->Graphics; g->Clear(Color::Aquamarine); Pen^ rectPen = gcnew Pen(Color::Black, 2); g->DrawRectangle(rectPen, left, top, Wx, Wy); + + mat4 proj; + switch (pType) + { + case Ortho: + proj = ortho(l, r, b, t, -n, -f); + break; + case Frustum: + proj = frustum(l, r, b, t, n, f); + break; + case Perspective: + proj = perspective(fovy_work, aspect_work, n, f); + break; + } + + mat3 cdr = cadrRL(vec2(-1.f, -1.f), vec2(2.f, 2.f), vec2(Wcx, Wcy), vec2(Wx, Wy)); + mat4 C = proj * T; + for (int k = 0; k < models.size(); k++) { vector<path> figure = models[k].figure; - mat3 TM = T * models[k].modelM; + mat4 TM = T * models[k].modelM; for (int i = 0; i < figure.size(); i++) { path lines = figure[i]; Pen^ pen = gcnew Pen(Color::FromArgb(lines.color.x, lines.color.y, lines.color.z)); pen->Width = lines.thickness; - vec2 start = normalize(TM * vec3(lines.vertices[0], 1.0)); + vec3 start_3D = normalize(TM * vec4(lines.vertices[0], 1.0)); + vec2 start = normalize(cdr * vec3(vec2(start_3D), 1.f)); for (int j = 1; j < lines.vertices.size(); j++) { - vec2 end = normalize(TM * vec3(lines.vertices[j], 1.0)); + vec3 end_3D = normalize(TM * vec4(lines.vertices[j], 1.0)); + vec2 end = normalize(cdr * vec3(vec2(end_3D), 1.f)); vec2 tmpEnd = end; if (clip(start, end, minX, minY, maxX, maxY)) { g->DrawLine(pen, start.x, start.y, end.x, end.y); @@ -135,7 +182,16 @@ namespace Guschin { } private: System::Void MyForm_KeyDown(System::Object^ sender, System::Windows::Forms::KeyEventArgs^ e) { - float Wcx = ClientRectangle.Width / 2.f; + switch (e->KeyCode) + { + case Keys::Escape: + initWorkPars(); + break; + default: + break; + } + Refresh(); + /*float Wcx = ClientRectangle.Width / 2.f; float Wcy = ClientRectangle.Height / 2.f; switch (e->KeyCode) { @@ -237,7 +293,7 @@ namespace Guschin { default: break; } - Refresh(); + Refresh();*/ } private: System::Void btnOpen_Click(System::Object^ sender, System::EventArgs^ e) { @@ -252,9 +308,9 @@ namespace Guschin { if (in.is_open()) { models.clear(); - mat3 M = mat3(1.f); - mat3 initM; - vector<mat3> transforms; + mat4 M = mat4(1.f); + mat4 initM; + vector<mat4> transforms; vector<path> figure; float thickness = 2; @@ -264,21 +320,17 @@ namespace Guschin { string str; getline(in, str); while (in) { - if ((str.find_first_not_of(" \t\r\n") != string::npos) && (str[0] != '#')) { + if ((str.find_first_not_of("\t\r\n") != string::npos) && (str[0] != '#')) { stringstream s(str); s >> cmd; - if (cmd == "frame") { - s >> Vx >> Vy; - aspectFig = Vx / Vy; - - float aspectRect = Wx / Wy; - mat3 T1 = translate(-Vx / 2, -Vy / 2); - float S = aspectFig < aspectRect ? Wy / Vy : Wx / Vx; - mat3 S1 = scale(S, -S); - mat3 T2 = translate(Wx / 2 + Wcx, Wcy - Wy / 2); - initT = T2 * (S1 * T1); - - T = initT; + if (cmd == "camera") { + s >> S.x >> S.y >> S.z; + s >> P.x >> P.y >> P.z; + s >> u.x >> u.y >> u.z; + } + else if (cmd == "screen") { + s >> fovy_work >> aspect >> near >> far; + fovy = fovy_work / 180.f * Math::PI; } else if (cmd == "color") { s >> r >> g >> b; @@ -287,46 +339,47 @@ namespace Guschin { s >> thickness; } else if (cmd == "path") { - vector<vec2> vertices; + vector<vec3> vertices; int N; s >> N; string str1; while (N > 0) { getline(in, str1); if ((str1.find_first_not_of(" \t\r\n") != string::npos) && (str1[0] != '#')) { - float x, y; + float x, y, z; stringstream s1(str1); - s1 >> x >> y; - vertices.push_back(vec2(x, y)); + s1 >> x >> y >> z; + vertices.push_back(vec3(x, y, z)); N--; } } figure.push_back(path(vertices, vec3(r, g, b), thickness)); } else if (cmd == "model") { - float mVcx, mVcy, mVx, mVy; - s >> mVcx >> mVcy >> mVx >> mVy; + float mVcx, mVcy, mVcz, mVx, mVy, mVz; + s >> mVcx >> mVcy >> mVcz >> mVx >> mVy >> mVz; float S = mVx / mVy < 1 ? 2.f / mVy : 2.f / mVx; - initM = scale(S) * translate(-mVcx, -mVcy); + initM = scale(S, S, S) * translate(-mVcx, -mVcy, -mVcz); figure.clear(); } else if (cmd == "figure") { models.push_back(model(figure, M * initM)); } else if (cmd == "translate") { - float Tx, Ty; - s >> Tx >> Ty; - M = translate(Tx, Ty) * M; + float Tx, Ty, Tz; + s >> Tx >> Ty >> Tz; + M = translate(Tx, Ty, Tz) * M; } else if (cmd == "scale") { float S; s >> S; - M = scale(S) * M; + M = scale(S, S, S) * M; } else if (cmd == "rotate") { float theta; - s >> theta; - M = rotate(-theta / 180.f * Math::PI) * M; + float nx, ny, nz; + s >> theta >> nx >> ny >> nz; + M = rotate(-theta / 180.f * Math::PI, vec3(nx, ny, nz)) * M; } else if (cmd == "pushTransform") { transforms.push_back(M); @@ -338,6 +391,7 @@ namespace Guschin { } getline(in, str); } + initWorkPars(); Refresh(); } } |