-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnit1.cpp
More file actions
268 lines (212 loc) · 7.67 KB
/
Unit1.cpp
File metadata and controls
268 lines (212 loc) · 7.67 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
// ---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
// ---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "RzPanel"
#pragma link "RzPanel"
#pragma link "RzButton"
#pragma resource "*.dfm"
#pragma comment (lib, "glaux.lib")
TForm1 *Form1;
int IndexPoint = 0;
OGL_Coord *OGL_Coord_Arr = new OGL_Coord[200];
// ---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) {
rotation = 0;
}
// ---------------------------------------------------------------------------
void TForm1::InitDisplay(HWND hwnd) {
PIXELFORMATDESCRIPTOR pfd;
int nPixelFormat = 0;
hdc = GetDC(hwnd);
ZeroMemory(&pfd, sizeof(pfd));
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
nPixelFormat = ChoosePixelFormat(hdc, &pfd);
SetPixelFormat(hdc, nPixelFormat, &pfd);
hrc = wglCreateContext(hdc);
wglMakeCurrent(hdc, hrc);
}
// ---------------------------------------------------------------------------
void TForm1::OpenGL_Init() {
// glViewport(0, 0, RzPanel1->Width, RzPanel1->Height);
glGetIntegerv(GL_VIEWPORT, vp);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// glOrtho(-2, 2, -2.0*RzPanel1->Width / RzPanel1->Height, 2.0*RzPanel1->Width / RzPanel1->Height, -2, 2);
// gluPerspective(45.0, (GLfloat)RzPanel1->Width / RzPanel1->Height, 0.1, 100.0);
// gluOrtho2D(-10., 10., -10., 10.);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glSelectBuffer(4, SelectBuf); // ñîçäàíèå áóôåðà âûáîðà
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender) {
InitDisplay(RzPanel1->Handle);
OpenGL_Init();
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender) {
wglMakeCurrent(0, 0);
wglDeleteContext(hrc);
delete[]TextureData;
}
// ---------------------------------------------------------------------------
void TForm1::OpenGL_Draw() {
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0, 1, 0);
// glPointSize(5);
glEnable(GL_POINT_SMOOTH);
glBegin(GL_POINTS);
for (int i = 0; i < IndexPoint; i++) {
glVertex2f(OGL_Coord_Arr[i].X, OGL_Coord_Arr[i].Y);
}
glEnd();
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::BtnExecuteMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
rotation += 5;
InvalidateRect(Handle, 0, false);
}
// ---------------------------------------------------------------------------
void TForm1::Render(GLenum mode) {
// êðàñíûé òðåóãîëüíèê
if (mode == GL_SELECT)
glLoadName(1); // èìåíóåì ïîä èìåíåì 1
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex2f(-1, -1);
glVertex2f(-1, 1);
glVertex2f(-0.1, 0);
glEnd();
// ñèíèé òðåóãîëüíèê
if (mode == GL_SELECT)
glLoadName(2); // èìåíóåì ïîä èìåíåì 2
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_POLYGON);
glVertex2f(0.1, 0);
glVertex2f(1, 1);
glVertex2f(1, -1);
glEnd();
}
// ---------------------------------------------------------------------------
GLint TForm1::DoSelect(GLint x, GLint y) {
GLint hits;
glRenderMode(GL_SELECT); // âêëþ÷àåì ðåæèì âûáîðà
// ðåæèì âûáîðà íóæåí äëÿ ðàáîòû ñëåäóþùèõ êîìàíä
glInitNames(); // èíèöèàëèçàöèÿ ñòåêà èìåí
glPushName(0); // ïîìåùåíèå èìåíè â ñòåê èìåí
glLoadIdentity();
gluPickMatrix(x, RzPanel1->Height - y, 2, 2, vp);
Render(GL_SELECT); // ðèñóåì îáúåêòû ñ èìåíîâàíèåì îáúåêòîâ
hits = glRenderMode(GL_SELECT);
if (hits <= 0)
return -1;
else
return SelectBuf[(hits - 1) * 4 + 3];
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::RzPanel1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) {
// if (Button == mbLeft) {
//
// mouse_OGL_Coord.X = 2.0 * X / RzPanel1->Width - 1;
// mouse_OGL_Coord.Y = 2.0 * (RzPanel1->Height - Y) / RzPanel1->Height - 1;
// AddPoint(mouse_OGL_Coord);
// InvalidateRect(RzPanel1->Handle, NULL, true);
// }
if (Button == mbRight) {
hit = DoSelect(X, Y); // íîìåð îáúåêòà ïîä êóðñîðîì
switch (hit) {
case -1:
ShowMessage("Ïóñòîå ìåñòî");
break;
case 1:
ShowMessage("Êðàñíûé òðåóãîëüíèê");
break;
case 2:
ShowMessage("Ñèíèé òðåóãîëüíèê");
break;
}
}
}
// ---------------------------------------------------------------------------
void TForm1::FindNormal(float v1x, float v1y, float v1z, float v2x, float v2y, float v2z, float v3x, float v3y, float v3z) {
const int x = 0, y = 1, z = 2;
double temp_v1[3], temp_v2[3], temp_length;
temp_v1[x] = v1x - v2x;
temp_v1[y] = v1y - v2y;
temp_v1[z] = v1z - v2z;
temp_v2[x] = v2x - v3x;
temp_v2[y] = v2y - v3y;
temp_v2[z] = v2z - v3z;
// calculate cross product
CNormal[x] = temp_v1[y] * temp_v2[z] - temp_v1[z] * temp_v2[y];
CNormal[y] = temp_v1[z] * temp_v2[x] - temp_v1[x] * temp_v2[z];
CNormal[z] = temp_v1[x] * temp_v2[y] - temp_v1[y] * temp_v2[x];
// normalize normal
temp_length = (CNormal[x] * CNormal[x]) + (CNormal[y] * CNormal[y]) + (CNormal[z] * CNormal[z]);
temp_length = temp_length * temp_length;
// prevent n/0
if (temp_length == 0)
temp_length = 1;
CNormal[x] /= temp_length;
CNormal[y] /= temp_length;
CNormal[z] /= temp_length;
}
// ---------------------------------------------------------------------------
void TForm1::Texture_Init() {
int i = 0;
TextureData = new GLubyte[128 * 128 * 3]; // make room for a 128x128 RGB texture
while (i < 128 * 128 * 3) {
TextureData[i] = 255;
TextureData[i + 1] = 0;
TextureData[i + 2] = 0;
i = i + 3;
}
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &TextureID);
glBindTexture(GL_TEXTURE_2D, TextureID); // now use it
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Texture filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Texture filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // Wrapping of Texture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // Wrapping of Texture
glTexImage2D(GL_TEXTURE_2D, 0, 3, 128, 128, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureData);
glBindTexture(GL_TEXTURE_2D, TextureID);
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::RzPanel1Paint(TObject *Sender) {
OpenGL_Draw();
// Render(GL_SELECT);
SwapBuffers(hdc);
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::RzPanel1Resize(TObject *Sender) {
glViewport(0, 0, RzPanel1->Width, RzPanel1->Height); // Ñáðîñ òåêóùåé îáëàñòè âûâîäà è ïåðñïåêòèâíûõ ïðåîáðàçîâàíèé
glGetIntegerv(GL_VIEWPORT, vp);
}
// ---------------------------------------------------------------------------
void TForm1::AddPoint(OGL_Coord P) {
OGL_Coord_Arr[IndexPoint] = P;
IndexPoint++;
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::RzPanel1MouseMove(TObject *Sender, TShiftState Shift, int X, int Y) {
if (Shift.Contains(ssLeft)) // make sure button is down
{
mouse_OGL_Coord.X = 2.0 * X / RzPanel1->Width - 1;
mouse_OGL_Coord.Y = 2.0 * (RzPanel1->Height - Y) / RzPanel1->Height - 1;
AddPoint(mouse_OGL_Coord);
InvalidateRect(RzPanel1->Handle, NULL, false);
}
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::BtnExecuteClick(TObject *Sender) {
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
ZeroMemory(OGL_Coord_Arr, sizeof(mouse_OGL_Coord)*1000);
IndexPoint = 0;
SwapBuffers(hdc);
}
// ---------------------------------------------------------------------------