Author Topic: Transformation Query  (Read 6060 times)

0 Members and 1 Guest are viewing this topic.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Transformation Query
« Reply #15 on: October 06, 2019, 01:43:11 pm »
Thank Petr,
useful informations... and using alpha channel now I can overlap software image on hardware images.
Programming isn't difficult, only it's  consuming time and coffee

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Transformation Query
« Reply #16 on: October 06, 2019, 03:47:01 pm »
I'm afraid a full-blown tutorial on this subject is going to be 100% needed, starting all the way back at trigonometry and vectors. Maybe I'll be your guy if I have the energy.

The reason vectors are relevant here should be dead-ass simple. You want to feel like you're viewing a 3D world on a 2D screen, right? This is called a projection, an absolute synonym for the dot product I'm talking about.  Yes, Petr, the scalar product yields a single number. But your dismissal of this as an incomplete solution is naive. Its like you're asking for a house to be made of a single piece of wood instead of many pieces. I'm telling you that your answer involves several scalar products in exactly the format I've given.
You're not done when it works, you're done when it's right.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Transformation Query
« Reply #17 on: October 06, 2019, 03:54:36 pm »
Going on with my attempt to use an interpolation method as workaround and to show it graphically

following this info
Quote
The _MAPTRIANGLE statement maps a triangular portion of an image onto a destination image or screen page.
Code: QB64: [Select]
  1. 3D drawing (hardware images only)
  2. _MAPTRIANGLE [{_CLOCKWISE|_ANTICLOCKWISE}] [{_SEAMLESS}] (sx1, sy1)-(sx2, sy2)-(sx3, sy3), source& TO (dx1, dy1, dz1)-(dx2, dy2, dz2)-(dx3, dy3, dz3)[, destination&][{_SMOOTH|_SMOOTHSHRUNK|_SMOOTHSTRETCHED}]]
Quote
For 3D drawing, the destination coordinates represent left (-x) to right (+x), bottom (-y) to top (+y) & furthest (-z) to nearest (z=-1). The center of the screen is therefore (0,0,-1). Note that a z value of 0 will result in off-screen content. The furthest visible z value is -10,000.

and looking at first example of wiki (that of Galleon)
http://www.qb64.org/wiki/MAPTRIANGLE
I typed this code (a mod of your issue) but I cannot get your button overlapped by a rectangular shape at star time!
So I have a huge difficult to use interpolation...we don't start from the same points!

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2. CLS , &HFFFFFF11
  3. LINE (100, 100)-(200, 150), &HFFFFFF00, BF
  4. LINE (100, 100)-(200, 150), &HFF000000, B
  5. COLOR &HFF000000
  6. _PRINTSTRING (125, 120), "Button"
  7. _PRINTSTRING (100, 400), "This is button created in 2D. Button coordinates are 100,100 to 200,150. Press key"
  8. LINE (100, 400)-(800, 416), &HFFFFFF00, BF
  9. _PRINTSTRING (100, 400), "But now. This is placed to space....                                              "
  10. LINE (100, 400)-(800, 416), &HFFFFFF00, BF
  11. s = _COPYIMAGE(_DEST, 33)
  12.  
  13.  
  14. CLS , &H33000000
  15. w = _WIDTH(s)
  16. h = _HEIGHT(s)
  17. g = -1.7
  18. '      (-w/2,-h/2)     (w/2,-h/2)         (-w/2,-h/2)     (w/2,-h/2)
  19. '    point1  (0,0)-----(w,0) point2     point1  (0,0)-----(w,0) point2
  20. '              | \      |                          |      / |
  21. '              |  \     |                          |     /  |
  22. '              |   \    |                          |    /   |
  23. '              |    \   |                          |   /    |
  24. '              |     \  |                          |  /     |
  25. '              |      \ |                          | /      |
  26. '              |       \|                          |/       |
  27. '      point3(0,h)-----(w,h) point4        point3(0,h)-----(w,h) point4
  28. '       (-w/2,h/2)     (w/2,h/2)            (-w/2,h/2)     (w/2,h/2)
  29.     _MAPTRIANGLE (0, 0)-(w, 0)-(0, h), s TO(-2, 2, -1.9)-(2, 2, g)-(-2, -2, -1.9)
  30.     _MAPTRIANGLE (w, 0)-(0, h)-(w, h), s TO(2, 2, g)-(-2, -2, -1.9)-(2, -2, g)
  31.  
  32.     ' these instructions don't work
  33.     '    _MAPTRIANGLE (0, 0)-(w, 0)-(0, h), s TO(-w / 2, -h / 2, -1.9)-(w / 2, -h / 2, g)-(-w / 2, h / 2, -1.9)
  34.     '    _MAPTRIANGLE (w, 0)-(0, h)-(w, h), s TO(w / 2, -h / 2, g)-(-w / 2, h / 2, -1.9)-(w / 2, h / 2, g)
  35.  
  36.     LINE (100, 100)-(200, 150), &HFFFFFFFF, B
  37.     _DISPLAY
  38.     _DELAY .5
  39.  
  40. FOR g = -1.7 TO -6 STEP -.01
  41.     _MAPTRIANGLE (0, 0)-(w, 0)-(0, h), s TO(-2, 2, -1.9)-(2, 2, g)-(-2, -2, -1.9)
  42.     _MAPTRIANGLE (w, 0)-(0, h)-(w, h), s TO(2, 2, g)-(-2, -2, -1.9)-(2, -2, g)
  43.     LINE (100, 100)-(200, 150), &HFFFFFFFF, B
  44.     _DISPLAY
  45.     _DELAY .05
  46.  
  47.  
  48.     _MAPTRIANGLE (0, 0)-(w, 0)-(0, h), s TO(-2, 2, -1.9)-(2, 2, g)-(-2, -2, -1.9)
  49.     _MAPTRIANGLE (w, 0)-(0, h)-(w, h), s TO(2, 2, g)-(-2, -2, -1.9)-(2, -2, g)
  50.     LINE (100, 100)-(200, 150), &HFFFFFFFF, B
  51.     COLOR &HFFFFFFFF
  52.     _PRINTSTRING (450, 584), "Which are 2D coordinates for button now?"
  53.     _DISPLAY
  54.     _DELAY .05
  55.  

So my attempt is stopped!
Programming isn't difficult, only it's  consuming time and coffee

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Transformation Query
« Reply #18 on: October 06, 2019, 05:27:53 pm »
Thank you for trying, TempodiBasic. I'll try it differently, tomorrow. I'm going to do some experiments in the way that STxAxTIC writes about.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Transformation Query
« Reply #19 on: October 08, 2019, 08:58:06 pm »
Hi Petr,

I'm considering writing on vectors and trigonometry - probably in PDF form. However it occurred to me that Sanctum (in Samples) implements this idea without GL. It should follow that in order to get the *same* 3D -> 2D transformation that GL does, there may meed to be some tuning of the magic numbers (for instance field of view distance, abbreviated fovd)

So the chunk that implements the equation is:
Code: QB64: [Select]
  1. ...
  2. IF vectorinview = 1 THEN
  3.     ' Project vectors onto the screen plane.
  4.     vec3Ddotnhat = vec(i, 1) * nhat(1) + vec(i, 2) * nhat(2) + vec(i, 3) * nhat(3)
  5.     vec2D(i, 1) = (vec(i, 1) * uhat(1) + vec(i, 2) * uhat(2) + vec(i, 3) * uhat(3)) * fovd / vec3Ddotnhat
  6.     vec2D(i, 2) = (vec(i, 1) * vhat(1) + vec(i, 2) * vhat(2) + vec(i, 3) * vhat(3)) * fovd / vec3Ddotnhat
  7. ...
  8.  

In the above, vec(i,1), vec(i,2), and vec(i,3) are the x, y, z-components, respectively of a vector that locates a 3D point, such as the corner of a cube, or a spot in a texture, or whatever. In a static world, these vectors never move. In a world where things move, the animations are done in vec(i,j) space. So far so good?

Meanwhile, there are three more vectors at play: uhat, vhat, and nhat. These all end in "hat" to denote that they are unit vectors (of length 1). The vector uhat is locked in the plane of your screen and points left to right. The vector vhat is also locked in the plane of your screen and points down to up. However, do not think of these vectors as *ON* your screen. Instead you must picture a giant plane cutting through your 3D scene, and your screen stays aligned with this plane, but far away. In other words, uhat and vhat track the orientation of the view you're computing. When the player swivels his head, these vectors are what change. Finally nhat is perpendicular to uhat and vhat, and points directly out the screen toward your eye.

In the code, "vec3Ddotnhat" is the projection of the 3D vector along the nhat vector. The answer is just a number, so put it on the shelf for second.

In the next two lines, we calculate the actual position, in 2D cartesian coordinates, of the original 3D point. (Finally we calculate what's ON screen.) How? We take that vector's projection (dot product) along uhat, and then separately along vhat. This will flatten the whole 3D scene onto the big imaginary plane cutting through it. Finally, to achieve the proper illusion of depth, we multiply by the field of view distance and divide by "vec3Ddotnat".

Anyway, hope that helps a little. This is what's going on in the sketch.

....

EDIT: Clipping planes are an advanced lesson - you don't want to render the *entire* level of course all at once - but I think this isn't on target with your question.... yet
« Last Edit: October 09, 2019, 06:24:32 am by STxAxTIC »
You're not done when it works, you're done when it's right.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Transformation Query
« Reply #20 on: October 09, 2019, 09:47:35 am »
I recall a number of years ago I read a Tutorial on converting 3D to 2D. Must admit I never did go down that road in my programming. I wonder if it may still be a good read today and offer something useful to this discussion. The Tutorial was entitled "3D Tutorial for Qbasic". It was written by Matt Bross and appeared on Pete's Qbasic site. Not sure if it's still available. The paper copy I have is 105 pages long and I have scribbled notes in it. It was written on March 15, 1997, so perhaps it is too dated to offer anything now.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Transformation Query
« Reply #21 on: October 09, 2019, 10:41:17 am »
Hi STxAxTIC, thank you for your reply. I will try to do some research in this direction. So far, I haven't done anything because the flu is trying to get me, so I heal. It's a little better, but my condition isn't 100 percent.