QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: Petr on October 05, 2019, 07:58:02 am

Title: Transformation Query
Post by: Petr on October 05, 2019, 07:58:02 am
Hi. Is there a function (either directly or in GL) in QB64 to convert the coordinate system from OpenGL to Cartesian coordinates?  (for hardware MAPTRIANGLE screens, it is one transformation settings for this use)
Title: Re: Transformation Query
Post by: bplus on October 05, 2019, 08:44:59 am
Hi. Is there a function (either directly or in GL) in QB64 to convert the coordinate system from OpenGL to Cartesian coordinates?  (for hardware MAPTRIANGLE screens, it is one transformation settings for this use)


Hi Petr,

Isn't it WINDOW and didn't we discuss this before?
Title: Re: Transformation Query
Post by: Petr on October 05, 2019, 11:29:55 am
Hi BPlus, I don't remember. I don't expect the Window statement to solve the shift of the X and Y axes depending on the value of the Z axis.
Title: Re: Transformation Query
Post by: bplus on October 05, 2019, 11:37:44 am
Hi BPlus, I don't remember. I don't expect the Window statement to solve the shift of the X and Y axes depending on the value of the Z axis.


Oh you're talking 3D stuff, now I understand.
Title: Re: Transformation Query
Post by: STxAxTIC on October 05, 2019, 11:59:58 am
Not to rub this in everyone's face again but if you understand vectors, this picture shows how I compute 2D projections (QB64 Cartesian coordinates) form a general 3D construct. The equation circled on the bottom is literally an/the/indistinguishable form the answer you seek.

EDIT: Petr, if this is valuable to you I can help interpret the sketch.
Title: Re: Transformation Query
Post by: Petr on October 05, 2019, 12:58:26 pm
Hi STxAxTIC

If I know the direction of the 3D vector and its size, if the 3D vector points inwards or outwards, it is simply not parallel to the X or Y axis, its size needs to be reduced for 2D viewing (does not extend as much because its direction is not perpendicular to 2D view). Perhaps I wrote it right. I wonder how to calculate this decrease.
Wait! This does actually determine the endpoint of the vector? In fact, the angle of deflection determines the increment ratio in the X and Y axes, right? If the start and end points of the vector had the same depth, then the increment in 3D would be the same as in 2D. However, if the starting and ending 3D points have different depths, then for 2D display it is necessary to calculate the ratio by how much the 2D vector representing the 3D vector on the 2D monitor will grow in a particular axis.

Well, I have to test it!
Title: Re: Transformation Query
Post by: STxAxTIC on October 05, 2019, 06:01:17 pm
You're asking the perfectly right question Petr.

This particular inquiry:

Quote
If I know the direction of the 3D vector and its size, if the 3D vector points inwards or outwards, it is simply not parallel to the X or Y axis, its size needs to be reduced for 2D viewing (does not extend as much because its direction is not perpendicular to 2D view). Perhaps I wrote it right. I wonder how to calculate this decrease.

... is answered by the equation I circled. This will be easiest in vector language, but tell me where you stand real quick. Do you know what I mean by a "dot product" specifically?
Title: Re: Transformation Query
Post by: Petr on October 06, 2019, 05:12:19 am
Hi. I don't know this term (dot product), but I've already looked at Wikipedia for what it is. The scalar product does not offer a solution because the expected result of the function is two numbers. Quite precisely, it is about converting coordinates from 3D to 2D and vice versa. In order to understand one another, run this program to show you my problem (and of course there will be complications with another coordinate system).

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. FOR g = -1.7 TO -4 STEP -.01
  14.     w = _WIDTH(s)
  15.     h = _HEIGHT(s)
  16.     _MAPTRIANGLE (0, 0)-(w, 0)-(0, h), s TO(-2, 2, -1.9)-(2, 2, g)-(-2, -2, -1.9)
  17.     _MAPTRIANGLE (w, 0)-(0, h)-(w, h), s TO(2, 2, g)-(-2, -2, -1.9)-(2, -2, g)
  18.     _DISPLAY
  19.     _DELAY .05
  20.  
  21.     _MAPTRIANGLE (0, 0)-(w, 0)-(0, h), s TO(-2, 2, -1.9)-(2, 2, g)-(-2, -2, -1.9)
  22.     _MAPTRIANGLE (w, 0)-(0, h)-(w, h), s TO(2, 2, g)-(-2, -2, -1.9)-(2, -2, g)
  23.     COLOR &HFFFFFFFF
  24.     _PRINTSTRING (450, 584), "Which are 2D coordinates for button now?"
  25.     _DISPLAY
  26.  


Of course, I can read the coordinates for now, in case the 3D image stays the same. Well, it's not a solution, it's just a bypassing the problem.
Title: Re: Transformation Query
Post by: TempodiBasic on October 06, 2019, 09:40:51 am
Hi Petr
but is the area pictured by _Maptriangles  avaiable for other graphic instructions?

with this I got nothing
Code: QB64: [Select]
  1.     _MAPTRIANGLE (0, 0)-(w, 0)-(0, h), s TO(-2, 2, -1.9)-(2, 2, g)-(-2, -2, -1.9)
  2.     _MAPTRIANGLE (w, 0)-(0, h)-(w, h), s TO(2, 2, g)-(-2, -2, -1.9)-(2, -2, g)
  3.     LINE (100, 100)-(200, 150), &HFFFFFFFF, B ' debug line
  4.     COLOR &HFFFFFFFF
  5.     _PRINTSTRING (450, 584), "Which are 2D coordinates for button now?"
  6.     _DISPLAY
  7.     _DELAY .05
I have tried a method of interpolation between extreme X and X of the point considered ( x1 to new X1 as MaxX to newMaxX) but to verify this I wanted to use a graphic method... but it seems that here I am in the deep blue I am not able to show original button on the screen overlapping the final image got using _maptriangle.

Title: Re: Transformation Query
Post by: STxAxTIC on October 06, 2019, 09:46:59 am
My dear Petr:

Nice job trying to research the dot product, or scalar product, or projection, or whatever anyone likes to call it. Unfortunately:

Quote
The scalar product does not offer a solution because the expected result of the function is two numbers.

EDIT: I just chose a second interpretation of your confusion so read the edit history of this post only if its interesting. Unfortunately its worse than I thought. I'm a little sick today so best of luck with this. The answer is right in front of you where I put it though. Keep on studying. For instance, count the number of dot products in the note I posted. There's a bit more than one.

EDIT 2: Are you familiar with vector language in the first place? I.e., the arrow over a letter means its really a list, right?
Title: Re: Transformation Query
Post by: Petr on October 06, 2019, 09:55:15 am
Hi TempodiBasic, use _DISPLAYORDER and CLS, 0 IN 32 BIT screen for transparent software screen background. As in this source:

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. CLS , 0
  13.  
  14.  
  15. FOR g = -1.7 TO -4 STEP -.01
  16.     w = _WIDTH(s)
  17.     h = _HEIGHT(s)
  18.     _MAPTRIANGLE (0, 0)-(w, 0)-(0, h), s TO(-2, 2, -1.9)-(2, 2, g)-(-2, -2, -1.9)
  19.     _MAPTRIANGLE (w, 0)-(0, h)-(w, h), s TO(2, 2, g)-(-2, -2, -1.9)-(2, -2, g)
  20.     _DISPLAY
  21.     _DELAY .05
  22.     _MAPTRIANGLE (0, 0)-(w, 0)-(0, h), s TO(-2, 2, -1.9)-(2, 2, g)-(-2, -2, -1.9)
  23.     _MAPTRIANGLE (w, 0)-(0, h)-(w, h), s TO(2, 2, g)-(-2, -2, -1.9)-(2, -2, g)
  24.     COLOR &HFF000000
  25.     _PRINTSTRING (40, 584), "Which are 2D coordinates for button now?"
  26.     _DISPLAY
  27.  


Hi STxAxTIC, please be patient with patience, today is not one of my bright days. The scalar product of vectors returns one number. Yes. I agree. To express a point in 2D, however, I need the 2 numbers X, Y. Therefore, I think the scalar product is not related to the 2D conversion. It's possible that I'm wrong, of course.
Title: Re: Transformation Query
Post by: bplus on October 06, 2019, 10:48:17 am
Hi Petr,

Code: QB64: [Select]
  1. _MAPTRIANGLE (w, 0)-(0, h)-(w, h), s TO(2, 2, g)-(-2, -2, -1.9)-(2, -2, g)

This is real eye opener for the 2nd, 3D, way to use _MAPTRIANGLE.

I played around with X, Y of the TO destination because it seems to have completely different coordinate system than screen to which it draws, sort of... like I change all 2's to 1's or 4's and same thing seems to be drawn through the loop of swing the text rectangle on left hinge opening door on right side but when I put 1's in instead of 2's my final screen is different.

So just translating x, y of screen to what _MAPTRIANGLE draws would require a study of Source code to _MAPTRIANGLE to see how it converts X, Y and then of course ultimately the Z's in question.

I don't see how learning Dot Product or Vectors will help you use _MAPTRIANGLE??? but it would give STxAxTIC some company to discuss his favorite subject :D 

(He, STxAxTIC, might consider some sort of tutorial on vectors and how it helps in rendering 3D space, I know I would be interested if he doesn't mind starting at elementary level.)

Title: Re: Transformation Query
Post by: Petr on October 06, 2019, 10:56:20 am
Hi BPlus. MAPTRIANGLE 3D (use hardware images) use OpenGL Coordinate system.


Converting coordinates between OpenGL and 2D is not so easy. This is way how to compile mouse to 3D coordinate system.


I assumed a built-in function in QB64 when it offers 3D and OpenGL environments. It does not really matter if I convert 3D coordinates to 2D or vice versa, unfortunately, without conversion between coordinate systems, the use of the mouse is limited to software screens only or OpenGL without real depth.


Title: Re: Transformation Query
Post by: bplus on October 06, 2019, 11:19:14 am
Hi BPlus. MAPTRIANGLE 3D (use hardware images) use OpenGL Coordinate system.


Converting coordinates between OpenGL and 2D is not so easy. This is way how to compile mouse to 3D coordinate system.


I assumed a built-in function in QB64 when it offers 3D and OpenGL environments. It does not really matter if I convert 3D coordinates to 2D or vice versa, unfortunately, without conversion between coordinate systems, the use of the mouse is limited to software screens only or OpenGL without real depth.

hmm... maybe need source code to GL too to see how it converts the coordinates it is passed from QB64.

... so enter the Matrix... :D
Title: Re: Transformation Query
Post by: Petr on October 06, 2019, 11:26:40 am
BPlus, I think the solution is directly contained in the 3D point itself. Coordinates X, Y, Z. If Z were the same as the depth of the 2D screen, that is, the depth of 3D would be at the same depth as the 2D screen, then X in 2D would be the same as X in 3D and Y as well. (I am thinking of looking directly at the screen). If the depth is higher, the point moves toward the center of the screen (in OpenGL, the starting point is 0.0.0 in the middle of the screen). Problem is, what this ratio is. At least I think so.

BPlus, on the proposal to use the function from OpenGL. It would be nice, but I'm a pessimist. Well, I'll try it somehow (in own way), myself, although it's probably very beyond my knowledge. Thanks for support.
Title: Re: Transformation Query
Post by: TempodiBasic 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.
Title: Re: Transformation Query
Post by: STxAxTIC 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.
Title: Re: Transformation Query
Post by: TempodiBasic 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 (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!
Title: Re: Transformation Query
Post by: Petr 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.
Title: Re: Transformation Query
Post by: STxAxTIC 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
Title: Re: Transformation Query
Post by: Dimster 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.
Title: Re: Transformation Query
Post by: Petr 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.