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

📄 texcanvas.java

📁 一个j2me的3d游戏实例
💻 JAVA
字号:
package testTexture;

import java.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.m3g.*;

public class texCanvas extends Canvas implements Runnable{
	
	private Graphics3D _graphics3d = null;
    private VertexArray vertexArray,texArray;
    private VertexBuffer vertexBuffer ;
    private IndexBuffer triangles;
    private Appearance appearance;
    private Texture2D texture;
    private Mesh mesh;
    private Light light;    
    private Transform transform = new Transform();
    private World world = new World();
    private Camera camera = null;
	
	public texCanvas()
	{		
		_graphics3d = Graphics3D.getInstance();
		
        byte vertrices[] = new byte[] {
        		-1, -1, 0,
                 1, -1, 0,
                 1, 1, 0,
                -1, 1, 0};

        byte texCoords[] = new byte[] {
        		0, (byte)1,
                (byte)1, (byte)1,
                (byte)1, 0,
                 0, 0};
        
        vertexArray = new VertexArray(vertrices.length/3, 3, 1);
        vertexArray.set(0, vertrices.length/3, vertrices);
                
        texArray = new VertexArray(texCoords.length/2,2,1);
        texArray.set(0,texCoords.length/2,texCoords);
        
        vertexBuffer = new VertexBuffer();
        vertexBuffer.setPositions(vertexArray, 1.0f, null);
        vertexBuffer.setTexCoords(0, texArray, 3, null);
        
        int indices[] = new int[] {0, 1, 3, 2};
        int[] stripLengths = new int[] {4};
        
        triangles = new TriangleStripArray(indices, stripLengths);

        appearance = new Appearance();
        PolygonMode pm = new PolygonMode();
        appearance.setPolygonMode(pm);
                
        try
        {
        	texture = createTexture2D("/room.png");
            appearance.setTexture(0, texture);
        }
        catch(Exception e)
        {
            System.out.println("Failed to create texture");
        }
        
        mesh = new Mesh(vertexBuffer, triangles, appearance);
        
        camera = new Camera();
        camera.setTranslation(0,0,5);
        
        float aspect = (float) getWidth() / (float) getHeight();
        camera.setPerspective(30.0f, aspect, 1.0f, 1000.0f);
        Transform camTransform = new Transform();
        camTransform.postTranslate(0.0f, 0.0f, 5.0f);
        
        light = new Light();
        light.setMode(Light.AMBIENT);
        light.setColor(0xffffff);         // 设置为白色光线t
        light.setIntensity(1.25f);
        
        world.addChild(mesh);
        world.addChild(camera);
        world.setActiveCamera(camera);
        world.addChild(light);
        Thread thread = new Thread(this);
        thread.start();
	}
	
    public static Texture2D createTexture2D(String texFilename) throws IOException {

        Image texImage = Image.createImage(texFilename);
        Texture2D theTexture = new Texture2D(new Image2D(Image2D.RGBA, texImage));
        
        theTexture.setBlending(Texture2D.FUNC_MODULATE);
        theTexture.setFiltering(Texture2D.FILTER_NEAREST, Texture2D.FILTER_NEAREST);
        //theTexture.setWrapping(Texture2D.WRAP_CLAMP, Texture2D.WRAP_CLAMP);
        //theTexture.setWrapping(Texture2D.WRAP_REPEAT, Texture2D.WRAP_REPEAT);
        //theTexture.setWrapping(Texture2D.WRAP_CLAMP, Texture2D.WRAP_REPEAT);
        theTexture.setWrapping(Texture2D.WRAP_REPEAT, Texture2D.WRAP_CLAMP);
        
        return theTexture;
    }

	protected void paint(Graphics g) {
		
		_graphics3d.bindTarget(g);
		_graphics3d.clear(null);
		_graphics3d.render(world);
		_graphics3d.releaseTarget();		

	}

	public void run() {
		
		while(true)
		{
			try
			{
				Thread.sleep(40);
			}catch(Exception e)
			{
				e.printStackTrace();
			}
			
			repaint();
		}
		
	}

}

⌨️ 快捷键说明

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