Friday, September 23, 2016

Export fixed, bump maps working

When last I wrote, I mentioned the image exports were being offset, the texture in the OBJ export looked wrong when viewed in Blender, and that bump maps were incomplete.  You can see how wrong it looked in the screenshot below.

Bad uv coordinates and lack of normals make for a poor model of a planet.


The problem with the texture in the OBJ export turned out to be horribly incorrect u components in the uv mapping (texture coordinates).

Normalizing (converting to 0..1 range) the longitude in radians should simply be a matter of dividing by two pi, like so
     u = lon / twoPI
and not the utterly wrong
    u = (twoPI - (lon / twoPI)) / twoPI
that I was using.  Somehow the latter had worked inside the Microsoft WPF 3D library, perhaps due to some automatic normalization code in that library.  Anyhow, with that corrected, the texture on the OBJ export looked better inside Blender.  

But it still looked faceted, rather than smooth.  Why?  Because there was no normal data!  3D models emulate curved surfaces by providing vertex normals.  This may sound crazy since a normal is by definition tangent to a surface, not a point.  What 3D programs do without normal data when rendering each pixel of a face (i.e. triangle) is use the normal of the face for its calculations.  If normal data is present, then the rendering algorithm at each pixel of the face uses the normals at each vertex to produce an interpolated normal at that point.  This emulates a rounded surface.  Calculating the vertex normals for a sphere is easy.  At each vertex, the center point of the sphere is subtracted from the vertex, resulting in a vector difference.  This vector is then normalized.  Et voila, we have the vertex normal.  And suddenly, in Blender, it now looks like so.


Exported OBJ model as seen in Blender, with vertex normals helping to simulate a smooth ly-curved surface


The bump maps for terrestrial worlds were also completed.  When exported to OBJ and imported into Blender, a rendering of the bump map shows how bumpy the effect can be.  I might need to do some tweaking.

Blender rendering of a planet, with bump mapping applied.

A couple of hints for anybody using Blender who has even less experience than I do.  After importing a model, the texture will not initially show.  You have to click Alt+Z to turn on the texture.  The other hint is that bum maps aren't applied in the usual editor mode or the OpenGL render, but only the render done by clicking the Camera button in the properties/toolbox.

The regular 2D image export also works properly now.  You can see a terrestrial world in globe mode.  For the map view, I changed the export code to simply export the entire image more directly, regardless of window size, aspect ratio.
 
Globe image exported from the Planets generator.  Note the alpha/transparency.
Next up is probably the POV-Ray export, which some brief research suggests will be very similar to the OBJ export if I export the geometry as mesh2 objects.  It may even be a copy/paste of the OBJ writer class code with minor modifications for the language/format differences.  Then the UI for entering planet parameters. Clouds probably come after that, and finishing up the craters. It probably wouldn't hurt for me to run a few tests of the export for the other planet types, as well.


No comments:

Post a Comment