problem with flares and halos

I’m doing some flare rendering, using the following code snippet. Everything works fine, except that i get flare in both directions: when i’m facing the light, and when i’m backing the light. I’d like to have the effect only in the light direction, and i’ve tried to look where the error is but unfortunately i haven’t found yet. Any help would be welcome


float sunx,suny,sunz; //light coord
GLdouble modelMatrix[16]; // model matrix.
GLdouble projMatrix[16]; // projection matrix.
GLdouble sx,sy,sz; // light screen coord
float brightness=0; //brightness factor
float depth; //depth
GLint viewport[4]; // viewport.

//compute the screen coord 
glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
glGetIntegerv(GL_VIEWPORT, viewport);
gluProject(sunx,suny,sunz,modelMatrix,projMatrix,viewport,&sx,&sy,&sz); // Find out where the light is on the screen.

(sz>1.0)?sz=1.0:sz=sz;//clamp the depth light

for(int x=-4;x<=4;x++) // look around the light
    for(int y=-4;y<=4;y++){
        if(viewport[2]/300.0*x+sx < viewport[2]
          && viewport[2]/300.0*x+sx >= 0 
          && viewport[3]/300.0*y+sy < viewport[3] 
          && viewport[3]/300.0*y+sy >= 0 ) // If the point is on the screen
                    &depth); // read the depth component.
  if(depth >= sz) 
                brightness += 0.009; // If there is nothing in front of the point, increase brightness


//clamp sx and sy on  [-1,1]
sx = sx/(float)viewport[2]*2.0-1.0;
sy = sy/(float)viewport[3]*2.0-1.0;                

//finally, draw a halo, of flares

[This message has been edited by uruloki (edited 07-02-2003).]

[This message has been edited by uruloki (edited 07-02-2003).]

What’s the return value of gluProject in case the sun is behind you?
If the point is behind the viewer the intermediate w-coordinate before screen projection must be negative. Sounds like gluProject always projects, but it should indicate that situation. (Not tried myself.)

gluProject always returns GL_TRUE, i guess that’s because there is no failure. I really don’t really how i could get the w coordinate. Should i make the projection myself, without using gluProject?

I would give it a try.
You could also do some quick check with
DotProduct(view_direction, Normalize(light_position - eye_position));
If it’s in front of you the sign is positive.

I’ll try the dot product approach.
Is the normalize necessary, since i’m only interested by the sign of the dot product?
About the view_direction vector, its coordinates are (m[2] -m[6] -m[10]),right?
(m is the modelview matrix)

ok, it works perfectly with the dot product.
for now i keep it simple, that is i check the sign of dotproduct(view_dir,sun_pos). it works because sun_pos>>> observ_pos

well thank you very much for your help


If you use normalized vectors the result is the cosine of the angle between the vectors and you can optimize the check to consider only angles in the field of view by having a gross cone clip around your frustum with that.