CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: how draw 3D just using the math?

  1. #1
    Join Date
    Apr 2009
    Posts
    1,290

    how draw 3D just using the math?

    from what i have learned from others forums and tutorials, i know convert from 3D(X, Y, Z) to 2D(X, Y):
    Code:
    x2D = (x * L) / (z + L)
    y2D = (y * L) / (z + L)
    the 'L' is the distance between the position object\dot and the player\camera.

    Code:
    class Console{
    public:
        HWND myconsole;
        HDC mydc;
        Console()
        {
            myconsole = GetConsoleWindow();
            //Get a handle to device context
            mydc = GetDC(myconsole);
        }
    
    
        ~Console()
        {
            ReleaseDC(myconsole, mydc);
        }
    
    
        operator HDC()
        {
            return mydc;
        }
    
    
        operator HWND()
        {
            return myconsole;
        }
    
    
        RECT GetWindowSize()
        {
            RECT RectWindow;
            GetWindowRect(myconsole,&RectWindow);
            return RectWindow;
        }
    
    
        void Clear(COLORREF Color=RGB(0,0,0))
        {
            //Clear:
            RECT RectWindow;
            GetWindowRect(myconsole,&RectWindow);
            RectWindow.top=0;
            RectWindow.left=0;
            FillRect(mydc,&RectWindow,CreateSolidBrush(Color));
        }
    
    
    }Console;
    after knowing these i drawed a rectangle that scales when i move back and front:
    Code:
    //draw a rectangle        HPEN hWhitePen = (HPEN)GetStockObject(WHITE_PEN);
            HPEN hOldPen = (HPEN)SelectObject(Console, hWhitePen);
            POINT points[5] =
            {
                { (X * MeDistance) / (Z + MeDistance), (Y * MeDistance) / (Z + MeDistance) },
                { Width+X, (Y * MeDistance) / (Z + MeDistance) },
                { Width+X, Height+Y },
                { (X * MeDistance) / (Z + MeDistance), Height+Y },
                { (X * MeDistance) / (Z + MeDistance), (Y * MeDistance) / (Z + MeDistance) },
            };
            if(MeDistance-Z>=-1 && MeDistance-Z<=100)
            {
                HBRUSH hBrush1 = CreateSolidBrush(RGB(121, 90, 0));
                HBRUSH holdBrush = (HBRUSH)SelectObject(Console, hBrush1);
                Polygon(Console, points, 5);
                SelectObject(Console, hOldPen);
                SelectObject(Console, holdBrush );
                DeleteObject(hBrush1);
                DeleteObject(hWhitePen);
            }
    doing the same way, i tried draw the floor without success
    Code:
    //Position and Size Floor:        int FloorX=0, FloorY=1000, FloorZ=0,FloorWidth=1000, FloorHeight=100, FloorZWidth=1000;
    
    
            //floor vectors:
            POINT FloorPoints[5] =
            {
                { (FloorX * MeDistance) / (FloorZ + MeDistance), (FloorY * MeDistance) / (FloorZ + MeDistance) },
                { FloorWidth+FloorX, (FloorY * MeDistance) / (FloorZ + MeDistance) },
                { FloorWidth+FloorX, FloorHeight+FloorY },
                { (FloorX * MeDistance) / (FloorZ + MeDistance), FloorHeight+FloorY },
                { (FloorX * MeDistance) / (FloorZ + MeDistance), (FloorY * MeDistance) / (FloorZ + MeDistance) },
            };
    
    
            //floor color:
            HBRUSH hBrush12 = CreateSolidBrush(RGB(0, 0, 255));
            HBRUSH holdBrush2 = (HBRUSH)SelectObject(Console, hBrush12);
    
    
            //draw the floor:
            Polygon(Console, FloorPoints, 5);
            SelectObject(Console, holdBrush2 );
            DeleteObject(hBrush12);
    i need to understand what i'm doing wrong for draw the floor... can anyone explain to me?

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,117

    Re: how draw 3D just using the math?

    where do you select a pen?

    Code:
    FillRect(mydc,&RectWindow,CreateSolidBrush(Color));
    Note that you have a resource leak here as the created handle isn't deleted.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.6.0)

  3. #3
    Join Date
    Apr 2009
    Posts
    1,290

    Re: how draw 3D just using the math?

    i'm sorry, but creating a pen\brush inside an argument, is a memory leak?
    Code:
    void Clear(COLORREF Color=RGB(0,0,0))    {
            //Clear:
            RECT RectWindow;
            GetWindowRect(myconsole,&RectWindow);
            RectWindow.top=0;
            RectWindow.left=0;
            HBRUSH clean=CreateSolidBrush(Color);
            HBRUSH oldbrush= (HBRUSH)SelectObject(myconsole,clean);
            FillRect(mydc,&RectWindow,clean);
            SelectObject(myconsole, oldbrush);
            DeleteObject(clean);
        }
    the FillRect() needs the brush for be drawed.
    the pen is the default and is white...
    i belive the problem is my calculations:
    Code:
    //floor vectors:
            POINT FloorPoints[5] =
            {
                { (FloorX * MeDistance) / (FloorZ + MeDistance), (FloorY * MeDistance) / (FloorZ + MeDistance) },
                { FloorWidth+FloorX, (FloorY * MeDistance) / (FloorZ + MeDistance) },
                { FloorWidth+FloorX, FloorHeight+FloorY },
                { (FloorX * MeDistance) / (FloorZ + MeDistance), FloorHeight+FloorY },
                { (FloorX * MeDistance) / (FloorZ + MeDistance), (FloorY * MeDistance) / (FloorZ + MeDistance) },
            };
    but i must retest and more see if the calculation is right for draw the floor plane

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,117

    Re: how draw 3D just using the math?

    creating a pen\brush inside an argument, is a memory leak?
    It still creates a handle to an object - just like if it had been a separate statement. But because you don't associate this with a variable, you can't then delete that handle when finished with - hence a resource leak (not a memory leak).
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.6.0)

  5. #5
    Join Date
    Apr 2009
    Posts
    1,290

    Re: how draw 3D just using the math?

    Quote Originally Posted by 2kaud View Post
    It still creates a handle to an object - just like if it had been a separate statement. But because you don't associate this with a variable, you can't then delete that handle when finished with - hence a resource leak (not a memory leak).
    thank you so much for that correction.
    besides that i found another error: division by zero hehehehe
    now theres no memory\resource leak and no division zero.
    how can i learn much more for i create a floor or draw more objects 3D using math?
    i did several searching, but i only get to type of tutorials:
    - Geometrices 2D and 3D: very limited, that's why i continue with problems... but uses math;
    - using OpenlGL or others libraries.
    ok... i'm using only math for learn more

  6. #6
    Join Date
    Apr 2009
    Posts
    1,290

    Re: how draw 3D just using the math?

    i'm testing and i really know that my calculations, on FloorPoints, are wrong... i need learn more and i can't find the right information

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,277

    Re: how draw 3D just using the math?

    Quote Originally Posted by Cambalinho View Post
    i'm testing and i really know that my calculations, on FloorPoints, are wrong... i need learn more and i can't find the right information
    There are many, many books on opengl - have you tried the library or the bookstore?

  8. #8
    Join Date
    Apr 2009
    Posts
    1,290

    Re: how draw 3D just using the math?

    Quote Originally Posted by Arjay View Post
    There are many, many books on opengl - have you tried the library or the bookstore?
    i'm sorry, i'm trying learn with Math and not libraries.
    i can draw a rectangle... but not thinking on Z and ZWidth, like the floor
    Code:
    //the Console is the HDC Window..
    double const MeDistance=500.0f;//i thot the 'MeDistance' was really the distance between me and the object... but it's the view point so can't be changed
    //Position and Size Floor:        int FloorX=0, FloorY=0, FloorZ=-100,FloorWidth=500, FloorHeight=500, FloorZWidth=500;
    
    
            //floor vectors:
            POINT FloorPoints[5] =
            {
                {(FloorX * MeDistance) / (FloorZ + MeDistance), (FloorY * MeDistance) / (FloorZ + MeDistance)   },
                {  (FloorX *MeDistance) / (FloorZ +MeDistance), (FloorHeight)*MeDistance/(FloorZWidth+MeDistance) },
                { (FloorWidth *MeDistance) / (FloorZ +MeDistance), (FloorHeight)*MeDistance/(FloorZWidth+MeDistance)},
                { (FloorWidth *MeDistance) / (FloorZ +MeDistance), (FloorY)*MeDistance/(FloorZWidth+MeDistance) },
                { (FloorX+FloorX)*MeDistance/(FloorZWidth+MeDistance), (FloorY*MeDistance) / (FloorZ*MeDistance) }
            };
    
    
            HBRUSH hBrush1 = CreateSolidBrush(RGB(0, 0, 255));
            HBRUSH holdBrush = (HBRUSH)SelectObject(Console, hBrush1);
            Polygon(Console, FloorPoints, 5);
    
    
            SelectObject(Console, holdBrush );
            DeleteObject(hBrush1);
    like i said the rectangle is showed but not like the floor
    see the blue rectangle.. the problem here isn't that it's little, but isn't showed like a floor
    https://imgur.com/cFw7Vkp

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,117

    Re: how draw 3D just using the math?

    When trying to show 3D on 2D, don't you have to use something like isometric projection - where the x and z axis are inclined to the horizontal plane at an angle of 30 degree?

    See https://en.wikipedia.org/wiki/Isometric_projection
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.6.0)

  10. #10
    Join Date
    Apr 2009
    Posts
    1,290

    Re: how draw 3D just using the math?

    why they always use Matrix?
    i don't know about it... i didn't learned on school(12)

  11. #11
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,723

    Re: how draw 3D just using the math?

    Quote Originally Posted by Cambalinho View Post
    why they always use Matrix?
    i don't know about it... i didn't learned on school(12)
    just because the matrix calculations as well as the formulas with using matrices look like and are much simpler!
    Victor Nijegorodov

  12. #12
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,117

    Re: how draw 3D just using the math?

    Quote Originally Posted by Cambalinho View Post
    why they always use Matrix?
    i don't know about it... i didn't learned on school(12)
    Search for isometric projection calculations - there's loads of sites available.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.6.0)

  13. #13
    Join Date
    Apr 2009
    Posts
    1,290

    Re: how draw 3D just using the math?

    2kaud: true and i have the function updated:
    Code:
    'Draw array Vertices:
    Private Type POINTAPI
        X As Long
        Y As Long
    End Type
    Private Declare Function Polygon Lib "gdi32" (ByVal hdc As Long, lpPoint As POINTAPI, ByVal nCount As Long) As Long
    
    
    Private Type Position3D    
        X As Double
        Y As Double
        Z As Double
    End Type
    
    
    Private Type Size3D
        Width As Long
        Height As Long
        ZDepth As Long
    End Type
    
    Private Function ConvertPositon3DTo2D(Position As Position3D, World3DSize As Size3D) As POINTAPI    
        Dim ConvertedPosition As POINTAPI
        Dim PosZZDepth As Long
        
        PosZZDepth = Position.Z + World3DSize.ZDepth
        
        If (PosZZDepth = 0) Then PosZZDepth = 1 'avoiding division by zero
        
        ConvertedPosition.X = (Position.X * World3DSize.ZDepth / PosZZDepth) + World3DSize.Width / 2
        ConvertedPosition.Y = (Position.Y * World3DSize.ZDepth / PosZZDepth) + World3DSize.Height / 2
        ConvertPositon3DTo2D = ConvertedPosition
    End Function
    it's in VB6 code, but forget that and it's more easy for others readers.
    i need ask more 1 thing: how can i avoid drawing hidden vertices(outside of view\camera)?
    i can compare if the vertice1 is outside the view, but if the vertice2 is inside the view: how can i subtract the vertice1 with view\camera position\size?
    Last edited by Cambalinho; May 13th, 2020 at 03:14 PM.

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)