Projection problem

I’ve a problem with vertices projection. See this image:

There are three points in this order:
-Red point (2.0, 2.0, 2.0)
-Green point (-2.0, 2.0, 2.0)
-Blue point (2.0, -2.0, 2.0)
Ok, now I need the projected coordinate of this three vertices. I proceed in this manner:

V1 = (2.0, 2.0, 2.0);
PM; //Is the projection matrix
MM; //Is the modelview matrix
TM = {(0.5 * 1024.0), 0.0, 0.0, 0.0, 0.0, (0.5 * 800.0), 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, (0.5 * 1024.0), (0.5 * 800.0), 0.0, 1.0};

V1O = (2.0, 2.0, 2.0, 1.0);


V1 = V1O.perspectiveDivision();

In this manner I obtain a wrong result, because the resulting coordinate are these: (yellow numbers)

Why the order is wrong? And way the second coordinate is wrong?

How do you place these yellow numbers onto screen?
Because there is a major difference in Y coordinate appearance: in Windows GDI zero is on the top, and growing direction is down. On the other hand, in OpenGL zero is down corner, and direction is upward.
So, it’s not surprising, that you get image, flipped vertically, if you draw your numbers via GDI calls.

No, the yellow numbers are drawed manually (with paint). The problem is that the projected vertices aren’t correct.
The returned vertices:

747.298394 507.298394
532.701606 507.298394
747.298394 292.701606

If you check this coordinate on the first image, you can check that are wrongs.


as I understand your code, you do perspective division after viewport transformation (TM matrix in your notation).
This is not correct. Graphics pipelines usually do viewport transform at the very last stage, after perspective division.

Ok, but if I made viewport transformation after the perspective divisione, the result is the same and wrong.
For example:

v1 = (2.0, 2.0, 2.0);
v1O = (v1, 1.0); //(2.0, 2.0, 2.0, 1.0);

v1O = PM * MM * V1O;
v1 = v1O.perspectiveDivision();

v1.x = (v1.x * 640) + 640;
v1.y = (v1.y * 400) + 400;

The results is the same, but why the projected coordinate aren’t correct?

That’s strange.
The way you do is exactly the way OpenGL does.
May be something wrong with projection matrix??
Try moving your points away from screen, remaining X and Y the same. May be, your projection matrix and OpenGL projection matrix differ somehow?
Try getting all these matrices via glGetFloatv() just to ensure they are the same.

Why these coordinates are not correct? Taking the origin at the bottom left corner I have found that your manually-drawn-with-paint points are at the coordinates you have shown above, they are the good ones with a +1 or -1 inacurracy.
Moreover the painted points are not really points, zooming closer to them they looks more a disc.
One, question, How did succeed to place these points at right location in pixel coordinates without knowing them in the OpenGL viewport space?
About, the perspective division, Jackis is completely right, in your case you obtained the same results because I think that you an orthographic projection.

Why dont you use gluProject? If you need source, get mesa source and dig gluProject function source.