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

M3G教程:进阶篇(一)金字塔

    博客分类:
  • J2ME
阅读更多

 

关于World

public class World extend Group

 

A special Group node that is a top-level container for scene graphs.

A scene graph is constructed from a hierarchy of nodes. In a complete scene graph, all nodes are ultimately connected to each other via a common root, which is a World node. An example of a complete scene graph is shown in the figure below.

 

Note that a scene graph need not be complete in order to be rendered; individual nodes and branches can be rendered using a separate method in Graphics3D. However, the semantics of rendering an incomplete scene graph are slightly different compared to rendering a World; see Graphics3D for more information

 

Despite that it is called a graph, the scene graph is actually a tree structure. This implies that a node can belong to at most one group at a time, and cycles are prohibited. However, component objects, such as VertexArrays, may be referenced by an arbitrary number of nodes and components. The basic rules for building valid scene graphs are summarized below.

 

Even though World is a scene graph node, its special role as the singular root node has two noteworthy consequences. Firstly, a World can not be a child of any Node. Secondly, the node transformation is ignored for World objects when rendering. In all other respects (get, set, animate), the transformation behaves just like any other node transformation. Note also that there is no conceptual "Universe" coordinate system above the World, contrary to some other scene graph APIs.

 

The method render(World) in Graphics3D renders a World as observed by the currently active camera of that world. If the active camera is null, or the camera is not in the world, an exception is thrown. The world can still be rendered with the render(Node,Transform) method by treating the World as a Group. In that case, however, the application must explicitly clear the background and set up the camera and lights prior to rendering.

 

关于Camera

setPerspective (float fovy, float aspectRatio, float near, float far)

其中
fovy:代表在y轴上的视野,它的正常值时在(0,90)度之间的范围内。
注意: 因为摄像机是面向负Z轴 (0,0,-1), 所以它这个范围表示在竖直方向可视范围在这个读数之间.( h = tan(fovy/2))
aspectRatio:屏幕高宽比,这是一个相当简单的参数,它是一个分数,告诉引擎当前屏幕的宽和高的关系。大多数计算机屏幕的比例是4:3(也就是高是宽的0.75倍),然而正常的移动电话屏幕有很多种不同的比例。要得到这个变量的值,你需要做的就是用高除当前屏幕的宽

near、far-:近截面和远截面,定义多近/多远的一个对象依然可以被渲染。那么例如,设置近截面为0.1和远截面为50,意味着所有距离照相机小于0.1单位的对象将不会被渲染。同样所有距离照相机大于50单位的对象也不会被渲染。

 

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.m3g.Appearance;
import javax.microedition.m3g.Camera;
import javax.microedition.m3g.Graphics3D;
import javax.microedition.m3g.IndexBuffer;
import javax.microedition.m3g.Mesh;
import javax.microedition.m3g.PolygonMode;
import javax.microedition.m3g.TriangleStripArray;
import javax.microedition.m3g.VertexArray;
import javax.microedition.m3g.VertexBuffer;
import javax.microedition.m3g.World;

public class M3GCanvas extends GameCanvas implements Runnable {
	
	public static final int FPS = 20;	//每秒绘制的帧数

	private Graphics3D g3d;
	private World world;
	private boolean runnable=true;
	private Thread thread;
	private Camera camera; // the camera in the scene
    private Mesh pyramidMesh; // the pyramid in the scene

	protected M3GCanvas() {
		super(false);
		setFullScreenMode(true);
		g3d = Graphics3D.getInstance();
		world = new World();
		
		camera = new Camera();
        world.addChild(camera); // add the camera to the world.

        float w = getWidth();
        float h = getHeight();

        // Constructs a perspective projection matrix and sets that as the current projection matrix.
        //setPerspective (float fovy, float aspectRatio, float near, float far)
        camera.setPerspective(60.0f, w / h, 0.1f, 50f);
        
        pyramidMesh = createpyramid(); // create our pyramid.
        
        //将对象沿Z轴移动-4个单位
        pyramidMesh.setTranslation(0.0f, 0.0f, -4.0f);
        world.addChild(pyramidMesh); // add the pyramid to the world

        //Sets the Camera to use when rendering this World. 
        world.setActiveCamera(camera);
	}

	public void run() {
		Graphics g = getGraphics();
		while (runnable) {
			long startTime = System.currentTimeMillis();
			
			// rotate the pyramid 3 degree around the Y-axis.
			//postRotate(float angle,float ax,float ay,float az)
            pyramidMesh.postRotate(3.0f, 0.0f, 1.0f, 0.0f);
            
			try {
				g3d.bindTarget(g);
				g3d.render(world);
			} finally {
				g3d.releaseTarget();
			}
			flushGraphics();
			
			long endTime = System.currentTimeMillis();
            long costTime = endTime - startTime;
            if(costTime<1000/FPS)
            {
                try{
                  Thread.sleep(1000/FPS-costTime);
                }
                catch(Exception e){
                   e.printStackTrace();
                }
            }
		}
		System.out.println("Canvas stopped");

	}
	
	public void start()
	{
		thread=new Thread(this);
		thread.start();
	}
	
	public void stop()
	{
		this.runnable=false;
		try {
			thread.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 创建金字塔
	 * @return
	 */
    private Mesh createpyramid(){
        
        /**
         * 组成金字塔的五个顶点
         */
        short[] points = new short[]{
        		-1, -1,  1,		//左前
                 1, -1,  1,		//右前
                 1, -1, -1,		//右后
                -1, -1, -1,		//左后
                 0,  1,  0		//顶
        };
                                                            
                                                            
        /**
         * 点索引,使用逆时针指定五个面,底面是一个四边形,分解成两个三角形
         */
        int[] indices = new int[]{
        		0, 1, 4,		//前
        		1, 2, 4,		//右
        		2, 3, 4,		//背
        		3, 0, 4,		//左
        		2, 1, 0,		//底1
        		2, 0, 3			//底2
        };
        /**
         * 各个顶点颜色
         */
        byte[] colors = new byte[]{
        		-1,  0,  0,   	//红色
                 0, -1,  0,   	//绿色
                 0,  0, -1,   	//蓝色
                -1,  0, -1,   	//紫色
                -1, -1,  0    	//黄色
        };
                                                        

        // The length of each sequence in the indices array.
        int[] length = new int[]{3, 3, 3, 3, 3, 3}; // the pyramid is built by six triangles
        
        VertexArray vertexArray, colorArray;
        IndexBuffer indexBuffer;
        
        //准备顶点数组用于创建顶点缓存,short型为2个字节
        //VertexArray(int numVertices, int numComponents, int componentSize)
        vertexArray = new VertexArray(points.length/3, 3, 2);
        //复制数据
        //set(int firstVertex, int numVertices, short[] values) 
        vertexArray.set(0, points.length/3, points);
        
        //同样操作颜色数据,byte为1个字节
        colorArray = new VertexArray(colors.length/3, 3, 1);
        colorArray.set(0, colors.length / 3, colors);
        indexBuffer = new TriangleStripArray(indices, length);
        
        // VertexBuffer holds references to VertexArrays that contain the positions, colors, normals, 
        // and texture coordinates for a set of vertices
        VertexBuffer vertexBuffer = new VertexBuffer();
        vertexBuffer.setPositions(vertexArray, 1.0f, null);
        vertexBuffer.setColors(colorArray);
        
        // Create the 3D object defined as a polygonal surface
        //Constructs a new Mesh consisting of only one submesh. 
        //Mesh(VertexBuffer vertices, IndexBuffer submesh, Appearance appearance) 
        Mesh mesh = new Mesh(vertexBuffer, indexBuffer, null);
        
        //Appearance用于定义Mesh或者Sprite3D的渲染属性,由基于组件对象组成
        //每个组件对象又由一系列相互关联的属性组成
        Appearance appearance = new Appearance(); // A set of component objects that define the rendering attributes of a Mesh
        PolygonMode polygonMode = new PolygonMode(); // An Appearance component encapsulating polygon-level attributes
        polygonMode.setPerspectiveCorrectionEnable(true);
        polygonMode.setWinding(PolygonMode.WINDING_CCW);//设置逆时针顺序为正面
        polygonMode.setCulling(PolygonMode.CULL_BACK); // 不画背面,如果为CULL_NONE只两面都画
        polygonMode.setShading(PolygonMode.SHADE_SMOOTH); //设置投影模式为光滑模式,也可以设置为平面模式SHADE_FLAT
        appearance.setPolygonMode(polygonMode);
        
        //setAppearance(int index, Appearance appearance)  Sets the Appearance for the specified submesh.
        mesh.setAppearance(0, appearance); // Set the appearance to the 3D object
        return mesh;
    }

}

 

运行效果如下:

分享到:
评论

相关推荐

    M3G API Docs

    2. **对象模型**:M3G API提供了一系列的对象,如World、Transform、Appearance、Image等,它们代表了3D图形的不同方面,如世界坐标系、物体变换、表面外观和图像纹理。 3. **3D几何**:掌握如何使用M3G API创建和...

    M3G Viewer:查看M3G文件内容-开源

    M3G查看器是一个独立的应用程序,用于查看3D图形文件格式的内容,该格式是对移动3D图形API(M3G)的补充。

    J2ME_M3G_API.rar_M3G API_j2me 3d_j2me m_jsr 184 api c_m3g a

    **J2ME_M3G_API** 是一个与Java 2 Micro Edition(J2ME)相关的压缩包,专注于M3G(Mobile 3D Graphics)API的文档。M3G API是J2ME平台上的一个标准,它允许开发者创建在移动设备上运行的3D图形应用程序。JSR 184...

    M3G 的快速模式编程

    【M3G 快速模式编程】:M3G,全称Mobile 3D Graphics API,是基于JSR 184规范定义的一种为移动设备提供标准3D图形功能的API。它分为快速模式和保留模式。快速模式专注于单个3D对象的渲染,适合进行低级别的3D操作,...

    tk_m3gtk_v4_5.zip_m3g_tk_m3g_tk_m3gtk_v4_5

    tk_m3gtk_v4_5.zip是一款专为M3G文件设计的查看器,它为Java J2ME开发者提供了一个必备的工具,使得在保留模式下处理3D图形变得更加便捷和高效。 M3G文件格式是基于OpenGL ES的,但在资源有限的移动设备上进行了...

    jsr_184_midlet.rar_DEMO_jsr184_jsr184-m3g.jar_m3g

    4. **m3g**:这是M3G文件格式的提及,它是JSR 184中定义的一种专为移动设备优化的3D模型格式。这种格式旨在减少存储空间和内存需求,同时保持足够的图形质量,以适应资源有限的移动设备。 在DEMO程序中,`jsr_184_...

    M3M0:M3m0工具:crossed_swords:网站漏洞扫描程序和自动利用程序

    M3M0渗透测试工具 M3m0工具 :crossed_swords: 网站漏洞扫描程序和自动浏览器您可以使用此工具通过在网站中找到漏洞来检查安全性,也可以使用此工具来获取Shell | 污损| cPanels | 资料库 M3M0 :laptop:M3m0工具 :...

    Mobile 3D Graphics with OpenGL ES and M3G

    《Mobile 3D Graphics with OpenGL ES and M3G》是一本深入探讨移动设备上3D图形编程的专业书籍,主要关注于OpenGL ES和M3G这两个技术。OpenGL ES(OpenGL for Embedded Systems)是OpenGL的轻量级版本,专为嵌入式...

    m3u8在线下载工具-m3u8视频在线提取工具

    其中,M3U8是一种广泛用于流媒体传输的文件格式,尤其在高清视频领域。然而,直接下载M3U8格式的视频并不像下载普通文件那样简单。这就引出了我们的主角——,一个轻量级、高效的在线工具,帮助用户轻松地将M3U8视频...

    宝箧印塔模型设计m3g

    本文将深入探讨如何利用Java M3G(Mobile 3D Graphics)技术设计并实现“宝箧印塔”这一具有文化特色的三维模型。Java M3G是JSR 184(Mobile 3D Graphics API)的一部分,旨在为移动设备提供高效、轻量级的三维图形...

    一款纯静态的M3U8播放器页面

    源码说明:一个纯静态的M3U8播放器页面,可以直接把M3U8的网址填进去进行播放,超级方便。 部署方法: 可以使用宝塔面板来部署 1,打开宝塔面板,添加一个网站 2,把压缩包上传到站点跟目录,然后解压 3,解压以后...

    M3G2FBX_neko_

    "M3G2FBX_neko_" 提供了解决这一问题的解决方案,它是一款专为游戏设计的模型转换工具,主要功能是将M3G格式的模型转换为FBX格式。M3G是一种由Java 3D API支持的3D模型格式,而FBX则是Autodesk的通用3D模型交换格式...

    m3u8资源下载

    在IT行业中,m3u8是一种常见的多媒体播放列表格式,主要应用于流媒体服务,如在线视频和音频。这种格式基于HTTP Live Streaming (HLS) 协议,由Apple公司开发,目的是实现不同网络条件下的自适应流媒体播放。m3u8...

    LoaderM3G.rar_j2me loaderm_m3g_手机加载_手机游戏加载

    LoaderM3G.rar是一个关于J2ME(Java 2 Micro Edition)平台的资源包,主要涉及的是3D手机游戏开发中的M3G文件加载技术。M3G(Mobile 3D Graphics)是J2ME中用于实现移动设备上的3D图形渲染的规范,它为在功能受限的...

    M3u Parser:m3u和m3u8解析器-开源

    通常,解析器会提供一个解析方法,接收M3U或M3U8文件的输入流,返回一个包含所有媒体URL的列表。例如: ```java M3uParser parser = new M3uParser(); List&lt;String&gt; playlistEntries = parser.parse(inputStream); ...

    [JSR-184][3D编程指南]Part V: Heightmap terrain rendering using M3G

    这篇【3D编程指南】的第五部分主要探讨的是使用M3G来实现地形渲染,特别是基于高度图(Heightmap)的地形渲染技术。 首先,理解高度图的概念至关重要。高度图是一种2D图像,其中每个像素的灰度值代表一个3D网格中的...

    Java M3G相关类.rar

    `M3GCanvas.java` 是一个关键的类,它扩展了Java的`Canvas`类,提供了与M3G交互的画布。在Java ME中,`Canvas`类是用户界面的基础,允许程序员创建自定义的图形显示。`M3GCanvas`通常会处理所有的渲染工作,包括3D...

    M3U8Loader:M3U8装载机

    M3U8Loader是一个专为处理M3U8格式文件的工具,主要功能是加载M3U8清单并将其内容组合成一个可播放的MP4文件。M3U8是一种基于HTTP的流媒体协议,广泛应用于在线视频传输,特别是在 HLS(HTTP Live Streaming)系统中...

Global site tag (gtag.js) - Google Analytics