Shine

Ok it’s been a while since I updated this, it’s also been a while since I’ve made any notable progress on this terrain rendering stuff.  I decided to scrap the chunking idea all together, as my VBO performance is more than adequate at the moment. Instead I started working on making it look better. It’s evident from the screenshots in my last post that the terrain doesn’t look that good, and you can’t really tell that it’s bumpy if it weren’t for the changes in colour.  I needed to improve this. To that end, I have added a couple of things to the terrain.

Normals

If my terrain is ever going to look better, I need normals. That is, vectors perpendicular to my terrain. Ok so it’s slightly more involved than that, but not much. If I haven’t detailed it already, my terrain generating system works like this:

Stage 1 – generating the vertices:

  1. Load an image (heightmap) into the engine using DevIL.
  2. Read the pixel data and generate an intensity based on the RGB values
  3. Create a vertex at each point (pixel * a scale value) using the intensity to calculate the height (Y axis)
  4. Calculate the colour for each point using the intensity (scaling up from blue to green)

Stage 2 – indexing the vertices and generating face normals:

  1. Create an index array using the vertices generated from the image (creating quads)
  2. For each quad generated, calculate the normal by getting the cross product of two of the edges of the quad and normalising it
  3. Add this to the array of face normals

Stage 3 – vertex normals and VBO:

  1. Read the 4 face normals for the faces around the current vertex
  2. Add the normalised sum of these 4 normals to the normal array – this is the normal for this vertex
  3. When the processing is complete, put the vertices, normals, and colours into the VBO

Well there it is, my terrain now has normals. The only problem is, there is no way to currently show them off nicely. This is where the next major addition to the render made an appearance.

Shaders

I added a class that encapsulates the creation and compilation of a GLSL shader program based on a number of text files containing shader code. This one is still under construction – needless to say, it does what I need it to do for now, which is to load and use a shader.  I also wrote a very basic shader to simulate a light, and using the normals from my terrain I calculate an intensity for each vertex (the dot product of the light direction and the vertex normal) and use it to scale the colour of the vertex. The result is quite nice, as you can see below…

normal-shader

The height in this image is a lot more obvious compared to drawing without normals and lighting.

~ by badgerr on March 22, 2009.

One Response to “Shine”

  1. You have a terrain system with vertex normals and a GLSL shader, now you’re just one step away from Normal Mapping the terrain, why not go the whole nine yards?

Leave a comment