Tutorial Java Monkey Engine-Load model with skybox-Part 5

setelah beberapa hari ini coba2 yang namanya JME ternyata mudah gan…, dulu sempat ingin jauhin yang namanya JME karena kata temanku codingnya susah itu yang hampir menyebabkan mainset otakku bilang JME sulit. ternyata setelah dicoba ga gitu2 amat..ok sekarang kita akan meload model dengan skybox…apa yang dimaksud skybox akan terlihat disini..:D

package MY_TUTORIAL_USING_JME_3_2;

import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingBox;
import com.jme.image.Image;
import com.jme.image.Texture;
import com.jme.math.FastMath;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.scene.Skybox;
import com.jme.scene.Spatial;
import com.jme.scene.Spatial.TextureCombineMode;
import com.jme.scene.shape.Box;
import com.jme.scene.state.CullState;
import com.jme.scene.state.FogState;
import com.jme.scene.state.TextureState;
import com.jme.util.TextureManager;
import com.jme.util.export.binary.BinaryImporter;
import com.jmex.model.converters.MaxToJme;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.net.URL;
import java.util.logging.Logger;


/**
 *
 * @author me
 */
public class Load3DandSkyBox extends SimpleGame{
    private static final Logger logger = Logger.getLogger(Load3DandSkyBox.class.getName());
    Node node;
    Skybox sky;//load sky 

    @Override
    protected void simpleInitGame() {
        display.setTitle("Load 3D and Terrain");
        lahan(); //load lahan 
        lightState.setEnabled(true);
        Catherdal(); // the rusian building at red square
        Mosque();
        WallSkybox();
        //HighBuilding();
    }
    public void lahan(){
        //create box
        Box bidang = new Box("lantai", new Vector3f(), 200, 1, 200);
        bidang.setModelBound(new BoundingBox());
        bidang.updateModelBound();
        bidang.getLocalTranslation().y = -20;
        TextureState ts = display.getRenderer().createTextureState();
        ts.setEnabled(true);
        ts.setTexture(TextureManager.loadTexture(
                Load3DandSkyBox.class.getClassLoader().getResource(
                        "MY_TUTORIAL_USING_JME_3_2/road.jpg"), Texture.MinificationFilter.Trilinear,
                Texture.MagnificationFilter.Bilinear));
        bidang.setRenderState(ts);
        rootNode.attachChild(bidang);
    }
    public void Catherdal(){
        node = null;
        try{
            //call Max file on JMEs
            MaxToJme model = new MaxToJme();
            ByteArrayOutputStream BO = new ByteArrayOutputStream();
            //model location loaded
            URL maxFile = Load3DandSkyBox.class.getClassLoader().getResource("MY_TUTORIAL_USING_JME_3_2/75Cathedral-model.3DS");
            model.convert(new BufferedInputStream(maxFile.openStream()),BO);
            node = (Node)BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));
            Quaternion temp = new Quaternion();
            //rotating model to stand
            temp.fromAngleAxis(FastMath.PI / 2, new Vector3f(-1, 0, 0));
            //location model
            node.setLocalTranslation(new Vector3f(2.0f,-15,80));
            node.setLocalRotation(temp);
            node.setLocalScale(.1f);
            node.setModelBound(new BoundingBox());
            node.updateModelBound();
        }catch(Exception e){
            logger.throwing(this.getClass().toString(), "Catherdal()",e);
        }
        node.updateRenderState();
        rootNode.attachChild(node);
    }
    public void Mosque(){
        node = null;
        try{
            //call Max file on JMEs
            MaxToJme model = new MaxToJme();
            ByteArrayOutputStream BO = new ByteArrayOutputStream();
            //model location loaded
            URL maxFile = Load3DandSkyBox.class.getClassLoader().getResource("MY_TUTORIAL_USING_JME_3_2/84mosque-ciro.3DS");
            model.convert(new BufferedInputStream(maxFile.openStream()),BO);
            node = (Node)BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));
            Quaternion temp = new Quaternion();
            //rotating model to stand
            temp.fromAngleAxis(FastMath.PI / 2, new Vector3f(-1, 0, 0));
            //location model
            node.setLocalTranslation(new Vector3f(50.0f,-18,0));
            node.setLocalRotation(temp);
            node.setLocalScale(.1f);
            node.setModelBound(new BoundingBox());
            node.updateModelBound();
        }catch(Exception e){
            logger.throwing(this.getClass().toString(), "Mosque()",e);
        }
        node.updateRenderState();
        rootNode.attachChild(node);
    }

    public void WallSkybox(){
        sky = new Skybox("skydome", 250, 250, 250);
        //load texture for north,south,east,west,up skybox
        Texture north = TextureManager.loadTexture(Load3DandSkyBox.class.getClassLoader().getResource("MY_TUTORIAL_USING_JME_3_2/north.jpg"),
        Texture.MinificationFilter.BilinearNearestMipMap,Texture.MagnificationFilter.Bilinear);
        Texture east = TextureManager.loadTexture(Load3DandSkyBox.class.getClassLoader().getResource("MY_TUTORIAL_USING_JME_3_2/east.jpg"),
        Texture.MinificationFilter.BilinearNearestMipMap,Texture.MagnificationFilter.Bilinear);
        Texture south = TextureManager.loadTexture(Load3DandSkyBox.class.getClassLoader().getResource("MY_TUTORIAL_USING_JME_3_2/south.jpg"),
        Texture.MinificationFilter.BilinearNearestMipMap,Texture.MagnificationFilter.Bilinear);        
        Texture up = TextureManager.loadTexture(Load3DandSkyBox.class.getClassLoader().getResource("MY_TUTORIAL_USING_JME_3_2/up.jpg"),
        Texture.MinificationFilter.BilinearNearestMipMap,Texture.MagnificationFilter.Bilinear);        
        Texture west = TextureManager.loadTexture(Load3DandSkyBox.class.getClassLoader().getResource("MY_TUTORIAL_USING_JME_3_2/west.jpg"),
        Texture.MinificationFilter.BilinearNearestMipMap,Texture.MagnificationFilter.Bilinear);
        Texture bottom = TextureManager.loadTexture(Load3DandSkyBox.class.getClassLoader().getResource("MY_TUTORIAL_USING_JME_3_2/bottom.jpg"),
        Texture.MinificationFilter.BilinearNearestMipMap,Texture.MagnificationFilter.Bilinear);

        //setting texture
        sky.setTexture(Skybox.Face.East, east);
        sky.setTexture(Skybox.Face.North, north);
        sky.setTexture(Skybox.Face.West, west);
        sky.setTexture(Skybox.Face.Up, up);
        sky.setTexture(Skybox.Face.South, south);
        sky.setTexture(Skybox.Face.Down, bottom);

        sky.preloadTextures();
        sky.updateRenderState();

        rootNode.attachChild(sky);
        
    }
    public static void main(String[] args){
        Load3DandSkyBox s = new Load3DandSkyBox();
        s.setConfigShowMode(ConfigShowMode.AlwaysShow);
        s.start();
    }
}

setelah dicompile hasilnya begini:

anda lihat gambar diatas..mungkin anda akan paham dengan apa yang dimaksud skybox class. intinya membuat kotak kemudian kotak itu kita

render bagian dalamnya dengan image awan atau pegunungan juga bisa…ya ini diskripsi saya jika salah mohon maaf…ok kita lanjut

// give you transparent skybox
        CullState cs = display.getRenderer().createCullState();
        cs.setCullFace(CullState.Face.None);
        cs.setEnabled(true);
        sky.setRenderState(cs);

dengan menambahkan cullstate class diatas kita akan mendapatkan sebuah effect seperti gambar dibawah ini:

lihat jika kita keluar dari skybox akan timbul effect seperti gambar diatas…hasil akhir dari code2 diatas seperti gambar dibawah ini:

tutorial ini saya tulis juga sebagai arsip saya karena jika saya tulis di file doc 100% data pasti hilang karena jumlah file di harddisk saya terlalu banyak..:D..jadi semoga bermanfaat…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s