Recent

Author Topic: Porting 3D Fractal Tree in openGL from Delphi to Lazarus  (Read 2058 times)

Boleeman

  • Sr. Member
  • ****
  • Posts: 490
Porting 3D Fractal Tree in openGL from Delphi to Lazarus
« on: September 09, 2023, 06:03:31 am »
Hi All

I tried converting a 3D Fractal tree that I found on Github that uses openGL from Delphi to Lazarus.

Got it to compile in Lazarus but it then throws an Access Violation Error at the start.
I read something about GL using threads and cylquad: PGLUquadric; using pointers that may be the problem in a few messages in the Lazarus Forumn.

I also did a compile in the Community Version of Delphi and that compiled OK. The program creates it's own API Window and alters the screen resolution. I tried doing a screenshot but I think it was not successful because it was an AP window so no capture could be done? The tree looks fairly realistic.

Using the Arrow keys you can change the perspective of the fractal tree.

I added dglOpenGL.pas to the Lazarus project and fiddled with the gluQuadricTexture(cylquad,gl_true); as it complained about BooleannByte and Byte values.

I changed:

    //gluQuadricTexture(cylquad,gl_true);
    gluQuadricTexture(cylquad, 1 );

Not sure if that was the correct thing to do?

What I wanted was that the program loads in a Windows canvas/Form instead of it's own API window.



Getting a bit lost with this all the GL codes and API. Can anyone possibly help come to the rescue?

I have attached both source codes (The Lazarus converted code and the original Delphi code made by a Russian programmer and uploaded to Github)
« Last Edit: September 10, 2023, 06:43:19 am by Boleeman »

wp

  • Hero Member
  • *****
  • Posts: 12030
Re: Porting 3D Fractal Tree in openGL from Delphi to Lazarus
« Reply #1 on: September 09, 2023, 11:15:23 am »
Please post also the texture bitmap files.

wp

  • Hero Member
  • *****
  • Posts: 12030
Re: Porting 3D Fractal Tree in openGL from Delphi to Lazarus
« Reply #2 on: September 09, 2023, 03:38:54 pm »
Find in the attachment a Lazarus port of the project. I did go a slightly different way, though, and added a TOpenGLControl to the form which encapsulates all the overhead for creating the OpenGL rendering context, getting bitmap format, etc. It basically has two important methods: OnPaint in which all the drawing OpenGL commands go, and OnResize in which the perspective is set up. Plus some initialization work which can go into the form's OnCreate or into some dedicated Init procedure.

Cannot fully test the program because I don't have the texture bitmap files. I thus put the texture-related drawing commands into an {$IFDEF TEXTURE} block. Just activate the {$DEFINE TEXTURE} if you have the texture files (but I did not test this, and there is some chance that it may still be buggy).

Had to modify some view-related parameters because initially I did not see the tree, and I wonder why this works in Delphi.

Boleeman

  • Sr. Member
  • ****
  • Posts: 490
Re: Porting 3D Fractal Tree in openGL from Delphi to Lazarus
« Reply #3 on: September 10, 2023, 12:45:43 am »
Wow, thankyou WP for the conversion of the fractal tree source code to Lazarus. Much appreciated.
"My knowledge of GL was like walking in a very dark room and stumbling everywhere"

"initially I did not see the tree, and I wonder why this works in Delphi"
I used Delphi Community version 11.3 but that program went into a console type mode and it was also going into a different graphics resolution (but I did see the tree).



The 2 textures are attached below.

I tried activating the texture bmp files but I got this error message
The way I understood you was to comment out the  //{$IFDEF TEXTURES} and replace the
FLeaf_Tex and FStem_Tex by their filenames 'leaf.bmp' and 'stem.bmp'
Maybe I misunderstood?

Incompatible types: got "Constant String" expected "LongWord"

         glEnable(GL_TEXTURE_2D);
          //{$IFDEF TEXTURES}
          glBindTexture(GL_TEXTURE_2D, 'stem.bmp');
          //{$ENDIF}
          glRotatef(-90, 1, 0, 0);
          gluQuadricTexture(FCylQuad, GL_TRUE);
          gluCylinder(FCylQuad, 0.1, 0.02, 1.5, 3, 1 );
        glPopMatrix();         


« Last Edit: September 10, 2023, 01:26:31 am by Boleeman »

wp

  • Hero Member
  • *****
  • Posts: 12030
Re: Porting 3D Fractal Tree in openGL from Delphi to Lazarus
« Reply #4 on: September 10, 2023, 01:19:15 am »
I had added the {$IFDEF}s in order to allow switching between texture and non-texture code because I did not have the texture files. To activate the texture code you simply remove the period after the opening curly brace of {.$DEFINE TEXTURES} at the top of the unit to become:
Code: Pascal  [Select][+][-]
  1. {$DEFINE TEXTURES}   // a '.' removed
This way the symbol TEXTURES is defined and the code accessing the textures is activated (the editor displays inactive code in gray).

No other changes required. Well -- except for this: Colors are wrong because the red/blue channel flip is missing in the LoadTexture method. Replace it by this code (and don't forget to add FPImage and IntfGraphics to the uses clause):

Code: Pascal  [Select][+][-]
  1. uses
  2.   FPImage, IntfGraphics;
  3.  
  4. // Load BMP textures
  5. function TMainForm.LoadTexture(AFilename: String; var ATexture: GLuint): Boolean;
  6. var
  7.   pData: Pointer = nil;
  8.   W, H: Integer;
  9.   bmp: TBitmap;
  10.   x, y: Integer;
  11.   img: TLazIntfImage;
  12.   clr: TFPColor;
  13.   tmp: DWord;
  14. begin
  15.   bmp := TBitmap.Create;
  16.   try
  17.     bmp.LoadFromFile(AFileName);
  18.     W := bmp.Width;
  19.     H := bmp.Height;
  20.  
  21.     // Exchange red and blue color channels
  22.     img := bmp.CreateIntfImage;
  23.     try
  24.       for y := 0 to H-1 do
  25.         for x := 0 to W-1 do
  26.         begin
  27.           clr := img.Colors[x, y];
  28.           tmp := clr.Red;
  29.           clr.Red := clr.Blue;
  30.           clr.Blue := tmp;
  31.           img.Colors[x, y] := clr;
  32.         end;
  33.       bmp.LoadFromIntfImage(img);
  34.     finally
  35.       img.Free;
  36.     end;
  37.     pData := Pointer(bmp.RawImage.Data);
  38.  
  39.     if Assigned(pData) then
  40.       Result := True
  41.     else
  42.     begin
  43.       Result := False;
  44.       MessageDlg('Unable to load ' + AFileName, mtError, [mbOK], 0);
  45.       exit;
  46.     end;
  47.  
  48.     glGenTextures(1, @ATexture);
  49.     glBindTexture(GL_TEXTURE_2D, ATexture);
  50.   //  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  51.   //  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  52.  
  53.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_NEAREST);
  54.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); { all of the above can be used }
  55.     gluBuild2DMipmaps(GL_TEXTURE_2D, 3, W, H, GL_RGB, GL_UNSIGNED_BYTE, pData);
  56.   finally
  57.     bmp.Free;
  58.   end;
  59. end;
« Last Edit: September 10, 2023, 01:36:36 am by wp »

Boleeman

  • Sr. Member
  • ****
  • Posts: 490
Re: Porting 3D Fractal Tree in openGL from Delphi to Lazarus
« Reply #5 on: September 10, 2023, 01:36:33 am »
OK I replaced that.

Sorry I misread your reply. I removed the dot and now it works. It looks a bit darker than the Delphi version.
« Last Edit: September 10, 2023, 01:43:01 am by Boleeman »

wp

  • Hero Member
  • *****
  • Posts: 12030
Re: Porting 3D Fractal Tree in openGL from Delphi to Lazarus
« Reply #6 on: September 10, 2023, 01:38:05 am »
I get a bit of a yellow branch?
Please read my previous post again - I had edited it in the meantime to explain how to activate the TEXTURES define.

Boleeman

  • Sr. Member
  • ****
  • Posts: 490
Re: Porting 3D Fractal Tree in openGL from Delphi to Lazarus
« Reply #7 on: September 10, 2023, 01:45:16 am »
Yes now it works. Hooray.

So Happy. I will look over how you have done the code. I very much appreciate your effort.
Here in Melbourne Australia the weather has gotten COLD. We had lots of wind a couple of days ago.
Typical football weather.

Don't know much about OpenGL so this is a good example to learn from as I am really into fractals and graphics.

It is a bit darker than the Delphi version (Maybe got to do with the delphi version going into a different resolution mode) or it could be just because the size is smaller?

I resized the form and it looks similar now. There are some dark patches in the leaves that sort of look too dark.

Was playing around with the left and right arrow keys. These I think alter the camera view.

A BIG Thank You WP.

« Last Edit: September 10, 2023, 06:42:24 am by Boleeman »

Boleeman

  • Sr. Member
  • ****
  • Posts: 490
Re: Porting 3D Fractal Tree in openGL from Delphi to Lazarus
« Reply #8 on: September 10, 2023, 06:40:42 am »
Not sure why, but sometimes my Spacebar press does not always create a New Tree
Seems to be intermittent
after I press the left/right arrow keys first then pressing Spacebar does not work on some occasions

Played around with some leaf texture files.
Attached are some to try out (by placing stem.bmp and leaf.bmp in the tex folder).
I settled for stem.bmp and leaf.bmp in the root of the attached zip file.
« Last Edit: September 10, 2023, 07:00:16 am by Boleeman »

 

TinyPortal © 2005-2018