Author Topic: 3D Stereo Discs Graphics Program  (Read 3545 times)

0 Members and 1 Guest are viewing this topic.

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
3D Stereo Discs Graphics Program
« on: February 10, 2020, 10:36:28 am »
Back in the days of [abandoned, outdated and now likely malicious qb64 dot net website - don’t go there] (ah, this seems an eternity ago!) I wrote a program which displayed balls in stereoscopic 3D.  In the stereo imaging used, the viewer could actually "see" near objects coming right out of the screen and far object set back from the screen.  The method used was to alternate red and blue images offset in x- by a stereo angle amount.  The program was able to give the stereoscopic behaviour when using Red/Blue 3D glasses.

The method of alternating red and blue images made the viewed image jittery.  I tried to synchronise the alternation with screen refresh rate, but nothing I tried would allow the removal of the jitteriness.  The project was abandoned incomplete.

What I really wanted to do was to display both the red and the blue images together without the second image overwriting the first.  A method to do this is _MEM processing (thanks again to Steve McNeill for his tutorials) where you can simultaneously alter the _RED, _GREEN, _BLUE and _ALHPA parameters of each image picture element.

The program here implements the _MEM method accordingly.  There are 10 images of a disc, each of which moves in the x-, y- and z- directions.  The displacement of the red and blue (green is not used) images is linearly dependent upon the z- position, and this gives the stereoscopic behaviour.  _MAPTRIANGLE(3D) is used to map each image and this handles all the perspective effects (I say, once again, what a marvellous, marvellous QB64 function this is).

If you run this program with Red/Blue 3D glasses (Blue over the right eye), you will see all the normal behaviour of _MAPTRIANGLE(3D), but in addition the discs will appear to move in and out of the screen.  Without the glasses you will see red and blue images overlapping (and giving magenta) with red/blue poking out sideways dependent upon z- position.

I realise that this program will have very limited interest: the number of QB64 members is not large, members who have red/blue glasses and who have an interest in such graphics will be a very select group.  For this reason I leave the program as a curiosity here in this state (there could be improvements which I will not attempt).  The 10 images are formed as separate software objects which are then individually _MEM processed, those images are copied to hardware and then those images displayed and then image-freed.  There will be a method to directly address the screen pixels, but this is a skill level beyond my limited ability.  It works anyway!
Code: QB64: [Select]
  1. '3D Stereo Discs Graphics Program v1 by Qwerkey 10/02/20
  2. '
  3.  
  4. CONST False = 0, True = NOT False, XScreen% = 1100, YScreen% = 800, PiConst! = 4 * ATN(1), ZOffset% = -620
  5. CONST Xst%% = 5, Stereo! = (5 * 2) / YScreen%, Rad%% = 50, Zf! = 1
  6. 'All perspective behaviour handled by _maptriangle(3D), stereo effect x-separation linear with z-
  7. 'Software Images (for _MEM processing).  Hardware images for display
  8.  
  9. DIM CMem(9) AS _MEM, COff AS _OFFSET, Balls!(9, 12), SoftImg&(9), HardImg&(9)
  10.  
  11. FOR K%% = 0 TO 9
  12.     Balls!(K%%, 0) = RND * XScreen% / 2
  13.     Balls!(K%%, 1) = RND * YScreen% / 2
  14.     Balls!(K%%, 2) = RND * YScreen% / 2
  15.     Balls!(K%%, 3) = 2 * PiConst! * (RND - 0.5)
  16.     Balls!(K%%, 4) = 2 * PiConst! * (RND - 0.5)
  17.     Balls!(K%%, 5) = 2 * PiConst! * (RND - 0.5)
  18.     Balls!(K%%, 6) = 0.007 + RND * 0.02
  19.     IF RND > 0.5 THEN Balls!(K%%, 6) = -Balls!(K%%, 6)
  20.     Balls!(K%%, 7) = 0.007 + RND * 0.02
  21.     IF RND > 0.5 THEN Balls!(K%%, 7) = -Balls!(K%%, 7)
  22.     Balls!(K%%, 8) = 0.007 + RND * 0.02
  23.     IF RND > 0.5 THEN Balls!(K%%, 8) = -Balls!(K%%, 8)
  24.     Balls!(K%%, 9) = Stereo! * Balls!(K%%, 2) * SIN(Balls!(K%%, 5))
  25.     SoftImg&(K%%) = _NEWIMAGE(2 * (Rad%% + Xst%%) + 1, 2 * Rad%% + 1, 32)
  26.     CMem(K%%) = _MEMIMAGE(SoftImg&(K%%))
  27.     FOR N% = 0 TO 2 * (Rad%% + Xst%%)
  28.         FOR M% = 0 TO 2 * Rad%%
  29.             COff = 4 * (N% + M% * 2 * (Rad%% + Xst%%) + 1) + CMem(K%%).OFFSET
  30.             _MEMPUT CMem(K%%), COff + 3, 255 AS _UNSIGNED _BYTE
  31.         NEXT M%
  32.     NEXT N%
  33.     HardImg&(K%%) = _COPYIMAGE(SoftImg&(K%%), 33)
  34. NEXT K%%
  35.  
  36. 'Screen (Hardware Only)
  37. SCREEN _NEWIMAGE(XScreen%, YScreen%, 32)
  38. 'Checking off in fully working
  39.  
  40.     _LIMIT 30
  41.  
  42.     FOR K%% = 0 TO 9
  43.         Balls!(K%%, 3) = PiBand!(Balls!(K%%, 3) + Balls!(K%%, 6))
  44.         Balls!(K%%, 4) = PiBand!(Balls!(K%%, 4) + Balls!(K%%, 7))
  45.         Balls!(K%%, 5) = PiBand!(Balls!(K%%, 5) + Balls!(K%%, 8))
  46.         Balls!(K%%, 9) = Stereo! * Balls!(K%%, 2) * SIN(Balls!(K%%, 5))
  47.         FOR N% = 0 TO 2 * (Rad%% + Xst%%)
  48.             FOR M% = 0 TO 2 * Rad%%
  49.                 COff = 4 * (N% + M% * (2 * (Rad%% + Xst%%) + 1)) + CMem(K%%).OFFSET
  50.                 _MEMPUT CMem(K%%), COff + 3, 0 AS _UNSIGNED _BYTE 'Alpha - first set all pixels to transparent
  51.                 Q0% = N% - (Rad%% + Xst%% + Balls!(K%%, 9))
  52.                 Q1% = M% - Rad%%
  53.                 IF SQR(Q0% * Q0% + Q1% * Q1%) <= Rad%% THEN
  54.                     _MEMPUT CMem(K%%), COff + 2, 255 AS _UNSIGNED _BYTE 'Red
  55.                     _MEMPUT CMem(K%%), COff + 3, 255 AS _UNSIGNED _BYTE
  56.                 ELSE
  57.                     _MEMPUT CMem(K%%), COff + 2, 0 AS _UNSIGNED _BYTE 'Red
  58.                 END IF
  59.                 Q0% = N% - (Rad%% + Xst%% - Balls!(K%%, 9))
  60.  
  61.                 IF SQR(Q0% * Q0% + Q1% * Q1%) <= Rad%% THEN
  62.                     _MEMPUT CMem(K%%), COff, 255 AS _UNSIGNED _BYTE 'Blue
  63.                     _MEMPUT CMem(K%%), COff + 3, 255 AS _UNSIGNED _BYTE
  64.                 ELSE
  65.                     _MEMPUT CMem(K%%), COff, 0 AS _UNSIGNED _BYTE 'Blue
  66.                 END IF
  67.             NEXT M%
  68.         NEXT N%
  69.         HardImg&(K%%) = _COPYIMAGE(SoftImg&(K%%), 33)
  70.         Zpos! = Balls!(K%%, 2) * SIN(Balls!(K%%, 5)) * Zf! + ZOffset%
  71.         X1! = Balls!(K%%, 0) * SIN(Balls!(K%%, 3)) + Rad%% + Xst%%
  72.         X0! = Balls!(K%%, 0) * SIN(Balls!(K%%, 3)) - (Rad%% + Xst%%)
  73.         Y1! = Balls!(K%%, 1) * SIN(Balls!(K%%, 4)) - Rad%%
  74.         Y0! = Balls!(K%%, 1) * SIN(Balls!(K%%, 4)) + Rad%%
  75.         _MAPTRIANGLE (0, 0)-(2 * (Rad%% + Xst%%), 0)-(0, 2 * Rad%%), HardImg&(K%%) TO(X0!, Y0!, Zpos!)-(X1!, Y0!, Zpos!)-(X0!, Y1!, Zpos!)
  76.         _MAPTRIANGLE (2 * (Rad%% + Xst%%), 2 * Rad%%)-(0, 2 * Rad%%)-(2 * (Rad%% + Xst%%), 0), HardImg&(K%%) TO(X1!, Y1!, Zpos!)-(X0!, Y1!, Zpos!)-(X1!, Y0!, Zpos!)
  77.         _FREEIMAGE HardImg&(K%%)
  78.     NEXT K%%
  79.     _DISPLAY
  80.  
  81.  
  82. FUNCTION PiBand! (PiAngle!)
  83.     SELECT CASE PiAngle!
  84.         CASE IS < -_PI
  85.             PiBand! = PiAngle! + _PI(2)
  86.         CASE IS > _PI
  87.             PiBand! = PiAngle! - _PI(2)
  88.         CASE ELSE
  89.             PiBand! = PiAngle!
  90.     END SELECT
  91.  

Offline keybone

  • Forum Regular
  • Posts: 116
  • My name a Nursultan Tulyakbay.
    • View Profile
Re: 3D Stereo Discs Graphics Program
« Reply #1 on: February 13, 2020, 05:27:26 am »
That's cool, makes me wish I had a pair of 3d glasses :-)
I am from a Kazakhstan, we follow the hawk.