Author Topic: _MAPTRIANGLE 3D ----> 2D ?  (Read 1159 times)

0 Members and 1 Guest are viewing this topic.

Offline MasterGy

  • Seasoned Forum Regular
  • Posts: 327
  • people lie, math never lies
_MAPTRIANGLE 3D ----> 2D ?
« on: February 20, 2022, 02:47:26 pm »
Hello !
Can you know from somewhere what formula _MAPTRIANGLE 3D calculates 2D points for?

The principle is this: 3D point is called X, Y, Z, then in this plane (2D) it will be.

FlatX = X / Z
FlatY = Y / Z

I used this a long time ago when _MAPTRIANGLE 3D didn’t exist and I know I had to enter a scaling value to make a proportional look of an object on the screen.
I should know what proportional value you are using. Specifically, I need the formula.

For example, if I wanted to detect the displayed 3D object with the position of the mouse, I would need to know the position of the FlatX and FlatY points.

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
Re: _MAPTRIANGLE 3D ----> 2D ?
« Reply #1 on: February 21, 2022, 12:25:38 am »
plot a set of 3D triangles then use POINT to find their corners!

Offline Qwerkey

  • Forum Resident
  • Posts: 755
Re: _MAPTRIANGLE 3D ----> 2D ?
« Reply #2 on: February 21, 2022, 11:10:01 am »
@_vince is quite right if you want to know where the cursor is: POINT(current graphic cursor position method) gives mouse column/row values.

But @MasterGy asks for the formula that _MAPTRIANGLE(3D) uses to work out the 2D screen row(X-)/column(Y-) values.  From the _MAPTRIANGLE (yet another of QB64's amazing bits of coding) wiki, we see that the originator of this code is likely to have been "Andrew L. Ayers" who is not, I think, a current member of the QB64 community but must have been one of @Galleon 's fantastic crew.  So, if you wanted to know what the coding for _MAPTRIANGLE (x,y,z) -> Screen(row,column) is I think we'd have to ask somebody like Steve ( @SMcNeill ) to look into that.  @Petr is a dab-hand at _MAPTRIANGLE(3D), perhaps he knows something?

BUT:  Different (x,y,z) positions can project to the same 2D (row,column) screen positions - because of perspective - so I do not see how looking for cursor(row,column) would uniquely specify a 3D object.

All z- values in _MAPTRIANGLE(3D) must be negative, and all I know is that at z- ~ -400 the projected 3D flat untilted object is the same size as the 2D object (the value of z- to achieve this depends somewhat on the window size): for z- values above this the projection gives a larger size, for z- values below this the projection gives a smaller size.


Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: _MAPTRIANGLE 3D ----> 2D ?
« Reply #3 on: February 21, 2022, 11:12:27 am »
You might find something by taking a deep dive into the QB64 source code for _MapTriangle unless the calculations are done with the _GL library added to QB64. Then you could do a deep dive into _GL source ;-))

Dang Qwerkey posted as I was writing this...

Offline MasterGy

  • Seasoned Forum Regular
  • Posts: 327
  • people lie, math never lies
Re: _MAPTRIANGLE 3D ----> 2D ?
« Reply #4 on: February 21, 2022, 04:49:37 pm »
Actually, by displaying 2 points, it could be counted back to make it work if I use 1 proportionality multiplier. However, if the proportionality factor changes, if there are elements in the formula for calculating the 3D point that do not work as easily as "FLatX = X / Z", I can easily run into chaos.
I want to make a simple 3d editor where I have the ability to place, stretch, and rotate rectangles in space and put textures on it. (I want a horror-fps game. Squeaking door, crying baby, scary castle, ghosts :)
The games so far have been made so I started something and added what I figured out on the fly. It was never an end in itself. It just became something. Unfortunately, if you don’t have a plan in the beginning, things can happen that need to fundamentally change something. So if the system isn’t good for the idea in the first place, it’s a lot of time and brainstorming.
I would like to start building a large castle first, but drawing it in a projection diagram in a booklet would be my brain.
If I can't find a solution to this, I'll calculate the 3d value myself and use _MAPTRIANGLE 2d. The only inconvenience is that the textures have to be displayed in order.
Funny because when I first came across qb64 and saw the _maptriangle statement, I was so happy to be able to drag an image onto the screen at good speed. I didn't know anything about qb64, only what I knew about qb 7.1. I was glad that my old desire, which I could not realize during the dos, would now be my chance. with _maptriangle 2d. I was inattentive because I immediately jumped in to make "Philip and the Resistants." And that required the "FlatX = X / Z" thing in the program and the sorting, which significantly reduced the maximum number of textures because a lot had to be counted. And then I came happily to show it here (I signed up then) and slapped a huge one on my forehead because I didn’t understand when someone asked me here on the forum, I don’t even remember ... “why did you use _maptriangle 2d”? :) I didn't know that _maptriangle can work directly in 3d :)
So the solution will be to turn back to that solution.
Thank you for your answers !

Offline Galleon

  • QB64 Developer
  • Newbie
  • Posts: 25
Re: _MAPTRIANGLE 3D ----> 2D ?
« Reply #5 on: March 01, 2022, 06:35:27 am »
I believe this is the C code used to setup the Open GL 3d viewport. Someone familiar with how Open GL calculates the 2D location of a 3D point should be able to help your further regardless of whether they are familiar with QB64 or not.

qb64\internal\c\libqb\gui.cpp
---------------------------------------------------
void set_view(int32 new_mode)
---------------------------------------------------
static int32 dst_w,dst_h;
dst_w=environment__window_width;
dst_h=environment__window_height;
glViewport(0, 0, (GLsizei)dst_w, (GLsizei)dst_h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//note: the max FOV is 90-degrees (this maximum applies to the longest screen dimension)
float fov;
if (environment_2d__screen_scaled_width>environment_2d__screen_scaled_height){
    fov=90.0f*((float)environment__window_width/(float)environment_2d__screen_scaled_width);
    //convert fov from horizontal to vertical
    fov=fov*((float)dst_h/(float)dst_w);
    }else{
    fov=90.0f*((float)environment__window_height/(float)environment_2d__screen_scaled_height);
}
gluPerspective(fov, (GLfloat)dst_w / (GLfloat)dst_h, 0.1, 10000.0); // Set the Field of view angle (in degrees), the aspect ratio of our window, and the new and far planes 
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
---------------------------------------------------

FYI - If undefined by the underlying library (depends on implementation/platform), gluPerspective is implemented manually by QB64 code as...
------------------------------------
double pi_as_double=3.14159265358979;
        void gluPerspective(double fovy, double aspect, double zNear, double zFar)
        {
            double xmin, xmax, ymin, ymax;
            ymax = zNear * tan(fovy * pi_as_double / 360.0);
            ymin = -ymax;
            xmin = ymin * aspect;
            xmax = ymax * aspect;
            glFrustum(xmin, xmax, ymin, ymax, zNear, zFar);
           
        }
------------------------------------

Offline MasterGy

  • Seasoned Forum Regular
  • Posts: 327
  • people lie, math never lies
Re: _MAPTRIANGLE 3D ----> 2D ?
« Reply #6 on: March 03, 2022, 04:32:29 am »
thanks for the reply Galleon! It’s interesting to look at how he encounters c.