简体   繁体   中英

C++ Advanced 3D coordinates to 2D Coordinates

My question: How to convert a 3D coordinate on a 2D screen? I red a lot about that but all my research just showed half answered or even unanswered replies, some were wrong (tested them) so I ask again and try to add as much detail as possible. Here are the structures we will work with:

struct COORD3D
{
    int X;
    int Y;
    int Z;
};
struct PAN3D//Rotate around these axis
{
    float X;
    float Y;
    float Z;
};
    struct COLOR
{
    Uint8 R;//RED
    Uint8 G;//GREEN
    Uint8 B;//BLUE
    Uint8 A;//ALPHA
};
struct CAMERA
{
    COORD3D Pos;
    PAN3D   Rot;
    float   angle;
    int RESX;//X Resolution
    int RESY;//Y Resolution
};
struct POI
{
    COORD3D Pos;
    COLOR   Col;
};
struct OBJECT
{
    COORD3D Pos;//absolute position
    list<POI> dots;//relative to object position
    list<pair<POI*, POI*>> lines;//Pointers to the dots to connect them
    PAN3D Rot;//absolute rotation
};

Now what I need is a function that looks like:

POI Convert3dTo2d(CAMERA cam, POI point);

(must be a "POI" because of the color ;) ) I already got an alogythm that goes through all objects and all of their points And the fact that there is a camera tells you that it's not an orthograhic voew but a perspective.

Please comment the code you write here ropperly so everyone can understand it. If you got no clue of how to do this or just got approaches or non direct solutions, please don't answer, that doesn't really help us. http://sta.sh/0de60ynp9id <- This image should describe it a bit

Os (Windows 7), Microsoft Visual Studio 2013(I'm just using the c++ part of it) I Build it for x64 (if it is important ;) ) But I don't think that is important for a little bit mathematic algorythms

If you got any questions, feel free to ask me

Okay, I think I got a new( to me new) way to do this, gonna try it tomorrow and will tell you if it work's (that's the part that everyone forgets but I try not to forget it)

You need to multiply your point by a view and projection matrix.

A view matrix translates the point into camera space. Aka, relative to CAMERA.

A projection matrix transforms the point from 3D space into a projection space. You'll need to decide what sort of projection you want. For example, orthographic projection or perspective projection.

See the matrix at the bottom of these pages for the layout of these matrices.

LookAtLH, or the view matrix: http://msdn.microsoft.com/en-us/library/windows/desktop/bb281710(v=vs.85).aspx

OrthoLH, or the projection matrix using orthographic projection: http://msdn.microsoft.com/en-us/library/windows/desktop/bb281723(v=vs.85).aspx

You'll also need to look into how to perform matrix multiplication.

The only way I can interpret this question is that you want to project a 3d point(s) onto a 2d plane. If that's not what you're looking for, clarify your question. If that is what you want, countless resources for 3d projection are around: http://en.wikipedia.org/wiki/3D_projection

You will need to multiply your points by a projection matrices to project(or "convert"?) your points on a 2d plane.

I suggest you can look at the following links for an explanation of transform 3D coordinates to 2D coordinates,

The OpenGL transform pipeline

OpenGL transform

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM