BumpMapController and colors

Hy there,



when I use the BumpMapController it seems that the bump-mapped surface does not react on any colors.

I extended the example TestBumpMapping by some lines to add a box and switched the lightning to amient red, but the texture still just reflects with light.



Do I need to know something about colors and bump-mapping that I don't ?

Or is the BumpMapController not capable of 'handling' the lights correctly ?



Thank you

maxx_981



/*
 * Copyright (c) 2003-2009 jMonkeyEngine
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
 *   may be used to endorse or promote products derived from this software
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package jmetest.effects;

import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingBox;
import com.jme.image.Texture;
import com.jme.light.DirectionalLight;
import com.jme.math.FastMath;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.shape.Torus;
import com.jme.scene.shape.Box;
import com.jme.scene.state.MaterialState;
import com.jme.scene.state.TextureState;
import com.jme.scene.state.ZBufferState;
import com.jme.system.DisplaySystem;
import com.jme.util.BumpMapColorController;
import com.jme.util.TextureManager;
import com.jme.util.resource.ResourceLocatorTool;

import java.net.URL;

/**
 * <code>TestLightState</code>
 *
 * @author Mark Powell
 * @version $Id: TestBumpMapping.java 4130 2009-03-19 20:04:51Z blaine.dev $
 * modified by maxx, to test the influence of light with some colors
 * -> the bump-mapped surface is not reacting on the colors, should it ?
 *
 */
public class TestBumpMapping extends SimpleGame
{

    private float angle0;

    private Torus t;

    /**
     * Entry point for the test,
     *
     * @param args
     */
    public static void main(String[] args)
    {
        TestBumpMapping app = new TestBumpMapping();
        app.setConfigShowMode(ConfigShowMode.AlwaysShow);
        app.start();
    }

    /**
     * Not used in this test.
     *
     * @see com.jme.app.BaseGame#update(float)
     */
    protected void simpleUpdate()
    {
        angle0 += 2 * tpf;

        ((DirectionalLight) lightState.get(0)).setDirection(new Vector3f(2.0f * FastMath.cos(angle0), 2.0f * FastMath.sin(angle0), -1.5f));
    }

    /**
     * builds the trimesh.
     *
     * @see com.jme.app.SimpleGame#initGame()
     */
    protected void simpleInitGame()
    {

        t = new Torus("Torus", 30, 30, 5, 10);
        t.setModelBound(new BoundingBox());
        t.updateModelBound();

        BumpMapColorController c = new BumpMapColorController(t);
        t.addController(c);

        MaterialState ms = DisplaySystem.getDisplaySystem().getRenderer().createMaterialState();
        ms.setColorMaterial(MaterialState.ColorMaterial.Diffuse);
        t.setRenderState(ms);
        t.updateRenderState();

        rootNode.attachChild(t);

        TextureState ts = display.getRenderer().createTextureState();
        ts.setEnabled(true);

//        URL tex1url = ResourceLocatorTool.locateResource(ResourceLocatorTool.TYPE_MODEL, "/projects/sheep/bock_8_34/UV_Texture_Body_2048.jpg");
//        Texture tex = TextureManager.loadTexture(tex1url, Texture.MinificationFilter.Trilinear, Texture.MagnificationFilter.Bilinear);


        Texture tex = TextureManager.loadTexture(TestBumpMapping.class.getClassLoader().getResource("jmetest/data/images/FieldstoneNormal.jpg"), Texture.MinificationFilter.Trilinear, Texture.MagnificationFilter.Bilinear);

        tex.setWrap(Texture.WrapMode.Repeat);
        tex.setApply(Texture.ApplyMode.Combine);
        tex.setCombineFuncRGB(Texture.CombinerFunctionRGB.Dot3RGB);
        tex.setCombineSrc0RGB(Texture.CombinerSource.CurrentTexture);
        tex.setCombineSrc1RGB(Texture.CombinerSource.PrimaryColor);

        ts.setTexture(tex, 0);


//        URL tex2url = ResourceLocatorTool.locateResource(ResourceLocatorTool.TYPE_MODEL, "/projects/sheep/bock_8_34/UV_Texture_Body_2048_NORMAL.jpg");
//               Texture tex2 = TextureManager.loadTexture(tex2url, Texture.MinificationFilter.Trilinear, Texture.MagnificationFilter.Bilinear);



        Texture tex2 = TextureManager.loadTexture(TestBumpMapping.class.getClassLoader().getResource("jmetest/data/images/Fieldstone.jpg"), Texture.MinificationFilter.Trilinear, Texture.MagnificationFilter.Bilinear);

        tex2.setApply(Texture.ApplyMode.Combine);
        tex2.setWrap(Texture.WrapMode.Repeat);
        tex2.setCombineFuncRGB(Texture.CombinerFunctionRGB.Modulate);
        tex2.setCombineSrc0RGB(Texture.CombinerSource.Previous);
        tex2.setCombineSrc1RGB(Texture.CombinerSource.CurrentTexture);
        ts.setTexture(tex2, 1);

        t.copyTextureCoordinates(0, 1, 1.0f);
        t.scaleTextureCoordinates(0, 8);
        t.scaleTextureCoordinates(1, 8);

        t.setRenderState(ts);

        ZBufferState buf = display.getRenderer().createZBufferState();
        buf.setEnabled(true);
        buf.setFunction(ZBufferState.TestFunction.LessThanOrEqualTo);

        t.setRenderState(buf);


        DirectionalLight dr = new DirectionalLight();
//        dr.setAmbient(new ColorRGBA(0.75f, 0.75f, 0.75f, 1));
        dr.setAmbient(new ColorRGBA(1f, 0f, 0f, 1));// reddish light
        dr.setDiffuse(new ColorRGBA(1, 1, 1, 1));
        dr.setEnabled(true);
        dr.setDirection(new Vector3f(1, 1, -1));

        createReferenceBox();

        lightState.detachAll();
        lightState.attach(dr);

        rootNode.updateRenderState();
        rootNode.updateGeometricState(0.0f, true);
      
   }

    public void createReferenceBox()
    {
        // Create a ref box
        Box b = new Box("Reference", new Vector3f(0f, 0f, 0f), 3f, 3f, 3f);
        // Give the box bounds
        b.setModelBound(new BoundingBox());
        b.setRandomColors();
        b.updateModelBound();

        rootNode.attachChild(b);
    }
}

It seems like the BumpMapController doesn't take material or light color into account.  :expressionless:

It is mostly shown as an example of how to achieve single-light bump mapping under video cards not supporting shaders, and is not intended for practical use. If you want more advanced bump mapping effects, or support for more than one light, you would have to write a bump mapping shader that supports what you need.


If you want more advanced bump mapping effects, or support for more than one light, you would have to write a bump mapping shader that supports what you need.


Can you give me some more hints on that ?

- What 'kind' of shaders do I have to write (I guess those with frag/vert endings) ?
- So it is in principle possible to use bump-mapping in jme2, I just need more shaders ?
- I would have to manage the shaders myself I guess, ie. enabling the correct shaders to the right time/point in space ?

Thank you

maxx_981 said:

- What 'kind' of shaders do I have to write (I guess those with frag/vert endings) ?

Shaders in the GLSL (OpenGL Shading Language), vertex and fragment. I suggest version 1.0 since most video cards support that.

maxx_981 said:

- So it is in principle possible to use bump-mapping in jme2, I just need more shaders ?

Not more shaders, you just need to use shaders. The BumpMapController method does not use shaders, it creates the bump mapping effect through the DOT3 texture combiner extension which requires more resources and multiple passes to support material colors and multiple lights.
Generally speaking, it is difficult to use shaders in jME2 because shaders are "second class" and don't receive much attention in terms of user-friendliness. E.g you can specify a shader, adjust the uniforms, etc but management is left entirely to you. jME3 in contrary is designed to work with shaders, in fact bump mapping support is available by default, you just need to provide a normal map to enable it.

maxx_981 said:

- I would have to manage the shaders myself I guess, ie. enabling the correct shaders to the right time/point in space ?

Yeah. You'll also have to configure the shaders. For example, if you want to use fog on one bump shader, but no fog on another, you'll have to either use defines, constants or uniforms to modify the shader's behavior.


it creates the bump mapping effect


Ok. I wasn't aware of that.


jME3 in contrary is designed to work with shaders, in fact bump mapping support is available by default, you just need to provide a normal map to enable it.


I'd love to use the 'shader based' jme3, that sounds very interesting and powerful.

On the other hand, I would miss all the nice features I am dependent on right now, like GameStates, Physics and Ogre-Skeletal animation.

I checked out jme3 and had a short look at it. Seems I need to build up some more knowledge about shaders in order to use it properly. So I will try out jme2 with GLSL programs.


I suggest version 1.0 since most video cards support that.


- So, I could write a shader for GLSL 1.5 theoretically, if the graphics cards supports it ?
- Can I test what version a card supports at runtime and use the matching GLSL program ?


Generally speaking, it is difficult to use shaders in jME2 because shaders are "second class" and don't receive much attention in terms of user-friendliness.


- But they can be used and are also not limited in theire performance somehow ?
maxx_981 said:

I would miss all the nice features I am dependent on right now, like GameStates, Physics and Ogre-Skeletal animation.

All of these features are planned for inclusion at some time for jME3, so although using jME3 might not work for you now, it will become more useful with time and support all the important features in jME2 that people depend on.

maxx_981 said:

I checked out jme3 and had a short look at it. Seems I need to build up some more knowledge about shaders in order to use it properly. So I will try out jme2 with GLSL programs.

Actually that is not entirely necessary. jME3 provides a large material library for you to use, these materials allow the you to achieve a large set of effects without having to write shaders or create your own materials. You are only required to provide the parameters for the material, like the textures and shading style.


maxx_981 said:

- So, I could write a shader for GLSL 1.5 theoretically, if the graphics cards supports it ?

If the graphics card and the driver installed on your computer, yes.

maxx_981 said:

- Can I test what version a card supports at runtime and use the matching GLSL program ?

Yes. For that jME3 has "techniques" in the materials, which can be selected at runtime depending on the features supported by the video card.

maxx_981 said:

- But they can be used and are also not limited in theire performance somehow ?

They are there "as is", a wrapper over OpenGL's shader API. No special features or extensions are provided otherwise.