c 3d graphics visual studio drawing a cube

  1. #1

    joaquim is offline

    Thread Starter

    PowerPoster joaquim's Avatar


    3D Math: how draw a 3D cube?

    ok.. i'one thousand trying learn much more nearly 3D... but i don't detect a nice tutorial about information technology
    for beginning i demand understand how tin can i draw: 10,Y,Z to X' and Y'... yes the screen don't have the Z coordinate.
    can anyone explain to me or just requite me a tutorial for i acquire it?
    Last edited past joaquim; Mar 30th, 2022 at 09:36 AM.

  2. #two

    Re: 3D Math: how a 3D cube?

    If y'all do a search like "3d coordinate to screen", are you saying the first few hits are not "prissy" enough tutorials or explanations for what y'all need?

    I would think that if one of those is non sufficient, I'm not certain what you might be looking for, and I presume I certainly wouldn't provide whatsoever meliorate explanation.

    "Anyone can practise any amount of work, provided information technology isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930


  3. #three

    joaquim is offline

    Thread Starter

    PowerPoster joaquim's Avatar


    Re: 3D Math: how a 3D cube?

    cheers i wasn't using the correct words.
    earlier i showtime more: let me ask you... the screen uses X and Y.... and then how can i convert 3D(X,Y,Z) to 2nd(X,Y)?
    for i start something

  4. #4

    joaquim is offline

    Thread Starter

    PowerPoster joaquim's Avatar


    Re: 3D Math: how a 3D cube?

    or is an 'illusion' and nosotros just depict the objects by order(ane after another)?

  5. #5

    Re: 3D Math: how a 3D cube?

    Well, that is what the first link that was presented when I did the search was explaining.
    Of course in that location are a lot of illustrations and math discussion involved in the bailiwick, and so information technology is easy to be overwhelmed. I don't recollect I can brand information technology whatever easier.

    Many years ago, I was a bit interested in investigating some unproblematic 3D projections myself, so the examples I was looking at at the time were written in apparently quondam GWBasic or QBasic, and would draw simple wireframe versions of a collection of points, and you lot could spin those points effectually the 3 centrality.

    I later reused those aforementioned calculations in a VB3 program to draw a Rubic's Cube type cube, and was experimenting with rotating some of the layers to possible allow a full rotational model of all the layers allowing replication of rotating the cube equally a whole and the various layers to emulate the manipulation of the cube as you would a Rubic's cube, but I lost interest in that before getting very far.
    I added other things to the plan to test things like rotating a sphere of dots in addition to the points that made up the junctions of the cube pieces. Also, irresolute the colour of the dot with the resulting Z value so that the dot faded as it was further from the eyepoint.

    I added various checkboxes to enable or disable various portions of the drawing process, so yous could meet various things without the others, e.thousand. the cube by itself without the cloud of dots around it, or the cloud of dots without the cube, or drawing a circumvolve inside the cloud of dots, and so that it looked like dots were passing in front and behind the circle that was in the middle of the sphere.

    Information technology was simply a hodgepodge of various experimental pieces of lawmaking, so there is a lot of commented out code in information technology, as well every bit unlabeled buttons and other GUI elements.
    The functions that rotate the points to calculate the new X,Y and Z points from the original 10,Y and Z points after rotation, are essentially unrolled versions of what happens when you run the points through a matrix transformation. The preferred method would be to utilize a Matrix considering a number of rotations or other transforms could be applied to the matrix, and then the final matrix would be used to transform the points once, moving them from their original definition, to their new position in one shot since the matrix would be a combination of all the transforms needed to get from original position to electric current position.

    Once yous accept the points all transformed, then the rendering needs to catechumen the resulting 3D points into 2nd points for display, and that is projection office of the process. And there is more than than merely determining where the point on the screen is. There is the thing of determining which points should be fatigued and which should not, and in the case of surfaces, which should be fatigued and which shouldn't. And with a better drawing algorithm, depending on how the surfaces may overlap because of the Z depth of the surface irresolute beyond the polygon existence filled, the existing polygon should be broken into smaller polygons at the indicate of overlap.

    The code I used in my tests were quite simplistic compared to what should really be washed to draw things correctly.
    I ported the VB3 code to VB6 long ago, and I accept a several versions of information technology hanging around on various computers.
    I approximate I can mail service one of the VB6 versions, equally is, and y'all tin play effectually with information technology. I can't really afford the fourth dimension to get back into what it does and how it does it, but maybe you tin pull something from it.

    "Anyone can exercise whatever amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930


  6. #half dozen

    joaquim is offline

    Thread Starter

    PowerPoster joaquim's Avatar


    Re: 3D Math: how a 3D cube?

    please run across these 2 lines that draws 2 aforementioned images(front and left):

    Lawmaking:

    readimagefile("C:\\New\\bkgBox.bmp",PosX,PosY,PosX+bmp.Width,PosY+bmp.Height);         readimagefile("C:\\New\\bkgBox.bmp",PosX-Z,PosY,PosX,PosY+bmp.Height);
    draw result: https://imgur.com/2ipUJRY
    the front paradigm don't employ the Z, but the left employ it.... is these the right way?
    Last edited by joaquim; Mar 29th, 2022 at 11:51 AM. Reason: right some text

  7. #7

    Re: 3D Math: how a 3D cube?

    Non really.
    To depict the left side properly, ii things would need to happen.
    The first is Y scale needs to have fore-shortening, which means you have less Y scale the closer the object gets to yous.
    So, the Y scale should increase as the left side goes further abroad from you, which ways the bitmap should compress as information technology goes away from yous.

    If you're looking at the box with the front face up existence perpendicular to your view, and yous're viewing the box with the centre level centered on the front face, then the left side should end upward being a trapazoid shape.
    Since you don't have fore-shortening, and then the left side looks like information technology is increasing in size, if your mind can accept that the left side of the image is further dorsum, then the fact that information technology didn't shrink in size on the screen is interpreted as increasing in size in 3D.

    The second indicate, is that while you squeeze the texture into a narrower infinite, this is done linearly across the narrow space, where in "reality", the squeezing should be less closer to the heart and more equally the texture gets further from the eye.

    To do this properly, you lot either should use a 3D library that can utilize a texture to a surfaces with perspective, or if you want to practise it yourself, you will have to practice a lot more than work.

    The regular drawing capabilities of VB6 (GDI) and .Cyberspace (GDI+) don't back up doing 3D texture mapping with perspective. .Net does support some perspective drawing in a limited fashion, only not with textures.

    I did do some other experiments with mapping a texture to a trapazoid in the limited instance where the eyepoint was always perpendicular to the vertical, i.e. think of running around in a Maze game, where you don't look up or downwards, and yous are always moving effectually the maze with the walls perpendicular to your view.

    What you have to do for the side walls in that situation is decide for each vertical line of pixels in the side wall, which vertical line of pixels from the texture maps to it, and then squeeze that line of pixels and draw it vertically where it should be fatigued.

    So instead of one transfer of the texture to a shape, yous take to practise a serial of transfers of vertical slices of the texture to match the perspective at that pixel location. It is a lot of work if you want to do the primitives involved yourself.

    And then, the question is what is your desired purpose for the post? Are y'all interested in the math involved, which is explained in the various posts you tin can observe by searching. Are you trying to manually depict some 3D objects using the available cartoon primitives inside VB6 or .Cyberspace?

    Y'all can do an isometric (not perspective) type of 3D using plgblt blazon operations in either VB6 or VB.Net, merely isometric does suffer from non fore-shortening with depth, and then generally simply looks relatively practiced when looking down at maybe a xxx degree angle, which a lot of simple games depend on. This is sometimes referred to as 2.5D as it is a look that is between 2nd and true 3D.

    There is no simple respond or trick if you want to practise this yourself. At that place are probably a number of different libraries to make this blazon of affair easier, simply fifty-fifty then it is nonetheless a off-white amount of work to go competent with those tools.

    "Anyone tin can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930


  8. #8

    joaquim is offline

    Thread Starter

    PowerPoster joaquim's Avatar


    Re: 3D Math: how a 3D cube?

    i want draw the image\cube using Geometric Math for learn it. i know that theres several libraries for it... but i want to learn describe the cube, camera and the move.
    the fog and the temper\furnishings isn't the goal at present

  9. #9

    Re: 3D Math: how a 3D cube?

    Take you looked at this post.
    I haven't looked at information technology in detail, and information technology won't aid with texturing the cube because that is a whole other level of complication, but it will probably requite you lot another expect at the math involved in transforming points from 3D space to a projection on a 2D airplane.

    "Anyone can do any amount of work, provided it isn't the piece of work he is supposed to exist doing at that moment" Robert Benchley, 1930


  10. #10

    joaquim is offline

    Thread Starter

    PowerPoster joaquim's Avatar


    Re: 3D Math: how a 3D cube?

    that is for rotate the cube... but lets go back a picayune...
    i'yard trying work with scale... from i have seen i must multiply the positions and sizes by scale:

    Code:

    readimagefile("C:\\New\\bkgBox.bmp",PosX*Scale,PosY*Scale,(PosX+bmp.Width)*Calibration,(PosY+bmp.Height)*Scale);         readimagefile("C:\\New\\bkgBox.bmp",(PosX-Z)*Scale,PosY*Calibration,PosX*Calibration,(PosY+bmp.Height)*Calibration);
    but i'm doing something wrong, considering sometimes the image isn't showed.
    can you tell me what i'chiliad doing wrong?
    Last edited past joaquim; Mar 30th, 2022 at 09:53 AM.

  11. #11

    Re: 3D Math: how a 3D cube?

    I certainly tin can't tell what you're doing wrong.
    If it works sometime, and not other times, so the basic part call is likely syntactically right and the problem is with the parameters passed to the function.
    If the only affair you are changing is scale, then the value for Scale must be incorrect in some situations.
    That is something you will accept to exam.

    I am not going to attempt to create a test project to test anything, that is for sure.
    I don't have your bitmap, and from the little bit of code posted, I can't tell if the language being used is C#, C++ or something else.
    I can guess that the readimagefile function may exist from the sometime Borland port of the BGI library to Windows, simply I don't even know that for sure.

    If I wanted to test this, then I probably would utilise four variables to concord the X, Y , Width and Height values and set them before the function telephone call.
    That style you can examine so or print out their values every bit you change scale, to see if the values make sense for passing to the function.
    At the very to the lowest degree, you lot can run across at what limits of scale the role manages to describe something. I'm pretty sure if the width or summit cease upward being less than a pixel in size, probably naught ends up getting fatigued.

    You could easily verify that the role is working on some bones standard cases, e.g. similar:
    a Scale of 1.0 should create an prototype the size of the original
    2.0 should be twice the size
    0.5 should be half the size
    0.25 should be 1/4 the size.

    If you declared Size as an Integer, then that obviously would be a problem.

    I don't really see how this will help with cartoon a cube. The function readimagefile can resize the paradigm, just it will always be a resized rectangle, and to depict a cube, most of the sides would not be rectangles on the screen.

    "Anyone can do whatever amount of piece of work, provided information technology isn't the piece of work he is supposed to be doing at that moment" Robert Benchley, 1930


  12. #12

    joaquim is offline

    Thread Starter

    PowerPoster joaquim's Avatar


    Re: 3D Math: how draw a 3D cube?

    thanks for that.
    aye when the scale is 1, it will requite the original size

    Code:

    double Scale=one.0; else if( GetKeyState('Southward') <0)         {              if(Calibration<=0.one)                 Calibration =two.0;             else                 Calibration-=0.1;         }         else if( GetKeyState('Due west')<0)         {             Scale+=0.1;             if(Calibration>=2.0) Scale =1.0;         }
    until here fine.
    but i need another correction: the calibration touch on the position too?

  13. #13

    joaquim is offline

    Thread Starter

    PowerPoster joaquim's Avatar


    Re: 3D Math: how draw a 3D cube?

    "I don't actually see how this will assistance with drawing a cube. The function readimagefile tin resize the image, merely it will always exist a resized rectangle, and to describe a cube, about of the sides would not be rectangles on the screen."
    that y'all have right!!!!
    on win32 theres 1 part that tin draw an image on that way?

  14. #xiv

    Re: 3D Math: how depict a 3D cube?

    First, y'all are better of using something like DirectX or some other game engine rather than reinventing the cycle, because they do other tasks that you might find that you go to exercise latter. With that in listen, here is a quick simple formula I used in the past:

    x2D = (x * L) / (z + L)
    y2D = (y * L) / (z + Fifty)

    L is a constant representing the distance from the user to the screen in pixels, typically ready to 1024 or 2048, which makes the in a higher place formula very fast as VB6 uses shifts when you apply a number that is multiple of the power of two.

    Hither is a function that return x, y when supplied with xyz. I have used Long data blazon, simply if you lot accept problem, use Double:

    VB Code:

                                    
    1. Public Blazon My2DPoint

    2.     x Equally Long

    3.     y As Long

    4. Terminate Type

    5. Public Role To2D(ByVal x As Long, ByVal y Equally Long, ByVal z As Long) As My2DPoint

    6.     ' 50 = Distance from screen to the user in pixels.

    7.     '       Information technology's not necessary to plough this into a variable, simply use a typical value.

    8.     Const 50 Every bit Long = 2048 ' User is at 2048 pixels abroad from monitor

    9.     Dim p2d As My2DPoint

    10.     Dim temp As Long

    11.     temp = z + L

    12.     p2d.x = x * Fifty / temp

    13.     p2d.y = y * L / temp

    14.     To2D = p2d

    15. Stop Office


  15. #15

    joaquim is offline

    Thread Starter

    PowerPoster joaquim's Avatar


    Re: 3D Math: how draw a 3D cube?

    thank you so much for the formula.. cheers...
    at present i can work more than piece of cake...
    thinking on 2D to 3D or Z is more like:
    Z=(x*L-Lx2D)/x2D
    right?

    and yes the passel has correct some objects, fifty-fifty the cube, isn't all faces rectangles but polygons.
    tin can i apply brushes on polygons?


  16. #16

    Re: 3D Math: how depict a 3D cube?

    Quote Originally Posted past joaquim View Post

    give thanks you so much for the formula.. cheers...
    now i tin can work more easy...
    thinking on 2nd to 3D or Z is more like:
    Z=(x*L-Lx2D)/x2D
    right?

    Correct, but you are missing "*":

    z = (x*L-L*x2D) / x2D

    Or:

    z = Fifty * (x-x2D) / x2D

    Quote Originally Posted by joaquim View Post

    can i use brushes on polygons?

    Y'all have to do that on your own, and I am certain that you will have many other questions in the future every bit you movement forward. A game engine saves y'all development time. These simple solutions are for express apply case scenarios.

  17. #17

    Re: 3D Math: how depict a 3D cube?

    Quote Originally Posted by joaquim View Post

    ...
    only i need another correction: the calibration affect the position too?

    If y'all don't desire to scale the whole view, merely the object, then don't multiply the Ten,Y position by the Scale, but the size, i.due east. width and summit.

    Quote Originally Posted by joaquim View Post

    "I don't actually see how this volition aid with drawing a cube. The function readimagefile can resize the image, only it will ever be a resized rectangle, and to draw a cube, most of the sides would not be rectangles on the screen."
    ...
    on win32 theres 1 office that tin can draw an image on that way?

    Quote Originally Posted by joaquim View Post

    ..., even the cube, isn't all faces rectangles but polygons.
    can i employ brushes on polygons?

    No. There is zilch in the windows (GDI or GDI+) APIs that would draw a perspective image using a texture castor, or to fill a polygon with such a brush that would look right.
    You lot didn't mention what language you're using, or what libraries you're using to draw with.

    On the surface, it looks similar you may be using C++ and the "Ported to Windows" version of the Borland Graphics Interface. I don't call up that WinBGIm even supports texture brushes, or brushes in general, if that is what y'all are using. WinBGIm is pretty primitive stuff.

    If you were using GDI+, then you could have a texture brush, and you could transpose the brush to rotate, skew, and translate the brush when information technology is used to fill a polygon, but that would never await right with polygons that correspond a 3D polygon projected to a 2nd surface. It would always exist a parallelogram shaped pattern filling an arbitrary shaped figure.

    The best you could practice with a texturebrush is, as I mentioned before, doing an Isometric blazon cartoon, where the shape to exist filled would always exist a parallelogram so a parallelogram shaped texture would fit the shape more realistically. The texture would notwithstanding non be variably distributed along the perceived Z-Centrality equally would be needed for "truthful" 3D texture mapping. It would exist linearly distributed across the perceived Z-Axis. This would be along the same lines as what could be done using GDI with the parallel blit (plgblt) part.

    For an example of what this isometric 3D texture looks like, I played around with plgblt in a VB6 project at to the lowest degree 12 years ago (constitute a zippo file with the project in information technology from 2008).
    Again, it is only code used to experiment with plgblt and textures, and then is a mishmash of code and commented out code.

    I went alee and fabricated a couple of changes to it today, so that it starts up with the plgblt checkbox enabled, (used flat color fills if that option is not selected), and information technology starts with the view already rotated and tilted and then that yous see three sides of the visible blocks when you lot start up the programme.
    I too added a Sleep API declaration and practice a "Sleep 1" in the loop and so the program isn't using 100% of the core it runs on. On my machine, with the Sleep 1 in place, the executable uses 0% CPU when you aren't moving, and generally less than 8%, when the scene is in motility.

    The Orange Circle, which would correspond the object moving through the space, really never moves in the window. Everything else in the surround moves to show relative motion.

    There are diverse controls that were part of the experiment, merely to kickoff with, you actually only need to know three things to encounter the basic drawing in activity.
    one. Use the Upwardly Pointer to move "forward" and the Downwards Arrow to move "backward" and the Left and Right arrows to rotate the heading.
    2. Apply the "a" and "z" keys to tilt the view up and down. The tilt volition rotate continuously through a full circle in either direction. I don't know if it would somewhen crash if y'all rotated in one management also long.
    3. If you motility abroad from the drawing environment area (i.eastward. you get lost), striking the push to reset the view back to the beginning view.

    Last edited by passel; Mar 31st, 2022 at 11:12 PM.

    "Anyone can do any corporeality of work, provided information technology isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930


  18. #xviii

    joaquim is offline

    Thread Starter

    PowerPoster joaquim's Avatar


    Re: 3D Math: how draw a 3D cube?

    ok.. now i know convert the 3D to 2nd and using the camera\me distance(Z).
    simply thinking on my position... how can i compare with my position(left-right and up-downward)?

  19. #19

    joaquim is offline

    Thread Starter

    PowerPoster joaquim's Avatar


    Re: 3D Math: how draw a 3D cube?

    heres my actual code:

    Code:

    #include<windows.h> #include<iostream> #include <fstream> #include <cmath>  using namespace std;  grade Console { public:     HWND myconsole;     HDC mydc;     Panel()     {         myconsole = GetConsoleWindow();         //Get a handle to device context         mydc = GetDC(myconsole);     }      ~Console()     {         ReleaseDC(myconsole, mydc);     }      operator HDC()     {         return mydc;     }      operator HWND()     {         return myconsole;     }   }Console;   #define PI 3.14 int primary() {     LONG Ten=200, Y=200, Z =ten, Width=10, Height = 10, ZWidth=50, MeDistance=50;       RECT RectWindow;     GetWindowRect(Panel,&RectWindow);     RectWindow.left=0;     RectWindow.height=0;      do     {          FillRect(Console,&RectWindow,CreateSolidBrush(RGB(0,0,0)));         HPEN hWhitePen = (HPEN)GetStockObject(WHITE_PEN);         HPEN hOldPen = (HPEN)SelectObject(Console, hWhitePen);         Bespeak points[5] =         {             { (X * MeDistance) / (Z + MeDistance), (Y * MeDistance) / (Z + MeDistance) },             { Width+Ten, (Y * MeDistance) / (Z + MeDistance) },             { Width+Ten, Superlative+Y },             { (X * MeDistance) / (Z + MeDistance), Height+Y },             { (X * MeDistance) / (Z + MeDistance), (Y * MeDistance) / (Z + MeDistance) },         }; //describe the rectangle if is on my front end:         if(MeDistance-Z>=0)         {             HBRUSH hBrush1 = CreateSolidBrush(RGB(121, 90, 0));             HBRUSH holdBrush = (HBRUSH)SelectObject(Panel, hBrush1);             Polygon(Console, points, 5);             SelectObject(Panel, hOldPen);             SelectObject(Console, holdBrush );             DeleteObject(hBrush1);             DeleteObject(hWhitePen);         } //for i motility to front and back:         if(GetKeyState(VK_UP)& 0x8000)         {             MeDistance-=1;         }         else if(GetKeyState(VK_DOWN)& 0x8000)         {             MeDistance+=1;         }         else if(GetKeyState(VK_ESCAPE)& 0x8000)         {             break;         }         Sleep(l);     }while(1);     return 0; }
    when i movement for left or right, the scale and rotation changes likewise?

  20. #xx

    Re: 3D Math: how draw a 3D cube?

    Quote Originally Posted by joaquim View Post

    ok.. now i know convert the 3D to 2nd and using the camera\me distance(Z).
    only thinking on my position... how can i compare with my position(left-right and up-downwardly)?

    Who are yous addressing?

    The formula I posted is not accurate, but information technology's good enough for games. The limitation is that objects don't become smaller as they go right or left, but get smaller and larger every bit they go astern or forward. For example, if y'all are looking at a wall 100 Meters away, and a person walks correct or left along the wall, he will not go smaller.

    Here is an accurate formula, but it'due south slower(VB6 Lawmaking, Sqr=Square root):

    x2D = x * L / Sqr((z + L) ^ ii + y ^ 2)
    y2D = y * 50 / Sqr((z + L) ^ 2 + ten ^ 2)


  21. #21

    joaquim is offline

    Thread Starter

    PowerPoster joaquim's Avatar


    Re: 3D Math: how draw a 3D cube?

    Quote Originally Posted by qvb6 View Post

    Who are you addressing?

    The formula I posted is not authentic, but information technology's proficient enough for games. The limitation is that objects don't become smaller as they go right or left, but become smaller and larger as they become backward or frontwards. For example, if you are looking at a wall 100 Meters away, and a person walks right or left along the wall, he volition not get smaller.

    Here is an authentic formula, but it'south slower(VB6 Lawmaking, Sqr=Square root):

    x2D = x * L / Sqr((z + Fifty) ^ 2 + y ^ 2)
    y2D = y * L / Sqr((z + Fifty) ^ 2 + 10 ^ 2)

    i need inquire more 2 things:
    - we use the angle directly on 'x' or subsequently we get the 'x2D'?
    - these formula is used, too, for make the flooring?
    yes i'g trying creating, now, the floor without success

    Code:

    //Position and Size Floor:         int FloorX=0, FloorY=yard, FloorZ=one,FloorWidth=m, FloorHeight=10, FloorZWidth=100;          //floor vectors:         POINT FloorPoints[5] =         {             { FloorWidth+FloorX, (FloorY) / (Z) },             { FloorWidth+FloorX, FloorHeight-FloorY },             { (FloorX *MeDistance) / (Z +MeDistance), FloorHeight-FloorY },             { (FloorX * MeDistance) / (Z + MeDistance), (FloorY * MeDistance) / (Z + MeDistance) }         };
    i'1000 totally new and i can't become a nice tutorial for learn much more almost information technology
    (these lawmaking isn't updated with sqr(), just the C\C++ have it)
    cheers for all

  22. #22

    Re: 3D Math: how draw a 3D cube?

    Quote Originally Posted by joaquim View Post

    i need ask more two things:
    - we use the angle straight on 'ten' or subsequently we get the 'x2D'?
    - these formula is used, as well, for brand the floor?

    Aye, you need to practise rotation/moving on x, y, z earlier calculating x2D, y2D. This as well apply to the flooring, only there perhaps some situations where yous don't have to do that, I am not sure. I haven't fabricated a game before, just I plan to make one using Unreal game engine(free, but they take five% after you make a lot of money). You can use C++, or a visual programming language called Blueprint. At that place are many YouTube tutorials on how to use it.

  23. #23

    joaquim is offline

    Thread Starter

    PowerPoster joaquim's Avatar


    Re: 3D Math: how describe a 3D cube?

    to be honest i didn't plant tutorials near these.
    thank you to you lot, i can kickoff with something, because you give me the key formula.
    and maybe tomorrow i can draw the flooring
    i beloved when works and i hates when fails hehehehehe

  24. #24

    Re: 3D Math: how describe a 3D cube?


  25. #25

    joaquim is offline

    Thread Starter

    PowerPoster joaquim's Avatar


    Re: 3D Math: how draw a 3D cube?

    Quote Originally Posted past qvb6 View Post

    i'yard sorry i was mean the math for the 3D games

  26. #26

    Re: 3D Math: how describe a 3D cube?

    Quote Originally Posted by joaquim View Post

    i'm distressing i was mean the math for the 3D games

    Information technology's called 3D Projection:

    https://en.wikipedia.org/wiki/3D_projection

    The fast formula that I posted, is something I developed myself, merely others may have come upward with the same formula. I am not sure near the accurate formula. It might be me or someone else. These are meant for apps that want to display 3D shapes, like engineering science apps.

    For games, and fifty-fifty applied science apps; a game engine is a better pick. Game engines practice textures, object collision, physics, lighting, shadows, sound, and more. Are y'all planning to spend months(more like a yr or two) in duplicating these futures? If you don't similar the principal two(Unreal/Unity), hither is a list:

    https://en.wikipedia.org/wiki/List_of_game_engines


  27. #27

    joaquim is offline

    Thread Starter

    PowerPoster joaquim's Avatar


    Re: 3D Math: how draw a 3D cube?

    right me 1 thing: the Polygon() function uses the POINT arrays for the vectors, simply uses LONG type instead bladder... that'due south why i get a warning.
    theres is another function for go several dots\vectors for create the lines using a float type?
    (is only for i don't get these warnings)

  28. #28

    joaquim is offline

    Thread Starter

    PowerPoster joaquim's Avatar


    Re: 3D Math: how draw a 3D cube?

    Code:

    POINT to2D         {             (LONG)Width * CameraDistance / sqrt(pow((ObjectZWidth + CameraDistance) , 2)) + pow(Height , 2), //all variables are 'float'             (LONG)Height * CameraDistance / sqrt(pow((ObjectZWidth + CameraDistance) , two)) + pow(Width , 2)         };
    warning: "narrowing conversion of '((((bladder)(LONG)((Point3D*)this)->Point3D::PositionX * ((Point3D*)this)->Point3D::CameraDistance) / std::sqrt(std:ow((((Point3D*)this)->Point3D::PositionZ + ((Point3D*)this)->Point3D::CameraDistance), 2.0e+0f))) + std:ow(((Point3D*)this)->Point3D::PositionY, two.0e+0f))' from 'float' to 'LONG' {aka 'long int'} inside { } [-Wnarrowing]"

    Code:

    grade Object3D: public SizePosition3D { public:     POINT Vectors[vi];     Object3D(Point3D Position3D=0, Size3D Size3D=0)     {         Position=Position3D;         Size=Size3D;         Vectors=         {             { (Position.To2D().x * Position.CameraDistance) / (Position.PositionZ + Position.CameraDistance), (Position.To2D().y * Position.CameraDistance) / (Position.PositionZ + Position.CameraDistance) },             { Size.To2D().ten+Position.To2D().x, (Position.To2D().y * Position.CameraDistance) / (Position.PositionZ + Position.CameraDistance) },             { Size.To2D().x+Position.To2D().x, Size.To2D().y+Position.To2D().y },             { (Position.To2D().x * Position.CameraDistance) / (Position.PositionZ + Position.CameraDistance), Size.To2D().y+Position.To2D().y },             { (Position.To2D().ten * Position.CameraDistance) / (Position.PositionZ + Position.CameraDistance), (Position.To2D().y * Position.CameraDistance) / (Position.PositionZ + Position.CameraDistance) }         };     }  };
    "error: assigning to an assortment from an initializer listing|"
    now i get an error confused

  29. #29

    Re: 3D Math: how depict a 3D cube?

    Quote Originally Posted by joaquim View Post

    Code:

    POINT to2D         {             (LONG)Width * CameraDistance / sqrt(pow((ObjectZWidth + CameraDistance) , 2)) + prisoner of war(Height , 2), //all variables are 'float'             (LONG)Height * CameraDistance / sqrt(pow((ObjectZWidth + CameraDistance) , two)) + prisoner of war(Width , 2)         };
    You lot tin can't use type casting to convert data. That only tells the compiler to treat the binary data every bit LONG. Use "double" information blazon instead of "long", and remove "(LONG)".
    Final edited by qvb6; Apr 5th, 2022 at 03:06 PM.

  30. #xxx

    joaquim is offline

    Thread Starter

    PowerPoster joaquim's Avatar


    Re: 3D Math: how draw a 3D cube?

    Quote Originally Posted by qvb6 View Post

    You can't use type casting to convert information. That just tells the compiler to care for the binary information as LONG. Use "double" data type instead of "long", and remove "(LONG)".

    possibly i didn't sympathize what you said.
    i changed from 'float' to 'double' and i delete the "(LONG)"... but the alarm remains

  31. #31

    joaquim is offline

    Thread Starter

    PowerPoster joaquim's Avatar


    Re: 3D Math: how describe a 3D cube?

    the casting take, too, an operator precedence and i never knew.
    i must examination more one thing abut drawing and the distance... then i will be back
    thanks for all

  32. #32

    joaquim is offline

    Thread Starter

    PowerPoster joaquim's Avatar


    Re: 3D Math: how depict a 3D cube?

    theres one thing that i'm notice with these formula:

    Code:

    x2D = (x * 50) / (z + L) y2D = (y * L) / (z + L)
    if the 'L' is more big, the object\rectangle is more big too..
    to exist honest: the more big the '50', the rectangle is more little?

  33. #33

    Re: 3D Math: how depict a 3D cube?

    Quote Originally Posted by joaquim View Post

    ...if the 'L' is more than big, the object\rectangle is more big too..
    to exist honest: the more large the 'L', the rectangle is more than trivial?

    L is the distance from you to the screen.
    Imagine if you had an empty picture frame that represented the screen, and you sabbatum at one finish of the table with the frame 400cm in forepart of you, and I was at the other end of the table and yous were looking at me through the frame.
    Picture how much of me yous might see in the frame.
    At present movement the frame further from yous, and thus closer to me.
    Would I wait smaller in the frame at present, or larger?

    "Anyone tin can do whatsoever amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930


  34. #34

    joaquim is offline

    Thread Starter

    PowerPoster joaquim's Avatar


    Re: 3D Math: how draw a 3D cube?

    i'k sorry, merely i go on confused: if the 'L' is less than 50, the object will be more far\small... but if is more than 50, the object will be more close\large.

  35. #35

    joaquim is offline

    Thread Starter

    PowerPoster joaquim's Avatar


    Re: 3D Math: how draw a 3D cube?

    when nosotros have FloorZ and the FloorZWidth, the formula but uses FloorZ or must use FloorZ+ FloorZWidth on size vector?

  36. #36

    Re: 3D Math: how depict a 3D cube?

    Quote Originally Posted past joaquim View Post

    i'm deplorable, but i continue confused: if the 'L' is less than 50, the object will be more far\pocket-size... but if is more than than 50, the object will be more close\big.

    Yes, that is true. If you understand that you are not confused.
    If yous don't understand that, then I'chiliad not certain I tin make it clearer.

    L is the distance from your eyepoint to the screen. The screen is a window, i.east. a viewport into your 3D world. The closer your eye is to the window, the more of the globe you encounter, if you meet more than of the world in your window, the world objects accept to appear smaller compared to the window.

    If your center is at the window, you tin can see 180 degrees of your world, and so all the objects you run into have to fit in that window, then they have to be small compared to the window.

    If your window is sixty cm wide, and your eye is 60 cm from the window, you can only come across 90 degrees of your world, so you come across much less of your world. Since yous see less things in your window, those things yous come across appear larger compared to the frame of the window.

    One time you choose a value for Fifty, y'all should not be changing information technology. It decides what your field of view is, i.e. how many degrees vertically and horizontally you can see. As y'all move around, your field of view doesn't change, simply the angle of direction that you are looking.
    You would but change Fifty if for instance, you looked through Binoculars, or some other device, that changed your field of view.

    "Anyone can do any amount of piece of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930


  37. #37

    joaquim is offline

    Thread Starter

    PowerPoster joaquim's Avatar


    Re: 3D Math: how depict a 3D cube?

    passel, to be honest, i was using 'L' like the scale... thanks for correct me.
    then now i understand what i tin can do for scale...
    i need ask more 1 thing that is kill me now.
    see these sample code:

    Code:

    //Position and Size Flooring:         int FloorX=0, FloorY=thou, FloorZ=100,FloorWidth=grand, FloorHeight=10, FloorZWidth=100;          //flooring vectors:         Indicate FloorPoints[5] =         {             { (FloorWidth+FloorX)*MeDistance/(FloorZWidth+MeDistance), (FloorY*MeDistance) / (FloorZ*MeDistance) },             { (FloorWidth+FloorX)*MeDistance/( FloorZWidth+MeDistance), (FloorHeight+FloorY)*MeDistance/( FloorZWidth+MeDistance) },             { (FloorX *MeDistance) / (FloorZ +MeDistance), (FloorHeight+FloorY)*MeDistance/(FloorZ+ FloorZWidth+MeDistance) },             { (FloorX * MeDistance) / (FloorZ + MeDistance), (FloorY * MeDistance) / (FloorZ + MeDistance) },             { (FloorWidth+FloorX)*MeDistance/(FloorZWidth+MeDistance), (FloorY*MeDistance) / (FloorZ*MeDistance) }         };
    is drawed, but not correctly.. i'thousand trying creating just a rectangle like a floor... can you lot explain to me what i'1000 doing wrong?

  38. #38

    Re: 3D Math: how draw a 3D cube?

    What is FloorZWidth ?

    It is a niggling hard to follow what you're doing. Y'all should probably create a function like qvb6 gave you lot and laissez passer the 3D value in and get the 2d Value dorsum.
    If you store those in variables or arrays of structure, then it would be easier to meet the values going in and the resulting X,Y values coming dorsum and then you lot can analyze the values.

    The X,Y,Z values being passed in have to exist relative to your centre point.
    Then, the first pace is to transform your world coordinates into local relative points to where you are looking from in the world.
    Once you lot've translated the world coordinates into eye bespeak relative coordinates, then those coordinates are what you catechumen from 3d to 2d for projection.

    It looks like y'all are using values that are off to one side from the eye point, e.chiliad. ranging from 0 to 1000, rather than -500 to 500.

    Then, if you are not going to utilize graphics commands to starting time the cartoon on the screen, y'all would desire to add together the center get-go when you describe.
    You're not showing what you exercise with the points after you lot create them, and so I don't know if you're taking care of that or not.

    For an case, I don't have VB6 on this machine, so cruel back to using VB.Net in VS2010 which is on this machine.
    I just took qvb6 function and changed the Longs to Integers since they are the 32-bit blazon in .Net, and inverse the Type annunciation to a Structure declaration.

    So, treat the your eyepoint as always existence (0,0,0) and the points of what you're drawing being relative to that.
    I originally just did the four points with Y as a positive value which I figured would put the rectangle below the centre point (i.e. the floor).
    That looked OK, so I copied the iv presets and just changed the Y value to a negative number, so they should be higher up the eye point (i.e. the ceiling). And that also drew the four points the style I would accept expected them to.

    And so, the basic function works to the degree that it does, and gives results that yous would look.
    Your primary trouble is most likely that your 3d points are non relative to your eye point, merely there may be other issues every bit your example reference variables that are not defined in the snippet you posted.

    Here is the exam lawmaking I tested with (VB.Internet)

    Lawmaking:

    Public Grade Form1    Private Structure My2DPoint     Public x Equally Integer     Public y Equally Integer   End Structure    Private Structure my3DPoint     Public 10 As Integer     Public y As Integer     Public z Every bit Integer   Cease Structure    Dim my2dArray(7) Every bit My2DPoint   Dim my3dArray(7) As my3DPoint    Private Sub Form1_Load(ByVal sender Every bit System.Object, ByVal due east As System.EventArgs) Handles MyBase.Load     With my3dArray(0)       .x = -200       .y = 100       .z = 0     End With     With my3dArray(1)       .x = -200       .y = 100       .z = 400     End With     With my3dArray(2)       .x = 200       .y = 100       .z = 400     End With     With my3dArray(3)       .x = 200       .y = 100       .z = 0     End With      With my3dArray(4)       .ten = -200       .y = -100       .z = 0     Stop With     With my3dArray(5)       .ten = -200       .y = -100       .z = 400     Finish With     With my3dArray(6)       .x = 200       .y = -100       .z = 400     End With     With my3dArray(7)       .10 = 200       .y = -100       .z = 0     Terminate With      For i As Integer = 0 To 7       With my3dArray(i)         my2dArray(i) = To2D(.x, .y, .z)       End With     Next    End Sub    Private Role To2D(ByVal x As Integer, ByVal y Every bit Integer, ByVal z As Integer) Every bit My2DPoint     ' L = Distance from screen to the user in pixels.     '       It's not necessary to turn this into a variable, just use a typical value.     Const L As Long = 1024 ' User is at 2048 pixels away from monitor      Dim p2d As My2DPoint     Dim temp As Long      temp = z + Fifty     p2d.10 = 10 * L / temp     p2d.y = y * L / temp      To2D = p2d   End Role    Individual Sub Form1_Paint(ByVal sender As Object, ByVal e Equally System.Windows.Forms.PaintEventArgs) Handles Me.Pigment     Dim k Every bit Graphics = due east.Graphics     Dim cx As Integer = 300, cy Equally Integer = 300      For i As Integer = 0 To vii       With my2dArray(i)         g.FillEllipse(Brushes.Black, cx + .x, cy + .y, 8, eight)       Finish With     Next   Finish Sub End Class

    "Anyone tin can do whatever corporeality of work, provided it isn't the piece of work he is supposed to be doing at that moment" Robert Benchley, 1930


  39. #39

    joaquim is offline

    Thread Starter

    PowerPoster joaquim's Avatar


    Re: 3D Math: how depict a 3D cube?

    "What is FloorZWidth ?"

    - on X, we take Width;
    - on Y, we take Acme;
    - and so on Z we have ZWidth(information technology'southward what i call, unless theres a technical name that i don't know it).
    for now i'thou trying depict a rectangle for the floor and then the sky.....

    Code:

    double const MeDistance=500.0f; //Position and Size Floor:         int FloorX=0, FloorY=0, FloorZ=-100,FloorWidth=500, FloorHeight=500, FloorZWidth=500;          //floor vectors:         Bespeak 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) }         };
    it's a normal bluish rectangle but nothing compare to floor on Z way
    result:
    https://imgur.com/zY7O0Z2

  40. #twoscore

    Re: 3D Math: how draw a 3D cube?

    Please run into the fastened VB6 project for a sample that draws a cube. The 3D Origin constants could have been improve, but I didn't have fourth dimension to make them based on PictureBox size.

    VB Code:

                                    
    1. Option Explicit

    2. Private Blazon T2DPoint

    3.     ten Every bit Long

    4.     y As Long

    5. End Type

    6. Private Type T3DPoint

    7.     x As Long

    8.     y As Long

    9.     z Equally Long

    10. End Type

    11. Private Origin2D Equally T2DPoint

    12. Private Origin3D Equally T3DPoint

    13. Const CubeSize As Long = 100 ' Pixels

    14. Individual Function To2D(ByVal ten Equally Long, ByVal y As Long, ByVal z As Long) As T2DPoint

    15.     ' L = Altitude from screen to the user in pixels.

    16.     '     Information technology'due south not necessary to turn this into a variable, just utilize a typical value.

    17.     Const L As Long = 2048 ' User is at 2048 pixels abroad from monitor

    18.     Dim p2d As T2DPoint

    19.     Dim temp As Long

    20.     If chkUseFastFormula.Value <> 0 Then

    21.         temp = z + L

    22.         p2d.x = 10 * L \ temp

    23.         p2d.y = y * Fifty \ temp

    24.     Else

    25.         p2d.x = x * L / Sqr((z + L) ^ 2 + y ^ 2)

    26.         p2d.y = y * 50 / Sqr((z + L) ^ 2 + ten ^ 2)

    27.     End If

    28.     To2D = p2d

    29. Finish Role

    30. Private Sub Form_Load()

    31.     Picture1.ScaleMode = vbPixels

    32.     Picture1.AutoRedraw = Truthful

    33.     ' 2D Origin

    34.     Origin2D.x = Picture1.ScaleWidth / 2

    35.     Origin2D.y = Picture1.ScaleHeight / 2

    36.     ' 3D Origin

    37.     Origin3D.ten = -300

    38.     Origin3D.y = -300 '-50

    39.     Origin3D.z = 1000

    40.     Timer1.Interval = 100

    41.     DrawCube

    42. Terminate Sub

    43. Private Sub Draw3DLine(ByVal x1 Every bit Long, ByVal y1 As Long, ByVal z1 As Long, ByVal x2 Equally Long, ByVal y2 Every bit Long, ByVal z2 As Long)

    44.     Dim pnt1 As T2DPoint

    45.     Dim pnt2 As T2DPoint

    46.     pnt1 = To2D(x1 + Origin3D.x, y1 + Origin3D.y, z1 + Origin3D.z)

    47.     pnt2 = To2D(x2 + Origin3D.x, y2 + Origin3D.y, z2 + Origin3D.z)

    48.     Picture1.Line (pnt1.x + Origin2D.x, pnt1.y + Origin2D.y)-(pnt2.ten + Origin2D.x, pnt2.y + Origin2D.y)

    49. End Sub

    50. Private Sub DrawCube()

    51.     ' Cube has 12 lines

    52.     ' Front end confront

    53.     Draw3DLine 0, 0, 0, CubeSize, 0, 0

    54.     Draw3DLine CubeSize, 0, 0, CubeSize, CubeSize, 0

    55.     Draw3DLine CubeSize, CubeSize, 0, 0, CubeSize, 0

    56.     Draw3DLine 0, CubeSize, 0, 0, 0, 0

    57.     ' Rear face

    58.     Draw3DLine 0, 0, CubeSize, CubeSize, 0, CubeSize

    59.     Draw3DLine CubeSize, 0, CubeSize, CubeSize, CubeSize, CubeSize

    60.     Draw3DLine CubeSize, CubeSize, CubeSize, 0, CubeSize, CubeSize

    61.     Draw3DLine 0, CubeSize, CubeSize, 0, 0, CubeSize

    62.     ' Front end to dorsum

    63.     Draw3DLine 0, 0, 0, 0, 0, CubeSize

    64.     Draw3DLine CubeSize, 0, 0, CubeSize, 0, CubeSize

    65.     Draw3DLine CubeSize, CubeSize, 0, CubeSize, CubeSize, CubeSize

    66.     Draw3DLine 0, CubeSize, 0, 0, CubeSize, CubeSize

    67. End Sub

    68. Individual Sub Timer1_Timer()

    69.     Picture1.Cls

    70.     Origin3D.x = Origin3D.x + 20: If Origin3D.x > 300 Then Origin3D.ten = -300

    71.     ' Comment the following line to make the cube motion horizenally

    72.     Origin3D.y = Origin3D.y + 20: If Origin3D.y > 300 Then Origin3D.y = -300

    73.     ' Uncomment the following line to make the cube move horizenally

    74.     'Origin3D.y = -50

    75.     Origin3D.z = 0

    76.     DrawCube

    77. End Sub


    DrawCubeProj.nix

Posting Permissions

  • You may not mail new threads
  • You may non mail service replies
  • You may not post attachments
  • You may not edit your posts
  • BB code is On
  • Smilies are On
  • [IMG] code is On
  • [VIDEO] code is On
  • HTML code is Off

Click Here to Expand Forum to Full Width

sherrodmintwoubity.blogspot.com

Source: https://www.vbforums.com/showthread.php?884923-3D-Math-how-draw-a-3D-cube

0 Response to "c 3d graphics visual studio drawing a cube"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel