Jump to content


Lighting with OpenGL on a basic heightmap


6 replies to this topic

#1 Zeophlite

    New Member

  • Members
  • Pip
  • 6 posts

Posted 09 January 2007 - 09:56 AM

Hello,

I'm attempting to make an extremely basic heightmap.
The desired effect is just to have "grey" triangles of different darkness. The dark triangles would be to the far side of the light, with the light triangles to the close side of the light.

Eventually I'm planning on changing the colour to match the terrain - probaly through textures. However now I'm just trying to have a generic grey heightmap.

When I try running my code, it outputs these images:
1
2
Obviously this isn't what I intended.

To clarify, this is the effect I was hoping for:
3
(note that this grid is a different heightmap, but the effect is still the same)


Here is my current code: (lots of unrelated code has been removed to ease reading)

main.cpp
CHeightMap.h
CHeightMap.cpp



What additions/changes need to be made to create the effect i desire?


Thanks in advance,


Daniel

#2 Reedbeta

    DevMaster Staff

  • Administrators
  • 4979 posts
  • LocationBellevue, WA

Posted 09 January 2007 - 10:07 AM

I looked over your code and didn't see anything obviously wrong. Try visualizing the normals (from each vertex draw a short line showing the direction of the normals); it should be fairly obvious if something's wrong.
reedbeta.com - developer blog, OpenGL demos, and other projects

#3 Zeophlite

    New Member

  • Members
  • Pip
  • 6 posts

Posted 09 January 2007 - 01:54 PM

I've tried that computationally.
The program draws a red unit line segment in the direction of the normal (code ommited). All normals point upwards.

Also, I sometimes have a point at the light source (toggle on and off)
This is above the heightmap

Thanks in advance

#4 Reedbeta

    DevMaster Staff

  • Administrators
  • 4979 posts
  • LocationBellevue, WA

Posted 09 January 2007 - 04:57 PM

Do all the normals point straight upwards? The normals should, of course, be angled to match the geometry. Think of hairs on a person's skin that stick straight out from the skin, like if your body has an electric charge. That's what the normals should look like. If the normals are all the same, that's of course why you're only getting a solid color.
reedbeta.com - developer blog, OpenGL demos, and other projects

#5 Zeophlite

    New Member

  • Members
  • Pip
  • 6 posts

Posted 11 January 2007 - 01:15 PM

Sorry, I mean the normals point generally upwards, not directly. (as in not parallel)

Even if they did, would my code cause that?
I've looked through and it doesn't seem to cause parallel normals.

Thanks anyway

#6 Wernaeh

    Senior Member

  • Members
  • PipPipPipPip
  • 368 posts

Posted 12 January 2007 - 12:44 AM

Hey there :)

After skipping through your code, I think that all the basics seem to be alright, though I didn't bother to actually check each and every index.

There is a bug though, regarding some of the parameters (especially the light and material ones) for gl<FunctionType>fv !

These expect pointers to float values, not to doubles. Just casting the array pointer to float will _not_ turn the entire array to floats, it will just interpret the data of a single double value as two consecutive floats. (bitwise!)

It's kinda hard to imagine what your normals actually look like from your description.
Vertex normals _tend_ to look more "upwards" than face normals, so that shouldn't be a problem, just consider that two faces that are nearly perpendicular to each other have a vertex normal that still faces up in a 45 degree angle.

Things to try out, if the float/double thingy doesn't solve it:

a) You do have quite a lot of lighting options enabled.
Disable everything except for diffuse lighting and a gray color (setting appropriate light and material components to 0), so you might have more of a grasp where things go wrong later on (otherwise, you can't actually tell whether the result is just a coincidence of ambient / specular effects with bad normals, for instance)

b) Use a parallel light pointing straight down, you can't be sure whether the point light is so near that everything just ends up at maximal intensity.

Other points to be careful with:
- Specify the lights _before_ specifying the camera, otherwise, they'll be local to the camera system!
- When you use glScale() to bring your terrain up to more terrainlike sizes, be sure to enable renormalizing! All your normals pass through the same transforms as your vertices...

Hope this helps,
Cheers,
- Wernaeh
Some call me mathematician, some just call me computer guy. Yet, I prefer the term professional weirdo :)

#7 Zeophlite

    New Member

  • Members
  • Pip
  • 6 posts

Posted 12 January 2007 - 12:25 PM

Thanks for that Wernaeh,

I am actually away from my pc (using an internet cafe :wallbash: ) so can't test your suggestions. However from what I hear, this is probaly the error.

I will try again once I return home.

Thanks again!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users