Search in sources :

Example 1 with NormalMapTexture

use of org.rajawali3d.materials.textures.NormalMapTexture in project Rajawali by Rajawali.

the class RajawaliVRExampleRenderer method initScene.

@Override
public void initScene() {
    DirectionalLight light = new DirectionalLight(0.2f, -1f, 0f);
    light.setPower(.7f);
    getCurrentScene().addLight(light);
    light = new DirectionalLight(0.2f, 1f, 0f);
    light.setPower(1f);
    getCurrentScene().addLight(light);
    getCurrentCamera().setFarPlane(1000);
    getCurrentScene().setBackgroundColor(0xdddddd);
    createTerrain();
    try {
        getCurrentScene().setSkybox(R.drawable.posx, R.drawable.negx, R.drawable.posy, R.drawable.negy, R.drawable.posz, R.drawable.negz);
        LoaderAWD loader = new LoaderAWD(getContext().getResources(), getTextureManager(), R.raw.space_cruiser);
        loader.parse();
        Material cruiserMaterial = new Material();
        cruiserMaterial.setDiffuseMethod(new DiffuseMethod.Lambert());
        cruiserMaterial.setColorInfluence(0);
        cruiserMaterial.enableLighting(true);
        cruiserMaterial.addTexture(new Texture("spaceCruiserTex", R.drawable.space_cruiser_4_color_1));
        Object3D spaceCruiser = loader.getParsedObject();
        spaceCruiser.setMaterial(cruiserMaterial);
        spaceCruiser.setZ(-6);
        spaceCruiser.setY(1);
        getCurrentScene().addChild(spaceCruiser);
        spaceCruiser = spaceCruiser.clone(true);
        spaceCruiser.setZ(-12);
        spaceCruiser.setY(-3);
        spaceCruiser.setRotY(180);
        getCurrentScene().addChild(spaceCruiser);
        loader = new LoaderAWD(getContext().getResources(), getTextureManager(), R.raw.dark_fighter);
        loader.parse();
        Material darkFighterMaterial = new Material();
        darkFighterMaterial.setDiffuseMethod(new DiffuseMethod.Lambert());
        darkFighterMaterial.setColorInfluence(0);
        darkFighterMaterial.enableLighting(true);
        darkFighterMaterial.addTexture(new Texture("darkFighterTex", R.drawable.dark_fighter_6_color));
        Object3D darkFighter = loader.getParsedObject();
        darkFighter.setMaterial(darkFighterMaterial);
        darkFighter.setZ(-6);
        getCurrentScene().addChild(darkFighter);
        CatmullRomCurve3D path = new CatmullRomCurve3D();
        path.addPoint(new Vector3(0, -5, -10));
        path.addPoint(new Vector3(10, -5, 0));
        path.addPoint(new Vector3(0, -4, 8));
        path.addPoint(new Vector3(-16, -6, 0));
        path.isClosedCurve(true);
        SplineTranslateAnimation3D anim = new SplineTranslateAnimation3D(path);
        anim.setDurationMilliseconds(44000);
        anim.setRepeatMode(RepeatMode.INFINITE);
        // -- orient to path
        anim.setOrientToPath(true);
        anim.setTransformable3D(darkFighter);
        getCurrentScene().registerAnimation(anim);
        anim.play();
        loader = new LoaderAWD(getContext().getResources(), getTextureManager(), R.raw.capital);
        loader.parse();
        Material capitalMaterial = new Material();
        capitalMaterial.setDiffuseMethod(new DiffuseMethod.Lambert());
        capitalMaterial.setColorInfluence(0);
        capitalMaterial.enableLighting(true);
        capitalMaterial.addTexture(new Texture("capitalTex", R.drawable.hullw));
        capitalMaterial.addTexture(new NormalMapTexture("capitalNormTex", R.drawable.hulln));
        capital = loader.getParsedObject();
        capital.setMaterial(capitalMaterial);
        capital.setScale(18);
        getCurrentScene().addChild(capital);
        path = new CatmullRomCurve3D();
        path.addPoint(new Vector3(0, 13, 34));
        path.addPoint(new Vector3(34, 13, 0));
        path.addPoint(new Vector3(0, 13, -34));
        path.addPoint(new Vector3(-34, 13, 0));
        path.isClosedCurve(true);
        anim = new SplineTranslateAnimation3D(path);
        anim.setDurationMilliseconds(60000);
        anim.setRepeatMode(RepeatMode.INFINITE);
        anim.setOrientToPath(true);
        anim.setTransformable3D(capital);
        getCurrentScene().registerAnimation(anim);
        anim.play();
    } catch (Exception e) {
        e.printStackTrace();
    }
    lookatSphere = new Sphere(1, 12, 12);
    Material sphereMaterial = new Material();
    sphereMaterial.setDiffuseMethod(new DiffuseMethod.Lambert());
    sphereMaterial.enableLighting(true);
    lookatSphere.setMaterial(sphereMaterial);
    lookatSphere.setColor(Color.YELLOW);
    lookatSphere.setPosition(0, 0, -6);
    getCurrentScene().addChild(lookatSphere);
    initAudio();
}
Also used : LoaderAWD(org.rajawali3d.loader.LoaderAWD) Material(org.rajawali3d.materials.Material) Vector3(org.rajawali3d.math.vector.Vector3) NormalMapTexture(org.rajawali3d.materials.textures.NormalMapTexture) Texture(org.rajawali3d.materials.textures.Texture) NormalMapTexture(org.rajawali3d.materials.textures.NormalMapTexture) SplineTranslateAnimation3D(org.rajawali3d.animation.SplineTranslateAnimation3D) TextureException(org.rajawali3d.materials.textures.ATexture.TextureException) Object3D(org.rajawali3d.Object3D) Sphere(org.rajawali3d.primitives.Sphere) CatmullRomCurve3D(org.rajawali3d.curves.CatmullRomCurve3D) DirectionalLight(org.rajawali3d.lights.DirectionalLight) DiffuseMethod(org.rajawali3d.materials.methods.DiffuseMethod)

Example 2 with NormalMapTexture

use of org.rajawali3d.materials.textures.NormalMapTexture in project Rajawali by Rajawali.

the class RajawaliVRExampleRenderer method createTerrain.

public void createTerrain() {
    //
    // -- Load a bitmap that represents the terrain. Its color values will
    //    be used to generate heights.
    //
    Bitmap bmp = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.terrain);
    try {
        SquareTerrain.Parameters terrainParams = SquareTerrain.createParameters(bmp);
        // -- set terrain scale
        terrainParams.setScale(4f, 54f, 4f);
        // -- the number of plane subdivisions
        terrainParams.setDivisions(128);
        // -- the number of times the textures should be repeated
        terrainParams.setTextureMult(4);
        //
        // -- Terrain colors can be set by manually specifying base, middle and
        //    top colors.
        //
        // --  terrainParams.setBasecolor(Color.argb(255, 0, 0, 0));
        //     terrainParams.setMiddleColor(Color.argb(255, 200, 200, 200));
        //     terrainParams.setUpColor(Color.argb(255, 0, 30, 0));
        //
        // -- However, for this example we'll use a bitmap
        //
        terrainParams.setColorMapBitmap(bmp);
        //
        // -- create the terrain
        //
        terrain = TerrainGenerator.createSquareTerrainFromBitmap(terrainParams, true);
    } catch (Exception e) {
        e.printStackTrace();
    }
    //
    // -- The bitmap won't be used anymore, so get rid of it.
    //
    bmp.recycle();
    //
    // -- A normal map material will give the terrain a bit more detail.
    //
    Material material = new Material();
    material.enableLighting(true);
    material.useVertexColors(true);
    material.setDiffuseMethod(new DiffuseMethod.Lambert());
    try {
        Texture groundTexture = new Texture("ground", R.drawable.ground);
        groundTexture.setInfluence(.5f);
        material.addTexture(groundTexture);
        material.addTexture(new NormalMapTexture("groundNormalMap", R.drawable.groundnor));
        material.setColorInfluence(0);
    } catch (TextureException e) {
        e.printStackTrace();
    }
    //
    // -- Blend the texture with the vertex colors
    //
    material.setColorInfluence(.5f);
    terrain.setY(-100);
    terrain.setMaterial(material);
    getCurrentScene().addChild(terrain);
}
Also used : Bitmap(android.graphics.Bitmap) TextureException(org.rajawali3d.materials.textures.ATexture.TextureException) SquareTerrain(org.rajawali3d.terrain.SquareTerrain) DiffuseMethod(org.rajawali3d.materials.methods.DiffuseMethod) Material(org.rajawali3d.materials.Material) NormalMapTexture(org.rajawali3d.materials.textures.NormalMapTexture) Texture(org.rajawali3d.materials.textures.Texture) NormalMapTexture(org.rajawali3d.materials.textures.NormalMapTexture) TextureException(org.rajawali3d.materials.textures.ATexture.TextureException)

Example 3 with NormalMapTexture

use of org.rajawali3d.materials.textures.NormalMapTexture in project Rajawali by Rajawali.

the class BlockSimpleMaterial method parseBlock.

public void parseBlock(AWDLittleEndianDataInputStream dis, BlockHeader blockHeader) throws Exception {
    // Lookup name
    mLookupName = dis.readVarString();
    // Material type
    mMaterialType = dis.readByte();
    // Shading method count
    mShadingMethodCount = dis.readByte();
    // Read properties
    final AwdProperties properties = dis.readProperties(EXPECTED_PROPS);
    mSpezialType = (Integer) properties.get((short) 4, 0);
    // Spezial type 2 or higher is not supported in the specification
    if (mSpezialType >= 2)
        throw new NotParsableException("Spezial type " + mSpezialType + " is not currently supported.");
    // Debug
    if (RajLog.isDebugEnabled()) {
        RajLog.d("  Lookup Name: " + mLookupName);
        RajLog.d("  Material Type: " + mMaterialType);
        RajLog.d("  Shading Methods: " + mShadingMethodCount);
        RajLog.d("  Spezial Type: " + mSpezialType);
    }
    // Parse the methods
    for (int i = 0; i < mShadingMethodCount; ++i) {
        // TODO Looking at the AWD source, this appears to be completely unused?
        dis.readUnsignedShort();
        dis.readProperties();
        dis.readUserAttributes(null);
    }
    final HashMap<String, Object> attributes = new HashMap<String, Object>();
    dis.readUserAttributes(attributes);
    mMaterial = new Material();
    long diffuseTexture = 0, ambientTexture = 0, diffuseColor = 0;
    // remove any chars that will break shader compile
    String cleanName = cleanName(mLookupName);
    switch(mMaterialType) {
        case TYPE_COLOR:
            // default to 0xcccccc per AWD implementation
            diffuseColor = (Long) properties.get((short) 1, 0xccccccL);
            final float[] colorFloat = new float[4];
            colorFloat[0] = ((diffuseColor >> 16) & 0xff) / 255.0f;
            colorFloat[1] = ((diffuseColor >> 8) & 0xff) / 255.0f;
            colorFloat[2] = (diffuseColor & 0xff) / 255.0f;
            colorFloat[3] = (((int) ((Double) properties.get(PROP_ALPHA, 1.0d) * 0xff)) & 0xff) / 255.0f;
            mMaterial.setColor(colorFloat);
            break;
        case TYPE_TEXTURE:
            diffuseTexture = (Long) properties.get(PROP_TEXTURE, 0L);
            ambientTexture = (Long) properties.get(PROP_AMBIENT_TEXTURE, 0L);
            if (diffuseTexture == 0 && ambientTexture == 0)
                throw new ParsingException("Texture ID can not be 0, document corrupt or unsupported version.");
            if (diffuseTexture > 0)
                mMaterial.addTexture(new Texture(cleanName + diffuseTexture, lookup(blockHeader, diffuseTexture)));
            if (ambientTexture > 0)
                mMaterial.addTexture(new Texture(cleanName + ambientTexture, lookup(blockHeader, ambientTexture)));
            mMaterial.setColorInfluence(0);
            break;
    }
    // either material type can have specular and/or normal maps
    long specularTexture = (Long) properties.get(PROP_SPECULAR_TEXTURE, 0L);
    long normalTexture = (Long) properties.get(PROP_NORMAL_TEXTURE, 0L);
    // either material type can have settings for diffuse, ambient, specular lighting
    double diffuseLevel = (Double) properties.get(PROP_DIFFUSE_LEVEL, 1.0d);
    long ambientColor = (Long) properties.get(PROP_AMBIENT_COLOR, (long) Color.WHITE);
    double ambientLevel = (Double) properties.get(PROP_AMBIENT_LEVEL, 1.0d);
    long specularColor = (Long) properties.get(PROP_SPECULAR_COLOR, (long) Color.WHITE);
    double specularGloss = (Double) properties.get(PROP_SPECULAR_GLOSS, 50.0D);
    double specularLevel = (Double) properties.get(PROP_SPECULAR_LEVEL, 1.0d);
    if (specularTexture > 0)
        mMaterial.addTexture(new SpecularMapTexture(cleanName + specularTexture, lookup(blockHeader, specularTexture)));
    if (normalTexture > 0)
        mMaterial.addTexture(new NormalMapTexture(cleanName + normalTexture, lookup(blockHeader, normalTexture)));
    // ambient 1.0 is default, washes-out object; assume < 1 is intended
    ambientLevel = (ambientLevel < 1.0 ? ambientLevel : 0.0);
    mMaterial.setAmbientIntensity(ambientLevel, ambientLevel, ambientLevel);
    mMaterial.setAmbientColor((int) ambientColor);
    if (// always 1.0 in current AWD implementation
    diffuseLevel > 0)
        mMaterial.setDiffuseMethod(new DiffuseMethod.Lambert());
    if (specularLevel > 0) {
        SpecularMethod.Phong phong = new SpecularMethod.Phong();
        phong.setSpecularColor((int) specularColor);
        phong.setShininess((float) specularGloss);
        phong.setIntensity((float) specularLevel);
        mMaterial.setSpecularMethod(phong);
    }
    // don't enable lighting if specular and diffuse are absent, otherwise enable
    if (diffuseLevel > 0 || specularLevel > 0)
        mMaterial.enableLighting(true);
}
Also used : HashMap(java.util.HashMap) NotParsableException(org.rajawali3d.loader.awd.exceptions.NotParsableException) Material(org.rajawali3d.materials.Material) NormalMapTexture(org.rajawali3d.materials.textures.NormalMapTexture) AwdProperties(org.rajawali3d.loader.LoaderAWD.AwdProperties) SpecularMapTexture(org.rajawali3d.materials.textures.SpecularMapTexture) SpecularMapTexture(org.rajawali3d.materials.textures.SpecularMapTexture) Texture(org.rajawali3d.materials.textures.Texture) NormalMapTexture(org.rajawali3d.materials.textures.NormalMapTexture) SpecularMethod(org.rajawali3d.materials.methods.SpecularMethod) ParsingException(org.rajawali3d.loader.ParsingException)

Aggregations

Material (org.rajawali3d.materials.Material)3 NormalMapTexture (org.rajawali3d.materials.textures.NormalMapTexture)3 Texture (org.rajawali3d.materials.textures.Texture)3 DiffuseMethod (org.rajawali3d.materials.methods.DiffuseMethod)2 TextureException (org.rajawali3d.materials.textures.ATexture.TextureException)2 Bitmap (android.graphics.Bitmap)1 HashMap (java.util.HashMap)1 Object3D (org.rajawali3d.Object3D)1 SplineTranslateAnimation3D (org.rajawali3d.animation.SplineTranslateAnimation3D)1 CatmullRomCurve3D (org.rajawali3d.curves.CatmullRomCurve3D)1 DirectionalLight (org.rajawali3d.lights.DirectionalLight)1 LoaderAWD (org.rajawali3d.loader.LoaderAWD)1 AwdProperties (org.rajawali3d.loader.LoaderAWD.AwdProperties)1 ParsingException (org.rajawali3d.loader.ParsingException)1 NotParsableException (org.rajawali3d.loader.awd.exceptions.NotParsableException)1 SpecularMethod (org.rajawali3d.materials.methods.SpecularMethod)1 SpecularMapTexture (org.rajawali3d.materials.textures.SpecularMapTexture)1 Vector3 (org.rajawali3d.math.vector.Vector3)1 Sphere (org.rajawali3d.primitives.Sphere)1 SquareTerrain (org.rajawali3d.terrain.SquareTerrain)1