From BlenderWiki

Jump to: navigation, search

About

This page describes a task during my Google Summer of Code project.

Scenario

Consider you wanted to add surface details to a model without changing the actual geometry but by using the paint tools that are already present in Blender.

Problem

There are several approaches to add "fake" geometrical detail to a model by applying a normal map. This normal map is then read by the visualization program to manipulate how the light bounces off the model's surface. With this approach you can for instance, sculpt a high-res model, bake a normal map, then switch back to a low-res version of the model and apply the normal map. This way you get lesser polygons to calculate but the surface detail remains nearly the same with the low-res model.

No normal mapping already works in blender as can be seen in the following pictures:

Normal Map of Suzanne
Render of the Normal Map applied to a plane, perpendicular to the surface.

Even though the rendered result with the normal map applied looks very promising there is one drawback: It is hard to manipulate a normal map by hand. This is exactly where bump map painting comes in.

A bump map is much less complex compared to a normal map. It is usually a grayscale image and the result when applied to a flat surface looks not as good as a normal map applied to a flat surface:

Bump Map of Suzanne
Render of the Bump Map applied to a plane.

As you can see, the bump map applied looks somehow flattened.

Current state in Blender

Currently, Blender can render normal map previews in the 3D view by using OpenGL Shaders, but there is no way to preview a bump map in such a way.

Idea

I want enable artists to paint and PREVIEW bump maps by using the normal paint tools. This way artists can paint directly onto the model in the viewport and see their strokes influencing how light bounces off the surface.

Video

This video demonstrates the current state of this approach: http://vimeo.com/13639081

Solution

How to visualize a bump map in the 3D view?

Since normal mapping already works, the easiest way to visualize a bump map in the 3D view is to convert the bump map into a normal map and use normal mapping as the technique to achieve the bump effect.

You might say that a color conversion for a complete image takes too long, but in fact it doesn't if you use the GPU do the computation for you. I've looked into the source code for The GIMP normalmap plugin and streamlined the explicit C code to an OpenGL shader that implicitly calculates a normal value based on four texture lookups around each pixel.

Screen shot

Screen shot of GLSL bump map preview and the final render

End-User Documentation

How to set up a model for bump map painting?

  1. Open Blender with the default cube scene.
  2. Create another space and set it to be an UV/Image Editor. Create a new image with the default settings and call it "bumpmap".
  3. Unwrap the cube using seams. Press "w" in the 3D view and select unwrap. Now in the UV/Image Editor select the bump map image.
  4. Set the texture mode to "Textured"
  5. Go to the "Texture Paint" mode.
  6. Press "n" and scroll down to the "Display" section of the view properties and under "Shading" select "GLSL". (You might also wanna check the "Only Render" box to have a nice looking preview without disturbing grids.)
  7. Go to the "Texture" settings and make these modifications to the current texture slot:
    1. Under "Mapping" set "Coordinates" to "UV".
    2. Under "Type" select "Image or Movie"
    3. Under "Image" select the "bumpmap" image we created earlier.
    4. Under "Influence->Geometry" check "Normal". You can use this value slider to adjust the height of the bumps. Negative values invert the bumps.
  8. In the UV/Image Editor under "Image" you can check "Image Painting" if you want to painting in the UV/Image Editor.

Now you've set up Blender to preview and paint Bump Maps.

What colors to use when painting?

I need to figure out how the Blender renderer interprets bump maps, but currently my shader maps dark to high/bumpy and white to low/flat.

To control the overall height of the bumps, you can use the "Normal" slider control under the texture influence group.

Bugs

Lately, Brecht reported two more errors I made when coding:

  • When there's no image, a division by zero can cause a crash. (Fixed locally but patch in tracker not updated).
  • I'm doing tangent space normals where the renderer uses something else; it probably uses UV. I need to look into this. Brecht reported that the Bump Mapping looks not good at UV seams.

Older bugs:

  • Currently there are some issues but I think they can be fixed quickly.
  • For instance if I'm not mistaken the texture seems to be projected upside down on the model.
  • Another issue to look at is the integration into Blender's normal render pipeline. Currently my patch only allows bump map preview through GLSL in the 3D view. The render output when hitting F12 still uses the height map as a color texture. It's strange though because I turned the color influence for the image texture to zero and my patch shouldn't affect the render pipeline.
  • I've used a Shader Model 4 function in the OpenGL Shader, namely textureSize2D() which seem to make problems with many graphics cards.
  • I am happy that the previewing is at least working the way I've planned it.


If you modify the GLSL code

Don't forget to generate a new "gpu_shader_material.glsl.c" file if you modified the "gpu_shader_material.glsl". You can do this by typing:

cd source/blender/gpu/intern/
../../../../release/datafiles/datatoc.py gpu_shader_material.glsl

You should then see an output like this:

Making C file <gpu_shader_material.glsl.c>
35002

Patch

Here's the patch you can apply to your trunk source code and have bump map painting working: http://www.pasteall.org/14580/diff

With the help of Fredrik H. I was able to simplify and fix the GLSL shader code and the new patch can be found here: http://www.pasteall.org/14593/diff .

I submitted a modified patch to the Patch Tracker: http://projects.blender.org/tracker/index.php?func=detail&aid=23084&group_id=9&atid=127 This patch fixes issues with graphics cards that have a Shader Model < 4 and it also simplifies the setup because the "Height to Normal" check box has been removed completely. Thanks again to Fredrik H., Tom M., Sam V. and all the friendly BlenderArtists.org users!

Builds

You can download a patched build from here:

About my GSoC branch

Currently my GSoC branch "soc-2010-kwk" is not the recommended way to test Bump Map Painting as there recently occurred some strange bugs with the paint tools.

If you want to try out Bump Map Painting, checkout the trunk and apply the patch linked above. Another opportunity is to try out the pre-patched build on graphicall.org. See above for the builds.

Example .blend file

If you want to try out painting a bump map yourself you can use this .blend file I've set up for direct painting. Either paint directly onto the model or onto the image in the UV/Image Editor.

File:Bump map painting.blend

Discussion

Thanks to Sam V. who started the thread there is also a discussion going on in the BlenderArtists.org Forum: http://blenderartists.org/forum/showthread.php?t=192874

Feedback

I really hope you like this feature! I would really appreciate every feedback or bug reports I can get. Please use the blender developers mailing list or add a comment to the video!

Thank you!

-Konrad