Recent

Author Topic: Simple Perspective program (included)  (Read 2301 times)

speter

  • Full Member
  • ***
  • Posts: 162
Simple Perspective program (included)
« on: December 16, 2020, 04:15:44 am »
Happy Christmas etc everyone :)

Attached is a simple perspective program (which I have written), which might be interesting for anyone who doesn't know how to draw a perspective digitally. It loads a scene file ("hut.per", included in the ZIP); and allows the user to move around. The program uses Cartesian coordinates. The perspective is drawn using a Z-sort system. It doesn't do any lighting calcs; and doesn't include any monsters. :P

I think it is about as comparable to something like the Castle game engine as a bicycle compares to a Ferrari. ;) :o

But, in it is kinda cool. I hope you enjoy it.

The scene file format is really simple, open "hut.per" in a text editor...

I've tested the program with windows 10 (Laz 2.0.8, FPC 3.0.4); but I don't think there are any windows dependencies, so it should run in other OS's.

I'd be interested to hear any feedback you might have (esp if using another OS).

cheers
S. :)

[Deleted ZIP -author request]
« Last Edit: December 18, 2020, 08:05:50 am by trev »
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Laz 2.0.10 / FPC 3.2.0 / Windows 10 (64bit)

Handoko

  • Hero Member
  • *****
  • Posts: 4087
  • My goal: build my own game engine using Lazarus
Re: Simple Perspective program (included)
« Reply #1 on: December 16, 2020, 04:27:04 am »
Wow, it works.
Just opened and run it on my Linux computer.

You must be good in math. You wrote the program without using OpenGL nor DirectX.

I ever did such attempt long time ago. It only showed some boxes in wireframe mode and that run very slow on 386 machine. Yours is much better. Awesome.

Thank you for sharing the code.

Merry Christmas and Happy New Year !

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1282
  • Former Delphi 1-7, 10.2 User
Re: Simple Perspective program (included)
« Reply #2 on: December 16, 2020, 04:58:47 am »
Compiled on macOS (aarch64) - the application runs, it loads the file (tested; incorrect filename causes error), coordinates are filled in, but nothing appears in the window.
Lazarus 2.1 r64368 FPC 3.3.1 r48100 macOS 10.14.6 Xcode 11.3.1
Lazarus 2.1 r64455 3.3.1 r48839   macOS 11.2.2 aarch64 Xcode 12.4
Lazarus 2.1 r61574 3.3.1 r42318 FreeBSD 12.1 amd64 VMware VM
Lazarus 2.1 r61574 3.0.4 Ubuntu 20.04 Parallels VM
Lazarus 2.0.10 3.2.0 Win10 Parallels VM

speter

  • Full Member
  • ***
  • Posts: 162
Re: Simple Perspective program (included)
« Reply #3 on: December 16, 2020, 05:28:47 am »
Thanks Handoko & trev,

@ Handoko: I wrote my first perspective program in 1984 on an Intell 8088 (aka an ancient [pre-IBM) PC; using graphics routines written in assembler (by someone else). :) It used to take 5 minutes to render a simple scene like the hut.

@trev: "incorrect filename causes error" - can you tell me what you mean by this? Wasn't "hut.per" correct (there should be a text file named "hut.per" in the source folder)? Regarding it not drawing (I am assuming you got past the first error); the program basically uses canvas.polygon() to draw the scene - I would have thought that that would be implemented!? If you didn't get past the "incorrect filename" error, then the program wouldn't draw anything. ;)

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Laz 2.0.10 / FPC 3.2.0 / Windows 10 (64bit)

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1282
  • Former Delphi 1-7, 10.2 User
Re: Simple Perspective program (included)
« Reply #4 on: December 16, 2020, 05:38:29 am »
I resolved the incorrect filename error (macOS cannot locate the file) by dragging it from Finder into the filename edit box at which point it does appear to load as the coordinates appear in the edit boxes, but nothing is drawn and there is no further error and nothing of any consequence in the macOS system logs.

I'll try on the Intel macOS machine as soon as it finishes fscking my corrupted 8TB Time Machine backup... it may be a while :)
Lazarus 2.1 r64368 FPC 3.3.1 r48100 macOS 10.14.6 Xcode 11.3.1
Lazarus 2.1 r64455 3.3.1 r48839   macOS 11.2.2 aarch64 Xcode 12.4
Lazarus 2.1 r61574 3.3.1 r42318 FreeBSD 12.1 amd64 VMware VM
Lazarus 2.1 r61574 3.0.4 Ubuntu 20.04 Parallels VM
Lazarus 2.0.10 3.2.0 Win10 Parallels VM

speter

  • Full Member
  • ***
  • Posts: 162
Re: Simple Perspective program (included)
« Reply #5 on: December 16, 2020, 07:55:37 am »
Does anyone out there have experience with drawing on a canvas in macOS?

@trev could you please do a little test!? Create a new (application) project and place a paintbox on the form (and optionally set the paintbox property align=alclient). Then add an OnPaint event to the paintbox with the following code:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.PaintBox1Paint(Sender: TObject);
  2. var
  3.   a : integer;
  4.   poly : array of tpoint;
  5. begin
  6.   randomize;
  7.   setlength(poly,4);
  8.   for a := 0 to 3 do
  9.     poly[a] := point(random(paintbox1.width),random(paintbox1.height));
  10.   paintbox1.canvas.brush.style := bssolid;
  11.   paintbox1.canvas.brush.color := tcolor(random(1 shl 25));
  12.   paintbox1.canvas.polygon(poly);
  13. end;
This code assumes your paintbox is called "paintbox1". :)

This should draw a random 4 sided polygon on the paintbox; and draw a new poly if you resize the application etc.

If this works, then it suggests the error is in my program...

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Laz 2.0.10 / FPC 3.2.0 / Windows 10 (64bit)

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1282
  • Former Delphi 1-7, 10.2 User
Re: Simple Perspective program (included)
« Reply #6 on: December 16, 2020, 09:07:22 am »
Yes! Your basic polygon onpaint application works on macOS.
Lazarus 2.1 r64368 FPC 3.3.1 r48100 macOS 10.14.6 Xcode 11.3.1
Lazarus 2.1 r64455 3.3.1 r48839   macOS 11.2.2 aarch64 Xcode 12.4
Lazarus 2.1 r61574 3.3.1 r42318 FreeBSD 12.1 amd64 VMware VM
Lazarus 2.1 r61574 3.0.4 Ubuntu 20.04 Parallels VM
Lazarus 2.0.10 3.2.0 Win10 Parallels VM

speter

  • Full Member
  • ***
  • Posts: 162
Re: Simple Perspective program (included)
« Reply #7 on: December 16, 2020, 11:34:49 am »
Tomorrow, I'll put together a version using the file-open dialog; hopefully that will play-better with the macOS, look for it in about 14 hours.  8)
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Laz 2.0.10 / FPC 3.2.0 / Windows 10 (64bit)

speter

  • Full Member
  • ***
  • Posts: 162
Re: Simple Perspective program (included)
« Reply #8 on: December 17, 2020, 01:55:07 am »
G'Day Folks,

Attached to this message is a new version of my perspective program.

Changes include:
    + added Form_info_panel and "Info Panel" checkbox.
    + removed scene filename edit box, replaced with "Open (hut)" button.
    + added "Open..." button (which uses the File-Open Dialog).
          ^^ uses paramstr(0) to set opendialog1.InitialDir
    + changed optimisation level from 3, down to 1

I still can't see why trev's Mac was having trouble drawing the scene!?

Also, I am unsure whether paramstr(0) works in non Windows' OSs!?
So, if you have issues with that, comment out that code - the first line in TForm1.Button_file_openClick().

The first button in the Info Panel is "Summary"; on my computer this gives:

Quote
Summary
--------------
Description: hut
Orig Eye Pt: 10.000 -10.000 5.000
Orig Target Pt: 3.500 2.000 2.000
Current Eye Pt: 10.000 -10.000 5.000
Current Target Pt: 3.500 2.000 2.000
Num Faces: 26
Num Points: 51
Num Screen Pts: 51
Paitnbox (w,h): 774,868

Once "hut.per" is opened all those values should be the same (except maybe the paintbox dimension - if the user has resized the application window or the OS makes the program form a different size etc).

All the points should range from (-3, -5, -0.2) - (10, 7, 3.7).
The "screen points" should range from (0,0) - (paintbox.width-1, paintbox.height-1).

If the program works ok, I'd suggest compiling with optimisation level 2 or 3.

I'd really like to hear from macOS folks, about their experiences.  8-)

cheers
S. :)

Edit: Corrected the points' extents.

[Deleted ZIP -author request]
« Last Edit: December 18, 2020, 08:06:20 am by trev »
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Laz 2.0.10 / FPC 3.2.0 / Windows 10 (64bit)

speter

  • Full Member
  • ***
  • Posts: 162
Re: Simple Perspective program (included)
« Reply #9 on: December 17, 2020, 02:59:39 am »
@Handoko: If you are interested in the math; have a look at scene.draw() - both in sceneu.pas. :)

cheers
S. :o
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Laz 2.0.10 / FPC 3.2.0 / Windows 10 (64bit)

Handoko

  • Hero Member
  • *****
  • Posts: 4087
  • My goal: build my own game engine using Lazarus
Re: Simple Perspective program (included)
« Reply #10 on: December 17, 2020, 03:24:04 am »
Of course I will. I can do some simple perspective drawing using OpenGL and DirectX but I always wonder how it can be done manually.

Also, I am unsure whether paramstr(0) works in non Windows' OSs!?

Both the open buttons work correctly on Linux.

There were some discussions about it, maybe you will be interested:
https://forum.lazarus.freepascal.org/index.php/topic,34273.0.html
https://forum.lazarus.freepascal.org/index.php/topic,51692.0.html

    + changed optimisation level from 3, down to 1

It runs without problem using level 4 optimization on Lazarus 2.0.10 GTK2 Ubuntu 20.04 64-bit, below is the screenshot.

One small issue is, Ubuntu use relatively large font by default. You should reduce the font size or make the buttons a bit bigger.

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1282
  • Former Delphi 1-7, 10.2 User
Re: Simple Perspective program (included)
« Reply #11 on: December 17, 2020, 11:51:18 am »
1. paramstr(0) shouldn't be used for any UNIX or UNIX-like OS for the reasons detailed here.

2. The Open File dialog currently initialises to the folder containing the executable which, because of the macOS application bundle concept is in my case: /Users/trev/Documents/Programming/LAZPROJECTS/laz_persp/persp.app/Contents/MacOS. To locate the bundle folder's folder, see this Wiki page.

3. This time I compiled it on the Intel Mac mini and the result was the same as before on the ARM64 Mac mini - nothing gets drawn on the paintbox. I've included a screen capture with the info panel displayed after loading the file.

4. I noticed a typo (transposition) in the info panel summary "Paitnbox".
« Last Edit: December 17, 2020, 11:53:34 am by trev »
Lazarus 2.1 r64368 FPC 3.3.1 r48100 macOS 10.14.6 Xcode 11.3.1
Lazarus 2.1 r64455 3.3.1 r48839   macOS 11.2.2 aarch64 Xcode 12.4
Lazarus 2.1 r61574 3.3.1 r42318 FreeBSD 12.1 amd64 VMware VM
Lazarus 2.1 r61574 3.0.4 Ubuntu 20.04 Parallels VM
Lazarus 2.0.10 3.2.0 Win10 Parallels VM

speter

  • Full Member
  • ***
  • Posts: 162
Re: Simple Perspective program (included)
« Reply #12 on: December 17, 2020, 12:16:48 pm »
Thanks Handoko & trev for your time and effort.

Both of you noted I shouldn't use Paramstr(0). :) So, I guess I should remove that. :o

@trev: do you have any idea what's happening here!? Does anything get drawn if you check "wire"?

I had thought that maybe I hadn't set the length of one of the arrays, but they all seem ok.

cheers
S.

Edit: removed list of screen coords.
« Last Edit: December 18, 2020, 03:53:38 am by speter »
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Laz 2.0.10 / FPC 3.2.0 / Windows 10 (64bit)

circular

  • Hero Member
  • *****
  • Posts: 3668
    • Personal webpage
Re: Simple Perspective program (included)
« Reply #13 on: December 17, 2020, 12:19:00 pm »
To display something on MacOS (and on Qt) you must draw in the OnPaint event, never outside of it. So when you want to redraw, you can for example call Invalidate and in the OnPaint event call the drawing method.

To get the location of your program, you can use Application.ExeName.
Conscience is the debugger of the mind

speter

  • Full Member
  • ***
  • Posts: 162
Re: Simple Perspective program (included)
« Reply #14 on: December 17, 2020, 12:55:36 pm »
Thanks very much circular!

I will need to think that through in the morning. I'll hopefully post a another version in 12 hours or so. :)

When I moved scene.draw() to the paintbox1.onpaint method (and replaced other scene.draw() instances with paintbox1.invalidate), the program endlessly redraws the scene. So (clearly) I am triggering the event again somewhere. :o

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Laz 2.0.10 / FPC 3.2.0 / Windows 10 (64bit)

 

TinyPortal © 2005-2018