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

Learning-Jme2-tutorial_3

    博客分类:
  • JME2
阅读更多

 

Learning-Jme2-tutorial_3

 

请浏览:http://peigen.info/2010/1/29/Learning-Jme2-tutorial_3/

以后在http://peigen.info/category/JME/更新

 

3) Hello TriMesh

( 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: https://jme.dev.java.net/source/browse/jme/src/jmetest/TutorialGuide/

 

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

 

This program introduces the TriMesh class and how to make your own objects from scratch. We will make a flat rectangle:

 

这个程序介绍TriMesh类,下面来做一个平面矩形.


import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingBox;
import com.jme.math.Vector2f;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.TexCoords;
import com.jme.scene.TriMesh;
import com.jme.util.geom.BufferUtils;
 
/**
 * Started Date: Jul 20, 2004<br><br>
 *
 * Demonstrates making a new TriMesh object from scratch.
 * 
 * @author Jack Lindamood
 */
public class HelloTriMesh extends SimpleGame {
 public static void main(String[] args) {
 HelloTriMesh app = new HelloTriMesh();
 app.setConfigShowMode(ConfigShowMode.AlwaysShow);
 app.start();
 }
 
 protected void simpleInitGame() {
 // TriMesh is what most of what is drawn in jME actually is
 TriMesh m=new TriMesh("My Mesh");
 
 // Vertex positions for the mesh
 Vector3f[] vertexes={
 new Vector3f(0,0,0),
 new Vector3f(1,0,0),
 new Vector3f(0,1,0),
 new Vector3f(1,1,0)
 };
 
 // Normal directions for each vertex position
 Vector3f[] normals={
 new Vector3f(0,0,1),
 new Vector3f(0,0,1),
 new Vector3f(0,0,1),
 new Vector3f(0,0,1)
 };
 
 // Color for each vertex position
 ColorRGBA[] colors={
 new ColorRGBA(1,0,0,1),
 new ColorRGBA(1,0,0,1),
 new ColorRGBA(0,1,0,1),
 new ColorRGBA(0,1,0,1)
 };
 
 // Texture Coordinates for each position
 Vector2f[] texCoords={
 new Vector2f(0,0),
 new Vector2f(1,0),
 new Vector2f(0,1),
 new Vector2f(1,1)
 };
 
 // The indexes of Vertex/Normal/Color/TexCoord sets. Every 3 makes a triangle.
 int[] indexes={
 0,1,2,1,2,3
 };
 
 // Feed the information to the TriMesh
 m.reconstruct(BufferUtils.createFloatBuffer(vertexes), BufferUtils.createFloatBuffer(normals),
 BufferUtils.createFloatBuffer(colors), TexCoords.makeNew(texCoords), BufferUtils.createIntBuffer(indexes));
 
 // Create a bounds
 m.setModelBound(new BoundingBox());
 m.updateModelBound();
 
 // Attach the mesh to my scene graph
 rootNode.attachChild(m);
 
 // Let us see the per vertex colors
 lightState.setEnabled(false);
 }
}
 

The first new thing here is:

// TriMesh is what most of what is drawn in jME actually is
TriMesh m=new TriMesh("My Mesh");
 

WARNING: Do not ever use the constructor new TriMesh(). Always use new TriMesh(“String name”). The empty constructor is for internal use only and will not render correctly. The same goes for Node, Box, Sphere, and anything else that extends com.jme.scene.Spatial;

 

警告:永远不要使用空的TriMesh构造体.而使用带名称的TriMesh构造体.空构造体尽在内部使用,并且不会被正确的渲染.跟Node,Box,Sphere一样,凡是继承自com.jme.scene.Spatial都使用带名字的构造体.

 

If you were to look at the class header for the Box and Sphere class we were using, you’d see

你可以看看Box和Sphere的文件头,都是继承自TriMesh的

public class Box extends TriMesh
 and
public class Sphere extends TriMesh
 

TriMesh is the parent class of both Box and Sphere. They are made from it. You’ll notice that most things actually drawn are from TriMesh.

 

TriMesh是Box和Sphere的父类.

// Vertex positions for the mesh
Vector3f[] vertexes={
 new Vector3f(0,0,0),
 new Vector3f(1,0,0),
 new Vector3f(0,1,0),
 new Vector3f(1,1,0) };
 

This creates positions for the corners of the rectangle we’re about to make. Now let’s give each vertex a normal:

 

设置矩形的一角.

// Normal directions for each vertex position
Vector3f[] normals={
 new Vector3f(0,0,1),
 new Vector3f(0,0,1),
 new Vector3f(0,0,1),
 new Vector3f(0,0,1) };
 

The normal for vertex[0] is normal[0], just like the normal for vertex[i] is normal[i]. Normals are a very common 3D graphics concept. For more information on how normals work with triangles, please visit jME’s math links. Basicly, they point in a direction where the light is its brightest (but not we don’t use lighting at all in this example). After normals, each vertex can have a color:

 

 

// Color for each vertex position
ColorRGBA[] colors={
 new ColorRGBA(1,0,0,1),
 new ColorRGBA(1,0,0,1),
 new ColorRGBA(0,1,0,1),
 new ColorRGBA(0,1,0,1) };
 

In the last program, we assigned a solid blue color to every vertex of our box (ColorRGBA.blue which is the equivalent of new ColorRGBA(0,0,1,1)). In this program, we give the first two vertexes the color red, and the last two green. You’ll notice the first two vertex positions are vertexes[0]=(0,0,0) and vertexes[1]=(1,0,0) which are the lower part of our rectangle and the last two are vertexes[2]=(0,1,0) and vertexes[3]=(1,1,0) which are the upper parts. Looking at the picture you can see how the rectangle does a smooth transition from red to green from bottom to top. Next, we assign texture coordinates:

// Texture Coordinates for each position
Vector2f[] texCoords={
 new Vector2f(0,0),
 new Vector2f(1,0),
 new Vector2f(0,1),
 new Vector2f(1,1) };
 

If you’ve worked with any 3D graphics before, the concept of texture coordinates is the same in jME as it is anywhere. Vector2f is just like a Vector3f, but it has 2 floats (notice the 2f in Vector2f) instead of 3 floats (notice the 3f in Vector3f). These two floats are x, y. I’ll go into texturing later, but for now I throw this in just so you can get the pattern of how constructing a TriMesh works. Finally, I have to make indexes for my TriMesh:

// The indexes of Vertex/Normal/Color/TexCoord sets. Every 3
// makes a triangle.
int[] indexes={
 0,1,2,1,2,3
 };
 

TriMesh means Triangle mesh. It is a collection of triangles. Your indexes array must always be a length of modulus 3 (3,6,9,12,15,etc). That’s because triangles always have 3 coordinates. Notice this is made up of two triangles. If I had {0,1,2,1,2,3,2,3,0}, that would be 3 triangles. Lets look at the first set of 0,1,2. This means that my TriMesh object’s first triangle is drawn by connecting vertexes [0] → vertexes [1] → vertexes [2]. Vertex [0] has a normal of normals [0], a color of colors [0], a texture coordinate of texCoords [0]. The next triangle is drawn from vertexes [1] → vertexes [2] → vertexes [3]. Note that it would be illegal to have the following:

int[] indexes={
 0,1,2,1,2,4
};
 

This is illegal because there are no vertexes[4]. After making all our data, we finally feed it into the TriMesh, then attach it after creating a bounds

// Feed the information to the TriMesh
m.reconstruct(BufferUtils.createFloatBuffer(vertexes),BufferUtils.createFloatBuffer(normals),
	BufferUtils.createFloatBuffer(colors),BufferUtils.createFloatBuffer(texCoords),
	BufferUtils.createIntBuffer(indexes));
// Create a bounds
m.setModelBound(new BoundingBox());
m.updateModelBound();
// Attach the mesh to my scene graph
rootNode.attachChild(m); // Let us see the per vertex colors
lightState.setEnabled(false);
 

The last line will make more sense after you’ve completed a few more chapters. For now, just realize that it turns on the per-vertex colors we need to see the rainbow effect on the box.

Note that I don’t use the texCoords really in this example (They only have meaning when you use a picture on your object). I could have used the following:

m.reconstruct(BufferUtils.createFloatBuffer(vertexes),BufferUtils.createFloatBuffer(normals),
	BufferUtils.createFloatBuffer(colors),null,BufferUtils.createIntBuffer(indexes));
 

I could even have done the following:

m.reconstruct(BufferUtils.createFloatBuffer(vertexes),null,null,null,BufferUtils.createIntBuffer(indexes));
 

Then I could have drawn a grey, boring TriMesh. Here is a picture of the scene graph:

rootNode
"My Mesh"

 

分享到:
评论

相关推荐

    oracle-jmesdk-8-0-rr-nb-plugins_Plugins_jmesdk_zip_

    Oracle JME SDK (Java Micro Edition Software Development Kit) 是Oracle公司为开发Java ME应用程序提供的一套工具集。这个压缩包"oracle-jmesdk-8-0-rr-nb-plugins.zip"包含了用于NetBeans集成开发环境(IDE)的插件...

    ant-jme.jar.zip

    JME,以前被称为Java 2 Micro Edition,是Java平台的一个子集,用于嵌入式设备和移动设备,如智能手机和小型家电。ant-jme.jar可能包含了JME开发所需的特定任务或类库,使得开发者能够在这些资源有限的平台上使用Ant...

    invoke-a-CGI.zip_cgi

    2. MIDlet:MIDlet是JME的一部分,它是一套针对资源有限的设备(如手机)的Java应用程序。MIDlet可以在这些设备上运行,提供类似于桌面应用的功能,如游戏、电子邮件客户端等。MIDP(Mobile Information Device ...

    联想的JME2207P键盘驱动

    标题中的“联想的JME2207P键盘驱动”是指专门为联想品牌的一款键盘型号为JME2207P的设备设计的驱动程序。在计算机硬件系统中,驱动程序是连接操作系统与硬件设备的关键软件,它使得操作系统能够识别并控制特定硬件,...

    real-java-1.13.rar_GPL calculator_floating

    在移动设备领域,尤其是在Java Micro Edition (JME) 或者Midp平台开发中,由于资源限制,实现高效的浮点计算往往是一项挑战。"Real Java 1.13.rar" 提供了一个针对MIDP设备的开源解决方案,它是一个专为这类设备优化...

    2.4寸TFT资料(SO44PIN)

    代码测试环境:JME-2核心板+1T指令周期的STC单片机(51内核 STC12LE5A60S2)+33M晶振 单片机工作电压3.3V 程序默认IO连接方式: 控制线:RS-P3^5; WR-P3^6; RD-P3^7; CS-P1^0; REST-P1^2; 数据线: DB0-DB7依次连接P0^0...

    联想FN功能键 jme2207p键盘驱动. XP windows7

    标题中的“联想FN功能键 jme2207p键盘驱动. XP windows7”表明这是一个针对联想笔记本电脑的FN功能键以及JME2207P型号键盘的驱动程序,适用于Windows XP和Windows 7操作系统。FN键是许多笔记本电脑上常见的辅助功能...

    jme.rar_JME_RPG

    RPG手机游戏开发代码,是一个完整的开发代码

    联想 键盘功能键 驱动 7155P 等

    联想键盘 LXB JME 7155P 驱动 功能键有效亲测 有音量调节键 邮件 关机 网页 各种功能键全部有效

    java8看不到源码-JME3-JFX:用于JME的JFXGui桥接器,具有用于常见用例的有用实用程序

    jme-3.0.10 兼容): 版本:1.+ 分支:jme_3.0 JME3-JFX 需要 java 8,但是可以使用 java 7 在纹理上绘制视频。对于 jME SDK,您应该创建一个 java 8 平台,但是 java8 支持非常糟糕(因为它基于 netbeans 7)。 ...

    JME中文教程.pdf

    - **jme3-lwjgl/jme3-lwjgl3/jme3-jogl**:为桌面应用提供不同的渲染模块。 - **jme3-android**/**jme3-android-native**:分别针对Android应用的核心API和所需本地库文件。 - **jme3-ios**:开发iOS应用的核心API...

    jme3 api(精华chm)

    com.jme3.animation com.jme3.app com.jme3.app.state com.jme3.asset com.jme3.asset.pack com.jme3.asset.plugins com.jme3.audio com.jme3.audio.joal ...jme3tools.preview

    flash-cards-jme

    "flash-cards-jme" 是一个基于 JavaScript 开发的项目,其主要目的是创建一款角形设计的闪存卡片工具,用于学习和记忆各种知识点。在第一天的开发阶段,这个项目可能涉及了基础的前端开发技术和JavaScript语言的核心...

    JME3学习文档

    ### JME3游戏开发引擎中文学习指南 #### 引言 JME3,全称jMonkeyEngine3,是一款开源的3D游戏开发引擎,专为Java开发者设计,旨在简化3D游戏和应用程序的开发过程。本文档将详细介绍如何在Netbeans6.x环境下搭建...

    TankBattle3D-jMEGame:塔防游戏。 SGGames。 由JME3提供支持。 libgdx-ai-jme的示例

    本项目"TankBattle3D-jMEGame"正是这样一个以Java为基础,结合了JMonkey Engine 3 (JME3) 和 Libgdx-ai 技术的3D塔防游戏。本文将深入探讨该项目中所应用的关键技术和设计思路。 首先,JMonkey Engine 3(简称JME3...

    jme3游戏demo rise-of-mutants

    《jme3游戏开发:Rise of Mutants》 在IT行业中,游戏开发是一个充满创新和技术挑战的领域,而Java语言并非通常首选的游戏开发工具。然而,随着技术的进步,Java也逐渐进入了游戏开发的舞台,其中JMonkeyEngine...

    java弹球游戏源码-GSoC_2020:GSoC_2020

    jME3 的一般熟悉以及中高级 Java 知识。 迷你简单游戏 项目介绍 Minie 是 JMonkeyEngine 的新物理附加组件: Minie 包括一些教程和一组简单的测试/演示应用程序,但到目前为止还没有示例游戏。 JMonkeyEngine 同样...

    jme5.5.zip

    标题 "jme5.5.zip" 提供的信息表明这是一个与Java Media Engine(JME)相关的压缩文件,版本为5.5。Java Media Engine是Java平台上的一个组件,用于处理多媒体内容,如音频、视频和图像。它允许开发者在Java应用程序...

Global site tag (gtag.js) - Google Analytics