您的位置:首页 > 其它

jMonkeyEngine译文 FlagRush1——通过SimpleGame创建你的第一个应用程序

2010-10-18 19:04 501 查看

今天在实验室忙了一下,趁着有空整理了jME的第一篇翻译,献给想在Java3D方面发展的人。


注:本系列教程全部翻译完之后可能会以PDF的形式发布。
如果有什么错误可以留言或EMAIL:kakashi9bi@gmail.com给我。

jME版本 :jME_2.0.1_Stable
开发工具:MyEclipse8.5
操作系统:Window7/Vista

1、 通过SimpleGame创建你的第一个应用程序

1.1、SimpleGame介绍

SimpleGame是包含在jME包中默认的应用程序类型。SimpleGame尝试为你关心所有的事。这让它很容易构建起原型并运行。它设置了所有的元素,诸如:Camera、InputHandle、基础RenderState等等。首先,我将运行一个由SimpleGame创建的简单的应用程序,它向屏幕绘制了一个球体(下文使用Sphere代替),接着我们将创建自己的应用程序类型,让你更好了解它和控制它。

首先,我们将创建一个新的类继承自SimpleGame。在我的例子中,我创建类Lesson1:

public class Lesson1 extends SimpleGame {}

SimpleGame包含一个抽象方法simpleInitGame。我们正是在这个方法中创建了Sphere。现在先增加这个方法,我们将在之后回来讨论Sphere。首先,我们想讨论main方法。这是jME应用程序的入口点(就像其他Java应用程序)。在创建期间,你需要创建你的应用程序并告诉它们开始执行游戏循环。主游戏循环执行update/render循环,直到被告知退出和清理。为了开始这个循环需要调用start。

为了允许使用者指定窗口参数(分辨率,全屏等等),我们将一直显示PropertiesDialog。为了做到这点,我们设置应用程序行为为ConfigShowMode.AlwaysShow。

import com.jme.app.SimpleGame;
public class Lesson1 extends SimpleGame {
@Override
protected void simpleInitGame() {
}
public static void main(String[] args) {
Lesson1 app = new Lesson1();
app.setConfigShowMode(ConfigShowMode.AlwaysShow);
app.start();
}
}

上面代码能真正被编译和运行,创建一个空的窗口。

1.2、显示一些东西

现在,我们想为simpleInitGame增加一些东西去显示带纹理(下文使用Texture代替)的Sphere。为了这么做,我们需要:
l 加载Sphere
l 加载图像
l 将图像应用到Sphere做为它的Texture
l 增加Sphere到场景(下文使用scene代替)
Sphere的创建和创建一个新的对象一样简单。

Sphere s = new Sphere("Sphere",30,30,25);
s.setLocalTranslation(new Vector3f(0,0,-40));
s.setModelBound(new BoundingBox());
s.updateModelBound();

你定义了Sphere垂直和水平位置的截面数目(在这个例子中都为30)和它的半径(25)。就那样,现在我们有了Sphere。我们能接着操纵Sphere的位置。在这个例子中,我们想让它沿着Z轴负向移动(这和把它移向屏幕里面等价)。我们接着设置了Sphere的边界体积(下文使用BoundingVolume代替)。这允许摄像机(下文使用Camera代替)的视锥剔除(下文使用Frustum Culling)工作。这意味着,如果我们将Camera偏移Sphere,它将不会被绘制(而统计掉为0)。

下一步,我们将加载Monkey.jpg图像并把它做为Texture应用到Sphere。为了加载图像并获取Texture,我们使用TextureManager和它的loadTexture方法。我们将通过基础Texture的值加载图像。

Texture texture = TextureManager.loadTexture(
Lesson1.class.getClassLoader().getResource(
"jmetest/data/images/Monkey.jpg"),
Texture.MinificationFilter.Trilinear,
Texture.MagnificationFilter.Bilinear
);

我们接着创建了TextureState并把这个texture设置给它。为了创建TextureState,我们使用以工厂方法的形式使用DisplaySystem。SimpleGame已经有了一个指向当前DisplaySystem的实例:“display”。

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

这样设置,如果允许在渲染(下文使用Render代替)期间使用渲染状态(下文使用Render State代替):

ts.setEnabled(true);

我们接着使用setTexture方法将Monkey.jpg图像的texture放入TextureState中。TextureState现在已经准备好被应用到Sphere中,调用setRenderState来这么做。现在这个TextureState被附加(下文使用attach代替)到Sphere,不管Sphere在什么时候被render,它将使用它的纹理坐标(下文使用texture coordinate)并将图像应用到自身。

ts.setTexture(texture);
s.setRenderState(ts);

最后,我们将Sphere attach到scene。SimpleGame提供一个对象叫rootNode,用于描述主场景(下文使用main scene代替),将sphere attach到这个结点(下文使用Node代替),为它的render做好准备。为了attach它,简单加入:

rootNode.attachChild(s);

通过这些简单的调用,我们已经有了一个带texture的sphere,它被render到屏幕上。








SimpleGame,正如它名字所暗示的,让一切简单。然而,为了创建一个非常成熟的游戏,我们想要完全控制一切。下一章将通过创建我们自己的游戏类型展示怎样做到这样。

源码:
import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingBox;
import com.jme.bounding.BoundingSphere;
import com.jme.image.Texture;
import com.jme.math.Vector3f;
import com.jme.scene.shape.Sphere;
import com.jme.scene.state.TextureState;
import com.jme.util.TextureManager;

public class Lesson1 extends SimpleGame {

@Override
protected void simpleInitGame() {
Sphere s = new Sphere("Sphere",30,30,25);
s.setLocalTranslation(new Vector3f(0,0,-40));
s.setModelBound(new BoundingBox());
s.updateModelBound();

Texture texture = TextureManager.loadTexture(
Lesson1.class.getClassLoader().getResource(
"jmetest/data/images/Monkey.jpg"),
Texture.MinificationFilter.Trilinear,
Texture.MagnificationFilter.Bilinear
);

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

ts.setTexture(texture);
s.setRenderState(ts);

rootNode.attachChild(s);
}

public static void main(String[] args) {
Lesson1 app = new Lesson1();
app.setConfigShowMode(ConfigShowMode.AlwaysShow);
app.start();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: