264 CHAPTER 8: Putting It All Together^
And from the lens flare object, ensure that renderFlareAt() is called with the new
parameters.
Another three helper routines we need are gluGetScreenLocation(), gluProject(), and
gluMultMatrixVector3f(), which will return the screen coordinates of a specified 3D
point by mimicking exactly what is happening inside OpenGL. With this we can get the
screen location of the sun needed to aim the flare in the proper direction. One end will
lead right to the sun, while the other will mirror that. To achieve this, add Listing 8-10 to
miniGLU.
Listing 8-10. Gets the screen coordinates of a given 3D location
GLint gluProject(GLfloat objx, GLfloat objy, GLfloat objz,
const GLfloat modelMatrix[16],
const GLfloat projMatrix[16],
const GLint viewport[4],
GLfloat *winx, GLfloat *winy, GLfloat *winz)
{
float in[4];
float out[4];
in[0]=objx; //1
in[1]=objy;
in[2]=objz;
in[3]=1.0;
gluMultMatrixVector3f (modelMatrix, in, out); //2
gluMultMatrixVector3f (projMatrix, out, in);
if (in[3] == 0.0)
in[3]=1;
in[0] /= in[3];
in[1] /= in[3];
in[2] /= in[3];
/* Map x, y and z to range 0-1 */
in[0] = in[0] * 0.5 + 0.5; //3
in[1] = in[1] * 0.5 + 0.5;
in[2] = in[2] * 0.5 + 0.5;
/* Map x,y to viewport */
in[0] = in[0] * viewport[2] + viewport[0];
in[1] = in[1] * viewport[3] + viewport[1];
*winx=in[0];
*winy=in[1];
*winz=in[3];
return(GL_TRUE);
}