Results 1 to 31 of 31

Thread: 3D Math: how calculate the 3D?

  1. #1

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    3D Math: how calculate the 3D?

    for i 'convert 3D to 2D' we must create a perspective function:

    Code:
    // calculate the perspective:
    perspect=FocalDistance/(FocalDistance+Pos3D.Z)
    
    // getting the the 2D position using the perspective:
    PosX2D = Pos3D.X * perspect + Center.x
    PosY2D = Pos3D.Y * perspect + Center.y
    
    //the positions are float types
    // now we must convert them using the trunc() function
    now i add 4 points on an array:
    Code:
    Position3D Plane={ {0, 0,0}, {10, 0,100}, {100, 100,100},{0, 100,0}}
    when i press Up or DOWN we move to front or to back, like changing the Z value.
    but when i press the key: the plane is like zoomed instead just move.
    can anyone explain to me what i'm doing wrong.. sometimes i don't get it
    VB6 2D Sprite control

    To live is difficult, but we do it.

  2. #2
    College Grad!!! Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Re: 3D Math: how calculate the 3D?

    I am going to introduce you to Andre LaMothe's technique on creating perspective, from a VB6 point of view to make it easier on ya:

    Assuming you have a 3d model loaded from file, such as an OBJ file which can easily be created using Autodesk 3DS Max, and assuming you have some sort of class to store this object broken down into faces (or polygons) that store vertices, transformed vertices, texture coords, and normals, you will first have your vertices stored into transformed vertices to avoid manipulation of its original vertex list by multiplying each transformed vertex by any translation matrices, rotation matrices, or scalar matrices:

    vb6 Code:
    1. Private Sub transform_poly(ByRef obj As object3d_type, ByVal translate_mat As Matrix, ByVal rotate_mat As Matrix, ByVal scale_mat As Matrix, ByVal index As Long)
    2.     If Not obj.face_list(index) Is Nothing Then
    3.         Set obj.face_list(index).v0 = translate_mat.multiplyVertex(obj.vertex_list(obj.face_list_num(index).v0))
    4.         Set obj.face_list(index).v1 = translate_mat.multiplyVertex(obj.vertex_list(obj.face_list_num(index).v1))
    5.         Set obj.face_list(index).v2 = translate_mat.multiplyVertex(obj.vertex_list(obj.face_list_num(index).v2))
    6.        
    7.         Set obj.face_list(index).v0 = rotate_mat.multiplyVertex(obj.face_list(index).v0)
    8.         Set obj.face_list(index).v1 = rotate_mat.multiplyVertex(obj.face_list(index).v1)
    9.         Set obj.face_list(index).v2 = rotate_mat.multiplyVertex(obj.face_list(index).v2)
    10.        
    11.         Set obj.face_list(index).v0 = scale_mat.multiplyVertex(obj.face_list(index).v0)
    12.         Set obj.face_list(index).v1 = scale_mat.multiplyVertex(obj.face_list(index).v1)
    13.         Set obj.face_list(index).v2 = scale_mat.multiplyVertex(obj.face_list(index).v2)
    14.     End If
    15. End Sub

    Next, you will add the objects world coordinates to the faces vertices:

    vb6 Code:
    1. Private Sub model_to_world_poly(ByRef obj As object3d_type, ByVal index As Long)
    2.     If Not obj.face_list(index) Is Nothing And obj.face_list(index).clipped = False And obj.face_list(index).backface = False Then
    3.         Set obj.face_list(index).v0 = obj.face_list(index).v0.add(obj.world_pos)
    4.         Set obj.face_list(index).v1 = obj.face_list(index).v1.add(obj.world_pos)
    5.         Set obj.face_list(index).v2 = obj.face_list(index).v2.add(obj.world_pos)
    6.     End If
    7. End Sub

    Assuming you have a camera class or matrix of some sort, you then would need to convert the vertices that got moved with the objects world coordinates to camera coordinates by simply multiplying the camera matrix to those vertices:

    vb6 Code:
    1. Private Sub world_to_cam_poly(ByRef obj As object3d_type, ByVal index As Long)
    2.     If Not obj.face_list(index) Is Nothing And obj.face_list(index).clipped = False And obj.face_list(index).backface = False Then
    3.         Set obj.face_list(index).v0 = mat_cam.multiplyVertex(obj.face_list(index).v0)
    4.         Set obj.face_list(index).v1 = mat_cam.multiplyVertex(obj.face_list(index).v1)
    5.         Set obj.face_list(index).v2 = mat_cam.multiplyVertex(obj.face_list(index).v2)
    6.     End If
    7. End Sub

    Now your camera will have a few properties. It will contain a viewplane width, viewplane height, viewport width (the screen basically at that resolution), viewport height, fov (field of view), near Z, far Z, aspect ratio, and a viewing distance.

    Viewplane width and height I generally have at 2 and 2.
    FOV I have at 90 degrees, or just 90.
    Viewport width and heights will just be your resolution or window width and height if in windowed mode
    near Z will be the closest Z possible to the viewer, but must be positive and must not be 0. Usually I have this at 10.
    far Z will be the furthest Z possible from the viewer, must be positive, and must be greater than near Z. Usually I have this at 10000.
    Aspect ratio can be your screen width / screen height or window width / window height
    And the viewing distance formula will play out with this simple formula which can be done during initialization

    Code:
    With cam
         .viewing_distance = 0.5 * .viewplane_width * Tan((.fov / 2) * PI_OVER_180)
    End With
    Now, with that said, camera coords of your vertices will now need converted to perspective coords. This is when X = X/Z and Y = Y/Z will play out:

    vb6 Code:
    1. Private Sub cam_to_perspective_poly(ByRef obj As object3d_type, ByVal index As Long)
    2.     If Not obj.face_list(index) Is Nothing And obj.face_list(index).clipped = False And obj.face_list(index).backface = False Then
    3.         If obj.face_list(index).v0.z >= cam.near_z And obj.face_list(index).v0.z <= cam.far_z Then
    4.             obj.face_list(index).v0.x = cam.stretch_width * cam.viewing_distance * obj.face_list(index).v0.x / obj.face_list(index).v0.z
    5.             obj.face_list(index).v0.y = cam.stretch_height * cam.viewing_distance * obj.face_list(index).v0.y * cam.aspect_ratio / obj.face_list(index).v0.z
    6.         Else
    7.             obj.face_list(index).clipped = True
    8.         End If
    9.        
    10.         If obj.face_list(index).v1.z >= cam.near_z And obj.face_list(index).v1.z <= cam.far_z Then
    11.             obj.face_list(index).v1.x = cam.stretch_width * cam.viewing_distance * obj.face_list(index).v1.x / obj.face_list(index).v1.z
    12.             obj.face_list(index).v1.y = cam.stretch_height * cam.viewing_distance * obj.face_list(index).v1.y * cam.aspect_ratio / obj.face_list(index).v1.z
    13.         Else
    14.             obj.face_list(index).clipped = True
    15.         End If
    16.        
    17.         If obj.face_list(index).v2.z >= cam.near_z And obj.face_list(index).v2.z <= cam.far_z Then
    18.             obj.face_list(index).v2.x = cam.stretch_width * cam.viewing_distance * obj.face_list(index).v2.x / obj.face_list(index).v2.z
    19.             obj.face_list(index).v2.y = cam.stretch_height * cam.viewing_distance * obj.face_list(index).v2.y * cam.aspect_ratio / obj.face_list(index).v2.z
    20.         Else
    21.             obj.face_list(index).clipped = True
    22.         End If
    23.     End If
    24. End Sub

    The stretch width and height is completely optional, and comes in handy when you stretch the window to keep its aspect ratio in tact. Otherwise this can be ommited.

    Finally, you must convert perspective to screen. This is when your center plays a roll:

    vb6 Code:
    1. Private Sub perspective_to_screen_poly(ByRef obj As object3d_type, ByVal index As Long)
    2.     Dim alpha As Double
    3.     Dim beta As Double
    4.  
    5.     alpha = (0.5 * SCREEN_WIDTH - 0.5)
    6.     beta = (0.5 * SCREEN_HEIGHT - 0.5)
    7.  
    8.     If Not obj.face_list(index) Is Nothing And obj.face_list(index).clipped = False And obj.face_list(index).backface = False Then
    9.         obj.face_list(index).v0.x = alpha + alpha * obj.face_list(index).v0.x
    10.         obj.face_list(index).v0.y = beta - beta * obj.face_list(index).v0.y
    11.        
    12.         obj.face_list(index).v1.x = alpha + alpha * obj.face_list(index).v1.x
    13.         obj.face_list(index).v1.y = beta - beta * obj.face_list(index).v1.y
    14.        
    15.         obj.face_list(index).v2.x = alpha + alpha * obj.face_list(index).v2.x
    16.         obj.face_list(index).v2.y = beta - beta * obj.face_list(index).v2.y
    17.     End If
    18. End Sub

    You now have the proper values necessary to render the object. I generally, in VB6 was tapping into the pointer of the video buffer of the video card, and simply manipulated the array with color values. It was quite literally the fastest way to render pixels as I was able to do this:



    Although I was aiming to do ray tracing, which will happen in due time, but I had to change languages as I found out the hardway that VB6 classes were entirely too slow even in 2022, so I had to switch languages to C++ to try and implement the same thing, although no where near done yet.

    [EDIT] As for your controls, simply manipulating the translation values of your camera matrix should move you around within the 3D world just fine. The above technique I shared gives you proper rendering of the 3D objects like in the screenshot.

  3. #3

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: 3D Math: how calculate the 3D?

    my problem was that i used the 0,0,0 when then 0,0,0 is the center of screen and not the top left corner
    now i'm trying avoid drawing the Z negative position
    isn't easy, but i can do it
    VB6 2D Sprite control

    To live is difficult, but we do it.

  4. #4
    College Grad!!! Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Re: 3D Math: how calculate the 3D?

    It's easy. Simply have an if statement checking if the z value is in between your near Z and far Z before rendering that pixel. Generally clipping can be implemented here. However, if all 3 vertices of the polygon are outside the near z, far z, and view planes, you can remove it from the render list entirely as there is no point on rendering a polygon you cannot see.

  5. #5

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: 3D Math: how calculate the 3D?

    Was hard, honestly, but now is almost done
    Now i must create a polygon position for don't lose the original positions. My class need more work, but it's great.
    Thank you so much for all
    VB6 2D Sprite control

    To live is difficult, but we do it.

  6. #6
    College Grad!!! Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Re: 3D Math: how calculate the 3D?

    Thats why you need a transformed vertex list. And youre welcome

  7. #7

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: 3D Math: how calculate the 3D?

    hehehe fixed
    i must update my class position... but, for now, i can draw a polygon without problems
    for draw a texture, i can use the the PlgBlt() API function
    but now i must learn more 1 thing
    1 - the polygon have several points;
    2 - using that we draw a polygon;
    3 - now i need understand: how can i draw the polygon without draw lines(yes the polygon is an line array) above lines?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  8. #8

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: 3D Math: how calculate the 3D?

    Jacob Roman: so what is your advice for draw the texture?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  9. #9

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: 3D Math: how calculate the 3D?

    heres my code updated... with position and Object3D class's:
    Code:
    #include <windows.h>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <math.h>
    #include <array>
    #include <stdlib.h>
    
    using namespace std;
    
    HDC HDCConsole = GetWindowDC(GetConsoleWindow());
    
    class position
    {
        public:
        float X;
        float Y;
        float Z;
        float perespective;
        float foco=300;
        position(float posx=0, float posy=0, float posz=0,float FocalDistance=300)
        {
            X = posx;
            Y = posy;
            Z = posz;
            foco = FocalDistance;
        }
    
        POINT Perspective(int FocalDistance=300, POINT Center={0,0})
        {
            foco=FocalDistance;
            if(Z==0) Z=1;
            if(foco<1) foco=1;
            perespective=foco/(foco+Z);
    
            float PosX2D = X * perespective + Center.x;
            float PosY2D = Y * perespective + Center.y;
            POINT Pos2D;
            Pos2D.x=trunc(PosX2D);
            Pos2D.y=trunc(PosY2D);
            return Pos2D;
        }
    };
    
    vector<position> GetLinePoints(position Origin, position Destination)
    {
        RECT    rcCli;
        GetClientRect(GetConsoleWindow(), &rcCli);
    
        vector<position> LinePoints;
        position LinePoint;
        int dx = abs(Destination.X-Origin.X), sx = Origin.X<Destination.X ? 1 : -1;
        int dy = abs(Destination.Y-Origin.Y), sy = Origin.Y<Destination.Y ? 1 : -1;
        int dz = abs(Destination.Z-Origin.Z), sz = Origin.Z<Destination.Z ? 1 : -1;
        int dm =std::max( { dx, dy, dz } ), i = dm; /* maximum difference */
        Destination.X = Destination.Y = Destination.Z = dm/2; /* error offset */
    
        do
        {
            Destination.X -= dx; if (Destination.X < 0) { Destination.X += dm; Origin.X += sx; }
            Destination.Y -= dy; if (Destination.Y < 0) { Destination.Y += dm; Origin.Y += sy; }
            Destination.Z -= dz; if (Destination.Z < 0) { Destination.Z += dm; Origin.Z += sz; }
            LinePoint.X=Origin.X;
            LinePoint.Y=Origin.Y;
            LinePoint.Z=Origin.Z;
            if (LinePoint.Z>0)
            {
                LinePoints.push_back(LinePoint);
            }
            i--;
        }while(i >=0);
        return LinePoints;
    }
    
    class Object3D
    {
    public:
        vector<position> pos;
        //image texture;
        Object3D(vector<position> ps)
        {
            pos=ps;
        }
    
        void Move(position posit)
        {
            for(unsigned int index=0; index<pos.size(); index++)
            {
                pos[index].X+=posit.X;
                pos[index].Y+=posit.Y;
                pos[index].Z+=posit.Z;
            }
        }
    
        void draw(HDC HDCDestination)
        {
            RECT WindowSize;
            GetClientRect(GetConsoleWindow(), & WindowSize);
            WindowSize.bottom += 30;
    
            //draw Polygon:
            bool PolygonVisible=true;
            vector<position> DrawPolygon = pos;
            POINT PosPolygon[pos.size()];
            for(unsigned int index=0; index<pos.size(); index++)
            {
    
    
                vector<position> pos;
                if(DrawPolygon[index].Z<0 )
                {
                    if(index<DrawPolygon.size()-1)
                    {
                        pos=GetLinePoints(DrawPolygon[index],DrawPolygon[index+1]);
                        if(pos.size()>0)
                            DrawPolygon[index]=pos[0];
    
                    }
                    else
                    {
                        pos=GetLinePoints(DrawPolygon[index-1],DrawPolygon[index]);
                        if(pos.size()>0)
                            DrawPolygon[index]=pos[pos.size()-1];
                    }
                    if(DrawPolygon[index].Z<0  && PolygonVisible==true)
                        PolygonVisible=false;
    
    
                }
                if (PolygonVisible==true && DrawPolygon.size()>0 )
                    PosPolygon[index]=DrawPolygon[index].Perspective(300,{WindowSize.right/2, WindowSize.bottom/2});
            }
            if (PolygonVisible==true && DrawPolygon.size()>0 )
                Polygon(HDCDestination, PosPolygon,DrawPolygon.size());
    
    
        }
    };
    
    bool KeyPressed(int Key)
    {
       return   (GetAsyncKeyState(Key) < 0);
    }
    
    
    int main()
    {
        RECT WindowSize;
        GetClientRect(GetConsoleWindow(), & WindowSize);
        WindowSize.bottom += 30;
        HPEN hPen1 = CreatePen(PS_SOLID, 1, RGB(0, 255, 0));
        HBRUSH hPlane = CreateSolidBrush(RGB(0,255,0));
    
        Object3D Plane({{-470, 255,0},  {-470, 255,1000}, {470, 255,1000},{470, 255,0}});
        Object3D Wall({{-470, -200,0}, {-470, -200,1000}, {-470, 255,1000}, {-470, 255,0}});
        int speed=1;
        HPEN oldhPen =(HPEN) SelectObject(HDCConsole,hPen1 );
    
        do
        {
    
            if(KeyPressed(VK_SHIFT))
            {
                speed = 10;
            }
            else
            {
                speed =1;
            }
            if(KeyPressed(VK_UP))
            {
    
                Plane.Move({0.0,0.0,(float)-speed});
                Wall.Move({0.0,0.0,(float)-speed});
    
            }
            else if(KeyPressed(VK_DOWN))
            {
                Plane.Move({0.0,0.0,(float)speed});
                Wall.Move({0.0,0.0,(float)speed});
            }
            FillRect(HDCConsole,&WindowSize,CreateSolidBrush(RGB(0,0,0)));
    
            Plane.draw(HDCConsole);
            Wall.draw(HDCConsole);
    
    
            Sleep(10);
    
        }while(!(GetKeyState(VK_ESCAPE) & 0x8000));
        SelectObject(HDCConsole,oldhPen );
        DeleteObject(hPen1);
        DeleteObject(hPlane);
        return 0;
    }
    now it's more easy for move and draw
    if you have another way for move, i accept suggestions
    how can i draw the Plane and Wall with right order? for don't been drawed 1 above the other and incorrectly?
    Attachment 185667
    Last edited by joaquim; Sep 4th, 2022 at 04:39 PM.
    VB6 2D Sprite control

    To live is difficult, but we do it.

  10. #10
    College Grad!!! Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Re: 3D Math: how calculate the 3D?

    Quote Originally Posted by joaquim View Post
    Jacob Roman: so what is your advice for draw the texture?
    This is my perspective correct texture mapping method I performed long time ago. This does it per pixel and it is accurate:
    https://www.vbforums.com/attachment....3&d=1387915938


    If you want your walls and planes to be rendered in the right order, you gotta do what is either zsorting the polygons using Quicksort (something i learned in data structures class in college), or you can use what is called a zbuffer, which is simply a 2D array of all of the Z pixels on screen, with no render being a very large impossible to reach value rather than a zero. Very easy to implement, but again, you need to change how you render your polygons as that requires a per pixel render. At least when you do so, you can check the z position of every pixel. If the z position of that pixel you are at is the closest z (the smallest number within your near z and far z range), it will override that pixel in the z buffer.

  11. #11

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: 3D Math: how calculate the 3D?

    "This is my perspective correct texture mapping method I performed long time ago. This does it per pixel and it is accurate:"
    i'm sorry but to much code. ok, you use 'Vertex3D', i use 'position' with more things. but it's the same. the rest is more that i need learn.
    but tell me: the PlgBlt() isn't the best for i start? don't eat too much CPU and works... at least i belive.. the worst is test for the tile lol

    "If you want your walls and planes to be rendered in the right order, you gotta do what is either zsorting the polygons using Quicksort (something i learned in data structures class in college), or you can use what is called a zbuffer, which is simply a 2D array of all of the Z pixels on screen, with no render being a very large impossible to reach value rather than a zero. Very easy to implement, but again, you need to change how you render your polygons as that requires a per pixel render. At least when you do so, you can check the z position of every pixel. If the z position of that pixel you are at is the closest z (the smallest number within your near z and far z range), it will override that pixel in the z buffer."
    please see these 3 vectors\arrays:
    Code:
    Object3D Roof({{-470, -200,0},  {-470, -200,1000}, {470, -200,1000},{470, -200,0}});
        Object3D Floor({{-470, 255,0},  {-470, 255,1000}, {470, 255,1000},{470, 255,0}});
        Object3D Wall({{-470, -200,0}, {-470, -200,1000}, {-470, 255,1000}, {-470, 255,0}});
        Object3D Wall2({{470, -200,0}, {470, -200,1000}, {470, 255,1000}, {470, 255,0}});
    result when i move up:
    Attachment 185672
    why i see that 2 vertical lines(on Z deph)?
    i can control the Z order on Floor, but 1 vertex3d is dependent on next. and, like you see on image, the some parts(depending on Z or even on X(maybe on Y too)(and tested) the Floor must be the 1st be drawed or maybe the last...
    i don't know control these
    VB6 2D Sprite control

    To live is difficult, but we do it.

  12. #12
    College Grad!!! Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Re: 3D Math: how calculate the 3D?

    Your attachment isnt working. Try IMG tags instead of ATTACH tags

  13. #13

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: 3D Math: how calculate the 3D?

    correct me 1 thing: i must draw the walls before the floor\roof... they must have a right order?
    ok.. they are 4 polygons and not 1... but seems that they must have 1 draw order depending on their positions
    i changed the Y and i notice that the roof seems been drawed above the floor, but the floor is the last.....
    Name:  imagem_2022-09-06_205824995.jpg
Views: 277
Size:  26.4 KB
    or
    https://imgur.com/yDiuhPX
    please teach me these drawed order and, like you see, the problem isn't only the Z.
    i can make the HPEN transparent, but when we use a texture, the problem remains
    Last edited by joaquim; Sep 6th, 2022 at 02:58 PM.
    VB6 2D Sprite control

    To live is difficult, but we do it.

  14. #14
    College Grad!!! Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Re: 3D Math: how calculate the 3D?

    Thats why i mentioned earlier that you need to Z Sort the polygons before rendering, since you are using polygon rendering apis. To accomplish this, you get the 3 Z values of every triangle, add them together, and divide them by 3 to obtain the average Z. Every face will have an average Z tied to it.

    Next, you will have a render list of all polygons visible on screen. But, the list will be Quicksorted (faster than bubble sort by the way) of every triangle so it starts from far away to close to the viewer. That way, when you render it, it will be in the correct z order. This still produces problems though, as it is not perfect.

    I recommended a different approach, called a z buffer. But this will require all of your polygons to be rendered, shaded, and texture mapped per pixel. Z buffer is very accurate, and even more accurate when its a double data type.

  15. #15

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: 3D Math: how calculate the 3D?

    i did these change, but the same bug is showed:
    Code:
    Object3D Floor({ {-470, 255,1000}, {470, 255,1000},{470, 255,0}, {-470, 255,0}});
        Object3D Roof({ {-470, -200,1000}, {470, -200,1000},{470, -200,0}, {-470, -200,0}});
    
        Object3D Wall({ {-470, -200,1000}, {-470, 255,1000}, {-470, 255,0}, {-470, -200,0}});
        Object3D Wall2({ {470, -200,1000}, {470, 255,1000}, {470, 255,0}, {470, -200,0}});
    i must combine the Roof, Floor with wall's?(just for give me 1 object)
    the polygon is drawed like a single rectangle.
    VB6 2D Sprite control

    To live is difficult, but we do it.

  16. #16

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: 3D Math: how calculate the 3D?

    i'm sorry Jacob, but can you tell me more?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  17. #17

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: 3D Math: how calculate the 3D?

    fixed
    was just the polygons order, depending on view or position:
    Code:
    if(Roof.pos[0].Y<0)
            {
                Roof.draw(HDCConsole);
                if(Floor.pos[0].Y<0)
                {
                    if(Wall2.pos[0].X>0)
                    {
                        Wall2.draw(HDCConsole);
                        Wall.draw(HDCConsole);
                    }
                    else
                    {
                        Wall.draw(HDCConsole);
                        Wall2.draw(HDCConsole);
                    }
    
                    Floor.draw(HDCConsole);
                }
                else
                {
                   Floor.draw(HDCConsole);
                   if(Wall2.pos[0].X>0)
                    {
                        Wall2.draw(HDCConsole);
                        Wall.draw(HDCConsole);
                    }
                    else
                    {
                        Wall.draw(HDCConsole);
                        Wall2.draw(HDCConsole);
                    }
                }
            }
            else
            {
    
                Floor.draw(HDCConsole);
               if(Wall2.pos[0].X>0)
                {
                    Wall2.draw(HDCConsole);
                    Wall.draw(HDCConsole);
                }
                else
                {
                    Wall.draw(HDCConsole);
                    Wall2.draw(HDCConsole);
                }
                Roof.draw(HDCConsole);
    
            }
    like i said: these 4 polygons are dependent, before draw them, on their positions.
    maybe you can advice me a more perfect\'clean' 'if's'?
    Last edited by joaquim; Sep 9th, 2022 at 04:54 PM.
    VB6 2D Sprite control

    To live is difficult, but we do it.

  18. #18

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: 3D Math: how calculate the 3D?

    heres the result:
    Attachment 185714
    VB6 2D Sprite control

    To live is difficult, but we do it.

  19. #19

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: 3D Math: how calculate the 3D?

    i had more 1 problem, on movement, that i fixed...
    but i need more updates:
    1 - i need understand why i must draw the walls\roofs\floors on right order depending on position(if X is more big than zero or not).. what you can tell me?
    2 - avoiding drawed outside the view... but with sometime i will do it
    3 - yes.. i didn't miss, i have the 3D rotation function:
    Code:
    //the computer use radians and not degrees:
    AngleCoordenate.z = AngleCoordenate.z*PI/180;
    AngleCoordenate.x = AngleCoordenate.x*PI/180;
    AngleCoordenate.y = AngleCoordenate.y*PI/180;
    
    //we substrate the rotation center or the rotation center will be 0,0,0:
    X-=RotCenter.X;
    Y-=RotCenter.Y;
    Z-=RotCenter.Z;
    
    //using the Z, Y and X order:
    Z-YAW:
    x_new = x * cos(alpha) - y * sin(alpha) + z * 0 = x * cos(alpha) - y * sin(alpha)
    y_new = x * sin(alpha) + y * cos(alpha) + z * 0 = x * sin(alpha) + y * cos(alpha)
    z_new = z * 1 = z
    
     Y-Pitch:
    x_new = x * cos(alpha) + y * 0 + z * sin(alpha) = x * cos(alpha) + Z * sin(alpha)
    y_new = y
    z_new = -x * sin(alpha) + Y * 0 + z * cos(alpha) = -x * sin(alpha) + z * cos(alpha)
    
     X-Roll:
    x_new = x
    y_new = z * 0 + y * cos(alpha) - z * sin(alpha) = y * cos(alpha) - z * sin(alpha)
    z_new = x * 0 + Y * sin(alpha) + z * cos(alpha) = Y * sin(alpha) + z * cos(alpha)
    
    //now we add the RotCenter:
    X+=RotCenter.X;
    Y+=RotCenter.Y;
    Z+=RotCenter.Z;
    if i'm mistake, you can correct me

    4 - i need understand how can i calculate the FPS... can you explain that please?(and tell me what is the best FPS)
    5 - after that i will try using the Texture... maybe using PlgBlt() function, but you advice not use it... but it's what i know
    Last edited by joaquim; Sep 11th, 2022 at 05:03 PM.
    VB6 2D Sprite control

    To live is difficult, but we do it.

  20. #20
    College Grad!!! Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Re: 3D Math: how calculate the 3D?

    Quote Originally Posted by joaquim View Post
    fixed
    was just the polygons order, depending on view or position:
    Code:
    if(Roof.pos[0].Y<0)
            {
                Roof.draw(HDCConsole);
                if(Floor.pos[0].Y<0)
                {
                    if(Wall2.pos[0].X>0)
                    {
                        Wall2.draw(HDCConsole);
                        Wall.draw(HDCConsole);
                    }
                    else
                    {
                        Wall.draw(HDCConsole);
                        Wall2.draw(HDCConsole);
                    }
    
                    Floor.draw(HDCConsole);
                }
                else
                {
                   Floor.draw(HDCConsole);
                   if(Wall2.pos[0].X>0)
                    {
                        Wall2.draw(HDCConsole);
                        Wall.draw(HDCConsole);
                    }
                    else
                    {
                        Wall.draw(HDCConsole);
                        Wall2.draw(HDCConsole);
                    }
                }
            }
            else
            {
    
                Floor.draw(HDCConsole);
               if(Wall2.pos[0].X>0)
                {
                    Wall2.draw(HDCConsole);
                    Wall.draw(HDCConsole);
                }
                else
                {
                    Wall.draw(HDCConsole);
                    Wall2.draw(HDCConsole);
                }
                Roof.draw(HDCConsole);
    
            }
    like i said: these 4 polygons are dependent, before draw them, on their positions.
    maybe you can advice me a more perfect\'clean' 'if's'?
    Thats why they need zsorted by average z by the computer, not manually sorted by hand prior to rendering. LOL

    That is exactly what I was trying to tell you the entire time! But it is not needed if you use a zbuffer (https://www.geeksforgeeks.org/z-buff...buffer-method/), which handles cases like zorder without sorting, or piercing polygons (like a triangle stabbing into another).



    Also, why aren't you using matrices or even quaternions for rotation?

  21. #21
    College Grad!!! Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Re: 3D Math: how calculate the 3D?

    In between homework this week (yes i have homework) im gonna tackle your project to show you exactly what i am talking about. Itll be a massive improvement, and polygons will be rendered correctly at any angle

  22. #22

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: 3D Math: how calculate the 3D?

    good step:
    Attachment 185755
    like you see the 3rd point isn't correctly calculated :P
    i don't know why. can you explain better?
    only the 3rd
    but at east i have the picture
    i must calculate\divide the distances and then tile it
    VB6 2D Sprite control

    To live is difficult, but we do it.

  23. #23
    College Grad!!! Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Re: 3D Math: how calculate the 3D?

    Your attachments aren't working. Use [IMG] tags for images.

    I think honestly, as ambitious as is is to do 3D stuff on command prompt, have you considered using Unity? Makes game creation a hell of a lot easier, and can output on either PC, phones, VR, Game consoles, Hololens, etc.



    See that? Used an image tag

  24. #24

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: 3D Math: how calculate the 3D?

    -image removed-
    Last edited by dday9; Sep 15th, 2022 at 08:24 AM.
    VB6 2D Sprite control

    To live is difficult, but we do it.

  25. #25

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: 3D Math: how calculate the 3D?

    to be honest, i'm just learning using 3D Math(by me and not by school or something)
    VB6 2D Sprite control

    To live is difficult, but we do it.

  26. #26
    College Grad!!! Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Re: 3D Math: how calculate the 3D?

    Vertices might be backwards around that 3rd point. You might wanna check the clockwise or counter clockwise orientation

  27. #27

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: 3D Math: how calculate the 3D?

    Object3D Floor({{-470, 255,0}, {-470, 255,1000}, {470, 255,1000},{470, 255,0}});
    Point[0]={-470, 255,0}
    Point[1]= {-470, 255,1000}
    Point[2]= {470, 255,1000}
    Point[3]={470, 255,0}

    'upper-left' = Point[0]
    'upper-right' = Point[1]
    'lower-left' = Point[3]
    i think is on these way.
    thinking on Z from close to far... what is 'upper-left', 'upper-right' and 'lower-left'?
    maybe i miss something here
    i'm trying several ways... but no luck
    VB6 2D Sprite control

    To live is difficult, but we do it.

  28. #28

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: 3D Math: how calculate the 3D?

    [IMG]185791[/IMG]
    Attached Images Attached Images  
    VB6 2D Sprite control

    To live is difficult, but we do it.

  29. #29
    College Grad!!! Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Re: 3D Math: how calculate the 3D?

    There ya go, you were working with quads rather than triangles. Your 4 vertices are upperleft, upperright, lowerleft and lowerright. Instead of 4 vertices, use 6 vertices for two triangles so you can get the orientation correctly. Try using clockwise orientation.

  30. #30

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: 3D Math: how calculate the 3D?

    damn.. i'm feeling crazy: i can't control the 3 points
    i'm trying just draw in a triangle way without success
    VB6 2D Sprite control

    To live is difficult, but we do it.

  31. #31

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: 3D Math: how calculate the 3D?

    i continue testing without success
    VB6 2D Sprite control

    To live is difficult, but we do it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width