Terrain Lighting: Strange lines

This is a discussion on Terrain Lighting: Strange lines within the Game Programming forums, part of the General Programming Boards category; I've got some weird lighting going on with my terrain. It's only in specific places, but it looks something like ...

  1. #1
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229

    Terrain Lighting: Strange lines

    I've got some weird lighting going on with my terrain. It's only in specific places, but it looks something like this:
    http://i14.photobucket.com/albums/a3...weirdlines.png
    It looks really bad in that one, but other places it's not noticeable at all.

    Heres a pic with the lines drawn in:
    http://i14.photobucket.com/albums/a3...ial/weird2.png

    Each pair of triangles seem to get lighter as you move toward the hypotenuse. Anyone else had this problem or know where in general the problem might be coming from?

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,597
    Yes I've had that problem before. It happens when your normals are incorrect. You are probably computing normals using the wrong winding order for your system. So the lighted vertex of the quad should actually be the dark portion of the quad. It's hard to explain but it gives really odd renders such as the one you've shown.

    EDIT:

    You may also have some backlighting happening. If you have culling turned off and are using infinite diffuse light you could be seeing the lighting from the other side of the quad.

    I've had this problem before as I said earlier but I don't remember exactly what caused it and that really irritates me.
    Last edited by VirtualAce; 04-03-2008 at 08:04 PM.

  3. #3
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Thanks, great information. I don't think it is the culling. I enabled it, but still got the same results. I am however ignorant to how exactly gl culling works. I think it's most likely the first issue you mentioned. Unfortunately I don't know what it means. I'm going to do some Googling, but if you have any references that would help me then link me please.

    Thanks!

    edit: OK, so When I did try culling I had to enable front face culling for it to work. Then when I changed the winding order it would work properly with back culling. The strange lighting persisted either way I did it though. To change the winding order I was just drawing verts 2, 1, then 0 instead of doing 0, 1, then 2. Is this really changing the winding order?
    Last edited by IdioticCreation; 04-03-2008 at 10:45 PM.

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    You might be computing the normals wrong in general, try rendering them.

  5. #5
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    I've been messing with it a lot. The normals look fine. I just can't figure out what's causing it.

    I calculate the normals for every triangle, then use those normals to calculate the normals for each vertex. Here is the function that calculates the vertex normals. Can someone please look over it for me?
    Code:
    void Map::calcVertexNorms()
    {
        float l;
        pos normal;
        normal.x = normal.y = normal.z = 0.0f;
        int pitch = numTrisW*2;
        for (int i=0; i < pitch*numTrisH; i+=2)
        {
            normal.x += mapTris[i].norm.x;
            normal.y += mapTris[i].norm.y;
            normal.z += mapTris[i].norm.z;
            normal.x += mapTris[i+1].norm.x;
            normal.y += mapTris[i+1].norm.y;
            normal.z += mapTris[i+1].norm.z;
            if (i - pitch > 0)
            {
                normal.x += mapTris[i-pitch].norm.x;
                normal.y += mapTris[i-pitch].norm.y;
                normal.z += mapTris[i-pitch].norm.z;
            }
            if (i - pitch > 0 && i &#37; pitch != 0 && i % pitch != 1)
            {
                normal.x += mapTris[i-pitch-1].norm.x;
                normal.y += mapTris[i-pitch-1].norm.y;
                normal.z += mapTris[i-pitch-1].norm.z;
                normal.x += mapTris[i-pitch-2].norm.x;
                normal.y += mapTris[i-pitch-2].norm.y;
                normal.z += mapTris[i-pitch-2].norm.z;
            }
            if (i % pitch != 0)
            {
                normal.x += mapTris[i-1].norm.x;
                normal.y += mapTris[i-1].norm.y;
                normal.z += mapTris[i-1].norm.z;
            }
            l = sqrtf((normal.x * normal.x) + (normal.y * normal.y) + (normal.z * normal.z));
            normal.x /= l;
            normal.y /= l;
            normal.z /= l;
    
            mapTris[i].verts[0].norm = normal;
            mapTris[i+1].verts[0].norm = normal;
    
            if (i - pitch > 0)
            {
                mapTris[i-pitch].verts[1].norm = normal;
            }
            if (i - pitch > 0 && i % pitch != 0 && i % pitch != 1)
            {
                mapTris[i-pitch-1].verts[1].norm = normal;
                mapTris[i-pitch-2].verts[2].norm = normal;
            }
            if (i % pitch != 0)
            {
                mapTris[i-1].verts[2].norm = normal;
            }
    
        }
    }
    If you need any clarifications let me know, because I made this on my own with out looking at any other terrain engines.

  6. #6
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    well for starters, this code doesn't do anything.
    Code:
            normal.x /= l;
            normal.y /= l;
            normal.z /= l;
    You need to normalize by dividing each component by the magnitude of the vector, dividing by 1 doesn't change anything!

    Second, you're assigning the same normal to all vertices, this is equivalent to just doing per poly lighting. To compute the normal for each vertex, you need need to consider all surrounding triangles.

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Quote Originally Posted by Perspective View Post
    well for starters, this code doesn't do anything.
    Code:
            normal.x /= l;
            normal.y /= l;
            normal.z /= l;
    You need to normalize by dividing each component by the magnitude of the vector, dividing by 1 doesn't change anything!
    Read the line above, it's "lowercase L", not "one"
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    hah! it looks exactly the same with my font set.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    43
    Every vertex in your map has 6 neighbouring triangles, but I don't see that you sum 6 normals (or perhaps I'm just to lazy to really understand your code)

    Try reducing the map to just a couple of triangles, then it's much easier to debug the code. Create a 2 by 4 strip that has a very steep angle, that would make it possible to spot where the problem is.

  10. #10
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Perspective, I am taking into account each triangle that is around the vertex. I think maybe you are not understand because I am assigning the calculated normal to the proper verticies of the surrounding triangles. Every triangle has its own set of 3 verticies. I don't know if this is how it's supposed to be done or not, but it's not the problem at the moment.

    fractality, you've got it. I have a variable called "normal" I set it to 0, Then "+=" 'd all the face normals. Your suggestion to use only a few triangles sounds like a good idea.

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    43
    Now I've actually read the code carefully and I can't say I see anything wrong with it. The indexes seems to be correct.
    Are you sure you put the right normal on the right vertex when you send the triangles to the graphics card?
    It would probably look a lot like on your pictures if you had accidentally swapped around the normals on the vertices of the triangles (like normal 0 on vertex 1 etc).
    Double check that, otherwise I've no more ideas on what could be wrong.

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    70
    Try switching between gouraud and flat shading, maybe that will reveal something.

  13. #13
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,597
    The shade mode is clearly not the problem since the values at the vertices are being interpolated in the example screenshots. It is the normals that are the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-07-2008, 07:19 PM
  2. Print out first N lines
    By YoYayYo in forum C Programming
    Replies: 1
    Last Post: 02-21-2008, 12:58 AM
  3. Continous LOD Terrain Source... Released!
    By Perspective in forum Game Programming
    Replies: 13
    Last Post: 04-18-2006, 12:21 AM
  4. Line Counting
    By 00Sven in forum C Programming
    Replies: 26
    Last Post: 04-02-2006, 09:59 PM
  5. New terrain engine
    By VirtualAce in forum Game Programming
    Replies: 16
    Last Post: 03-16-2006, 02:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21