# Convert 3d point to 2d always flat to camera

I’m new to opengl and I’m trying to project a polygon into 2d, but I need that the polygon always stay flat to the camera to get
a orthographic projection (something like billboard)

What I did was to calculate the normal vector of the polygon and then set the view from the extreme of the
normal vector to the point of the polygon, so the view is totally flat.

But I cannot get the right 2d coords.

if I move/rotate the viewport, the 2d coord change, and I don’t want that.
For me the important are the relation between points of the polygon, because I need this data to triangulate, so the position relative to viewport is not important.

This is the code. The initial routine is: stroke_2d_flat

``````
/* get 2d location */
void get_2d_loc(GLdouble objx, GLdouble objy, GLdouble objz, int r_co)
{
GLdouble modelMatrix;
GLdouble projMatrix;
GLint viewport;

glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
glGetIntegerv(GL_VIEWPORT, viewport);

GLdouble win_x;
GLdouble win_y;
GLdouble win_z;

int result = gluProject(objx, objy, objz, modelMatrix, projMatrix, viewport,
&win_x, &win_y, &win_z);
r_co = (int)win_x;
// reverse y to adapt from OpenGL to windows
r_co = viewport + viewport - (int)win_y;

}
/* get points always flat to view */
void stroke_2d_flat(bGPDspoint *points, int totpoints, float(*points2d))
{
bGPDspoint *pt;
int i;

GLfloat modelMatrix;
GLfloat projMatrix;
GLint viewport;

glGetIntegerv(GL_VIEWPORT, viewport);
bGPDspoint dpt0 = points;
bGPDspoint dpt1 = points;
bGPDspoint dpt2 = points;

// Create vectors
GLdouble v1 = { dpt1.x - dpt0.x, dpt1.y - dpt0.y, dpt1.z - dpt0.z };
GLdouble v2 = { dpt2.x - dpt1.x, dpt2.y - dpt1.y, dpt2.z - dpt1.z };

// Cross product to get normal vector (no need normalize)
GLdouble cx = (v1 * v2) - (v1 * v2);
GLdouble cy = (v1 * v2) - (v1 * v2);
GLdouble cz = (v1 * v2) - (v1 * v2);

// Set camera from normal to vertice (point 2)
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
gluLookAt(cx + dpt1.x, cy + dpt1.y, cz + dpt1.z, dpt1.x, dpt1.y, dpt1.z, 0, 1, 0);

// Create orthographic projection matrix
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glOrtho(0, 0, viewport, viewport, 0.0f, 1.0f);

// Convert all points to 2d
printf("Points:
");
for (i = 0, pt = points; i < totpoints; i++, pt++)
{
int co;
get_2d_loc(pt->x, pt->y, pt->z, co);
printf("%d: 3d: %f, %f, %f 	 2d: %d,%d
", i, pt->x, pt->y, pt->z, co, co);
points2d[i] = co;
points2d[i] = co;
}

// Reset
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();

``````

[QUOTE=ax2638;1281044]I’m new to opengl and I’m trying to project a polygon into 2d, but I need that the polygon always stay flat to the camera to get
a orthographic projection (something like billboard)

if I move/rotate the viewport, the 2d coord change, and I don’t want that.
[/quote]

Ok, let’s get clear on your requirements first.

You want these polygons to be billboarded so that they always face the camera.

It sounds like you want these polygons to be rendered in orthographic.

It also sounds like you want these polygons to be rendered in screen-space (window-relative), not in world-space (world-relative) so that their position and size is fixed relative to the window.

If so, why not activate an orthographic projection with an identity (or constant) modelview transformation when rendering these polygons? You don’t have to render these polygons with the same transformations as you render scene objects unless you have some other requirements which desire (or require) it.

Really I don’t want render the polygon. I have the polygon on screen already, what I need is convert all 3d points to 2d, but I need to get “always” a flat polygon (the camera must look at the polygon surface orthogonal as a billboard). I need these 2d points to use in a routine and if I convert 3d to 2d points with the current rotation, the 2d points change depending of the rotation and I don’t want this.

To put the camera, I used a normal vector calculated using the 3 first points of the polygon and apply the cross product.

Maybe my solution is totally wrong and there is a simple method.

I got a solution:

``````
void stroke_to2d_flat(Mypoint *points, int totpoints, float(*points2d))
{
GLfloat locx;
GLfloat loc3;

GLint viewport;
glGetIntegerv(GL_VIEWPORT, viewport);

// local X axis (p0-p1)
locx = points.x - points.x;
locx = points.y - points.y;
locx = points.z - points.z;

// point vector at 3/4
int p3 = totpoints * 0.75;
loc3 = points[p3].x - points.x;
loc3 = points[p3].y - points.y;
loc3 = points[p3].z - points.z;

//  vector orthogonal to polygon plane
GLfloat normal;
cross_v3_v3v3(normal, locx, loc3);

// local Y axis (cross to normal/x axis)
GLfloat locy;
cross_v3_v3v3(locy, normal, locx);

// Normalize vectors
normalize_v3(locx);
normalize_v3(locy);

// Get all points in local space
for (int i = 0; i < totpoints; i++)
{
GLfloat loc;
// Get local space using first point as origin
loc = points[i].x - points.x;
loc = points[i].y - points.y;
loc = points[i].z - points.z;

GLfloat co;
co = dot_v3v3(loc, locx);
co = dot_v3v3(loc, locy);
points2d[i] = co;
points2d[i] = co;
}
}

``````