`
micheal19840929
  • 浏览: 163157 次
  • 性别: 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)的补充。

    M3G 的快速模式编程

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

    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的网址填进去进行播放,超级方便。 部署方法: 可以使用宝塔面板来部署 1,打开宝塔面板,添加一个网站 2,把压缩包上传到站点跟目录,然后解压 3,解压以后...

    宝箧印塔模型设计m3g

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

    m3u8资源下载

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

    [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)系统中...

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

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

    tk_m3gtk_v4_5.zip_m3g_tk_m3g_tk_m3gtk_v4_5

    tk_m3gtk_v4_5.zip tk_m3gtk_v4_5.zip 一款M3G文件查看器,Java J2ME中保留模式中必用。

    联想BIOS_L-IG41M3 V1.1 版本:DMKT05AUS 新圆梦F208

    联想BIOS_L-IG41M3 V1.1 版本:DMKT05AUS 新圆梦F208 原机备份 支持Q9400 支持8G内存 需两条4G双面内存 两个BIOS文件 AFUWIN备份的BIOS BIOS_Backup_TooKit_V2.0备份的BIOS

    jsr184+M3G+API.rar

    【标题】"jsr184+M3G+API.rar" 涉及到的主要技术是Java Mobile 3D Graphics API(JSR 184)和M3G(Mobile 3D Graphics)标准,以及相关的API接口。JSR 184是Java Community Process发布的一个规范,目的是为Java ME...

    m3u8Download:java下载m3u8视频,解密并合并ts java download m3u8 video,decrypt & merge ts slice

    m3u8Dowloadjava下载m3u8视频,解密并合并tsm3u8链接获取方式以及代码分析请参见:准备JDK:1.8开发环境:IntelliJ IDEA用于解密的jar包:bcprov-jdk16-139.jar了解一些m3u8标签作用...M3u8DownloadFactory.getInstance...

    M3U8VideoDownloadDemo:m3u8视频下载

    本篇文章将深入探讨如何使用Objective-C编写一个M3U8视频下载的演示项目,帮助开发者理解其核心原理与实现步骤。 首先,我们需要了解M3U8的基本概念。M3U8是一个基于HTTP的文件格式,用于列出多媒体文件的地址,...

    联想 L-IG41M3 V1.1_主板BIOS

    总结来说,【联想 L-IG41M3 V1.1_主板BIOS】是一个针对联想L-IG41M3主板的BIOS升级,旨在提供更稳定、高效和安全的系统环境。通过正确地应用这个更新,用户可以享受到更优化的硬件性能和兼容性,同时避免由旧版BIOS...

    M3GToolkit-0.5.0

    M3GToolkit-0.5.0是一款专为J2ME(Java 2 Micro Edition)开发者设计的实用工具,用于检查和分析M3G(Mobile 3D Graphics)文件。在移动设备上的3D图形开发中,M3G是一种重要的标准,它允许开发者创建出具有高质量3D...

    m3u8文件视频播放(ckplayer)完整代码

    在描述中提到,尽管CKPlayer官方文档可能没有特别详尽地介绍如何配置M3U8播放,但只需要添加一个特定的SWF文件——`m3u8.swf`,就可以让CKPlayer具备播放M3U8文件的能力。 为了播放M3U8视频,你需要按照以下步骤...

Global site tag (gtag.js) - Google Analytics