`
peigen
  • 浏览: 76158 次
  • 性别: 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"

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics