This section is under re-development. It is not finished, and not fully functional. The current
development stage is pre-alpha and as such, not every feature is even implemented yet.
Heightmapping
For a change of scene from PHP for a bit, I had a huge coding spree with C++. Ill tell you now, the more I use this language, the more I like it.
I was getting bored of rendering a quad or two (or the un-finished Quake BSP classes pumping out a bunch of coloured brushes), so I decided it was time to give the graphics card a bit more work, and render something a bit more interesting, like a natural landscape. So, I did! I wrote a reader for a .raw image, which is nothing but the actual 'raw' image data. Just fread() the pointer to the file, for the size of its width * height, and you have the image data. The heightmap is somewhat interesting - the colour value for each pixel denotes a vertex's height. So, if the pixel at 37,48 had a colour value (as a byte) of 127 (greyish), then the height value (as a float) would be 127/256.0 = 0.49609375. So, when rendering the heightmap - we glVertex3f() the x position (from the for() loop), the z position (from the sub-for() loop), and use the height value we calculated earlier for that individual vertex's height. So in this case, it would be: glVertex3f(37, 0.49609375, 48); //X, Y, Z For the ONE vertex. But bare in mind that, for terrain, were using GL_TRIANGLE_STRIP, which wants 4 vertices specified in a 'Z' pattern: //Top left pointHeight = this->pointHeights[x][z]; glVertex3f(x, pointHeight, z); //Top Right pointHeight = this->pointHeights[x+1][z]; glVertex3f(x+1, pointHeight, z); //Bottom Left pointHeight = this->pointHeights[x][z+1]; glVertex3f(x, pointHeight, z+1); //Bottom Right pointHeight = this->pointHeights[x+1][z+1]; glVertex3f(x+1, pointHeight, z+1); Not too bad when it comes to rendering, but the bit that had me stumped was actually filling up the pointHeights[][] array. For ages I was referencing the vertex in the texture data as texture->textureData[x*z], which was wrong. It should be: texture->textureData[(x*terrainWidth) + z] So to summarise, filling the values and rendering the values are the same for() and sub-for(). The worst part was getting the maths right when loading up the pointHeights array. This is not meant to be a tutorial on it - maybe one day ill write one though. At the moment im certainly not convinced that ive done it as efficiently as it could be. Now the question is - why on earth cant I get this .raw texture to map to a quad?!? |