Page 2 of 2 FirstFirst 12
Results 11 to 14 of 14

Thread: Spot lights...

  1. #11

    Spot lights...

    Ok it seems the reason why my light was not working properly was because my quad was not tessallated enough ( from http://www.opengl.org/resources/feat...es/oglpitfall/ ).

    Ok I have that appearing with...
    [pascal]
    // Start Drawing Our Quads
    glBegin(GL_QUADS);
    for i := 0 to 43 do
    begin
    for j := 0 to 43 do
    begin
    w := 1 / 44 * Application.Width;
    h := 1 / 44 * Application.Height;
    glTexCoord2f((i+1)/GridWidth, (j+1)/GridHeight); glVertex3f( (i+1)*w, (j+1)*h,0);
    glTexCoord2f((i+1)/GridWidth, j /GridHeight); glVertex3f( (i+1)*w, j *h,0);
    glTexCoord2f( i /GridWidth, j /GridHeight); glVertex3f( i *w, j *h,0);
    glTexCoord2f( i /GridWidth, (j+1)/GridHeight); glVertex3f( i *w, (j+1)*h,0);
    end;
    end;
    glEnd();
    [/pascal]

    Ok but my light still does not appear properly.

    I currently have a SpotLight record set as follows...
    [pascal]
    TLight = record
    amb : array[0..3] of GLFloat;
    diff : array[0..3] of GLFloat;
    spec : array[0..3] of GLFloat;
    pos : array[0..3] of GLFloat;
    spotDir : array[ 0..2 ] of GLfloat;
    spotExp : GLfloat;
    spotCutoff : GLfloat;
    atten : array[ 0..2 ] of GLfloat;

    trans : array[ 0..2 ] of GLfloat;
    rot : array[ 0..2 ] of GLfloat;
    end;
    [/pascal]
    and populating it as follows...
    [pascal]
    // ambient
    SpotLight.amb[0] := 0.2;
    SpotLight.amb[1] := 0.0;
    SpotLight.amb[2] := 0.0;
    SpotLight.amb[3] := 1.0;

    // diffuse
    SpotLight.diff[0] := 0.8;
    SpotLight.diff[1] := 0.0;
    SpotLight.diff[2] := 0.0;
    SpotLight.diff[3] := 1.0;

    // specular
    SpotLight.spec[0] := 0.4;
    SpotLight.spec[1] := 0.0;
    SpotLight.spec[2] := 0.0;
    SpotLight.spec[3] := 1.0;

    // position
    SpotLight.pos[0] := 0.0;
    SpotLight.pos[1] := 0.0;
    SpotLight.pos[2] := 0.0;
    SpotLight.pos[3] := 1.0;

    // direction
    SpotLight.spotDir[0] := 0.0;
    SpotLight.spotDir[1] := 0.0;
    SpotLight.spotDir[2] := -1.0;

    // exponent
    SpotLight.spotExp := 20.0;

    // cutoff
    SpotLight.spotCutoff := 60.0;

    // attenuation
    SpotLight.atten[0] := 1.0;
    SpotLight.atten[1] := 0.0;
    SpotLight.atten[2] := 0.0;

    // translation
    SpotLight.trans[0] := 0.0;
    SpotLight.trans[1] := 1.25;
    SpotLight.trans[2] := 0.0;

    // rotation
    SpotLight.rot[0] := 0.0;
    SpotLight.rot[1] := 0.0;
    SpotLight.rot[2] := 0.0;
    [/pascal]

    and I'm setting the light as follows
    [pascal]
    glLightfv(GL_LIGHT0, GL_POSITION, @SpotLight.pos);
    glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, @SpotLight.spotDir);
    [/pascal]

    Why is my light not appearing when I use glOrtho for projection?
    I know there are other ways to get this effect, but I really want to learn how to do it with OpenGL lighting. I just want the spot light to shine on a small portion of the background texture, or another example would be for the light to only highlight the bits of the background texture that the mouse is over. I'm sure you are all familiar with the effect.

    I think my problem is my understanding of the co-ordinate system as it applies to glOrtho. With gluPerspective I sort of understand you need to use small decimal values, but with glOrtho I not sure if that is still the case or not. I think I need to go back to OpenGL school. Can anyone shed some light ( not pun intended ) on the situation?
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

  2. #12

    Spot lights...

    Did a quick test, it works for me
    [pascal]
    pos[0]:= 0.0;
    pos[1]:= 0.0;
    pos[2]:= 0.0;
    pos[3]:= 1.0;

    Dir[0]:= 0.0;
    Dir[1]:= 0.0;
    Dir[2]:= -1.0;

    glLightfv(GL_LIGHT0, GL_POSITION, @pos);
    glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, @Dir);
    glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 60);
    glLightf( GL_LIGHT0, GL_SPOT_EXPONENT, 20.0);

    glenable(Gl_LIGHTING);
    glenable(GL_LIGHT0);
    glMatrixMode(GL_PROJECTION);
    glloadidentity();
    glOrtho(0, 1, 0, 1, 10, -10);

    glBegin(GL_QUADS);
    for i:= 0 to 43 do
    begin
    for j:= 0 to 43 do
    begin
    w := 1 / 44;
    h := 1 / 44;
    glVertex3f((i+1) * w, (j+1) * h, -1);
    glVertex3f( i * w, (j+1) * h, -1);
    glVertex3f( i * w, j * h, -1);
    glVertex3f((i+1) * w, j * h, -1);
    end;
    end;
    glEnd;[/pascal]

  3. #13

    Spot lights...

    My glOrtho is set to [pascal]glOrtho( 0.0, width, height, 0.0, -1.0, 1.0 );[/pascal]

    What is the advantage is there in using your implemenation over mine?
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

  4. #14

    Spot lights...

    Height and width are constants just out of my preference to keep things resolution invariant. Top is more than bottom naturally, if done the other way around winding order changes.
    Edit: I used zNear and zFar incorrectly, it should be like yours near < far

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •