-
Notifications
You must be signed in to change notification settings - Fork 0
/
Unit2.cpp
234 lines (210 loc) · 6.63 KB
/
Unit2.cpp
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
//---------------------------------------------------------------------------
#include <fmx.h>
#include <math.h>
#pragma hdrstop
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.fmx"
TForm3D2 *Form3D2;
//---------------------------------------------------------------------------
TPointF *Down;
double MinF = 2000;
double MaxF = -2000;
TVector3D CamVector;
TSpeedButton *Btn;
TTimer *Tmr;
int Count;
//---------------------------------------------------------------------------
double f(int Func, double x, double z)
{
double Result;
double temp = x*x+z*z;
double Pi = 3.1415926535897932384626433832795;
double Epsilon = 1E-20;
if (temp < Epsilon) {
temp = Epsilon;
}
switch (Func) {
case 1: Result = -2000*Sin(temp/180*Pi)/temp;
break;
case 2: Result = -5*Exp(Sin(x/10)-Cos(z/10));
break;
case 3: Result = -5*Exp(Sin(x/10)+Cos(z/10));
break;
case 4: Result = -5*Exp(Sin(Sqrt(fabs(x)))+Cos(Sqrt(fabs(z))));
break;
case 5: Result = -7*Exp(ArcTan2(x*x,z*z));
break;
}
if (Result < MinF) {
MinF = Result;
}
if (Result > MaxF) {
MaxF = Result;
}
return Result;
}
//---------------------------------------------------------------------------
void __fastcall TForm3D2::GenerateMesh(int Func)
{
int MaxX = 60; //30;
int MaxZ = 60; //30;
double u, v;
TPoint3D P0, P1, P2, P3;
double d = 0.5;
int NP = 0;
int NI = 0;
TBitmap* BMP;
int k;
TBitmapData BitmapData;
// We have to set these up front. The buffers are cleared every time Length is set.
Mesh1->Data->VertexBuffer->Length = 2*MaxX*2*MaxZ/d/d*4;
Mesh1->Data->IndexBuffer ->Length = 2*MaxX*2*MaxZ/d/d*6;
BMP = new TBitmap(1,360);
if (BMP->Map(TMapAccess::Write, BitmapData)) {
for (k = 0; k < 360; k++)
BitmapData.SetPixel(0,k, CorrectColor(HSLtoRGB(k/360.0,0.75,0.5)));
BMP->Unmap(BitmapData); // unlock the bitmap
}
u = -MaxX;
while (u < MaxX) {
v = -MaxZ;
while (v < MaxZ) {
// Set up the points in the XZ plane
P0.X = u;
P0.Z = v;
P1.X = u+d;
P1.Z = v;
P2.X = u+d;
P2.Z = v+d;
P3.X = u;
P3.Z = v+d;
// Calculate the corresponding function values for Y = f(X,Z)
P0.Y = f(Func,P0.X,P0.Z);
P1.Y = f(Func,P1.X,P1.Z);
P2.Y = f(Func,P2.X,P2.Z);
P3.Y = f(Func,P3.X,P3.Z);
// Set the points
Mesh1->Data->VertexBuffer->Vertices[NP+0] = P0;
Mesh1->Data->VertexBuffer->Vertices[NP+1] = P1;
Mesh1->Data->VertexBuffer->Vertices[NP+2] = P2;
Mesh1->Data->VertexBuffer->Vertices[NP+3] = P3;
// Map the colors
Mesh1->Data->VertexBuffer->TexCoord0[NP+0] = PointF(0,(P0.Y+35)/45);
Mesh1->Data->VertexBuffer->TexCoord0[NP+1] = PointF(0,(P1.Y+35)/45);
Mesh1->Data->VertexBuffer->TexCoord0[NP+2] = PointF(0,(P2.Y+35)/45);
Mesh1->Data->VertexBuffer->TexCoord0[NP+3] = PointF(0,(P3.Y+35)/45);
// Map the triangles
Mesh1->Data->IndexBuffer->Indices[NI+0] = NP+1;
Mesh1->Data->IndexBuffer->Indices[NI+1] = NP+2;
Mesh1->Data->IndexBuffer->Indices[NI+2] = NP+3;
Mesh1->Data->IndexBuffer->Indices[NI+3] = NP+3;
Mesh1->Data->IndexBuffer->Indices[NI+4] = NP+0;
Mesh1->Data->IndexBuffer->Indices[NI+5] = NP+1;
NP = NP+4;
NI = NI+6;
v = v+d;
}
u = u+d;
}
Mesh1->Data->CalcFaceNormals();
TextureMaterialSource1->Texture = BMP;
Mesh1->MaterialSource = TextureMaterialSource1;
}
//---------------------------------------------------------------------------
__fastcall TForm3D2::TForm3D2(TComponent* Owner)
: TForm3D(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm3D2::Button1Click(TObject *Sender)
{
Text3D1->Text = Button1->Text;
GenerateMesh(1);
}
//---------------------------------------------------------------------------
void __fastcall TForm3D2::Button2Click(TObject *Sender)
{
Text3D1->Text = Button2->Text;
GenerateMesh(2);
}
//---------------------------------------------------------------------------
void __fastcall TForm3D2::Button3Click(TObject *Sender)
{
Text3D1->Text = Button3->Text;
GenerateMesh(3);
}
//---------------------------------------------------------------------------
void __fastcall TForm3D2::Button4Click(TObject *Sender)
{
Text3D1->Text = Button4->Text;
GenerateMesh(4);
}
//---------------------------------------------------------------------------
void __fastcall TForm3D2::Button5Click(TObject *Sender)
{
Text3D1->Text = Button5->Text;
GenerateMesh(5);
}
//---------------------------------------------------------------------------
void __fastcall TForm3D2::CheckBox1Change(TObject *Sender)
{
GridXY->Visible = CheckBox1->IsChecked;
GridXZ->Visible = CheckBox1->IsChecked;
GridYZ->Visible = CheckBox1->IsChecked;
}
//---------------------------------------------------------------------------
void __fastcall TForm3D2::ArcDial1Change(TObject *Sender)
{
Layout3D1->RotationAngle->Z = ArcDial1->Value;
}
//---------------------------------------------------------------------------
void __fastcall TForm3D2::sbDnMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift,
float X, float Y)
{
Count = 0;
Btn = (TSpeedButton*) Sender;
if (Tmr == NULL) {
Tmr = new TTimer(this);
Tmr->Interval = 25;
Tmr->OnTimer = RotateButtonTimer;
}
Tmr->Enabled = True;
}
//---------------------------------------------------------------------------
void __fastcall TForm3D2::sbDnMouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift,
float X, float Y)
{
Tmr->Enabled = False;
}
//---------------------------------------------------------------------------
void __fastcall TForm3D2::tbZoomChange(TObject *Sender)
{
float zoom = tbZoom->Value / 20;
Label1 ->Text = zoom;
TVector3D vec = CamVector;
vec.X = vec.X * zoom;
vec.Y = vec.Y * zoom;
vec.Z = vec.Z * zoom;
Camera1->Position->Vector = vec;
}
//---------------------------------------------------------------------------
void __fastcall TForm3D2::Form3DCreate(TObject *Sender)
{
CamVector = Camera1->Position->Vector;
}
//---------------------------------------------------------------------------
void __fastcall TForm3D2::RotateButtonTimer(TObject *Sender)
{
Count++;
if (Btn == sbUp)
Layout3D1->RotationAngle->X++;
if (Btn == sbDn)
Layout3D1->RotationAngle->X--;
if (Btn == sbRt)
Layout3D1->RotationAngle->Y++;
if (Btn == sbLt)
Layout3D1->RotationAngle->Y--;
}
//---------------------------------------------------------------------------