File: fpv3d_mainform.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (367 lines) | stat: -rw-r--r-- 10,730 bytes parent folder | download | duplicates (5)
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
unit fpv3d_mainform; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, OpenGLContext, Forms, Controls, Graphics,
  Dialogs, EditBtn, StdCtrls, ComCtrls, fpvectorial, gl, glu, FPimage,
  Math, lasvectorialreader;

type

  { TformFPV3D }

  TformFPV3D = class(TForm)
    Button1: TButton;
    btnConvert3DPointArrayToHeightMap: TButton;
    btnRotY: TButton;
    buttonCutFile: TButton;
    buttonRotZ: TButton;
    buttonZoomIn: TButton;

    buttonZoomOut: TButton;  buttonLoad: TButton;
    editFileName: TFileNameEdit;
    glControl: TOpenGLControl;
    labelStatus: TLabel;
    progressBar: TProgressBar;
    procedure btnConvert3DPointArrayToHeightMapClick(Sender: TObject);
    procedure btnRotYClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure buttonCutFileClick(Sender: TObject);
    procedure buttonLoadClick(Sender: TObject);
    procedure buttonRotZClick(Sender: TObject);
    procedure buttonZoomInClick(Sender: TObject);
    procedure buttonZoomOutClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure glControlPaint(Sender: TObject);
    procedure HandleVecDocProgress(APercentage: Byte);
  private
    { private declarations }
    procedure Render3DPointsArrayAlternative1();
    //
    function GetMapHeight(X, Y: Integer): Byte;
    procedure SetVertexColor(bRenderPolygons: Boolean; x, y: Integer);
    procedure RenderHeightMapV1Helper(bRenderPolygons: Boolean);
    procedure RenderHeightMapV1;
  public
    { public declarations }
    VecDoc: TvVectorialDocument;
    glAltitude: Integer;
    glRotateAngleY, glRotateAngleZ: Double;
    HeightMap: TvRasterImage;
  end; 

const
  STEP_SIZE    = 16;		     // Width And Height Of Each Quad (NEW)
  HEIGHT_RATIO = 1.5;		     // Ratio That The Y Is Scaled According To The X And Z (NEW)

var
  formFPV3D: TformFPV3D;

implementation

{$R *.lfm}

{ TformFPV3D }

procedure TformFPV3D.glControlPaint(Sender: TObject);
begin
  glControl.SwapBuffers;

  //Render3DPointsArrayAlternative1;

  RenderHeightMapV1();
end;

procedure TformFPV3D.HandleVecDocProgress(APercentage: Byte);
begin
  progressBar.Position := APercentage;
  Application.ProcessMessages;
end;

procedure TformFPV3D.Render3DPointsArrayAlternative1;
var
  VecPage: TvVectorialPage;
  i: Integer;
  lPoint1, lPoint2, lPoint3: TvPoint;
  lEntity: TvEntity;
  lPos1, lPos2, lPos3: T3DPoint;
  lColor: TFPColor;
begin
  glClearColor(1.0, 1.0, 1.0, 1.0);
  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
  glEnable(GL_DEPTH_TEST);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(45.0, double(width) / height, 0.1, 100.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

  glTranslatef(0.0, 0.0,-glAltitude);

  if glRotateAngleY <> 0 then
    glRotatef(glRotateAngleY, 0, 1, 0);
  if glRotateAngleZ <> 0 then
    glRotatef(glRotateAngleZ, 0, 0, 1);

  VecPage := VecDoc.GetCurrentPageAsVectorial();
  if VecPage = nil then Exit;
  for i := 0 to VecPage.GetEntitiesCount() - 3 do
  begin
    lEntity := VecPage.GetEntity(i);
    if not (lEntity is TvPoint) then Continue;
    lPoint1 := lEntity as TvPoint;

    lEntity := VecPage.GetEntity(i+1);
    if not (lEntity is TvPoint) then Continue;
    lPoint2 := lEntity as TvPoint;

    lEntity := VecPage.GetEntity(i+2);
    if not (lEntity is TvPoint) then Continue;
    lPoint3 := lEntity as TvPoint;

    glBegin(GL_TRIANGLES);		// Drawing Using Triangles
      lPos1 := lPoint1.GetNormalizedPos(VecPage, -1, 1);
      lPos2 := lPoint2.GetNormalizedPos(VecPage, -1, 1);
      lPos3 := lPoint3.GetNormalizedPos(VecPage, -1, 1);
      lColor := lPoint1.Pen.Color;
      glColor3f(lColor.Red / $FFFF, lColor.Green / $FFFF, lColor.Blue / $FFFF);
      glVertex3f(lPos1.X, lPos1.Y, lPos1.Z);
      glVertex3f(lPos2.X, lPos2.Y, lPos2.Z);
      glVertex3f(lPos3.X, lPos3.Y, lPos3.Z);
    glEnd();					// Finished Drawing
  end;
end;

function TformFPV3D.GetMapHeight(X, Y: Integer): Byte;
var
  lPos: TPoint;
begin
  lPos.X := Min(X, HeightMap.RasterImage.Width-1);
  lPos.Y := Min(Y, HeightMap.RasterImage.Height-1);
  Result := Byte(HeightMap.RasterImage.Colors[lPos.X, lPos.Y].Red div $FF);
end;

{-----------------------------------------------------------------------------}
{  Sets The Color Value For A Particular Index, Depending On The Height Index }
{-----------------------------------------------------------------------------}
procedure TformFPV3D.SetVertexColor(bRenderPolygons: Boolean; x, y : Integer);
var fColor : glFloat;
begin
  fColor :=-0.2 + GetMapHeight(X, Y) / $FF;

  // Assign This Blue Shade To The Current Vertex
  if bRenderPolygons then
    glColor3f((220-104*fColor)/256, (220-110*abs(fColor-0.4))/256, (220-200*abs(fColor-0.6))/256)
  else
    glColor3i(0, 0, 0);
end;

procedure TformFPV3D.RenderHeightMapV1Helper(bRenderPolygons: Boolean);
var
  X, Y : Integer;
  x2, y2, z2 : Integer;
begin
  if HeightMap = nil then Exit;
  if HeightMap.RasterImage = nil then Exit;

  if (bRenderPolygons) then                        // What We Want To Render
    glBegin( GL_QUADS )                    // Render Polygons
  else
    glBegin( GL_LINES );                   // Render Lines Instead

  X :=0;
  while X < HeightMap.RasterImage.Width-1 do
  begin
    Y :=0;
    while Y < HeightMap.RasterImage.Height-1 do
    begin
      // Get The (X, Y, Z) Value For The Bottom Left Vertex
      x2 := X;
      y2 := GetMapHeight(X, Y);
      z2 := Y;

      // Set The Color Value Of The Current Vertex
      SetVertexColor(bRenderPolygons, x2, z2);

      // Send This Vertex To OpenGL To Be Rendered (Integer Points Are Faster)
      glVertex3i(x2, y2, z2);

      // Get The (X, Y, Z) Value For The Top Left Vertex
      x2 := X;
      y2 := GetMapHeight(X, Y + STEP_SIZE);
      z2 := Y + STEP_SIZE ;

      // Set The Color Value Of The Current Vertex
      SetVertexColor(bRenderPolygons, x2, z2);

      // Send This Vertex To OpenGL To Be Rendered
      glVertex3i(x2, y2, z2);

      // Get The (X, Y, Z) Value For The Top Right Vertex
      x2 := X + STEP_SIZE;
      y2 := GetMapHeight(X + STEP_SIZE, Y + STEP_SIZE);
      z2 := Y + STEP_SIZE ;

      // Set The Color Value Of The Current Vertex
      SetVertexColor(bRenderPolygons, x2, z2);

      // Send This Vertex To OpenGL To Be Rendered
      glVertex3i(x2, y2, z2);

      // Get The (X, Y, Z) Value For The Bottom Right Vertex
      x2 := X + STEP_SIZE;
      y2 := GetMapHeight(X + STEP_SIZE, Y );
      z2 := Y;

      // Set The Color Value Of The Current Vertex
      SetVertexColor(bRenderPolygons, x2, z2);

      // Send This Vertex To OpenGL To Be Rendered
      glVertex3i(x2, y2, z2);

      Y :=Y + STEP_SIZE
    end;
    X := X + STEP_SIZE
  end;
  glEnd();
  glColor4f(1.0, 1.0, 1.0, 1.0);             // Reset The Color
end;

procedure TformFPV3D.RenderHeightMapV1();
var
  ScaleValue: Double;
begin
  // Init
  glClearColor(0.0, 0.0, 0.0, 0.5); 	   // Black Background
  glShadeModel(GL_SMOOTH);                 // Enables Smooth Color Shading
  glClearDepth(1.0);                       // Depth Buffer Setup
  glEnable(GL_DEPTH_TEST);                 // Enable Depth Buffer
  glDepthFunc(GL_LEQUAL);	           // The Type Of Depth Test To Do
  glDisable(GL_TEXTURE_2D);                // Disable Texture Mapping
  glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);   //Realy Nice perspective calculations

  // Resize
  glViewport(0, 0, Width, Height);    // Set the viewport for the OpenGL window
  glMatrixMode(GL_PROJECTION);        // Change Matrix Mode to Projection
  glLoadIdentity();                   // Reset View
  gluPerspective(45.0, glControl.Width/glControl.Height, 1.0, 500.0);  // Do the perspective calculations. Last value = max clipping depth
  glMatrixMode(GL_MODELVIEW);         // Return to the modelview matrix
  glLoadIdentity();                   // Reset View

  //bRender :=TRUE;
  ScaleValue := 0.18 - glAltitude * 0.01;

  // Paint repetition

  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);    // Clear The Screen And The Depth Buffer
  glLoadIdentity();                                       // Reset The View
  //           Position        View      Up Vector
  gluLookAt(212, 60, 194,  186, 55, 171,  0, 1, 0);	  // This Determines Where The Camera's Position And View Is
  glScalef(scaleValue, scaleValue * HEIGHT_RATIO, scaleValue);

  // Rotation
  if glRotateAngleY <> 0 then
    glRotatef(glRotateAngleY, 0, 1, 0);
  if glRotateAngleZ <> 0 then
    glRotatef(glRotateAngleZ, 0, 0, 1);

  RenderHeightMapV1Helper(True);
  RenderHeightMapV1Helper(False);
end;

procedure TformFPV3D.FormCreate(Sender: TObject);
begin
  VecDoc := TvVectorialDocument.Create;
  glAltitude := 3;
end;

procedure TformFPV3D.buttonLoadClick(Sender: TObject);
begin
  labelStatus.Caption := 'Loading file';
  VecDoc.OnProgress := @HandleVecDocProgress;
  VecDoc.ReadFromFile(editFileName.FileName);
  labelStatus.Caption := 'Done';
  glControl.Invalidate;
end;

procedure TformFPV3D.buttonRotZClick(Sender: TObject);
begin
  glRotateAngleZ := glRotateAngleZ + 10;
  glControl.Invalidate;
end;

procedure TformFPV3D.buttonZoomInClick(Sender: TObject);
begin
  Dec(glAltitude);
  if glAltitude < 1 then glAltitude := 1;
  glControl.Invalidate;
end;

procedure TformFPV3D.buttonZoomOutClick(Sender: TObject);
begin
  Inc(glAltitude);
  glControl.Invalidate;
end;

procedure TformFPV3D.Button1Click(Sender: TObject);
begin
  glControl.Invalidate;
end;

procedure TformFPV3D.btnConvert3DPointArrayToHeightMapClick(Sender: TObject);
var
  lRasterImage: TvRasterImage;
  lPage: TvVectorialPage;
  lFile: TFileStream;
  x, y: Integer;
  lRed: Word;
begin
  lPage := VecDoc.GetPageAsVectorial(0);
  lRasterImage := TvRasterImage.Create(lPage);
  HeightMap := lRasterImage;
  lPage.AddEntity(lRasterImage);
  lRasterImage.InitializeWithConvertionOf3DPointsToHeightMap(lPage, 1024, 1024);

  lFile := TFileStream.Create('Terrain.raw', fmCreate);
  try
    for x := 0 to 1023 do
      for y := 0 to 1023 do
      begin
        lRed := lRasterImage.RasterImage.Colors[x, y].Red;
        lFile.WriteByte(Byte(lRed div $FF));
      end;
  finally
    lFile.Free;
  end;

  glControl.Invalidate;
end;

procedure TformFPV3D.btnRotYClick(Sender: TObject);
begin
  glRotateAngleY := glRotateAngleY + 10;
  glControl.Invalidate;
end;

procedure TformFPV3D.buttonCutFileClick(Sender: TObject);
var
  lPage: TvVectorialPage;
begin
  VecDoc.ReadFromFile(editFileName.FileName);
  //lPage := VecDoc.GetPage(0);
  //while lPage.DeleteEntity(20000) do ;
  VecDoc.WriteToFile(editFileName.FileName + 'smaller.las');
end;

procedure TformFPV3D.FormDestroy(Sender: TObject);
begin
  VecDoc.Free;
end;

end.