`
peigen
  • 浏览: 76149 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Learning_JME2-Tutorial_1

    博客分类:
  • JME2
阅读更多

Learning_JME2-Tutorial_1

 

请浏览:http://peigen.info/2010/1/29/Learning-Jme2-tutorial_1/
以后在http://peigen.info/category/JME/更新

1) Hello World

( Note: the starter tutorials in this wiki are not always up to date with the latest development version of jME. You can find up to date source files for the tutorials here: http://code.google.com/p/jmonkeyengine/source/browse/#svn/trunk/src/jmetest/TutorialGuide )

Here we’ll learn the basics of creating a jME program, by exploring SimpleGame , Box , and rootNode. OK, let’s just dive in. Here’s as basic a program as you can get:

 

(注解:这份指南在wiki上并不一定是跟最新的jME开发版本一样.你可以在http://code.google.com/p/jmonkeyengine/source/browse/#svn/trunk/src/jmetest/TutorialGuide 找到最新的源码文件)

这里我们将通过探索SimpleGame,Box,rootNode学习基本的创建一个jME程序.OK,开始了,下面是一个简单的程序:


import com.jme.app.SimpleGame;
import com.jme.scene.shape.Box;
import com.jme.math.Vector3f;
/**
 * Started Date: Jul 20, 2004<br><br>
 * Simple HelloWorld program for jME
 *
 * @author Jack Lindamood
 */
public class HelloWorld extends SimpleGame{
 public static void main(String[] args) {
 HelloWorld app = new HelloWorld(); // Create Object
 // Signal to show properties dialog
 app.setConfigShowMode(ConfigShowMode.AlwaysShow);
 app.start(); // Start the program
 }
 protected void simpleInitGame() {
 // Make a box
 Box b = new Box("Mybox", new Vector3f(0,0,0), new Vector3f(1,1,1));
 rootNode.attachChild(b); // Put it in the scene graph
 }
}
 

Pretty short, right? The real meat of our program begins with the following:

 

是不是很精炼?我们的程序从这里开始:

public class HelloWorld extends SimpleGame{
 

SimpleGame does a lot of initialization for us behind our back. If you really want to, you can look at its code, but for now just understand that it creates all the basics needed for rendering. It's a great class to start with for prototyping and testing.

 

SimpleGame在后面帮我们干了很多初始化工作.如果你想细致的了解,请看源码,现在只需要了解它创建了很多基础的需要渲染的东西.这是个很好的原型和测试的类.

app.setConfigShowMode(ConfigShowMode.AlwaysShow);
 

You know the picture of a monkey you see when the program is first run, the one that lets you select the resolution?

 

就像你看到的那样,程序一开始有一个猴子,下面有让我们选择分辨率的选项

 

Well, this command makes it appear. As the name suggests, it always shows the properties dialog on every run. You’ll never see that dialog box if you change it to the following:

 

是 app.setConfigShowMode ( ConfigShowMode.AlwaysShow ) ;让它显示出来的,就像它的名字一样每当运行的时候配置框都会出来,如果你不想看到的话写成 ConfigShowMode.NeverShow即可


ConfigShowMode.NeverShow
 

Not too difficult.

也不太困难嘛.

app.start(); // Start the program启动程序
 

The function start() is a while loop. First, it initializes the jME system. Then, the while loop does two things per iteration: first, it tells everything in your game that it needs to move, and second, it renders everything. Basically, it gets the game going.

 

start()是循环执行的.首先,初始化jME系统.然后,每次循环都重复两件事情:第一,它告诉所有程序哪些要移动,第二,它负责渲染.基本上,是它让程序运行的.

protected void simpleInitGame() {
 // Make a box
 Box b = new Box("Mybox",
 new Vector3f(0,0,0),
 new Vector3f(1,1,1));
 rootNode.attachChild(b); // Put it in the scene graph
}
  

The function simpleInitGame() is abstract in SimpleGame , so you’re forced to implement it every time you extend SimpleGame . Looking at the code we can see that two things happen. First, I make a box (it’s the thing you saw on the screen). Second, I attach the box to the root of my scene graph. The object rootNode is of class Node which is created by SimpleGame for you. You’ll attach everything to it or one of its children. Notice I gave b 3 parameters: a string and two Vector3f objects. Every Node , Box , Circle, Person or anything in your scene graph will have a name. Usually you want the name to be specific for each object. I called this one “My box”, but really you could have called it anything. The next two parameters specify the corners of the Box. It has one corner at the origin, and another at x=1, y=1, z=1. Basically, it’s a unit cube.

 

simpleInitGame() 方法是SimpleGame的抽象方法,所有你每次继承SimpleGame的时候都必须重载它.通过代码我们可以看到,两件事发生,第一,我创建了一个 Box(就是你在屏幕上看到的那个东西).第二,我把box附给了我的场景图.对象rootNode是Node类的实例是从SimpleGame里面创建 的.你可以把所有的东西(组件)都附给rootNode或者其他子节点.注意我给了b(Box)3个参数:一个String和两个Vector3f对象. 每个Node,Box,Circle,Person或者任何在你场景里面的东西都必须有一个名字.通常你需要为每个对象命名专用名字.就像"My box"一样,当然你可以随意给它起名子.另外两个参数指定了Box的两个角.从一个角开始(new Vector3f( 0 ,0 ,0 ) ),到另一个角(new Vector3f( 1 ,1 ,1 ) ),在x=1,y=1,z=1的地方,这就是一个方块单位.

 

OK, I’ve created the Box, but I have to tell it I want it rendered, too. That’s why I attach it to the rootNode object. Your scene graph basically looks like this:

 

OK,我们已经创建了一个Box了,我必须告诉程序我们要渲染它.这就是我为什么把它附给rootNode对象.你的场景看起来像这样(Mybox在rootNode下):

rootNode
My box

 

The object rootNode is at the top and “My box” is below it. So, when SimpleGame tries to draw rootNode it will try to draw “My box” as well. That’s it! Now, on to something more complex.

2
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics