I’m trying to make an single image (quad) facing to the camera in all cases.
which means considering all of this parameters:
Camera position, up vector, forward vector and object position.
I ran over all the tutorials and examples on net. each example solve “kind of”, part of the problem. most of them don’t consider the target point of the camera, or the up vector, and others don’t consider the object position…
Could anyone write for me a CreateBillboard function or explain to me what i did wrong in the attached code…
When i finally thought that i’m getting into OGL programming this topic came and ruined the mood.
I lost hope in this topic. just need it to work.
thank you all in advanced.
I cut most of the code for simplicity.
its written in c# if it will be easier to solve i will post the full files (which is not allot also)
void CreateBillboardMatrix(float[] matrix, Vector3D right, Vector3D up, Vector3D look, Point3D pos)
{
matrix[0] = (float)right.X;
matrix[1] = (float)right.Y;
matrix[2] = (float)right.Z;
matrix[3] = 0;
matrix[4] = (float)up.X;
matrix[5] = (float)up.Y;
matrix[6] = (float)up.Z;
matrix[7] = 0;
matrix[8] = (float)look.X;
matrix[9] = (float)look.Y;
matrix[10] = (float)look.Z;
matrix[11] = 0;
// Add the translation in as well.
matrix[12] = (float)pos.X;
matrix[13] = (float)pos.Y;
matrix[14] = (float)pos.Z;
matrix[15] = 1;
}
void BillboardPoint(Point3D pos, Point3D camPos, Vector3D camUp)
{ // create the look vector: pos -> camPos
Vector3D look = camPos - pos;
look.Normalize();
// right hand rule cross products
Vector3D right = Vector3D.CrossProduct(camUp, look);
Vector3D up = Vector3D.CrossProduct(look, right);
float[] matrix = new float[16];
CreateBillboardMatrix(matrix, right, up, look, pos);
// apply the billboard
GL.glMultMatrixf(matrix);
}
.
.
.
GLU.gluLookAt(m_posCamObs.X, m_posCamObs.Y, m_posCamObs.Z,
m_posCamTarget.X, m_posCamTarget.Y, m_posCamTarget.Z,
m_vecCamUp.X, m_vecCamUp.Y, m_vecCamUp.Z);
BillboardPoint(m_posCamTarget, m_posCamObs, m_vecCamUp);
GL.glEnable(GL.GL_TEXTURE_2D);
GL.glBindTexture(GL.GL_TEXTURE_2D, g_texID);
GL.glBegin(GL.GL_POLYGON);
{
// Face to Y
GL.glTexCoord2f(0.0f, 0.0f);
GL.glVertex3d(m_posObject.X - dblHSize, m_posObject.Y + dblHSize, m_posObject.Z);
GL.glTexCoord2f(1.0f, 0.0f);
GL.glVertex3d(m_posObject.X + dblHSize, m_posObject.Y + dblHSize, m_posObject.Z);
GL.glTexCoord2f(1.0f, 1.0f);
GL.glVertex3d(m_posObject.X + dblHSize, m_posObject.Y - dblHSize, m_posObject.Z);
GL.glTexCoord2f(0.0f, 1.0f);
GL.glVertex3d(m_posObject.X - dblHSize, m_posObject.Y - dblHSize, m_posObject.Z);
GL.glEnd();
}
GL.glDisable(GL.GL_TEXTURE_2D);