⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 model.java

📁 <j2me 开发精解> 詹建光著 里所有的源码。对J2me的开发相当有帮助
💻 JAVA
字号:
package com.j2medev.chapter8.m3g;

import java.io.IOException;

import javax.microedition.m3g.*;
import javax.microedition.lcdui.Image;

public class Model {
    
    private Image target_img;
    private Appearance target_app;
    
    public Mesh creatArrow(){
        short verts[]=new short[]{	0,0,0,
                -5,-5,30,    5,-5,30,   -5,5,30,    5,5,30,
                -5,-5,970,   5,-5,970,  -5,5,970,   5,5,970
        };
        short norms[]=new short[]{0,-127,0,
                -127,-127,-127,    127,-127,-127,    -127,127,-127,   127,127,-127,
                -127,-127,127,     127,-127,127,     -127,127,127,    127,127,127
                
        };
        
        int index[]=new int[]{
            0,3,4,
                    0,4,2,
                    0,2,1,
                    0,1,3,
                    7,8,3,4,
                    8,6,4,2,
                    6,5,2,1,
                    5,7,1,3,
                    5,6,7,8
                    
        };
        int length[]=new int[]{3,3,3,3,4,4,4,4,4};
        
        VertexArray vertexArray=new VertexArray(verts.length/3,3,2);
        vertexArray.set(0,verts.length/3,verts);
        VertexArray normls=new VertexArray(norms.length/3,3,2);
        normls.set(0,norms.length/3,norms);
        
        TriangleStripArray triangle=new TriangleStripArray(index,length);
        
        VertexBuffer verBuffer=new VertexBuffer();
        verBuffer.setPositions(vertexArray,1f/100f,null);
        verBuffer.setNormals(normls);
        
        Mesh arrow=new Mesh(verBuffer,triangle,null);
        
        Material material=new Material();
        material.setVertexColorTrackingEnable(true);
        material.setColor(Material.DIFFUSE,0xffffff00);
        material.setColor(Material.AMBIENT,0xffffff00);
        material.setColor(Material.EMISSIVE,0xffffff00);
        material.setColor(Material.SPECULAR,0xffffff00);
        material.setShininess(100f);
        
        PolygonMode polygonMode=new PolygonMode();
        polygonMode.setCulling(PolygonMode.CULL_BACK);
        polygonMode.setShading(PolygonMode.SHADE_SMOOTH);
        polygonMode.setPerspectiveCorrectionEnable(true);
        Appearance app=new Appearance();
        app.setMaterial(material);
        app.setPolygonMode(polygonMode);
        
        arrow.setAppearance(0,app);
        return arrow;
    }
    
    public Mesh creatBox(Image target){
        short verts[]=new short[]{
            -5,-5,1,    5,-5,1,   -5,5,1,    5,5,1,         //front
                    5,-5,-1,   -5,-5,-1,  5,5,-1,   -5,5,-1,        //back
                    5,-5,1,    5,-5,-1,   5,5,1,    5,5,-1,         //right
                    -5,-5,-1,   -5,-5,1,    -5,5,1,   -5,5,-1,        //left
                    -5,5,1,    5,5,1,    -5,5,-1,    5,5,-1,        //top
                    5,-5,1,    -5,-5,1,  5,-5,-1,    -5,-5,-1       //botton
        };
        byte terx[]=new byte[]{
            0,32,         32,32,      0,0,       32,0,
                    0,1,         1,1,      0,0,       1,0,
                    0,1,         1,1,      0,0,       1,0,
                    0,1,         1,1,      0,0,       1,0,
                    0,1,         1,1,      0,0,       1,0,
                    0,1,         1,1,      0,0,       1,0
        };
        
        int index[]=new int[]{0,1,2,3,
                4,5,6,7,
                8,9,10,11,
                12,13,14,15,
                16,17,18,19,
                20,21,22,23
        };
        int length[]=new int[]{4,4,4,4,4,4};
        
        VertexArray verArr=new VertexArray(verts.length/3,3,2);
        verArr.set(0,verts.length/3,verts);
        VertexArray terxArr=new VertexArray(terx.length/2,2,1);
        terxArr.set(0,terx.length/2,terx);
        
        VertexBuffer buffer=new VertexBuffer();
        buffer.setPositions(verArr,1.0f,null);
        buffer.setTexCoords(0,terxArr,(1f/32f),null);
        TriangleStripArray triangle=new TriangleStripArray(index,length);
        Mesh box=new Mesh(buffer,triangle,null);
        Appearance app=new Appearance();
        PolygonMode poly=new PolygonMode();
        poly.setCulling(PolygonMode.CULL_BACK);
        poly.setShading(PolygonMode.SHADE_FLAT);
        poly.setPerspectiveCorrectionEnable(true);
        app.setPolygonMode(poly);
        Texture2D texture=null;
        texture=new Texture2D(new Image2D(Image2D.RGB,target));
        app.setTexture(0,texture);
        box.setAppearance(0,app);
        
        return box;
    }
    
    public Mesh creatPlane(){
        Mesh plane;
        
        short verts[]=new short[]{
            -100,0,100,     100,0,100,     -100,0,-100,    100,0,-100
        };
        byte terx[]=new byte[]{
            0,8,            8,8,          0,0,          8,0
        };
        int index[]=new int[]{0,1,2,3};
        int length[]=new int[]{4};
        
        VertexArray verArr=new VertexArray(verts.length/3,3,2);
        verArr.set(0,verts.length/3,verts);
        VertexArray terxArr=new VertexArray(terx.length/2,2,1);
        terxArr.set(0,terx.length/2,terx);
        
        VertexBuffer buffer=new VertexBuffer();
        buffer.setPositions(verArr,1.0f,null);
        buffer.setTexCoords(0,terxArr,1f,null);
        
        TriangleStripArray triangle=new TriangleStripArray(index,length);
        
        plane=new Mesh(buffer,triangle,null);
        
        Appearance app=new Appearance();
        
        PolygonMode poly=new PolygonMode();
        poly.setCulling(PolygonMode.CULL_BACK);
        poly.setShading(PolygonMode.SHADE_FLAT);
        poly.setPerspectiveCorrectionEnable(true);
        app.setPolygonMode(poly);
        Texture2D texture=null;
        try{
            texture=new Texture2D(new Image2D(Image2D.RGB,Image.createImage("/roof1.png")));
        }catch(IOException ioe){
            ioe.printStackTrace();
        }
        app.setTexture(0,texture);
        plane.setAppearance(0,app);
        return plane;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -