您的位置:首页 > 编程语言

JME基础教程代码分析3

2011-08-12 11:24 316 查看
[align=center][/align]

package com.hello;

import com.jme3.app.SimpleApplication;

import com.jme3.font.BitmapText;

import com.jme3.light.DirectionalLight;

import com.jme3.material.Material;

import com.jme3.math.Vector3f;

import com.jme3.scene.Geometry;

import com.jme3.scene.Spatial;

import com.jme3.scene.shape.Box;

/** Sample 3 - how to load an OBJ model, and OgreXML model,

* a material/texture, or text. */

public class HelloAssets extends SimpleApplication {

public static void main(String[] args) {

HelloAssets app = new HelloAssets();

app.start();

}

@Override

public void simpleInitApp() {

//加载了一个茶壶,是个.obj文件

Spatial teapot = assetManager.loadModel("Models/Teapot/Teapot.obj");

Material mat_default = new Material(

assetManager, "Common/MatDefs/Misc/ShowNormals.j3md");

teapot.setMaterial(mat_default);

rootNode.attachChild(teapot);

// Create a wall with a simple texture from test_data

//加载一个box中心点是Vector3f.ZERO

Box box = new Box(Vector3f.ZERO, 2.5f,2.5f,1.0f);

Spatial wall = new Geometry("Box", box );

Material mat_brick = new Material(

assetManager, "Common/MatDefs/Misc/Unshaded.j3md");

//纹理是个.jpg文件,ColorMap是个键

mat_brick.setTexture("ColorMap",

assetManager.loadTexture("Textures/Terrain/BrickWall/BrickWall.jpg"));

wall.setMaterial(mat_brick);

//对位置进行移动,后附加到根节点

wall.setLocalTranslation(2.0f,-2.5f,0.0f);

rootNode.attachChild(wall);

// Display a line of text with a default font

//显示一行默认字体的文字,我试过中文不行。guiNode是用户节点,先剔除所有子节点

guiNode.detachAllChildren();

//加载字体,guiFont用户界面字体

guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");

BitmapText helloText = new BitmapText(guiFont, false);

//设置字号,内容

helloText.setSize(guiFont.getCharSet().getRenderedSize());

helloText.setText("Hello World");

//移动字体位置

helloText.setLocalTranslation(300, helloText.getLineHeight(), 0);

//附加到guiNode节点

guiNode.attachChild(helloText);

// Load a model from test_data (OgreXML + material + texture)

//加载忍者,这是个xml文件

Spatial ninja = assetManager.loadModel("Models/Ninja/Ninja.mesh.xml");

//必要的缩放,旋转和位置移动

ninja.scale(0.05f, 0.05f, 0.05f);

ninja.rotate(0.0f, -3.0f, 0.0f);

ninja.setLocalTranslation(0.0f, -5.0f, -2.0f);

rootNode.attachChild(ninja);

// You must add a light to make the model visible

//必须加载光,让模型可见,DirectionalLight是定向光线的意思,可以看成太阳光

DirectionalLight sun = new DirectionalLight();

sun.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f));

rootNode.addLight(sun);

}

}

注:中级手册中详细介绍了JME可以加载的模型文件、图片、音频等文件的格式

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: