-
April 4th, 2020, 09:55 AM
#1
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?
-
April 4th, 2020, 11:19 AM
#2
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++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 4th, 2020, 11:46 AM
#3
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
-
April 4th, 2020, 12:04 PM
#4
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++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 4th, 2020, 12:36 PM
#5
Re: how draw 3D just using the math?
Originally Posted by 2kaud
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
-
April 4th, 2020, 02:00 PM
#6
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
-
April 9th, 2020, 08:14 AM
#7
Re: how draw 3D just using the math?
Originally Posted by Cambalinho
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?
-
April 9th, 2020, 08:26 AM
#8
Re: how draw 3D just using the math?
Originally Posted by Arjay
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
-
April 9th, 2020, 10:55 AM
#9
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++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 9th, 2020, 01:09 PM
#10
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º)
-
April 9th, 2020, 01:41 PM
#11
Re: how draw 3D just using the math?
Originally Posted by Cambalinho
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
-
April 10th, 2020, 03:42 AM
#12
Re: how draw 3D just using the math?
Originally Posted by Cambalinho
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++23 Compiler: Microsoft VS2022 (17.6.5)
-
May 13th, 2020, 03:05 PM
#13
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.
-
June 27th, 2020, 01:43 PM
#14
Re: how draw 3D just using the math?
2kaud these function convert the 3D to 2D and add the vector to center of the screen: "+ World3DSize.Width / 2"
if i use the camera position zero(from the window) and the size(from window), how can i calculate the camera position and size?
the camera goes from zero to window size(all the window).
if the plane position is zero, it will be half of window... how can i add it to camera?
Code:
Private Sub Form_Resize() 'ScaleMode = vbPixels
'AutoRedraw = True
FillPosition3D Player1.Position, Me.ScaleWidth / 2, Me.ScaleHeight / 2, 0 'Camera Position
camera1.Size.Height = Me.ScaleHeight + 200
camera1.Size.Width = Me.ScaleWidth
camera1.Position.X = -Me.ScaleWidth / 2
camera1.Position.Y = -Me.ScaleHeight / 2
camera1.Position.Z = -100
camera1.Size.ZDepth = 20000
camera1.Size.distance = 100
ScrollChanged 'draw the things
End Sub
i can draw the rectangle, but when i use the:
Code:
Private Function IsOnCamera(VerticePosition As Position3D, CameraPosition As Position3D, CameraSize As Size3D) As Boolean
If (((VerticePosition.Z) >= CameraPosition.Z) And ((VerticePosition.Z) <= (CameraPosition.Z + CameraSize.ZDepth)) _
And (((VerticePosition.Y) >= CameraPosition.Y) And ((VerticePosition.Y) <= (CameraPosition.Y + CameraSize.Height))) _
And (((VerticePosition.X) >= CameraPosition.X) And ((VerticePosition.X) <= (CameraPosition.X + CameraSize.Width)))) Then
IsOnCamera = True
Else
IsOnCamera = False
End If
End Function
i can get wrong results because of that...
can you advice me more?
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|