Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Help finding the right lighting normal to use.

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2011
    Posts
    11

    Help finding the right lighting normal to use.

    So I have these, basically floor tiles, that are like this:

    glNormal3f(0,1.0f,0);
    glVertex3f(x,heightmap[x][z],z);
    glVertex3f(x+1,heightmap[x+1][z],z); glVertex3f(x+1,heightmap[x+1][z+1],z+1);
    glVertex3f(x,heightmap[x][z+1],z+1);

    and they work fine.

    Then I have a wall tile like this:
    glNormal3f(0,0,1.0f);
    glVertex3f(35,5,52);
    glVertex3f(35,0,52);
    glVertex3f(65,0,52);
    glVertex3f(65,5,52);

    and no light will appear on it, what should the normal be?

    Can someone tell me how to calculate some lighting normals for some simple, straight, quads?

  2. #2
    Senior Member Regular Contributor
    Join Date
    Oct 2010
    Location
    France
    Posts
    458

    Re: Help finding the right lighting normal to use.

    Regarding the order of your vertices, the normals are in good orientation.

    Maybe it's just your light that is not directed to your wall ?

  3. #3
    Junior Member Newbie
    Join Date
    Feb 2011
    Posts
    11

    Re: Help finding the right lighting normal to use.

    What do you mean my light isn't directed?
    This is the code to my light:
    GLfloat position[] = { 50.0f,1.0f,50.0f,1.0f };
    glLightfv(GL_LIGHT0, GL_POSITION, position);
    GLfloat diffuseLight[] = { 2.0f, 2.0f, 2.0f, 1.0f };
    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
    GLfloat ambient[] = { 0.05f, 0.05f, 0.05f };
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
    glEnable(GL_LIGHT0);

  4. #4
    Senior Member Regular Contributor
    Join Date
    Oct 2010
    Location
    France
    Posts
    458

    Re: Help finding the right lighting normal to use.

    Can you show more of your code, mainly where the light code is, and your drawing function ?

  5. #5
    Junior Member Newbie
    Join Date
    Feb 2011
    Posts
    11

    Re: Help finding the right lighting normal to use.

    Oh wow, I forgot about this thread, well gonna continue anyway...
    Here's my scene draw code:
    void DrawGLScene()
    {

    GLfloat position[] = { 50.0f,1.0f,50.0f,1.0f };
    glLightfv(GL_LIGHT0, GL_POSITION, position);
    GLfloat diffuseLight[] = { 2.0f, 2.0f, 2.0f, 1.0f };
    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
    GLfloat ambient[] = { 0.05f, 0.05f, 0.05f };
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
    glEnable(GL_LIGHT0);

    glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); // Clear The Screen And The Depth Buffer
    glLoadIdentity(); // Reset The View

    GLfloat x_m, y_m, z_m, u_m, v_m;
    GLfloat xtrans, ztrans, ytrans;
    GLfloat sceneroty;
    int numtriangles;

    // calculate translations and rotations.
    xtrans = -xpos;
    ztrans = -zpos;
    ytrans = -walkbias-0.5f;
    sceneroty = 360.0f - yrot;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
    glLoadIdentity();

    glRotatef(lookupdown, 1.0f, 0, 0);
    glRotatef(sceneroty, 0, 1.0f, 0);

    glTranslatef(xtrans, ytrans+0.025f, ztrans);

    glBegin(GL_QUADS);
    glMateriali(GL_FRONT, GL_SHININESS, 50);
    float mcolor[] = {0.5,0.5,0.5,1.0f};
    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mcolor);
    glNormal3f(0,0,1.0f);
    glVertex3f(35,5,52);
    glVertex3f(35,0,52);
    glVertex3f(65,0,52);
    glVertex3f(65,5,52);
    glEnd();

    for(int z = 1; z<99; z++)
    {
    for(int x = 1; x<99; x++)
    {
    glBegin(GL_QUADS); // start drawing the cube.

    glMateriali(GL_FRONT, GL_SHININESS, 50);

    if(terrainmap[x][z] == 0)
    {
    float mcolor[] = {0.419,0.556,0.137,1.0f};
    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mcolor);
    }
    else
    {
    float mcolor[] = {0.96,0.815,0.686,1.0f};
    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mcolor);
    }

    glNormal3f(0,1.0f,0);
    glVertex3f(x,heightmap[x][z],z); // Top Right Of The Quad (Top)
    glVertex3f(x+1,heightmap[x+1][z],z); // Top Left Of The Quad (Top)
    glVertex3f(x+1,heightmap[x+1][z+1],z+1); // Bottom Left Of The Quad (Top)
    glVertex3f(x,heightmap[x][z+1],z+1); // Bottom Right Of The Quad (Top)

    glEnd();
    }
    }





    // swap buffers to display, since we're double buffered.
    SDL_GL_SwapBuffers();
    }

    And that's the draw scene code.
    All heightmap values are zero currently.

  6. #6
    Junior Member Newbie
    Join Date
    Feb 2011
    Posts
    11

    Re: Help finding the right lighting normal to use.

    Okay, so for a standing tile like this:
    glVertex3f(35,5,52);
    glVertex3f(35,0,52);
    glVertex3f(65,0,52);
    glVertex3f(65,5,52);
    what should the normal be?

  7. #7
    Senior Member Regular Contributor
    Join Date
    Mar 2003
    Location
    Los Angeles
    Posts
    374

    Re: Help finding the right lighting normal to use.

    The normal should be (0,0,1).
    Your poly lies in a plane parallel to the XY plane.
    Thus the normal has to either +Z or -Z.
    (No calculations have to be done.)
    In your case it's +Z because of the order in which
    the vertices are defined. This assumes you are
    using counterclockwise winding to define the frontface
    of your polygons (which is the OpenGL default).
    Am I doing your homework for you?

  8. #8
    Senior Member Regular Contributor Rosario Leonardi's Avatar
    Join Date
    Aug 2008
    Location
    Italy
    Posts
    356

    Re: Help finding the right lighting normal to use.

    Quote Originally Posted by DrenDran
    and no light will appear on it, what should the normal be?
    What does it mean `no light appear on it`?
    You see your quad? It's black? it's a flat color?

    Remember that the lighting is computed only in the vertexes, so if your light is equidistant from the vertexes and the dot product between the normal and the distance vector is the same (the light is over the center of the face), you will see an uniform color.
    Also having ambient light and diffuse light with the same parameter don't help a lot.
    ~ ~ I tell you, realtime 3D is made of blood, sweat and screams! ~ ~

  9. #9
    Junior Member Newbie
    Join Date
    Feb 2011
    Posts
    11

    Re: Help finding the right lighting normal to use.

    Using a normal/settings like this:
    glBegin(GL_QUADS);
    glMateriali(GL_FRONT, GL_SHININESS, 50);
    float mcolor[] = {0.5,0.5,0.5,1.0f};
    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mcolor);
    glNormal3f(0,0,1.0f);
    glVertex3f(35,5,52);
    glVertex3f(35,0,52);
    glVertex3f(65,0,52);
    glVertex3f(65,5,52);
    glEnd();

    I get this:


    As you can see, the ground is fine, but the polygon/quad wall is just black.

  10. #10
    Senior Member Regular Contributor
    Join Date
    Oct 2009
    Posts
    463

    Re: Help finding the right lighting normal to use.

    You will need to calculate the normals for the wall too, which will probably be (1,0,0), (-1,0,0), (0,1,0) and (0,-1,0). If you need to calculate the normal of a triangle, you can use the vector cross product ( http://en.wikipedia.org/wiki/Cross_product ), to calculate the surface normal http://en.wikipedia.org/wiki/Surface_normal .

Page 1 of 2 12 LastLast

Similar Threads

  1. Lighting errors with normal mapping
    By CDeniz in forum OpenGL: GLSL
    Replies: 3
    Last Post: 02-04-2015, 06:41 AM
  2. Lighting effect problem with normal matrix
    By Phinehas in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 10-29-2010, 03:46 AM
  3. Lighting and Normal Calculations
    By WillsB in forum OpenGL: Basic Coding
    Replies: 9
    Last Post: 03-30-2009, 06:54 PM
  4. dot3 normal mapping and per-pixel lighting?
    By bansheeogl in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 06-07-2005, 04:11 AM
  5. Ambient lighting with multitexture normal mapping.
    By imported_billw in forum OpenGL: Advanced Coding
    Replies: 4
    Last Post: 05-10-2005, 12:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean